Communication methods and devices
By identifying and sending the unexecuted Redis database command number through the main thread of the lib library, the problem of network resources being occupied by a large amount of data synchronization is solved, and efficient data synchronization is achieved under network oscillation or short disconnection time.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- NEW H3C TECH CO LTD
- Filing Date
- 2022-05-23
- Publication Date
- 2026-05-05
AI Technical Summary
In the event of network instability or short-term disconnection, network resources are occupied by a large amount of data synchronization, resulting in the loss of other business communication messages, and the Redis database is unable to respond to normal write commands in a timely manner.
After the Redis database connection is lost, the main thread of the lib library obtains the number of the last successful operation command, identifies it as being within the command number range, and retrieves and sends the business data corresponding to the unexecuted command number to the Redis database, reducing duplicate writes.
In cases of network oscillations or short disconnection periods, network devices do not need to write all data to the Redis database, which greatly shortens the update time, reduces network pressure, and solves the problem of network resources being occupied by large amounts of data synchronization.
Smart Images

Figure CN115033554B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of communication technology, and in particular to a communication method and apparatus. Background Technology
[0002] Currently, Redis databases can be used to store business-related data on network devices in real time. When a network device restarts abnormally, data can be read from the Redis database to restore business operations. Redis is a high-performance key-value database that allows for operations such as adding, deleting, modifying, and querying data.
[0003] To utilize Redis database for service recovery after network device restarts, data needs to be written to the Redis database in real time while the network device is running normally to ensure consistency between the current data on the network device and the data in the Redis database. Specifically, when the network device disconnects from the Redis database, data cannot be written to the Redis database during the disconnection period. Upon subsequent reconnection of the network device to the Redis database, the network device will back up all current data to the Redis database.
[0004] When the network is unstable, frequent disconnections and reconnections occur. To ensure data consistency between the network device and the Redis database, the network device will frequently write all its current data to the Redis database. If the disconnection is short, only a few or dozens of write commands may fail. If the disconnection is long, tens of thousands of write commands may fail. When the network device reconnects to the Redis database, it may already contain tens or hundreds of thousands of records. All of these records will then be rewritten to maintain consistency. This process significantly consumes the network device's CPU and network bandwidth, and also greatly depletes the performance of the Redis database.
[0005] In a real-world network environment, when multiple network devices are backing up data to a Redis database, network instability will occur. At this time, network resources are consumed by a large amount of data synchronization, leading to the loss of other business communication packets, and the Redis database will also be unable to respond to normal write commands in a timely manner. Summary of the Invention
[0006] In view of this, this application provides a communication method and apparatus to solve the problem that when network resources are occupied by a large amount of data synchronization under network instability or short disconnection time, other business communication messages are lost, and the Redis database is unable to respond to normal write commands in a timely manner.
[0007] Firstly, this application provides a communication method applied to the main thread of a lib library. When the lib library is initialized by a business module, a storage buffer is created. This storage buffer stores the command number of each piece of business data to be sent to a Redis database. The command numbers of multiple pieces of business data form a command number range. The method includes:
[0008] When the worker thread created by the lib library loses connection with the Redis database and re-establishes connection, it obtains the first command number, which is the number of the last successful operation command executed by the Redis database.
[0009] Identify whether the first command number is within the command number range;
[0010] If it is in the range, then take the second command number as the starting number and the third command number within the command number range as the ending number to obtain all command numbers between the starting number and the ending number. The second command number is the number value that is adjacent to the first command number and is greater than the first command number. The third command number is the maximum value of the command number within the command number range.
[0011] The worker thread sends the first business data corresponding to each of the command numbers in the total number of commands to the worker thread, so that the worker thread encapsulates the first business data into a first operation command and sends it to the Redis database, thereby causing the Redis database to execute the first operation command again.
[0012] Secondly, this application provides a communication device applied within the main thread of a lib library. The lib library creates a storage buffer during initialization by a business module. This storage buffer stores the command number of each piece of business data to be sent to a Redis database. The command numbers of multiple pieces of business data constitute a command number range. The device includes:
[0013] The acquisition unit is used to acquire a first command number after the worker thread created by the lib library loses connection with the Redis database and re-establishes connection. The first command number is the number of the last successful operation command executed by the Redis database.
[0014] The identification unit is used to identify whether the first command number is within the command number range;
[0015] The acquisition unit is further configured to, if present, acquire all command numbers between the starting number and the ending number, using the second command number as the starting number and the third command number within the command number range as the ending number, wherein the second command number is the number value adjacent to the first command number and greater than the first command number, and the third command number is the maximum value of the command number within the command number range.
[0016] The sending unit is used to send the first business data corresponding to each of the all command numbers to the worker thread, so that the worker thread encapsulates the first business data into a first operation command and sends it to the Redis database, thereby causing the Redis database to execute the first operation command again.
[0017] Thirdly, this application provides a computer program product, including a computer program that can be executed by one or more processors to implement the communication method provided in the first aspect above.
[0018] Therefore, by applying the communication method and apparatus provided in this application, when the worker thread created by the lib library disconnects from the Redis database and re-establishes the connection, the main thread of the lib library obtains the first command number, which is the number of the last successful operation command executed by the Redis database. The lib library identifies whether the first command number is within the command number range. If it is, the main thread of the lib library uses the second command number as the starting number and the third command number within the command number range as the ending number to obtain all command numbers between the starting and ending numbers. The second command number is the number value adjacent to the first command number and greater than the first command number, and the third command number is the maximum value of the command number within the command number range. The main thread of the lib library sends the first business data corresponding to each command number in the total number of command numbers to the worker thread, so that the worker thread encapsulates the first business data into a first operation command and sends it to the Redis database, thereby causing the Redis database to execute the first operation command again.
[0019] Thus, in the event of network fluctuations or short disconnections, network devices do not need to write all data to the Redis database. They only need to write the data that was not written during the disconnection period, significantly reducing update time and network pressure. This solves the problem of network resources being occupied by large amounts of data synchronization, leading to the loss of other business communication packets, while the Redis database is unable to respond to normal write commands in a timely manner. Attached Figure Description
[0020] Figure 1 A flowchart illustrating the communication method provided in the embodiments of this application;
[0021] Figure 2 A schematic diagram of the storage buffer provided in the embodiments of this application;
[0022] Figure 3 A structural diagram of a communication device provided in an embodiment of this application;
[0023] Figure 4 The network device hardware structure provided in the embodiments of this application. Detailed Implementation
[0024] Exemplary embodiments will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numerals in different drawings denote the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this application. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this application as detailed in the appended claims.
[0025] The terminology used in this application is for the purpose of describing particular embodiments only and is not intended to be limiting of the application. The singular forms “a,” “the,” and “the” used in this application and the appended claims are also intended to include the plural forms unless the context clearly indicates otherwise. It should also be understood that the term “and / or” as used herein refers to and includes any or all possible combinations of one or more of the corresponding listed items.
[0026] It should be understood that although the terms first, second, third, etc., may be used in this application to describe various information, such information should not be limited to these terms. These terms are only used to distinguish information of the same type from one another. For example, without departing from the scope of this application, first information may also be referred to as second information, and similarly, second information may also be referred to as first information. Depending on the context, the word "if" as used herein may be interpreted as "when," "when," or "in response to determination."
[0027] The communication method provided in the embodiments of this application will be described in detail below. See also... Figure 1 , Figure 1 A flowchart illustrating a communication method provided in an embodiment of this application. This method is applied to the main thread of the lib library. The communication method provided in this application may include the following steps.
[0028] Step 110: When the worker thread created by the lib library loses connection with the Redis database and re-establishes connection, obtain the first command number, which is the number of the last successful operation command executed by the Redis database.
[0029] Specifically, the Redis lib library (hereinafter referred to as the lib library) resides within the process context of the business module. The lib library is a collection of functions that provides various APIs, enabling business modules within the network device to read and write to the Redis database by calling the APIs without needing to be aware of the connection and read / write details.
[0030] The process of reading and writing to the Redis database by the business module is described below, and only a brief description is given here.
[0031] Business modules may create multiple threads. When a particular thread needs to call an API provided by the lib library, that thread can be called the lib library main thread. It will call the initialization function provided by the lib library. The lib library's initialization function will create multiple worker threads. After obtaining the address information of the Redis database, it will send this information to these worker threads, instructing them to establish a connection with the Redis database based on the address information.
[0032] Once a connection is successfully established, the worker threads send a connection establishment notification message to the main thread of the lib library. After receiving the connection establishment notification message from each worker thread, the main thread of the lib library sends a notification message to the business module, so that the business module can determine that the Redis database is in a ready state.
[0033] When the business module receives a notification from the main thread of the lib library confirming that the Redis database is in a ready state, the business module calls a function provided by the lib library to pass the business data to be written to the Redis database to the lib library's main thread (in this embodiment, the business data includes the data to be written to the Redis database and the operation type, for example, a write operation). Based on different business data types, the lib library's main thread hashes the business data to the corresponding worker thread. Based on the operation type, the worker thread encapsulates the business data into an operation command and sends the operation command to the Redis database through the established connection. The Redis database executes the operation command. Specifically, the operation command is a write operation command.
[0034] In this embodiment, the business module also initializes the lib library using the initialization function provided by the lib library, and passes a parameter to the initialization function to guide the creation of the storage buffer size. After receiving the parameter, the lib library determines whether the value of the parameter is 0. If it is 0, it indicates that the business module does not need to use the buffer function; otherwise, it creates the buffer according to the parameter. The storage buffer is used to store business data to be written to the Redis database, the command number corresponding to the business data, the command execution result, etc. The size of the storage buffer can be specified by the user according to the memory availability of the network device, such as 50M or 100M, etc.
[0035] In the above process, worker threads can send a single operation command to the Redis database at once, or they can send multiple operation commands at once, for example, 100 operation commands at once. When executing multiple operation commands, the Redis database automatically returns the execution results of the multiple commands to the worker threads. The worker threads then send the execution results (success or failure, represented by a BOOL value) to the lib library main thread. Upon receiving this, the lib library main thread calls the lib library processing function, which stores the execution results of the multiple commands in a storage cache for later viewing. The execution results of multiple commands can then be viewed all at once, and the lib library main thread updates the first command number in real time based on the execution results.
[0036] After the main thread of the lib library receives business data from the business module, if the business data is to be written to the Redis database, the main thread of the lib library sets a command number for that business data. The command number is 64 bits in size, starting with a value of 1. Each time the business module sends out a piece of business data, the main thread of the lib library increments the command number by 1. For example, the command number for the first piece of business data is 1, and the command number for the second piece of business data is 2.
[0037] Then, the lib library main thread sends the business data and command number together to the corresponding worker thread. Based on the operation type, the worker thread encapsulates the business data into an operation command (specifically a write operation command). When a worker thread loses connection to the Redis database, it notifies the lib library main thread. Upon receiving the notification, the lib library main thread can still receive business data from the business modules, but it will no longer send data to any worker thread. When there is free storage space in the storage buffer, the lib library main thread stores the business data and command number together in the storage buffer.
[0038] Understandably, during the disconnection period, worker threads will no longer send operation commands to the Redis database.
[0039] In this embodiment, the disconnection time can be a few seconds or hundreds of milliseconds. After a few seconds or hundreds of milliseconds, the worker thread re-establishes a connection with the Redis database and notifies the lib library main thread. The lib library main thread determines that all worker threads have established a connection with the Redis database and then obtains the first command number. The first command number is the command number of the last successful operation command executed by the Redis database; all operation commands before this command number have been executed successfully.
[0040] Optionally, the storage buffer consists of multiple structure arrays, each storing business data, the command number corresponding to the business data, and the command execution result. The lib library's main thread also creates a variable within the storage buffer. This variable stores the command number of the last successfully executed operation command in the Redis database. This command number is the highest number of the currently successfully executed operation command, and all operation commands corresponding to command numbers preceding it should have been executed successfully.
[0041] After Redis returns the results of multiple command executions to the worker threads, the worker threads then send these results to the main thread of the lib library. The main thread of the lib library stores the execution result of each operation command in a structure array corresponding to the command number.
[0042] The process by which the above-mentioned lib library stores operation commands and command numbers into the storage buffer is only a simplified description and will be described in detail in subsequent embodiments.
[0043] Step 120: Identify whether the first command number is within the command number range.
[0044] Specifically, according to the description of step 110, after the lib library main thread obtains the first command number, it identifies whether the first command number is within the command number range.
[0045] If the first command number is within the command number range, the lib library executes step 130.
[0046] As described in the preceding steps, the main thread of the lib library stores the command numbers in a storage buffer, thus forming a range of command numbers within the storage buffer. This range includes the minimum and maximum command numbers.
[0047] For example, if the storage cache has a capacity of 1000 business data entries and currently stores 500 entries, and the command number range within the storage cache is [5000, 5499], then the minimum command number is 5000 and the maximum command number is 5499. The main thread of the lib library compares the first command number with the minimum and maximum command numbers to determine whether the first command number falls within the command number range.
[0048] If the first command number is 5200, the lib library determines that the first command number is within the command number range and executes step 130.
[0049] Step 130: If the condition is met, then take the second command number as the starting number and the third command number within the command number interval as the ending number to obtain all command numbers between the starting number and the ending number. The second command number is the number value that is adjacent to the first command number and is greater than the first command number. The third command number is the maximum value of the command number within the command number interval.
[0050] Specifically, according to the description of step 120, if the lib library main thread determines that the first command number is within the command number range, then the lib library main thread will use the second command number as the starting number and the third command number as the ending number to obtain all command numbers between the starting number and the ending number.
[0051] The second command number is the number value that is adjacent to the first command number and is greater than the first command number, and the third command number is the maximum value of the command number within the command number range.
[0052] For example, according to the previous example, the first command number is 5200, and the maximum value of the command number is 5499. Then the second command number is 5201, and the third command number is 5499. That is, the starting number is 5201, and the ending number is 5499. The main thread of the lib library obtains all command numbers between 5201 and 5499.
[0053] It should be noted that when there is free storage space in the storage buffer, the main thread of the lib library can receive the business data to be written to the Redis database from the business module, regardless of whether the worker thread is connected to or disconnected from the Redis database, and store the business data and command number in the storage buffer.
[0054] In the example above, the last successfully executed operation command in the Redis database is numbered 5200, indicating that the operation commands corresponding to command numbers between 5201 and 5499 were not successfully executed by the Redis database. One possibility is that the Redis database failed to execute the command, and another possibility is that due to a disconnection, the worker thread did not send the command to the Redis database.
[0055] Regardless of the reason mentioned above, the main thread of the lib library determines that all business data after 5200 needs to be sent to the Redis database again, while all business data before 5200, including 5200, has been executed successfully and does not need to be sent to the Redis database again.
[0056] Step 140: Send the first business data corresponding to each command number in the total number of command numbers to the worker thread, so that the worker thread encapsulates the first business data into a first operation command and sends it to the Redis database, thereby causing the Redis database to execute the first operation command again.
[0057] Specifically, according to the description in step 130, after the lib library main thread determines all command numbers, it obtains and sends the first business data corresponding to each command number in the total number of command numbers to the worker process.
[0058] The worker thread encapsulates the first business data into a first operation command based on the operation type and sends it to the Redis database, thereby causing the Redis database to execute the first operation command again.
[0059] like Figure 2 As shown, Figure 2 This is a schematic diagram of a storage buffer provided in an embodiment of this application. Figure 2 In the data, the business data corresponding to 5000-5200 has been executed successfully; the business data corresponding to 5201-5499 has failed to be executed; the remaining space is free storage space.
[0060] Therefore, by applying the communication method provided in this application, when the worker thread created by the lib library disconnects from the Redis database and reconnects, the main thread of the lib library obtains the first command number, which is the number of the last successful operation command executed by the Redis database. The main thread of the lib library identifies whether the first command number is within the command number range. If it is, the main thread of the lib library uses the second command number as the starting number and the third command number within the command number range as the ending number to obtain all command numbers between the starting and ending numbers. The third command number is the maximum value of the command numbers within the command number range. The main thread of the lib library sends the first business data corresponding to each command number in the total number of command numbers to the worker thread. The worker thread encapsulates the first business data into a first operation command and sends it to the Redis database, thereby causing the Redis database to execute the first operation command again.
[0061] Thus, in the event of network fluctuations or short disconnections, network devices do not need to write all data to the Redis database. They only need to write the data that was not written during the disconnection period, significantly reducing update time and network pressure. This solves the problem of network resources being occupied by large amounts of data synchronization, leading to the loss of other business communication packets, while the Redis database is unable to respond to normal write commands in a timely manner.
[0062] Optionally, in the identification process of step 130 in the embodiments of this application, another situation is also included, namely, the case where the lib library determines that the first command number is not within the command number range.
[0063] Specifically, according to the description of step 120, after the lib library main thread obtains the first command number, it identifies whether the first command number is within the command number range.
[0064] If the first command number is not within the command number range, the lib library main thread sends a first notification message to the business module, enabling the business module to determine that the Redis database is in a ready state based on the first notification message. The business module will then send all business data to the lib library main thread; this all business data consists of all business data sent before the worker thread disconnected from the Redis database.
[0065] The main thread of the lib library sends all business data to the worker threads, so that the worker threads can encapsulate the business data into corresponding operation commands according to the operation type, and send all operation commands to the Redis database, thereby causing the Redis database to execute all operation commands again.
[0066] For example, if the storage cache has a capacity of 1000 business data entries and currently stores 1000 entries, and the command number range within the storage cache is [5000, 5999], then the minimum command number is 5000 and the maximum command number is 5999. The main thread of the lib library compares the first command number with the minimum and maximum command numbers to determine whether the first command number falls within the command number range.
[0067] If the first command number is 4000, the lib library main thread determines that the first command number is not within the command number range, and also determines that the operation commands corresponding to 4001-4999 are not stored in the storage buffer. At this point, the lib library main thread determines that all data needs to be synchronized to the Redis database (that is, from the first operation command sent to the Redis database by all worker threads to the current last operation command).
[0068] Optionally, before step 110 in the embodiments of this application, there is also a process in which the main thread of the lib library obtains the second business data and the command number of the second business data, and stores the obtained information into the storage buffer.
[0069] Specifically, as described in step 110, after the worker threads establish a connection with the Redis database, they send notifications to the lib library main thread. Upon receiving notification messages from each worker thread, the lib library main thread sends a notification message to the business module, enabling the business module to determine that the Redis database is in a ready state.
[0070] The business module passes the second business data to the main thread of the lib library. If the second business data is data to be written to the Redis database, the main thread of the lib library assigns a command number to each piece of the second business data. The main thread of the lib library sends the command number of the second business data to the corresponding worker thread. According to the operation type, the worker thread encapsulates each piece of second business data into a corresponding second operation command and sends the second operation command to the Redis database through the established connection.
[0071] The main thread of the lib library stores the second business data and the command number of the second business data into the storage buffer.
[0072] Optionally, the specific process by which the main thread of the lib library stores the second business data and its command number into the storage buffer is as follows:
[0073] The main thread of the lib library determines whether there is free storage space in the storage buffer; if there is free storage space in the storage buffer, the main thread of the lib library stores the second business data and the command number of the second business data into the storage buffer.
[0074] If there is no free storage space in the storage buffer, the lib library main thread retrieves the business data corresponding to the minimum command number from the storage buffer and deletes the business data corresponding to the minimum command number. The lib library main thread then stores the second business data and its command number into the storage buffer.
[0075] For example, according to the previous example, the storage cache has a capacity of 1000 business data entries. The command number range in the storage cache is [5000, 5999], and 1000 business data entries have already been stored. At this time, the command number of the second business data entry is 6000. The lib library main thread determines that there is no free storage space in the storage buffer, so the lib library main thread retrieves the business data corresponding to 5000 and deletes the business data corresponding to 5000 (of course, the command number 5000 is also deleted). The lib library main thread stores the second business data entry and its command number into the storage buffer. The current command number range in the storage cache is [5001, 6000].
[0076] Optionally, in this embodiment of the application, after the worker thread disconnects from the Redis database, the main thread of the lib library obtains the third business data and assigns a number to the command for the third business data, and stores the obtained information in the storage buffer.
[0077] Specifically, after the worker thread disconnects from the Redis database, it sends a notification to the lib library main thread to inform it that the connection has been severed. Once the lib library main thread confirms the disconnection, it does not immediately send a notification message to the business modules, ensuring they are unaware of the disconnection event. The business modules still perceive the Redis database as ready and continue to feed business data to the lib library main thread.
[0078] If the third business data is data to be written to the Redis database, the main thread of the lib library sets a command number for each piece of third business data. Since the worker threads have been disconnected from the Redis database, the main thread of the lib library will no longer send the third business data and command number to the corresponding worker threads.
[0079] The main thread of the lib library stores the third business data and its command number into the storage buffer.
[0080] Understandably, during the disconnection period, worker threads will no longer send operation commands to the Redis database.
[0081] Optionally, the specific process by which the main thread of the lib library executes the aforementioned third business data and stores the command number of the third business data in the storage buffer is as follows:
[0082] The main thread of the lib library determines whether there is free storage space in the storage buffer; if there is free storage space in the storage buffer, the main thread of the lib library stores the third business data and the command number of the third business data into the storage buffer.
[0083] If there is no free storage space in the storage buffer, the main thread of the lib library retrieves the business data corresponding to the minimum command number from the storage buffer and identifies whether the business data corresponding to the minimum command number has been successfully executed by the Redis database.
[0084] The main thread of the lib library checks the command execution results stored in the storage buffer to identify whether the business data corresponding to the smallest command number has been successfully executed by the Redis database.
[0085] If the business data corresponding to the minimum command number fails to execute, the main thread of the lib library retains the business data corresponding to the minimum command number and sets the connection status between the worker thread and the Redis database to an abnormal state (i.e., a disconnection event, with the Redis database in a not ready state).
[0086] When the main thread of the library determines that the Redis database is in a "not ready" state, it sends a second notification message to the business module. Upon receiving this second notification message, the business module confirms the "not ready" state and stops sending business data to the main thread of the library.
[0087] The main thread of the lib library deletes all business data stored in the storage buffer, as well as the command numbers of all business data.
[0088] Optionally, in this embodiment, after the lib library main thread clears the storage buffer, the process further includes sending a third notification message to the business module when all worker threads have re-established connections with the Redis database. The business module determines that the Redis database is in a ready state based on the third notification message and then passes all business data to the lib library main thread.
[0089] Specifically, as mentioned above, the lib library has cleared its storage buffer. After all worker threads have re-established connections with the Redis database, the lib main thread sends a third notification message to the business module. Based on this message, the business module determines that the Redis database is in a ready state and then passes all business data to the lib main thread.
[0090] The main thread of the lib library sends all business data to the worker threads, so that the worker threads can encapsulate the business data into corresponding operation commands according to the operation type, and send all operation commands to the Redis database, thereby causing the Redis database to execute all operation commands again.
[0091] In this embodiment, when the lib library main thread detects that the worker thread has lost connection with the Redis server, the lib library main thread traces the command number that failed to execute the Redis database command. This is because, when the lib library main thread detects the disconnection, the failure of the Redis database operation command may have already occurred.
[0092] For example, the storage buffer has a capacity of caching 1000 business data entries, and the current command number range in the storage buffer is [5000, 5999], meaning there is no free storage space in the storage buffer.
[0093] After the lib library's main thread detects that the worker threads have lost connection with the Redis database and confirms that all operation commands corresponding to 5900-5999 have failed, it does not notify the business module that the Redis database is in a "not ready" state. Therefore, the business module continues to send business data to the lib library's main thread. Since all operation commands before 5900 have executed successfully, the lib library's main thread deletes the data corresponding to the smallest command number each time it receives a piece of business data.
[0094] For example, when the main thread of the lib library obtains business data 1, the command number of which is 6000, the lib library deletes the business data corresponding to 5000 and stores business data 1 and command number 6000. At this time, the command number range in the storage buffer is [5001, 6000].
[0095] The lib library's main thread can repeat the above process until it retrieves the business data for command number 6900. Since the command number range in the storage buffer is [5900, 6899], and 1000 business data entries have already been stored, and the operation command corresponding to 5900 failed, the lib library's main thread will not delete the business data corresponding to 5900. This is because after subsequent worker threads re-establish a connection with the Redis database, the business data corresponding to each command number within the range [5900, 6899] needs to be sent to the Redis database again.
[0096] The main thread of the lib library retains the business data corresponding to 5900 and sets the connection status between the worker thread and the Redis database to an abnormal state (i.e., a disconnection event, with the Redis database in a notready state).
[0097] If the main thread of the library determines that the Redis database is in a "not ready" state, it sends a notification message to the business module. Upon receiving the notification message, the business module also determines that the Redis database is in a "not ready" state and stops sending business data to the main thread of the library. The main thread of the library then clears its storage buffer.
[0098] After the worker threads re-establish connections with the Redis database, the main thread of the library receives a notification message and checks whether all worker threads have re-established connections with the Redis database. If so, it sends a notification message to the business module. Upon receiving the notification message, the business module determines that the Redis database is in a ready state. The business module then sends all business data to the main thread of the library. The main thread of the library sends all business data to the worker threads, so that, based on the operation type, the worker threads encapsulate the business data into corresponding operation commands and send all operation commands to the Redis database, thereby causing the Redis database to execute all operation commands again.
[0099] Therefore, by applying the communication method provided in this application, when the worker thread created by the lib library disconnects from the Redis database and reconnects, the lib library obtains the first command number, which is the number of the last successful operation command executed by the Redis database. The lib library identifies whether the first command number is within the command number range. If it is, the worker thread uses the second command number as the starting number and the third command number within the command number range as the ending number to obtain all command numbers between the starting and ending numbers. The second command number is the number value adjacent to the first command number and greater than the first command number, and the third command number is the maximum value of the command number within the command number range. The lib library main thread sends the first business data corresponding to each command number in the total number of command numbers to the worker thread, so that the worker thread encapsulates the first business data into a first operation command and sends it to the Redis database, thereby causing the Redis database to execute the first operation command again.
[0100] Thus, in the event of network fluctuations or short disconnections, network devices do not need to write all data to the Redis database. They only need to write the data that was not written during the disconnection period, significantly reducing update time and network pressure. This solves the problem of network resources being occupied by large amounts of data synchronization, leading to the loss of other business communication packets, while the Redis database is unable to respond to normal write commands in a timely manner.
[0101] Based on the same inventive concept, embodiments of this application also provide a communication device corresponding to the communication method. See also Figure 3 , Figure 3 A communication device provided in this application embodiment is applied to the main thread of a lib library. The lib library creates a storage buffer when initialized by a business module. The storage buffer stores the command number of each piece of business data to be sent to a Redis database. The command numbers of multiple pieces of business data form a command number range. The device includes:
[0102] The first acquisition unit 310 is used to acquire a first command number when the worker thread created by the lib library loses connection with the Redis database and re-establishes connection. The first command number is the number of the last successful operation command executed by the Redis database.
[0103] The identification unit 320 is used to identify whether the first command number is within the command number range;
[0104] The second acquisition unit 330 is used to, if it is in the range of command numbers, acquire all command numbers between the starting number and the ending number, with the second command number as the starting number and the third command number within the range of command numbers as the ending number, wherein the second command number is the number value adjacent to the first command number and the number value is greater than the first command number, and the third command number is the maximum value of the command number within the range of command numbers.
[0105] The sending unit 340 is used to send the first business data corresponding to each of the all command numbers to the worker thread, so that the worker thread encapsulates the first business data into a first operation command and sends it to the Redis database, thereby causing the Redis database to execute the first operation command again.
[0106] Optionally, the sending unit 340 is further configured to send a first notification message to the service module if the database is not in a notready state, so that the service module determines that the Redis database is in a notready state.
[0107] The device further includes: a receiving unit (not shown in the figure), used to receive all business data sent by the business module before the working thread disconnected from the Redis database;
[0108] The sending unit 340 is further configured to send all the business data to the worker thread, so that the worker thread encapsulates all the business data into corresponding operation commands and sends all the operation commands to the Redis database, thereby causing the Redis database to execute all the operation commands again.
[0109] Optionally, the device further includes: a third acquisition unit (not shown in the figure), used to obtain the second business data and the command number of the second business data after the worker thread establishes a connection with the Redis database;
[0110] The first storage unit (not shown in the figure) is used to store the second service data and the command number of the second service data into the storage buffer.
[0111] Optionally, the first storage unit (not shown in the figure) is specifically used to determine whether there is any free storage space in the storage buffer;
[0112] If it exists, the second business data and the command number of the second business data are stored in the storage buffer;
[0113] If it does not exist, the business data corresponding to the minimum command number is retrieved from the storage buffer, the business data corresponding to the minimum command number is deleted, and the second business data and the command number of the second business data are stored in the storage buffer.
[0114] Optionally, the device further includes: a fourth acquisition unit (not shown in the figure), used to obtain the third business data and the command number of the third business data after the worker thread is disconnected from the Redis database;
[0115] The second storage unit (not shown in the figure) is used to store the third service data and the command number of the third service data into the buffer.
[0116] Optionally, the second storage unit (not shown in the figure) is specifically used to: determine whether there is free storage space in the storage buffer;
[0117] If it exists, the third service data and the command number of the third service data are stored in the storage buffer.
[0118] If it does not exist, retrieve the business data corresponding to the minimum command number from the storage buffer, and identify whether the business data corresponding to the minimum command number has been successfully executed by the Redis database;
[0119] If the execution fails, the business data corresponding to the minimum value of the command number will be retained, and the connection status between the worker thread and the Redis database will be set to an abnormal state.
[0120] A second notification message is sent to the business module so that the business module determines that the Redis database is in a not ready state and stops sending business data to the lib library main process;
[0121] Delete all business data stored in the storage buffer, as well as the command numbers of all business data.
[0122] Optionally, the sending unit 340 is further configured to send a third notification message to the worker thread after the worker thread re-establishes a connection with the Redis database, so that the worker thread can determine that the Redis database is in a ready state;
[0123] The receiving unit (not shown in the figure) is also used to receive all business data sent by the business module before the worker thread disconnected from the Redis database;
[0124] The sending unit 340 is further configured to send all the business data to the worker thread, so that the worker thread encapsulates all the business data into corresponding operation commands and sends all the operation commands to the Redis database, thereby causing the Redis database to execute all the operation commands again.
[0125] Therefore, by applying the communication device provided in this application, when the worker thread created by the lib library disconnects from the Redis database and reconnects, the main thread of the lib library obtains the first command number, which is the number of the last successful operation command executed by the Redis database. The lib library identifies whether the first command number is within the command number range. If it is, the main thread of the lib library uses the second command number as the starting number and the third command number within the command number range as the ending number to obtain all command numbers between the starting and ending numbers. The second command number is the number value adjacent to the first command number and greater than the first command number, and the third command number is the maximum value of the command number within the command number range. The main thread of the lib library sends the first business data corresponding to each command number in the total number of command numbers to the worker thread, so that the worker thread encapsulates the first business data into a first operation command and sends it to the Redis database, thereby causing the Redis database to execute the first operation command again.
[0126] Thus, in the event of network fluctuations or short disconnections, network devices do not need to write all data to the Redis database. They only need to write the data that was not written during the disconnection period, significantly reducing update time and network pressure. This solves the problem of network resources being occupied by large amounts of data synchronization, leading to the loss of other business communication packets, while the Redis database is unable to respond to normal write commands in a timely manner.
[0127] Based on the same inventive concept, embodiments of this application also provide a network device, such as... Figure 4 As shown, the system includes a processor 410, a transceiver 420, and a machine-readable storage medium 430. The machine-readable storage medium 430 stores machine-executable instructions that can be executed by the processor 410. The processor 410 is prompted by the machine-executable instructions to execute the communication method provided in the embodiments of this application. (The foregoing...) Figure 3 The communication device shown can be used as follows: Figure 4 The hardware structure of the network device shown is implemented.
[0128] The aforementioned computer-readable storage medium 430 may include random access memory (RAM) or non-volatile memory (NVM), such as at least one disk storage device. Optionally, the computer-readable storage medium 430 may also be at least one storage device located remotely from the aforementioned processor 410.
[0129] The processor 410 mentioned above can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components.
[0130] In this embodiment of the application, the processor 410 reads the machine-executable instructions stored in the machine-readable storage medium 430, and is prompted by the machine-executable instructions to enable the processor 410 itself and the transceiver 420 to execute the communication method described in the foregoing embodiment of the application.
[0131] In addition, this application provides a machine-readable storage medium 430 that stores machine-executable instructions. When called and executed by the processor 410, the machine-executable instructions cause the processor 410 itself and the transceiver 420 to execute the communication method described in the aforementioned application.
[0132] The specific implementation process of the functions and roles of each unit in the above device can be found in the implementation process of the corresponding steps in the above method, and will not be repeated here.
[0133] For the device embodiments, since they basically correspond to the method embodiments, the relevant parts can be referred to in the description of the method embodiments. The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this application according to actual needs. Those skilled in the art can understand and implement this without creative effort.
[0134] For the embodiments of communication devices and machine-readable storage media, since the methods involved are basically similar to those of the aforementioned method embodiments, the description is relatively simple, and relevant details can be found in the descriptions of the method embodiments.
[0135] The above description is merely a preferred embodiment of this application and is not intended to limit this application. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the scope of protection of this application.
Claims
1. A communication method, characterized in that, The method is applied to the main thread of the lib library. When the lib library is initialized by the business module, a storage buffer is created. This storage buffer stores the command number of each piece of business data to be sent to the Redis database. The command numbers of multiple pieces of business data form a command number range. The method includes: When the worker thread created by the lib library loses connection with the Redis database and re-establishes connection, it obtains the first command number, which is the number of the last successful operation command executed by the Redis database. Identify whether the first command number is within the command number range; If it is in the range, then take the second command number as the starting number and the third command number within the command number range as the ending number to obtain all command numbers between the starting number and the ending number. The second command number is the number value that is adjacent to the first command number and has a value greater than the first command number. The third command number is the maximum value of the command number within the command number range. The worker thread sends the first business data corresponding to each of the command numbers in the total number of commands to the worker thread, so that the worker thread encapsulates the first business data into a first operation command and sends it to the Redis database, thereby causing the Redis database to execute the first operation command again.
2. The method according to claim 1, characterized in that, The method further includes: If not, a first notification message is sent to the business module so that the business module can determine that the Redis database is in a ready state; Receive all business data sent by the business module before the worker thread disconnected from the Redis database; All business data is sent to the worker thread, so that the worker thread encapsulates all business data into corresponding operation commands and sends all operation commands to the Redis database, thereby causing the Redis database to execute all operation commands again.
3. The method according to claim 1, characterized in that, Before obtaining the first command number after the worker thread created by the lib library loses connection with the Redis database and re-establishes connection, the method further includes: After the worker thread establishes a connection with the Redis database, it obtains the second business data and the command number of the second business data. The second business data and its command number are stored in the storage buffer.
4. The method according to claim 3, characterized in that, The step of storing the second business data and its command number into the storage buffer specifically includes: Determine whether there is any free storage space in the storage buffer; If it exists, the second business data and the command number of the second business data are stored in the storage buffer; If it does not exist, the business data corresponding to the minimum command number is retrieved from the storage buffer, the business data corresponding to the minimum command number is deleted, and the second business data and the command number of the second business data are stored in the storage buffer.
5. The method according to claim 1, characterized in that, The method further includes: When the worker thread loses connection with the Redis database, it retrieves the third business data and the command number of the third business data. The third business data and the command number of the third business data are stored in the buffer.
6. The method according to claim 5, characterized in that, The step of storing the third service data and its command number in the buffer specifically includes: Determine whether there is any free storage space in the storage buffer; If it exists, the third service data and the command number of the third service data are stored in the storage buffer. If it does not exist, retrieve the business data corresponding to the minimum command number from the storage buffer, and identify whether the business data corresponding to the minimum command number has been successfully executed by the Redis database; If the execution fails, the business data corresponding to the minimum value of the command number will be retained, and the connection status between the worker thread and the Redis database will be set to an abnormal state. Send a second notification message to the business module so that the business module determines that the Redis database is in a notready state and stops sending business data to the lib library main process; Delete all business data stored in the storage buffer, as well as the command numbers of all business data.
7. The method according to claim 6, characterized in that, The method further includes: After the worker thread re-establishes a connection with the Redis database, a third notification message is sent to the worker thread so that the worker thread can determine that the Redis database is in a ready state. Receive all business data sent by the business module before the worker thread disconnected from the Redis database; All business data is sent to the worker thread, so that the worker thread encapsulates all business data into corresponding operation commands and sends all operation commands to the Redis database, thereby causing the Redis database to execute all operation commands again.
8. A communication device, characterized in that, The device is applied to the main thread of the lib library. The lib library creates a storage buffer when it is initialized by the business module. This storage buffer stores the command number of each piece of business data to be sent to the Redis database. The command numbers of multiple pieces of business data form a command number range. The device includes: The first acquisition unit is used to acquire a first command number after the worker thread created by the lib library loses connection with the Redis database and re-establishes connection. The first command number is the number of the last successful operation command executed by the Redis database. The identification unit is used to identify whether the first command number is within the command number range; The second acquisition unit is configured to, if in a given state, acquire all command numbers between the starting number and the ending number, using the second command number as the starting number and the third command number within the command number interval as the ending number, wherein the second command number is the number value adjacent to the first command number and greater than the first command number, and the third command number is the maximum value of the command number within the command number interval; The sending unit is used to send the first business data corresponding to each of the all command numbers to the worker thread, so that the worker thread encapsulates the first business data into a first operation command and sends it to the Redis database, thereby causing the Redis database to execute the first operation command again.
9. The apparatus according to claim 8, characterized in that, The sending unit is further configured to, if not in a ready state, send a first notification message to the business module so that the business module determines that the Redis database is in a ready state; The device further includes: a receiving unit, configured to receive all business data sent by the business module before the worker thread disconnected from the Redis database; The sending unit is further configured to send all the business data to the worker thread, so that the worker thread encapsulates all the business data into corresponding operation commands and sends all the operation commands to the Redis database, thereby causing the Redis database to execute all the operation commands again.
10. The apparatus according to claim 8, characterized in that, The device further includes: The third acquisition unit is used to obtain the second business data and the command number of the second business data after the worker thread establishes a connection with the Redis database; The first storage unit is used to store the second business data and the command number of the second business data into the storage buffer.
11. The apparatus according to claim 10, characterized in that, The first storage unit is specifically used to determine whether there is any free storage space in the storage buffer. If it exists, the second business data and the command number of the second business data are stored in the storage buffer; If it does not exist, the business data corresponding to the minimum command number is retrieved from the storage buffer, the business data corresponding to the minimum command number is deleted, and the second business data and the command number of the second business data are stored in the storage buffer.
12. The apparatus according to claim 9, characterized in that, The device further includes: The fourth acquisition unit is used to obtain the third business data and the command number of the third business data after the worker thread loses connection with the Redis database. The second storage unit is used to store the third service data and the command number of the third service data into the buffer.
13. The apparatus according to claim 12, characterized in that, The second storage unit is specifically used for: Determine whether there is any free storage space in the storage buffer; If it exists, the third service data and the command number of the third service data are stored in the storage buffer. If it does not exist, retrieve the business data corresponding to the minimum command number from the storage buffer, and identify whether the business data corresponding to the minimum command number has been successfully executed by the Redis database; If the execution fails, the business data corresponding to the minimum value of the command number will be retained, and the connection status between the worker thread and the Redis database will be set to an abnormal state. Send a second notification message to the business module so that the business module determines that the Redis database is in a notready state and stops sending business data to the lib library main process; Delete all business data stored in the storage buffer, as well as the command numbers of all business data.
14. The apparatus according to claim 13, characterized in that, The sending unit is further configured to send a third notification message to the worker thread after the worker thread re-establishes a connection with the Redis database, so that the worker thread can determine that the Redis database is in a ready state; The receiving unit is also used to receive all business data sent by the business module before the worker thread disconnected from the Redis database; The sending unit is further configured to send all the business data to the worker thread, so that the worker thread encapsulates all the business data into corresponding operation commands and sends all the operation commands to the Redis database, thereby causing the Redis database to execute all the operation commands again.
Citation Information
Patent Citations
Method and device for controlling SDN switch
CN104052687A
Message information management method and device, server and storage medium
CN114285807A