HDFS2.x RPC Process Analysis

Source: Internet
Author: User

HDFS2.x RPC Process Analysis 1 Overview

Hadoop provides a unified RPC mechanism for communication between client-namenode, namenode-dataname, and client-dataname. RPC is the core of the communication framework in Hadoop. ProtocolBuf is used as the default Implementation of RPC. The overall calling process of RPC is as follows:

<ClientProtocol> proxyInfo =. dtService =. namenode = proxyInfo. getProxy ();

 

This indicates that the namenode implements the ClientProtocol interface, that is, the RPC communication protocol between the Client and the NameNode.

HDFS2.x introduces the HA of NameNode, which enables the Client to have multiple underlying proxies connected to Active NN and Standby NN respectively. However, in the actual operation process, you need to call the Client to present a unified interface, so there is an upper-layer proxy to unify the above two bottom-layer proxies. All methods called by Clientfa first reach the upper-layer proxy and forward it to the lower-layer proxy through the upper-layer proxy. In addition, the upper-layer proxy determines whether to perform operations such as Failover or Retry Based on the Exception returned by the underlying proxy.

When using the HA mode, the overall process for creating a proxy on the client is as follows:

 

Where,

(1)RetryProxy. createThe method creates an upper-layer proxy to receive client requests and call the underlying proxy that connects to the current two NameNode as needed.

 Class<?>

 

The generated proxy object implements the ClientProtocol interface, through which the Client can call the corresponding methods in the ClientProtocol interface. According to the dynamic proxy mechanism of Java, you can call the methods of this proxy object to call the invoke () method in the RetryInvocationHandler (proxyProvider, retryPolicy) object. RetryInvocationHandler is closely related to FailoverProxyProvider because it requires FailoverProxyProvider to provide support for underlying proxies.

(2)When a request is received by a contemporary object, the invoke method is called to process the request.InvokeThe method is the RetryInvocationHanlder. invoke method in the upper-layer proxy.

First, obtain a RetryPolicy. the Default policy is the parameter when constructing RetryInvocationHandler. The RetryPolicy of ClientProtocol between Client and NameNode is:

FailoverOnNetworkException(RetryPolicies.TRY_ONCE_THEN_FAIL, Config. maxFailoverAttempts, config. failoverSleepBaseMillis,

Next, the invokeMethod method is called to call the underlying proxy for actual processing:

Method. invoke (currentProxy, args );

CurrentProxyIs the underlying proxy currently in use. When a master-slave switchover occurs in NN, The currentProxy also changes accordingly.

If an exception occurs during the call process, different processing will be performed for different exceptions. The judgment here is based on the RetryPolicy policy given when a dynamic proxy (upper-layer proxy) is generated, the default RetryPolicy isFailoverOnNetworkExceptionRetryTherefore, the corresponding shouldRetry () function is called.

(2.1)If the number of retries exceeds the maximum number of attempts,

RetryAction. retrydeworkflow. FAIL RetryAction.

(2.2)If one of the thrown exceptions is ConnectionException, NoRouteToHostException, UnKnownHostException, StandbyException, and RemoteException, it indicates that the underlying proxy is unable to connect to the Active NN during the RPC process, or the Master/Slave switchover has occurred, then a RetryAction is returned. retrydemo.FAILOVER_AND_RETRYYou need to execute the restore mfailover () operation, and then try again with the underlying proxy of another NN.

(2.3)If the thrown exception is a SocketException, IOException, or other non-RemoteException exception, you cannot determine whether the RPC command is successfully executed. It may be a local Socket or IO problem, or it may be a NN Socket or IO problem. Further judgment: If the called method is idempotent, that is, multiple executions have no side effects, connect to another underlying proxy and try again; otherwise, RetryAction is directly returned. retrydemo.FAIL.

(3)FailoverProxyProviderThe current implementation class of the class isConfiguredFailoverProxyProvider. It is responsible for managing the two activeNN and standbyNN proxies. When the upper-layer proxy receives an RPC command from the user, it forwards it to the currently used underlying proxy (by ConfiguredFailoverProxyProvider. currentProxyIndex indicates the serial number of the current proxy object), and then checks whether an exception is thrown. If an exception is thrown, you can determine whether to execute a failover or retry Based on the exception type, or do not do either. If you need to switch the NameNode proxy, the following command is executed:

The underlying proxy is implemented in non-HA mode:

CreateNonHAProxy(Conf,

False). GetProxy ();

Call NameNodeProxies. createNNProxyWithClientProtocol

RPC.GetProtocolProxy

And encapsulate the proxy object proxy of the ClientNamenodeProtocolPB typeClientNamenodeProtocolTranslatorPBType.

The dynamic proxy of Java is involved in RPC.GetProtocolProxyMethod when the proxy object is generated, RPC.GetProtocolProxyThe implementation code is:

Return GetProtocolEngine(Protocol, conf). getProxy (protocol, clientVersion, addr, ticket, conf, factory, rpcTimeout, connectionRetryPolicy );

The engine here is protocolbuf. Therefore, all RPC requests will eventually call the invoke method in the ProtobufRpcEngine class to communicate with the RPC Server and perform data serialization and deserialization operations.

The operation that encapsulates Client requests into calls is also carried out in invoke:

RPC_PROTOCOL_BUFFER,

NewRpcRequestWritable (rpcRequest), remoteId );

The specific implementation of encapsulation is to call the call method in the Client class:

NewCall (rpcKind, rpcRequest );

The execution process (HA mode) of the RPC client is as follows:

 

Actually initialize the NameNodeRpcServer object and call its constructor:

Return NewNameNodeRpcServer (conf,This);

In the constructor, two rpcservers are initialized. One is serviceRpcServer, which is used to process RPC requests sent from data nodes and the other is clientRpcServer, which is used to process RPC requests sent from clients.

The NameNodeRpcServer constructor initializes the number of handler required by the RPC Server (10 by default), sets the processing engine to Protocolbuf, and initializes the ClientNamenodeProtocolServerSideTranslatorPB type object.ClientProtocolServerTranslatorIt is used to deserialize transmitted data and serialize sent data.

In addition, the object BlockingService that provides different RPC services will be initialized. for clients and data nodes:

ClientProtocolServerTranslator);

Then, the Server object of RPC is obtained:

.clientRpcServer = RPC.getServer(org.apache.hadoop.hdfs.protocolPB.ClientNamenodeProtocolPB., 
conf,
             namesystem.getDelegationTokenSecretManager());

 

This object is mainly responsible for receiving network connections, reading data, calling data processing functions, and returning results. The first two parameters indicate that if RPC sends the ClientNamenodeProtocolPB protocol, the service responsible for processing this Protocol (the object of the com. google. protobuf. BlockingService type) is the clientNNPbService.

This Server object contains Listener, Handler, and Responder internal classes:

(1) Listener Thread: The Server starts a Listener thread, which is mainly used to listen to requests sent by the Client. Listene starts a Reader thread group, the Connection object sent from the client is passed to Reader through NIO's SelectionKey. The Listener is equivalent to forwarding only one layer;

(2)Reader Thread Pool: It is mainly used to read the Connection passed by Listener, Call the readAndProcess method of Connection to read the Request, and encapsulate the Request into a Call into the Call Queue;

(3)Hanlder Thread Pool: The Server starts a group of threads to process the Call in the Call Queue and puts the Respone to the response queue;

(4)Responder Thread: Mainly processes the response in the response queue and sends the response to the client. If the current response queue is empty, the first newly added response will be sent to the client immediately, will not be sent through responer thread.

This RPC. getServer () will be called layer by layer, because the default RPCEngine isProtobufRpcEngine(ProtobufRpcEngine. java), the function ProtobufRpcEngine. getServer is called. A Server object is generated here, which is used to receive RPC requests, process, and reply from the client. This Server object is a pure network service Server, which plays a role in the basic network I/O service in RPC.

The general process of RPC Server creation is as follows:

 

4.1 Reader Processing

The Reader thread in the Server is also based on the Selector asynchronous IO mode.SelectionKeyThen, the SelectionKey is called. attachment () obtains the Connection object of the SelectionKey attach (attatch In the Listener run method), and then executes the corresponding readAndProcess () method, read the network IO data on the pipeline corresponding to this SelectionKey into the buffer zone. The readAndProcess () method is calledServer. processData ()In this method, the data read from network IO is deserialized into an rpcRequest object.

The type of the rpcRequest object is the object that inherits the subclass of the Writable type, that is, the class that can be serialized/deserialized. Here, the Content Object of the RPC Request contained in the rpcRequest object is composed. class generated by Message in the proto file, that is, the class automatically compiled by the PB framework. You can call the get method of this class to obtain the real data transmitted in RPC. Put the generated rpcRequest object into a Call object, and then put the Call object in the queue.Server. callQueue.

The process flow chart of Reader is as follows:

Actually executed in the last call () function.

The call method first checks whether the data sent from this request is reasonable. Then the service that implements this protocol is obtained. The service implementing the Protocol has already been registered during initialization, that is, the com. google. protobuf. BlockingService object mentioned above.ClientNNPbService.

This is the service that implements the ClientNamenodeProtocol between the Client and the NameNode. by calling this Code:

Result = service. callBlockingMethod (methodDescriptor, null, param );

The logic of this RPC request is executed. The service object transfers the corresponding method call to an implementation class inherited from the BlockingInterface interface. The real implementation class of the Service isClientProtocolServerTranslatorIs the parameter of the function newReflectiveBlockingService. This class is a subclass of ClientNamenodeProtocolProtos and is created based on the proto file during HDFS compilation. Since the parameters passed in the clientProtocolServerTranslator constructor are NameNodeRpcServer, further method calls are implemented in NameNodeRpcServer.

The Handler processing process is as follows:

 

If the metadata operation logic NameNodeRpcServer throws an IOException, it encapsulates it as a ServiceException and passes it to the client. On the client side, IOException encapsulated in ServiceException is obtained through ProtobufHelper. getRemoteException.

The general process of RPC Server is as follows:

 

 

 

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.