C and C++ to achieve the STORM running information view and control methods

  • 2020-04-02 02:28:46
  • OfStack

The program example described in this article mainly realizes the real-time acquisition of the STORM cluster operation information and the topology-related submission and control on the back-end application server. For this, through the STORM UI and CMD source analysis, we can achieve these functions through its thrift interface call. Download a thrift library to code and install. See this place for thrift. Once installed, copy STORM. Thrift from the STORM source to the thrift directory. Input:


hrift -gen cpp storm.thrift

You get a gen- CPP directory, which is the C++ implementation of the thrift first close script. Let's first look at storm. Thrift file interface:


service Nimbus 
{
 //TOPOLOGY upload interface
 void submitTopology(1: string name, 2: string uploadedJarLocation, 3: string jsonConf, 4: StormTopology topology);
 void submitTopologyWithOpts(1: string name, 2: string uploadedJarLocation, 3: string jsonConf, 4: StormTopology topology, 5: SubmitOptions options);
 void killTopology(1: string name);
 void killTopologyWithOpts(1: string name, 2: KillOptions options) throws (1: NotAliveException e);
 void activate(1: string name) ;
 void deactivate(1: string name);
 void rebalance(1: string name, 2: RebalanceOptions options);
 
 //The TOPOLOGY JAR package uploader interface
 string beginFileUpload();
 void uploadChunk(1: string location, 2: binary chunk);
 void finishFileUpload(1: string location);
 string beginFileDownload(1: string file);
 binary downloadChunk(1: string id);

 //Get the configuration information for NIMBUS
 string getNimbusConf();
 //Get the STORM cluster run information
 ClusterSummary getClusterInfo();
 //Gets the runtime status information for the TOPOLOGY
 TopologyInfo getTopologyInfo(1: string id);
 //Gets the TOPOLOGY object information
 string getTopologyConf(1: string id);
 StormTopology getTopology(1: string id);
 StormTopology getUserTopology(1: string id);
}

Once the C++ file is generated, we can call its interface, and since the thrift C++ framework is implemented using the boost library, we must install the boost library dependency. The implementation code is as follows:


#define HAVE_NETDB_H //Macros that use network modules must be opened
#include "Nimbus.h"
#include "storm_types.h"

#include <string>
#include <iostream>
#include <set>

#include <transport/TSocket.h> 
#include <transport/TBufferTransports.h> 
#include <protocol/TBinaryProtocol.h> 
int test_storm_thrift()
{
 boost::shared_ptr<TSocket> tsocket(new TSocket("storm-nimbus-server", 6627));
 boost::shared_ptr<TTransport> ttransport(new TFramedTransport(tsocket, 1024 * 512)); //You must use TFramedTransport here
 boost::shared_ptr<TProtocol> tprotocol(new TBinaryProtocol(ttransport));
 try{
 //Create a nimbus client object
 NimbusClient client(tprotocol);
 //Open the channel
  ttransport->open();

 ClusterSummary summ;
 std::string conf;
 //RPC call to STORM, get information directly, synchronously.
 client.getNimbusConf(conf);
 client.getClusterInfo(summ);
 //Close the channel
 ttransport->close(); 
  }catch(TException &tx){ 
 printf("InvalidOperation: %s
", tx.what()); 
  }
}

The above code can directly obtain nimbus configuration and cluster information, and so on. It is worth noting that storm. Thrift to C++ generates the storm_types.h file with the operator < None of the functions are implemented, so you have to add the implementation manually or you'll have problems compiling.

In addition, STORM control can be implemented not only in C++, but also in PHP and other languages, as long as thrift supports it. Interested readers can try it out on their own.


Related articles: