A data synchronization method and service node
By introducing the synchronization lock and message lock mechanism into the distributed system, the problem of resource waste when multiple service nodes are synchronized at the same time is solved, efficient data synchronization and consistency processing are achieved, and data coverage and repeated updates are avoided.
Patent Information
- Application Number
- CN202010973163.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-09-16
- Publication Date
- 2024-10-11
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
In a distributed system, when multiple service nodes perform data synchronization at the same time, it leads to the problem of resource waste, and the existing technology lacks an efficient solution.
By introducing the synchronization lock mechanism in the distributed system, the first service node determines whether there is a synchronization lock when receiving the data synchronization instruction. If not, it generates and loads the synchronization lock, pulls the data to be synchronized and updates the database, and other service nodes Skip data synchronization operations to avoid repeated updates. At the same time, message locks and long connections are used to process the changed data pushed by the data center to ensure data consistency.
It effectively avoids the waste of resources when multiple service nodes are synchronized at the same time, ensures the consistency and integrity of database data, avoids data overwriting, and improves the efficiency of data synchronization.
Smart Images

Figure CN112069266B_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the field of data analysis technology, and in particular, to a data synchronization method and a service node. Background Art
[0002] A distributed system is a system in which hardware or software components are distributed on different network computers and communicate and coordinate with each other through message passing. A distributed system includes multiple service nodes with equal status and no master-slave distinction. Each service node performs the same or different tasks. By adopting a distributed system, the problem of single point failure can be effectively avoided.
[0003] When each service node in a distributed system shares a database, there is a problem of duplicate database writing. Taking the monitoring scenario as an example, since there are multiple service nodes performing the same task in the distributed system, any service node will actively obtain the status data of each device from the data center under the set conditions. At the same time, the service node will also obtain the status data of each device from the database connected to it. Therefore, the service node compares the status data of each device obtained from the data center with the status data of each device obtained from the database, and updates the changed status data to the database, thus achieving data consistency between the status data of each device in the database and the actual status of the device. However, if there are multiple service nodes that meet the set conditions simultaneously or successively, each service node will perform the same data synchronization operation, that is, synchronize the status data of each device obtained from the data center with the database. Since most of the status data of each device obtained from the data center and the status data of each device obtained from the database are even exactly the same, there will be repeated updates of the changed data to the database, resulting in a waste of resources.
[0004] In summary, there is currently a lack of an efficient data synchronization method for distributed systems. Summary of the invention
[0005] The present application provides a data synchronization method and a data node, which are used to solve the problem of resource waste caused when multiple service nodes update a database in a distributed system.
[0006] In a first aspect, an embodiment of the present application provides a data synchronization method, which is applicable to a distributed system including multiple service nodes, and the multiple service nodes share a database; the method includes: after receiving a data synchronization instruction, the first service node determines whether a synchronization lock is loaded in the distributed system; when the first service node determines that there is no synchronization lock, the first service node loads the synchronization lock generated by the first service node into the distributed system; the first service node pulls the data to be synchronized from the data center and updates the database according to the data to be synchronized.
[0007] Based on this scheme, for any one of the multiple service nodes in the distributed system, that is, the first service node, when it receives a data synchronization instruction, it determines whether there is a synchronization lock in the distributed system where it is located, and when it is determined that there is no synchronization lock, it adds a synchronization lock to the distributed system where it is located, and at the same time it can pull the data to be synchronized from the data center to update the database. In this way, when it is determined that a service node is already performing data synchronization in the distributed system, a synchronization lock is set for the distributed system, so that when other service nodes receive the data synchronization instruction and determine that the distributed system already has a synchronization lock, these service nodes will skip the data synchronization and directly enter the non-data synchronization working state, so as to avoid the waste of resources caused by at least two service nodes performing data synchronization at the same time in the same distributed system.
[0008] In one possible implementation method, before updating the database according to the data to be synchronized, it also includes: the first service node generates a message lock; the message lock is used to indicate that the first service node is performing data synchronization; the first service node establishes a long connection with the data center; the long connection is used for the data center to send the changed data to the first server; when the first service node determines that it has the message lock, the first service node stores the changed data in a message queue.
[0009] Based on this solution, after the first service node pulls the data to be synchronized from the data center and before updating the database, the first service node sets a message lock to indicate that it is performing data synchronization. At the same time, the first service node will also establish a long connection with the data center. In this way, when the first service node is performing data synchronization, if the first service node receives changed data sent by the data center through the long connection, the first service node can store the changed data in the message queue. As a result, the first service node can process the data in the message queue one by one after data synchronization, avoiding the data overwrite phenomenon caused by the first service node still having to process the changed data at the same time during the data synchronization process due to the lack of a message lock.
[0010] In a possible implementation method, after storing the changed data in the message queue, it also includes: after the first service node updates the data to be synchronized to the database, deleting the message lock; the first service node updates the data in the message queue to the database.
[0011] Based on this solution, after the first service node synchronizes the data to be synchronized pulled from the data center with the database, the message lock is further deleted, so that the first service node can synchronize the changed data in the message queue with the database one by one. This method can ensure that the first service node synchronizes the database according to the data to be synchronized and the data in the message queue without omission.
[0012] In one possible implementation method, when the first server determines that the synchronization lock exists, the first server stops executing the data synchronization instruction.
[0013] Based on this solution, for any service node among multiple service nodes in a distributed system, that is, the first service node, when it receives a data synchronization instruction, it determines whether there is a synchronization lock in the distributed system where it is located. When it is determined that there is a synchronization lock, the first service node can directly skip the execution of data synchronization and directly enter a non-data synchronization working state, avoiding data duplication and waste of resources caused by multiple service nodes performing data synchronization at the same time.
[0014] In a possible implementation method, the data synchronization instruction is a data update instruction or a service start instruction received by the first service node.
[0015] Based on this scheme, for the operation used to instruct the first service node to perform data synchronization, it can be that a service node in a standby state receives an instruction to start the service on it, or it can be that a service node in a working state receives a data update instruction. For these two situations, the service node can determine whether it is really necessary to perform the data synchronization operation.
[0016] In one possible implementation method, loading the synchronization lock generated by the first service node into the distributed system includes: the first service node generates a synchronization lock with a valid duration; the first service node starts a scheduled task; the scheduled task is used to periodically refresh the valid duration of the synchronization lock; the duration of the cycle is shorter than the valid duration.
[0017] Based on this solution, the first service node is provided with a synchronization lock with an effective duration, and a timed task is started to allow the timed task to periodically refresh the effective duration of the synchronization lock. This can ensure that, during the data synchronization process of the first service node, other service nodes do not need to repeat the data synchronization operation. Secondly, after the first service node synchronizes the data, due to the existence of the synchronization lock, even if other service nodes receive data update instructions or service start instructions, these service nodes will not synchronize the data. Thirdly, after the first service node fails, the timed task will also stop, so that the synchronization lock will also become invalid after the effective duration. Other service nodes can synchronize data after receiving data update instructions or service start instructions, thereby avoiding the phenomenon that these service nodes mistakenly believe that the first service node is always synchronizing data, which leads to the inability to synchronize the database.
[0018] In a possible implementation method, after receiving the service stop instruction, the first service node deletes the synchronization lock.
[0019] Based on this solution, the first service node sets a synchronization lock, which has no validity period. When the first service node receives a service stop instruction, it deletes the synchronization lock. Other service nodes can synchronize data after receiving data update instructions or service start instructions, thereby avoiding the phenomenon that these service nodes mistakenly believe that the first service node is always synchronizing data, which leads to the inability to synchronize the database.
[0020] In a possible implementation method, the synchronization lock is loaded into a redis storage system in the distributed system; and the message queue is constructed through the redis storage system.
[0021] Based on this solution, since the redis storage system is lightweight and fast, by loading the synchronization lock on the redis storage system, the first service node accesses the redis storage system after receiving the data synchronization instruction to determine whether there is a synchronization lock. This process takes a short time. In addition, the message queue is also built through the redis storage system, so the storage and reading of changed data will be fast.
[0022] In the second aspect, an embodiment of the present application provides a service node, which is suitable for a distributed system including multiple service nodes, and the multiple service nodes share a database; the service node includes: a synchronization lock determination unit, which is used to determine whether a synchronization lock is loaded in the distributed system after receiving a data synchronization instruction; a synchronization lock loading unit, which is used to load the synchronization lock generated by the service node into the distributed system when it is determined that there is no synchronization lock; a data synchronization unit, which is used to pull the data to be synchronized from the data center and update the database according to the data to be synchronized.
[0023] Based on this scheme, for any one of the multiple service nodes in the distributed system, that is, the first service node, when it receives a data synchronization instruction, it determines whether there is a synchronization lock in the distributed system where it is located, and when it is determined that there is no synchronization lock, it adds a synchronization lock to the distributed system where it is located, and at the same time it can pull the data to be synchronized from the data center to update the database. In this way, when it is determined that a service node is already performing data synchronization in the distributed system, a synchronization lock is set for the distributed system, so that when other service nodes receive the data synchronization instruction and determine that the distributed system already has a synchronization lock, these service nodes will skip the data synchronization and directly enter the non-data synchronization working state, so as to avoid the waste of resources caused by at least two service nodes performing data synchronization at the same time in the same distributed system.
[0024] In one possible implementation method, the data synchronization unit is also used to generate a message lock; the message lock is used to indicate that the service node is performing data synchronization; a long connection is established with the data center; the long connection is used for the data center to send the changed data to the service node; when it is determined that the message lock is present, the changed data is stored in a message queue.
[0025] Based on this solution, after the first service node pulls the data to be synchronized from the data center and before updating the database, the first service node sets a message lock to indicate that it is performing data synchronization. At the same time, the first service node will also establish a long connection with the data center. In this way, when the first service node is performing data synchronization, if the first service node receives changed data sent by the data center through the long connection, the first service node can store the changed data in the message queue. As a result, the first service node can process the data in the message queue one by one after data synchronization, avoiding the data overwrite phenomenon caused by the first service node still having to process the changed data at the same time during the data synchronization process due to the lack of a message lock.
[0026] In a possible implementation method, the data synchronization unit is further used to delete the message lock after updating the data to be synchronized to the database; and update the data in the message queue to the database.
[0027] Based on this solution, after the first service node synchronizes the data to be synchronized pulled from the data center with the database, the message lock is further deleted, so that the first service node can synchronize the changed data in the message queue with the database one by one. This method can ensure that the first service node synchronizes the database according to the data to be synchronized and the data in the message queue without omission.
[0028] In a possible implementation method, the synchronization lock loading unit is further used to stop executing the data synchronization instruction when it is determined that the synchronization lock exists.
[0029] Based on this solution, for any service node among multiple service nodes in a distributed system, that is, the first service node, when it receives a data synchronization instruction, it determines whether there is a synchronization lock in the distributed system where it is located. When it is determined that there is a synchronization lock, the first service node can directly skip the execution of data synchronization and directly enter a non-data synchronization working state, avoiding data duplication and waste of resources caused by multiple service nodes performing data synchronization at the same time.
[0030] In a possible implementation method, the data synchronization instruction is a data update instruction or a service start instruction received by the first service node.
[0031] Based on this scheme, for the operation used to instruct the first service node to perform data synchronization, it can be that a service node in a standby state receives an instruction to start the service on it, or it can be that a service node in a working state receives a data update instruction. For these two situations, the service node can determine whether it is really necessary to perform the data synchronization operation.
[0032] In one possible implementation method, the synchronization lock loading unit is specifically used to generate a synchronization lock with a valid duration; start a timing task; the timing task is used to refresh the valid duration of the synchronization lock periodically; the duration of the cycle is shorter than the valid duration.
[0033] Based on this solution, the first service node is provided with a synchronization lock with an effective duration, and a timed task is started to allow the timed task to periodically refresh the effective duration of the synchronization lock. This can ensure that, during the data synchronization process of the first service node, other service nodes do not need to repeat the data synchronization operation. Secondly, after the first service node synchronizes the data, due to the existence of the synchronization lock, even if other service nodes receive data update instructions or service start instructions, these service nodes will not synchronize the data. Thirdly, after the first service node fails, the timed task will also stop, so that the synchronization lock will also become invalid after the effective duration. Other service nodes can synchronize data after receiving data update instructions or service start instructions, thereby avoiding the phenomenon that these service nodes mistakenly believe that the first service node is always synchronizing data, which leads to the inability to synchronize the database.
[0034] In a possible implementation method, the device further includes a synchronization lock deleting unit, which is used to delete the synchronization lock after receiving a service stop instruction.
[0035] Based on this solution, the first service node sets a synchronization lock, which has no validity period. When the first service node receives a service stop instruction, it deletes the synchronization lock. Other service nodes can synchronize data after receiving data update instructions or service start instructions, thereby avoiding the phenomenon that these service nodes mistakenly believe that the first service node is always synchronizing data, which leads to the inability to synchronize the database.
[0036] In a possible implementation method, the synchronization lock is loaded into a redis storage system in the distributed system; and the message queue is constructed through the redis storage system.
[0037] Based on this solution, since the redis storage system is lightweight and fast, by loading the synchronization lock on the redis storage system, the first service node accesses the redis storage system after receiving the data synchronization instruction to determine whether there is a synchronization lock. This process takes a short time. In addition, the message queue is also built through the redis storage system, so the storage and reading of changed data will be fast.
[0038] In a third aspect, an embodiment of the present application provides a computing device, including:
[0039] A memory for storing program instructions;
[0040] The processor is used to call the program instructions stored in the memory and execute any implementation method of the first aspect according to the obtained program.
[0041] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores computer-executable instructions, and the computer-executable instructions are used to enable a computer to execute any implementation method of the first aspect. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.
[0043] Figure 1 A possible system architecture provided for an embodiment of the present application;
[0044] Figure 2 A data synchronization method provided in an embodiment of the present application;
[0045] Figure 3A flowchart of a data synchronization method provided in an embodiment of the present application;
[0046] Figure 4 A service node provided in an embodiment of the present application;
[0047] Figure 5 A schematic diagram of a computing device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0048] In order to make the purpose, technical solutions and advantages of the present application clearer, the present application will be further described in detail below in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present application.
[0049] For a distributed system, if multiple service nodes synchronize data with the database connected to the distributed system at the same time or nearly at the same time, the data to be synchronized pulled by these multiple service nodes from the data center is similar, and the data they obtain from the database is also similar, resulting in similar content that they need to update to the database. In this case, the behavior of multiple service nodes pulling data to be synchronized from the data center will waste resources.
[0050] like Figure 1 As shown, a possible system architecture provided by an embodiment of the present application includes a distributed system 110, a database 120, a data center 130, and a device 140. As an example, the system architecture can be applied to a scenario of inspecting the status of one or more smart devices in a hotel, and after identifying that the status of one or some smart devices is abnormal, an alarm prompt message is pushed to the hotel's equipment maintenance personnel in a timely manner, so that the maintenance personnel can maintain the smart devices as soon as possible, thereby improving the user experience of hotel customers.
[0051] Wherein, multiple service nodes are deployed in the distributed system 110. As an example, Figure 1 The distributed system 110 shown includes service node 1101, service node 1102 and service node 1103. The three service nodes are respectively an independent server, and the projects deployed on each service node are used to perform the same function, such as inspecting the status of the device.
[0052] The database 120 is connected to the distributed system 110, and is mainly connected to any service node in the distributed system 110, such as the service node 1101, the service node 1102, and the service node 1103 are respectively connected to the database 120. Since these service nodes share the same database 120, when one of the service nodes goes down, the working status of other service nodes is not affected.
[0053] The data center 130 can interact with the distributed system 110 for data, which mainly means that the data center 130 can interact with any service node in the distributed system 110 for data, including: when a service node receives a data synchronization instruction, under the condition that it is determined that the distributed system is not loaded with a synchronization lock, the service node can actively obtain the data to be synchronized from the data center 130, and thereafter and before performing data synchronization, the service node establishes a message lock and a long connection with the data center 130, so that when the service node synchronizes the database 120 according to the data to be synchronized, the service node can still receive the changed data actively sent by the data center 130 and store it in the message queue.
[0054] Based on the scenario of inspecting the status of the hotel's smart devices, device 140 may refer to the hotel's smart devices. As an example, Figure 1 Device 1401, device 1402, device 1403, ... device 1490 are shown in the figure. Among them, these 90 devices can be devices whose device status needs to be inspected by the same hotel registered on the data center 130, or they can be devices whose device status needs to be inspected by multiple hotels registered on the data center 130, and this application does not limit this. It should be noted that in addition to being a smart device for a hotel, device 140 can also be a device for various scenarios, such as a camera used to monitor the intersection status of a set area.
[0055] In this way, the data center 130 can exchange data with the distributed system 110 on the one hand, and can be used to inspect the status of the device 140 on the other hand. In specific implementation, the data center 130 can obtain the status information of the device 140 in real time, wherein the status information may include whether the device is online or offline, and for the device in the online state, the power of the device, and other information, and record the obtained status information. After the status information of the device 140 recorded on the data center 130 is pulled by the distributed system 110, if the data center 130 detects that the status information of the device 140 has changed, the data center 130 can actively push the changed data to the distributed system through the long connection established with the distributed system 110. Among them, the change in the status information of the device 140 may include the addition of a new device to the data center 130, the withdrawal of a device already existing in the data center 130 from the data center 130, and the change in the status of a device already existing in the data center 130.
[0056] Regarding the background technology and Figure 1 The system architecture shown in the figure, the embodiment of the present application provides a method for data synchronization. Figure 2 As shown, this method can be Figure 1 Any service node in the distributed system shown can execute:
[0057] Step 201: After receiving a data synchronization instruction, the first service node determines whether a synchronization lock is loaded in the distributed system.
[0058] In this step, the first service node can be any one of the multiple service nodes in the distributed system. These multiple service nodes share a database. The number of databases can be one or more, which is not specifically limited in this application. The type of database can be a relational database, a non-relational database, or other types of databases, which is not specifically limited in this application. A project is deployed on each of these multiple service nodes, and the projects deployed on each service node are used to perform the same function, such as inspecting the status of the device.
[0059] Step 202: When the first service node determines that there is no synchronization lock, the first service node loads the synchronization lock generated by the first service node into the distributed system.
[0060] Step 203: The first service node pulls the data to be synchronized from the data center and updates the database according to the data to be synchronized.
[0061] Based on this scheme, for any one of the multiple service nodes in the distributed system, that is, the first service node, when it receives a data synchronization instruction, it determines whether there is a synchronization lock in the distributed system where it is located, and when it is determined that there is no synchronization lock, it adds a synchronization lock to the distributed system where it is located, and at the same time it can pull the data to be synchronized from the data center to update the database. In this way, when it is determined that a service node is already performing data synchronization in the distributed system, a synchronization lock is set for the distributed system, so that when other service nodes receive the data synchronization instruction and determine that the distributed system already has a synchronization lock, these service nodes will skip the data synchronization and directly enter the non-data synchronization working state, so as to avoid the waste of resources caused by at least two service nodes performing data synchronization at the same time in the same distributed system.
[0062] The above steps will be described in detail below with reference to examples.
[0063] In an implementation of the above step 201, the data synchronization instruction is a data update instruction or a service start instruction received by the first service node.
[0064] For example, based on Figure 1 In the system architecture shown, it is assumed that the projects deployed on service nodes 1101, 1102, and 1103 are used to inspect the equipment status of the hotel, and it is assumed that service node 1101 is the first service node. Then, how service node 1101 receives the data synchronization instruction may include the following two situations:
[0065] Scenario 1: After the service node 1101 is powered on, the project deployed on the service node 1101 immediately receives a service start instruction. In order to make the service node 1101 clear whether it needs to perform a data synchronization behavior, the service node 1101 needs to determine whether the distributed system is loaded with a synchronization lock;
[0066] Scenario 2: The service node 1101 that is inspecting the status of the hotel equipment receives a data update instruction, such as a user clicking a refresh button set on the service node 1101. Similarly, in order for the service node 1101 to know whether it needs to perform a data synchronization, the service node 1101 needs to determine whether the distributed system is loaded with a synchronization lock.
[0067] In an implementation of the above step 202, when the first server determines that the synchronization lock exists, the first server stops executing the data synchronization instruction.
[0068] As in the above example, when the service node 1101 receives the data synchronization instruction, the service node 1101 does not immediately pull the data to be synchronized from the data center, but needs to determine whether the distributed system is loaded with a synchronization lock. At this time, the following two situations may be included:
[0069] Case 1: The distributed system is loaded with a synchronization lock. At this time, the service node 1101 can confirm that there is already a service node that is updating the database according to the data to be synchronized, or that the service node has finished updating the database according to the data to be synchronized, but it is already updating the database according to the long connection, that is, the service node 1101 determines that it no longer needs to perform the data synchronization operation, and the service node 1101 can stop executing the data synchronization instruction.
[0070] Case 2: The distributed system is not loaded with a synchronization lock. At this time, the service node 1101 can determine that it is a service node that needs to perform data synchronization, so the service node 1101 can load the generated synchronization lock in the distributed system to prompt other service nodes that may receive the data synchronization instruction. When it is determined that the distributed system has a synchronization lock, these other service nodes do not need to perform the data synchronization operation again, which can reduce the waste of resources caused by data synchronization.
[0071] The generated synchronization lock can be in the following two forms:
[0072] Optionally, loading the synchronization lock generated by the first service node into the distributed system includes: the first service node generates a synchronization lock with a valid duration; the first service node starts a scheduled task; the scheduled task is used to periodically refresh the valid duration of the synchronization lock; the duration of the cycle is shorter than the valid duration.
[0073] As in the previous example, when the service node 1101 determines that the distributed system is not loaded with a synchronization lock, the service node 1101 can generate a synchronization lock with an effective duration, such as a synchronization lock with a duration of 10 seconds, and load it into the distributed system. In addition, the service node 1101 can also start a certain time task at the same time. After the timed task is started, the effective duration of the synchronization lock can be periodically refreshed. In this process, since it takes a certain duration to start the scheduled task, the duration of starting the scheduled task can be set to be slightly longer than the effective duration of the synchronization lock, such as setting it to 1 minute, that is, the timing is started when the scheduled task starts to be started. If within 1 minute after the timing, the service node 1101 can load the generated synchronization lock into the distributed system, and the synchronization lock will take effect immediately, and the duration of the refresh cycle of the effective duration of the synchronization lock can be controlled to be slightly longer than the effective duration of the synchronization lock. This ensures that once service node 1101 is selected for data synchronization, unless service node 1101 crashes, the crash will cause the synchronization lock loaded in the distributed system to become invalid after the effective period expires. At this time, if there are other service nodes that receive the data synchronization instruction, the service node can determine that the distributed system no longer has a synchronization lock, and the service node will assume the role of data synchronization again. Otherwise, no other service node will be needed for data synchronization.
[0074] Optionally, after receiving the service stop instruction, the first service node deletes the synchronization lock.
[0075] As in the previous example, when service node 1101 determines that the distributed system is not loaded with a synchronization lock, service node 1101 can generate a synchronization lock without a valid duration. In order to avoid that when service node 1101 goes down in the future, other service nodes that receive data synchronization instructions see the synchronization lock that is always loaded on the distributed system, and make the service node mistakenly believe that service node 1101 is still updating the database, therefore, if service node 1101 generates a synchronization lock without a valid duration, when the project on service node 1101 stops service, service node 1101 can actively delete the synchronization lock loaded on the distributed system, so that other service nodes can update the database when receiving data synchronization instructions.
[0076] In one implementation of the above step 202, before updating the database according to the data to be synchronized, it also includes: the first service node generates a message lock; the message lock is used to indicate that the first service node is performing data synchronization; the first service node establishes a long connection with the data center; the long connection is used for the data center to send the changed data to the first server; when the first service node determines that it has the message lock, the first service node stores the changed data in the message queue.
[0077] As in the above example, when service node 1101 receives the data synchronization instruction and determines that the distributed system is loaded with a synchronization lock, service node 1101 can generate a message lock on the one hand, and the generated message lock is used to indicate that service node 1101 is updating the data in the database according to the data to be synchronized pulled from the data center; on the other hand, service node 1101 will also establish a long connection with the data center, so that service node 1101 can still receive the changed data actively pushed by the data center, that is, the update information of the device status, during the process of updating the data in the database. At this time, due to the existence of the message lock, service node 1101 will not immediately process the changed data, but put it into the message queue.
[0078] In one embodiment, after storing the changed data in the message queue, the method further includes: after the first service node updates the data to be synchronized to the database, deleting the message lock; and the first service node updates the data in the message queue to the database.
[0079] In the above example, when the service node 1101 is synchronizing the data to be synchronized pulled from the data center, it will also receive the changed data sent by the data center through a long connection. Due to the existence of the message lock, the service node 1101 can give priority to the data to be synchronized pulled from the data center. For the changed data that may be received, the service node 1101 only needs to store it in the message queue until the service node 1101 completes the processing of all the synchronized data. At this time, the service node 1101 deletes the message lock and then turns to the processing of the changed data in the message queue. It can be understood that when the service node 1101 processes the data in the message queue, if the service node 1101 continues to receive new changed data, the service node 1101 can continue to store the received data in the message queue. In this way, the service node 1101 processes the changed data in the message queue one by one, and will not miss the processing of a certain changed data.
[0080] In one implementation of the above step 203, on the one hand, the first service node pulls the data to be synchronized from the data center according to the data synchronization instruction, and on the other hand, the first service node obtains data from the database, and then compares the pulled data to be synchronized with the data obtained from the database one by one: for example, for devices with the same number, if it is determined that its status in the data to be synchronized is different from the status in the database, then its status in the data to be synchronized is updated to the database; for some devices that only appear in the data to be synchronized, but these devices do not exist in the database, it is necessary to add these devices that appear in the data to be synchronized and their status data to the database; for some devices that only appear in the database, but these devices do not exist in the data to be synchronized, it is necessary to delete these devices that appear in the database and their status data.
[0081] In one implementation method, the synchronization lock is loaded into the redis storage system in the distributed system; and the message queue is constructed through the redis storage system.
[0082] Since the redis storage system is lightweight and fast, by loading the synchronization lock on the redis storage system, after receiving the data synchronization instruction, the first service node accesses the redis storage system to determine whether there is a synchronization lock. This process takes a short time. In addition, the message queue is also built through the redis storage system, so the storage and reading of changed data will be fast.
[0083] like Figure 3 FIG. 1 is a flow chart of a data synchronization method provided in an embodiment of the present application, comprising the following steps:
[0084] Step 301: A first service node receives a data synchronization instruction.
[0085] Step 302, determining whether the distributed system is loaded with a synchronization lock: if not, executing step 303; if yes, the first service node skips executing the data synchronization instruction.
[0086] Step 303, start the scheduled task; after the scheduled task is started, a synchronization lock with a valid duration is loaded to the distributed system, and the valid duration of the synchronization lock is periodically refreshed.
[0087] Step 304: Generate a message lock and establish a long connection with the data center.
[0088] Step 305: Pull the data to be synchronized from the data center.
[0089] Step 306 , in the process of synchronizing the database according to the data to be synchronized, determine whether there is a message pushed by a long connection: if yes, execute step 308 ; if not, execute step 307 .
[0090] Step 307: After updating the database according to the data to be synchronized, delete the message lock.
[0091] Step 308, adding the message to the message queue and executing step 307 at the same time.
[0092] Step 309: Process the messages in the message queue.
[0093] Step 310, in the process of processing the messages in the message queue, determine whether there is a message pushed by a long connection: if yes, execute step 312, if not, execute step 311.
[0094] Step 311, continue to process the messages in the message queue, specifically refers to processing the messages first added to the message queue.
[0095] Step 312, adding the message to the message queue and executing step 311 at the same time.
[0096] Based on the same concept, an embodiment of the present application provides a service node, which is applicable to a distributed system including multiple service nodes, wherein the multiple service nodes share a database; Figure 4 As shown, the service node includes:
[0097] A synchronization lock determination unit 401, configured to determine whether a synchronization lock is loaded in the distributed system after receiving a data synchronization instruction;
[0098] A synchronization lock loading unit 402, configured to load the synchronization lock generated by the service node into the distributed system when it is determined that there is no synchronization lock;
[0099] The data synchronization unit 403 is used to pull the data to be synchronized from the data center and update the database according to the data to be synchronized.
[0100] Furthermore, for the service node, the data synchronization unit 403 is also used to generate a message lock; the message lock is used to indicate that the service node is performing data synchronization; a long connection is established with the data center; the long connection is used for the data center to send the changed data to the service node; when it is determined that the message lock is present, the changed data is stored in the message queue.
[0101] Furthermore, for the service node, the data synchronization unit 403 is further configured to delete the message lock after updating the data to be synchronized to the database; and update the data in the message queue to the database.
[0102] Furthermore, for the service node, the synchronization lock loading unit 402 is further configured to stop executing the data synchronization instruction when it is determined that the synchronization lock exists.
[0103] Further, for the service node, the data synchronization instruction is a data update instruction or a service start instruction received by the first service node.
[0104] Further, for the service node, the synchronization lock loading unit 402 is specifically used to generate a synchronization lock with a valid duration; start a timing task; the timing task is used to refresh the valid duration of the synchronization lock periodically; the duration of the cycle is shorter than the valid duration.
[0105] Furthermore, the service node further includes a synchronization lock deleting unit 404, which is used to delete the synchronization lock after receiving the service stop instruction.
[0106] Furthermore, for the service node, the synchronization lock is loaded into the redis storage system in the distributed system; and the message queue is constructed through the redis storage system.
[0107] The embodiment of the present application also provides a computing device, which may be a desktop computer, a portable computer, a smart phone, a tablet computer, a personal digital assistant (PDA), etc. The computing device may include a central processing unit (CPU), a memory, an input / output device, etc. The input device may include a keyboard, a mouse, a touch screen, etc. The output device may include a display device, such as a liquid crystal display (LCD), a cathode ray tube (CRT), etc.
[0108] The memory may include a read-only memory (ROM) and a random access memory (RAM), and provides the processor with program instructions and data stored in the memory. In an embodiment of the present application, the memory may be used to store program instructions of the data synchronization method;
[0109] The processor is used to call the program instructions stored in the memory and execute the data synchronization method according to the obtained program.
[0110] like Figure 5 FIG. 1 is a schematic diagram of a computing device provided in an embodiment of the present application, wherein the computing device includes:
[0111] Processor 501, memory 502, transceiver 503, bus interface 504; wherein the processor 501, memory 502 and transceiver 503 are connected via a bus 505;
[0112] The processor 501 is configured to read the program in the memory 502 and execute the network switching method;
[0113] The processor 501 may be a central processing unit (CPU), a network processor (NP), or a combination of a CPU and a NP. It may also be a hardware chip. The hardware chip may be an application-specific integrated circuit (ASIC), a programmable logic device (PLD), or a combination thereof. The PLD may be a complex programmable logic device (CPLD), a field-programmable gate array (FPGA), a generic array logic (GAL), or any combination thereof.
[0114] The memory 502 is used to store one or more executable programs and can store data used by the processor 501 when performing operations.
[0115] Specifically, the program may include program code, and the program code includes computer operation instructions. The memory 502 may include volatile memory, such as random-access memory (RAM); the memory 502 may also include non-volatile memory, such as flash memory, hard disk drive (HDD) or solid-state drive (SSD); the memory 502 may also include a combination of the above-mentioned types of memory.
[0116] The memory 502 stores the following elements, executable modules or data structures, or a subset thereof, or an extended set thereof:
[0117] Operation instructions: include various operation instructions, used to implement various operations.
[0118] Operating system: includes various system programs used to implement various basic businesses and handle hardware-based tasks.
[0119] The bus 505 may be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 5 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.
[0120] The bus interface 504 may be a wired communication access port, a wireless bus interface or a combination thereof, wherein the wired bus interface may be, for example, an Ethernet interface. The Ethernet interface may be an optical interface, an electrical interface or a combination thereof. The wireless bus interface may be a WLAN interface.
[0121] An embodiment of the present application further provides a computer-readable storage medium, wherein the computer-readable storage medium stores computer-executable instructions, and the computer-executable instructions are used to enable a computer to execute a data synchronization method.
[0122] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that contain computer-usable program code.
[0123] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0124] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0125] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0126] Although the preferred embodiments of the present application have been described, those skilled in the art may make other changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications falling within the scope of the present application.
[0127] Obviously, those skilled in the art can make various changes and modifications to the present application without departing from the spirit and scope of the present application. Thus, if these modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is also intended to include these modifications and variations.
Claims
1. A data synchronization method, characterized in that: Applicable to a distributed system including multiple service nodes, each of the multiple service nodes has a project deployed thereon, and the project deployed on each service node is used to perform the same function; the multiple service nodes share a database; the method includes: After receiving the data synchronization instruction, the first service node determines whether a synchronization lock is loaded in the distributed system; When the first service node determines that there is no synchronization lock, the first service node loads the synchronization lock generated by the first service node to the distributed system; when the first service node determines that there is the synchronization lock, the first service node stops executing the data synchronization instruction; The first service node generates a message lock; the message lock is used to indicate that the first service node is performing data synchronization; The first service node establishes a persistent connection with the data center; the persistent connection is used by the data center to send the changed data to the first service node; The first service node pulls the data to be synchronized from the data center and updates the database according to the data to be synchronized; The step of loading the synchronization lock generated by the first service node into the distributed system includes: The first service node generates a synchronization lock with a valid duration; The first service node starts a timed task; the timed task is used to refresh the effective duration of the synchronization lock periodically; the duration of the period is shorter than the effective duration.
2. The method according to claim 1, characterized in that Before updating the database according to the data to be synchronized, the method further includes: When determining that the first service node has the message lock, the first service node stores the changed data in a message queue.
3. The method according to claim 2, characterized in that After storing the changed data in the message queue, the method further includes: After the first service node updates the data to be synchronized to the database, the message lock is deleted; The first service node updates the data in the message queue to the database.
4. The method according to claim 1, characterized in that The data synchronization instruction is a data update instruction or a service start instruction received by the first service node.
5. The method according to any one of claims 1 to 3, characterized in that: Also includes: After receiving the service stop instruction, the first service node deletes the synchronization lock.
6. The method according to claim 1, characterized in that The synchronization lock is loaded into the redis storage system in the distributed system; the message queue is constructed through the redis storage system.
7. A service node, characterized in that: Applicable to a distributed system including multiple service nodes, each of which has a project deployed on it, and the projects deployed on each service node are used to perform the same function; the multiple service nodes share a database; The service node includes: A synchronization lock determination unit, configured to determine whether a synchronization lock is loaded in the distributed system after receiving a data synchronization instruction; A synchronization lock loading unit, used for loading the synchronization lock generated by the service node into the distributed system when it is determined that there is no synchronization lock; and stopping the execution of the data synchronization instruction when it is determined that there is the synchronization lock; wherein, loading the synchronization lock generated by the first service node into the distributed system comprises: the first service node generates a synchronization lock with a valid duration; the first service node starts a timed task; the timed task is used to refresh the valid duration of the synchronization lock periodically; the duration of the period is shorter than the valid duration; A data synchronization unit is used to generate a message lock for the first service node; the message lock is used to indicate that the first service node is performing data synchronization; the first service node establishes a long connection with the data center; the long connection is used for the data center to send the changed data to the first service node; pull the data to be synchronized from the data center and update the database according to the data to be synchronized.
8. A computing device, characterized in that include: A memory for storing program instructions; A processor is used to call the program instructions stored in the memory and execute the method according to any one of claims 1 to 6 according to the obtained program.
