The main logic of using hadoop RPC on the client is as follows: Code Complete remote function call
Object proxy = rpc. getproxy (protocol, address: Port, socketfactory );
(Protocol) proxy. Method (...);
1. There is a protocol-> rpcengine ing in the RPC layer. The default value is writablerpcengine.
When the first line of code is executed, a dynamic proxy object is generated at the rpcengine Layer Based on protocol.
Proxy. newproxyinstance (protocol. classloader, protocol, new invoker (protocol, address: Port, socketfactory ));
When executing the second line of code to execute a remote function, the local code is
Invoker. Invoke (method, paramters) its workflow is
A. Maintain a socketfactory-> client ing in the rpcengine layer. If no socking exists, a new client is generated.
Execute client. Call (method: paramters, address: Port, protocol...). The logic is to find the corresponding network connection at the client layer, and then
Sending method: paramters is serialized and sent out, waiting for the server to return the calculation result
B. Maintain a address ing of Address: Port-> connection on the client layer. If no connection is found, a address ing is generated.
The generated connection will use pinginputstream to wrap inputstream. Because the socket sets the read timeout, the read logic is added.
Capture sockettimeoutexception. Every time this exception is caught, ping data is written to the output stream. At the same time, you must
Write some handshake information to the output stream. If the server verification fails, the connection is closed. The handshake information is:
"HRPC"-server. current_version-Protocol. getname (). bytes ()
A connection is a separate thread that maintains two data items: A Connection established with the specified address: Port, and a connection established with the specified address: port.
A map structure of callid-> call. Each time client. Call is called on the upper layer, a new call object is generated, which mainly includes method: paramters,
A globally unique callid will be assigned to it and placed in the task map.
The running logic of the connection thread is: While (waitforwork () receiveresponse (); close ();
1. waitforwork ()-if the connection is not marked as closed, it is marked as closed mainly because of ioexception caused by network exceptions.
If the task map is empty and RPC is not closed, if all three conditions are met, it will be based on the last connection read time and the current time
Calculate the idle time of the connection, and then the remaining time of the sleep maximum idle time. If the three conditions are not met, false is returned immediately.
Next, if the condition map is not empty, RPC is not closed, and the connection is not marked as closed, true is returned. Otherwise, false is returned.
If the connection is disabled, the connection is closed.
2. receiveresponse ()-Exit immediately if the connection flag is closed.
Otherwise, readint () is used to read the response data from the server. The value is callid, and then readint. The value is the server computing result status.
If success is used, the computation is completed successfully. Then, the computation result is read, the result is written to the call object, and the task map is
This call is excluded; if it is an error, it indicates that the calculation is abnormal, then the exception information is read, and then the exception information is encapsulated and written.
The call object is also removed from the task map. Writing the calculation result or exception information to the call will cause the thread that submits the task to execute it.
The invoker. Invoke thread is awakened to obtain the calculation result.
3. If the thread exits the while loop due to waitforwork () = false, the connection close operation is executed, mainly to close the output input.
Stream and socket. If there are still tasks in the task map at this time, you need to wake up the threads that execute invoker. invoke and tell them the tasks.
If the execution fails, the exception information is written to the call object.
2. synchronization between the connection thread and the invoker. Invoke call thread
The main process for the invoker. Invoke thread to execute client. Call is
A. CILS. Put (callid, call)
B. sendparam (CALL) sends the call information through connection.
C. While (! Call. Done) Call. Wait (); wait until the task is completed or the connection is closed
D. Obtain the calculation result or exception information.
The connection thread is mainly used to read server response data.