A state machine persistence method and system

By using the etcd storage system and Kubernetes' List-watch mechanism in the state machine, the interruption problem when the state machine exits abnormally is solved, breakpoint recovery and event response optimization are achieved, and the stability and performance of the system are improved.

CN115562578BActive Publication Date: 2025-11-28DUXIAOMAN TECH (BEIJING) CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202211190771.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-09-28
Publication Date
2025-11-28
Estimated Expiration
2042-09-28

AI Technical Summary

Technical Problem

Existing technologies cannot effectively preserve intermediate states when a state machine exits abnormally, leading to interruptions in business logic and wasting machine resources and impacting performance during event responses.

Method used

A state machine persistence method is adopted, which saves the state machine description file through the etcd storage system, and utilizes Kubernetes' List-watch mechanism and Reconcile method to realize the breakpoint recovery and event response mode switching of the state machine, avoiding loop waiting and reducing resource waste.

Benefits of technology

It enables breakpoint recovery of the state machine in abnormal scenarios, avoids interruption of business logic, optimizes the event response mode, reduces waste of machine resources, and improves system performance.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115562578B_ABST
    Figure CN115562578B_ABST
Patent Text Reader

Abstract

The application discloses a state machine persistence method, comprising the following steps: step 1, defining a state machine according to a business scenario, and issuing the state machine; step 2, loading the state machine to execute an event of a starting state; step 3, perceiving an execution result of the event, loading a next state, and repeating the process of steps 1-3 until the last state is executed. The application guarantees that the state machine is resumed at a breakpoint and continues to execute under an abnormal scenario, and avoids interruption of business logic after the state machine exits due to various abnormalities.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of computer, in particular to a state machine persistence method and system. BACKGROUND

[0002] In the actual application scene of the state machine, one of the most core problems is the state persistence of the state machine, that is, before the state machine is completed, the intermediate state is expected to be retained, and the breakpoint recovery and continuous execution are supported, so as to avoid the business logic interruption after the state machine exits due to various exceptions. The common persistence methods include single-machine memory, Redis cache and database persistence. Among them, the memory is the most fragile, and the data is completely lost after the single-machine restart; although the Redis cache and the database can guarantee the data storage through cache persistence and hard disk persistence, they cannot guarantee the data safety in 100%, and additional data processing logic is required.

[0003] Another difficulty of the state machine is event response. Usually, a dead loop is required to wait for event triggering in the state. For some events that need a long time to feedback, the continuous loop waiting will waste machine resources, and the more state machines maintained in operation, the more the machine and state machine performance will be affected. SUMMARY

[0004] Therefore, the present application provides a state machine persistence method and system, which guarantees the breakpoint recovery and continuous execution of the state machine in the abnormal scene, and avoids the business logic interruption after the state machine exits due to various exceptions. The state machine can also support two event response modes of loop query and suspension waiting in the event response, so as to avoid the waste of machine resources and the performance decline in the loop waiting.

[0005] In order to achieve the purpose of the present application, the following technical solutions are adopted:

[0006] A state machine persistence method comprises the following steps:

[0007] Step 1: defining the state machine according to the business scene, and issuing the state machine;

[0008] Step 2: loading the state machine, and executing the event of the starting state;

[0009] Step 3: perceiving the event execution result, loading the next event, and repeating the steps 1-3 until the last state is executed.

[0010] The state machine persistence method, wherein the state machine is issued by transmitting the state machine description file in step 1.

[0011] The state machine persistence method, wherein after receiving the state machine description file, the file is stored in the etcd storage system.

[0012] The state machine persistence method, wherein step 2 comprises coordinated control of events, comprising:

[0013] (1) obtaining current stage information;

[0014] (2) determining whether the current stage is running complete;

[0015] (3) assuming that the event of the current stage is not executed complete, determining whether the following is a loop waiting, if it is a loop waiting, exiting the state machine and waiting for a predetermined time before rejoining the execution queue, and repeating the above operation until the event of the current stage is executed complete; assuming that it is not a loop waiting, exiting the state machine until the event state of the stage is changed by a downstream callback, and re-executing the coordinated control of the state;

[0016] (4) assuming that the event of the current stage is executed complete, if there is no following stage at this time, proving that the state machine has been executed complete, and exiting the state machine; if there is a following stage, executing the next stage of the state machine, executing the event of the new stage, and repeating steps (1)-(4) until all stages are running complete.

[0017] The state machine persistence method, wherein the judgment basis of step (2) comprises: actively calling a downstream execution unit to check the event state; or only querying a local state, and modifying the current state when a downstream execution unit callback.

[0018] A state machine persistence system, comprising a request unit, a control unit and an execution unit, characterized in that the state machine persistence system is used to execute the state machine persistence method as described above.

[0019] The state machine persistence system, wherein: the request unit is used to issue the state machine combined with a business attribute; the control unit is used to analyze the state machine, control the execution, issuance and result recovery of events, and control whether the state is transferred according to the event result; and the execution unit is used to execute the events in this scenario when part of the events need to call a downstream for execution, and provide passive query or active reporting of the event result.

[0020] A computer readable memory storing processor executable instructions, when the instructions are executed by a processor, causing the processor to execute the above method. BRIEF DESCRIPTION OF DRAWINGS

[0021] Figure 1 Fig. 1 is a structural schematic diagram of a state machine persistence system;

[0022] Figure 2 Fig. 4 is a schematic diagram of a state machine reconcile operation flow. DETAILED DESCRIPTION

[0023] The accompanying drawings are included to provide a further understanding of the application, and are incorporated in and constitute a part of this specification. The drawings illustrate embodiments of the application and, together with the description, serve to explain the principles of the application. Figures 1-2 The specific embodiments of the present application will now be described in detail with specific reference being made to the figures. The embodiments are meant to be illustrative only and not limiting of the application. It will be apparent, of course, that embodiments of the application other than those described herein, and shown in the accompanying drawings, are possible and within the scope of the application.

[0024] In this specification, the term "one embodiment" or "an embodiment" or "some embodiments" means that a particular feature, structure, or characteristic described in connection with the embodiment is included in at least one embodiment of the application. The appearances of the phrase "in one embodiment" or "an embodiment" or "some embodiments" in various places in the specification are not necessarily all referring to the same embodiment, nor are they necessarily referring to a single embodiment. Furthermore, the term "comprises" or "comprising" or "includes" or "including" when used in this specification, means "including, but not limited to," or "comprising, but not limited to," and should not be interpreted as being restrictive.

[0025] Finite State Machine (FSM) is a tool for modeling the behavior process of an object, which describes the complete life cycle of the object through a plurality of state sequences of the object and through state transition triggered by events between states.

[0026] As shown in FIG. 1, the state machine persistence system includes a request unit, a control unit and an execution unit. Figure 1

[0027] The request unit is a system or tool for users. The users issue a state machine combined with business attributes through the request unit.

[0028] The control unit is a state machine controller based on Kubernetes, which is used for parsing the state machine, controlling the execution, issuance and result collection of events, and controlling whether the state is transferred according to the event result.

[0029] The execution unit is responsible for the actual execution of events in this scenario, and provides the function of passive query or active reporting of event results.

[0030] In this embodiment, the running flow of the state machine persistence system includes:

[0031] (1) The request unit defines the state machine according to the business scenario, and the users complete the issuance of the state machine through the request unit: the request unit sends a complete state machine description file (operator.yaml) readable by k8s to the control unit.

[0032] ​(2) After the control unit receives the user's request, according to the running mechanism of K8S, the operator.yaml file is stored in the etcd storage system.

[0033] (3) The state machine Operator (controller) is a kind of controller (control unit) based on k8s, which observes the addition of state machine instances through the k8s List-watch mechanism, and loads the state machine to execute the event of the initial state.

[0034] List-watch mechanism: List-watch is an asynchronous message passing method in the k8s system, which is a typical publish-subscribe mode. The k8s component perceives the change event of the subscribed data through the mechanism, and decides whether to process after perceiving the data change. In the state machine, the control unit can realize event perception and response through the List-watch mechanism.

[0035] (4) The event of each stage can be internal code logic or call the downstream execution unit for event execution.

[0036] (5) After the execution unit receives the event, it executes according to the requirements. At this time, the execution unit needs to provide the event execution state to determine whether the event is executed. The control unit can be queried cyclically, or the control unit can be called back after the execution unit is executed, reducing invalid communication between different units.

[0037] (6) After the control unit perceives the execution result of the event, it needs to judge according to the state transition condition which stage should be transferred to, so as to load the next stage, repeat the process of 4-6, until the last state (i.e. the final state of the last event in the state machine) is executed. At this time, the state machine is completely executed successfully.

[0038] To realize the persistence of the state machine, the application proposes a reconcile method based on the state machine operator: reconcile is the core of k8s-operator, and when List-watch monitors the event, reconcile will be called for "coordination" work. The reconcile includes return and requeue, and the return corresponds to the pause mode in the event response mode, and the event will not be executed again unless the event execution result callback of the execution unit is perceived; the requeue corresponds to the loop query mode in the event response mode, and the event can be reconciled again after a specified time, that is, it is added to the state machine execution queue for event execution. The state machine control based on the operator is essentially requeue, and the next stage is executed after a stage is completed, and the state machine is completed until all stages are run.

[0039] As shown in Figure 2 , the reconcile running flow of the state machine operator includes:

[0040] (1) obtaining the current stage information;

[0041] (2) judging whether the current stage is run completed, and the judgment basis has two kinds, one is to actively call the downstream execution unit to check the event state; the other is to modify the state when the downstream execution unit callback.

[0042] (3) assuming that the event of the current stage is not executed completed, it is needed to judge whether the subsequent is circularly waited, if it is circularly waited, that is, actively querying the downstream state, the requeue of the operator is called to exit, at this time, the k8s will add the state machine to the execution queue again, and wait for several seconds to run reconcile again, and it is circularly waited until the event of the stage is executed completed; assuming that it is not circularly waited, that is, the task is issued, and the downstream callback is needed to be waited, therefore, it is not circularly waited, and the computer resource is wasted, at this time, the return exits, and the state machine exits, unless the event state of the stage is changed by the downstream callback, the reconcile is executed again.

[0043] (4) assuming that the event of the current stage is executed completed, it is needed to jump to the next stage state, if there is no subsequent stage at this time, it is proved that the state machine is executed completed, and the state machine can be exited. If there is a subsequent stage, the state machine needs to execute the next stage, and the state migration is completed. The event of the new stage is executed, and the requeue is exited after the execution is ended, and 1-4 is repeated until all stages are run completed.

[0044] According to one embodiment of the present disclosure, a computer readable storage medium is provided. A person skilled in the art can understand that all or part of the steps of various methods in the above embodiments can be completed by instructions, or by related hardware controlled by the instructions, which can be stored in a computer readable storage medium and loaded and executed by a processor. For this purpose, the embodiments of the present application provide a storage medium having a plurality of instructions stored therein, which can be loaded by a processor to execute the steps of any one of the state machine persistence methods provided by the embodiments of the present application. The beneficial effects that can be achieved by the embodiments of the present application are described in detail in the foregoing embodiments, and will not be described here. The specific implementation of each operation can refer to the foregoing embodiments, and will not be described here.

[0045] The storage medium can include a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, and more specifically, can include specific categories such as a static random access memory (SRAM) and a dynamic random access memory (DRAM).

[0046] The above is only a preferred embodiment of the present application, and does not limit the present application in any form. Although the present application has been disclosed as above with reference to the preferred embodiment, it is not intended to limit the present application. Any person skilled in the art can make some changes or modifications to the above disclosed technical content without departing from the scope of the technical solution of the present application, and any equivalent embodiments with equivalent changes are equivalent. Any modification, change, modification and modification of the above embodiments according to the technical essence of the present application, as long as it does not deviate from the technical solution of the present application, still belongs to the scope of the technical solution of the present application.

Claims

1. A method for persisting a state machine, characterized in that... include: Step 1: Define a state machine according to the business scenario and issue the state machine; Step 2: Load the state machine, execute the events of the initial state, and coordinate and control the events by the Kubernetes-based state machine operator, including the following steps: (1) Obtain information for the current stage; (2) Determine whether the current stage has been completed; (3) Assuming that the event in the current stage has not been completed, it is necessary to determine whether there is a loop waiting. If there is a loop waiting, the operator's requeue is called to exit, and the state machine is re-added to the execution queue. After waiting for a few seconds, reconcile is run again, and the loop continues until the event in this stage is completed. Assuming that there is no loop waiting, return is called to exit, and the state machine will exit until the downstream callback changes the event state of the stage and reconcile is executed again. (4) Assuming the current stage event is completed, it is necessary to jump to the next stage state and repeat steps (1)-(4) until all stages are completed; Step 3: Sensing the event execution result, loading the next event, and repeating the process of steps 1-3 until the last state is reached.

2. The state machine persistence method according to claim 1, characterized in that: In step 1, the state machine is sent out by transmitting the state machine description file.

3. The state machine persistence method according to claim 2, characterized in that: After receiving the state machine description file, the file is stored in the etcd storage system.

4. The state machine persistence method according to claim 1, characterized in that... The criteria for judgment in step (2) include: actively calling the downstream execution unit to check the event status; or waiting for the downstream execution unit to call back and modify the current status.

5. A state machine persistence system, comprising a request unit, a control unit, and an execution unit, characterized in that, The state machine persistence system is used to execute the state machine persistence method as described in any one of claims 1-4; the request unit is used to send out the state machine combined with business attributes; the control unit is used to parse the state machine and control the execution of events; and the execution unit is used to execute events.

6. A computer-readable storage device, characterized in that, The device stores processor-executable instructions that, when executed by the processor, cause the processor to perform the method according to any one of claims 1 to 4.

Citation Information

Patent Citations

  • State machine persistence method and state machine persistence system

    CN111142975A