Reactor architecture mode and framework Overview

Source: Internet
Author: User

The reactor framework is the most basic framework in ACE frameworks. Other frameworks use the reactor framework more or less. This article analyzes the basic principles of the reactor architecture mode.

2.1 reactor architecture Mode

For the analysis of each architecture model, we use the analytical style of references, focusing on analyzing the intent, context, problem, solution, structure and implementation. The implementation is the ace source code.

1. Intention

In an event-driven application, one or more customer service requests are separated (demultiplex) and scheduled (dispatch) to the application.

2. Context

In event-driven applications, multiple service requests received at the same time are processed synchronously and sequentially.

3. Problem

In distributed systems, especially server-based event-driven applications, although these requests are ultimately serialized, they must always be prepared to process multiple simultaneous service requests. In actual applications, these requests are always expressed by an event (such as connector, read, and write. Before processing these service requests in an orderly manner, the application must first separate and schedule these simultaneously arriving events. To effectively solve this problem, we need to do the following:

  • To improve system measurability and response time, applications cannot block an event source for a long time and stop processing other events, which seriously reduces the response to the client.
  • To increase throughput, avoid any unnecessary context switching, synchronization, and data movement between CPUs.
  • Introducing new services or improving existing services must have as little impact on the existing event separation and Scheduling Mechanisms as possible.
  • A large number of application code needs to be hidden behind complicated multithreading and synchronization mechanisms.
4. Solution

Wait for the event to arrive at one or more event sources. For example, a connected socket descriptor is an event source. The separation and scheduling of events are integrated into the services that process them, while the separation and Scheduling Mechanisms are separated from the application's processing of specific events, that is to say, the separation and Scheduling Mechanisms are irrelevant to specific applications.

Specifically, each service provided by each application has an independent event processor. The event processor processes specific types of events from the event source. Each event processor is registered in the reactor manager in advance. The reactor Manager uses the synchronization event splitter to wait for an event to occur in one or more event sources. When an event occurs, the synchronization event splitter notifies the reactor manager, and the reactor manager schedules the Event-related event processor to complete the requested service.

5. Structure

In reactor mode, there are 5 key participants.

  • Descriptor (handle): Provided by the operating system to identify every event, such as socket Descriptor and file descriptor. In Linux, it is represented by an integer. Events can come from external sources, such as client connection requests and data. Events can also come from internal sources, such as timer events.
  • Demultiplexer: A function used to wait for one or more events. The caller is blocked until an event occurs in the descriptor set separated by the separator. The select function in Linux is a frequently used separator.
  • Event Handler: an interface composed of one or more template functions. These template functions describe application-related operations on an event.
  • Specific event processor: it is the implementation of the event processor interface. It implements a service provided by the application. The sum of each specific event processor is related to one descriptor. It uses descriptors to identify events and services provided by applications.
  • Reactor Manager (Reactor): defines some interfaces for application control event scheduling and Application Registration and deletion of event processors and related descriptors. It is the scheduling core of the event processor. The reactor Manager uses a synchronization event splitter to wait for an event to occur. Once an event occurs, the reactor manager detaches each event, schedules the event processor, and calls the relevant template function to process the event.

Through the above analysis, we noticed that the reactor manager rather than the application is responsible for waiting for events, separating events and scheduling events. In fact, the reactor manager is not called by a specific event processor. Instead, it schedules a specific event processor and processes the event. This is the "reverse control" similar to the Hollywood principle ". The application only needs to implement a specific event processor, and then register it in the reactor manager. The following work is done by the manager. The relationship between these participants is 2-1.

Now, let's take a look at the relationship between participants in the reactor architecture mode and five elements in the framework based on Chapter 5. the specific implementation of the reactor architecture mode corresponds to element 1, and the event processor interface corresponds to element 2; the specific event processor corresponds to element 3. The reactor Manager uses the Hollywood principle and can be considered to correspond to element 5. The function of element 4 is not obvious and there is no clear ing relationship.

If you still do not understand the reactor architecture mode, it does not matter. The source code will indicate all problems. In this case, you can analyze the reactor architecture mode again and continue with the following content.

Figure 2-1 reactor Architecture Structure

2.2 reactor framework Overview

From the analysis of the reactor architecture pattern, we can see that to design and implement a simple reactor framework to support I/O events, we need to implement two components: the event processor interface and the reactor manager. For other components, such as the synchronization event splitter, you can use the select, poll, or other similar functions provided by the operating system, and the descriptor can use file descriptors or other data structures that can identify events, generally, the operating system provides this function. The event processor Interface contains a series of template functions that can be designed based on the data actually processed. The reactor manager is designed to separate and schedule events.

The ACE reactor framework uses the file descriptor as the descriptor of an I/o Event on the Linux platform and the ace_event_handler class as the processor interface for various events. Place the synchronization event separation function in the reactor manager, so that different synchronization event separation functions need to implement different reactor managers. Ace used the bridge design mode to solve this problem and put the operations related to the synchronization event separation function into the implementor of the bridge design mode. Any synchronization event separation function supported by Ace has a specific implementor.

Ace's reactor Manager also provides operations for implementing the singleton design mode. When using these operations, a process can have only one global reactor manager. When calling the singleton design mode interface, the reactor manager selects a specific implementor according to the operating system configuration at startup. Of course, if you do not like this default implementor, you can use the function to replace it. To improve the system's performance in event separation and scheduling, ACE also allows applications to create multiple reactor manager instances. In this case, the application cannot call operations used in singleton design mode. You can only use the reactor manager Instance Object method to separate and schedule events. Both methods are provided to meet the demanding requirements of applications to the maximum extent.

The reactor framework structure implemented by Ace is much more complex than the structure analyzed in the reactor architecture mode. This is because the ace reactor Framework processes timer, semaphore, and other common events in addition to I/O events, and all these processes must meet cross-platform requirements. To abstract the processing of these events and provide a unified interface for the application, the implementation of the ACE reactor Manager also adopts the facade design mode. In fact, the I/O events, semaphore events, Timer events, and notify events managed by the reactor framework have a small component in implementation, in this way, the reactor manager can be decoupled from the specific event processing. Using the facade design pattern, encapsulate the interfaces of these small components so that applications cannot perceive their existence and reduce the number of objects processed by applications, and make these small components more convenient to use.

The three design patterns and factory design patterns analyzed above are frequently used in the implementation of ACE framework manager. These design patterns and their usage provide a good scenario for us to learn the design patterns, and provide practical methods for us to implement the software framework manager. The reactor framework of ACE is closely related to the five elements of the framework. It is a typical event-driven framework. It opens the door to the ACE framework and serves as the basis for learning other frameworks.

Before going deep into the Framework Code, let's look at a reactor framework example. Although the example is simple, it provides a real application, it also provides some ideas for our analysis.

---------------------------------------------

This article is excerpted from "ace technology Insider: in-depth analysis of ACE Architecture Design and Implementation Principles" by Pan Rong.

Description: This book systematically analyzes the architecture design and implementation principles of the classical network framework ACE (Adaptive Communication Environment) from three dimensions: Architecture mode, programming example, and source code, it can solve four problems: First, it helps framework designers understand the universal principles and ideas of software framework design, and then design their own software frameworks. Second, help ace application developers to deepen their understanding of the ACE framework, improve the development level, better customize and expand the ACE framework, and solve the difficulties in C ++ network communication. Third, help C ++ developers to deepen their C ++ language skills. The book has a lot of analysis on C ++ source code, including network programming, dynamic library programming and asynchronous programming, it also involves the analysis of more than 10 classic design patterns. Fourth, it enhances the technical cultivation of platform developers and software architects. Ace's design and implementation are extremely good, its source code and architecture are worth learning and researching.

The book consists of seven chapters, detailed analysis of the Architecture Design and Implementation Principles of ACE six frameworks, including reactor, service configurator, task, acceptor_connector, proactor, and streams. The analysis of each framework is divided into three parts: first, the design analysis of the framework. Each Framework (except the task framework) has a framework pattern corresponding to it. The framework pattern illustrates the design principle of the framework, the overall structure of the Framework is given, which is the theoretical basis of the learning framework. Second, for application analysis of the framework, each framework has an application instance corresponding to it, application Instances help readers understand how to use the framework, and provide readers with an application program that can be debugged, so that readers can use the debugger to explore the internal secrets of the Framework. Third, implementation analysis of the framework, this is the focus of this book. It analyzes the implementation principles of the framework in detail, and provides UML class diagrams and UML sequence diagrams for the key classes and processes, it allows readers to master the implementation technology of the Framework in a short time.

Net purchase: http://product.china-pub.com/3662472

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.