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