Multi-machine master-slave judgment method in local area network mode

By using a multi-machine communication method of heartbeat messages and permission application messages in the LAN mode, the master and slave machines are judged based on the start time of the device, and the problem of unstable master and slave judgment in the prior art is solved, and the master and slave mode with fast switching and high reaction speed is realized.

CN119603086APending Publication Date: 2025-03-11HAIYING ENTERPRISE GROUP
View PDF 5 Cites 0 Cited by

Patent Information

Application Number
CN202411738984.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-29
Publication Date
2025-03-11

AI Technical Summary

Technical Problem

In the existing LAN mode, the multi-master-slave determination method is unstable when the number of devices increases, lacks flexibility and adaptability, and cannot effectively determine the master and slave and complete the master and slave switching.

Method used

By defining the structure to send heartbeat messages and permission application messages, multi-machine communication is performed using multicast addresses, master-slave machines are judged based on the startup time of each device, and the device startup time is updated when master-slave switches.

Benefits of technology

It realizes the ability to quickly switch master-slave mode in LAN mode, improves the stability and response speed of the system, and meets the functional needs of multi-machine networks.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119603086A_ABST
    Figure CN119603086A_ABST
Patent Text Reader

Abstract

The invention relates to a multi-machine master-slave judgment method in a local area network mode, the judgment method judges a master machine and a slave machine in the local area network mode, meanwhile, a master-slave mode can be rapidly switched, and the judgment method comprises the following steps of defining a structural body, setting up an environment, designing a visual interface, designing an information environment, obtaining a judgment basis, screening heartbeats and judging the master and the slave. And switching master-slave and broadcast. The multi-machine master-slave judgment method is applied to multi-machine master judgment in any local area network mode, the response speed is extremely high when the multi-machine master-slave judgment method judges master and slave machines in the local area network mode, the master-slave mode can be rapidly switched, and the overall operation of the system is not affected.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of multi-machine communication in a local area network, and in particular to a multi-machine master-slave determination method in a local area network mode. Background Art

[0002] In actual production work, in the LAN mode, multiple computers or devices often need to work together, and the same program needs to be deployed in multiple devices, and these programs need to control the same machine. If different staff members simultaneously operate the programs in the computer to control the machine, the machine cannot work according to the actual working parameters due to the different control parameters sent, resulting in some unexpected consequences. Therefore, it is necessary to set up different computers. A certain computer is used as the host and can control the machine arbitrarily. The rest of the computers in the LAN are used as slaves, which can only view the working status of the machine but cannot control the machine, thereby realizing the master-slave separation of multiple machines. However, the existing more common master-slave determination method is to simply judge according to the startup time of the program to determine the master and slave. However, this method is relatively good when there are only two devices, but when the number of computer devices in the LAN increases, instability will occur, lack of flexibility and adaptability, and cannot meet the actual functional requirements. Therefore, it is necessary to redesign it and optimize the algorithm process. Of course, there are also some relatively complex master-slave judgment methods, such as Keepalived, ZooKeeper, etc., but the functions of these methods are not limited to the judgment between the master and the slave, but include more complex functions, such as Keepalived, which is mainly a high-availability solution based on the LInux platform, mainly providing load balancing and failover functions. It mainly uses the virtual routing redundancy protocol VRRP to implement failover services between multiple machines, which is relatively complex to implement; ZooKeeper is an open source distributed coordination service that provides efficient and reliable distributed coordination services for distributed applications. It mainly maintains cluster information by using a directory tree and determines the leader through an election process. It is mainly used in distributed systems; the implementation scenarios of these methods are relatively complex and have many functions, so they will not be repeated here.

[0003] However, according to the system test, the existing scheme has certain defects. Due to the lack of judgment conditions in the judgment process, it is easy to cause confusion when receiving multicast data, and it is impossible to effectively judge and process the startup time of the device in the multicast, resulting in the inability to judge the master and slave and complete the switching between the master and slave. As mentioned above, when testing this method, for the convenience of observation, the UI interface is used to observe the enabled state of the button to judge the master and slave. When using the initial method, according to the test results, when observing the interface button, the enabled state of the button is constantly jumping with the naked eye, indicating that the master-slave judgment is unstable, which may cause system bugs and fail to truly achieve master-slave separation;

[0004] Therefore, a solution is urgently needed to realize multicast communication in a multi-machine network; the time difference between the current device and other devices in the LAN is determined based on the startup time. A larger time indicates a later startup time, and vice versa. The device with the earliest startup time is set as the host; when the host suddenly crashes, other devices in the LAN can select the host again through judgment. Summary of the invention

[0005] In order to solve the above technical problems, the present invention provides a method for determining the master and slave of multiple machines in a local area network mode. The method determines the master and slave machines in the local area network mode and can quickly switch the master and slave modes, comprising the following steps:

[0006] Step S1: define a structure. To implement the master-slave function, two structures need to be defined, including one structure for sending heartbeat messages and another structure for sending permission applications.

[0007] Step S2: The environment is set up and the multicast routing of the Kylin system is started. After completion, data packets are sent to the multicast address.

[0008] Step S3: Visual interface design, using Qt to build a program development framework, adding a "permission application" button to the interface, and at the same time, adding a button as a control button for the software;

[0009] Step S4: Information environment design. Before filling in the local IP address in the data message, write a function to read the local IP address and store it in the list container. When using it, just read the data in the container.

[0010] Step S5: Obtain the judgment basis, send its own heartbeat to the multicast address, and use the function to read the local IP address and sysStartTime;

[0011] Step S6: Filter the heartbeats, receive the heartbeat messages of other devices in the multicast, the network end receives the heartbeats of other devices in the multicast, checks whether it is the heartbeat of the local device, only processes the heartbeat messages of other devices, and filters its own heartbeat;

[0012] Step S7: Determine the master and slave, and select the master and slave according to the startup time of each device;

[0013] Step S8: Switch between master and slave, and send permission application data message. If the current machine is a slave, but needs to control the software, then apply to be the master, click the "Permission Application" button, and send the permission application data message to the multicast address;

[0014] Step S9: broadcasting, receiving responses to permission requests. When a slave device in the local area network applies for software control permission, a permission request message will be sent to the local area network.

[0015] In one embodiment of the present invention, the fields defined in the structure of the heartbeat message in step S1 include: data classification identification, IP address, and system startup time in the current device.

[0016] In one embodiment of the present invention, the structure of sending permission application is consistent with the heartbeat message, but the data classification identifier is different. When applying for permission, the system startup time of the current device is changed to the time of applying for permission.

[0017] In one embodiment of the present invention, in step S3, when the current device is in the host state, the enable state of the control button is true, and control is performed; when the current device is in the slave state, the enable state of the control button is false, and control is unavailable.

[0018] In one embodiment of the present invention, in step S5, the data classification identifier of the heartbeat message is defined as 0x5000 to distinguish it from the permission application message; the multicast address is set to "239.255.255.250", and the port number for sending and receiving data messages is set to 60000. The above parameters are in a writable editing state.

[0019] In one embodiment of the present invention, in step S6, the heartbeat message is sent frequently. Under normal circumstances, the program startup time remains unchanged. Therefore, only one startup time of the device needs to be stored during processing to prevent multiple storages from causing misjudgment.

[0020] In one embodiment of the present invention, in step S7, the master-slave judgment logic is: after receiving the heartbeat of each device in the previous step and storing its startup time in the container, traverse the container in turn, find the minimum value therein, and then compare it with the startup time of the local machine. If the minimum value is greater than the startup time of the local machine, it is considered that the local machine is started the earliest and is set as the master machine, otherwise the local machine is a slave machine.

[0021] In one embodiment of the present invention, in step S8, the data classification identifier of the permission application data message is 0x5001, and the multicast address and port number are the same as the heartbeat message, but at this time, the program startup time needs to be changed to the current permission application time.

[0022] In one embodiment of the present invention, in step S9, after other devices in the local area network receive the data message, regardless of whether they are master or slave devices, they will change their own startup time to the permission application time + the local IP address, so that it is greater than the startup time of the device applying for permission, which is convenient for subsequent judgment.

[0023] The above-mentioned technical scheme of the present invention has the following advantages compared with the prior art: the multi-machine master-slave determination method described in the present invention is applied to multi-machine host determination in any local area network mode. It determines the master and slave machines in the local area network mode, has an extremely high response speed, can quickly switch the master-slave mode, and has no influence on the overall operation of the system. BRIEF DESCRIPTION OF THE DRAWINGS

[0024] In order to make the contents of the present invention more clearly understood, the present invention is further described in detail below based on specific embodiments of the present invention in conjunction with the accompanying drawings.

[0025] Figure 1 It is a flow chart of the multi-machine master-slave determination method in the local area network mode of the present invention. DETAILED DESCRIPTION

[0026] like Figure 1 As shown, the multi-machine host judgment logic in the LAN mode is shown. This embodiment provides a multi-machine master-slave judgment method in the LAN mode. The judgment method judges the master and slave machines in the LAN mode and can quickly switch the master-slave mode, including the following steps:

[0027] (I) This implementation optimizes the original algorithm, and also makes judgments based on the early or late start time of the program. First, define the structure. In this design process, two structures need to be defined to implement the master-slave function. One structure is used to send heartbeat messages, and the other structure is used to send permission requests. The following fields are defined in the heartbeat message structure:

[0028] 1. Data classification identification; used to distinguish the type of data;

[0029] 2. IP address; usually the last digit of the local IP address, used to distinguish which device sent the data; if the current IP address is: 192.168.0.11, then the field here is filled with 11, based on which device the message was sent;

[0030] 3. The system startup time in the current device; as the main basis for judgment, according to actual needs, the system startup time is converted into UTC time, that is, "Greenwich" standard time, to facilitate comparison. The smaller one represents an earlier startup time, and the larger one represents a later startup time.

[0031] The core fields mainly include the above categories. If there are other requirements, they can be added according to actual conditions.

[0032] The structure of sending permission application is consistent with the heartbeat message, but the data classification identifier is different. When applying for permission, the system startup time of the current device is changed to the time of applying for permission, and after other devices in the LAN receive the permission application instruction, the system startup time is changed to the permission application time + the last digit of the local IP address (to avoid the same time, to ensure that the startup time of all devices in the LAN is different).

[0033] (II) When setting up the second part of the environment, the multicast routing of the Kylin system is started. After completion, data packets can be sent to the multicast address. The process and judgment method are as follows:

[0034] According to the process Figure 1 As shown, multiple computers are deployed in the LAN. After the environment variables are configured, the same Qt program is imported into all computer devices, and then the Qt program is started in sequence. At this time, the startup time of each device is different. Once the program is started, it will send its own heartbeat message to the multicast address at a frequency of 10 times per second (the sending frequency of the heartbeat message can be modified according to actual needs). While sending its own heartbeat, it will also continuously receive the heartbeat messages of other devices in the multicast. When the heartbeat of other devices is received, the startup time of the device is stored in a container, and then the container is traversed to find the minimum value and compare it with the current device. If the startup time of the current device is less than the minimum value, it means that the startup time of the current device is the earliest, so the device will be used as the host in the LAN and can control the software; otherwise, it is considered that the startup time of the current device is later than that of a certain device in the LAN, and the current device should be set as a slave, which can only view the software status but not control the software.

[0035] If a slave needs to control the software, it can click the button to apply for permission and send a data message to the multicast address to apply for permission. At the same time, the time in the container is cleared and the startup time of each device is ready to be re-entered. At this time, the startup time of the device currently applying for permission is changed to the time of applying for permission. After receiving the data message, other devices in the group change their own startup time to the time when the other party applies for permission + the last digit of the local IP address to ensure that the startup time of each device is different, which is easy to judge. At this time, the startup time of the original host is changed to the time of applying for permission plus the IP of the device, which will be greater than the startup time of the device applying for permission, so the original host is downgraded to a slave, and the device applying for permission is promoted to a host, which can control the software.

[0036] If the current host is offline or destroyed for some reason, but other devices are working well, the remaining devices will continue to re-judge based on the heartbeat packets sent to the multicast and their respective system startup time, and then re-select the host and slave based on the judgment mechanism. It should be noted that this machine does not process its own heartbeat. When processing data packets, it should filter out its own heartbeat packets to ensure that it does not interfere with the judgment process.

[0037] Specifically, the construction includes:

[0038] Operating system: Kylin V10 operating system

[0039] Integrated development environment: Qt Creator 5.12 and above

[0040] Programming framework and language: Qt development framework, C++

[0041] LAN mode: Three or more computer devices are connected to each other through a switch. Set the IP addresses of all devices in the LAN to the same network segment and ensure that the devices can communicate normally. Use the ping command to check whether they can communicate normally.

[0042] Hard conditions: You must ensure that the time of all computers in the LAN is synchronized, or use the NTP service to synchronize the time. If the time of each device is not synchronized, it may cause misjudgment and lead to master-slave allocation errors.

[0043] Routing settings: Set up routing for each computer device, start multicast routing, and ensure that multicast communication can be normal

[0044] Open a terminal for each computer device and enter the following command lines in the terminal one by one:

[0045] sudo sysctl-wnet.ipv4.conf.default.mc_forwarding=1

[0046] sudo ifconfigenaphyt4i1 multicast

[0047] sudo sysctl-wnet.ipv4.ip_forward=1

[0048] sudorouteadd-net224.0.0.0netmask240.0.0.0devenaphyt4i1

[0049] sudo ipmaddradd239.255.255.250devenaphyt4i1

[0050] After completion, multicast routing will be started, and you can use the program to send data to and receive data from the multicast address. The multicast address used in this test is 239.255.255.250, which can be modified according to actual needs.

[0051] (III) To facilitate observation of test results, based on visualization requirements, Qt is used to build a program development framework, and a "permission application" button is added to the interface. At the same time, a button is added as a control button for the software. When in the host state, the enable state of the control button is true, and it can be controlled. When in the slave state, the enable state of the control button is false, and it cannot be controlled. For convenience, during the test, the master-slave setting is judged whether it is successful by observing the enable state of the control button.

[0052] (IV) To improve the portability and flexibility of the program and avoid hard coding in the source code, there are multiple devices in this test, and the IP address of each device is different. Therefore, before filling in the local IP address in the data message, write a function to read the local IP address and store it in the list container. When using it, just read the data in the container.

[0053] (V) The core code to implement this function is as follows. The following codes are all pseudo codes:

[0054] 1. Send its own heartbeat to the multicast address. Here, the data classification identifier of the heartbeat message is defined as 0x5000 to distinguish it from the permission application message; the multicast address is set to "239.255.255.250", and the port number for sending and receiving data messages is set to 60000. These parameters can be modified according to actual conditions. Since the IP address of each device is different, for convenience, a function is used to read the local IP address, and the IP address is stored in a list container. Here, list[3].toInt() is the last digit of the IP address, which is one of the bases for judgment; sysStartTime is the UTC time obtained when the local program is started. This time is used as the basic basis for judging the master and slave.

[0055] void slt_sendSysStartTimer()

[0056] {

[0057] login->dataclassFlag=0x5000;

[0058] if(list.size()>1)

[0059] {

[0060] login->ip=list[3].toInt();

[0061] }

[0062] login->UTCTime=sysStartTime;

[0063] multUDp->sendData(sendLoginBuf,sizeof(sendLoginBuf),"239.255.255.250",60000);

[0064] }

[0065] (VI) Receive the heartbeat messages from other devices in the multicast. The network end receives the heartbeat messages from other devices in the multicast and checks whether they are the heartbeats of the local device. It only processes the heartbeat messages from other devices and filters its own heartbeat. Since the heartbeat messages are sent frequently, under normal circumstances, the program startup time does not change. Therefore, when processing, only one startup time of the device needs to be stored to prevent multiple storage and misjudgment.

[0066] voidMiddleMain::slt_PeerStart(constchar*data)

[0067] {

[0068] quint64peerTime=login->UTCTime;

[0069] shortip=plogin->ip;

[0070] if(list.size()>1)

[0071] {

[0072] if(ip!=list[3])

[0073] {

[0074] / / If the time does not exist in the container, store the time in the container

[0075] }

[0076] }

[0077] else

[0078] {

[0079] perror("localIP is error");

[0080] }

[0081] }

[0082] (VII) According to the startup time of each device, the master and slave are selected. The judgment logic is: after receiving the heartbeat of each device in the previous step and storing its startup time in the container, traverse the container in turn to find the minimum value (because the UTC time is stored, the smaller the startup time, the earlier the startup time), and then compare it with the startup time of the local machine. If the minimum value is greater than the startup time of the local machine, it is considered that the local machine is started the earliest and is set as the host. Otherwise, the local machine is the slave. Here, the enabled state of the QPushButton button is used for observation. If the local machine is the host, the button can be controlled, otherwise the button cannot be controlled. If there is no data in the container, it means that there is no heartbeat message from other devices. At this time, it is considered that only the local machine is online in the current LAN and automatically becomes the host.

[0083] voidMiddleMain::slt_dealTimer()

[0084] {

[0085] quint64 MIN;

[0086] if(PeerV.size()>0)

[0087] {

[0088] MIN=PeerV[0];

[0089] for(QVector <quint64>::const_iterator iter=PeerV.begin(); iter! =PeerV.end();iter++)

[0090] {

[0091] / / Traverse the container to find the minimum value

[0092] }

[0093] / / Compare the minimum value found in the container with the startup time of the local machine. If the startup time of the local machine is shorter than the minimum value,

[0094] / / Then the machine is considered as the host, and the button enable state is set to true, otherwise, the enable state is false

[0095] }

[0096] else

[0097] {

[0098] / / If the container size is 0, it is assumed that there is only one device in the LAN, so the local machine automatically becomes the host and the button enable state is set to true

[0099] }

[0100] }

[0101] (VIII) Send permission application data message. If the current machine is a slave but needs to control the software, you can apply to be the host. Click the "Permission Application" button to send the permission application data message to the multicast address. The data classification identifier of the permission application data message is 0x5001. The multicast address and port number are the same as the heartbeat message. However, at this time, the program startup time needs to be changed to the current permission application time. It should be noted that when applying for permissions, the startup time of the device will be changed, so the container storing the startup time of other devices needs to be emptied and wait for it to be put back.

[0102] voidMiddleMain::on_m_PermissionRequest_clicked()

[0103] {

[0104] PeerV.clear(); / / Clear the container content and wait for the startup time of each device to be updated

[0105] prequest->dataclassFlag=0x5001;

[0106] if(list.size()>1)

[0107] {

[0108] prequest->ip=list[3].toInt();

[0109] }

[0110] quint64requestTime = QDateTime::currentDateTime().toMSecsSinceEpoch(); / / Get the time of current permission application

[0111] prequest->UTCTime=requestTime;

[0112] multUDp->sendData(sendRequest,sizeof(sendRequest),"239.255.255.250",60000);

[0113] }

[0114] (IX) Receive the response to the permission application. When a slave device in the local area network applies for software control permission, it will send a permission application message to the local area network. After other devices in the local area network receive the data message, regardless of whether they are master or slave, they will change their own startup time to the permission application time + the local IP address, so that it is greater than the startup time of the device applying for permission, which is convenient for subsequent judgment.

[0115] void slt_request(constchar*data)

[0116] {

[0117] / / Modify the startup time of this machine to the time of applying for permission + the IP address of this machine

[0118] sysStartTime=request->UTCTime+list[3].toInt();

[0119] }

[0120] The multi-machine master-slave determination method described in this embodiment can better complete these functions after optimization. The buttons on the observation interface will not jump, and the response speed of the master-slave switching is fast. Because in this method, a heartbeat message is sent to the multicast address every 100ms, and a multi-threaded processing method is adopted, plus the system judgment time, each processing time can be achieved within 200ms; when the master-slave switch occurs, the processing time is usually within 500ms. Even if the current host is down, the remaining devices in the multicast can react and process in a shorter time. The processing time is within 300ms to 500ms, which can meet the actual work project needs and achieve the expected goals.

[0121] Obviously, the above embodiments are merely examples for clear explanation and are not intended to limit the implementation methods. For those skilled in the art, other different forms of changes or modifications can be made based on the above description. It is not necessary and impossible to list all the implementation methods here. The obvious changes or modifications derived from these are still within the protection scope of the invention.

Claims

1. A method for determining the master and slave of multiple machines in a local area network mode, wherein the method determines the master and slave machines in the local area network mode and can quickly switch the master and slave modes, characterized in that: The steps include: Step S1: define a structure. To implement the master-slave function, two structures need to be defined, including one structure for sending heartbeat messages and another structure for sending permission applications. Step S2: The environment is set up and the multicast routing of the Kylin system is started. After completion, data packets are sent to the multicast address. Step S3: Visual interface design, using Qt to build a program development framework, adding a "permission application" button to the interface, and at the same time, adding a button as a control button for the software; Step S4: Information environment design. Before filling in the local IP address in the data message, write a function to read the local IP address and store it in the list container. When using it, just read the data in the container. Step S5: Obtain the judgment basis, send its own heartbeat to the multicast address, and use the function to read the local IP address and sysStartTime; Step S6: Filter the heartbeats, receive the heartbeat messages of other devices in the multicast, the network end receives the heartbeats of other devices in the multicast, checks whether it is the heartbeat of the local device, only processes the heartbeat messages of other devices, and filters its own heartbeat; Step S7: Determine the master and slave, and select the master and slave according to the startup time of each device; Step S8: Switch between master and slave, and send permission application data message. If the current machine is a slave, but needs to control the software, then apply to be the master, click the "Permission Application" button, and the permission application data message will be sent to the multicast address; Step S9: broadcasting, receiving responses to permission requests. When a slave device in the local area network applies for software control permission, a permission request message will be sent to the local area network.

2. The multi-machine master-slave determination method according to claim 1, characterized in that: The fields defined in the structure of the heartbeat message described in step S1 include: data classification identification, IP address, and system startup time in the current device.

3. The method for determining the master and slave status of multiple machines according to claim 1, wherein: The structure of the permission application is consistent with the heartbeat message, but the data classification identifier is different. When applying for permission, the system startup time of the current device is changed to the time of applying for permission.

4. The method for determining the master and slave status of multiple machines according to claim 1, wherein: In step S3, when the current device is in the master state, the enable state of the control button is true, and control is performed; when the current device is in the slave state, the enable state of the control button is false, and control is unavailable.

5. The method for determining the master and slave status of multiple machines according to claim 1, wherein: In step S5, the data classification identifier of the heartbeat message is defined as 0x5000 to distinguish it from the permission application message; the multicast address is set to "239.255.255.250", and the port number for sending and receiving data messages is set to 60000. The above parameters are in a writable editing state.

6. The method for determining the master and slave status of multiple machines according to claim 1, characterized in that: In step S6, the heartbeat message is sent frequently. Under normal circumstances, the program startup time remains unchanged, so only one startup time of the device needs to be stored during processing.

7. The method for determining the master and slave status of multiple machines according to claim 1, wherein: In step S7, the master-slave judgment logic is as follows: after receiving the heartbeat of each device in the previous step and storing its startup time in the container, traverse the container in turn to find the minimum value, and then compare it with the startup time of the local machine. If the minimum value is greater than the startup time of the local machine, it is considered that the local machine is started the earliest and is set as the master machine, otherwise the local machine is a slave machine.

8. The method for determining the master and slave status of multiple machines according to claim 1, characterized in that: In step S8, the data classification identifier of the permission application data message is 0x5001, and the multicast address and port number are the same as the heartbeat message, but at this time, the program startup time needs to be changed to the current permission application time.

9. The method for determining the master and slave status of multiple machines according to claim 1, characterized in that: In step S9, after receiving the data message, other devices in the local area network, regardless of whether they are master or slave, will change their own startup time to the permission application time + the local IP address, so that it is greater than the startup time of the device applying for permission.

Citation Information

Patent Citations

  • Master and slave switch method and device of task scheduling example

    CN107203420A

  • A method for realizing multi-cam pattern recognition

    CN109729305A

  • Automatic networking method and system for energy storage equipment

    CN116346749A

  • Master-slave network-based node determination method, network equipment and storage medium

    CN118523978A

  • Computing Instance Placement Using Estimated Launch Times

    US20160070590A1