base.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. # Copyright 2015, Google Inc.
  2. # All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are
  6. # met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above
  11. # copyright notice, this list of conditions and the following disclaimer
  12. # in the documentation and/or other materials provided with the
  13. # distribution.
  14. # * Neither the name of Google Inc. nor the names of its
  15. # contributors may be used to endorse or promote products derived from
  16. # this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. """The base interface of RPC Framework."""
  30. import abc
  31. import enum
  32. # abandonment is referenced from specification in this module.
  33. from grpc.framework.foundation import abandonment # pylint: disable=unused-import
  34. class NoSuchMethodError(Exception):
  35. """Indicates that an unrecognized operation has been called."""
  36. @enum.unique
  37. class Outcome(enum.Enum):
  38. """Operation outcomes."""
  39. COMPLETED = 'completed'
  40. CANCELLED = 'cancelled'
  41. EXPIRED = 'expired'
  42. LOCAL_SHUTDOWN = 'local shutdown'
  43. REMOTE_SHUTDOWN = 'remote shutdown'
  44. RECEPTION_FAILURE = 'reception failure'
  45. TRANSMISSION_FAILURE = 'transmission failure'
  46. LOCAL_FAILURE = 'local failure'
  47. REMOTE_FAILURE = 'remote failure'
  48. class Completion(object):
  49. """An aggregate of the values exchanged upon operation completion.
  50. Attributes:
  51. terminal_metadata: A terminal metadata value for the operaton.
  52. code: A code value for the operation.
  53. message: A message value for the operation.
  54. """
  55. __metaclass__ = abc.ABCMeta
  56. class OperationContext(object):
  57. """Provides operation-related information and action."""
  58. __metaclass__ = abc.ABCMeta
  59. @abc.abstractmethod
  60. def outcome(self):
  61. """Indicates the operation's outcome (or that the operation is ongoing).
  62. Returns:
  63. None if the operation is still active or the Outcome value for the
  64. operation if it has terminated.
  65. """
  66. raise NotImplementedError()
  67. @abc.abstractmethod
  68. def add_termination_callback(self, callback):
  69. """Adds a function to be called upon operation termination.
  70. Args:
  71. callback: A callable to be passed an Outcome value on operation
  72. termination.
  73. Returns:
  74. None if the operation has not yet terminated and the passed callback will
  75. later be called when it does terminate, or if the operation has already
  76. terminated an Outcome value describing the operation termination and the
  77. passed callback will not be called as a result of this method call.
  78. """
  79. raise NotImplementedError()
  80. @abc.abstractmethod
  81. def time_remaining(self):
  82. """Describes the length of allowed time remaining for the operation.
  83. Returns:
  84. A nonnegative float indicating the length of allowed time in seconds
  85. remaining for the operation to complete before it is considered to have
  86. timed out. Zero is returned if the operation has terminated.
  87. """
  88. raise NotImplementedError()
  89. @abc.abstractmethod
  90. def cancel(self):
  91. """Cancels the operation if the operation has not yet terminated."""
  92. raise NotImplementedError()
  93. @abc.abstractmethod
  94. def fail(self, exception):
  95. """Indicates that the operation has failed.
  96. Args:
  97. exception: An exception germane to the operation failure. May be None.
  98. """
  99. raise NotImplementedError()
  100. class Operator(object):
  101. """An interface through which to participate in an operation."""
  102. __metaclass__ = abc.ABCMeta
  103. @abc.abstractmethod
  104. def advance(
  105. self, initial_metadata=None, payload=None, completion=None,
  106. allowance=None):
  107. """Progresses the operation.
  108. Args:
  109. initial_metadata: An initial metadata value. Only one may ever be
  110. communicated in each direction for an operation, and they must be
  111. communicated no later than either the first payload or the completion.
  112. payload: A payload value.
  113. completion: A Completion value. May only ever be non-None once in either
  114. direction, and no payloads may be passed after it has been communicated.
  115. allowance: A positive integer communicating the number of additional
  116. payloads allowed to be passed by the remote side of the operation.
  117. """
  118. raise NotImplementedError()
  119. class Subscription(object):
  120. """Describes customer code's interest in values from the other side.
  121. Attributes:
  122. kind: A Kind value describing the overall kind of this value.
  123. termination_callback: A callable to be passed the Outcome associated with
  124. the operation after it has terminated. Must be non-None if kind is
  125. Kind.TERMINATION_ONLY. Must be None otherwise.
  126. allowance: A callable behavior that accepts positive integers representing
  127. the number of additional payloads allowed to be passed to the other side
  128. of the operation. Must be None if kind is Kind.FULL. Must not be None
  129. otherwise.
  130. operator: An Operator to be passed values from the other side of the
  131. operation. Must be non-None if kind is Kind.FULL. Must be None otherwise.
  132. """
  133. @enum.unique
  134. class Kind(enum.Enum):
  135. NONE = 'none'
  136. TERMINATION_ONLY = 'termination only'
  137. FULL = 'full'
  138. class Servicer(object):
  139. """Interface for service implementations."""
  140. __metaclass__ = abc.ABCMeta
  141. @abc.abstractmethod
  142. def service(self, group, method, context, output_operator):
  143. """Services an operation.
  144. Args:
  145. group: The group identifier of the operation to be serviced.
  146. method: The method identifier of the operation to be serviced.
  147. context: An OperationContext object affording contextual information and
  148. actions.
  149. output_operator: An Operator that will accept output values of the
  150. operation.
  151. Returns:
  152. A Subscription via which this object may or may not accept more values of
  153. the operation.
  154. Raises:
  155. NoSuchMethodError: If this Servicer does not handle operations with the
  156. given group and method.
  157. abandonment.Abandoned: If the operation has been aborted and there no
  158. longer is any reason to service the operation.
  159. """
  160. raise NotImplementedError()
  161. class End(object):
  162. """Common type for entry-point objects on both sides of an operation."""
  163. __metaclass__ = abc.ABCMeta
  164. @abc.abstractmethod
  165. def start(self):
  166. """Starts this object's service of operations."""
  167. raise NotImplementedError()
  168. @abc.abstractmethod
  169. def stop_gracefully(self):
  170. """Gracefully stops this object's service of operations.
  171. Operations in progress will be allowed to complete, and this method blocks
  172. until all of them have.
  173. """
  174. raise NotImplementedError()
  175. @abc.abstractmethod
  176. def stop_immediately(self):
  177. """Immediately stops this object's service of operations.
  178. Operations in progress will not be allowed to complete.
  179. """
  180. raise NotImplementedError()
  181. @abc.abstractmethod
  182. def operate(
  183. self, group, method, subscription, timeout, initial_metadata=None,
  184. payload=None, completion=None):
  185. """Commences an operation.
  186. Args:
  187. group: The group identifier of the invoked operation.
  188. method: The method identifier of the invoked operation.
  189. subscription: A Subscription to which the results of the operation will be
  190. passed.
  191. timeout: A length of time in seconds to allow for the operation.
  192. initial_metadata: An initial metadata value to be sent to the other side
  193. of the operation. May be None if the initial metadata will be later
  194. passed via the returned operator or if there will be no initial metadata
  195. passed at all.
  196. payload: An initial payload for the operation.
  197. completion: A Completion value indicating the end of transmission to the
  198. other side of the operation.
  199. Returns:
  200. A pair of objects affording information about the operation and action
  201. continuing the operation. The first element of the returned pair is an
  202. OperationContext for the operation and the second element of the
  203. returned pair is an Operator to which operation values not passed in
  204. this call should later be passed.
  205. """
  206. raise NotImplementedError()
  207. @abc.abstractmethod
  208. def operation_stats(self):
  209. """Reports the number of terminated operations broken down by outcome.
  210. Returns:
  211. A dictionary from Outcome value to an integer identifying the number
  212. of operations that terminated with that outcome.
  213. """
  214. raise NotImplementedError()
  215. @abc.abstractmethod
  216. def add_idle_action(self, action):
  217. """Adds an action to be called when this End has no ongoing operations.
  218. Args:
  219. action: A callable that accepts no arguments.
  220. """
  221. raise NotImplementedError()