Mark Slee | 89e2bb8 | 2007-03-01 00:20:36 +0000 | [diff] [blame] | 1 | # |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 2 | # Licensed to the Apache Software Foundation (ASF) under one |
| 3 | # or more contributor license agreements. See the NOTICE file |
| 4 | # distributed with this work for additional information |
| 5 | # regarding copyright ownership. The ASF licenses this file |
| 6 | # to you under the Apache License, Version 2.0 (the |
| 7 | # "License"); you may not use this file except in compliance |
| 8 | # with the License. You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, |
| 13 | # software distributed under the License is distributed on an |
| 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | # KIND, either express or implied. See the License for the |
| 16 | # specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | # |
Mark Slee | 89e2bb8 | 2007-03-01 00:20:36 +0000 | [diff] [blame] | 19 | |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 20 | import Queue |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 21 | import os |
| 22 | import sys |
| 23 | import threading |
| 24 | import traceback |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 25 | |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame^] | 26 | import logging |
| 27 | logger = logging.getLogger(__name__) |
| 28 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 29 | from thrift.Thrift import TProcessor |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 30 | from thrift.protocol import TBinaryProtocol |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 31 | from thrift.transport import TTransport |
| 32 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 33 | |
| 34 | class TServer: |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 35 | """Base interface for a server, which must have a serve() method. |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 36 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 37 | Three constructors for all servers: |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 38 | 1) (processor, serverTransport) |
| 39 | 2) (processor, serverTransport, transportFactory, protocolFactory) |
| 40 | 3) (processor, serverTransport, |
| 41 | inputTransportFactory, outputTransportFactory, |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 42 | inputProtocolFactory, outputProtocolFactory) |
| 43 | """ |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 44 | def __init__(self, *args): |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 45 | if (len(args) == 2): |
| 46 | self.__initArgs__(args[0], args[1], |
| 47 | TTransport.TTransportFactoryBase(), |
| 48 | TTransport.TTransportFactoryBase(), |
| 49 | TBinaryProtocol.TBinaryProtocolFactory(), |
| 50 | TBinaryProtocol.TBinaryProtocolFactory()) |
| 51 | elif (len(args) == 4): |
| 52 | self.__initArgs__(args[0], args[1], args[2], args[2], args[3], args[3]) |
| 53 | elif (len(args) == 6): |
| 54 | self.__initArgs__(args[0], args[1], args[2], args[3], args[4], args[5]) |
| 55 | |
| 56 | def __initArgs__(self, processor, serverTransport, |
| 57 | inputTransportFactory, outputTransportFactory, |
| 58 | inputProtocolFactory, outputProtocolFactory): |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 59 | self.processor = processor |
| 60 | self.serverTransport = serverTransport |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 61 | self.inputTransportFactory = inputTransportFactory |
| 62 | self.outputTransportFactory = outputTransportFactory |
| 63 | self.inputProtocolFactory = inputProtocolFactory |
| 64 | self.outputProtocolFactory = outputProtocolFactory |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 65 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 66 | def serve(self): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 67 | pass |
| 68 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 69 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 70 | class TSimpleServer(TServer): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 71 | """Simple single-threaded server that just pumps around one transport.""" |
| 72 | |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 73 | def __init__(self, *args): |
| 74 | TServer.__init__(self, *args) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 75 | |
Mark Slee | 794993d | 2006-09-20 01:56:10 +0000 | [diff] [blame] | 76 | def serve(self): |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 77 | self.serverTransport.listen() |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 78 | while True: |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 79 | client = self.serverTransport.accept() |
Roger Meier | ab2793a | 2014-04-21 21:20:00 +0200 | [diff] [blame] | 80 | if not client: |
| 81 | continue |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 82 | itrans = self.inputTransportFactory.getTransport(client) |
| 83 | otrans = self.outputTransportFactory.getTransport(client) |
| 84 | iprot = self.inputProtocolFactory.getProtocol(itrans) |
Mark Slee | fb84b2b | 2007-02-20 03:37:28 +0000 | [diff] [blame] | 85 | oprot = self.outputProtocolFactory.getProtocol(otrans) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 86 | try: |
| 87 | while True: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 88 | self.processor.process(iprot, oprot) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 89 | except TTransport.TTransportException, tx: |
| 90 | pass |
Todd Lipcon | 2b2560e | 2012-12-10 14:29:59 -0800 | [diff] [blame] | 91 | except Exception, x: |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame^] | 92 | logger.exception(x) |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 93 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 94 | itrans.close() |
| 95 | otrans.close() |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 96 | |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 97 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 98 | class TThreadedServer(TServer): |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 99 | """Threaded server that spawns a new thread per each connection.""" |
| 100 | |
Bryan Duxbury | f2ef59f | 2010-09-02 15:12:06 +0000 | [diff] [blame] | 101 | def __init__(self, *args, **kwargs): |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 102 | TServer.__init__(self, *args) |
Bryan Duxbury | f2ef59f | 2010-09-02 15:12:06 +0000 | [diff] [blame] | 103 | self.daemon = kwargs.get("daemon", False) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 104 | |
| 105 | def serve(self): |
| 106 | self.serverTransport.listen() |
| 107 | while True: |
| 108 | try: |
| 109 | client = self.serverTransport.accept() |
Roger Meier | ab2793a | 2014-04-21 21:20:00 +0200 | [diff] [blame] | 110 | if not client: |
| 111 | continue |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 112 | t = threading.Thread(target=self.handle, args=(client,)) |
Bryan Duxbury | f2ef59f | 2010-09-02 15:12:06 +0000 | [diff] [blame] | 113 | t.setDaemon(self.daemon) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 114 | t.start() |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 115 | except KeyboardInterrupt: |
| 116 | raise |
Todd Lipcon | 2b2560e | 2012-12-10 14:29:59 -0800 | [diff] [blame] | 117 | except Exception, x: |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame^] | 118 | logger.exception(x) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 119 | |
| 120 | def handle(self, client): |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 121 | itrans = self.inputTransportFactory.getTransport(client) |
| 122 | otrans = self.outputTransportFactory.getTransport(client) |
| 123 | iprot = self.inputProtocolFactory.getProtocol(itrans) |
Mark Slee | fb84b2b | 2007-02-20 03:37:28 +0000 | [diff] [blame] | 124 | oprot = self.outputProtocolFactory.getProtocol(otrans) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 125 | try: |
| 126 | while True: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 127 | self.processor.process(iprot, oprot) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 128 | except TTransport.TTransportException, tx: |
| 129 | pass |
Todd Lipcon | 2b2560e | 2012-12-10 14:29:59 -0800 | [diff] [blame] | 130 | except Exception, x: |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame^] | 131 | logger.exception(x) |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 132 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 133 | itrans.close() |
| 134 | otrans.close() |
| 135 | |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 136 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 137 | class TThreadPoolServer(TServer): |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 138 | """Server with a fixed size pool of threads which service requests.""" |
| 139 | |
Bryan Duxbury | f2ef59f | 2010-09-02 15:12:06 +0000 | [diff] [blame] | 140 | def __init__(self, *args, **kwargs): |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 141 | TServer.__init__(self, *args) |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 142 | self.clients = Queue.Queue() |
| 143 | self.threads = 10 |
Bryan Duxbury | f2ef59f | 2010-09-02 15:12:06 +0000 | [diff] [blame] | 144 | self.daemon = kwargs.get("daemon", False) |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 145 | |
Mark Slee | 4ce787f | 2006-10-24 18:54:06 +0000 | [diff] [blame] | 146 | def setNumThreads(self, num): |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 147 | """Set the number of worker threads that should be created""" |
| 148 | self.threads = num |
| 149 | |
| 150 | def serveThread(self): |
| 151 | """Loop around getting clients from the shared queue and process them.""" |
| 152 | while True: |
| 153 | try: |
Mark Slee | 9a695ba | 2006-10-24 18:55:36 +0000 | [diff] [blame] | 154 | client = self.clients.get() |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 155 | self.serveClient(client) |
| 156 | except Exception, x: |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame^] | 157 | logger.exception(x) |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 158 | |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 159 | def serveClient(self, client): |
| 160 | """Process input/output from a client for as long as possible""" |
Aditya Agarwal | 5c46819 | 2007-02-06 01:14:33 +0000 | [diff] [blame] | 161 | itrans = self.inputTransportFactory.getTransport(client) |
| 162 | otrans = self.outputTransportFactory.getTransport(client) |
| 163 | iprot = self.inputProtocolFactory.getProtocol(itrans) |
Mark Slee | 04342d8 | 2007-02-20 03:41:35 +0000 | [diff] [blame] | 164 | oprot = self.outputProtocolFactory.getProtocol(otrans) |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 165 | try: |
| 166 | while True: |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 167 | self.processor.process(iprot, oprot) |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 168 | except TTransport.TTransportException, tx: |
| 169 | pass |
Todd Lipcon | 2b2560e | 2012-12-10 14:29:59 -0800 | [diff] [blame] | 170 | except Exception, x: |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame^] | 171 | logger.exception(x) |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 172 | |
Mark Slee | 4ac459f | 2006-10-25 21:39:01 +0000 | [diff] [blame] | 173 | itrans.close() |
| 174 | otrans.close() |
| 175 | |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 176 | def serve(self): |
| 177 | """Start a fixed number of worker threads and put client into a queue""" |
| 178 | for i in range(self.threads): |
| 179 | try: |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 180 | t = threading.Thread(target=self.serveThread) |
Bryan Duxbury | f2ef59f | 2010-09-02 15:12:06 +0000 | [diff] [blame] | 181 | t.setDaemon(self.daemon) |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 182 | t.start() |
Todd Lipcon | 2b2560e | 2012-12-10 14:29:59 -0800 | [diff] [blame] | 183 | except Exception, x: |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame^] | 184 | logger.exception(x) |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 185 | |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 186 | # Pump the socket for clients |
| 187 | self.serverTransport.listen() |
| 188 | while True: |
| 189 | try: |
| 190 | client = self.serverTransport.accept() |
Roger Meier | ab2793a | 2014-04-21 21:20:00 +0200 | [diff] [blame] | 191 | if not client: |
| 192 | continue |
Mark Slee | b90aa7c | 2006-10-24 18:49:45 +0000 | [diff] [blame] | 193 | self.clients.put(client) |
Todd Lipcon | 2b2560e | 2012-12-10 14:29:59 -0800 | [diff] [blame] | 194 | except Exception, x: |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame^] | 195 | logger.exception(x) |
David Reiss | 6653654 | 2008-06-10 22:54:49 +0000 | [diff] [blame] | 196 | |
| 197 | |
David Reiss | 6653654 | 2008-06-10 22:54:49 +0000 | [diff] [blame] | 198 | class TForkingServer(TServer): |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 199 | """A Thrift server that forks a new process for each request |
David Reiss | 6653654 | 2008-06-10 22:54:49 +0000 | [diff] [blame] | 200 | |
David Reiss | 6653654 | 2008-06-10 22:54:49 +0000 | [diff] [blame] | 201 | This is more scalable than the threaded server as it does not cause |
| 202 | GIL contention. |
| 203 | |
| 204 | Note that this has different semantics from the threading server. |
| 205 | Specifically, updates to shared variables will no longer be shared. |
| 206 | It will also not work on windows. |
| 207 | |
| 208 | This code is heavily inspired by SocketServer.ForkingMixIn in the |
| 209 | Python stdlib. |
| 210 | """ |
David Reiss | 6653654 | 2008-06-10 22:54:49 +0000 | [diff] [blame] | 211 | def __init__(self, *args): |
| 212 | TServer.__init__(self, *args) |
| 213 | self.children = [] |
| 214 | |
| 215 | def serve(self): |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 216 | def try_close(file): |
| 217 | try: |
| 218 | file.close() |
Todd Lipcon | 2b2560e | 2012-12-10 14:29:59 -0800 | [diff] [blame] | 219 | except IOError, e: |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame^] | 220 | logger.warning(e, exc_info=True) |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 221 | |
David Reiss | 6653654 | 2008-06-10 22:54:49 +0000 | [diff] [blame] | 222 | self.serverTransport.listen() |
| 223 | while True: |
| 224 | client = self.serverTransport.accept() |
Roger Meier | ab2793a | 2014-04-21 21:20:00 +0200 | [diff] [blame] | 225 | if not client: |
| 226 | continue |
David Reiss | 6653654 | 2008-06-10 22:54:49 +0000 | [diff] [blame] | 227 | try: |
| 228 | pid = os.fork() |
| 229 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 230 | if pid: # parent |
David Reiss | 6653654 | 2008-06-10 22:54:49 +0000 | [diff] [blame] | 231 | # add before collect, otherwise you race w/ waitpid |
| 232 | self.children.append(pid) |
| 233 | self.collect_children() |
| 234 | |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 235 | # Parent must close socket or the connection may not get |
| 236 | # closed promptly |
| 237 | itrans = self.inputTransportFactory.getTransport(client) |
| 238 | otrans = self.outputTransportFactory.getTransport(client) |
| 239 | try_close(itrans) |
| 240 | try_close(otrans) |
David Reiss | 6653654 | 2008-06-10 22:54:49 +0000 | [diff] [blame] | 241 | else: |
| 242 | itrans = self.inputTransportFactory.getTransport(client) |
| 243 | otrans = self.outputTransportFactory.getTransport(client) |
| 244 | |
| 245 | iprot = self.inputProtocolFactory.getProtocol(itrans) |
| 246 | oprot = self.outputProtocolFactory.getProtocol(otrans) |
| 247 | |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 248 | ecode = 0 |
David Reiss | 6653654 | 2008-06-10 22:54:49 +0000 | [diff] [blame] | 249 | try: |
Kevin Clark | 1e0744d | 2008-06-24 20:46:32 +0000 | [diff] [blame] | 250 | try: |
| 251 | while True: |
| 252 | self.processor.process(iprot, oprot) |
| 253 | except TTransport.TTransportException, tx: |
| 254 | pass |
Todd Lipcon | 2b2560e | 2012-12-10 14:29:59 -0800 | [diff] [blame] | 255 | except Exception, e: |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame^] | 256 | logger.exception(e) |
Kevin Clark | 1e0744d | 2008-06-24 20:46:32 +0000 | [diff] [blame] | 257 | ecode = 1 |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 258 | finally: |
| 259 | try_close(itrans) |
| 260 | try_close(otrans) |
David Reiss | 6653654 | 2008-06-10 22:54:49 +0000 | [diff] [blame] | 261 | |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 262 | os._exit(ecode) |
David Reiss | 6653654 | 2008-06-10 22:54:49 +0000 | [diff] [blame] | 263 | |
| 264 | except TTransport.TTransportException, tx: |
| 265 | pass |
Todd Lipcon | 2b2560e | 2012-12-10 14:29:59 -0800 | [diff] [blame] | 266 | except Exception, x: |
Konrad Grochowski | 3a724e3 | 2014-08-12 11:48:29 -0400 | [diff] [blame^] | 267 | logger.exception(x) |
David Reiss | 6653654 | 2008-06-10 22:54:49 +0000 | [diff] [blame] | 268 | |
David Reiss | 6653654 | 2008-06-10 22:54:49 +0000 | [diff] [blame] | 269 | def collect_children(self): |
| 270 | while self.children: |
| 271 | try: |
| 272 | pid, status = os.waitpid(0, os.WNOHANG) |
| 273 | except os.error: |
| 274 | pid = None |
| 275 | |
| 276 | if pid: |
| 277 | self.children.remove(pid) |
| 278 | else: |
| 279 | break |