-
Notifications
You must be signed in to change notification settings - Fork 196
/
dispatcher.py
359 lines (306 loc) · 13.1 KB
/
dispatcher.py
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
# Copyright 2017, Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
from __future__ import division
import functools
import itertools
import logging
import math
import time
import threading
import typing
from typing import List, Optional, Sequence, Union
import warnings
from google.api_core.retry import exponential_sleep_generator
from google.cloud.pubsub_v1.subscriber._protocol import helper_threads
from google.cloud.pubsub_v1.subscriber._protocol import requests
if typing.TYPE_CHECKING: # pragma: NO COVER
import queue
from google.cloud.pubsub_v1.subscriber._protocol.streaming_pull_manager import (
StreamingPullManager,
)
RequestItem = Union[
requests.AckRequest,
requests.DropRequest,
requests.LeaseRequest,
requests.ModAckRequest,
requests.NackRequest,
]
_LOGGER = logging.getLogger(__name__)
_CALLBACK_WORKER_NAME = "Thread-CallbackRequestDispatcher"
_MAX_BATCH_SIZE = 100
"""The maximum number of requests to process and dispatch at a time."""
_MAX_BATCH_LATENCY = 0.01
"""The maximum amount of time in seconds to wait for additional request items
before processing the next batch of requests."""
_ACK_IDS_BATCH_SIZE = 2500
"""The maximum number of ACK IDs to send in a single StreamingPullRequest.
The backend imposes a maximum request size limit of 524288 bytes (512 KiB) per
acknowledge / modifyAckDeadline request. ACK IDs have a maximum size of 164
bytes, thus we cannot send more than o 524288/176 ~= 2979 ACK IDs in a single
StreamingPullRequest message.
Accounting for some overhead, we should thus only send a maximum of 2500 ACK
IDs at a time.
"""
_MIN_EXACTLY_ONCE_DELIVERY_ACK_MODACK_RETRY_DURATION_SECS = 1
"""The time to wait for the first retry of failed acks and modacks when exactly-once
delivery is enabled."""
_MAX_EXACTLY_ONCE_DELIVERY_ACK_MODACK_RETRY_DURATION_SECS = 10 * 60
"""The maximum amount of time in seconds to retry failed acks and modacks when
exactly-once delivery is enabled."""
class Dispatcher(object):
def __init__(self, manager: "StreamingPullManager", queue: "queue.Queue"):
self._manager = manager
self._queue = queue
self._thread: Optional[threading.Thread] = None
self._operational_lock = threading.Lock()
def start(self) -> None:
"""Start a thread to dispatch requests queued up by callbacks.
Spawns a thread to run :meth:`dispatch_callback`.
"""
with self._operational_lock:
if self._thread is not None:
raise ValueError("Dispatcher is already running.")
worker = helper_threads.QueueCallbackWorker(
self._queue,
self.dispatch_callback,
max_items=_MAX_BATCH_SIZE,
max_latency=_MAX_BATCH_LATENCY,
)
# Create and start the helper thread.
thread = threading.Thread(name=_CALLBACK_WORKER_NAME, target=worker)
thread.daemon = True
thread.start()
_LOGGER.debug("Started helper thread %s", thread.name)
self._thread = thread
def stop(self) -> None:
with self._operational_lock:
if self._thread is not None:
# Signal the worker to stop by queueing a "poison pill"
self._queue.put(helper_threads.STOP)
self._thread.join()
self._thread = None
def dispatch_callback(self, items: Sequence[RequestItem]) -> None:
"""Map the callback request to the appropriate gRPC request.
Args:
items:
Queued requests to dispatch.
"""
lease_requests: List[requests.LeaseRequest] = []
modack_requests: List[requests.ModAckRequest] = []
ack_requests: List[requests.AckRequest] = []
nack_requests: List[requests.NackRequest] = []
drop_requests: List[requests.DropRequest] = []
for item in items:
if isinstance(item, requests.LeaseRequest):
lease_requests.append(item)
elif isinstance(item, requests.ModAckRequest):
modack_requests.append(item)
elif isinstance(item, requests.AckRequest):
ack_requests.append(item)
elif isinstance(item, requests.NackRequest):
nack_requests.append(item)
elif isinstance(item, requests.DropRequest):
drop_requests.append(item)
else:
warnings.warn(
f'Skipping unknown request item of type "{type(item)}"',
category=RuntimeWarning,
)
_LOGGER.debug("Handling %d batched requests", len(items))
if lease_requests:
self.lease(lease_requests)
if modack_requests:
self.modify_ack_deadline(modack_requests)
# Note: Drop and ack *must* be after lease. It's possible to get both
# the lease and the ack/drop request in the same batch.
if ack_requests:
self.ack(ack_requests)
if nack_requests:
self.nack(nack_requests)
if drop_requests:
self.drop(drop_requests)
def ack(self, items: Sequence[requests.AckRequest]) -> None:
"""Acknowledge the given messages.
Args:
items: The items to acknowledge.
"""
# If we got timing information, add it to the histogram.
for item in items:
time_to_ack = item.time_to_ack
if time_to_ack is not None:
self._manager.ack_histogram.add(time_to_ack)
# We must potentially split the request into multiple smaller requests
# to avoid the server-side max request size limit.
items_gen = iter(items)
ack_ids_gen = (item.ack_id for item in items)
total_chunks = int(math.ceil(len(items) / _ACK_IDS_BATCH_SIZE))
for _ in range(total_chunks):
future_reqs_dict = {
req.ack_id: req
for req in itertools.islice(items_gen, _ACK_IDS_BATCH_SIZE)
if req.future
}
requests_completed, requests_to_retry = self._manager.send_unary_ack(
ack_ids=list(itertools.islice(ack_ids_gen, _ACK_IDS_BATCH_SIZE)),
future_reqs_dict=future_reqs_dict,
)
# Remove the completed messages from lease management.
self.drop(requests_completed)
# Retry on a separate thread so the dispatcher thread isn't blocked
# by sleeps.
if requests_to_retry:
self._start_retry_thread(
"Thread-RetryAcks",
functools.partial(self._retry_acks, requests_to_retry),
)
def _start_retry_thread(self, thread_name, thread_target):
# note: if the thread is *not* a daemon, a memory leak exists due to a cpython issue.
# https://github.com/googleapis/python-pubsub/issues/395#issuecomment-829910303
# https://github.com/googleapis/python-pubsub/issues/395#issuecomment-830092418
retry_thread = threading.Thread(
name=thread_name, target=thread_target, daemon=True,
)
# The thread finishes when the requests succeed or eventually fail with
# a back-end timeout error or other permanent failure.
retry_thread.start()
def _retry_acks(self, requests_to_retry):
retry_delay_gen = exponential_sleep_generator(
initial=_MIN_EXACTLY_ONCE_DELIVERY_ACK_MODACK_RETRY_DURATION_SECS,
maximum=_MAX_EXACTLY_ONCE_DELIVERY_ACK_MODACK_RETRY_DURATION_SECS,
)
while requests_to_retry:
time_to_wait = next(retry_delay_gen)
_LOGGER.debug(
"Retrying {len(requests_to_retry)} ack(s) after delay of "
+ str(time_to_wait)
+ " seconds"
)
time.sleep(time_to_wait)
future_reqs_dict = {
req.ack_id: req for req in requests_to_retry if req.future
}
requests_completed, requests_to_retry = self._manager.send_unary_ack(
ack_ids=[req.ack_id for req in requests_to_retry],
future_reqs_dict=future_reqs_dict,
)
assert (
len(requests_to_retry) <= _ACK_IDS_BATCH_SIZE
), "Too many requests to be retried."
# Remove the completed messages from lease management.
self.drop(requests_completed)
def drop(
self,
items: Sequence[
Union[requests.AckRequest, requests.DropRequest, requests.NackRequest]
],
) -> None:
"""Remove the given messages from lease management.
Args:
items: The items to drop.
"""
assert self._manager.leaser is not None
self._manager.leaser.remove(items)
ordering_keys = (k.ordering_key for k in items if k.ordering_key)
self._manager.activate_ordering_keys(ordering_keys)
self._manager.maybe_resume_consumer()
def lease(self, items: Sequence[requests.LeaseRequest]) -> None:
"""Add the given messages to lease management.
Args:
items: The items to lease.
"""
assert self._manager.leaser is not None
self._manager.leaser.add(items)
self._manager.maybe_pause_consumer()
def modify_ack_deadline(self, items: Sequence[requests.ModAckRequest]) -> None:
"""Modify the ack deadline for the given messages.
Args:
items: The items to modify.
"""
# We must potentially split the request into multiple smaller requests
# to avoid the server-side max request size limit.
items_gen = iter(items)
ack_ids_gen = (item.ack_id for item in items)
deadline_seconds_gen = (item.seconds for item in items)
total_chunks = int(math.ceil(len(items) / _ACK_IDS_BATCH_SIZE))
for _ in range(total_chunks):
future_reqs_dict = {
req.ack_id: req
for req in itertools.islice(items_gen, _ACK_IDS_BATCH_SIZE)
if req.future
}
# no further work needs to be done for `requests_to_retry`
requests_completed, requests_to_retry = self._manager.send_unary_modack(
modify_deadline_ack_ids=list(
itertools.islice(ack_ids_gen, _ACK_IDS_BATCH_SIZE)
),
modify_deadline_seconds=list(
itertools.islice(deadline_seconds_gen, _ACK_IDS_BATCH_SIZE)
),
future_reqs_dict=future_reqs_dict,
)
assert (
len(requests_to_retry) <= _ACK_IDS_BATCH_SIZE
), "Too many requests to be retried."
# Retry on a separate thread so the dispatcher thread isn't blocked
# by sleeps.
if requests_to_retry:
self._start_retry_thread(
"Thread-RetryModAcks",
functools.partial(self._retry_modacks, requests_to_retry),
)
def _retry_modacks(self, requests_to_retry):
retry_delay_gen = exponential_sleep_generator(
initial=_MIN_EXACTLY_ONCE_DELIVERY_ACK_MODACK_RETRY_DURATION_SECS,
maximum=_MAX_EXACTLY_ONCE_DELIVERY_ACK_MODACK_RETRY_DURATION_SECS,
)
while requests_to_retry:
time_to_wait = next(retry_delay_gen)
_LOGGER.debug(
"Retrying {len(requests_to_retry)} modack(s) after delay of "
+ str(time_to_wait)
+ " seconds"
)
time.sleep(time_to_wait)
future_reqs_dict = {
req.ack_id: req for req in requests_to_retry if req.future
}
requests_completed, requests_to_retry = self._manager.send_unary_modack(
modify_deadline_ack_ids=[req.ack_id for req in requests_to_retry],
modify_deadline_seconds=[req.seconds for req in requests_to_retry],
future_reqs_dict=future_reqs_dict,
)
def nack(self, items: Sequence[requests.NackRequest]) -> None:
"""Explicitly deny receipt of messages.
Args:
items: The items to deny.
"""
self.modify_ack_deadline(
[
requests.ModAckRequest(
ack_id=item.ack_id, seconds=0, future=item.future
)
for item in items
]
)
self.drop(
[
requests.DropRequest(
ack_id=item.ack_id,
byte_size=item.byte_size,
ordering_key=item.ordering_key,
)
for item in items
]
)