Class RequestCorrelator
- java.lang.Object
-
- org.jgroups.blocks.RequestCorrelator
-
public class RequestCorrelator extends java.lang.ObjectFramework to send requests and receive matching responses (matching on request ID). Multiple requests can be sent at a time. Whenever a response is received, the correctRspCollectoris looked up (key = id) and its methodreceiveResponse()invoked. A caller may usedone()to signal that no more responses are expected, and that the corresponding entry may be removed.RequestCorrelatorcan be installed at both client and server sides, it can also switch roles dynamically; i.e., send a request and at the same time process an incoming request (when local delivery is enabled, this is actually the default).- Author:
- Bela Ban
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classRequestCorrelator.HeaderThe header for RequestCorrelator messages
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Stack<Address>call_stackThis field is used only if deadlock detection is enabled.protected booleanconcurrent_processingProcess items on the queue concurrently (Scheduler).protected booleandeadlock_detectionWhether or not to perform deadlock detection for synchronous (potentially recursive) group method invocations.protected Addresslocal_addrThe address of this group memberprotected static org.apache.commons.logging.Loglogprotected RpcDispatcher.Marshaller2marshallerPossibility for an external marshaller to marshal/unmarshal responsesprotected java.lang.Stringnamemakes the instance unique (together with IDs)protected RequestHandlerrequest_handlerThe handler for the incoming requests.protected java.util.concurrent.ConcurrentMap<java.lang.Long,RspCollector>requestsThe table of pending requests (keys=Long (request IDs), values=RequestEntry)protected SchedulerschedulerThe dispatching thread poolprotected booleanstartedprotected java.lang.ObjecttransportThe protocol layer to use to pass up/down messages.
-
Constructor Summary
Constructors Constructor Description RequestCorrelator(java.lang.String name, java.lang.Object transport, RequestHandler handler)Constructor.RequestCorrelator(java.lang.String name, java.lang.Object transport, RequestHandler handler, boolean deadlock_detection)Constructor.RequestCorrelator(java.lang.String name, java.lang.Object transport, RequestHandler handler, boolean deadlock_detection, boolean concurrent_processing)RequestCorrelator(java.lang.String name, java.lang.Object transport, RequestHandler handler, boolean deadlock_detection, Address local_addr)RequestCorrelator(java.lang.String name, java.lang.Object transport, RequestHandler handler, boolean deadlock_detection, Address local_addr, boolean concurrent_processing)RequestCorrelator(java.lang.String name, java.lang.Object transport, RequestHandler handler, Address local_addr)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddone(long id)Used to signal that a certain request may be garbage collected as all responses have been received.AddressgetLocalAddress()RpcDispatcher.MarshallergetMarshaller()protected voidhandleRequest(Message req, RequestCorrelator.Header hdr)Handle a request msg for this correlatorbooleanreceive(Event evt)Callback.booleanreceiveMessage(Message msg)Handles a message coming from a layer belowvoidreceiveSuspect(Address mbr)Event.SUSPECT event received from a layer below.voidreceiveView(View new_view)Event.VIEW_CHANGE event received from a layer below.voidregisterProbeHandler(TP transport)voidsendRequest(long id, java.util.List<Address> dest_mbrs, Message msg, RspCollector coll)voidsendRequest(long id, java.util.List<Address> dest_mbrs, Message msg, RspCollector coll, boolean use_anycasting)Send a request to a group.voidsendRequest(long id, Message msg, RspCollector coll)Helper method forsendRequest(long,List,Message,RspCollector).voidsetConcurrentProcessing(boolean concurrent_processing)voidsetDeadlockDetection(boolean flag)Switch the deadlock detection mechanism on/offvoidsetLocalAddress(Address local_addr)voidsetMarshaller(RpcDispatcher.Marshaller marshaller)voidsetRequestHandler(RequestHandler handler)voidstart()voidstop()voidunregisterProbeHandler(TP transport)
-
-
-
Field Detail
-
transport
protected java.lang.Object transport
The protocol layer to use to pass up/down messages. Can be either a Protocol or a Transport
-
requests
protected final java.util.concurrent.ConcurrentMap<java.lang.Long,RspCollector> requests
The table of pending requests (keys=Long (request IDs), values=RequestEntry)
-
request_handler
protected RequestHandler request_handler
The handler for the incoming requests. It is called from inside the dispatcher thread
-
marshaller
protected RpcDispatcher.Marshaller2 marshaller
Possibility for an external marshaller to marshal/unmarshal responses
-
name
protected java.lang.String name
makes the instance unique (together with IDs)
-
scheduler
protected Scheduler scheduler
The dispatching thread pool
-
local_addr
protected Address local_addr
The address of this group member
-
call_stack
protected java.util.Stack<Address> call_stack
This field is used only if deadlock detection is enabled. In case of nested synchronous requests, it holds a list of the addreses of the senders with the address at the bottom being the address of the first caller
-
deadlock_detection
protected boolean deadlock_detection
Whether or not to perform deadlock detection for synchronous (potentially recursive) group method invocations. If on, we use a scheduler (handling a priority queue), otherwise we don't and call handleRequest() directly.
-
concurrent_processing
protected boolean concurrent_processing
Process items on the queue concurrently (Scheduler). The default is to wait until the processing of an item has completed before fetching the next item from the queue. Note that setting this to true may destroy the properties of a protocol stack, e.g total or causal order may not be guaranteed. Set this to true only if you know what you're doing !
-
started
protected boolean started
-
log
protected static final org.apache.commons.logging.Log log
-
-
Constructor Detail
-
RequestCorrelator
public RequestCorrelator(java.lang.String name, java.lang.Object transport, RequestHandler handler)Constructor. Uses transport to send messages. Ifhandleris not null, all incoming requests will be dispatched to it (viahandle(Message)).- Parameters:
name- Used to differentiate between different RequestCorrelators (e.g. in different protocol layers). Has to be unique if multiple request correlators are used.transport- Used to send/pass up requests. Can be either a Transport (only send() will be used then), or a Protocol (up_prot.up()/down_prot.down() will be used)handler- Request handler. Methodhandle(Message)will be called when a request is received.
-
RequestCorrelator
public RequestCorrelator(java.lang.String name, java.lang.Object transport, RequestHandler handler, Address local_addr)
-
RequestCorrelator
public RequestCorrelator(java.lang.String name, java.lang.Object transport, RequestHandler handler, boolean deadlock_detection)Constructor. Uses transport to send messages. Ifhandleris not null, all incoming requests will be dispatched to it (viahandle(Message)).- Parameters:
name- Used to differentiate between different RequestCorrelators (e.g. in different protocol layers). Has to be unique if multiple request correlators are used.transport- Used to send/pass up requests. Can be either a Transport (only send() will be used then), or a Protocol (up_prot.up()/down_prot.down() will be used)handler- Request handler. Methodhandle(Message)will be called when a request is received.deadlock_detection- When enabled (true) recursive synchronous message calls will be detected and processed with higher priority in order to solve deadlocks. Slows down processing a little bit when enabled due to runtime checks involved.
-
RequestCorrelator
public RequestCorrelator(java.lang.String name, java.lang.Object transport, RequestHandler handler, boolean deadlock_detection, boolean concurrent_processing)
-
RequestCorrelator
public RequestCorrelator(java.lang.String name, java.lang.Object transport, RequestHandler handler, boolean deadlock_detection, Address local_addr)
-
RequestCorrelator
public RequestCorrelator(java.lang.String name, java.lang.Object transport, RequestHandler handler, boolean deadlock_detection, Address local_addr, boolean concurrent_processing)
-
-
Method Detail
-
setDeadlockDetection
public void setDeadlockDetection(boolean flag)
Switch the deadlock detection mechanism on/off- Parameters:
flag- the deadlock detection flag
-
setRequestHandler
public void setRequestHandler(RequestHandler handler)
-
setConcurrentProcessing
public void setConcurrentProcessing(boolean concurrent_processing)
-
sendRequest
public void sendRequest(long id, Message msg, RspCollector coll) throws java.lang.ExceptionHelper method forsendRequest(long,List,Message,RspCollector).- Throws:
java.lang.Exception
-
getMarshaller
public RpcDispatcher.Marshaller getMarshaller()
-
setMarshaller
public void setMarshaller(RpcDispatcher.Marshaller marshaller)
-
sendRequest
public void sendRequest(long id, java.util.List<Address> dest_mbrs, Message msg, RspCollector coll) throws java.lang.Exception- Throws:
java.lang.Exception
-
sendRequest
public void sendRequest(long id, java.util.List<Address> dest_mbrs, Message msg, RspCollector coll, boolean use_anycasting) throws java.lang.ExceptionSend a request to a group. If no response collector is given, no responses are expected (making the call asynchronous).- Parameters:
id- The request ID. Must be unique for this JVM (e.g. current time in millisecs)dest_mbrs- The list of members who should receive the call. Usually a group RPC is sent via multicast, but a receiver drops the request if its own address is not in this list. Will not be used if it is null.msg- The request to be sent. The body of the message carries the request datacoll- A response collector (usually the object that invokes this method). Its methodsreceiveResponse()andsuspect()will be invoked when a message has been received or a member is suspected, respectively.- Throws:
java.lang.Exception
-
done
public void done(long id)
Used to signal that a certain request may be garbage collected as all responses have been received.
-
receive
public boolean receive(Event evt)
Callback.Called by the protocol below when a message has been received. The algorithm should test whether the message is destined for us and, if not, pass it up to the next layer. Otherwise, it should remove the header and check whether the message is a request or response. In the first case, the message will be delivered to the request handler registered (calling its
handle()method), in the second case, the corresponding response collector is looked up and the message delivered.- Parameters:
evt- The event to be received- Returns:
- Whether or not the event was consumed. If true, don't pass message up, else pass it up
-
start
public final void start()
-
stop
public void stop()
-
registerProbeHandler
public void registerProbeHandler(TP transport)
-
unregisterProbeHandler
public void unregisterProbeHandler(TP transport)
-
receiveSuspect
public void receiveSuspect(Address mbr)
Event.SUSPECT event received from a layer below.All response collectors currently registered will be notified that
mbrmay have crashed, so they won't wait for its response.
-
receiveView
public void receiveView(View new_view)
Event.VIEW_CHANGE event received from a layer below.Mark all responses from members that are not in new_view as NOT_RECEIVED.
-
receiveMessage
public boolean receiveMessage(Message msg)
Handles a message coming from a layer below- Returns:
- true if the message was consumed, don't pass it further up, else false
-
getLocalAddress
public Address getLocalAddress()
-
setLocalAddress
public void setLocalAddress(Address local_addr)
-
handleRequest
protected void handleRequest(Message req, RequestCorrelator.Header hdr)
Handle a request msg for this correlator- Parameters:
req- the request msg
-
-