Example usage for org.apache.thrift.server TThreadPoolServer TThreadPoolServer

List of usage examples for org.apache.thrift.server TThreadPoolServer TThreadPoolServer

Introduction

In this page you can find the example usage for org.apache.thrift.server TThreadPoolServer TThreadPoolServer.

Prototype

public TThreadPoolServer(Args args) 

Source Link

Usage

From source file:org.corfudb.infrastructure.RocksLogUnitServer.java

License:Apache License

public void serverloop() throws Exception {

    log.warn("@C@ RocksDBLoggingUnit starting");

    if (!RAMMODE) {
        RocksDB.loadLibrary();//from   ww  w  . j a v a 2 s  .c  o m

        Options options = new Options().setCreateIfMissing(true);
        options.setAllowMmapReads(true);
        // For easy prefix-lookups.
        options.setMemTableConfig(new HashSkipListMemTableConfig());
        options.setTableFormatConfig(new PlainTableConfig());
        options.useFixedLengthPrefixExtractor(16); // Prefix length in bytes
        try {
            db = RocksDB.open(options, DRIVENAME);
        } catch (RocksDBException e) {
            e.printStackTrace();
            log.warn("couldn't open rocksdb, exception: {}", e);
            System.exit(1); // not much to do without storage...
        }
    } else {
        //TODO: Rammode in RocksDB?
    }

    if (RECOVERY) {
        recover();
    } else if (REBUILD) {
        CorfuDBSimpleLogUnitProtocol protocol = null;
        try {
            // Fix epoch later; but if we set it to -1, this guarantees that any write will trigger a view change,
            // which we want
            //TODO: Fix how CorfuDBSimpleLogUnitProtocol is essentially hardcoded?
            protocol = (CorfuDBSimpleLogUnitProtocol) IServerProtocol
                    .protocolFactory(CorfuDBSimpleLogUnitProtocol.class, rebuildnode, -1);
        } catch (Exception ex) {
            log.error("Error invoking protocol for protocol: ", ex);
            log.error("Cannot rebuild node");
            System.exit(1);
        }

        if (!rebuildFrom(protocol))
            System.exit(1);
    } else {
        initLogStore(UNITCAPACITY);
        //writegcmark();
    }
    ready.set(true);

    TServerSocket serverTransport;
    System.out.println("run..");

    try {
        serverTransport = new TServerSocket(PORT);

        //LogUnitConfigServiceImpl cnfg = new LogUnitConfigServiceImpl();

        TMultiplexedProcessor mprocessor = new TMultiplexedProcessor();
        mprocessor.registerProcessor("SUNIT", new RocksLogUnitService.Processor<RocksLogUnitServer>(this));
        //TODO: Figure out what the Config service is for a RocksDB indexed server?
        //mprocessor.registerProcessor("CONFIG", new SimpleLogUnitConfigService.Processor<LogUnitConfigServiceImpl>(cnfg));

        server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(mprocessor)
                .protocolFactory(TCompactProtocol::new)
                .inputTransportFactory(new TFastFramedTransport.Factory())
                .outputTransportFactory(new TFastFramedTransport.Factory()));
        System.out.println("Starting Corfu storage unit server on multiplexed port " + PORT);

        server.serve();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.corfudb.infrastructure.SimpleLogUnitServer.java

License:Apache License

public void serverloop() throws Exception {

    log.warn("@C@ CorfuLoggingUnit starting");

    if (!RAMMODE) {
        try {/* w  w w .  j  a v a 2  s.c o m*/
            RandomAccessFile f = new RandomAccessFile(DRIVENAME, "rw");
            if (!RECOVERY)
                f.setLength(0);
            DriveChannel = f.getChannel();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.exit(1); // not much to do without storage...
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1); // not much to do without storage...
        }

        // fork off a thread to constantly force syncing to disk
        //
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (;;) {
                    try {
                        DriveChannel.force(false);
                        synchronized (DriveLck) {
                            DriveLck.notifyAll();
                        }
                        Thread.sleep(1);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    } else {
        inmemoryStore = new ByteBuffer[UNITCAPACITY];
    }

    if (RECOVERY) {
        recover();
    } else if (REBUILD) {
        log.warn("REBUILD TEMPORARILY DISABLED");
        //TODO reimplement rebuild
        //rebuildfromnode();
    } else {
        initLogStore(UNITCAPACITY);
        writegcmark();
    }

    TServerSocket serverTransport;

    try {
        serverTransport = new TServerSocket(PORT);

        LogUnitConfigServiceImpl cnfg = new LogUnitConfigServiceImpl();

        TMultiplexedProcessor mprocessor = new TMultiplexedProcessor();
        mprocessor.registerProcessor("SUNIT", new SimpleLogUnitService.Processor<SimpleLogUnitServer>(this));
        mprocessor.registerProcessor("CONFIG",
                new SimpleLogUnitConfigService.Processor<LogUnitConfigServiceImpl>(cnfg));
        server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(mprocessor)
                .protocolFactory(TCompactProtocol::new)
                .inputTransportFactory(new TFastFramedTransport.Factory())
                .outputTransportFactory(new TFastFramedTransport.Factory()));
        log.info("Starting Corfu storage unit server on multiplexed port " + PORT);

        server.serve();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.corfudb.infrastructure.SimpleSequencerServer.java

License:Apache License

public void serverloop() {
    TServerSocket serverTransport;//from  w w  w. j a  v  a  2 s  .com
    SimpleSequencerService.Processor<SimpleSequencerServer> processor;
    log.debug("Simple sequencer entering service loop.");
    try {
        serverTransport = new TServerSocket(port);
        processor = new SimpleSequencerService.Processor<SimpleSequencerServer>(this);
        server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor)
                .protocolFactory(TCompactProtocol::new)
                .inputTransportFactory(new TFastFramedTransport.Factory())
                .outputTransportFactory(new TFastFramedTransport.Factory()));
        log.info("Simple sequencer starting on port " + port);
        server.serve();
    } catch (TTransportException e) {
        log.warn("SimpleSequencer encountered exception, restarting.", e);
    }
}

From source file:org.corfudb.infrastructure.StreamingSequencerServer.java

License:Apache License

public void serverloop() {
    StreamingSequencerService.Processor<StreamingSequencerServer> processor;
    log.debug("Streaming sequencer entering service loop.");
    try {/*from ww  w .  ja  v  a  2s. c o m*/
        serverTransport = new TServerSocket(port);
        processor = new StreamingSequencerService.Processor<StreamingSequencerServer>(this);
        server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor)
                .protocolFactory(TCompactProtocol::new)
                .inputTransportFactory(new TFastFramedTransport.Factory())
                .outputTransportFactory(new TFastFramedTransport.Factory()));
        log.info("Streaming sequencer starting on port " + port);
        server.serve();
    } catch (TTransportException e) {
        log.warn("Streaming sequencer encountered exception, restarting.", e);
    }
}

From source file:org.corfudb.runtime.BaseRuntime.java

License:Apache License

public void registerHandler(int portnum, RPCServerHandler h) {
    final RPCServerHandler handler = h;
    final TServer server;
    TServerSocket serverTransport;// w  ww.  j a va 2  s.  c  om
    RemoteReadService.Processor<RemoteReadService.Iface> processor;
    try {
        serverTransport = new TServerSocket(portnum);
        processor = new RemoteReadService.Processor(new RemoteReadService.Iface() {
            @Override
            public ByteBuffer remote_read(ByteBuffer arg) throws TException {
                return Utils.serialize(handler.deliverRPC(Utils.deserialize(arg)));
            }
        });
        server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
        new Thread(new Runnable() {
            @Override
            public void run() {
                server.serve(); //this seems to be a blocking call, putting it in its own thread
            }
        }).start();
        System.out.println("listening on port " + portnum);
    } catch (TTransportException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.corfudb.sharedlog.loggingunit.LogUnitTask.java

License:Apache License

public void serverloop() throws Exception {

    log.warn("@C@ CorfuLoggingUnit starting");

    if (!RAMMODE) {
        try {//from w  ww  .j a  va 2  s . c  o  m
            RandomAccessFile f = new RandomAccessFile(DRIVENAME, "rw");
            if (!RECOVERY)
                f.setLength(0);
            DriveChannel = f.getChannel();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.exit(1); // not much to do without storage...
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1); // not much to do without storage...
        }

        // fork off a thread to constantly force syncing to disk
        //
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (;;) {
                    try {
                        DriveChannel.force(false);
                        synchronized (DriveLck) {
                            DriveLck.notifyAll();
                        }
                        Thread.sleep(1);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    } else {
        inmemoryStore = new ByteBuffer[UNITCAPACITY];
    }

    if (RECOVERY) {
        recover();
    } else if (REBUILD) {
        rebuildfromnode();
    } else {
        initLogStore(UNITCAPACITY);
        writegcmark();
    }

    TServer server;
    TServerSocket serverTransport;
    System.out.println("run..");

    try {
        serverTransport = new TServerSocket(PORT);

        LogUnitConfigServiceImpl cnfg = new LogUnitConfigServiceImpl();

        TMultiplexedProcessor mprocessor = new TMultiplexedProcessor();
        mprocessor.registerProcessor("SUNIT", new LogUnitService.Processor<LogUnitTask>(this));
        mprocessor.registerProcessor("CONFIG",
                new LogUnitConfigService.Processor<LogUnitConfigServiceImpl>(cnfg));

        server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(mprocessor));
        System.out.println("Starting Corfu storage unit server on multiplexed port " + PORT);

        server.serve();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.corfudb.sharedlog.sequencer.SequencerTask.java

License:Apache License

public void serverloop() {

    TServer server;/*from  w  w  w  .  j ava2  s  . c  o  m*/
    TServerSocket serverTransport;
    SequencerService.Processor<SequencerTask> processor;
    System.out.println("run..");

    try {
        serverTransport = new TServerSocket(port);
        processor = new SequencerService.Processor<SequencerTask>(this);
        server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
        System.out.println("Starting sequencer on port " + port);

        server.serve();
    } catch (TTransportException e) {
        e.printStackTrace();
    }
}

From source file:org.elasticsearch.thrift.ThriftServer.java

License:Apache License

@Override
protected void doStart() throws ElasticsearchException {
    InetAddress bindAddrX;//from www  .java  2  s .  c om
    try {
        bindAddrX = networkService.resolveBindHostAddress(bindHost);
    } catch (IOException e) {
        throw new BindTransportException("Failed to resolve host [" + bindHost + "]", e);
    }
    final InetAddress bindAddr = bindAddrX;

    PortsRange portsRange = new PortsRange(port);
    final AtomicReference<Exception> lastException = new AtomicReference<Exception>();
    boolean success = portsRange.iterate(new PortsRange.PortCallback() {
        @Override
        public boolean onPortNumber(int portNumber) {
            ThriftServer.this.portNumber = portNumber;
            try {
                Rest.Processor processor = new Rest.Processor(client);

                // Bind and start to accept incoming connections.
                TServerSocket serverSocket = new TServerSocket(new InetSocketAddress(bindAddr, portNumber));

                TThreadPoolServer.Args args = new TThreadPoolServer.Args(serverSocket).minWorkerThreads(16)
                        .maxWorkerThreads(Integer.MAX_VALUE).inputProtocolFactory(protocolFactory)
                        .outputProtocolFactory(protocolFactory).processor(processor);

                if (frame <= 0) {
                    args.inputTransportFactory(new TTransportFactory());
                    args.outputTransportFactory(new TTransportFactory());
                } else {
                    args.inputTransportFactory(new TFramedTransport.Factory(frame));
                    args.outputTransportFactory(new TFramedTransport.Factory(frame));
                }

                server = new TThreadPoolServer(args);
            } catch (Exception e) {
                lastException.set(e);
                return false;
            }
            return true;
        }
    });
    if (!success) {
        throw new BindTransportException("Failed to bind to [" + port + "]", lastException.get());
    }
    logger.info("bound on port [{}]", portNumber);
    try {
        nodeService.putNodeAttribute("thrift_address",
                new InetSocketAddress(networkService.resolvePublishHostAddress(publishHost), portNumber)
                        .toString());
    } catch (Exception e) {
        // ignore
    }

    daemonThreadFactory(settings, "thrift_server").newThread(new Runnable() {
        @Override
        public void run() {
            server.serve();
        }
    }).start();
}

From source file:org.geb.core.ClientServerTest.java

@BeforeClass
public static void setUpClass() throws TTransportException {
    { // Server//from  w  w  w  .  java  2s  . c  o  m
        final EndpointService.Iface handler = new TestHandler();
        final EndpointService.Processor<EndpointService.Iface> processor = new EndpointService.Processor<>(
                handler);
        final TServerTransport serverTransport = new TServerSocket(port);
        server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
        new Thread(new Runnable() {

            @Override
            public void run() {
                LOG.info("Start server");
                server.serve();
                LOG.info("Server stopped");
            }
        }, "Server").start();
    }

    { // Client
        transport = new TSocket("localhost", port);
        LOG.info("Open client");
        transport.open();
        final TProtocol protocol = new TBinaryProtocol(transport);
        client = new EndpointService.Client(protocol);
    }
}

From source file:org.hashtrees.manager.HashTreesThriftServerTask.java

License:Apache License

private static TServer createServer(int serverPortNo, HashTreesThriftServer hashTreeServer)
        throws TTransportException {
    TServerSocket serverTransport = new TServerSocket(serverPortNo);
    HashTreesSyncInterface.Processor<Iface> processor = new HashTreesSyncInterface.Processor<HashTreesSyncInterface.Iface>(
            hashTreeServer);/*from   ww w  .ja  v  a  2  s .com*/
    TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
    return server;
}