Multi-dimensional state atomic recovery method and system with ring buffer and database backup
By assigning a globally unique identifier to each message in a long-connection real-time interactive system, and combining a two-level progressive compensation method of a fixed-length hot buffer queue and a persistent database, the problem of inconsistent state and integrity recovery during client disconnection and reconnection is solved, achieving atomic recovery and integrity determination at a single logical moment.
Patent Information
- Application Number
- CN202611209552.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-08-11
- Publication Date
- 2026-11-06
AI Technical Summary
In long-connection real-time interactive systems, when a client disconnects and reconnects, the message stream and multi-dimensional running state are obtained in stages, falling at different logical moments in the event sequence, resulting in inconsistent intermediate states and an inability to determine the integrity of the recovery.
A multi-dimensional state atomic recovery method with a circular buffer and database fallback is adopted. By assigning a globally unique message identifier to each message that monotonically increases over time, and combining a fixed-length hot buffer queue and a persistent database for two-level progressive compensation, a set of compensation messages is obtained. The runtime state of the role and the global context time are queried in parallel, and the messages are assembled into a single compensation event and sent out at once.
It achieves consistent recovery of message flow, role runtime state, and global context time at a single logical moment, and can ensure the integrity of the recovered data, avoiding inconsistent intermediate states and data loss.
Smart Images

Figure CN123055771A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of state recovery technology for real-time communication systems, specifically to a multi-dimensional state atomic recovery method and system with a ring buffer and database fallback. Background Technology
[0002] In multiplayer real-time interactive systems based on long-connection protocols, a persistent connection is maintained between the client and the server. The server uses this connection to push real-time messages and status updates to the client. Such systems are widely used in online desktop role-playing platforms, real-time collaboration tools, and multiplayer online virtual worlds. Their common characteristics include a continuously generated message stream and an evolving operational state that progresses with the messages, such as the virtual character's current scene, derived attributes like health points, temporary effects, equipment, and global context time. Due to network fluctuations, device hibernation, application switching, and other reasons, the connection between the client and server can be interrupted at any time. Clients typically have a built-in automatic reconnection mechanism to attempt to re-establish the connection after it is lost. After the connection is restored, the key challenge for such systems is ensuring that the client catches up on the messages missed during the disconnection and returns to an operational state consistent with the server.
[0003] There are generally two existing reconnection schemes. The first is a full reload, where the client requests a complete snapshot of the current state from the server after reconnecting and reloads all data. This scheme is simple to implement, but in scenarios with a large message volume, each reconnection requires the transmission and parsing of a large amount of historical data, even if the user is only disconnected for a few seconds, resulting in significant performance overhead. The second is incremental compensation based on message sequence numbers, where each message carries an incrementing sequence number. When the client reconnects, it reports the last received sequence number, and the server returns messages after that sequence number. This scheme reduces the amount of data transmission, but it usually only compensates for the message itself, without compensating for other runtime dimensions such as the client's role state and global time, which may lead to inconsistencies between messages and states.
[0004] Chinese patent application CN106714334A discloses a method, apparatus, and system for reconnecting after a disconnection. This method generates consecutive sequence numbers for messages sent from the server to the client according to the sending order, stores a fixed number of the most recent messages in a fixed-length message cache queue, and when the client reconnects and reports its last received sequence number, it determines whether the sequence number falls within the current sequence number range of the message cache queue. If it does, it sends messages after that sequence number to the client for command synchronization; if it does not, it sends complete client state data to the client for state synchronization. However, this solution models disconnection and reconnection as a message resending problem. Its fallback measure when the cache misses is to send a full state snapshot all at once, and when the cache hits, it only resends the message itself. In other words, this solution always chooses between resending the message and sending the full state, failing to restore the message stream missed during the disconnection, the role's runtime state, and the global context time as an inseparable whole at the same logical moment. Its fundamental limitation lies in the fact that the character's runtime state and global context time are essentially the result of folding and reducing the message event sequence one by one. The message stream and runtime state are not two objects that can be obtained independently, but rather projections of the same causal event sequence at different granularities. Once the message and state are obtained separately with two independent requests and responses, the two acquisitions will inevitably fall at different logical moments in the continuously advancing event sequence. As long as new events are written between these two acquisitions, the client will be updated to an intermediate state that has never actually existed on the server timeline. For example, the client may have received a message that the character has moved to a new scene, but the local display still shows the character in the original scene due to a lack of state synchronization. At the same time, this solution cannot inform the client whether the data restored is complete without relying on the success of the query itself. When the disconnection time is too long and some messages have exceeded the retention range of the message cache queue, the client has no way of knowing that it may have missed earlier messages, resulting in a break in the storyline it sees without its knowledge. These limitations mean that after reconnection, the client may enter an inconsistent intermediate state and cannot determine the completeness of the restoration result.
[0005] Therefore, there is a need in this field for a technical solution that can ensure the consistent recovery of missing messages and multidimensional operational states at a single logical moment when the client disconnects and reconnects, and make a deterministic determination of the integrity of the recovery. Summary of the Invention
[0006] Addressing the core bottleneck of existing long-connection real-time interactive systems where message streams and multi-dimensional operational states are acquired in stages, resulting in inconsistent intermediate states and uncertain integrity during recovery, this invention provides a multi-dimensional state atomic recovery method and system with a circular buffer and database fallback. This method assigns a globally unique message identifier to each message, monotonically increasing over time, as a common sampling coordinate. It employs a two-stage progressive compensation approach using a fixed-length hot buffer queue and a persistent database to obtain a compensation message set. During the same processing, it queries the role's runtime state and global context time in parallel. Finally, it packages the integrity flag obtained from comparing the oldest identifier with the aforementioned dimensions into a single compensation event and sends it out all at once. Under the constraint that a single storage medium cannot simultaneously achieve low latency and no data loss, this method achieves consistent recovery of message streams, role runtime states, and global context time, and ensures the determinability of recovery integrity, based on the principle of atomic snapshot recovery of multi-dimensional operational states at a single logical moment.
[0007] The technical solution of this invention is a multi-dimensional state atomic recovery method with a circular buffer and database fallback, comprising the following steps: assigning a globally unique message identifier to each message generated in each session of a long-connection real-time interactive system, which monotonically increases over time; writing the messages in the order of generation to a persistent database and then to a fixed-length hot buffer queue of the session, wherein the fixed-length hot buffer queue is truncated during writing to retain only the most recent preset number of messages; receiving a reconnection request sent by the client after re-establishing the connection, the reconnection request carrying a breakpoint identifier, wherein the breakpoint identifier is the message identifier of the last message received by the client before the disconnection; reading messages whose message identifier is greater than the breakpoint identifier from the fixed-length hot buffer queue as a compensation message set, and adding them to the fixed-length hot buffer queue. When the hot buffer queue is not hit, the process is downgraded to the persistent database. Within a preset time window, messages with message identifiers greater than the breakpoint identifier are queried as the compensation message set. During the same process of obtaining the compensation message set, the role runtime state and global context time of the session are queried in parallel. The breakpoint identifier is compared with the message identifier of the oldest message in the fixed-length hot buffer queue to obtain an integrity flag characterizing whether the compensation message set may be incomplete. The compensation message set, the role runtime state, the global context time, and the integrity flag are assembled into a single compensation event and sent to the client at once through the connection, so that the client can atomically update its local message list, role runtime state, and global context time.
[0008] In one implementation, the character's runtime state includes a current scene identifier, derived attributes, temporary effects, and equipment and skill growth markers; the global context time and the personal story time in the character's runtime state are obtained at the same logical moment as the breakpoint identifier, using the breakpoint identifier as a common sampling coordinate. In one implementation, the message identifier is constructed by concatenating the high-order bits of the timestamp, the middle-order bits of the work node identifier, and the low-order bits of the sequence number within the work node; the comparison of the message identifier being greater than the breakpoint identifier is achieved by comparing the message identifier with a large integer. In one implementation, a query to the persistent database is triggered only when the compensation message set read from the fixed-length hot buffer queue is empty, and the query to the persistent database is limited to the range of the preset time window from the current moment rather than limited to a fixed number of entries. In one implementation, the acquisition of the integrity flag is independent of whether the query to the persistent database returns a result.
[0009] This invention also provides a multi-dimensional state atomic recovery system with a ring buffer and database fallback, comprising: a message writing module, used to assign a globally unique and monotonically increasing message identifier to each message generated in each session of a long-connection real-time interactive system, and write the messages to a persistent database first, and then to a fixed-length hot buffer queue of the session in the order of generation, wherein the fixed-length hot buffer queue is truncated to retain only the most recent preset number of messages; and a reconnection processing module, used to receive a reconnection request carrying a breakpoint identifier sent by the client after re-establishing the connection, read messages with message identifiers greater than the breakpoint identifier from the fixed-length hot buffer queue as a compensation message set, and in the fixed-length hot buffer queue, to... Upon a hit, the system downgrades to the persistent database and retrieves the compensation message set within a preset time window. A status query module is used to query the role runtime state and global context time of the session in parallel during the same process the reconnection processing module retrieves the compensation message set. An integrity determination module compares the breakpoint identifier with the message identifier of the oldest message in the fixed-length hot buffer queue to obtain an integrity flag indicating whether the compensation message set may be incomplete. A packaging and delivery module assembles the compensation message set, the role runtime state, the global context time, and the integrity flag into a single compensation event and delivers it to the client at once via the connection.
[0010] The beneficial effects of this invention are as follows.
[0011] Firstly, this invention eliminates the problem of clients entering inconsistent intermediate states after reconnection. The mechanism lies in the fact that the role's runtime state and global context time are essentially the result of folding and reducing the message event sequence line by line. They, along with the message stream, are projections of the same causal event sequence at different granularities. This invention acquires the role's runtime state, global context time, and compensation message set at the same logical moment using the breakpoint identifier as a common sampling coordinate, and assembles them into a single compensation event for atomic updating by the client in one go. This ensures that the updated multidimensional data strictly corresponds to the same logical moment in the event sequence. Compared to only choosing between resending messages and issuing the full state, and acquiring messages and states separately, this invention avoids the problem of two independent requests and responses falling at different points in a continuously advancing event sequence, resulting in inconsistency as long as an event is written in between. Therefore, the client will not update to an intermediate state that has never actually existed on the server's timeline.
[0012] Secondly, this invention achieves both low latency for the hot path and no data loss for the cold path, despite the constraint that a single storage medium cannot simultaneously guarantee low latency and no data loss. The mechanism lies in the fixed-length hot buffer queue, which maintains a fixed length by inserting and truncating the head. Both writing and reading have constant time complexity, and most reconnections caused by brief network fluctuations can be completed in the first-level read, with a response time in the millisecond range. Only when the first-level read returns an empty set is the process degraded to the persistent database and queried within a preset time window from the current moment, thus ensuring no data loss during prolonged disconnections. Furthermore, the write order—first writing to the persistent database and then to the fixed-length hot buffer queue—ensures that even if the fixed-length hot buffer queue write fails, the persistent database still retains complete records for degraded query overwriting. Compared to solutions relying solely on a single storage medium that cannot provide compensation data when the cache expires or the database query times out, this invention uses a two-level progressive compensation system to ensure that the hot and cold paths each fulfill their respective functions.
[0013] Third, this invention enables the client to deterministically perceive whether the recovered data is complete. The mechanism lies in the fact that the acquisition of the integrity flag is independent of whether the query in the persistent database returns results. It uses whether the breakpoint identifier is less than the message identifier of the oldest message in the fixed-length hot buffer queue as a criterion, thereby capturing the silent incompleteness caused by the blind zone between the length boundary of the fixed-length hot buffer queue and the time boundary of the preset time window. Within this blind zone, although the query returns several messages, it may still miss earlier messages that both overflow the fixed-length hot buffer queue and exceed the preset time window. Compared to existing solutions that cannot inform the client whether the compensation data is complete, this invention delivers this integrity flag as a first-class output dimension to the client.
[0014] Fourth, the aforementioned features exhibit a synergistic effect exceeding the sum of their individual effects. The mechanism lies in the fact that while multidimensional atomic packaging alone can guarantee consistency, it cannot determine completeness; and while integrity determination alone may not guarantee consistency between the flag and the recovered data, the data may still be inconsistent. This invention combines multidimensional atomic packaging, integrity determination, two-level progressive compensation, and a dual-write order of first storing and then caching. This allows the client to obtain a consistently accurate and incomplete local message list, role runtime state, and global context time each time a connection is re-established, without scanning the entire historical record. The consistency and integrity guarantees exceed the sum of the individual effects of each of the aforementioned mechanisms. Attached Figure Description
[0015] Figure 1 This is a flowchart of the multidimensional state atomic recovery method of the circular buffer and database fallback of the present invention; Figure 2 This is an architectural diagram of the multidimensional state atomic recovery system of the present invention. Detailed Implementation
[0016] To make the objectives, technical solutions, and advantages of the present invention clearer, the specific embodiments of the present invention will be further described in detail below with reference to the accompanying drawings. It should be understood that the specific embodiments described herein are only for explaining the present invention and are not intended to limit the present invention.
[0017] This embodiment uses an online tabletop role-playing platform as an application background to illustrate the multi-dimensional state atomic recovery method of the present invention with circular buffering and database fallback. In this platform, multiple players and a host interact in real time around the same session. Clients maintain a persistent connection with the server via a long-connection protocol, and the server continuously pushes messages to clients belonging to the same session. The types of messages include, but are not limited to, chat messages, dice results, system notifications, and time stamps. All types of messages are uniformly processed by the present invention. In addition to the message flow, the platform also maintains the evolving operational state as messages progress, including each virtual character's current scene identifier, derived attributes such as health and mana, temporary effects such as poison, equipment, skill growth markers, and the global context time shared by the entire session. The core of the present invention lies in modeling the recovery process when a client disconnects and reconnects as an atomic snapshot recovery of the multi-dimensional operational state at a single logical moment, rather than isolated message resending. See also Figure 1 The method in this embodiment includes steps S1 to S7, which will be described one by one below.
[0018] Step S1: Message writing and dual-write consistency guarantee. During the session, for each message generated by the server, a globally unique message identifier is assigned to that message, which monotonically increases over time. In a preferred implementation, the message identifier is a 64-bit integer, with its highest bit being the sign bit and always set to zero. The remaining 63 bits are formed by concatenating the high-order bits of the timestamp, the middle-order bits of the worker node identifier, and the low-order bits of the sequence number within the worker node. In this embodiment, the bit widths of these three bits are 41 bits, 10 bits, and 12 bits, respectively. The high-order bits of the timestamp carry the millisecond-level time information of the message generation time, the middle-order bits of the worker node identifier carry the number of the worker node that generated the message, and the low-order bits of the sequence number carry the increasing sequence of the worker node within the same millisecond. Therefore, the message identifier naturally maintains temporal order, and in a distributed deployment with multiple message writing sources, even if different worker nodes generate messages within the same millisecond, the message identifier remains globally unique and does not conflict because their worker node identifiers are different. After assigning the message identifier, the server performs a double-write operation in the order of generation: first, the message is written to the persistent database, i.e., a record is inserted into the message table. This record includes the message identifier, the session identifier, the scene identifier, the message content, the generation time, and the visibility range fields, etc.; then, the message is written to the fixed-length hot buffer queue of the session. In a preferred implementation, the fixed-length hot buffer queue is a list type in a key-value storage system, and its key name is associated with the session identifier, for example, named in the form of session identifier concatenated with a fixed suffix; during writing, the serialized message is inserted into the head of the list, and then the length of the list is truncated to a preset number of entries. In this embodiment, the preset number of entries is 200, so that the fixed-length hot buffer queue always retains only the most recent 200 messages, and the latest message is located at index 0 of the list. The fixed-length hot buffer queue has several advantageous characteristics: its length is fixed, each message occupies approximately 1KB, and the memory usage of a single session is approximately 200KB, which is controllable and does not grow indefinitely over time; it maintains the insertion order, with the latest message always at the head; both head insertion and length truncation have constant time complexity, without blocking the main processing thread; and all types of messages are uniformly entered into the fixed-length hot buffer queue, without discrimination based on message type. It is particularly important to emphasize the write order of first writing to the persistent database and then to the fixed-length hot buffer queue: since the persistent database is responsible for data persistence, while the fixed-length hot buffer queue only serves to accelerate queries, the database-first, cache-later order ensures that even if the fixed-length hot buffer queue write fails, the persistent database still contains complete records, and subsequent fallback queries can overwrite them, thus preventing data loss due to the failure of the fixed-length hot buffer queue.Furthermore, using the message identifier, rather than a simple auto-incrementing integer or database auto-incrementing identifier, as the globally unique incrementing identifier for messages has significant advantages in high-frequency message scenarios and distributed deployment scenarios: simple auto-incrementing integers or database auto-incrementing identifiers may cause identifier conflicts or out-of-order identifiers when multiple message writing sources coexist, due to each writing source maintaining its own count, thereby disrupting the temporal relationship between messages; while the message identifier is composed of the high-order bits of the timestamp, the middle-order bits of the worker node identifier, and the low-order bits of the sequence number within the worker node. Its high-order bits of the timestamp ensure temporal ordering across writing sources, its middle-order bits of the worker node identifier ensure non-conflict between different writing sources, and its low-order bits of the sequence number ensure unique differentiation of the same worker node within the same millisecond. The combination of these three factors makes the message identifier globally unique at any writing source and at any time, and monotonically incrementing with time, thus laying the foundation for using it as a common sampling coordinate and integrity judgment benchmark. Simultaneously with the writing of the message, targeted push is also performed based on its visibility range field; this part is detailed in the following explanation of targeted visibility. Furthermore, the preset number of entries in the fixed-length hot buffer queue mentioned in this step is 200, which is a trade-off between memory usage and hot path hit rate: if the preset number of entries is too small, even short disconnections will frequently miss the hot path and be downgraded to the persistent database, increasing the trigger frequency of cold paths; if the preset number of entries is too large, the memory usage of a single session will increase accordingly, putting pressure on the memory of the storage system when there are many sessions; when the number of entries is 200, the memory usage of a single session is about 200KB, which is sufficient to cover most of the messages missed by disconnections caused by brief network fluctuations, so that such disconnections can be hit in the hot path.
[0019] Step S2: Client Disconnection Detection and Rejoining the Session with Breakpoint Identifier. When a long connection between the client and server is broken due to network fluctuations, the client's built-in automatic reconnection mechanism is triggered. In a preferred implementation, this mechanism is configured with a maximum of 10 reconnections and a reconnection interval of 1000ms. Before disconnection, the client continuously records the message identifier of the last message it received and saves this message identifier as a breakpoint identifier. When the connection is successfully re-established, the client sends a reconnection request to the server. This reconnection request carries a session identifier, an optional role identifier, and the breakpoint identifier, where the breakpoint identifier is the message identifier of the last message received by the client before disconnection. Upon receiving the reconnection request, the server first verifies the initiator's user identity and their access permissions to the session. If the verification is successful and the breakpoint identifier is not empty, the server determines that the client is reconnecting after a disconnection rather than joining for the first time, thus entering the reconnection processing flow of this invention. If the breakpoint identifier is empty, it indicates that the client is joining the session for the first time, and the process is handled according to the normal initial loading process, without triggering the compensation and recovery process of this invention. Through this step, the server obtains a clear recovery boundary, namely the breakpoint identifier. Subsequent acquisition of compensation message sets and integrity determination are based on this recovery boundary.
[0020] Step S3, the first-level query, involves reading the compensation message set from the fixed-length hot buffer queue. The server constructs the key name of the fixed-length hot buffer queue using the session identifier and reads all elements in the queue, i.e., retrieving all serialized messages from index 0 to the end of the list. Subsequently, the server deserializes each retrieved message, extracts its message identifier, and compares each message with the breakpoint identifier, filtering out all messages whose message identifier is greater than the breakpoint identifier. These messages are then sorted in ascending order by their message identifiers as candidate compensation message sets. The comparison of message identifiers being greater than the breakpoint identifiers is achieved by comparing the message identifiers with large integers: since the message identifiers are 64-bit integers of equal length, lexicographically comparing their string form is equivalent to comparing their numerical values, provided the lengths are consistent. Alternatively, they can be directly compared using large integer values. Because the message identifier is formed by concatenating the high-order bits of the timestamp, the middle-order bits of the worker node identifier, and the low-order bits of the sequence number within the worker node, it is naturally ordered by time. Therefore, this comparison can correctly filter out all messages generated during the disconnection period that are still retained in the fixed-length hot buffer queue. If the candidate compensation message set obtained in this step is not empty, it indicates that the disconnection time is short, the missed messages have not overflowed the fixed-length hot buffer queue, the hot path is hit, the server can directly use the candidate compensation message set as the compensation message set, and the response is in the millisecond level; at this time, no query to the persistent database is triggered.
[0021] Step S4, the second-level query, i.e., the persistent database as a fallback. This step uses a two-level progressive compensation trigger criterion: a query to the persistent database is triggered only when the candidate compensation message set read from the fixed-length hot buffer queue in step S3 is empty; an empty candidate compensation message set means that the disconnection time was long, and all messages generated during the disconnection period have overflowed the fixed-length hot buffer queue. The trigger criterion for the two-level progressive compensation and the acquisition of the compensation message set can be formally expressed as: , in: The compensation message set is an ordered set of messages, whose number of elements is an integer not less than 0, dimensionless, and is calculated by this formula. It represents all the messages that need to be sent to the client during the disconnection period. The fixed-length hot buffer queue is an ordered set of messages that retains only the most recent preset number of messages. It is dimensionless and its content is obtained by writing and truncating in step S1, representing the source of compensation for the hot path. The message record set corresponding to the session in the persistent database is a message set, which is dimensionless and its content is obtained by persistent writing in step S1, representing the source of compensation for the cold path; For traversal or A single message variable, dimensionless, that takes all elements of the corresponding set; To obtain information The operation of the message identifier is performed, and its value is a 64-bit integer, the value range of which is the representation range of the integer type, dimensionless, and assigned by step S1, representing the position of the message on monotonic coordinates; This is the breakpoint identifier, a 64-bit integer, with a value range of the same as... Dimensionless, obtained from the reconnection request in step S2, characterizing the recovery boundary; For the message The time of generation is a scalar, with the unit being h, and is recorded by step S1, representing the time when the message was generated; The current time is a scalar value, with the unit being h, obtained from the server system clock, and represents the time when the query occurred. The preset time window is a scalar with a value of 24h. It is determined by the distribution of disconnection scenarios in the session and represents the time span traced back by the cold path query. If its value is too small, it may truncate messages that still need to be supplemented in long disconnection scenarios. If it is too large, it will increase the query range and the performance risk of full table scan. Taking 24h can cover most disconnection scenarios caused by network failure, device shutdown, etc. This is the logical AND operator, indicating that both conditions must be true simultaneously. The symbol for an empty set represents a set containing no elements. As can be seen from the above formula, the fallback query of this invention is limited to the preset time window from the current time, rather than a fixed number of records. The reason for using a time window instead of a fixed number of records is that a fixed number of records would truncate a large number of messages during long disconnections, leading to incomplete recovery. A time window, on the other hand, ensures that the query range is controllable, avoids a full table scan of the message records in the persistent database, and covers most disconnection scenarios. In a preferred implementation, the query in the persistent database also deserializes several fields of the query results, that is, restoring the visibility range field, the plot time field corresponding to the message, and the metadata field from their stored string form to object form, and then using the query results as the compensation message set.
[0022] Step S5, Parallel Multi-Dimensional State Query. During the same processing step S3 or S4 where the compensation message set is obtained, the present invention queries the character runtime state and global context time of the session in parallel. Specifically, the server executes two queries in parallel: the first query uses the session identifier as a condition to read the global context time from the session table, i.e., the global context time currently shared by the session, representing the current time of the storyline; the second query uses the character identifier and the session identifier as conditions to read the character runtime state from the character scene state table. The character runtime state includes the current scene identifier, derived attributes, temporary effects, equipment and skill growth markers. The current scene identifier represents the scene the character is currently in. Derived attributes include the current value, maximum value, and possible temporary increases or decreases of attributes such as health and mana. Temporary effects are recorded in the form of names and values, such as a continuous effect like poison reducing a certain amount of health per turn. Equipment is recorded in a list format, showing the equipment currently held by the character. Skill growth markers are recorded in key-value format, showing the growth status of several skills. The global context time and the personal story time in the character's runtime state are obtained at the same logical moment as the compensation message set, using the breakpoint identifier as a common sampling coordinate. This common sampling coordinate is one of the key aspects of this invention: since the character's runtime state and the global context time are the result of message event sequence folding and reduction, binding their query and the acquisition of the compensation message set to the same recovery boundary, i.e., under the breakpoint identifier, and completing them together in the same processing process, ensures that the queried multi-dimensional state strictly corresponds to the same logical moment in the event sequence as the compensation message set, thus providing a time-consistent data foundation for subsequent atomic packaging. Unifying multiple sub-dimensions of the character's runtime state, such as the current scene identifier, derived attributes, temporary effects, equipment, and skill growth markers, into atomic units that are of the same origin as the messages and must be sampled together, rather than treating them as independent contexts that can be retrieved separately as needed, is the fundamental difference between this invention and existing solutions that obtain states and messages separately.
[0023] Step S6, Integrity Determination and Integrity Flag Calculation. The acquisition of the integrity flag in this invention is independent of whether the query in the persistent database returns results. That is, regardless of whether the query in step S4 returns several messages, the integrity flag is calculated independently according to the same criterion. The integrity determination is as follows: the breakpoint identifier is compared with the message identifier of the oldest message in the fixed-length hot buffer queue. If the compensation message set is empty, or the breakpoint identifier is smaller than the message identifier of the oldest message in the fixed-length hot buffer queue, the integrity flag is set to indicate possible incompleteness; otherwise, it is set to indicate completeness. The calculation of the integrity flag can be formally expressed as: , in: The integrity flag is a Boolean value that takes the value of either true or false. It is dimensionless and is calculated by this formula. It represents whether the compensation message set may be incomplete. A value of true indicates that it may be incomplete and a value of false indicates that it is complete. The compensation message set is defined in the same way as the formula in step S4 above; The symbol for the empty set. This indicates that the compensation message set is empty, meaning there are no messages available for reference. This is the breakpoint identifier, defined in the same formula as in step S4 above; This is a fixed-length hot buffer queue, defined as in the formula in step S4 above; To obtain information The operation of the message identifier is defined in the same way as the formula in step S4 above; For the fixed-length hot buffer queue The operation of taking the minimum value of the message identifier of all messages in the queue results in a 64-bit integer, dimensionless, which is the message identifier of the oldest message in the fixed-length hot buffer queue, representing the earliest historical horizon that the hot path can trace back to. For traversal The variable is a single message variable, dimensionless; and are Boolean constants, representing possible incompleteness and completeness, respectively. The essence of the completeness determination is: when the breakpoint identifier is less than the message identifier of the oldest message in the fixed-length hot buffer queue, it indicates that some messages generated during the disconnection period have overflowed the fixed-length hot buffer queue. Even if the persistent database query returns results, there may still be earlier messages beyond the preset time window that have not been covered, thus the recovery result may be incomplete. Conversely, when the breakpoint identifier is not less than the message identifier of the oldest message in the fixed-length hot buffer queue, it indicates that the fixed-length hot buffer queue completely covers the disconnection interval, and the recovery result is complete. Therefore, this invention can capture situations where queries return several messages but silently omit earlier messages in the blind zone between the length boundary of the fixed-length hot buffer queue and the time boundary of the preset time window, making incompleteness known.
[0024] In a further preferred embodiment, to achieve a better balance between reducing the frequency of queries to the persistent database and reducing integrity misjudgments, the determination of the degradation trigger condition and the integrity flag is refined as follows: First, regarding degradation triggering: instead of using whether the candidate compensation message set read from the fixed-length hot buffer queue is empty as the sole trigger condition, a query to the persistent database is triggered when the fixed-length hot buffer queue fails to find a match or is insufficient to prove its integrity. Here, insufficient to prove integrity means that the breakpoint identifier is smaller than the message identifier of the oldest message in the fixed-length hot buffer queue, i.e., although the fixed-length hot buffer queue returns several messages, it cannot cover the entire interval from the breakpoint identifier. Secondly, regarding the correction of the integrity flag: After triggering the query of the persistent database, the integrity flag is further corrected using the existence status of the message corresponding to the breakpoint identifier in the persistent database as an anchor point. That is, the message record whose message identifier is equal to the breakpoint identifier is retrieved in the persistent database, and it is determined whether the time of generation of the record is still within the preset time window from the current time: when the breakpoint message record is still within the preset time window, it indicates that all messages from the breakpoint identifier have fallen into the query range of the persistent database and are compensated for as complete, and the integrity flag is set to indicate completeness; when the breakpoint message record has exceeded the preset time window or no longer exists in the persistent database, it indicates that there may be earlier messages that have both overflowed the fixed-length hot buffer queue and exceeded the preset time window and have not been covered, and the integrity flag is set to indicate possible incompleteness. Compared to determining integrity solely based on comparing the breakpoint identifier with the message identifier of the oldest message in the fixed-length hot buffer queue, this preferred embodiment utilizes the existence of the breakpoint message within the preset time window as an anchor point. This allows for the differentiation between clients that have been caught up after a disconnection and have no missing messages, and clients that have been disconnected for too long and have earlier message overflows. This maintains the ability to determine the integrity of the restored message set while avoiding misjudging a situation where the compensation message set is empty but has actually been caught up as potentially incomplete, further reducing the false alarm rate of the integrity flag. It should be noted that the modification of the integrity flag in this preferred embodiment is a further optimization based on the aforementioned basic determination: the former can provide a conservative integrity determination without querying the persistent database, while the latter provides a more accurate integrity determination even after a persistent database query has been triggered. Both use the breakpoint identifier as a unified benchmark and can be used individually or in combination, without departing from the core idea of this invention to make a deterministic determination of the restored integrity using the breakpoint identifier as a common sampling coordinate.
[0025] Step S7: Multidimensional State Atomic Packaging and Distribution. The server assembles the compensation message set obtained in step S3 or S4, the role runtime state and global context time obtained through parallel query in step S5, and the integrity flag calculated in step S6 into a single compensation event, and distributes it to the client all at once through the connection. In a preferred implementation, the compensation event includes four fields, respectively carrying the compensation message set, the role runtime state, the global context time, and the integrity flag, and all four are distributed together as equal dimensions in the same event. After receiving the compensation event, the client performs a local atomic update: merging the messages in the compensation message set into the local message list in ascending order of the message identifier, and skipping existing messages locally to avoid duplication; replacing the local role runtime state cache with the role runtime state; replacing the local global context time display with the global context time; and when the integrity flag representation may be incomplete, driving the client to trigger a consistency check or displaying a prompt to the user, such as that some historical messages may be lost, suggesting refreshing the complete state. Since all four dimensions are sent out together in the same compensation event and are atomically completed by the client with a single local update, the client will not encounter an intermediate state where one dimension has been updated while another dimension has not been updated after receiving the compensation event. This avoids race conditions between multiple requests and responses and also prevents the client from entering an intermediate state that has never actually existed on the server timeline.
[0026] The following provides further explanation of several supplementary implementations of the method embodiments of the present invention.
[0027] Regarding targeted visibility: Each message generated in the session carries a visibility scope field to characterize the visibility scope of the message. For messages whose visibility scope field indicates they are visible to all, they are broadcast through the session's broadcast channel, i.e., broadcast to all clients belonging to the session. For messages whose visibility scope field indicates limited visibility, such as certain messages only visible to specific users, the target user's current connection is found through the user identifier-to-connection mapping, and the message is directed to that connection. During the reconnection compensation phase, after receiving the compensation message set, the server further filters the compensation message set according to the visibility scope field, retaining only messages visible to the currently reconnected user, thus preventing the client from receiving messages it should not be able to see. Therefore, this invention maintains consistent visibility control with the real-time push phase during the recovery phase, avoiding the problem in existing solutions where visibility control is ignored during the compensation phase, resulting in the delivery of unseen messages to the client.
[0028] Regarding connection lifecycle management: When a client joins the session, the server adds the user identifier corresponding to the client to the online set of the session and records the mapping from the user identifier to the connection for subsequent targeted push notifications to that user. When a client disconnects, the server removes the user identifier from the online set, deletes the mapping from the user identifier to the connection, and clears all event listeners for the corresponding connection to prevent resource leaks caused by the accumulation of event listeners. In a preferred implementation, the aforementioned online set and mapping are stored in a key-value storage system, sharing the same storage system instance as the fixed-length hot buffer queue.
[0029] Regarding alternative implementations: Several alternative implementations are possible without departing from the core idea of this invention. First, instead of using the message identifier as the recovery boundary, a timestamp can be used as the cursor. That is, the client records the timestamp of the last message it received before disconnecting, and the server queries messages after that timestamp upon reconnection. This alternative implementation is simpler, but has lower accuracy; multiple messages may exist within the same millisecond, and the timestamp is not globally unique. Second, an incrementing version number can be assigned to each state change, and the client records the latest version number. If the version numbers are not consecutive upon reconnection, a complete state snapshot is requested. This alternative implementation is suitable for scenarios with fewer state dimensions and lower change frequency, but in high-frequency message scenarios, the version number grows too quickly, and the snapshot transmission overhead is large. Third, a least recently used cache within a process can be added between the fixed-length hot buffer queue and the persistent database to further reduce the query latency of hot paths. This alternative implementation is suitable for ultra-high concurrency scenarios, but increases the complexity of data consistency maintenance. Fourth, an operation log mechanism can be adopted, recording each state change as an operation log entry. During reconnection, the operation logs are replayed in sequence to achieve accurate state recovery. This alternative implementation provides strong accuracy guarantees, but its implementation complexity and storage overhead are high. The above alternative implementations can all solve the problems to be solved by this invention to a basic extent, and are equivalent transformations of this invention.
[0030] The following describes an embodiment of the multidimensional state atomic recovery system of the present invention. See also Figure 2 The system of the present invention includes a client layer, a communication gateway, and a server-side processing layer. The server-side processing layer includes a message writing module, a reconnection processing module, a status query module, an integrity determination module, and a packaging and distribution module, and is connected to a storage layer including the persistent database and the fixed-length hot buffer queue. Each module of the system corresponds one-to-one with each step of the aforementioned method embodiment, and will be described in detail below.
[0031] The message writing module is used to assign a globally unique message identifier to each message generated in each session of the long-connection real-time interactive system, and to write the messages to a persistent database first, and then to a fixed-length hot buffer queue of the session, in the order of their generation. The fixed-length hot buffer queue is truncated to retain only the most recent preset number of messages. The specific processing of the message writing module is the same as step S1 in the aforementioned method embodiment, including the allocation of the message identifier, the double-write order of first writing to the database and then to the cache, and the insertion of the head and truncation of the fixed-length hot buffer queue, which will not be repeated here. The message writing module ensures the data consistency between the persistent database and the fixed-length hot buffer queue, providing a consistent data source for subsequent two-level progressive compensation.
[0032] The reconnection processing module receives a reconnection request carrying a breakpoint identifier sent by the client after re-establishing the connection. It reads messages with message identifiers greater than the breakpoint identifier from the fixed-length hot buffer queue as a compensation message set. If the message is not found in the fixed-length hot buffer queue, it downgrades to the persistent database and queries the compensation message set within a preset time window. The specific processing of the reconnection processing module is the same as steps S2, S3, and S4 in the aforementioned method embodiment, including identity and permission verification, first-level reading and filtering in the fixed-length hot buffer queue, and a second-level persistent database time window query triggered only when the candidate compensation message set is empty. The reconnection processing module is the main execution body of the two-level progressive compensation of this invention, responsible for coordinating the fixed-length hot buffer queue query and the persistent database fallback query.
[0033] The status query module is used to query the role runtime state and global context time of the session in parallel during the same processing process in which the reconnection processing module obtains the compensation message set. The specific processing of the status query module is the same as step S5 in the aforementioned method embodiment; it uses the breakpoint identifier as a common sampling coordinate and obtains the role runtime state and global context time at the same logical moment as the compensation message set, thereby providing time-consistent multidimensional data for atomic packaging. The status query module and the reconnection processing module work in parallel, and their outputs are merged in the same processing process.
[0034] The integrity determination module compares the breakpoint identifier with the message identifier of the oldest message in the fixed-length hot buffer queue to obtain an integrity flag characterizing whether the compensation message set may be incomplete. The specific processing of the integrity determination module is the same as step S6 in the aforementioned method embodiment; it determines the integrity independently of whether the query in the persistent database returns a result, capturing silent incompleteness caused by the blind zone between the length boundary of the fixed-length hot buffer queue and the time boundary of the preset time window. The output of the integrity determination module, namely the integrity flag, is used as a first-order output dimension in subsequent atomic packaging.
[0035] The packaged delivery module is used to assemble the compensation message set, the role runtime state, the global context time, and the integrity flag into a single compensation event, and then deliver it to the client all at once through the connection. The specific processing of the packaged delivery module is the same as step S7 in the aforementioned method embodiment. It gathers the compensation message set from the reconnection processing module, the role runtime state and global context time from the status query module, and the integrity flag from the integrity determination module, assembles them into a single compensation event, and then delivers it all at once, allowing the client to atomically update its local message list, role runtime state, and global context time.
[0036] As can be seen from the above system embodiments, the modules of this invention form a deeply coupled closed-loop collaboration: the dual-write output of the message writing module provides a consistent data source for the two-level progressive compensation of the reconnection processing module; the processing of the compensation message set by the reconnection processing module triggers and constrains the parallel query of the status query module to be performed at the same logical moment; the integrity determination module calculates the integrity flag using the breakpoint identifier and the oldest identifier of the fixed-length hot buffer queue as inputs; the packaging and distribution module collects the outputs of the aforementioned modules and atomically packages them, and the integrity flag it distributes in turn drives the client to trigger a consistency backcheck when there may be incompleteness. The above module collaboration enables the system to achieve atomic snapshot recovery of multi-dimensional operating states at a single logical moment and the determination of the integrity of the recovery. More specifically, the aforementioned coupling loop is not a simple chain of functions of each module, but a deep collaboration in which the output of the front-end module serves as the key input of the back-end module, and the results of the back-end module in turn constrain the processing boundary of the front-end module: if the message writing module does not adopt the double-write order of first database and then cache, the second-level fallback query of the reconnection processing module may fail due to the missing records in the persistent database; if the reconnection processing module does not use the breakpoint identifier as a unified benchmark, the parallel query of the status query module will not be able to align with the compensation message set to the same logical moment; if the integrity determination module deviates from the oldest identifier of the fixed-length hot buffer queue, it will be unable to make a determination at the boundary blind zone between the hot path and the cold path; the integrity flag issued by the packaging and distribution module will, through the consistency back lookup of the client, have a reverse effect on the value of the breakpoint identifier during the next reconnection. It is this closed-loop structure, where outputs are inputs to each other and results inversely constrain parameters, that ensures the consistency and integrity of this invention, exceeding the sum of the individual actions of each module. This is fundamentally different from existing solutions that only select one between resending messages and issuing full status, and where each process is isolated from the others.
[0037] It should be noted that the modules in the system embodiments of the present invention can be deployed on the same physical server or distributed across multiple physical servers; the persistent database and the fixed-length hot buffer queue can be implemented using any storage system known in the art that meets the requirements of persistence and low latency; the connection can be implemented using any communication protocol known in the art that meets the requirements of persistent long connections. All variations in the above deployment methods and implementation choices do not depart from the core idea of the present invention.
[0038] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included within the protection scope of the present invention.
Claims
1. A multidimensional state atomic recovery method with circular buffer and database fallback, characterized in that, include: Assign a globally unique message identifier to each message generated in each session of the long-connection real-time interactive system, and write the message to a persistent database first, and then to a fixed-length hot buffer queue of the session in the order of generation. The fixed-length hot buffer queue is truncated during writing to retain only the most recent preset number of messages. The client receives a reconnection request carrying a breakpoint identifier after re-establishing the connection. The breakpoint identifier is the message identifier of the last message received by the client before the disconnection. Messages whose message identifier is greater than the breakpoint identifier are read from the fixed-length hot buffer queue as compensation message set, and when the message is not hit in the fixed-length hot buffer queue, it is downgraded to the persistent database. Messages whose message identifier is greater than the breakpoint identifier are queried within a preset time window as compensation message set. During the same process of acquiring the compensation message set, the role runtime state and global context time of the session are queried in parallel. The breakpoint identifier is compared with the message identifier of the oldest message in the fixed-length hot buffer queue to obtain an integrity flag characterizing whether the compensation message set may be incomplete. The compensation message set, the role runtime state, the global context time, and the integrity flag are assembled into a single compensation event, which is then sent to the client all at once via the connection, allowing the client to atomically update its local message list, role runtime state, and global context time.
2. The multidimensional state atomic recovery method with circular buffer and database fallback as described in claim 1, characterized in that, The character's runtime state includes the current scene identifier, derived attributes, temporary effects, and equipment and skill growth markers; the global context time and the personal story time in the character's runtime state are obtained at the same logical moment as the breakpoint identifier, using the breakpoint identifier as the common sampling coordinate. After receiving the compensation event, the client atomically replaces the local role runtime state and global context time with a single local update, and merges the messages in the compensation message set into the local message list in ascending order of the message identifier, skipping messages that already exist locally.
3. The multidimensional state atomic recovery method with circular buffer and database fallback as described in claim 2, characterized in that, The message identifier is composed of the high-order bits of the timestamp, the middle-order bits of the work node identifier, and the low-order bits of the sequence number within the work node. It is globally unique and monotonically increases with time. The comparison between the message identifier and the breakpoint identifier is achieved by comparing the message identifier with a large integer. When there are multiple message writing sources, each writing source is distinguished by a different work node identifier. The message identifiers generated by each writing source still constitute a unified monotonic coordinate to provide a consistent definition for the common sampling coordinate.
4. The multidimensional state atomic recovery method with circular buffer and database fallback as described in claim 3, characterized in that, When the fixed-length hot buffer queue is not hit or is insufficient to prove its integrity, a downgrade to a query in the persistent database is triggered. The missing message or insufficient to prove its integrity includes the compensation message set being empty or the breakpoint identifier being less than the message identifier of the oldest message in the fixed-length hot buffer queue. The query in the persistent database is limited to a preset time window from the current time, rather than a fixed number of messages. The preset time window is 24 hours.
5. The multidimensional state atomic recovery method with circular buffer and database fallback as described in claim 4, characterized in that, The integrity flag is obtained independently of whether the query of the persistent database returns a result. It is determined as follows: when the compensation message set is empty or the breakpoint identifier is less than the message identifier of the oldest message in the fixed-length hot buffer queue, the integrity flag is set to indicate that it may be incomplete. Otherwise, the integrity flag is set to complete. The integrity flag is included in the compensation event as a dimension equivalent to the compensation message set, the role runtime state, and the global context time. When the integrity flag may be incomplete, the client is driven to trigger a consistency check or display a prompt.
6. The multidimensional state atomic recovery method with circular buffer and database fallback as described in claim 5, characterized in that, The writing order of first writing to the persistent database and then to the fixed-length hot buffer queue ensures that the persistent database retains complete records for fallback query coverage even if the writing to the fixed-length hot buffer queue fails. The combination of the writing order, the fixed-length hot buffer queue, and the integrity flag enables the client to obtain a consistent and incomplete local message list, role runtime status, and global context time each time a connection is re-established, without scanning the entire historical record.
7. The multidimensional state atomic recovery method with circular buffer and database fallback as described in claim 1, characterized in that, The fixed-length hot buffer queue is a list type in a key-value storage system, and its key name is associated with the identifier of the session; the truncation during writing includes inserting the new message at the head of the list and truncating the length of the list to 200 entries, so that the latest message is located at index 0 of the list; the reading in the fixed-length hot buffer queue includes reading all elements of the list and then filtering by the message identifier.
8. The multidimensional state atomic recovery method with circular buffer and database fallback as described in claim 1, characterized in that, Each message generated in the session carries a visibility range field; messages that are visible to all are sent through the broadcast channel of the session; messages that are visible to a limited number of users are sent through a mapping from user identifier to connection; when the compensation message set is obtained, the compensation message set is filtered according to the visibility range field so that the client does not receive messages that it should not be able to see.
9. The multidimensional state atomic recovery method with circular buffer and database fallback as described in claim 1, characterized in that, When a client joins the session, the user identifier corresponding to the client is added to the online set of the session, and the mapping from the user identifier to the connection is recorded; when a client disconnects, the user identifier is removed from the online set, the mapping from the user identifier to the connection is deleted, and the event listener of the corresponding connection is cleared.
10. A multidimensional state atomic recovery system with circular buffer and database fallback, used to implement the multidimensional state atomic recovery method with circular buffer and database fallback as described in any one of claims 1-9, characterized in that, include: The message writing module is used to assign a globally unique message identifier to each message generated in each session of the long-connection real-time interactive system, which is monotonically increasing over time. The message is written to the persistent database first and then to the fixed-length hot buffer queue of the session in the order of generation. The fixed-length hot buffer queue is truncated to retain only the most recent preset number of messages. The reconnection processing module is used to receive a reconnection request carrying a breakpoint identifier sent by the client after re-establishing the connection, read messages whose message identifier is greater than the breakpoint identifier from the fixed-length hot buffer queue as a compensation message set, and when the message identifier is not found in the fixed-length hot buffer queue, it is downgraded to the persistent database and the compensation message set is obtained within a preset time window. The breakpoint identifier is the message identifier of the last message received by the client before the disconnection. The status query module is used to query the role runtime status and global context time of the session in parallel during the same process in which the reconnection processing module obtains the compensation message set. The integrity determination module is used to compare the breakpoint identifier with the message identifier of the oldest message in the fixed-length hot buffer queue to obtain an integrity flag characterizing whether the compensation message set may be incomplete. The packaging and delivery module is used to assemble the compensation message set, the role runtime state, the global context time, and the integrity flag into a single compensation event, and deliver it to the client in one go through the connection.
Citation Information
Patent Citations
Disconnection reconnection method, device and system
CN106714334A
