use of org.apache.thrift.server.TServer in project jstorm by alibaba.
the class AMServer method simple.
public void simple(JstormAM.Processor processor) {
try {
TServerTransport serverTransport = new TServerSocket(port);
TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
LOG.info("Starting the simple server...");
server.serve();
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.apache.thrift.server.TServer in project dubbo by alibaba.
the class ThriftProtocol method exportThreadedSelectorServer.
private <T> Runnable exportThreadedSelectorServer(T impl, Class<T> type, URL url) throws RpcException {
final TServer thriftServer = getTServer(impl, type, url);
if (thriftServer == null) {
return null;
}
SERVER_MAP.put(url.getAddress(), thriftServer);
new Thread(() -> {
logger.info("Start Thrift ThreadedSelectorServer");
thriftServer.serve();
logger.info("Thrift ThreadedSelectorServer started.");
}).start();
return () -> {
try {
logger.info("Close Thrift NonblockingServer");
thriftServer.stop();
} catch (Throwable e) {
logger.warn(e.getMessage(), e);
}
};
}
use of org.apache.thrift.server.TServer in project dubbo by alibaba.
the class ThriftProtocol method getTServer.
private <T> TServer getTServer(T impl, Class<T> type, URL url) {
TThreadedSelectorServer.Args tArgs = null;
String typeName = type.getName();
TServer tserver;
if (typeName.endsWith(THRIFT_IFACE)) {
String processorClsName = typeName.substring(0, typeName.indexOf(THRIFT_IFACE)) + THRIFT_PROCESSOR;
try {
Class<?> clazz = Class.forName(processorClsName);
Constructor constructor = clazz.getConstructor(type);
TProcessor tprocessor = (TProcessor) constructor.newInstance(impl);
processor.registerProcessor(typeName, tprocessor);
tserver = SERVER_MAP.get(url.getAddress());
if (tserver == null) {
/**
*Solve the problem of only 50 of the default number of concurrent connections
*/
TNonblockingServerSocket.NonblockingAbstractServerSocketArgs args = new TNonblockingServerSocket.NonblockingAbstractServerSocketArgs();
/**
*1000 connections
*/
args.backlog(1000);
String bindIp = url.getParameter(Constants.BIND_IP_KEY, url.getHost());
if (url.getParameter(ANYHOST_KEY, false)) {
bindIp = ANYHOST_VALUE;
}
int bindPort = url.getParameter(Constants.BIND_PORT_KEY, url.getPort());
args.bindAddr(new InetSocketAddress(bindIp, bindPort));
/**
*timeout: 10s
*/
args.clientTimeout(10000);
TNonblockingServerSocket transport = new TNonblockingServerSocket(args);
tArgs = new TThreadedSelectorServer.Args(transport);
tArgs.workerThreads(200);
tArgs.selectorThreads(4);
tArgs.acceptQueueSizePerThread(256);
tArgs.processor(processor);
tArgs.transportFactory(new TFramedTransport.Factory());
tArgs.protocolFactory(new TCompactProtocol.Factory());
} else {
// if server is starting, return and do nothing here
return null;
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new RpcException("Fail to create nativethrift server(" + url + ") : " + e.getMessage(), e);
}
}
if (tArgs == null) {
logger.error("Fail to create native thrift server(" + url + ") due to null args");
throw new RpcException("Fail to create nativethrift server(" + url + ") due to null args");
}
tserver = new TThreadedSelectorServer(tArgs);
return tserver;
}
use of org.apache.thrift.server.TServer in project hive by apache.
the class HiveMetaStore method startMetaStore.
/**
* Start Metastore based on a passed {@link HadoopThriftAuthBridge}.
*
* @param port The port on which the Thrift server will start to serve
* @param bridge
* @param conf Configuration overrides
* @param startMetaStoreThreads Start the background threads (initiator, cleaner, statsupdater, etc.)
* @param startedBackgroundThreads If startMetaStoreThreads is true, this AtomicBoolean will be switched to true,
* when all of the background threads are scheduled. Useful for testing purposes to wait
* until the MetaStore is fully initialized.
* @throws Throwable
*/
public static void startMetaStore(int port, HadoopThriftAuthBridge bridge, Configuration conf, boolean startMetaStoreThreads, AtomicBoolean startedBackgroundThreads) throws Throwable {
isMetaStoreRemote = true;
// Server will create new threads up to max as necessary. After an idle
// period, it will destroy threads to keep the number of threads in the
// pool to min.
long maxMessageSize = MetastoreConf.getLongVar(conf, ConfVars.SERVER_MAX_MESSAGE_SIZE);
int minWorkerThreads = MetastoreConf.getIntVar(conf, ConfVars.SERVER_MIN_THREADS);
int maxWorkerThreads = MetastoreConf.getIntVar(conf, ConfVars.SERVER_MAX_THREADS);
boolean tcpKeepAlive = MetastoreConf.getBoolVar(conf, ConfVars.TCP_KEEP_ALIVE);
boolean useCompactProtocol = MetastoreConf.getBoolVar(conf, ConfVars.USE_THRIFT_COMPACT_PROTOCOL);
boolean useSSL = MetastoreConf.getBoolVar(conf, ConfVars.USE_SSL);
HMSHandler baseHandler = new HMSHandler("new db based metaserver", conf, false);
AuthFactory authFactory = new AuthFactory(bridge, conf, baseHandler);
useSasl = authFactory.isSASLWithKerberizedHadoop();
if (useSasl) {
// we are in secure mode. Login using keytab
String kerberosName = SecurityUtil.getServerPrincipal(MetastoreConf.getVar(conf, ConfVars.KERBEROS_PRINCIPAL), "0.0.0.0");
String keyTabFile = MetastoreConf.getVar(conf, ConfVars.KERBEROS_KEYTAB_FILE);
UserGroupInformation.loginUserFromKeytab(kerberosName, keyTabFile);
saslServer = authFactory.getSaslServer();
delegationTokenManager = authFactory.getDelegationTokenManager();
}
TProcessor processor;
TTransportFactory transFactory = authFactory.getAuthTransFactory(useSSL, conf);
final TProtocolFactory protocolFactory;
final TProtocolFactory inputProtoFactory;
if (useCompactProtocol) {
protocolFactory = new TCompactProtocol.Factory();
inputProtoFactory = new TCompactProtocol.Factory(maxMessageSize, maxMessageSize);
} else {
protocolFactory = new TBinaryProtocol.Factory();
inputProtoFactory = new TBinaryProtocol.Factory(true, true, maxMessageSize, maxMessageSize);
}
IHMSHandler handler = newRetryingHMSHandler(baseHandler, conf);
TServerSocket serverSocket;
if (useSasl) {
processor = saslServer.wrapProcessor(new ThriftHiveMetastore.Processor<>(handler));
LOG.info("Starting DB backed MetaStore Server in Secure Mode");
} else {
// we are in unsecure mode.
if (MetastoreConf.getBoolVar(conf, ConfVars.EXECUTE_SET_UGI)) {
processor = new TUGIBasedProcessor<>(handler);
LOG.info("Starting DB backed MetaStore Server with SetUGI enabled");
} else {
processor = new TSetIpAddressProcessor<>(handler);
LOG.info("Starting DB backed MetaStore Server");
}
}
msHost = MetastoreConf.getVar(conf, ConfVars.THRIFT_BIND_HOST);
if (msHost != null && !msHost.trim().isEmpty()) {
LOG.info("Binding host " + msHost + " for metastore server");
}
if (!useSSL) {
serverSocket = SecurityUtils.getServerSocket(msHost, port);
} else {
String keyStorePath = MetastoreConf.getVar(conf, ConfVars.SSL_KEYSTORE_PATH).trim();
if (keyStorePath.isEmpty()) {
throw new IllegalArgumentException(ConfVars.SSL_KEYSTORE_PATH.toString() + " Not configured for SSL connection");
}
String keyStorePassword = MetastoreConf.getPassword(conf, MetastoreConf.ConfVars.SSL_KEYSTORE_PASSWORD);
String keyStoreType = MetastoreConf.getVar(conf, ConfVars.SSL_KEYSTORE_TYPE).trim();
String keyStoreAlgorithm = MetastoreConf.getVar(conf, ConfVars.SSL_KEYMANAGERFACTORY_ALGORITHM).trim();
// enable SSL support for HMS
List<String> sslVersionBlacklist = new ArrayList<>();
for (String sslVersion : MetastoreConf.getVar(conf, ConfVars.SSL_PROTOCOL_BLACKLIST).split(",")) {
sslVersionBlacklist.add(sslVersion);
}
serverSocket = SecurityUtils.getServerSSLSocket(msHost, port, keyStorePath, keyStorePassword, keyStoreType, keyStoreAlgorithm, sslVersionBlacklist);
}
if (tcpKeepAlive) {
serverSocket = new TServerSocketKeepAlive(serverSocket);
}
TThreadPoolServer.Args args = new TThreadPoolServer.Args(serverSocket).processor(processor).transportFactory(transFactory).protocolFactory(protocolFactory).inputProtocolFactory(inputProtoFactory).minWorkerThreads(minWorkerThreads).maxWorkerThreads(maxWorkerThreads);
TServer tServer = new TThreadPoolServer(args);
TServerEventHandler tServerEventHandler = new TServerEventHandler() {
@Override
public void preServe() {
}
@Override
public ServerContext createContext(TProtocol tProtocol, TProtocol tProtocol1) {
Metrics.getOpenConnectionsCounter().inc();
return null;
}
@Override
public void deleteContext(ServerContext serverContext, TProtocol tProtocol, TProtocol tProtocol1) {
Metrics.getOpenConnectionsCounter().dec();
// If the IMetaStoreClient#close was called, HMSHandler#shutdown would have already
// cleaned up thread local RawStore. Otherwise, do it now.
HMSHandler.cleanupHandlerContext();
}
@Override
public void processContext(ServerContext serverContext, TTransport tTransport, TTransport tTransport1) {
}
};
tServer.setServerEventHandler(tServerEventHandler);
LOG.info("Started the new metaserver on port [" + port + "]...");
LOG.info("Options.minWorkerThreads = " + minWorkerThreads);
LOG.info("Options.maxWorkerThreads = " + maxWorkerThreads);
LOG.info("TCP keepalive = " + tcpKeepAlive);
LOG.info("Enable SSL = " + useSSL);
logCompactionParameters(conf);
boolean directSqlEnabled = MetastoreConf.getBoolVar(conf, ConfVars.TRY_DIRECT_SQL);
LOG.info("Direct SQL optimization = {}", directSqlEnabled);
if (startMetaStoreThreads) {
Lock metaStoreThreadsLock = new ReentrantLock();
Condition startCondition = metaStoreThreadsLock.newCondition();
AtomicBoolean startedServing = new AtomicBoolean();
startMetaStoreThreads(conf, metaStoreThreadsLock, startCondition, startedServing, isMetastoreHousekeepingLeader(conf, getServerHostName()), startedBackgroundThreads);
signalOtherThreadsToStart(tServer, metaStoreThreadsLock, startCondition, startedServing);
}
// If dynamic service discovery through ZooKeeper is enabled, add this server to the ZooKeeper.
if (MetastoreConf.getVar(conf, ConfVars.THRIFT_SERVICE_DISCOVERY_MODE).equalsIgnoreCase("zookeeper")) {
try {
zooKeeperHelper = MetastoreConf.getZKConfig(conf);
String serverInstanceURI = getServerInstanceURI(port);
zooKeeperHelper.addServerInstanceToZooKeeper(serverInstanceURI, serverInstanceURI, null, new ZKDeRegisterWatcher(zooKeeperHelper));
LOG.info("Metastore server instance with URL " + serverInstanceURI + " added to " + "the zookeeper");
} catch (Exception e) {
LOG.error("Error adding this metastore instance to ZooKeeper: ", e);
throw e;
}
}
tServer.serve();
}
use of org.apache.thrift.server.TServer in project hive by apache.
the class HiveMetaStore method startMetaStore.
/**
* Start Metastore based on a passed {@link HadoopThriftAuthBridge}
*
* @param port
* @param bridge
* @param conf
* configuration overrides
* @throws Throwable
*/
public static void startMetaStore(int port, HadoopThriftAuthBridge bridge, HiveConf conf, Lock startLock, Condition startCondition, AtomicBoolean startedServing) throws Throwable {
try {
isMetaStoreRemote = true;
// Server will create new threads up to max as necessary. After an idle
// period, it will destroy threads to keep the number of threads in the
// pool to min.
long maxMessageSize = conf.getLongVar(HiveConf.ConfVars.METASTORESERVERMAXMESSAGESIZE);
int minWorkerThreads = conf.getIntVar(HiveConf.ConfVars.METASTORESERVERMINTHREADS);
int maxWorkerThreads = conf.getIntVar(HiveConf.ConfVars.METASTORESERVERMAXTHREADS);
boolean tcpKeepAlive = conf.getBoolVar(HiveConf.ConfVars.METASTORE_TCP_KEEP_ALIVE);
boolean useFramedTransport = conf.getBoolVar(ConfVars.METASTORE_USE_THRIFT_FRAMED_TRANSPORT);
boolean useCompactProtocol = conf.getBoolVar(ConfVars.METASTORE_USE_THRIFT_COMPACT_PROTOCOL);
boolean useSSL = conf.getBoolVar(ConfVars.HIVE_METASTORE_USE_SSL);
useSasl = conf.getBoolVar(HiveConf.ConfVars.METASTORE_USE_THRIFT_SASL);
TProcessor processor;
TTransportFactory transFactory;
final TProtocolFactory protocolFactory;
final TProtocolFactory inputProtoFactory;
if (useCompactProtocol) {
protocolFactory = new TCompactProtocol.Factory();
inputProtoFactory = new TCompactProtocol.Factory(maxMessageSize, maxMessageSize);
} else {
protocolFactory = new TBinaryProtocol.Factory();
inputProtoFactory = new TBinaryProtocol.Factory(true, true, maxMessageSize, maxMessageSize);
}
HMSHandler baseHandler = new HiveMetaStore.HMSHandler("new db based metaserver", conf, false);
IHMSHandler handler = newRetryingHMSHandler(baseHandler, conf);
TServerSocket serverSocket = null;
if (useSasl) {
// we are in secure mode.
if (useFramedTransport) {
throw new HiveMetaException("Framed transport is not supported with SASL enabled.");
}
saslServer = bridge.createServer(conf.getVar(HiveConf.ConfVars.METASTORE_KERBEROS_KEYTAB_FILE), conf.getVar(HiveConf.ConfVars.METASTORE_KERBEROS_PRINCIPAL));
// Start delegation token manager
delegationTokenManager = new HiveDelegationTokenManager();
delegationTokenManager.startDelegationTokenSecretManager(conf, baseHandler, ServerMode.METASTORE);
saslServer.setSecretManager(delegationTokenManager.getSecretManager());
transFactory = saslServer.createTransportFactory(MetaStoreUtils.getMetaStoreSaslProperties(conf));
processor = saslServer.wrapProcessor(new ThriftHiveMetastore.Processor<IHMSHandler>(handler));
serverSocket = HiveAuthUtils.getServerSocket(null, port);
LOG.info("Starting DB backed MetaStore Server in Secure Mode");
} else {
// we are in unsecure mode.
if (conf.getBoolVar(ConfVars.METASTORE_EXECUTE_SET_UGI)) {
transFactory = useFramedTransport ? new ChainedTTransportFactory(new TFramedTransport.Factory(), new TUGIContainingTransport.Factory()) : new TUGIContainingTransport.Factory();
processor = new TUGIBasedProcessor<IHMSHandler>(handler);
LOG.info("Starting DB backed MetaStore Server with SetUGI enabled");
} else {
transFactory = useFramedTransport ? new TFramedTransport.Factory() : new TTransportFactory();
processor = new TSetIpAddressProcessor<IHMSHandler>(handler);
LOG.info("Starting DB backed MetaStore Server");
}
// enable SSL support for HMS
List<String> sslVersionBlacklist = new ArrayList<String>();
for (String sslVersion : conf.getVar(ConfVars.HIVE_SSL_PROTOCOL_BLACKLIST).split(",")) {
sslVersionBlacklist.add(sslVersion);
}
if (!useSSL) {
serverSocket = HiveAuthUtils.getServerSocket(null, port);
} else {
String keyStorePath = conf.getVar(ConfVars.HIVE_METASTORE_SSL_KEYSTORE_PATH).trim();
if (keyStorePath.isEmpty()) {
throw new IllegalArgumentException(ConfVars.HIVE_METASTORE_SSL_KEYSTORE_PASSWORD.varname + " Not configured for SSL connection");
}
String keyStorePassword = ShimLoader.getHadoopShims().getPassword(conf, HiveConf.ConfVars.HIVE_METASTORE_SSL_KEYSTORE_PASSWORD.varname);
serverSocket = HiveAuthUtils.getServerSSLSocket(null, port, keyStorePath, keyStorePassword, sslVersionBlacklist);
}
}
if (tcpKeepAlive) {
serverSocket = new TServerSocketKeepAlive(serverSocket);
}
TThreadPoolServer.Args args = new TThreadPoolServer.Args(serverSocket).processor(processor).transportFactory(transFactory).protocolFactory(protocolFactory).inputProtocolFactory(inputProtoFactory).minWorkerThreads(minWorkerThreads).maxWorkerThreads(maxWorkerThreads);
TServer tServer = new TThreadPoolServer(args);
TServerEventHandler tServerEventHandler = new TServerEventHandler() {
@Override
public void preServe() {
}
@Override
public ServerContext createContext(TProtocol tProtocol, TProtocol tProtocol1) {
try {
Metrics metrics = MetricsFactory.getInstance();
if (metrics != null) {
metrics.incrementCounter(MetricsConstant.OPEN_CONNECTIONS);
}
} catch (Exception e) {
LOG.warn("Error Reporting Metastore open connection to Metrics system", e);
}
return null;
}
@Override
public void deleteContext(ServerContext serverContext, TProtocol tProtocol, TProtocol tProtocol1) {
try {
Metrics metrics = MetricsFactory.getInstance();
if (metrics != null) {
metrics.decrementCounter(MetricsConstant.OPEN_CONNECTIONS);
}
} catch (Exception e) {
LOG.warn("Error Reporting Metastore close connection to Metrics system", e);
}
// If the IMetaStoreClient#close was called, HMSHandler#shutdown would have already
// cleaned up thread local RawStore. Otherwise, do it now.
cleanupRawStore();
}
@Override
public void processContext(ServerContext serverContext, TTransport tTransport, TTransport tTransport1) {
}
};
tServer.setServerEventHandler(tServerEventHandler);
HMSHandler.LOG.info("Started the new metaserver on port [" + port + "]...");
HMSHandler.LOG.info("Options.minWorkerThreads = " + minWorkerThreads);
HMSHandler.LOG.info("Options.maxWorkerThreads = " + maxWorkerThreads);
HMSHandler.LOG.info("TCP keepalive = " + tcpKeepAlive);
if (startLock != null) {
signalOtherThreadsToStart(tServer, startLock, startCondition, startedServing);
}
tServer.serve();
} catch (Throwable x) {
x.printStackTrace();
HMSHandler.LOG.error(StringUtils.stringifyException(x));
throw x;
}
}
Aggregations