Method and device for realizing Elasticsearch cluster strong consistency synchronization

By realizing real-time synchronization of indexes and historical data recovery between Elasticsearch clusters, it solves the problem of high failure recovery and migration costs of native Elasticsearch in the data center, ensures the consistency and high availability of master-slave index data, supports seamless migration, and reduces R&D and application costs.

CN118643097AActive Publication Date: 2024-09-13CHINA UNITECHS
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202410775431.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-06-17
Publication Date
2024-09-13
Estimated Expiration
2044-06-17

AI Technical Summary

Technical Problem

Native Elasticsearch has difficulty in recovering in the face of data center-level failures and the cost of data migration between clusters is high. It cannot guarantee the strong consistency of master-slave index data, cannot support historical data synchronization, and cannot meet the needs of enterprises for high availability and rapid disaster recovery.

Method used

By copying the index in the remote cluster to the local cluster, real-time synchronization of master-slave data and historical data recovery are achieved. The master-slave read mechanism is used to regularly check data differences and automatically recover to ensure the consistency and reliability of master-slave index data. , supports seamless active/standby switching and inter-cluster migration.

Benefits of technology

It achieves consistent replication of master-slave index data between Elasticsearch clusters, ensures high reliability and fast disaster recovery, supports seamless migration, and reduces R&D and application costs.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118643097A
    Figure CN118643097A
Patent Text Reader

Abstract

The invention discloses a method and device for realizing Elasticsearch cluster strong consistency synchronization, and the method comprises the steps: writing data into a main fragment of a main index to which a document belongs, after the data is successfully written, sending a writing request to a node where a main fragment of a slave index to which the document belongs is located, and meanwhile, taking the main fragment of the slave index as a remote copy of the main fragment of the main index, writing the data into the remote copy to realize real-time data synchronization; the main fragment of the main index sends and stores the segment file in the disk to the remote copy, and the storage information of the actual data in the segment file is analyzed; and the main fragment of the main index sends a translog which is not stored in a disk to the remote copy, so that the remote copy is recovered to a data state consistent with that of the main fragment, and historical data recovery is realized. According to the method and the device, the index in the remote cluster is copied to the local cluster, and high consistency of master and slave data is ensured, so that high availability and rapid disaster recovery capability are provided, and meanwhile, uninterrupted inter-cluster index migration is realized.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of information technology, and in particular to a method and device for realizing strong consistency synchronization of an Elasticsearch cluster. Background Art

[0002] Native Elasticsearch provides high availability to ensure the reliability of data within the cluster. However, for users who have higher requirements for reliability, this high availability is not enough. Native Elasticsearch has some problems, such as the inability to quickly recover from data center-level failures and the high cost of migrating data between clusters, which requires the use of external tools to perform multiple complex operations.

[0003] Currently, enterprises often adopt the method of writing data to different clusters at the same time to meet the high availability requirements between clusters. However, this method is highly dependent, does not support historical data synchronization, and cannot guarantee the strong consistency of master-slave index data. Elasticsearch officially introduced the inter-cluster data replication function in version 6.7.0, but this function needs to be paid and can only guarantee the final consistency of master-slave index data. The core business of many enterprises requires strong consistency of master-slave index data. Therefore, this function is still not enough to meet the needs of enterprises. Summary of the invention

[0004] In view of the above situation, the present invention provides a method and device for realizing strong consistency synchronization of Elasticsearch clusters, aiming to realize the function of copying the index in the remote cluster to the local cluster and ensure the strong consistency of the master-slave data, thereby providing high availability and rapid disaster recovery capabilities, while realizing uninterrupted index migration between clusters.

[0005] To achieve the above object, the present invention adopts the following technical solutions:

[0006] In one embodiment of the present invention, a method for realizing strong consistency synchronization of an Elasticsearch cluster is proposed, and the method includes:

[0007] Write the data to the primary shard of the primary index to which the document belongs. After the data is written successfully, send the write request to the node where the primary shard of the secondary index to which the document belongs is located. At the same time, treat the primary shard of the secondary index as a remote copy of the primary shard of the primary index, and write the data to the remote copy to achieve real-time data synchronization;

[0008] The primary shard of the primary index sends the segment files that have been stored on disk to the remote replica, and parses the storage information of the actual data in the segment files; the primary shard of the primary index sends the translog logs that have not been stored on disk to the remote replica, so as to restore the remote replica to a data state consistent with the primary shard, thereby realizing historical data recovery.

[0009] Furthermore, the association between the master and slave indexes is defined in the Elasticsearch cluster state to establish a link between the Elasticsearch clusters, and is updated according to the Elasticsearch cluster state change event, thereby triggering real-time data synchronization.

[0010] Furthermore, the connection status between Elasticsearch clusters and the data differences between the master and slave indexes are checked regularly; when the data difference between the master and slave indexes is too large or the connection is abnormal, the master cluster will actively disconnect the connection between the Elasticsearch clusters and notify the slave index to perform the difference data recovery operation.

[0011] Furthermore, the historical data recovery of the slave index is triggered by the RPC call of the main cluster, and only the primary shard data of the slave index needs to be restored;

[0012] After the historical data is restored, the primary shard of the slave index is added to the remote replica group and starts receiving write requests for the primary index.

[0013] In one embodiment of the present invention, a device for realizing strong consistency synchronization of an Elasticsearch cluster is also proposed, and the device includes:

[0014] The Elasticsearch write module is used to write data to the primary shard of the primary index to which the document belongs. After the data is written successfully, the write request is sent to the node where the primary shard of the secondary index to which the document belongs is located. At the same time, the primary shard of the secondary index is regarded as a remote copy of the primary shard of the primary index, and the data is written to the remote copy to achieve real-time data synchronization;

[0015] The Elasticsearch secondary shard recovery module is used for the primary shard of the primary index to send the segment files that have been stored on disk to the remote replica, and parse the storage information of the actual data in the segment files; the primary shard of the primary index sends the translog logs that have not been stored on disk to the remote replica, so as to restore the remote replica to a data state consistent with the primary shard, thereby realizing historical data recovery.

[0016] Furthermore, the association between the master and slave indexes is defined in the Elasticsearch cluster state to establish a link between the Elasticsearch clusters, and is updated according to the Elasticsearch cluster state change event, thereby triggering real-time data synchronization.

[0017] Furthermore, the connection status between Elasticsearch clusters and the data differences between the master and slave indexes are checked regularly; when the data difference between the master and slave indexes is too large or the connection is abnormal, the master cluster will actively disconnect the connection between the Elasticsearch clusters and notify the slave index to perform the difference data recovery operation.

[0018] Furthermore, the historical data recovery of the slave index is triggered by the RPC call of the main cluster, and only the primary shard data of the slave index needs to be restored;

[0019] After the historical data is restored, the primary shard of the slave index is added to the remote replica group and starts receiving write requests for the primary index.

[0020] In one embodiment of the present invention, a computer device is also proposed, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, the aforementioned method for realizing strong consistency synchronization of an Elasticsearch cluster is implemented.

[0021] In one embodiment of the present invention, a computer-readable storage medium is further proposed, which stores a computer program for executing a method for implementing strong consistency synchronization of an Elasticsearch cluster.

[0022] Beneficial effects:

[0023] 1. The present invention realizes the consistent replication of master-slave index data between Elasticsearch clusters, ensures the high consistency of master-slave index data, and meets the high reliability requirements of the core business of the enterprise.

[0024] 2. The present invention draws on the replica recovery mechanism to achieve efficient historical data recovery, and adopts the master-write-slave-read mechanism to ensure the timeliness of real-time data synchronization.

[0025] 3. The present invention performs data verification and automatic recovery regularly, which fully guarantees the reliability of master-slave index data.

[0026] 4. The present invention supports seamless master-slave switching and inter-cluster migration, provides continuous service capabilities, and shortens service migration time.

[0027] 5. The present invention has low source code intrusion, low development difficulty, strong versatility, and reduces R&D and application costs. BRIEF DESCRIPTION OF THE DRAWINGS

[0028] Figure 1 It is a writing flow chart of Elasticsearch of the present invention;

[0029] Figure 2 It is a secondary shard recovery flow chart of Elasticsearch of the present invention;

[0030] Figure 3 This is the core concept diagram of the present invention for achieving strong consistency synchronization of the Elasticsearch cluster;

[0031] Figure 4 This is a flow chart of a method for realizing strong consistency synchronization of an Elasticsearch cluster according to the present invention;

[0032] Figure 5 This is a flowchart of establishing a link between Elasticsearch clusters according to an embodiment of the present invention;

[0033] Figure 6 is a flowchart of historical data recovery according to an embodiment of the present invention;

[0034] Figure 7 is a real-time data synchronization flow chart of an embodiment of the present invention;

[0035] Figure 8 is a flow chart of master-slave index data quality verification according to an embodiment of the present invention;

[0036] Fig. 9 It is a schematic diagram of the structure of the device for realizing strong consistency synchronization of Elasticsearch cluster in the present invention;

[0037] Fig.10 It is a schematic diagram of the computer device structure of the present invention. DETAILED DESCRIPTION

[0038] The principles and spirit of the present invention will be described below with reference to several exemplary embodiments. It should be understood that these embodiments are provided only to enable those skilled in the art to better understand and implement the present invention, and are not intended to limit the scope of the present invention in any way. On the contrary, these embodiments are provided to make the present disclosure more thorough and complete, and to fully convey the scope of the present disclosure to those skilled in the art.

[0039] Those skilled in the art will appreciate that the embodiments of the present invention may be implemented as a system, device, apparatus, method or computer program product. Therefore, the present disclosure may be specifically implemented in the following forms, namely: complete hardware, complete software (including firmware, resident software, microcode, etc.), or a combination of hardware and software.

[0040] According to an embodiment of the present invention, a method for realizing strong consistency synchronization of Elasticsearch clusters is proposed, which aims to realize the function of copying the index in the remote cluster to the local cluster and ensure the strong consistency of the master-slave data, thereby providing high availability and fast disaster recovery capabilities, while realizing uninterrupted inter-cluster index migration. When performing the replication process, the following two aspects need to be considered:

[0041] 1. Real-time data synchronization and historical data synchronization:

[0042] (1) Real-time data synchronization refers to copying the latest data in the remote cluster to the local cluster in real time to ensure that the data between the two clusters remains synchronized.

[0043] (2) Historical data synchronization refers to copying historical data in the remote cluster to the local cluster to ensure that the local cluster has a complete copy of the data.

[0044] 2. Real-time data synchronization depends on the writing mechanism of Elasticsearch, and data synchronization depends on the replica recovery mechanism of Elasticsearch:

[0045] (1) During real-time data synchronization, the present invention relies on the writing mechanism of Elasticsearch to ensure that data can be synchronized to the local cluster in a timely manner after being written to the remote cluster.

[0046] (2) During the historical data synchronization process, the present invention relies on the replica recovery mechanism of Elasticsearch to ensure that data can be restored in the event of failure or damage.

[0047] The scheme of the present invention and the implementation details are based on the Elasticsearch write and recovery process. The following is a brief overview of the two key processes of Elasticsearch.

[0048] 1. Basic writing process

[0049] The writing process of Elasticsearch follows the following steps: First, write the data to the primary shard (P) of the primary index. After the writing of the primary shard (P) is completed, the request will be forwarded to the replica (R) in parallel. After the replica (R) completes the processing, the writing result will be returned to the primary shard. Note: In this invention, Si represents the specific shard of the Elasticsearch primary index, P represents the primary shard, and R represents the replica. The specific process is as follows Figure 1As shown: the client initiates a write request, the Master node NODE1 determines that the document belongs to shard 0 of the primary index based on the write request ID, and routes the write request to the NODE3 node where the primary shard P0 of shard 0 is located. After the primary shard P0 of the NODE3 node is written successfully, the write request is sent to the replica R0. When the writing of replica R0 is completed, the write result is returned to the client.

[0050] 2. Copy recovery process

[0051] To ensure the consistency of the data replica, the replica needs to be restored to a state consistent with the primary shard in order to provide external services normally. Elasticsearch's replica recovery is based on the shard level, including the primary shard recovery process and the replica recovery process. Since the replica recovery process of Elasticsearch is extremely complex, and the present invention only involves the data recovery process related to the replica recovery process, only a brief introduction to the replica recovery process is given.

[0052] The goal of replica recovery is to restore local data to a state consistent with the primary shard. The main process is divided into two stages:

[0053] Phase 1: The primary shard sends the segment file to the replica. In this phase, the primary shard sends the stored segment file to the replica and parses the segment file containing the specific data. The segment file contains the storage information of the actual data. The segment file is the smallest unit of index physical storage and query in Elasticsearch. It contains all the document data, index information, document ID, etc. belonging to the segment file.

[0054] Phase 2: The primary shard sends the translog log to the replica. In this phase, the primary shard sends the data that has not been written to the disk to the replica, which is similar to the WAL Log of MySQL, that is, the translog log. This log records the data operations that have not yet been persisted. The translog log is an implementation of the write-before log (WAL) in Elasticsearch, which is used to record the latest data changes that have not yet been persisted for data recovery and data consistency.

[0055] After completing the above two stages, it is completed. Through these steps, the replica can reach a data state consistent with the primary shard, ensuring data consistency and availability.

[0056] like Figure 2 As shown in the figure, the specific copy recovery process is as follows:

[0057] (1) The replica recovery is triggered by the cluster status change event, and then INIT initializes to check the local replica index data status.

[0058] (2) After initialization, the recovery process begins and INDEX obtains the index file from the primary shard.

[0059] (3) The primary shard sends the persisted segment file to the replica. The replica writes the received segment file to local storage.

[0060] (4) After writing to the local computer, before receiving the translog operation log, VERIFY_INDEX is required to verify the index integrity and compare the file lists of the primary shard and the replica shard to detect whether any files are missing. If there are any missing files, the primary shard will be requested to resend the missing files.

[0061] (5) During the VERIFY_INDEX verification phase, the engine of the replica shard will be started. Only after the engine is started can the changes in the operation log be applied.

[0062] (6) At the same time, the replica shard is added to the replica group of the primary shard. After joining the replica group, the primary shard will send the data changes in the operation log to the replica shard.

[0063] If you do not join the replica group, the primary shard will not copy the operation log to the shard.

[0064] (7) TRANSLOG obtains the operation log from the primary shard.

[0065] The primary shard sends a non-persistent translog operation log to the replica. The replica reads the log and replays the operation, applying it to local storage.

[0066] (8) FINALIZE final processing

[0067] Set the replica shard to an available state so that it can serve normally. Add the replica shard to the normal replica group of the primary shard.

[0068] 3. Design Concept

[0069] The core concept of the present invention is to treat the shard corresponding to the slave index as a remote copy of the shard corresponding to the master index. Figure 3 As shown, the primary shard S0(P) of the slave index shard0 is considered a remote copy of the primary shard S0(P) of the primary index shard0.

[0070] The remote replica in the present invention is an extension of the Elasticsearch data replica model. It uses the primary shard of the primary index to save the relevant metadata of the remote replica, and draws on Microsoft's PacificA algorithm in its implementation. This design concept is in line with the Elasticsearch data replica model, and can fully reuse the Elasticsearch replica logic, thereby reducing the development difficulty and reducing the intrusion into the open source Elasticsearch core. The PacificA algorithm is a distributed strong consistency replication algorithm proposed by Microsoft in 2013. The Elasticsearch data replica model refers to the Elasticsearch replica design method or pattern. The core concepts of the Elasticsearch data replica model are primary shards and replicas. The primary shard is the original copy of each index shard, which is responsible for processing all write operations and query requests; the replica is a copy of the primary shard, which is used to provide redundancy and load balancing; the replica can be distributed on different nodes, thereby achieving high availability and fault tolerance of data.

[0071] Table 1 below lists some core terms of the algorithm and their correspondence with the Elasticsearch data replication model.

[0072] Table 1

[0073]

[0074] The present invention includes the following contents:

[0075] (1) Inter-cluster link construction: Define inter-cluster links in Elasticsearch through custom metadata, including master index, slave index, and slave cluster information. Update the metadata of inter-cluster links to the Elasticsearch cluster status using the API provided by Elasticsearch, and update according to Elasticsearch cluster status change events, thereby triggering real-time data synchronization.

[0076] (2) Historical data recovery: Trigger the replica recovery of the slave index through the RPC call of the master cluster. Only the primary shard of the slave index is restored to speed up the recovery. During the migration process, the primary shard will be locked to prevent data migration. After the recovery is complete, the primary shard will be added to the remote replica group from the slave index.

[0077] (3) Real-time data synchronization: After the historical data synchronization is completed, the incremental data is synchronized to the slave index after the primary shard is successfully written, and the data is forwarded to the replica and remote replica. This solution can ensure strong data consistency. After the primary shard is successfully written, the data is synchronously forwarded to the slave index to ensure strong consistency.

[0078] (4) Data quality verification: Regularly check the link status between Elasticsearch clusters. If a problem is found, the link between clusters will be automatically disconnected and the slave index will be notified to perform recovery operations. The master node will check the metadata, and the data node will determine whether the primary and secondary shards need to be restored.

[0079] The design of the present invention first involves defining the association between the master and slave indexes in the Elasticsearch cluster state to establish the Elasticsearch inter-cluster link, which is the basis for the entire invention to achieve data synchronization. Once the Elasticsearch inter-cluster link is established, the historical data needs to be restored next, by copying the data sharding level of the master index to the slave index, ensuring that the data state of the slave index is consistent with the master index, laying the foundation for subsequent real-time data synchronization. After the historical data recovery is completed, the real-time data synchronization mechanism is started, and the strong consistency of data between the master and slave indexes is achieved by synchronously forwarding the write request of the master index to the slave index. At the same time, data quality verification is performed regularly to check whether there is a data difference between the master and slave indexes. If a problem is found, the Elasticsearch inter-cluster link is disconnected and a new historical data recovery is triggered to ensure the reliability of the data. Through the organic coordination of these links, the present invention realizes the strong consistency replication of data between different Elasticsearch clusters, which can be applied to the purposes of high availability and no downtime migration between Elasticsearch clusters.

[0080] It should be noted that, although the operations of the method of the present invention are described in a specific order in the above embodiments and the accompanying drawings, this does not require or imply that the operations must be performed in the specific order, or that all the operations shown must be performed to achieve the desired results. Additionally or alternatively, some steps may be omitted, multiple steps may be combined into one step, and / or one step may be decomposed into multiple steps.

[0081] In order to more clearly explain the above-mentioned method for realizing strong consistency synchronization of the Elasticsearch cluster, a specific embodiment is used for illustration below. However, it should be noted that this embodiment is only for better illustrating the present invention and does not constitute an improper limitation to the present invention.

[0082] Example:

[0083] The present invention relates to a method for realizing inter-cluster data replication capability. To realize this function, it is first necessary to clearly define the index template or index that needs to replicate data between Elasticsearch clusters, that is, to establish the Elasticsearch inter-cluster link of the present invention. Secondly, the slave index in the present invention acts as a remote copy, and it needs to be restored to a data state consistent with the master index so that it can provide services normally, that is, historical data recovery. After achieving consistency between the slave index and the master index, it is necessary to determine how to write data to the slave index when new data is added to the master index, that is, real-time data synchronization. After completing the above steps, the slave index will be able to provide services normally. In order to ensure the reliability of the data, it is also necessary to perform data quality verification of the master-slave index.

[0084] like Figure 4 As shown, the scheme design of the present invention includes the following four contents:

[0085] 1. Inter-cluster link construction

[0086] The Elasticsearch cluster is a system driven by cluster state. The core of the present invention is to establish Elasticsearch inter-cluster links by changing the Elasticsearch cluster state and apply the new Elasticsearch cluster state configuration on the corresponding nodes. Usually, enterprises use Elasticsearch in the form of index templates (a set of indexes with the same prefix). Therefore, when designing Elasticsearch inter-cluster links, it is necessary to support template-level and index-level Elasticsearch inter-cluster propagation.

[0087] The present invention implements the propagation of cluster meta information between Elasticsearch clusters by customizing the metaData field in the cluster state for Elasticsearch. Elasticsearch cluster links use a unified naming rule to distinguish templates and indexes. The main information includes:

[0088] Template inter-cluster links:

[0089]

[0090] Index inter-cluster links:

[0091]

[0092] "replica_cluster": "ClusterA", / / from the cluster name

[0093] "replication_state": true / / Inter-cluster link status

[0094] }

[0095] }

[0096] The Elasticsearch cluster provides an application programming interface (API) that can be used to save and manage the created inter-cluster link information. The present invention synchronizes the inter-cluster link meta information to the cluster state configuration of each cluster by adding access and update functions to the metaData field to the API. When the cluster state configuration changes, the system of the present invention detects this event and enters the data synchronization process, such as Figure 5 As shown:

[0097] When designing a data synchronization mechanism for inter-cluster links, pay attention to the following details:

[0098] (1) Considering the difficulty of development and the intrusion to the source code, the master index and the slave index use the same index name and index ID. When creating a slave index, the system will propagate the index ID corresponding to the master index to the slave cluster. This can prevent the slave index from generating a new inconsistent index ID when it is created.

[0099] (2) After the index is deleted, the Elasticsearch system will temporarily retain the data of the deleted index in the shard responsible for recovery. In order to prevent the failure of the secondary index reconstruction caused by this, the system of the present invention will first scan the shard responsible for recovery before creating the secondary index, and delete the index whose index ID is consistent with the primary index, so as to solve the problem of failure to rebuild after the secondary index is deleted.

[0100] (3) When a new inter-cluster link is created, the system will synchronize the mapping definition of the master index to the slave cluster. In the slave cluster, it will also create a slave index corresponding to the master index based on the propagated mapping definition. This ensures that the master and slave indexes of each cluster in the inter-cluster link are always consistent in mapping structure.

[0101] 2. Historical data recovery

[0102] The design of the historical data recovery solution draws on the Elasticsearch replica recovery mechanism. Specifically, data recovery of inter-cluster links is also performed at the shard level, and segment and translog replication is also required. Conditions for historical data recovery:

[0103] (1) When a new inter-cluster link is created, historical data needs to be restored based on the primary index.

[0104] (2) When the writing of the shard data of the slave index fails, the master index timing task will rebuild the inter-cluster link of the present invention.

[0105] like Figure 6As shown, the replica recovery process of the slave index as a remote replica is basically the same as that of Elasticsearch in terms of historical data recovery, and the main difference lies in the data recovery triggering condition in step 1 and the way to join the remote replica group in step 6. Since the replica recovery of Elasticsearch is driven by the cluster state change event, and the recovery of the slave index is between clusters, it relies on the RPC call of the main cluster to trigger the historical data recovery of the slave cluster. At the same time, the slave index only needs to recover its own primary shard data, while the replica recovery of the slave index depends on the Elasticsearch replica mechanism of the slave cluster. After the first phase (phase1) of the Elasticsearch replica is over, the replica starts the engine and adds the restored replica to the primary shard replica group, and starts to receive write requests from the primary shard. The recovery of the slave index is also similar. The primary shard of the slave index is used as a remote copy of the primary shard corresponding to the primary index. After the first phase (phase1) of the primary shard of the primary index is over, after the engine of the slave index is started, the corresponding primary shard of the slave index is added to the remote replica group, and write requests are started. After the historical data recovery of the slave index is completed, the slave index can normally receive the write request of the primary index, thereby greatly reducing the historical data recovery time of the inter-cluster link of the present invention.

[0106] The implementation of remote replicas draws on Microsoft's PacificA algorithm, which is to add a remote prepared list (prepared sequence for write operations) named preparedRemoteCopies (prepared remote replicas) list in the ReplicationGroup (replica group) class of the primary index to track remote replicas. When the primary shard needs to synchronize a write request to a remote replica, first add the remote replica node information to the preparedRemoteCopies list, and then call the syncWithRemoteCopies (synchronization method with remote replicas) method of ReplicationGroup to synchronize the write request to the remote replica over the network. When the remote replica data is synchronized, it is moved from the preparedRemoteCopies list to the official remoteCopies (remote replica) list. After that, the write request of the primary shard can be copied to both the local replica and the remote replica at the same time.

[0107] 3. Real-time data synchronization

[0108] The real-time data synchronization of the present invention refers to the process of instant copying of incremental data to the slave index after the historical data synchronization is completed. According to the writing principle of Elasticsearch, the write request is first sent to the primary shard, and then the write operation is synchronously forwarded to the replica. The present invention adopts a technical solution that immediately forwards the data copy to the local replica and the remote replica after the primary shard is successfully written to achieve real-time data synchronization. This solution can ensure strong consistency data synchronization.

[0109] like Figure 7 As shown, the real-time data synchronization strategy of the present invention is similar to the historical data recovery mechanism, and both adopt the technical means of forwarding write requests to remote copies. Specifically, when the primary shard receives a write request, while writing the data to the local copy, the write request must also be forwarded to the remote copy. By calling the synchronization remote copy method (syncWithRemoteCopies) of the replication group (Replication Group), the write request can be sent to the remote copy node through the network. After receiving the write request, the remote copy node writes the data to the shard file. After the remote copy is successfully written, a confirmation message will be sent to the primary shard node. It should be noted that the processing strategy when the remote copy fails to write. In Elasticsearch, the processing of copy write failure is to remove the faulty copy from the synchronization copy group and re-execute the data recovery process. The remote copy write failure processing strategy of the present invention is similar to Elasticsearch, which is to delete the faulty remote copy from the remote copy group of the primary shard, and the primary index shard will no longer forward the write request to the slave index, but rely on the regular inspection mechanism of the slave index to re-trigger the data recovery process.

[0110] 4. Master-slave index data quality verification

[0111] Data quality verification is a key step in ensuring the reliability of slave index data. It regularly compares the metadata information in the cluster status with the actual operating status of the inter-cluster link, and processes the inter-cluster link accordingly based on the verification results. When the data difference between the master and slave indexes is too large or the link is abnormal, the master cluster will actively disconnect the inter-cluster link and notify the slave index to perform the difference data recovery operation. In the Elasticsearch cluster, the master node is responsible for managing the cluster metadata. Therefore, if Figure 8 As shown, in the scheme of the present invention, the master node of the master cluster mainly completes the functions of generating metadata for inter-cluster links and verifying whether slave indexes exist; the data node (Data Node) of the master cluster is responsible for data storage, and its main function is to determine whether master-slave shards need data recovery, including comparison of the total amount of master-slave index data, remote copy metadata verification, differential data recovery verification, and index link health check.

[0112] After the above four steps, data can be natively copied from one Elasticsearch cluster to another Elasticsearch cluster. Combined with the master-slave switching strategy, high availability between clusters can be achieved while ensuring strong data consistency. For the goal of non-stop inter-cluster index migration, the present invention synchronizes data to the destination cluster, waits for the existing data to be restored, and then performs another master-slave switch. For the goal of reliable version upgrades, the present invention copies the version data to be upgraded to the backup cluster, and the cluster can be quickly switched when the version upgrade is abnormal.

[0113] Based on the same inventive concept, the present invention also proposes a device for realizing strong consistency synchronization of an Elasticsearch cluster. The implementation of the device can refer to the implementation of the above method, and the repeated parts will not be repeated. The term "module" used below can be a combination of software and / or hardware that implements a predetermined function. Although the device described in the following embodiments is preferably implemented in software, the implementation of hardware, or a combination of software and hardware, is also possible and conceivable.

[0114] Fig. 9 This is a schematic diagram of the structure of the device for realizing strong consistency synchronization of the Elasticsearch cluster of the present invention. Fig. 9 As shown, the device comprises:

[0115] The Elasticsearch write module 101 is used to write data into the primary shard of the primary index to which the document belongs. After the data is written successfully, the write request is sent to the node where the primary shard of the secondary index to which the document belongs is located. At the same time, the primary shard of the secondary index is regarded as a remote copy of the primary shard of the primary index, and the data is written to the remote copy to achieve real-time data synchronization.

[0116] Elasticsearch secondary shard recovery module 102 is used for the primary shard of the primary index to send the segment files that have been stored on the disk to the remote copy, and parse the storage information of the actual data in the segment files; the primary shard of the primary index sends the translog logs that have not been stored on the disk to the remote copy, so as to restore the remote copy to a data state consistent with the primary shard, thereby realizing historical data recovery.

[0117] Define the association between the master and slave indexes in the Elasticsearch cluster state to establish a link between Elasticsearch clusters, and update them according to the Elasticsearch cluster state change events to trigger real-time data synchronization.

[0118] Regularly check the connection status between Elasticsearch clusters and the data differences between the master and slave indexes; when the data difference between the master and slave indexes is too large or the connection is abnormal, the master cluster will actively disconnect the connection between the Elasticsearch clusters and notify the slave index to perform the difference data recovery operation.

[0119] The RPC call of the main cluster is used to trigger the recovery of the historical data of the slave index. At the same time, only the primary shard data of the slave index needs to be restored.

[0120] After the historical data is restored, the primary shard of the slave index is added to the remote replica group and starts receiving write requests for the primary index.

[0121] It should be noted that although several modules for implementing the strong consistency synchronization device of the Elasticsearch cluster are mentioned in the above detailed description, this division is only exemplary and not mandatory. In fact, according to an embodiment of the present invention, the features and functions of two or more modules described above can be concretized in one module. Conversely, the features and functions of one module described above can be further divided into multiple modules for concretization.

[0122] Based on the above invention concept, Fig.10 As shown, the present invention also proposes a computer device 200, including a memory 210, a processor 220, and a computer program 230 stored in the memory 210 and executable on the processor 220. When the processor 220 executes the computer program 230, the aforementioned method for realizing strong consistency synchronization of an Elasticsearch cluster is implemented.

[0123] Based on the aforementioned inventive concept, the present invention also proposes a computer-readable storage medium, which stores a computer program for executing the aforementioned method for realizing strong consistency synchronization of an Elasticsearch cluster.

[0124] The method and device for realizing strong consistency synchronization of Elasticsearch cluster proposed in the present invention have the following highlights:

[0125] 1. It realizes the strong consistency replication of index data between Elasticsearch clusters, ensures the high consistency of master-slave index data, and meets the high reliability requirements of the enterprise's core business.

[0126] 2. By drawing on the replica recovery mechanism, efficient historical data recovery is achieved, and the master-write-slave-read mechanism is adopted to ensure the timeliness of real-time data synchronization.

[0127] 3. Regular data verification and automatic recovery are performed to fully ensure the reliability of master-slave index data.

[0128] 4. Supports seamless master-slave switching and inter-cluster migration, provides continuous service capabilities, and shortens business migration time.

[0129] 5. It has low invasiveness to source code, low development difficulty, strong versatility, and reduces R&D and application costs.

[0130] Although the spirit and principle of the present invention have been described with reference to several specific embodiments, it should be understood that the present invention is not limited to the disclosed specific embodiments, and the division of various aspects does not mean that the features in these aspects cannot be combined to benefit, and such division is only for the convenience of expression. The present invention is intended to cover various modifications and equivalent arrangements contained in the spirit and scope of the attached claims.

[0131] Regarding the limitation of the protection scope of the present invention, those skilled in the art should understand that, based on the technical solution of the present invention, various modifications or variations that can be made by those skilled in the art without creative work are still within the protection scope of the present invention.

Claims

1. A method for realizing strong consistency synchronization of an Elasticsearch cluster, characterized in that: The method includes: Write the data to the primary shard of the primary index to which the document belongs. After the data is written successfully, send the write request to the node where the primary shard of the secondary index to which the document belongs is located. At the same time, treat the primary shard of the secondary index as a remote copy of the primary shard of the primary index, and write the data to the remote copy to achieve real-time data synchronization; The primary shard of the primary index sends the segment files that have been stored on disk to the remote replica, and parses the storage information of the actual data in the segment files; the primary shard of the primary index sends the translog logs that have not been stored on disk to the remote replica, so as to restore the remote replica to a data state consistent with the primary shard, thereby realizing historical data recovery.

2. The method for realizing strong consistency synchronization of an Elasticsearch cluster according to claim 1, characterized in that: Define the association between the master and slave indexes in the Elasticsearch cluster state to establish a link between Elasticsearch clusters, and update them according to the Elasticsearch cluster state change events to trigger real-time data synchronization.

3. The method for realizing strong consistency synchronization of an Elasticsearch cluster according to claim 1, characterized in that: Regularly check the connection status between Elasticsearch clusters and the data differences between the master and slave indexes; when the data difference between the master and slave indexes is too large or the connection is abnormal, the master cluster will actively disconnect the connection between the Elasticsearch clusters and notify the slave index to perform the difference data recovery operation.

4. The method for realizing strong consistency synchronization of Elasticsearch cluster according to claim 1, characterized in that: The RPC call of the main cluster is used to trigger the recovery of the historical data of the slave index. At the same time, only the primary shard data of the slave index needs to be restored. After the historical data is restored, the primary shard of the slave index is added to the remote replica group and starts receiving write requests for the primary index.

5. A device for realizing strong consistency synchronization of an Elasticsearch cluster, characterized in that: The device includes: The Elasticsearch write module is used to write data to the primary shard of the primary index to which the document belongs. After the data is written successfully, the write request is sent to the node where the primary shard of the secondary index to which the document belongs is located. At the same time, the primary shard of the secondary index is regarded as a remote copy of the primary shard of the primary index, and the data is written to the remote copy to achieve real-time data synchronization; The Elasticsearch secondary shard recovery module is used for the primary shard of the primary index to send the segment files that have been stored on disk to the remote replica, and parse the storage information of the actual data in the segment files; the primary shard of the primary index sends the translog logs that have not been stored on disk to the remote replica, so as to restore the remote replica to a data state consistent with the primary shard, thereby realizing historical data recovery.

6. The device for realizing strong consistency synchronization of Elasticsearch cluster according to claim 5, characterized in that: Define the association between the master and slave indexes in the Elasticsearch cluster state to establish a link between Elasticsearch clusters, and update them according to the Elasticsearch cluster state change events to trigger real-time data synchronization.

7. The device for realizing strong consistency synchronization of Elasticsearch cluster according to claim 5, characterized in that: Regularly check the connection status between Elasticsearch clusters and the data differences between the master and slave indexes; when the data difference between the master and slave indexes is too large or the connection is abnormal, the master cluster will actively disconnect the connection between the Elasticsearch clusters and notify the slave index to perform the difference data recovery operation.

8. The device for realizing strong consistency synchronization of Elasticsearch cluster according to claim 5, characterized in that: The RPC call of the main cluster is used to trigger the recovery of the historical data of the slave index. At the same time, only the primary shard data of the slave index needs to be restored. After the historical data is restored, the primary shard of the slave index is added to the remote replica group and starts receiving write requests for the primary index.

9. A computer device comprising a memory, a processor and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the method according to any one of claims 1 to 4 is implemented.

10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program for executing the method according to any one of claims 1 to 4.

Citation Information

Patent Citations

  • Distributed storage method, server and client

    CN111061431A

  • Inter-cluster data migration method and device, storage medium and electronic equipment

    CN115408366A

  • Indexing heterogeneous serchable data in a multi-tenant cloud

    US20160203171A1