Model-Oriented Software Architecture 3-resource management model Reading Notes (7)-coordinator model

Source: Internet
Author: User

3.3 Coordinator mode

The Coordinator mode describes how to maintain system consistency by coordinating the completion of tasks involving multiple participants (each participant contains resources, resource users, and resource providers. This mode proposes a solution that enables tasks involving multiple participants to be completed, or tasks of all participants are not completed. This ensures that the system is always in the same state.

 

1. Problem

Many systems perform tasks involving more than one participant. A participant is an active entity that includes both resource users and resource providers. In addition, in some cases, resources (such as services) can be active, so they are directly involved in the task. Participants may be in the same process, or across multiple processes and nodes. Each participant executes a part of the task sequentially. In order for the task to succeed, all the tasks executed by each participant must be successful. If the task is successfully executed, the change should keep the system in a consistent state. However, consider what will happen if a participant fails to perform the work. If this task is located at a later stage of the task execution sequence, many participants have completed the task. The work of these participants may change the system. However, failed participants cannot make necessary changes to the system. As a result, system changes are inconsistent. This may cause instability or even errors to the system. In such a system, some failures are even worse than global failures.

One possible solution is to introduce point-to-point communication between participants. Each participant delivers their work results to other participants and takes necessary steps to ensure system status consistency. However, such a solution requires all participants to focus on all other participants involved in the task, which is unrealistic. In addition, such a solution also has poor scalability for the number of participants. To solve these problems, pay attention to the following points:

1) consistency ). A task should either create a new legal status for the system, or (if an error occurs) Restore all data to the status before the task starts.

2) atomicity (Atomicity ). In a task involving two or more participants, or all the work of the participants is completed, or all the work is not completed (although the participants are independent of each other ).

3) location transparency ). The solution should be applicable to distributed systems, although distributed systems are more likely to encounter local failures than integral systems.

4) scalability ). The solution should be scalable to the number of participants without significantly reducing performance.

5) Transparency ). The solution should be transparent to system users and should be minimizedCodeRequirements for changes.

 

2. Solution

Introduce the Coordinator to perform and complete tasks for all participants. The work performed by all participants is divided into two phases: Preparation and commit ).

In the first phase, the Coordinator requires each participant to prepare for the work to be completed. Each participant must use this phase to check consistency and determine whether the execution result will fail. If a participant does not return a successful result in the preparation phase, the Coordinator stops the task execution sequence. The Coordinator will require all participants who have successfully completed the preparation phase to stop and resume the status before the task starts. Because no permanent changes have been made to the participants, the system status remains consistent.

If all participants have successfully completed the preparation phase, the Coordinator initiates the submission phase for each participant. Participants will perform practical work at this stage. Because each participant indicates that the work is successful in the preparation phase, the submission phase is successful and the entire task is successfully executed.

 

3. Structure

A task is a unit of work that involves multiple participants.

A participant is an active entity that performs part of the task. Participants can include resource users, resource providers, and resources.

Coordinator is the entity responsible for coordinating the overall completion of tasks.

The client is the initiator of the task. The customer guides the Coordinator to execute tasks.

 

 

4. Implementation

1) identify participants. Any participant, as long as their work needs to be coordinated, must be identified from the very beginning. In the context of resource management, participants may be resource users, resource providers, or resources themselves. A resource user can be a participant. If the user actively tries to acquire, use, and release multiple resources, the resource provider can also be a participant, coordination is required when it tries to provide one or more resources to resource users. resources (such as services) can also be participants. If they are active, they can directly participate in the task.

2) define the coordination interface. Define a coordination interface implemented by the participants of a part of the work to be executed.

Public Interface Coordination {

Public BooleanPrepare ();

Public VoidAbort ();

Public VoidCommit ();

}

The coordinator uses the prepare () method to initiate a preparation phase for each participant. Each participant uses this phase to check consistency and determine whether execution will cause failure. If the returned value is true, the preparation phase is successful. Therefore, the submission phase is successful. If the returned value is false, the preparation stage fails. Therefore, the participant cannot successfully execute the submission stage.

If any participant returns false in the preparation phase, the Coordinator terminates the task. It calls the abort () method of each participant that has returned successfully from the preparation phase. This indicates to the participants that the task is terminated and they need to perform all necessary cleanup operations.

3) define the Coordinator interface. The Coordinator interface should provide the customer with methods to start or end the task. In addition, it should allow participants of the task to register. Before participants can register them, they must be able to discover coordinators. To do this, participants can use the lookup mode.

Public Interface Coordinator {

Public VoidBegintask ();

Public VoidRegister (coordination particle ant );

Public BooleanCommittask ();
}

The customer uses the begintask () method to define the start of a task. At this time, the coordinator does not do anything. At the beginning of a task, the task participants register with the Coordinator using the Register () method. Once all participants are registered, the customer executes the committask () method of the Coordinator. At this time, the Coordinator means two events. First, all task participants are registered. Second, the Coordinator can start to coordinate the participants to execute the task. Now the coordinator uses the two-phase commit protocol defined by the Coordinator interface to ensure that the participants complete the task.

4) handle error conditions. The Coordinator allows participants to indicate whether the work they are responsible for is successful. If any participant indicates that the task is not successful, the Coordinator can abort the task without causing any persistent inconsistency of the system status. If all participants indicate that the preparation phase is successful, the Coordinator then submits the phase to all participants. However, there is a possibility that one or more participants fail in the submission phase, although their preparation phase has been completed successfully. This may be caused by factors beyond the control of the participants, such as disconnection. To handle such error conditions, participants can maintain status information optional before performing the tasks they are responsible. If a participant fails to submit, the Coordinator can call the rollback () method for the remaining successfully submitted participants. This gives participants a chance to restore their status before they execute the task. However, such a feature requires maintenance of status information, which may be costly.

 

5. Conclusion

Advantages:

1) atomicity (Atomicity ). The Coordinator mode ensures that the tasks involving two or more participants, or the work of all participants is completed, or all work is not completed. The preparation phase ensures that all participants can complete their work. If any participant returns a failure in the preparation phase, the task is executed, ensuring that no participant completes the work.

2) consistency ). The Coordinator mode ensures that the system status is consistent. If a task is successfully executed, a new legal state is created for the system. On the other hand, if any failure occurs, the Coordinator mode ensures that all data is returned to the status before the task starts. Because each task is atomic, or all participants complete their work, or no participant completes any work. In both cases, the result is that the system is in the same state.

3) scalability ). The solution is scalable for participant data. Increasing the number of participants does not affect task execution. With the increase of participant data, one of the participants is more likely to fail. However, when this mode is used, the failure will be detected in the preparation phase, thus ensuring that the system is in a consistent state.

4) Transparency ). The Coordinator mode is transparent to users, because the two-step commit execution of the task is invisible to users.

 

Disadvantages:

1) overhead ). The Coordinator mode requires that each task be divided into two stages, which leads to the execution sequence involving all participants being repeated twice. If the participants are distributed, this means two times the remote call data, which may also lead to loss of transparency.

2) Additional responsibilities (additional responsibility ). The Coordinator mode adds additional responsibilities to participants, and they also need to be registered with the Coordinator. If the participants are distributed, the registration process will cause the participants to execute remote calls.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.