Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

# Copyright (c) 2014, Facebook, Inc.  All rights reserved. 

# 

# This source code is licensed under the BSD-style license found in the 

# LICENSE file in the root directory of this source tree. An additional grant 

# of patent rights can be found in the PATENTS file in the same directory. 

# 

"""thrift-related helper tasks""" 

from __future__ import absolute_import 

 

from ..vtask import VTask 

 

from sparts.sparts import option 

from thrift.server.TNonblockingServer import TNonblockingServer 

from thrift.transport.TSocket import TServerSocket 

 

import time 

 

 

class ThriftHandlerTask(VTask): 

    """A loopless task that handles thrift requests. 

 

    You will need to subclass this task, set MODULE, and implement the 

    necessary methods in order for requests to be mapped here.""" 

    LOOPLESS = True 

    MODULE = None 

 

    _processor = None 

 

    def initTask(self): 

        super(ThriftHandlerTask, self).initTask() 

        assert self.MODULE is not None 

        self._verifyInterface() 

 

    def _verifyInterface(self): 

        iface = self.MODULE.Iface 

        missing_methods = [] 

        for k in dir(iface): 

            v = getattr(iface, k, None) 

            if not callable(v) or k.startswith('_'): 

                continue 

            v2 = getattr(self, k, None) 

            if v2 is None or not callable(v): 

                missing_methods.append(k) 

 

        if missing_methods: 

            raise TypeError("%s is missing the following methods: %s" % 

                (self.__class__.__name__, missing_methods)) 

 

    def _makeProcessor(self): 

        return self.MODULE.Processor(self) 

 

    @property 

    def processor(self): 

        if self._processor is None: 

            self._processor = self._makeProcessor() 

        return self._processor 

 

 

class ThriftServerTask(VTask): 

    MODULE = None 

 

    def initTask(self): 

        super(ThriftServerTask, self).initTask() 

        processors = self._findProcessors() 

        assert len(processors) > 0, "No processors found for %s" % (self.MODULE) 

        assert len(processors) == 1, "Too many processors found for %s" % \ 

                (self.MODULE) 

        self.processorTask = processors[0] 

 

    @property 

    def processor(self): 

        return self.processorTask.processor 

 

    def _checkTaskModule(self, task): 

        """Returns True if `task` implements the appropriate MODULE Iface""" 

        # Skip non-ThriftHandlerTasks 

        if not isinstance(task, ThriftHandlerTask): 

            return False 

 

        # If self.MODULE is None, then connect *any* ThriftHandlerTask 

84        if self.MODULE is None: 

            return True 

 

        iface = self.MODULE.Iface 

        # Verify task has all the Iface methods. 

        for method_name in dir(iface): 

            method = getattr(iface, method_name) 

 

            # Skip field attributes 

            if not callable(method): 

                continue 

 

            # Check for this method on the handler task 

            handler_method = getattr(task, method_name, None) 

            if handler_method is None: 

                self.logger.debug("Skipping Task %s (missing method %s)", 

                                  task.name, method_name) 

                return False 

 

            # And make sure that attribute is actually callable 

            if not callable(handler_method): 

                self.logger.debug("Skipping Task %s (%s not callable)", 

                                  task.name, method_name) 

                return False 

 

        # If all the methods are there, the shoe fits. 

        return True 

 

    def _findProcessors(self): 

        """Returns all processors that match this tasks' MODULE""" 

        processors = [] 

        for task in self.service.tasks: 

            if self._checkTaskModule(task): 

                processors.append(task) 

        return processors 

 

 

class NBServerTask(ThriftServerTask): 

    """Spin up a thrift TNonblockingServer in a sparts worker thread""" 

    DEFAULT_HOST = '0.0.0.0' 

    DEFAULT_PORT = 0 

    OPT_PREFIX = 'thrift' 

 

    bound_host = bound_port = None 

 

    host = option(default=lambda cls: cls.DEFAULT_HOST, metavar='HOST', 

                  help='Address to bind server to [%(default)s]') 

    port = option(default=lambda cls: cls.DEFAULT_PORT, 

                  type=int, metavar='PORT', 

                  help='Port to run server on [%(default)s]') 

    num_threads = option(name='threads', default=10, type=int, metavar='N', 

                         help='Server Worker Threads [%(default)s]') 

 

    def initTask(self): 

        """Overridden to bind sockets, etc""" 

        super(NBServerTask, self).initTask() 

 

        self._stopped = False 

 

        # Construct TServerSocket this way for compatibility with fbthrift 

        self.socket = TServerSocket(port=self.port) 

        self.socket.host = self.host 

 

        self.server = TNonblockingServer(self.processor, self.socket, 

                                         threads=self.num_threads) 

        self.server.prepare() 

 

        self.bound_addrs = [] 

        for handle in self._get_socket_handles(self.server.socket): 

            addrinfo = handle.getsockname() 

            self.bound_host, self.bound_port = addrinfo[0:2] 

            self.logger.info("%s Server Started on %s", self.name, 

                self._fmt_hostport(self.bound_host, self.bound_port)) 

 

    def _get_socket_handles(self, tsocket): 

        """Helper to retrieve the socket objects for a given TServerSocket""" 

        handle = getattr(tsocket, 'handle', None) 

164        if handle is not None: 

            return [tsocket.handle] 

 

        # Some TServerSocket implementations support multiple handles per 

        # TServerSocket (e.g., to support binding v4 and v6 without 

        # v4-mapped addresses 

        return tsocket.handles.values() 

 

    def _fmt_hostport(self, host, port): 

168        if ':' in host: 

            return '[%s]:%d' % (host, port) 

        else: 

            return '%s:%d' % (host, port) 

 

    def stop(self): 

        """Overridden to tell the thrift server to shutdown asynchronously""" 

        self.server.stop() 

        self.server.close() 

        self._stopped = True 

 

    def _runloop(self): 

        """Overridden to execute TNonblockingServer's main loop""" 

        while not self.server._stop: 

            self.server.serve() 

        while not self._stopped: 

            time.sleep(0.1)