Storm-source code analysis-Use of Thrift

Source: Internet
Author: User
Document directory
  • 1 IDL
  • 2 client
  • 3 Server
1 IDL

Storm. Thrift defines the data structure and service used as IDL.
Then, backtype. Storm. generated stores the Java code automatically converted from IDL through thrift.

For example, for Nimbus Service
In the definition of IDL,

service Nimbus {  void submitTopology(1: string name, 2: string uploadedJarLocation, 3: string jsonConf, 4: StormTopology topology) throws (1: AlreadyAliveException e, 2: InvalidTopologyException ite);  void submitTopologyWithOpts(1: string name, 2: string uploadedJarLocation, 3: string jsonConf, 4: StormTopology topology, 5: SubmitOptions options) throws (1: AlreadyAliveException e, 2: InvalidTopologyException ite);  void killTopology(1: string name) throws (1: NotAliveException e);  void killTopologyWithOpts(1: string name, 2: KillOptions options) throws (1: NotAliveException e);  void activate(1: string name) throws (1: NotAliveException e);  void deactivate(1: string name) throws (1: NotAliveException e);  void rebalance(1: string name, 2: RebalanceOptions options) throws (1: NotAliveException e, 2: InvalidTopologyException ite);  // need to add functions for asking about status of storms, what nodes they're running on, looking at task logs  string beginFileUpload();  void uploadChunk(1: string location, 2: binary chunk);  void finishFileUpload(1: string location);    string beginFileDownload(1: string file);  //can stop downloading chunks when receive 0-length byte array back  binary downloadChunk(1: string id);  // returns json  string getNimbusConf();  // stats functions  ClusterSummary getClusterInfo();  TopologyInfo getTopologyInfo(1: string id) throws (1: NotAliveException e);  //returns json  string getTopologyConf(1: string id) throws (1: NotAliveException e);  StormTopology getTopology(1: string id) throws (1: NotAliveException e);  StormTopology getUserTopology(1: string id) throws (1: NotAliveException e);}

The Java code corresponding to Nimbus. Java is as follows,

public class Nimbus {  public interface Iface {    public void submitTopology(String name, String uploadedJarLocation, String jsonConf, StormTopology topology) throws AlreadyAliveException, InvalidTopologyException, org.apache.thrift7.TException;    public void submitTopologyWithOpts(String name, String uploadedJarLocation, String jsonConf, StormTopology topology, SubmitOptions options) throws AlreadyAliveException, InvalidTopologyException, org.apache.thrift7.TException;    public void killTopology(String name) throws NotAliveException, org.apache.thrift7.TException;    public void killTopologyWithOpts(String name, KillOptions options) throws NotAliveException, org.apache.thrift7.TException;    public void activate(String name) throws NotAliveException, org.apache.thrift7.TException;    public void deactivate(String name) throws NotAliveException, org.apache.thrift7.TException;    public void rebalance(String name, RebalanceOptions options) throws NotAliveException, InvalidTopologyException, org.apache.thrift7.TException;    public String beginFileUpload() throws org.apache.thrift7.TException;    public void uploadChunk(String location, ByteBuffer chunk) throws org.apache.thrift7.TException;    public void finishFileUpload(String location) throws org.apache.thrift7.TException;    public String beginFileDownload(String file) throws org.apache.thrift7.TException;    public ByteBuffer downloadChunk(String id) throws org.apache.thrift7.TException;    public String getNimbusConf() throws org.apache.thrift7.TException;    public ClusterSummary getClusterInfo() throws org.apache.thrift7.TException;    public TopologyInfo getTopologyInfo(String id) throws NotAliveException, org.apache.thrift7.TException;    public String getTopologyConf(String id) throws NotAliveException, org.apache.thrift7.TException;    public StormTopology getTopology(String id) throws NotAliveException, org.apache.thrift7.TException;    public StormTopology getUserTopology(String id) throws NotAliveException, org.apache.thrift7.TException;  }

 

2 client

1. First get client,

NimbusClient client = NimbusClient.getConfiguredClient(conf);

Check the logic of client. getconfiguredclient under backtype. Storm. utils,

Only the Host: Port of nimbus is retrieved from the configuration, and the new nimbusclient

    public static NimbusClient getConfiguredClient(Map conf) {        try {            String nimbusHost = (String) conf.get(Config.NIMBUS_HOST);            int nimbusPort = Utils.getInt(conf.get(Config.NIMBUS_THRIFT_PORT));            return new NimbusClient(conf, nimbusHost, nimbusPort);        } catch (TTransportException ex) {            throw new RuntimeException(ex);        }    }

Nimbusclient inherits from thrifle tclient and public class nimbusclient extends thrifle tclient

What did thritclient do? The key is how to serialize data and how to transmit data to remote

Here we can see thrift's encapsulation of transport and protocol.

For transport, it is actually the encapsulation of socket, using tsocket (host, Port)

For protocol, tbinaryprotocol is used by default. If you do not specify this parameter

    public ThriftClient(Map storm_conf, String host, int port, Integer timeout) throws TTransportException {        try {            //locate login configuration             Configuration login_conf = AuthUtils.GetConfiguration(storm_conf);            //construct a transport plugin            ITransportPlugin  transportPlugin = AuthUtils.GetTransportPlugin(storm_conf, login_conf);            //create a socket with server            if(host==null) {                throw new IllegalArgumentException("host is not set");            }            if(port<=0) {                throw new IllegalArgumentException("invalid port: "+port);            }                        TSocket socket = new TSocket(host, port);            if(timeout!=null) {                socket.setTimeout(timeout);            }            final TTransport underlyingTransport = socket;            //establish client-server transport via plugin            _transport =  transportPlugin.connect(underlyingTransport, host);         } catch (IOException ex) {            throw new RuntimeException(ex);        }        _protocol = null;        if (_transport != null)            _protocol = new  TBinaryProtocol(_transport);    }

 

2. Call any RPC

Take a look at submittopologywithopts

client.getClient().submitTopologyWithOpts(name, submittedJar, serConf, topology, opts); 

We can see that the interface of nimbus has the definition of this method. Thrift not only automatically generates Java interfaces, but also provides the implementation of the entire RPC client.

    public void submitTopologyWithOpts(String name, String uploadedJarLocation, String jsonConf, StormTopology topology, SubmitOptions options) throws AlreadyAliveException, InvalidTopologyException, org.apache.thrift7.TException    {      send_submitTopologyWithOpts(name, uploadedJarLocation, jsonConf, topology, options);      recv_submitTopologyWithOpts();    }

In two steps,

First, send_submittopologywithopts calls sendbase

Then, recv_submittopologywithopts calls receivebase

  protected void sendBase(String methodName, TBase args) throws TException {    oprot_.writeMessageBegin(new TMessage(methodName, TMessageType.CALL, ++seqid_));    args.write(oprot_);    oprot_.writeMessageEnd();    oprot_.getTransport().flush();  }  protected void receiveBase(TBase result, String methodName) throws TException {    TMessage msg = iprot_.readMessageBegin();    if (msg.type == TMessageType.EXCEPTION) {      TApplicationException x = TApplicationException.read(iprot_);      iprot_.readMessageEnd();      throw x;    }    if (msg.seqid != seqid_) {      throw new TApplicationException(TApplicationException.BAD_SEQUENCE_ID, methodName + " failed: out of sequence response");    }    result.read(iprot_);    iprot_.readMessageEnd();  }

We can see that thrift encapsulates protocol and does not need to process serialization on its own. It can be done by calling the Protocol interface.

 

3 Server

What makes thrift powerful is that it implements the entire protocol stack, not only the transformation of IDL, but also a variety of implementations for the server.

Next let's take a look at the nimbus server, which is written using clojure.

It can be seen that nonblockingserversocket, thshaserver, tbinaryprotocol, and proccessor encapsulated by thrift are very simple.

Processor uses service-handle to process Recv data. Therefore, as a user, you only need to implement Nimbus $ iface in service-handle. Other server-related, thrift has been encapsulated for you. The IDL used here is also in backtype. storm. because clojure is based on JVM, IDL only needs to be converted to Java.

(defn launch-server! [conf nimbus]  (validate-distributed-mode! conf)  (let [service-handler (service-handler conf nimbus)        options (-> (TNonblockingServerSocket. (int (conf NIMBUS-THRIFT-PORT)))                    (THsHaServer$Args.)                    (.workerThreads 64)                    (.protocolFactory (TBinaryProtocol$Factory.))                    (.processor (Nimbus$Processor. service-handler))                    )       server (THsHaServer. options)]    (.addShutdownHook (Runtime/getRuntime) (Thread. (fn [] (.shutdown service-handler) (.stop server))))    (log-message "Starting Nimbus server...")    (.serve server)))

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.