High-precision clock synchronization method and device based on distributed protocol and electronic equipment
By adopting the high-precision clock synchronization method of distributed protocol in distributed systems, the traditional centralized clock synchronization method is solved in the case of network delay and node failure, and the high availability and high-precision time synchronization is achieved.
Patent Information
- Application Number
- CN202510024158.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-07
- Publication Date
- 2025-04-11
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
The traditional centralized clock synchronization method has the risk of single point failure, and it is difficult to ensure synchronization accuracy in the case of network delay and node failure.
The high-precision clock synchronization method based on the distributed protocol is adopted, and the time difference is calculated by periodically exchanging the timestamps of nodes, and the global time deviation is calculated using a distributed consistency algorithm, and the nodes are gradually adjusted to achieve global synchronization. In addition, according to the redundancy mechanism and the fault detection algorithm, a new coordinator node after the node fails is determined.
It effectively avoids the single point of failure risk brought by centralized methods, ensures that the system can still maintain high-precision time synchronization in the event of node failure, and improves the performance of distributed systems in high availability and high-precision applications.
Smart Images

Figure CN119814216A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of distributed computing, and in particular to a high-precision clock synchronization method, device and electronic equipment based on a distributed protocol. Background Art
[0002] In modern complex systems, especially in distributed computing environments, ensuring accurate time synchronization across all components is a significant challenge. Many applications, such as financial trading, real-time data processing, and distributed databases, require highly consistent clocks across all nodes. However, traditional centralized clock synchronization methods pose a risk of single points of failure and struggle to guarantee synchronization accuracy in the face of network delays and node failures. Therefore, improvements are needed. Summary of the Invention
[0003] Based on this, it is necessary to provide a high-precision clock synchronization method, device and electronic device based on a distributed protocol to address the risk of single point failure in traditional centralized clock synchronization methods and the difficulty in ensuring synchronization accuracy in the event of network delays and node failures.
[0004] The present invention provides a high-precision clock synchronization method based on a distributed protocol, the method comprising:
[0005] Periodically exchange the timestamps of nodes in the distributed system and calculate the time difference with other nodes so that each node can detect the time deviation between itself and other nodes;
[0006] Based on the distributed consensus algorithm, the global time deviation is calculated and the nodes are gradually adjusted to achieve global synchronization.
[0007] Determine the new coordinator node after node failure based on the redundancy mechanism and fault detection algorithm;
[0008] After completing the time adjustment, each node feeds back the current time status and adjustment results to the coordinator node. The coordinator node determines whether to make further adjustments or elect a new coordinator based on the feedback information.
[0009] In one embodiment, the periodic exchange of timestamps of nodes in the distributed system and calculation of time differences with other nodes so that each node can detect time deviations between itself and other nodes includes:
[0010] Each node exchanges timestamps with its neighboring nodes at fixed time intervals;
[0011] Store the time it takes each node to receive a response from a neighboring node, and calculate the round-trip time and time deviation between two nodes;
[0012] Get multiple time deviation values of a node within a preset time period and calculate the average value.
[0013] In one embodiment, the periodic exchange of timestamps of nodes in the distributed system and calculation of time differences with other nodes so that each node can detect time deviations between itself and other nodes further includes:
[0014] The calculated time offset is stored locally and updated periodically.
[0015] In one embodiment, the method of calculating the global time deviation based on a distributed consensus algorithm and causing nodes to gradually adjust their clocks to achieve global synchronization includes:
[0016] Set the node to be in follower state at startup;
[0017] In response to a node not receiving a heartbeat signal from the leader within a certain period of time, the node transitions to the candidate state and requests votes from other nodes;
[0018] In response to the candidate node gaining support from more than half of the nodes, the node transitions to the leader state;
[0019] The leader node sends a request to all follower nodes. After receiving the request, each follower node returns the locally calculated time offset to the leader node. The leader node stores the time offset information of all follower nodes.
[0020] The leader node averages or weighted averages all time deviation values to calculate the global time deviation;
[0021] The leader node sends the global time deviation to all follower nodes. Each follower node gradually adjusts its local clock according to the instructions of the leader node to achieve global time synchronization.
[0022] In one embodiment, each follower node gradually adjusts its local clock according to the instructions of the leader node to achieve global time synchronization, including:
[0023] Each node calculates the adjustment range based on its own current time and the global time deviation;
[0024] After receiving the adjustment instruction, each node gradually adjusts its local clock according to the set adjustment range.
[0025] In one embodiment, each follower node gradually adjusts its local clock according to the instructions of the leader node to achieve global time synchronization, further comprising:
[0026] After each adjustment, the node pauses for a period of time and obtains real-time information about the system's operating status and application response;
[0027] In response to a system or application exception, the node pauses adjustment and performs status evaluation.
[0028] In one embodiment, determining a new coordinator after a node failure based on a redundancy mechanism and a fault detection algorithm includes:
[0029] Give each node a priority during initialization;
[0030] Each node periodically sends heartbeat signals to other nodes to confirm its online status;
[0031] In response to a node not receiving a heartbeat signal within a predetermined time, the node is marked as a failed node, and other candidate coordinator nodes are transformed into an election state;
[0032] The candidate coordinator node sends its priority information to other nodes. Other nodes vote based on the priority and select the node with the highest priority as the new coordinator.
[0033] In response to the new coordinator node obtaining votes from more than half of the nodes, the new coordinator node sends a message to all nodes that it has become the new coordinator.
[0034] The present invention also provides a high-precision clock synchronization device based on a distributed protocol, comprising:
[0035] The exchange module is used to periodically exchange the timestamps of nodes in the distributed system and calculate the time difference with other nodes so that each node can detect the time deviation between itself and other nodes;
[0036] The adjustment module is used to calculate the global time deviation based on the distributed consensus algorithm and enable nodes to gradually adjust their clocks to achieve global synchronization;
[0037] A determination module is used to determine a new coordinator node after a node fails based on a redundancy mechanism and a fault detection algorithm;
[0038] The feedback module is used for each node to feedback the current time status and adjustment results to the coordinator node after completing the time adjustment. The coordinator node determines whether to make further adjustments or elect a new coordinator based on the feedback information.
[0039] The present invention also provides an electronic device, comprising a memory and a processor, wherein the memory stores a computer program, and when the processor executes the computer program, it implements any of the above-described high-precision clock synchronization methods based on a distributed protocol.
[0040] The present invention also provides a computer-readable storage medium storing a computer program, wherein when the computer program is executed by a processor, the high-precision clock synchronization method based on a distributed protocol as described above is implemented.
[0041] The above-mentioned high-precision clock synchronization method, device and electronic device based on distributed protocol, on the one hand, avoids the single point failure risk brought by the centralized method by utilizing a distributed consistency algorithm to ensure the consistency of each node during the time adjustment process. At the same time, through the calculated global time deviation, the node gradually adjusts its clock to achieve a global synchronization strategy, and determines the new coordinator node after the node fails based on the redundancy mechanism and fault detection algorithm. It can ensure that in the event of node failure, the system can still maintain high-precision time synchronization, which will greatly improve the performance of distributed systems in high-availability and high-precision applications. On the other hand, the feedback mechanism performed by each node after completing the time adjustment is conducive to timely detection of problems and adjustment. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] In order to more clearly illustrate the technical solutions in the present invention or the prior art, a brief introduction will be given below to the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0043] Figure 1 1 is a flow chart of a high-precision clock synchronization method based on a distributed protocol in one embodiment;
[0044] Figure 2 A schematic diagram of a process for calculating a time difference with other nodes in one embodiment;
[0045] Figure 3 Schematic diagram of a flow chart for calculating a global time deviation in one embodiment;
[0046] Figure 4 Schematic diagram of a process for gradually adjusting a local clock in one embodiment;
[0047] Figure 5 FIG1 is a schematic diagram of a process flow of monitoring a system state after each adjustment by a node in an embodiment;
[0048] Figure 6 Schematic diagram of a process for determining a new coordinator node after a node fails in one embodiment;
[0049] Figure 7 Schematic diagram of the structure of a high-precision clock synchronization device based on a distributed protocol in one embodiment;
[0050] Figure 8 A node time deviation detection logic diagram in one embodiment;
[0051] Figure 9 A logic diagram of a distributed consensus algorithm in one embodiment;
[0052] Figure 10 A time adjustment strategy logic diagram in one embodiment;
[0053] Figure 11 A logic diagram of a fault handling mechanism in one embodiment;
[0054] Figure 12 FIG. 1 is a diagram showing the internal structure of an electronic device according to an embodiment. DETAILED DESCRIPTION
[0055] To make the objectives, technical solutions, and advantages of the embodiments of the present invention more clear, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts shall fall within the scope of protection of the present invention.
[0056] The following combination Figures 1-12 The present invention describes a high-precision clock synchronization method, device and electronic device based on a distributed protocol.
[0057] like Figure 1 As shown, in one embodiment, a high-precision clock synchronization method based on a distributed protocol includes the following steps:
[0058] Step S100 , regularly exchanging timestamps of nodes in the distributed system and calculating time differences with other nodes, so that each node can detect the time deviation between itself and other nodes.
[0059] Node time deviation detection is a key step in ensuring time synchronization among components in a distributed system. To achieve this goal, each node needs to be able to actively detect the time deviation between itself and other nodes.
[0060] In step S200 , based on a distributed consensus algorithm, the global time deviation is calculated, and the nodes are made to gradually adjust their clocks to achieve global synchronization.
[0061] Distributed consensus algorithms, such as Paxos or Raft, are key to achieving high-precision time synchronization. To ensure that all nodes reach consensus during time adjustments, the Raft algorithm is recommended. Raft is an easy-to-understand and efficient distributed consensus protocol that ensures all nodes reach consensus on time adjustment decisions without a central coordinator.
[0062] Step S300: Determine a new coordinator node after a node fails based on a redundancy mechanism and a fault detection algorithm.
[0063] It is convenient to ensure that distributed systems can maintain high-precision time synchronization even in the event of node failure.
[0064] In step S400, after completing the time adjustment, each node feeds back the current time status and adjustment result to the coordinator node. The coordinator node determines whether to make further adjustments or elect a new coordinator based on the feedback information.
[0065] Monitor the health of the system through feedback mechanisms to identify potential problems in a timely manner.
[0066] This high-precision clock synchronization method based on a distributed protocol, on the one hand, ensures the consistency of each node in the time adjustment process by utilizing a distributed consistency algorithm, thereby avoiding the single point failure risk brought by the centralized method. At the same time, it uses the calculated global time deviation to enable the node to gradually adjust its clock to achieve a global synchronization strategy, and determines the new coordinator node after the node fails based on the redundancy mechanism and fault detection algorithm. This ensures that in the event of node failure, the system can still maintain high-precision time synchronization, which will greatly improve the performance of distributed systems in high-availability and high-precision applications. On the other hand, the feedback mechanism performed by each node after completing the time adjustment is conducive to timely detection of problems and adjustment.
[0067] In this embodiment, see Figure 2 , periodically exchange the timestamps of nodes in the distributed system and calculate the time difference with other nodes so that each node can detect the time deviation between itself and other nodes, including the following steps:
[0068] In step S110 , each node exchanges timestamps with its neighboring nodes within a fixed time interval.
[0069] For example, node A sends its local timestamp T to node B at time t1. A , and request the timestamp of node B. After receiving the request, node B records the receiving time t2 and sets its local timestamp T B It is sent back to node A together with the receiving time t2.
[0070] Step S120 , storing the response time of each node when receiving the response from the neighboring node, and calculating the round-trip time and time deviation between the two nodes.
[0071] For example, after receiving the response from node B, node A records the response time t3.
[0072] The round-trip time (RTT) calculation formula is:
[0073] RTT=(t3-t1)-(t2-t1)=t3-t2,
[0074] The time offset calculation formula is:
[0075]
[0076] Among them, T A is the timestamp of node A, T B is the timestamp of node B.
[0077] Through these calculations, node A can obtain the time offset between itself and node B.
[0078] Step S130: obtaining multiple time deviation values of the node within a preset time period and calculating an average value.
[0079] By calculating the average of multiple time deviation values, the impact of accidental errors can be easily reduced.
[0080] In this embodiment, the timestamps of nodes in the distributed system are periodically exchanged, and the time difference with other nodes is calculated so that each node can detect the time deviation between itself and other nodes. The following steps are also included:
[0081] Step S140: Store the calculated time deviation locally and update it regularly.
[0082] It is convenient to call up the stored time deviation at any time.
[0083] The specific examples are as follows:
[0084] See also Figure 8 Consider a distributed system consisting of five nodes, with nodes A, B, C, D, and E requiring time deviation detection. Each node exchanges timestamps with two of its neighbors every second. Node A first sends a request to nodes B and C, recording the send and receive timestamps. Nodes B and C return their timestamps, and node A calculates the time deviation from B and C. After ten exchanges, node A obtains multiple time deviations and calculates an average deviation of 5 milliseconds. Based on this deviation, node A decides to adjust its local clock forward by 5 milliseconds to synchronize with the other nodes.
[0085] With this approach, nodes can effectively detect and adjust time deviations, thereby achieving high-precision time synchronization in distributed systems.
[0086] In this embodiment, see Figure 3 ,Based on the distributed consensus algorithm, the global time deviation is calculated, and the nodes are gradually adjusted,to achieve global synchronization, including the following steps:
[0087] Step S210: Setting the node to be in follower state upon startup.
[0088] It should be noted that in the Raft distributed consensus algorithm, nodes are divided into three roles: leader, follower, and candidate.
[0089] Step S220 , in response to the node not receiving a heartbeat signal from the leader within a certain period of time, the node changes to a candidate state and requests votes from other nodes.
[0090] It should be noted that the frequency of sending heartbeat signals and the timeout threshold can be adjusted according to system requirements to balance network load and the timeliness of fault detection.
[0091] In step S230 , in response to the candidate node obtaining support from more than half of the nodes, the node transitions to a leader state.
[0092] The election process ensures high availability and fault tolerance of the system.
[0093] In step S240, the leader node sends a request to all follower nodes. After receiving the request, each follower node returns the locally calculated time offset to the leader node. The leader node stores the time offset information of all follower nodes.
[0094] By using the leader node to store the time deviation information of all follower nodes, the subsequent calculation of the global time deviation is facilitated.
[0095] In step S250 , the leader node averages or weighted averages all the time deviation values to calculate the global time deviation.
[0096] The global time deviation is calculated by averaging or weighted averaging all time deviation values to ensure the rationality and effectiveness of the adjustment.
[0097] The specific calculation formula is as follows:
[0098]
[0099] Among them, N is the number of nodes participating in synchronization, Offseti is the time deviation of the i-th node.
[0100] In step S260, the leader node sends the global time deviation to all follower nodes. Each follower node gradually adjusts its local clock according to the instruction of the leader node to achieve global time synchronization.
[0101] The specific examples are as follows:
[0102] See also Figure 9 Suppose that in a distributed system consisting of five nodes, nodes A, B, C, D, and E need to synchronize their time. First, node A starts up and does not receive a heartbeat signal. It becomes a candidate and requests a vote. Nodes B and C respond and vote for node A, making node A the leader. Next, node A requests a time offset from all nodes. Nodes B, C, D, and E each respond with their calculated time offset. Node A calculates a global time offset of 3 milliseconds and sends this information to all nodes. Each node adjusts its local clock by 3 milliseconds according to the instruction, achieving global time synchronization.
[0103] In this way, each node in the distributed system can achieve high-precision time synchronization through the Raft algorithm without a central clock, ensuring high availability and consistency of the system.
[0104] In this embodiment, see Figure 4 ,Each follower node gradually adjusts its local clock according to the instructions of the leader node to achieve global time synchronization, including the following steps:
[0105] In step S261, each node calculates an adjustment range based on its own current time and the global time deviation.
[0106] The adjustment range should be set according to system requirements, and a small step adjustment strategy is usually adopted to avoid affecting running applications.
[0107] The adjustment range is calculated as follows:
[0108]
[0109] Among them, Current Time is the current local time of the node, Global Offset is the global time offset, and Step Count is the preset number of adjustment steps to ensure that the amplitude of each adjustment is not too large.
[0110] In step S262 , after receiving the adjustment instruction, each node gradually adjusts its local clock according to the set adjustment range.
[0111] By enabling each node to gradually adjust its local clock in small steps after receiving the global time deviation, system instability or application anomalies caused by time mutations can be effectively avoided.
[0112] For example, if the global time deviation is 30 milliseconds and the number of steps is set to 10, the adjustment amplitude is 3 milliseconds each time.
[0113] In this embodiment, see Figure 5 , each follower node gradually adjusts its local clock according to the instructions of the leader node to achieve global time synchronization, which also includes the following steps:
[0114] In step S263 , after each adjustment, the node pauses for a period of time and obtains real-time information on the system operation status and application response.
[0115] For example, after each adjustment, the node pauses for 100 milliseconds to observe the stability of the system and the running status of the application.
[0116] Step S264: In response to an abnormality in the system or application, the node suspends adjustment and performs a status assessment.
[0117] Convenient to ensure the stability of the system during the gradual adjustment process.
[0118] The specific examples are as follows:
[0119] See also Figure 10 Suppose that in a distributed system containing 5 nodes, nodes A, B, C, D and E need to synchronize time. Leader node A calculates the global time deviation to be 30 milliseconds and sends it to all nodes. The number of adjustment steps set for each node is 10 steps, so the adjustment amplitude is 3 milliseconds each time. After receiving the instruction, node B first adds 3 milliseconds to the current time and adjusts its local clock to the new time value. After the adjustment, node B pauses for 100 milliseconds to monitor the stability of the system. After confirming that there are no abnormalities, it makes the next adjustment. After each adjustment, node B feeds back the current time status to leader node A to ensure that the time synchronization process of the entire system proceeds smoothly.
[0120] Through this progressive time adjustment strategy, each node can achieve high-precision time synchronization without a central clock, ensuring the stability and consistency of the system.
[0121] In this embodiment, see Figure 6 ,According to the redundancy mechanism and fault detection algorithm, the new coordinator after the node failure is determined,including the following steps:
[0122] Step S310: assigning a priority to each node during initialization.
[0123] Multiple candidate coordinator nodes are introduced into the system. Each node is assigned a priority during initialization. The priority can be dynamically adjusted based on the node's performance, load, or other indicators. In this way, when the current coordinator node fails, the system can quickly elect a new coordinator.
[0124] In step S320, each node periodically sends a heartbeat signal to other nodes to confirm its online status.
[0125] It is convenient to monitor the health status of nodes in real time and detect faulty nodes in time.
[0126] Step S330: In response to a node not receiving a heartbeat signal within a predetermined time, the node is marked as a failed node, and other candidate coordinator nodes are switched to an election state.
[0127] It is convenient to quickly detect failed nodes and immediately enter the election state after the current coordinator node fails.
[0128] In step S340 , the candidate coordinator node sends its priority information to other nodes. Other nodes vote based on the priority and select the node with the highest priority as the new coordinator.
[0129] This makes it easy to quickly elect a new coordinator after detecting that the coordinator node has failed.
[0130] Step S350: In response to the new coordinator node obtaining votes from more than half of the nodes, the new coordinator node sends a message to all nodes that it has become the new coordinator.
[0131] After taking over, the newly elected coordinator node first collects the current time status and deviation information of all nodes, calculates the new global time deviation, and redistributes adjustment instructions. This process ensures that the system can maintain time synchronization even in the event of node failure.
[0132] The specific examples are as follows:
[0133] See also Figure 11 Consider a distributed system with five nodes. Node A serves as the coordinator and regularly sends heartbeat signals to nodes B, C, D, and E. If node B doesn't receive a heartbeat signal from node A within a certain period of time, it considers node A to be potentially down. Node B immediately enters the election state and broadcasts its priority level (e.g., 2). Nodes C, D, and E also vote based on their own priorities.
[0134] Assume that node C has a priority of 1, node D has a priority of 3, and node E has a priority of 2. During the voting process, node D receives more than half of the votes and becomes the new coordinator. Node D then collects the current time status of all nodes, calculates the new global time offset, and sends the adjustment instructions to all nodes. Each node gradually adjusts its local clock according to the new instructions, ensuring that the system's time synchronization remains stable.
[0135] Through the combination of this redundancy mechanism and fault detection algorithm, the distributed system can quickly recover and continue to perform high-precision time synchronization in the event of node failure, ensuring the stability and consistency of the system.
[0136] To ensure the effectiveness and reliability of the distributed time synchronization protocol in actual applications, the protocol needs to be tested in a simulated environment. The specific test steps are as follows:
[0137] 1. Simulation environment construction
[0138] Goal: Create a test environment that can simulate a real distributed system.
[0139] Method: Use virtual machines or container technology (such as Docker) to build multiple nodes, with each node running the same time synchronization protocol. These nodes can be deployed using cloud service platforms (such as AWS and Azure) or on-premises servers. Ensure that the network latency and bandwidth settings for each node simulate the network conditions expected in real-world scenarios.
[0140] 2. Protocol Implementation
[0141] Objective: Implement the designed distributed time synchronization protocol into runnable code.
[0142] Method: Choose a suitable programming language (such as Python, Go, or Java) to implement the protocol. During the implementation process, you need to pay attention to the following aspects:
[0143] Timestamp exchange: Implement a timestamp exchange mechanism between nodes to ensure that each node can obtain the time information of other nodes.
[0144] Consensus algorithm: Implement the chosen consensus algorithm (such as Raft) to ensure that nodes can reach consensus during the time adjustment process.
[0145] Fault handling: Implement fault detection and dynamic election mechanisms to ensure that a new coordinator can be quickly elected when a node fails.
[0146] 3. Test case design
[0147] Objective: Design a variety of test cases to verify the performance of the protocol under different conditions.
[0148] Method: Design test cases based on the following dimensions:
[0149] Number of nodes: Test the impact of different numbers of nodes (such as 3, 5, 10, and 20 nodes) on synchronization accuracy.
[0150] Network delay: simulates the impact of different network delays (such as 10ms, 50ms, 100ms) on time synchronization.
[0151] Load conditions: Apply different loads (such as high load and low load) on the nodes to observe the robustness of the protocol.
[0152] 4. Data Collection and Analysis
[0153] Objective: To collect data generated during testing to evaluate the performance of the protocol.
[0154] Methodology: The following data was recorded during each test:
[0155] Time deviation: The time deviation between each node and the coordinator node is used to calculate the synchronization accuracy.
[0156] Fault recovery time: The time required for the system to recover to a normal synchronization state after a node fails.
[0157] System Load: Records the CPU and memory usage of the node under different loads to evaluate the resource consumption of the protocol.
[0158] 5. Result verification and optimization
[0159] Objective: To verify the effectiveness of the protocol based on the collected data and to perform necessary optimizations.
[0160] Method: Analyze the test results and evaluate the synchronization accuracy and robustness of the protocol under different conditions. If the synchronization accuracy is found to be below the standard in some cases, the following optimization measures can be considered:
[0161] Adjust heartbeat frequency: Dynamically adjust the frequency of sending heartbeat signals based on network latency and node load to improve the timeliness of fault detection.
[0162] Optimize time adjustment strategy: Design a more sophisticated time adjustment strategy based on the time deviation of the node to avoid large time jumps.
[0163] The specific examples are as follows:
[0164] Assume a simulated environment with five nodes, A, B, C, D, and E, all running the implemented time synchronization protocol. First, build a cluster of Docker containers, with each container representing a node. Next, write code to implement timestamp exchange and the Raft algorithm to ensure that the nodes can communicate with each other and synchronize their time.
[0165] During the testing phase, multiple test cases are designed. For example, under a network latency of 50ms, the time deviation between each node and the coordinator is recorded. Assume that during the test, the time deviation of node A is 5ms, node B is 7ms, node C is 3ms, node D is 6ms, and node E is 4ms. By calculating the average of these deviations, the synchronization accuracy of the protocol can be evaluated.
[0166] In the failure test, we simulated the failure of node A and recorded the time it took for the system to re-elect a coordinator. If the system successfully elected a new coordinator and restored synchronization within 5 seconds, it indicates that the protocol performed well in handling failures.
[0167] It is convenient to ensure that the synchronization accuracy and robustness of the protocol under different scales and loads are verified.
[0168] The following describes a high-precision clock synchronization device based on a distributed protocol provided by the present invention. The high-precision clock synchronization device based on a distributed protocol described below and the high-precision clock synchronization method based on a distributed protocol described above can refer to each other.
[0169] like Figure 7 As shown, in one embodiment, a high-precision clock synchronization device based on a distributed protocol includes an exchange module 710 , an adjustment module 720 , a determination module 730 and a feedback module 740 .
[0170] The exchange module 710 is used to periodically exchange the timestamps of nodes in the distributed system and calculate the time difference with other nodes so that each node can detect the time deviation between itself and other nodes.
[0171] The adjustment module 720 is used to calculate the global time deviation based on a distributed consensus algorithm, and enable nodes to gradually adjust their clocks to achieve global synchronization.
[0172] The determination module 730 is used to determine a new coordinator node after a node fails based on a redundancy mechanism and a fault detection algorithm.
[0173] The feedback module 740 is used for each node to feed back the current time status and adjustment result to the coordinator node after completing the time adjustment. The coordinator node determines to make further adjustments or elect a new coordinator based on the feedback information.
[0174] In this embodiment, the switching module 710 is specifically configured to:
[0175] Each node exchanges timestamps with its neighboring nodes at fixed time intervals;
[0176] Store the time it takes each node to receive a response from a neighboring node, and calculate the round-trip time and time deviation between two nodes;
[0177] Get multiple time deviation values of a node within a preset time period and calculate the average value.
[0178] In this embodiment, the switching module 710 is further specifically configured to:
[0179] The calculated time offset is stored locally and updated periodically.
[0180] In this embodiment, the adjustment module 720 is specifically configured to:
[0181] Set the node to be in follower state at startup;
[0182] In response to a node not receiving a heartbeat signal from the leader within a certain period of time, the node transitions to the candidate state and requests votes from other nodes;
[0183] In response to the candidate node gaining support from more than half of the nodes, the node transitions to the leader state;
[0184] The leader node sends a request to all follower nodes. After receiving the request, each follower node returns the locally calculated time offset to the leader node. The leader node stores the time offset information of all follower nodes.
[0185] The leader node averages or weighted averages all time deviation values to calculate the global time deviation;
[0186] The leader node sends the global time deviation to all follower nodes. Each follower node gradually adjusts its local clock according to the instructions of the leader node to achieve global time synchronization.
[0187] In this embodiment, each follower node gradually adjusts its local clock according to the instructions of the leader node to achieve global time synchronization, specifically for:
[0188] Each node calculates the adjustment range based on its own current time and the global time deviation;
[0189] After receiving the adjustment instruction, each node gradually adjusts its local clock according to the set adjustment range.
[0190] In this embodiment, each follower node gradually adjusts its local clock according to the instructions of the leader node to achieve global time synchronization, and is also specifically used to:
[0191] After each adjustment, the node pauses for a period of time and obtains real-time information about the system's operating status and application response;
[0192] In response to a system or application exception, the node pauses adjustment and performs status evaluation.
[0193] In this embodiment, the determination module 730 is specifically configured to:
[0194] Give each node a priority during initialization;
[0195] Each node periodically sends heartbeat signals to other nodes to confirm its online status;
[0196] In response to a node not receiving a heartbeat signal within a predetermined time, the node is marked as a failed node, and other candidate coordinator nodes are transformed into an election state;
[0197] The candidate coordinator node sends its priority information to other nodes. Other nodes vote based on the priority and select the node with the highest priority as the new coordinator.
[0198] In response to the new coordinator node obtaining votes from more than half of the nodes, the new coordinator node sends a message to all nodes that it has become the new coordinator.
[0199] This high-precision clock synchronization device based on a distributed protocol, on the one hand, ensures the consistency of each node in the time adjustment process by utilizing a distributed consistency algorithm, thereby avoiding the single point failure risk brought by a centralized method. At the same time, it uses the calculated global time deviation to enable the node to gradually adjust its clock to achieve a global synchronization strategy, and determines the new coordinator node after the node fails based on the redundancy mechanism and fault detection algorithm. This can ensure that in the event of node failure, the system can still maintain high-precision time synchronization, which will greatly improve the performance of distributed systems in high-availability and high-precision applications. On the other hand, the feedback mechanism performed by each node after completing the time adjustment is conducive to timely detection of problems and adjustment.
[0200] Figure 12 The following is a schematic diagram of the physical structure of an electronic device. The electronic device may be a smart terminal, and its internal structure diagram may be as follows: Figure 12As shown. The electronic device includes a processor, a memory, and a network interface connected via a system bus. The processor of the electronic device is used to provide computing and control capabilities. The memory of the electronic device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and computer program in the non-volatile storage medium. The network interface of the electronic device is used to communicate with an external terminal via a network connection. When the computer program is executed by the processor, a high-precision clock synchronization method based on a distributed protocol is implemented, the method comprising:
[0201] Periodically exchange the timestamps of nodes in the distributed system and calculate the time difference with other nodes so that each node can detect the time deviation between itself and other nodes;
[0202] Based on the distributed consensus algorithm, the global time deviation is calculated and the nodes are gradually adjusted to achieve global synchronization.
[0203] Determine the new coordinator node after node failure based on the redundancy mechanism and fault detection algorithm;
[0204] After completing the time adjustment, each node feeds back the current time status and adjustment results to the coordinator node. The coordinator node determines whether to make further adjustments or elect a new coordinator based on the feedback information.
[0205] Those skilled in the art will understand that Figure 12 The structure shown in the figure is merely a block diagram of a portion of the structure related to the solution of the present invention, and does not constitute a limitation on the electronic device to which the solution of the present invention is applied. The specific electronic device may include more or fewer components than shown in the figure, or combine certain components, or have a different component arrangement.
[0206] On the other hand, the present invention also provides a computer storage medium storing a computer program, which, when executed by a processor, implements a high-precision clock synchronization method based on a distributed protocol, the method comprising:
[0207] Periodically exchange the timestamps of nodes in the distributed system and calculate the time difference with other nodes so that each node can detect the time deviation between itself and other nodes;
[0208] Based on the distributed consensus algorithm, the global time deviation is calculated and the nodes are gradually adjusted to achieve global synchronization.
[0209] Determine the new coordinator node after node failure based on the redundancy mechanism and fault detection algorithm;
[0210] After completing the time adjustment, each node feeds back the current time status and adjustment results to the coordinator node. The coordinator node determines whether to make further adjustments or elect a new coordinator based on the feedback information.
[0211] In yet another aspect, a computer program product or computer program is provided, the computer program product or computer program including computer instructions stored in a computer-readable storage medium. A processor of an electronic device reads the computer instructions from the computer-readable storage medium, and when the processor executes the computer instructions, implements a high-precision clock synchronization method based on a distributed protocol, the method comprising:
[0212] Periodically exchange the timestamps of nodes in the distributed system and calculate the time difference with other nodes so that each node can detect the time deviation between itself and other nodes;
[0213] Based on the distributed consensus algorithm, the global time deviation is calculated and the nodes are gradually adjusted to achieve global synchronization.
[0214] Determine the new coordinator node after node failure based on the redundancy mechanism and fault detection algorithm;
[0215] After completing the time adjustment, each node feeds back the current time status and adjustment results to the coordinator node. The coordinator node determines whether to make further adjustments or elect a new coordinator based on the feedback information.
[0216] Those skilled in the art will understand that all or part of the processes in the above-mentioned embodiment methods can be implemented by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided by the present invention may include non-volatile and / or volatile memory. Non-volatile memory may include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory may include random access memory (RAM) or external cache memory.
[0217] By way of illustration and not limitation, RAM is available in many forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), Synchronous Link DRAM (SLDRAM), Rambus direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), etc.
[0218] The technical features of the above embodiments can be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0219] The above-described embodiments merely illustrate several embodiments of the present invention, and while the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of the present invention. It should be noted that a person skilled in the art would be able to make various modifications and improvements without departing from the spirit of the present invention, and these modifications and improvements fall within the scope of the present invention. Therefore, the scope of the present invention shall be determined by the appended claims.
Claims
1. A high-precision clock synchronization method based on a distributed protocol, characterized in that: The method comprises: Periodically exchange the timestamps of nodes in the distributed system and calculate the time difference with other nodes so that each node can detect the time deviation between itself and other nodes; Based on the distributed consensus algorithm, the global time deviation is calculated, and the nodes are gradually adjusted to achieve global synchronization; Determine the new coordinator node after node failure based on the redundancy mechanism and fault detection algorithm; After completing the time adjustment, each node will feedback the current time status and adjustment results to the coordinator node. The coordinator node will determine whether to make further adjustments or elect a new coordinator based on the feedback information.
2. The high-precision clock synchronization method based on a distributed protocol according to claim 1, characterized in that: The periodic exchange of timestamps of nodes in the distributed system and calculation of time differences with other nodes so that each node can detect the time deviation between itself and other nodes includes: Each node exchanges timestamps with its neighboring nodes at fixed time intervals; Store the time each node takes to receive a response from a neighboring node, and calculate the round-trip time and time deviation between two nodes; Get multiple time deviation values of a node within a preset time period and calculate the average value.
3. The high-precision clock synchronization method based on a distributed protocol according to claim 2, characterized in that: The method of periodically exchanging the timestamps of nodes in the distributed system and calculating the time difference with other nodes so that each node can detect the time deviation between itself and other nodes also includes: The calculated time offset is stored locally and updated periodically.
4. The high-precision clock synchronization method based on a distributed protocol according to claim 3 is characterized in that: The distributed consensus algorithm is used to calculate the global time deviation and enable the nodes to gradually adjust their clocks to achieve global synchronization, including: Set the node to be in follower state at startup; In response to a node not receiving a heartbeat signal from the leader within a certain period of time, the node changes to the candidate state and requests votes from other nodes; In response to the candidate node gaining support from more than half of the nodes, the node transitions to the leader state; The leader node sends a request to all follower nodes. After receiving the request, each follower node returns the locally calculated time deviation to the leader node. The leader node stores the time deviation information of all follower nodes. The leader node averages or weighted averages all time deviation values to calculate the global time deviation; The leader node sends the global time deviation to all follower nodes, and each follower node gradually adjusts its local clock according to the instructions of the leader node to achieve global time synchronization.
5. The high-precision clock synchronization method based on a distributed protocol according to claim 4 is characterized in that: Each follower node gradually adjusts its local clock according to the instructions of the leader node to achieve global time synchronization, including: Each node calculates the adjustment range based on its own current time and the global time deviation; After receiving the adjustment instruction, each node gradually adjusts its local clock according to the set adjustment range.
6. The high-precision clock synchronization method based on a distributed protocol according to claim 5, characterized in that: Each follower node gradually adjusts its local clock according to the instructions of the leader node to achieve global time synchronization, including: After each adjustment, the node pauses for a while and obtains real-time information about the system operation status and application response; In response to a system or application exception, the node pauses adjustment and performs status assessment.
7. The high-precision clock synchronization method based on a distributed protocol according to any one of claims 1 to 6, characterized in that: Determining a new coordinator after a node fails based on a redundancy mechanism and a fault detection algorithm includes: Give each node a priority during initialization; Each node periodically sends heartbeat signals to other nodes to confirm its online status; In response to a node not receiving a heartbeat signal within a predetermined time, the node is marked as a failed node, and other candidate coordinator nodes are changed to an election state; The candidate coordinator node sends its priority information to other nodes. Other nodes vote based on the priority and select the node with the highest priority as the new coordinator. In response to the new coordinator node obtaining votes from more than half of the nodes, the new coordinator node sends a message to all nodes that it has become the new coordinator.
8. A high-precision clock synchronization device based on a distributed protocol, characterized in that: include: An exchange module is used to periodically exchange the timestamps of nodes in the distributed system and calculate the time difference with other nodes so that each node can detect the time deviation between itself and other nodes; The adjustment module is used to calculate the global time deviation based on the distributed consensus algorithm and enable the nodes to gradually adjust their clocks to achieve global synchronization; A determination module is used to determine a new coordinator node after a node fails based on a redundancy mechanism and a fault detection algorithm; The feedback module is used for each node to feedback the current time status and adjustment results to the coordinator node after completing the time adjustment. The coordinator node determines to make further adjustments or elect a new coordinator based on the feedback information.
9. An electronic device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the high-precision clock synchronization method based on a distributed protocol according to any one of claims 1 to 7 are implemented.
10. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the steps of the high-precision clock synchronization method based on a distributed protocol according to any one of claims 1 to 7 are implemented.