aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/python/grpcio/grpc/framework/crust/_control.py
blob: 9b4167bda0c20dbab18a7f0ae95b017fc31f4de4 (plain)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# Copyright 2015, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""State and behavior for translating between sync and async control flow."""

import collections
import enum
import sys
import threading
import time

from grpc.framework.foundation import abandonment
from grpc.framework.foundation import callable_util
from grpc.framework.foundation import future
from grpc.framework.foundation import stream
from grpc.framework.interfaces.base import base
from grpc.framework.interfaces.base import utilities
from grpc.framework.interfaces.face import face

_DONE_CALLBACK_LOG_MESSAGE = 'Exception calling Future "done" callback!'
_INTERNAL_ERROR_LOG_MESSAGE = ':-( RPC Framework (Crust) Internal Error! )-:'

_CANNOT_SET_INITIAL_METADATA = (
    'Could not set initial metadata - has it already been set, or has a ' +
    'payload already been sent?')
_CANNOT_SET_TERMINAL_METADATA = (
    'Could not set terminal metadata - has it already been set, or has RPC ' +
    'completion already been indicated?')
_CANNOT_SET_CODE = (
    'Could not set code - has it already been set, or has RPC completion ' +
    'already been indicated?')
_CANNOT_SET_DETAILS = (
    'Could not set details - has it already been set, or has RPC completion ' +
    'already been indicated?')


class _DummyOperator(base.Operator):

  def advance(
      self, initial_metadata=None, payload=None, completion=None,
      allowance=None):
    pass

_DUMMY_OPERATOR = _DummyOperator()


class _Awaited(
    collections.namedtuple('_Awaited', ('kind', 'value',))):

  @enum.unique
  class Kind(enum.Enum):
    NOT_YET_ARRIVED = 'not yet arrived'
    ARRIVED = 'arrived'

_NOT_YET_ARRIVED = _Awaited(_Awaited.Kind.NOT_YET_ARRIVED, None)
_ARRIVED_AND_NONE = _Awaited(_Awaited.Kind.ARRIVED, None)


class _Transitory(
    collections.namedtuple('_Transitory', ('kind', 'value',))):

  @enum.unique
  class Kind(enum.Enum):
    NOT_YET_SEEN = 'not yet seen'
    PRESENT = 'present'
    GONE = 'gone'

_NOT_YET_SEEN = _Transitory(_Transitory.Kind.NOT_YET_SEEN, None)
_GONE = _Transitory(_Transitory.Kind.GONE, None)


class _Termination(
    collections.namedtuple(
        '_Termination', ('terminated', 'abortion', 'abortion_error',))):
  """Values indicating whether and how an RPC has terminated.

  Attributes:
    terminated: A boolean indicating whether or not the RPC has terminated.
    abortion: A face.Abortion value describing the RPC's abortion or None if the
      RPC did not abort.
    abortion_error: A face.AbortionError describing the RPC's abortion or None
      if the RPC did not abort.
  """

_NOT_TERMINATED = _Termination(False, None, None)

_OPERATION_OUTCOME_KIND_TO_TERMINATION_CONSTRUCTOR = {
    base.Outcome.Kind.COMPLETED: lambda *unused_args: _Termination(
        True, None, None),
    base.Outcome.Kind.CANCELLED: lambda *args: _Termination(
        True, face.Abortion(face.Abortion.Kind.CANCELLED, *args),
        face.CancellationError(*args)),
    base.Outcome.Kind.EXPIRED: lambda *args: _Termination(
        True, face.Abortion(face.Abortion.Kind.EXPIRED, *args),
        face.ExpirationError(*args)),
    base.Outcome.Kind.LOCAL_SHUTDOWN: lambda *args: _Termination(
        True, face.Abortion(face.Abortion.Kind.LOCAL_SHUTDOWN, *args),
        face.LocalShutdownError(*args)),
    base.Outcome.Kind.REMOTE_SHUTDOWN: lambda *args: _Termination(
        True, face.Abortion(face.Abortion.Kind.REMOTE_SHUTDOWN, *args),
        face.RemoteShutdownError(*args)),
    base.Outcome.Kind.RECEPTION_FAILURE: lambda *args: _Termination(
        True, face.Abortion(face.Abortion.Kind.NETWORK_FAILURE, *args),
        face.NetworkError(*args)),
    base.Outcome.Kind.TRANSMISSION_FAILURE: lambda *args: _Termination(
        True, face.Abortion(face.Abortion.Kind.NETWORK_FAILURE, *args),
        face.NetworkError(*args)),
    base.Outcome.Kind.LOCAL_FAILURE: lambda *args: _Termination(
        True, face.Abortion(face.Abortion.Kind.LOCAL_FAILURE, *args),
        face.LocalError(*args)),
    base.Outcome.Kind.REMOTE_FAILURE: lambda *args: _Termination(
        True, face.Abortion(face.Abortion.Kind.REMOTE_FAILURE, *args),
        face.RemoteError(*args)),
}


def _wait_once_until(condition, until):
  if until is None:
    condition.wait()
  else:
    remaining = until - time.time()
    if remaining < 0:
      raise future.TimeoutError()
    else:
      condition.wait(timeout=remaining)


def _done_callback_as_operation_termination_callback(
    done_callback, rendezvous):
  def operation_termination_callback(operation_outcome):
    rendezvous.set_outcome(operation_outcome)
    done_callback(rendezvous)
  return operation_termination_callback


def _abortion_callback_as_operation_termination_callback(
    rpc_abortion_callback, rendezvous_set_outcome):
  def operation_termination_callback(operation_outcome):
    termination = rendezvous_set_outcome(operation_outcome)
    if termination.abortion is not None:
      rpc_abortion_callback(termination.abortion)
  return operation_termination_callback


class Rendezvous(base.Operator, future.Future, stream.Consumer, face.Call):
  """A rendez-vous for the threads of an operation.

  Instances of this object present iterator and stream.Consumer interfaces for
  interacting with application code and present a base.Operator interface and
  maintain a base.Operator internally for interacting with base interface code.
  """

  def __init__(self, operator, operation_context):
    self._condition = threading.Condition()

    self._operator = operator
    self._operation_context = operation_context

    self._protocol_context = _NOT_YET_ARRIVED

    self._up_initial_metadata = _NOT_YET_ARRIVED
    self._up_payload = None
    self._up_allowance = 1
    self._up_completion = _NOT_YET_ARRIVED
    self._down_initial_metadata = _NOT_YET_SEEN
    self._down_payload = None
    self._down_allowance = 1
    self._down_terminal_metadata = _NOT_YET_SEEN
    self._down_code = _NOT_YET_SEEN
    self._down_details = _NOT_YET_SEEN

    self._termination = _NOT_TERMINATED

    # The semantics of future.Future.cancel and future.Future.cancelled are
    # slightly wonky, so they have to be tracked separately from the rest of the
    # result of the RPC. This field tracks whether cancellation was requested
    # prior to termination of the RPC
    self._cancelled = False

  def set_operator_and_context(self, operator, operation_context):
    with self._condition:
      self._operator = operator
      self._operation_context = operation_context

  def _down_completion(self):
    if self._down_terminal_metadata.kind is _Transitory.Kind.NOT_YET_SEEN:
      terminal_metadata = None
      self._down_terminal_metadata = _GONE
    elif self._down_terminal_metadata.kind is _Transitory.Kind.PRESENT:
      terminal_metadata = self._down_terminal_metadata.value
      self._down_terminal_metadata = _GONE
    else:
      terminal_metadata = None
    if self._down_code.kind is _Transitory.Kind.NOT_YET_SEEN:
      code = None
      self._down_code = _GONE
    elif self._down_code.kind is _Transitory.Kind.PRESENT:
      code = self._down_code.value
      self._down_code = _GONE
    else:
      code = None
    if self._down_details.kind is _Transitory.Kind.NOT_YET_SEEN:
      details = None
      self._down_details = _GONE
    elif self._down_details.kind is _Transitory.Kind.PRESENT:
      details = self._down_details.value
      self._down_details = _GONE
    else:
      details = None
    return utilities.completion(terminal_metadata, code, details)

  def _set_outcome(self, outcome):
    if not self._termination.terminated:
      self._operator = _DUMMY_OPERATOR
      self._operation_context = None
      self._down_initial_metadata = _GONE
      self._down_payload = None
      self._down_terminal_metadata = _GONE
      self._down_code = _GONE
      self._down_details = _GONE

      if self._up_initial_metadata.kind is _Awaited.Kind.NOT_YET_ARRIVED:
        initial_metadata = None
      else:
        initial_metadata = self._up_initial_metadata.value
      if self._up_completion.kind is _Awaited.Kind.NOT_YET_ARRIVED:
        terminal_metadata = None
      else:
        terminal_metadata = self._up_completion.value.terminal_metadata
      if outcome.kind is base.Outcome.Kind.COMPLETED:
        code = self._up_completion.value.code
        details = self._up_completion.value.message
      else:
        code = outcome.code
        details = outcome.details
      self._termination = _OPERATION_OUTCOME_KIND_TO_TERMINATION_CONSTRUCTOR[
          outcome.kind](initial_metadata, terminal_metadata, code, details)

      self._condition.notify_all()

    return self._termination

  def advance(
      self, initial_metadata=None, payload=None, completion=None,
      allowance=None):
    with self._condition:
      if initial_metadata is not None:
        self._up_initial_metadata = _Awaited(
            _Awaited.Kind.ARRIVED, initial_metadata)
      if payload is not None:
        if self._up_initial_metadata.kind is _Awaited.Kind.NOT_YET_ARRIVED:
          self._up_initial_metadata = _ARRIVED_AND_NONE
        self._up_payload = payload
        self._up_allowance -= 1
      if completion is not None:
        if self._up_initial_metadata.kind is _Awaited.Kind.NOT_YET_ARRIVED:
          self._up_initial_metadata = _ARRIVED_AND_NONE
        self._up_completion = _Awaited(
            _Awaited.Kind.ARRIVED, completion)
      if allowance is not None:
        if self._down_payload is not None:
          self._operator.advance(payload=self._down_payload)
          self._down_payload = None
          self._down_allowance += allowance - 1
        else:
          self._down_allowance += allowance
      self._condition.notify_all()

  def cancel(self):
    with self._condition:
      if self._operation_context is not None:
        self._operation_context.cancel()
        self._cancelled = True
      return False

  def cancelled(self):
    with self._condition:
      return self._cancelled

  def running(self):
    with self._condition:
      return not self._termination.terminated

  def done(self):
    with self._condition:
      return self._termination.terminated

  def result(self, timeout=None):
    until = None if timeout is None else time.time() + timeout
    with self._condition:
      while True:
        if self._termination.terminated:
          if self._termination.abortion is None:
            return self._up_payload
          elif self._termination.abortion.kind is face.Abortion.Kind.CANCELLED:
            raise future.CancelledError()
          else:
            raise self._termination.abortion_error  # pylint: disable=raising-bad-type
        else:
          _wait_once_until(self._condition, until)

  def exception(self, timeout=None):
    until = None if timeout is None else time.time() + timeout
    with self._condition:
      while True:
        if self._termination.terminated:
          if self._termination.abortion is None:
            return None
          else:
            return self._termination.abortion_error
        else:
          _wait_once_until(self._condition, until)

  def traceback(self, timeout=None):
    until = None if timeout is None else time.time() + timeout
    with self._condition:
      while True:
        if self._termination.terminated:
          if self._termination.abortion_error is None:
            return None
          else:
            abortion_error = self._termination.abortion_error
            break
        else:
          _wait_once_until(self._condition, until)

    try:
      raise abortion_error
    except face.AbortionError:
      return sys.exc_info()[2]

  def add_done_callback(self, fn):
    with self._condition:
      if self._operation_context is not None:
        outcome = self._operation_context.add_termination_callback(
            _done_callback_as_operation_termination_callback(fn, self))
        if outcome is None:
          return
        else:
          self._set_outcome(outcome)

    fn(self)

  def consume(self, value):
    with self._condition:
      while True:
        if self._termination.terminated:
          return
        elif 0 < self._down_allowance:
          self._operator.advance(payload=value)
          self._down_allowance -= 1
          return
        else:
          self._condition.wait()

  def terminate(self):
    with self._condition:
      if self._termination.terminated:
        return
      elif self._down_code.kind is _Transitory.Kind.GONE:
        # Conform to specified idempotence of terminate by ignoring extra calls.
        return
      else:
        completion = self._down_completion()
        self._operator.advance(completion=completion)

  def consume_and_terminate(self, value):
    with self._condition:
      while True:
        if self._termination.terminated:
          return
        elif 0 < self._down_allowance:
          completion = self._down_completion()
          self._operator.advance(payload=value, completion=completion)
          return
        else:
          self._condition.wait()

  def __iter__(self):
    return self

  def __next__(self):
    return self.next()

  def next(self):
    with self._condition:
      while True:
        if self._termination.abortion_error is not None:
          raise self._termination.abortion_error
        elif self._up_payload is not None:
          payload = self._up_payload
          self._up_payload = None
          if self._up_completion.kind is _Awaited.Kind.NOT_YET_ARRIVED:
            self._operator.advance(allowance=1)
          return payload
        elif self._up_completion.kind is _Awaited.Kind.ARRIVED:
          raise StopIteration()
        else:
          self._condition.wait()

  def is_active(self):
    with self._condition:
      return not self._termination.terminated

  def time_remaining(self):
    if self._operation_context is None:
      return 0
    else:
      return self._operation_context.time_remaining()

  def add_abortion_callback(self, abortion_callback):
    with self._condition:
      if self._operation_context is None:
        return self._termination.abortion
      else:
        outcome = self._operation_context.add_termination_callback(
            _abortion_callback_as_operation_termination_callback(
                abortion_callback, self.set_outcome))
        if outcome is not None:
          return self._set_outcome(outcome).abortion
        else:
          return self._termination.abortion

  def protocol_context(self):
    with self._condition:
      while True:
        if self._protocol_context.kind is _Awaited.Kind.ARRIVED:
          return self._protocol_context.value
        elif self._termination.abortion_error is not None:
          raise self._termination.abortion_error
        else:
          self._condition.wait()

  def initial_metadata(self):
    with self._condition:
      while True:
        if self._up_initial_metadata.kind is _Awaited.Kind.ARRIVED:
          return self._up_initial_metadata.value
        elif self._termination.terminated:
          return None
        else:
          self._condition.wait()

  def terminal_metadata(self):
    with self._condition:
      while True:
        if self._up_completion.kind is _Awaited.Kind.ARRIVED:
          return self._up_completion.value.terminal_metadata
        elif self._termination.terminated:
          return None
        else:
          self._condition.wait()

  def code(self):
    with self._condition:
      while True:
        if self._up_completion.kind is _Awaited.Kind.ARRIVED:
          return self._up_completion.value.code
        elif self._termination.terminated:
          return None
        else:
          self._condition.wait()

  def details(self):
    with self._condition:
      while True:
        if self._up_completion.kind is _Awaited.Kind.ARRIVED:
          return self._up_completion.value.message
        elif self._termination.terminated:
          return None
        else:
          self._condition.wait()

  def set_initial_metadata(self, initial_metadata):
    with self._condition:
      if (self._down_initial_metadata.kind is not
          _Transitory.Kind.NOT_YET_SEEN):
        raise ValueError(_CANNOT_SET_INITIAL_METADATA)
      else:
        self._down_initial_metadata = _GONE
        self._operator.advance(initial_metadata=initial_metadata)

  def set_terminal_metadata(self, terminal_metadata):
    with self._condition:
      if (self._down_terminal_metadata.kind is not
          _Transitory.Kind.NOT_YET_SEEN):
        raise ValueError(_CANNOT_SET_TERMINAL_METADATA)
      else:
        self._down_terminal_metadata = _Transitory(
            _Transitory.Kind.PRESENT, terminal_metadata)

  def set_code(self, code):
    with self._condition:
      if self._down_code.kind is not _Transitory.Kind.NOT_YET_SEEN:
        raise ValueError(_CANNOT_SET_CODE)
      else:
        self._down_code = _Transitory(_Transitory.Kind.PRESENT, code)

  def set_details(self, details):
    with self._condition:
      if self._down_details.kind is not _Transitory.Kind.NOT_YET_SEEN:
        raise ValueError(_CANNOT_SET_DETAILS)
      else:
        self._down_details = _Transitory(_Transitory.Kind.PRESENT, details)

  def set_protocol_context(self, protocol_context):
    with self._condition:
      self._protocol_context = _Awaited(
          _Awaited.Kind.ARRIVED, protocol_context)
      self._condition.notify_all()

  def set_outcome(self, outcome):
    with self._condition:
      return self._set_outcome(outcome)


class _ProtocolReceiver(base.ProtocolReceiver):

  def __init__(self, rendezvous):
    self._rendezvous = rendezvous

  def context(self, protocol_context):
    self._rendezvous.set_protocol_context(protocol_context)


def protocol_receiver(rendezvous):
  return _ProtocolReceiver(rendezvous)


def pool_wrap(behavior, operation_context):
  """Wraps an operation-related behavior so that it may be called in a pool.

  Args:
    behavior: A callable related to carrying out an operation.
    operation_context: A base_interfaces.OperationContext for the operation.

  Returns:
    A callable that when called carries out the behavior of the given callable
      and handles whatever exceptions it raises appropriately.
  """
  def translation(*args):
    try:
      behavior(*args)
    except (
        abandonment.Abandoned,
        face.CancellationError,
        face.ExpirationError,
        face.LocalShutdownError,
        face.RemoteShutdownError,
        face.NetworkError,
        face.RemoteError,
    ) as e:
      if operation_context.outcome() is None:
        operation_context.fail(e)
    except Exception as e:
      operation_context.fail(e)
  return callable_util.with_exceptions_logged(
      translation, _INTERNAL_ERROR_LOG_MESSAGE)