CN-115033554-B - Communication method and device
Abstract
The application provides a communication method and a communication device, wherein the method comprises the steps of obtaining a first command number after a working thread created by a lib library is disconnected with a Redis database and connection is reestablished, identifying whether the first command number is in a command number interval, if so, taking a second command number as a starting number and a third command number in the command number interval as an ending number, obtaining all command numbers between the starting number and the ending number, and sending first service data corresponding to each command number in all command numbers to the working thread, so that the working thread packages the first service data into a first operation command and then sends the first operation command to the Redis database, and further the Redis database executes the first operation command again.
Inventors
- WAN QING
Assignees
- 新华三技术有限公司
Dates
- Publication Date
- 20260505
- Application Date
- 20220523
Claims (14)
- 1. A communication method, wherein the method is applied to a lib library main thread, the lib library creates a storage buffer when initialized by a service module, the storage buffer stores a command number of each service data to be sent to a Redis database, and the command numbers of a plurality of service data form a command number interval, the method comprising: When a working thread created by the lib library is disconnected with the Redis database and connection is reestablished, a first command number is obtained, wherein the first command number is the number of the last successful operation command executed by the Redis database; Identifying whether the first command number is within the command number interval; If the command is in the first command number, taking a second command number as a starting number, taking a third command number in the command number interval as an ending number, and acquiring all command numbers between the starting number and the ending number, wherein the second command number is a number value adjacent to the first command number and the number value is larger than the first command number, and the third command number is the maximum value of the command numbers in the command number interval; And sending first service data corresponding to each command number in all the command numbers to the working thread, so that the working thread packages the first service data into a first operation command and then sends the first operation command to the Redis database, and further the Redis database executes the first operation command again.
- 2. The method according to claim 1, wherein the method further comprises: If not, a first notification message is sent to the service module, so that the service module determines that the Redis database is in a ready state; receiving all service data which are sent by the service module and sent before the working thread is disconnected with the Redis database; And sending the all service data to the working thread, so that the working thread encapsulates the all service data into corresponding operation commands, and sends all operation commands to the Redis database, so that the Redis database executes all operation commands again.
- 3. The method of claim 1, wherein the method further comprises, after the lib library created worker thread disconnects from the Redis database and reestablishes the connection, before the first command number is obtained: After the working thread establishes connection with the Redis database, second service data and a command number of the second service data are acquired; And storing the second service data and the command number of the second service data into the storage buffer.
- 4. A method according to claim 3, wherein storing the second service data and the command number of the second service data in the memory buffer comprises: Judging whether a spare storage space exists in the storage buffer area or not; If so, storing the second service data and the command number of the second service data into the storage buffer; And if the service data does not exist, acquiring service data corresponding to the minimum command number from the storage buffer, deleting the service data corresponding to the minimum command number, and storing the second service data and the command number of the second service data into the storage buffer.
- 5. The method according to claim 1, wherein the method further comprises: When the working thread is disconnected with the Redis database, third service data and a command number of the third service data are acquired; And storing the third service data and the command number of the third service data into the buffer area.
- 6. The method according to claim 5, wherein storing the third service data and the command number of the third service data in the buffer comprises: Judging whether a spare storage space exists in the storage buffer area or not; If so, storing the third service data and the command number of the third service data into the storage buffer area and storing the third service data and the command number of the third service data into the storage buffer area; If not, acquiring service data corresponding to the minimum command number from the storage buffer area, and identifying whether the service data corresponding to the minimum command number is successfully executed by the Redis database; If the execution fails, reserving service data corresponding to the minimum value of the command number, and setting the connection state between the working thread and the Redis database as an abnormal state; Sending a second notification message to the service module, so that the service module determines that the Redis database is in a non ready state and does not send service data to the lib library main process; And deleting all the stored service data in the storage buffer area and the command numbers of all the service data.
- 7. The method of claim 6, wherein the method further comprises: After the working thread reestablishes connection with the Redis database, sending a third notification message to the working thread so that the working thread determines that the Redis database is in a ready state; receiving all service data which are sent by the service module and sent before the working thread is disconnected with the Redis database; And sending the all service data to the working thread, so that the working thread encapsulates the all service data into corresponding operation commands, and sends all operation commands to the Redis database, so that the Redis database executes all operation commands again.
- 8. A communication apparatus, the apparatus being applied to a lib library main thread, the lib library creating a memory buffer when initialized by a service module, the memory buffer storing command numbers of each service data to be transmitted to a Redis database, the command numbers of the plurality of service data constituting a command number section, the apparatus comprising: The first acquisition unit is used for acquiring a first command number after the working thread created by the lib library is disconnected with the Redis database and connection is reestablished, wherein the first command number is the number of the last successful operation command executed by the Redis database; The identification unit is used for identifying whether the first command number is in the command number interval; The second obtaining unit is used for obtaining all command numbers between the starting number and the ending number by taking a second command number as a starting number and taking a third command number in the command number interval as an ending number if the second command number is in the position, wherein the second command number is a number value adjacent to the first command number and the number value is larger than the first command number, and the third command number is the maximum value of the command numbers in the command number interval; And the sending unit is used for sending the first service data corresponding to each command number in all the command numbers to the working thread, so that the working thread packages the first service data into a first operation command and then sends the first operation command to the Redis database, and further the Redis database executes the first operation command again.
- 9. The apparatus of claim 8, wherein the sending unit is further configured to send a first notification message to the service module if the request is not in the ready state, so that the service module determines that the Redis database is in the ready state; The device also comprises a receiving unit, a processing unit and a processing unit, wherein the receiving unit is used for receiving all service data which is issued before the working thread and the Redis database are disconnected and sent by the service module; The sending unit is further configured to send the all service data to the working thread, so that the working thread encapsulates the all service data into corresponding operation commands, and sends all operation commands to the Redis database, so that the Redis database executes all operation commands again.
- 10. The apparatus of claim 8, wherein the apparatus further comprises: A third obtaining unit, configured to obtain second service data and a command number of the second service data after the working thread establishes a connection with the Redis database; And the first storage unit is used for storing the second service data and the command number of the second service data into the storage buffer.
- 11. The apparatus of claim 10, wherein the first storage unit is specifically configured to determine whether there is a free storage space in the storage buffer; If so, storing the second service data and the command number of the second service data into the storage buffer; And if the service data does not exist, acquiring service data corresponding to the minimum command number from the storage buffer, deleting the service data corresponding to the minimum command number, and storing the second service data and the command number of the second service data into the storage buffer.
- 12. The apparatus of claim 9, wherein the apparatus further comprises: a fourth obtaining unit, configured to obtain third service data and a command number of the third service data after the working thread is disconnected from the Redis database; And the second storage unit is used for storing third service data and command numbers of the third service data into the buffer area.
- 13. The apparatus of claim 12, wherein the second storage unit is specifically configured to: Judging whether a spare storage space exists in the storage buffer area or not; If so, storing the third service data and the command number of the third service data into the storage buffer area and storing the third service data and the command number of the third service data into the storage buffer area; If not, acquiring service data corresponding to the minimum command number from the storage buffer area, and identifying whether the service data corresponding to the minimum command number is successfully executed by the Redis database; If the execution fails, reserving service data corresponding to the minimum value of the command number, and setting the connection state between the working thread and the Redis database as an abnormal state; Sending a second notification message to the service module, so that the service module determines that the Redis database is in a non ready state and does not send service data to the lib library main process; And deleting all the stored service data in the storage buffer area and the command numbers of all the service data.
- 14. The apparatus of claim 13, wherein the sending unit is further configured to send a third notification message to the worker thread after the worker thread reestablishes the connection with the Redis database, so that the worker thread determines that the Redis database is in a ready state; The receiving unit is further used for receiving all service data which is sent by the service module and sent before the working thread is disconnected with the Redis database; The sending unit is further configured to send the all service data to the working thread, so that the working thread encapsulates the all service data into corresponding operation commands, and sends all operation commands to the Redis database, so that the Redis database executes all operation commands again.
Description
Communication method and device Technical Field The present application relates to the field of communications technologies, and in particular, to a communications method and apparatus. Background Presently, redis databases are available to store traffic-related data on network devices in real-time. When the network equipment is restarted, the data can be read from the Redis database and service recovery can be carried out. The Redis database is a high-performance key-value database, and can accept actions such as adding, deleting, changing, checking and the like on data. In order to recover the service after restarting the network equipment by using the Redis database, data is required to be written into the Redis database in real time when the network equipment is in normal operation so as to ensure that the current data in the network equipment is consistent with the data in the Redis database. In particular, when a network device disconnects from the Redis database, data cannot be written to the Redis database during the disconnection time. When the subsequent network device is reconnected to the Redis database, the network device will batch all the current data to the Redis database. When the network is unstable, a network disconnection reconnection often occurs. At this time, in order to ensure that the data in the network device is consistent with the data in the Redis database, the network device frequently writes all local current data into the Redis database. If the disconnection time is short, only a few or tens of write command operations may fail. If the disconnection time is long, tens of thousands of write command operations may fail. When the network device reestablishes connection with the Redis database, tens of thousands or hundreds of thousands of pieces of data may exist in the network device, and all the pieces of data are rewritten into the Redis database at this time, so that the data of the network device are consistent with the data of the Redis database. This action will then result in a significant occupation of the CPU, network bandwidth of the network device, while also greatly consuming the capabilities of the Redis database. In an actual network environment, when a plurality of network devices perform data backup to a Redis database, network oscillation occurs in the whole network. At this time, network resources are occupied by a large amount of data synchronization, so that other service communication messages are lost, and the Redis database cannot respond to normal writing commands in time. Disclosure of Invention In view of this, the present application provides a communication method and apparatus, which are used to solve the problem that network resources are occupied by a large amount of data synchronously under the condition of network oscillation or short disconnection time, so that other service communication messages are lost, and the Redis database cannot respond to normal write commands in time. In a first aspect, the present application provides a communication method, the method being applied to a lib library main thread, the lib library creating a storage buffer when initialized by a service module, the storage buffer storing command numbers of each service data to be sent to a Redis database, the command numbers of a plurality of service data constituting a command number interval, the method comprising: When a working thread created by the lib library is disconnected with the Redis database and connection is reestablished, a first command number is obtained, wherein the first command number is the number of the last successful operation command executed by the Redis database; Identifying whether the first command number is within the command number interval; If the command is in the first command number, the second command number is taken as a starting number, a third command number in the command number interval is taken as an ending number, all command numbers between the starting number and the ending number are obtained, the second command number is a number value adjacent to the first command number and the number value is larger than the first command number, and the third command number is the maximum value of the command numbers in the command number interval; And sending first service data corresponding to each command number in all the command numbers to the working thread, so that the working thread packages the first service data into a first operation command and then sends the first operation command to the Redis database, and further the Redis database executes the first operation command again. In a second aspect, the present application provides a communication device, the device being applied to a lib library main thread, the lib library creating a memory buffer when initialized by a service module, the memory buffer storing a command number of each service data to be sent to a Redis database, the command numbers of a plurality of service