Mark Slee | 7eb0d63 | 2007-03-01 00:00:27 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 7 | package com.facebook.thrift.server; |
| 8 | |
| 9 | import com.facebook.thrift.TException; |
| 10 | import com.facebook.thrift.TProcessor; |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 11 | import com.facebook.thrift.TProcessorFactory; |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 12 | import com.facebook.thrift.protocol.TProtocol; |
| 13 | import com.facebook.thrift.protocol.TProtocolFactory; |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 14 | import com.facebook.thrift.protocol.TBinaryProtocol; |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 15 | import com.facebook.thrift.transport.TServerTransport; |
| 16 | import com.facebook.thrift.transport.TTransport; |
| 17 | import com.facebook.thrift.transport.TTransportException; |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 18 | import com.facebook.thrift.transport.TTransportFactory; |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 19 | |
| 20 | import java.util.concurrent.ExecutorService; |
Mark Slee | 737ce02 | 2008-01-15 02:59:12 +0000 | [diff] [blame] | 21 | import java.util.concurrent.Executors; |
Mark Slee | 19c9777 | 2008-01-10 19:57:47 +0000 | [diff] [blame] | 22 | import java.util.concurrent.SynchronousQueue; |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 23 | import java.util.concurrent.ThreadPoolExecutor; |
| 24 | import java.util.concurrent.TimeUnit; |
| 25 | |
| 26 | |
| 27 | /** |
| 28 | * Server which uses Java's built in ThreadPool management to spawn off |
Mark Slee | 19c9777 | 2008-01-10 19:57:47 +0000 | [diff] [blame] | 29 | * a worker pool that |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 30 | * |
| 31 | * @author Mark Slee <mcslee@facebook.com> |
| 32 | */ |
| 33 | public class TThreadPoolServer extends TServer { |
| 34 | |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 35 | // Executor service for handling client connections |
| 36 | private ExecutorService executorService_; |
| 37 | |
Mark Slee | 0502e61 | 2007-11-03 05:30:32 +0000 | [diff] [blame] | 38 | // Flag for stopping the server |
| 39 | private volatile boolean stopped_; |
| 40 | |
| 41 | // Server options |
| 42 | private Options options_; |
| 43 | |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 44 | // Customizable server options |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 45 | public static class Options { |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 46 | public int minWorkerThreads = 5; |
| 47 | public int maxWorkerThreads = Integer.MAX_VALUE; |
Mark Slee | 0502e61 | 2007-11-03 05:30:32 +0000 | [diff] [blame] | 48 | public int stopTimeoutVal = 60; |
| 49 | public TimeUnit stopTimeoutUnit = TimeUnit.SECONDS; |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | public TThreadPoolServer(TProcessor processor, |
| 53 | TServerTransport serverTransport) { |
Mark Slee | 19c9777 | 2008-01-10 19:57:47 +0000 | [diff] [blame] | 54 | this(processor, serverTransport, |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 55 | new TTransportFactory(), new TTransportFactory(), |
Mark Slee | 737ce02 | 2008-01-15 02:59:12 +0000 | [diff] [blame] | 56 | new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory()); |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 59 | public TThreadPoolServer(TProcessorFactory processorFactory, |
Mark Slee | 737ce02 | 2008-01-15 02:59:12 +0000 | [diff] [blame] | 60 | TServerTransport serverTransport) { |
Mark Slee | 19c9777 | 2008-01-10 19:57:47 +0000 | [diff] [blame] | 61 | this(processorFactory, serverTransport, |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 62 | new TTransportFactory(), new TTransportFactory(), |
Mark Slee | 737ce02 | 2008-01-15 02:59:12 +0000 | [diff] [blame] | 63 | new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory()); |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 66 | public TThreadPoolServer(TProcessor processor, |
| 67 | TServerTransport serverTransport, |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 68 | TProtocolFactory protocolFactory) { |
| 69 | this(processor, serverTransport, |
| 70 | new TTransportFactory(), new TTransportFactory(), |
Mark Slee | 737ce02 | 2008-01-15 02:59:12 +0000 | [diff] [blame] | 71 | protocolFactory, protocolFactory); |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | public TThreadPoolServer(TProcessor processor, |
| 75 | TServerTransport serverTransport, |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 76 | TTransportFactory transportFactory, |
| 77 | TProtocolFactory protocolFactory) { |
Mark Slee | 19c9777 | 2008-01-10 19:57:47 +0000 | [diff] [blame] | 78 | this(processor, serverTransport, |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 79 | transportFactory, transportFactory, |
Mark Slee | 737ce02 | 2008-01-15 02:59:12 +0000 | [diff] [blame] | 80 | protocolFactory, protocolFactory); |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 81 | } |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 82 | |
| 83 | public TThreadPoolServer(TProcessorFactory processorFactory, |
Mark Slee | 737ce02 | 2008-01-15 02:59:12 +0000 | [diff] [blame] | 84 | TServerTransport serverTransport, |
| 85 | TTransportFactory transportFactory, |
| 86 | TProtocolFactory protocolFactory) { |
Mark Slee | 19c9777 | 2008-01-10 19:57:47 +0000 | [diff] [blame] | 87 | this(processorFactory, serverTransport, |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 88 | transportFactory, transportFactory, |
Mark Slee | 737ce02 | 2008-01-15 02:59:12 +0000 | [diff] [blame] | 89 | protocolFactory, protocolFactory); |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Mark Slee | 737ce02 | 2008-01-15 02:59:12 +0000 | [diff] [blame] | 92 | public TThreadPoolServer(TProcessor processor, |
| 93 | TServerTransport serverTransport, |
| 94 | TTransportFactory inputTransportFactory, |
| 95 | TTransportFactory outputTransportFactory, |
| 96 | TProtocolFactory inputProtocolFactory, |
| 97 | TProtocolFactory outputProtocolFactory) { |
| 98 | this(new TProcessorFactory(processor), serverTransport, |
| 99 | inputTransportFactory, outputTransportFactory, |
| 100 | inputProtocolFactory, outputProtocolFactory); |
| 101 | } |
| 102 | |
| 103 | public TThreadPoolServer(TProcessorFactory processorFactory, |
| 104 | TServerTransport serverTransport, |
| 105 | TTransportFactory inputTransportFactory, |
| 106 | TTransportFactory outputTransportFactory, |
| 107 | TProtocolFactory inputProtocolFactory, |
| 108 | TProtocolFactory outputProtocolFactory) { |
| 109 | super(processorFactory, serverTransport, |
| 110 | inputTransportFactory, outputTransportFactory, |
| 111 | inputProtocolFactory, outputProtocolFactory); |
| 112 | options_ = new Options(); |
| 113 | executorService_ = Executors.newCachedThreadPool(); |
| 114 | } |
Mark Slee | 19c9777 | 2008-01-10 19:57:47 +0000 | [diff] [blame] | 115 | |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 116 | public TThreadPoolServer(TProcessor processor, |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 117 | TServerTransport serverTransport, |
| 118 | TTransportFactory inputTransportFactory, |
| 119 | TTransportFactory outputTransportFactory, |
| 120 | TProtocolFactory inputProtocolFactory, |
| 121 | TProtocolFactory outputProtocolFactory, |
| 122 | Options options) { |
| 123 | this(new TProcessorFactory(processor), serverTransport, |
| 124 | inputTransportFactory, outputTransportFactory, |
| 125 | inputProtocolFactory, outputProtocolFactory, |
| 126 | options); |
| 127 | } |
Mark Slee | 19c9777 | 2008-01-10 19:57:47 +0000 | [diff] [blame] | 128 | |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 129 | public TThreadPoolServer(TProcessorFactory processorFactory, |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 130 | TServerTransport serverTransport, |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 131 | TTransportFactory inputTransportFactory, |
| 132 | TTransportFactory outputTransportFactory, |
| 133 | TProtocolFactory inputProtocolFactory, |
| 134 | TProtocolFactory outputProtocolFactory, |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 135 | Options options) { |
Mark Slee | 19c9777 | 2008-01-10 19:57:47 +0000 | [diff] [blame] | 136 | super(processorFactory, serverTransport, |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 137 | inputTransportFactory, outputTransportFactory, |
| 138 | inputProtocolFactory, outputProtocolFactory); |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 139 | |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 140 | executorService_ = null; |
| 141 | |
Mark Slee | 19c9777 | 2008-01-10 19:57:47 +0000 | [diff] [blame] | 142 | SynchronousQueue<Runnable> executorQueue = |
| 143 | new SynchronousQueue<Runnable>(); |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 144 | |
| 145 | executorService_ = new ThreadPoolExecutor(options.minWorkerThreads, |
| 146 | options.maxWorkerThreads, |
| 147 | 60, |
| 148 | TimeUnit.SECONDS, |
| 149 | executorQueue); |
Mark Slee | 0502e61 | 2007-11-03 05:30:32 +0000 | [diff] [blame] | 150 | |
| 151 | options_ = options; |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | |
Mark Slee | 4e755ca | 2006-09-12 00:46:08 +0000 | [diff] [blame] | 155 | public void serve() { |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 156 | try { |
| 157 | serverTransport_.listen(); |
| 158 | } catch (TTransportException ttx) { |
| 159 | ttx.printStackTrace(); |
| 160 | return; |
| 161 | } |
| 162 | |
Mark Slee | 0502e61 | 2007-11-03 05:30:32 +0000 | [diff] [blame] | 163 | stopped_ = false; |
| 164 | while (!stopped_) { |
Mark Slee | 4e755ca | 2006-09-12 00:46:08 +0000 | [diff] [blame] | 165 | int failureCount = 0; |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 166 | try { |
| 167 | TTransport client = serverTransport_.accept(); |
| 168 | WorkerProcess wp = new WorkerProcess(client); |
| 169 | executorService_.execute(wp); |
| 170 | } catch (TTransportException ttx) { |
Mark Slee | 0502e61 | 2007-11-03 05:30:32 +0000 | [diff] [blame] | 171 | if (!stopped_) { |
| 172 | ++failureCount; |
| 173 | ttx.printStackTrace(); |
| 174 | } |
Mark Slee | 19c9777 | 2008-01-10 19:57:47 +0000 | [diff] [blame] | 175 | } |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 176 | } |
Mark Slee | 0502e61 | 2007-11-03 05:30:32 +0000 | [diff] [blame] | 177 | |
| 178 | executorService_.shutdown(); |
| 179 | try { |
| 180 | executorService_.awaitTermination(options_.stopTimeoutVal, |
| 181 | options_.stopTimeoutUnit); |
| 182 | } catch (InterruptedException ix) { |
| 183 | // Ignore and more on |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | public void stop() { |
| 188 | stopped_ = true; |
| 189 | serverTransport_.interrupt(); |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | private class WorkerProcess implements Runnable { |
| 193 | |
| 194 | /** |
| 195 | * Client that this services. |
| 196 | */ |
| 197 | private TTransport client_; |
| 198 | |
| 199 | /** |
| 200 | * Default constructor. |
| 201 | * |
| 202 | * @param client Transport to process |
| 203 | */ |
| 204 | private WorkerProcess(TTransport client) { |
| 205 | client_ = client; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Loops on processing a client forever |
| 210 | */ |
| 211 | public void run() { |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 212 | TProcessor processor = null; |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 213 | TTransport inputTransport = null; |
| 214 | TTransport outputTransport = null; |
| 215 | TProtocol inputProtocol = null; |
| 216 | TProtocol outputProtocol = null; |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 217 | try { |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 218 | processor = processorFactory_.getProcessor(client_); |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 219 | inputTransport = inputTransportFactory_.getTransport(client_); |
| 220 | outputTransport = outputTransportFactory_.getTransport(client_); |
| 221 | inputProtocol = inputProtocolFactory_.getProtocol(inputTransport); |
| 222 | outputProtocol = outputProtocolFactory_.getProtocol(outputTransport); |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 223 | while (processor.process(inputProtocol, outputProtocol)) {} |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 224 | } catch (TTransportException ttx) { |
| 225 | // Assume the client died and continue silently |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 226 | } catch (TException tx) { |
| 227 | tx.printStackTrace(); |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 228 | } catch (Exception x) { |
| 229 | x.printStackTrace(); |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 230 | } |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 231 | |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 232 | if (inputTransport != null) { |
| 233 | inputTransport.close(); |
| 234 | } |
| 235 | |
| 236 | if (outputTransport != null) { |
| 237 | outputTransport.close(); |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 238 | } |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | } |