1. Finite state machines

Let’s start with a few concepts about state machines

  1. States. In a state machine, states are known and finite
  2. Events, a state machine instance accepts events and senses the current state for different processing
  3. Transition, defines the processing flow of the event in a specific state, bing and outputs the processed state

It can be seen from the above introduction that in the state machine, all states, events and converters are defined in advance. In order to better understand the implementation of the state machine, we select the existing implementation from shallow to deep, and explain the implementation and application of the state machine

2. Mina state machine realization

To introduce the Mina state machine, we will introduce some basic concepts:

A State machine is a complete State machine. A State machine is a State machine. A State machine is a State machine. Changes the state of the current StateContext. StateContext, the unique context of the current state machine, stores the state to which the current Event belongs, and some general attribute data. Event, indicating an EventCopy the code

Specific diagram:As can be seen from the figure above, the state of an entity is not maintained in StateMachine, but in StateContext. Therefore, context of each entity needs to be maintained independently to achieve the coexistence of multiple state machines. StateMachine only focuses on the processing of state logic

After StateMachine is initialized, all states are included. State will include the Transition converter with the state as the lead state. As shown in the figure below, Transition can be used as a connection between two statesThe specific entry is in StateMachine. Handle (Event Event). The handle method of the StateMachine will receive events with StateContext, and the specific logic is as follows:

  1. After receiving the event, Handle stores the event in the column
  2. Retrieves an event from the queue and the StateContext from the event
  3. Process the event with Transition from the current state. After processing the new state, the state is updated to StateContext

The pseudo-code flow is as follows:

  1. The StateMachine. Handle entry method preserves only the core logic code
Public void handle Event (Event) {/ / the Event Event to join eventQueue queue LinkedList < Event > eventQueue = eventQueueThreadLocal. The get ();  eventQueue.addLast(event); // Process the message queue processEvents(eventQueue); }Copy the code
  1. Process events in the queue
private void processEvents(LinkedList<Event> eventQueue) { while (! eventQueue.isEmpty()) { Event event = eventQueue.removeFirst(); StateContext context = event.getContext(); Handle (Context.getCurrentState (), event); handle(context.getCurrentState(), event); }}Copy the code
  1. Process a single event by fetching all transitions from state and then processing the event until it is successfully processed
private void handle(State state, Event event) { StateContext context = event.getContext(); for (Transition t : state.getTransitions()) { if (t.execute(event)) { setCurrentState(context, t.getNextState()); return; }}}Copy the code

Mina state machine is just a simple implementation, hoping to give you a preliminary understanding of the shape machine. Meanwhile, mina has many features that are not supported, such as:

  1. Implementation of complex state machine processes

    The matching input state and event are defined in the Transition converter, and there is only one state for the output, which cannot be dynamically selected based on the condition

  2. Persistence of state machines

    It may take some time to complete all states ina state machine. If the service is restarted, the data in the state machine will be lost. Besides, the state machine in MINA does not terminate, so there is no clear termination point for the state machine

The above process code does not contain the processing of exceptions and concurrent control. If you are interested in the source code, you can see the specific introduction of the package

< the dependency > < groupId > org. Apache. Mina < / groupId > < artifactId > mina - the statemachine < / artifactId > < version > 2.1.4 < / version > </dependency>Copy the code

3. Specific application

  1. For specific applications, you can take a look at the service orchestration product of Ali Cloud. The orchestration of a service can be considered as a model version of the state machine, which defines the whole process and promotes the process by promoting the state change. If you are interested, you can take a look at the service orchestration of Ali Cloud
  2. For those interested, see the implementation of distributed transactions in the Seata Saga model, which also uses the idea of state machines to implement service orchestration

Reference:

  1. Mina.apache.org/mina-projec…
  2. Gitee.com/wphmoon/sta…
  3. Help.aliyun.com/document_de…
  4. Seata. IO/useful – cn/docs /…
  5. Docs.aws.amazon.com/zh_cn/step-…