Active Object

Active Object

Active Object An Object Behavioral Pattern for Concurrent Programming R. Greg Lavender Douglas C. Schmidt [email protected] [email protected] ISODE Consortium Inc. Department of Computer Science Austin, TX Washington University, St. Louis An earlier version of this paper appeared in a chapter in TRACKING the book “Pattern Languages of Program Design 2” ISBN SATELLITES STATION 0-201-89527-7, edited by John Vlissides, Jim Coplien, and PEERS Norm Kerth published by Addison-Wesley, 1996. Abstract STATUS INFO This paper describes the Active Object pattern, which decou- ples method execution from method invocation in order to WIDE AREA NETWORK simplify synchronized access to an object that resides in its COMMANDS BULK DATA own thread of control. The Active Object pattern allows one TRANSFER or more independent threads of execution to interleave their access to data modeled as a single object. A broad class of GATEWAY producer/consumer and reader/writer applications are well- suited to this model of concurrency. This pattern is com- monly used in distributed systems requiring multi-threaded LOCAL AREA NETWORK servers. In addition, client applications, such as window- GROUND ing systems and network browsers, employ active objects to STATION simplify concurrent, asynchronous network operations. PEERS Figure 1: Communication Gateway 1Intent The Active Object design pattern decouples method execu- In our example, the Gateway, suppliers, and consumers tion from method invocation to enhance concurrency and communicate over TCP, which is a connection-oriented pro- simplify synchronized access to an object that resides in its tocol [4]. Therefore, the Gateway software may encounter own thread of control. flow control from the TCP transport layer when it tries to send data to a remote consumer. TCP uses flow control to ensure that fast suppliers or Gateways do not produce data 2 Also Known As more rapidly than slow consumers or congested networks can buffer and process the data. Concurrent Object and Actor To improve end-to-end quality of service (QoS) for all suppliers and consumers, the entire Gateway process must not block waiting for flow control to abate over any one con- 3 Example nection to a consumer. In addition, the Gateway must be able to scale up efficiently as the number of suppliers and To illustrate the Active Object pattern, consider the design consumers increase. of a communication Gateway [1]. A Gateway decouples co- An effective way to prevent blocking and to improve per- operating components and allows them to interact without formance is to introduce concurrency into the Gateway de- having direct dependencies among each other [2]. The Gate- sign. Concurrent applications allow the thread of control of way shown in Figure 1 routes messages from one or more an object O that executes a method to be decoupled from the supplier processes to one or more consumer processes in a threads of control that invoke methods on O . Moreover, us- distributed system [3]. ing concurrency in the gateway enables threads whose TCP 1 connections are flow controlled to block without impeding This decoupling is designed so the client thread appears to the progress of threads whose TCP connections are not flow invoke an ordinary method. This method is automatically controlled. converted into a method request object and passed to another thread of control, where it is converted back into a method and executed on the object implementation. 4 Context An active object consists of the following components. A Proxy [5, 2] represents the interface of the object and a Clients that access objects running in separate threads of con- Servant [6] provides the object’s implementation. Both the trol. Proxy and the Servant run in separate threads so that method invocation and method execution can run concurrently: the proxy runs in the client thread, while the servant runs in a dif- 5Problem ferent thread. At run-time, the Proxy transforms the client’s method invocation into a Method Request, which is stored Many applications benefit from using concurrent objects to in an Activation Queue by a Scheduler. The Scheduler runs improve their QoS, e.g., by allowing an application to handle continuously in the same thread as the servant, dequeueing multiple client requests in parallel. Instead of using single- Method Requests from the Activation Queue when they be- threaded passive objects, which execute their methods in the come runnable and dispatching them on the Servant that im- thread of control of the client that invoked the methods, con- plements the Active Object. Clients can obtain the results of current objects reside in their own thread of control. How- a method’s execution via the Future returned by the Proxy. ever, if objects run concurrently we must synchronize access to their methods and data if these objects are shared by mul- tiple client threads. In the presence of this problem, three forces arise: 7 Structure 1. Methods invoked on an object concurrently should not The structure of the Active Object pattern is illustrated in the block the entire process in order to prevent degrading the following Booch class diagram: QoS of other methods: For instance, if one outgoing TCP connection to a consumer in our Gateway example becomes blocked due to flow control, the Gateway process should still loop { be able to queue up new messages while waiting for flow m = act_queue_.dequeue() control to abate. Likewise, if other outgoing TCP connec- Proxy if (m.guard()) m.call() tions are not flow controlled, they should be able to send } Future m1() 1: enqueue(new M1) messages to their consumers independently of any blocked Future m2() connections. Future m3() 3: dispatch() Activation Scheduler Queue 2. Synchronized access to shared objects should be sim- dispatch() enqueue() ple: Applications like the Gateway example are often hard VISIBLE enqueue() 1 1 dequeue() to program if developers must explicitly use low-level syn- TO CLIENTS 1 2: enqueue(M1) chronization mechanisms, such as acquiring and releasing 1 1 M1 mutual exclusion (mutex) locks. In general, methods that are Servant Method n subject to synchronization constraints should be serialized INVISIBLE m1() M2 TO Request transparently when an object is accessed by multiple client CLIENTS m2() 11 threads. m3() guard() M3 4: m1() call() 3. Applications should be designed to transpar- ently leverage the parallelism available on a hard- ware/software platform: In our Gateway example, mes- There are six key participants in the Active Object pattern: sages destined for different consumers should be sent in par- allel by a Gateway over different TCP connections. If the Proxy entire Gateway is programmed to only run in a single thread of control, however, performance bottlenecks cannot be al- A Proxy [2, 5] provides an interface that allows clients leviated transparently by running the Gateway on a multi- to invoke publically accessible methods on an Ac- processor. tive Object using standard, strongly-typed program- ming language features, rather than passing loosely- typed messages between threads. When a client invokes 6 Solution a method defined by the Proxy, this triggers the con- struction and queueing of a Method Request object onto For each object that requires concurrent execution, decou- the Scheduler’s Activation Queue, all of which occurs ple method invocation on the object from method execution. in the client’s thread of control. 2 Method Request Proxy, a Future is returned immediately to the client. The Future reserves space for the invoked method to A Method Request is used to pass context information store its results. When a client wants to obtain these re- about a specific method invocation on a Proxy, such sults, it can “rendezvous” with the Future, either block- as method parameters and code, from the Proxy to a ing or polling until the results are computed and stored Scheduler running in a separate thread. An abstract into the Future. Method Request class defines an interface for execut- ing methods of an Active Object. The interace also contains guard methods that can be used to determine 8 Dynamics when a Method Request’s synchronization constraints are met. For every Active Object method offered by The following figure illustrates the three phases of collabo- the Proxy that requires synchronized access in its Ser- rations in the Active Object pattern: vant, the abstract Method Request class is subclassed to create a concrete Method Request class. Instances of these classes are created by the proxy when its methods Client Scheduler M1 Activation are invoked and contain the specific context informa- Proxy Queue Servant tion necessary to execute these method invocations and INVOKE m1() return any results back to clients. CREATE METHOD enqueue(new M1) REQUEST RETURN FUTURE future() CONSTRUCTION Activation Queue METHOD OBJECT enqueue(M1) / INSERT INTO ACTIVATION QUEUE An Activation Queue maintains a bounded buffer of guard() DEQUEUE SUITABLE pending Method Requests created by the Proxy. This METHOD REQUEST dequeue(M1) queue keeps track of which Method Requests to exe- EXECUTION dispatch(M1) SCHEDULING EXECUTE cute. It also decouples the client thread from the servant call() thread so the two threads can run concurrently. m1() RETURN RESULT reply_to_future() Scheduler COMPLETION A Scheduler runs in a different thread than its clients, managing an Activation Queue of Method Requests 1. Method Request construction and scheduling: In this that are pending execution. A Scheduler decides which phase, the client invokes a method on the Proxy. This trig- Method Request to dequeue next and execute on the gers the creation of a Method Request, which maintains the Servant that implements this method. This schedul- argument bindings to the method, as well as any other bind- ing decision is based on various criteria, such as or- ings required to execute the method and return its results.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    12 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us