Search in sources :

Example 1 with TServerTransport

use of org.apache.thrift.transport.TServerTransport in project hbase by apache.

the class ThriftServerRunner method setupServer.

/**
   * Setting up the thrift TServer
   */
private void setupServer() throws Exception {
    // Construct correct ProtocolFactory
    TProtocolFactory protocolFactory;
    if (conf.getBoolean(COMPACT_CONF_KEY, false)) {
        LOG.debug("Using compact protocol");
        protocolFactory = new TCompactProtocol.Factory();
    } else {
        LOG.debug("Using binary protocol");
        protocolFactory = new TBinaryProtocol.Factory();
    }
    final TProcessor p = new Hbase.Processor<>(handler);
    ImplType implType = ImplType.getServerImpl(conf);
    TProcessor processor = p;
    // Construct correct TransportFactory
    TTransportFactory transportFactory;
    if (conf.getBoolean(FRAMED_CONF_KEY, false) || implType.isAlwaysFramed) {
        if (qop != null) {
            throw new RuntimeException("Thrift server authentication" + " doesn't work with framed transport yet");
        }
        transportFactory = new TFramedTransport.Factory(conf.getInt(MAX_FRAME_SIZE_CONF_KEY, 2) * 1024 * 1024);
        LOG.debug("Using framed transport");
    } else if (qop == null) {
        transportFactory = new TTransportFactory();
    } else {
        // Extract the name from the principal
        String name = SecurityUtil.getUserFromPrincipal(conf.get("hbase.thrift.kerberos.principal"));
        Map<String, String> saslProperties = new HashMap<>();
        saslProperties.put(Sasl.QOP, qop);
        TSaslServerTransport.Factory saslFactory = new TSaslServerTransport.Factory();
        saslFactory.addServerDefinition("GSSAPI", name, host, saslProperties, new SaslGssCallbackHandler() {

            @Override
            public void handle(Callback[] callbacks) throws UnsupportedCallbackException {
                AuthorizeCallback ac = null;
                for (Callback callback : callbacks) {
                    if (callback instanceof AuthorizeCallback) {
                        ac = (AuthorizeCallback) callback;
                    } else {
                        throw new UnsupportedCallbackException(callback, "Unrecognized SASL GSSAPI Callback");
                    }
                }
                if (ac != null) {
                    String authid = ac.getAuthenticationID();
                    String authzid = ac.getAuthorizationID();
                    if (!authid.equals(authzid)) {
                        ac.setAuthorized(false);
                    } else {
                        ac.setAuthorized(true);
                        String userName = SecurityUtil.getUserFromPrincipal(authzid);
                        LOG.info("Effective user: " + userName);
                        ac.setAuthorizedID(userName);
                    }
                }
            }
        });
        transportFactory = saslFactory;
        // Create a processor wrapper, to get the caller
        processor = new TProcessor() {

            @Override
            public boolean process(TProtocol inProt, TProtocol outProt) throws TException {
                TSaslServerTransport saslServerTransport = (TSaslServerTransport) inProt.getTransport();
                SaslServer saslServer = saslServerTransport.getSaslServer();
                String principal = saslServer.getAuthorizationID();
                hbaseHandler.setEffectiveUser(principal);
                return p.process(inProt, outProt);
            }
        };
    }
    if (conf.get(BIND_CONF_KEY) != null && !implType.canSpecifyBindIP) {
        LOG.error("Server types " + Joiner.on(", ").join(ImplType.serversThatCannotSpecifyBindIP()) + " don't support IP " + "address binding at the moment. See " + "https://issues.apache.org/jira/browse/HBASE-2155 for details.");
        throw new RuntimeException("-" + BIND_CONF_KEY + " not supported with " + implType);
    }
    // Thrift's implementation uses '0' as a placeholder for 'use the default.'
    int backlog = conf.getInt(BACKLOG_CONF_KEY, 0);
    if (implType == ImplType.HS_HA || implType == ImplType.NONBLOCKING || implType == ImplType.THREADED_SELECTOR) {
        InetAddress listenAddress = getBindAddress(conf);
        TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(new InetSocketAddress(listenAddress, listenPort));
        if (implType == ImplType.NONBLOCKING) {
            TNonblockingServer.Args serverArgs = new TNonblockingServer.Args(serverTransport);
            serverArgs.processor(processor).transportFactory(transportFactory).protocolFactory(protocolFactory);
            tserver = new TNonblockingServer(serverArgs);
        } else if (implType == ImplType.HS_HA) {
            THsHaServer.Args serverArgs = new THsHaServer.Args(serverTransport);
            CallQueue callQueue = new CallQueue(new LinkedBlockingQueue<>(), metrics);
            ExecutorService executorService = createExecutor(callQueue, serverArgs.getMaxWorkerThreads(), serverArgs.getMaxWorkerThreads());
            serverArgs.executorService(executorService).processor(processor).transportFactory(transportFactory).protocolFactory(protocolFactory);
            tserver = new THsHaServer(serverArgs);
        } else {
            // THREADED_SELECTOR
            TThreadedSelectorServer.Args serverArgs = new HThreadedSelectorServerArgs(serverTransport, conf);
            CallQueue callQueue = new CallQueue(new LinkedBlockingQueue<>(), metrics);
            ExecutorService executorService = createExecutor(callQueue, serverArgs.getWorkerThreads(), serverArgs.getWorkerThreads());
            serverArgs.executorService(executorService).processor(processor).transportFactory(transportFactory).protocolFactory(protocolFactory);
            tserver = new TThreadedSelectorServer(serverArgs);
        }
        LOG.info("starting HBase " + implType.simpleClassName() + " server on " + Integer.toString(listenPort));
    } else if (implType == ImplType.THREAD_POOL) {
        // Thread pool server. Get the IP address to bind to.
        InetAddress listenAddress = getBindAddress(conf);
        int readTimeout = conf.getInt(THRIFT_SERVER_SOCKET_READ_TIMEOUT_KEY, THRIFT_SERVER_SOCKET_READ_TIMEOUT_DEFAULT);
        TServerTransport serverTransport = new TServerSocket(new TServerSocket.ServerSocketTransportArgs().bindAddr(new InetSocketAddress(listenAddress, listenPort)).backlog(backlog).clientTimeout(readTimeout));
        TBoundedThreadPoolServer.Args serverArgs = new TBoundedThreadPoolServer.Args(serverTransport, conf);
        serverArgs.processor(processor).transportFactory(transportFactory).protocolFactory(protocolFactory);
        LOG.info("starting " + ImplType.THREAD_POOL.simpleClassName() + " on " + listenAddress + ":" + Integer.toString(listenPort) + " with readTimeout " + readTimeout + "ms; " + serverArgs);
        TBoundedThreadPoolServer tserver = new TBoundedThreadPoolServer(serverArgs, metrics);
        this.tserver = tserver;
    } else {
        throw new AssertionError("Unsupported Thrift server implementation: " + implType.simpleClassName());
    }
    // A sanity check that we instantiated the right type of server.
    if (tserver.getClass() != implType.serverClass) {
        throw new AssertionError("Expected to create Thrift server class " + implType.serverClass.getName() + " but got " + tserver.getClass().getName());
    }
    registerFilters(conf);
}
Also used : TProtocolFactory(org.apache.thrift.protocol.TProtocolFactory) TNonblockingServerTransport(org.apache.thrift.transport.TNonblockingServerTransport) TProcessor(org.apache.thrift.TProcessor) SaslServer(javax.security.sasl.SaslServer) InetSocketAddress(java.net.InetSocketAddress) TThreadedSelectorServer(org.apache.thrift.server.TThreadedSelectorServer) LogFactory(org.apache.commons.logging.LogFactory) TProtocolFactory(org.apache.thrift.protocol.TProtocolFactory) TTransportFactory(org.apache.thrift.transport.TTransportFactory) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) TCompactProtocol(org.apache.thrift.protocol.TCompactProtocol) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) AuthorizeCallback(javax.security.sasl.AuthorizeCallback) TServerSocket(org.apache.thrift.transport.TServerSocket) THsHaServer(org.apache.thrift.server.THsHaServer) TProcessor(org.apache.thrift.TProcessor) TProtocol(org.apache.thrift.protocol.TProtocol) TFramedTransport(org.apache.thrift.transport.TFramedTransport) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) TTransportFactory(org.apache.thrift.transport.TTransportFactory) TNonblockingServer(org.apache.thrift.server.TNonblockingServer) TServerTransport(org.apache.thrift.transport.TServerTransport) TSaslServerTransport(org.apache.thrift.transport.TSaslServerTransport) SaslGssCallbackHandler(org.apache.hadoop.security.SaslRpcServer.SaslGssCallbackHandler) Callback(javax.security.auth.callback.Callback) AuthorizeCallback(javax.security.sasl.AuthorizeCallback) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TNonblockingServerSocket(org.apache.thrift.transport.TNonblockingServerSocket) ExecutorService(java.util.concurrent.ExecutorService) Map(java.util.Map) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) InetAddress(java.net.InetAddress)

Example 2 with TServerTransport

use of org.apache.thrift.transport.TServerTransport in project hbase by apache.

the class ThriftServer method getTThreadPoolServer.

private static TServer getTThreadPoolServer(TProtocolFactory protocolFactory, TProcessor processor, TTransportFactory transportFactory, int workerThreads, InetSocketAddress inetSocketAddress, int backlog, int clientTimeout, ThriftMetrics metrics) throws TTransportException {
    TServerTransport serverTransport = new TServerSocket(new TServerSocket.ServerSocketTransportArgs().bindAddr(inetSocketAddress).backlog(backlog).clientTimeout(clientTimeout));
    log.info("starting HBase ThreadPool Thrift server on " + inetSocketAddress.toString());
    TThreadPoolServer.Args serverArgs = new TThreadPoolServer.Args(serverTransport);
    serverArgs.processor(processor);
    serverArgs.transportFactory(transportFactory);
    serverArgs.protocolFactory(protocolFactory);
    if (workerThreads > 0) {
        serverArgs.maxWorkerThreads(workerThreads);
    }
    ThreadPoolExecutor executor = new THBaseThreadPoolExecutor(serverArgs.minWorkerThreads, serverArgs.maxWorkerThreads, serverArgs.stopTimeoutVal, TimeUnit.SECONDS, new SynchronousQueue<>(), metrics);
    serverArgs.executorService(executor);
    return new TThreadPoolServer(serverArgs);
}
Also used : TServerSocket(org.apache.thrift.transport.TServerSocket) THBaseThreadPoolExecutor(org.apache.hadoop.hbase.thrift.THBaseThreadPoolExecutor) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) TThreadPoolServer(org.apache.thrift.server.TThreadPoolServer) TServerTransport(org.apache.thrift.transport.TServerTransport) THBaseThreadPoolExecutor(org.apache.hadoop.hbase.thrift.THBaseThreadPoolExecutor)

Example 3 with TServerTransport

use of org.apache.thrift.transport.TServerTransport in project dubbo by alibaba.

the class ServiceMethodNotFoundTest method init.

protected void init() throws Exception {
    TServerTransport serverTransport = new TServerSocket(PORT);
    DubboDemoImpl impl = new DubboDemoImpl();
    $__DemoStub.Processor processor = new $__DemoStub.Processor(impl);
    // for test
    Field field = processor.getClass().getSuperclass().getDeclaredField("processMap");
    field.setAccessible(true);
    Object obj = field.get(processor);
    if (obj instanceof Map) {
        ((Map) obj).remove("echoString");
    }
    // ~
    TBinaryProtocol.Factory bFactory = new TBinaryProtocol.Factory();
    MultiServiceProcessor wrapper = new MultiServiceProcessor();
    wrapper.addProcessor(Demo.class, processor);
    server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).inputProtocolFactory(bFactory).outputProtocolFactory(bFactory).inputTransportFactory(getTransportFactory()).outputTransportFactory(getTransportFactory()).processor(wrapper));
    Thread startTread = new Thread() {

        @Override
        public void run() {
            server.serve();
        }
    };
    startTread.start();
    while (!server.isServing()) {
        Thread.sleep(100);
    }
}
Also used : MultiServiceProcessor(com.alibaba.dubbo.rpc.protocol.thrift.ext.MultiServiceProcessor) MultiServiceProcessor(com.alibaba.dubbo.rpc.protocol.thrift.ext.MultiServiceProcessor) TServerTransport(org.apache.thrift.transport.TServerTransport) com.alibaba.dubbo.rpc.gen.dubbo.$__DemoStub(com.alibaba.dubbo.rpc.gen.dubbo.$__DemoStub) TServerSocket(org.apache.thrift.transport.TServerSocket) Field(java.lang.reflect.Field) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TThreadPoolServer(org.apache.thrift.server.TThreadPoolServer) Map(java.util.Map)

Example 4 with TServerTransport

use of org.apache.thrift.transport.TServerTransport in project airavata by apache.

the class AiravataAPIServer method startAiravataServer.

public void startAiravataServer(Airavata.Processor<Airavata.Iface> airavataAPIServer) throws AiravataSystemException {
    try {
        final String serverHost = ServerSettings.getSetting(Constants.API_SERVER_HOST, null);
        if (!ServerSettings.isTLSEnabled()) {
            final int serverPort = Integer.parseInt(ServerSettings.getSetting(Constants.API_SERVER_PORT, "8930"));
            TServerTransport serverTransport;
            if (ServerSettings.isAPIServerTLSEnabled()) {
                logger.info("Starting API Server with TLS Security..");
                String keystore = ServerSettings.getApiServerKeystore();
                String keystorePWD = ServerSettings.getApiServerKeystorePasswd();
                TSSLTransportFactory.TSSLTransportParameters tlsParams = new TSSLTransportFactory.TSSLTransportParameters();
                tlsParams.setKeyStore(keystore, keystorePWD);
                serverTransport = TSSLTransportFactory.getServerSocket(serverPort, 10000, InetAddress.getByName(serverHost), tlsParams);
            } else {
                if (serverHost == null) {
                    serverTransport = new TServerSocket(serverPort);
                } else {
                    InetSocketAddress inetSocketAddress = new InetSocketAddress(serverHost, serverPort);
                    serverTransport = new TServerSocket(inetSocketAddress);
                }
            }
            TThreadPoolServer.Args options = new TThreadPoolServer.Args(serverTransport);
            options.minWorkerThreads = Integer.parseInt(ServerSettings.getSetting(Constants.API_SERVER_MIN_THREADS, "50"));
            server = new TThreadPoolServer(options.processor(airavataAPIServer));
            new Thread() {

                public void run() {
                    server.serve();
                    setStatus(ServerStatus.STOPPED);
                    logger.info("Airavata API Server Stopped.");
                }
            }.start();
            new Thread() {

                public void run() {
                    while (!server.isServing()) {
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            break;
                        }
                    }
                    if (server.isServing()) {
                        setStatus(ServerStatus.STARTED);
                        logger.info("Starting Airavata API Server on Port " + serverPort);
                        logger.info("Listening to Airavata Clients ....");
                    }
                }
            }.start();
            logger.info("Started API Server ....");
        } else {
            /**
             ********start thrift server over TLS*****************
             */
            TSSLTransportFactory.TSSLTransportParameters TLSParams = new TSSLTransportFactory.TSSLTransportParameters();
            TLSParams.setKeyStore(ServerSettings.getKeyStorePath(), ServerSettings.getKeyStorePassword());
            TServerSocket TLSServerTransport = TSSLTransportFactory.getServerSocket(ServerSettings.getTLSServerPort(), ServerSettings.getTLSClientTimeout(), InetAddress.getByName(serverHost), TLSParams);
            TThreadPoolServer.Args settings = new TThreadPoolServer.Args(TLSServerTransport);
            settings.minWorkerThreads = Integer.parseInt(ServerSettings.getSetting(Constants.API_SERVER_MIN_THREADS, "50"));
            TLSServer = new TThreadPoolServer(settings.processor(airavataAPIServer));
            new Thread() {

                public void run() {
                    TLSServer.serve();
                    setStatus(ServerStatus.STOPPED);
                    logger.info("Airavata API Server over TLS Stopped.");
                }
            }.start();
            new Thread() {

                public void run() {
                    while (!TLSServer.isServing()) {
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            break;
                        }
                    }
                    if (TLSServer.isServing()) {
                        setStatus(ServerStatus.STARTED);
                    }
                }
            }.start();
            logger.info("API server started over TLS on Port: " + ServerSettings.getTLSServerPort() + " ...");
        }
        /*perform any security related initialization at the server startup, according to the underlying security
             manager implementation being used.*/
        AiravataSecurityManager securityManager = SecurityManagerFactory.getSecurityManager();
        securityManager.initializeSecurityInfra();
    } catch (TTransportException e) {
        logger.error(e.getMessage(), e);
        setStatus(ServerStatus.FAILED);
        logger.error("Failed to start API server ...");
        throw new AiravataSystemException(AiravataErrorType.INTERNAL_ERROR);
    } catch (ApplicationSettingsException e) {
        logger.error(e.getMessage(), e);
        throw new AiravataSystemException(AiravataErrorType.INTERNAL_ERROR);
    } catch (UnknownHostException e) {
        logger.error(e.getMessage(), e);
        throw new AiravataSystemException(AiravataErrorType.INTERNAL_ERROR);
    } catch (AiravataSecurityException e) {
        logger.error(e.getMessage(), e);
        throw new AiravataSystemException(AiravataErrorType.INTERNAL_ERROR);
    }
}
Also used : ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) AiravataSecurityManager(org.apache.airavata.service.security.AiravataSecurityManager) TTransportException(org.apache.thrift.transport.TTransportException) TSSLTransportFactory(org.apache.thrift.transport.TSSLTransportFactory) TServerTransport(org.apache.thrift.transport.TServerTransport) TServerSocket(org.apache.thrift.transport.TServerSocket) AiravataSystemException(org.apache.airavata.model.error.AiravataSystemException) TThreadPoolServer(org.apache.thrift.server.TThreadPoolServer) AiravataSecurityException(org.apache.airavata.security.AiravataSecurityException)

Example 5 with TServerTransport

use of org.apache.thrift.transport.TServerTransport in project airavata by apache.

the class SharingRegistryServer method start.

@Override
public void start() throws Exception {
    try {
        setStatus(IServer.ServerStatus.STARTING);
        final int serverPort = Integer.parseInt(ServerSettings.getSetting(SHARING_REG_SERVER_PORT));
        final String serverHost = ServerSettings.getSetting(SHARING_REG_SERVER_HOST);
        SharingRegistryService.Processor processor = new SharingRegistryService.Processor(new SharingRegistryServerHandler());
        TServerTransport serverTransport;
        if (serverHost == null) {
            serverTransport = new TServerSocket(serverPort);
        } else {
            InetSocketAddress inetSocketAddress = new InetSocketAddress(serverHost, serverPort);
            serverTransport = new TServerSocket(inetSocketAddress);
        }
        TThreadPoolServer.Args options = new TThreadPoolServer.Args(serverTransport);
        options.minWorkerThreads = 30;
        server = new TThreadPoolServer(options.processor(processor));
        new Thread() {

            public void run() {
                server.serve();
                setStatus(IServer.ServerStatus.STOPPED);
                logger.info("Sharing Registry Server Stopped.");
            }
        }.start();
        new Thread() {

            public void run() {
                while (!server.isServing()) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        break;
                    }
                }
                if (server.isServing()) {
                    try {
                        logger.info("Register sharing service with DB Event publishers");
                        SharingServiceDBEventMessagingFactory.registerSharingServiceWithPublishers(Constants.PUBLISHERS);
                        logger.info("Start sharing service DB Event subscriber");
                        SharingServiceDBEventMessagingFactory.getDBEventSubscriber();
                    } catch (AiravataException | SharingRegistryException e) {
                        logger.error("Error starting sharing service. Error setting up DB event services.");
                        server.stop();
                    }
                    setStatus(IServer.ServerStatus.STARTED);
                    logger.info("Starting Sharing Registry Server on Port " + serverPort);
                    logger.info("Listening to Sharing Registry server clients ....");
                }
            }
        }.start();
    } catch (TTransportException e) {
        setStatus(IServer.ServerStatus.FAILED);
        throw new Exception("Error while starting the Sharing Registry service", e);
    }
}
Also used : SharingRegistryService(org.apache.airavata.sharing.registry.service.cpi.SharingRegistryService) InetSocketAddress(java.net.InetSocketAddress) TTransportException(org.apache.thrift.transport.TTransportException) TServerTransport(org.apache.thrift.transport.TServerTransport) SharingRegistryException(org.apache.airavata.sharing.registry.models.SharingRegistryException) TTransportException(org.apache.thrift.transport.TTransportException) AiravataException(org.apache.airavata.common.exception.AiravataException) TServerSocket(org.apache.thrift.transport.TServerSocket) TThreadPoolServer(org.apache.thrift.server.TThreadPoolServer)

Aggregations

TServerSocket (org.apache.thrift.transport.TServerSocket)20 TServerTransport (org.apache.thrift.transport.TServerTransport)20 TThreadPoolServer (org.apache.thrift.server.TThreadPoolServer)14 InetSocketAddress (java.net.InetSocketAddress)9 TServer (org.apache.thrift.server.TServer)8 TTransportException (org.apache.thrift.transport.TTransportException)8 Map (java.util.Map)3 TProcessor (org.apache.thrift.TProcessor)3 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)3 TSimpleServer (org.apache.thrift.server.TSimpleServer)3 Field (java.lang.reflect.Field)2 ExecutorService (java.util.concurrent.ExecutorService)2 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2 com.alibaba.dubbo.rpc.gen.dubbo.$__DemoStub (com.alibaba.dubbo.rpc.gen.dubbo.$__DemoStub)1 MultiServiceProcessor (com.alibaba.dubbo.rpc.protocol.thrift.ext.MultiServiceProcessor)1 CrossPlatformService (com.baeldung.thrift.impl.CrossPlatformService)1 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 Config (com.netflix.metacat.common.server.properties.Config)1 RegistryUtil (com.netflix.metacat.common.server.util.RegistryUtil)1 Registry (com.netflix.spectator.api.Registry)1