public TSOServer() { super(); this.config = TSOServerConfig.configFactory(); this.finish = false; this.lock = new Object(); }
@Override public void run() { Configuration conf = OmidConfiguration.create(); // *** Start the Netty configuration *** // Start server with Nb of active threads = 2*NB CPU + 1 as maximum. // int maxSocketThreads = conf.getInt("tso.maxsocketthread", // (Runtime.getRuntime().availableProcessors() * 2 + 1) * 2); // more worder threads has an inverse impact on performance, unless the one is saturated int maxSocketThreads = conf.getInt("tso.maxsocketthread", 1); ChannelFactory factory = new NioServerSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool(), maxSocketThreads); ServerBootstrap bootstrap = new ServerBootstrap(factory); // Create the global ChannelGroup ChannelGroup channelGroup = new DefaultChannelGroup(TSOServer.class.getName()); // threads max // int maxThreads = Runtime.getRuntime().availableProcessors() *2 + 1; // More concurrency gives lower performance due to synchronizations int maxThreads = conf.getInt("tso.maxthread", 4); System.out.println("maxThreads: " + maxThreads); // int maxThreads = 5; // Memory limitation: 1MB by channel, 1GB global, 100 ms of timeout ThreadPoolExecutor pipelineExecutor = new OrderedMemoryAwareThreadPoolExecutor( maxThreads, 1048576, 1073741824, 100, TimeUnit.MILLISECONDS, Executors.defaultThreadFactory()); // This is the only object of timestamp oracle // TODO: make it singleton // TimestampOracle timestampOracle = new TimestampOracle(); // The wrapper for the shared state of TSO state = BookKeeperStateBuilder.getState(this.config); if (state == null) { LOG.error("Couldn't build state"); return; } TSOState.BATCH_SIZE = config.getBatchSize(); System.out.println("PARAM MAX_ITEMS: " + TSOState.MAX_ITEMS); System.out.println("PARAM BATCH_SIZE: " + TSOState.BATCH_SIZE); System.out.println("PARAM LOAD_FACTOR: " + TSOState.LOAD_FACTOR); System.out.println("PARAM MAX_THREADS: " + maxThreads); final TSOHandler handler = new TSOHandler(channelGroup, state); bootstrap.setPipelineFactory(new TSOPipelineFactory(pipelineExecutor, handler)); bootstrap.setOption("tcpNoDelay", false); bootstrap.setOption("child.tcpNoDelay", false); bootstrap.setOption("child.keepAlive", true); bootstrap.setOption("child.reuseAddress", true); bootstrap.setOption("child.connectTimeoutMillis", 60000); bootstrap.setOption("readWriteFair", true); // *** Start the Netty running *** // Create the monitor ThroughputMonitor monitor = new ThroughputMonitor(state); // Add the parent channel to the group Channel channel = bootstrap.bind(new InetSocketAddress(config.getPort())); channelGroup.add(channel); // Compacter handler ChannelFactory comFactory = new NioServerSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool(), (Runtime.getRuntime().availableProcessors() * 2 + 1) * 2); ServerBootstrap comBootstrap = new ServerBootstrap(comFactory); ChannelGroup comGroup = new DefaultChannelGroup("compacter"); final CompacterHandler comHandler = new CompacterHandler(comGroup, state); comBootstrap.setPipelineFactory( new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("decoder", new ObjectDecoder()); pipeline.addLast("encoder", new ObjectEncoder()); pipeline.addLast("handler", comHandler); return pipeline; } }); comBootstrap.setOption("tcpNoDelay", false); comBootstrap.setOption("child.tcpNoDelay", false); comBootstrap.setOption("child.keepAlive", true); comBootstrap.setOption("child.reuseAddress", true); comBootstrap.setOption("child.connectTimeoutMillis", 100); comBootstrap.setOption("readWriteFair", true); channel = comBootstrap.bind(new InetSocketAddress(config.getPort() + 1)); // Starts the monitor monitor.start(); synchronized (lock) { while (!finish) { try { lock.wait(); } catch (InterruptedException e) { break; } } } // timestampOracle.stop(); handler.stop(); comHandler.stop(); state.stop(); // *** Start the Netty shutdown *** // End the monitor System.out.println("End of monitor"); monitor.interrupt(); // Now close all channels System.out.println("End of channel group"); channelGroup.close().awaitUninterruptibly(); comGroup.close().awaitUninterruptibly(); // Close the executor for Pipeline System.out.println("End of pipeline executor"); pipelineExecutor.shutdownNow(); // Now release resources System.out.println("End of resources"); factory.releaseExternalResources(); comFactory.releaseExternalResources(); }
/** * Take two arguments :<br> * -port to listen to<br> * -nb of connections before shutting down * * @param args * @throws Exception */ public static void main(String[] args) throws Exception { TSOServerConfig config = TSOServerConfig.parseConfig(args); new TSOServer(config).run(); }