Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pubsub): implement max_duration_per_lease_extension option #38

Merged
merged 7 commits into from Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions google/cloud/pubsub_v1/subscriber/_protocol/leaser.py
Expand Up @@ -130,8 +130,8 @@ def maintain_leases(self):
# Determine the appropriate duration for the lease. This is
# based off of how long previous messages have taken to ack, with
# a sensible default and within the ranges allowed by Pub/Sub.
p99 = self._manager.ack_histogram.percentile(99)
_LOGGER.debug("The current p99 value is %d seconds.", p99)
deadline = self._manager.ack_deadline
_LOGGER.debug("The current deadline value is %d seconds.", deadline)
IlyaFaer marked this conversation as resolved.
Show resolved Hide resolved

# Make a copy of the leased messages. This is needed because it's
# possible for another thread to modify the dictionary while
Expand Down Expand Up @@ -173,7 +173,7 @@ def maintain_leases(self):
# way for ``send_request`` to fail when the consumer
# is inactive.
self._manager.dispatcher.modify_ack_deadline(
[requests.ModAckRequest(ack_id, p99) for ack_id in ack_ids]
[requests.ModAckRequest(ack_id, deadline) for ack_id in ack_ids]
)

# Now wait an appropriate period of time and do this again.
Expand All @@ -182,7 +182,7 @@ def maintain_leases(self):
# period between 0 seconds and 90% of the lease. This use of
# jitter (http://bit.ly/2s2ekL7) helps decrease contention in cases
# where there are many clients.
snooze = random.uniform(0.0, p99 * 0.9)
snooze = random.uniform(0.0, deadline * 0.9)
_LOGGER.debug("Snoozing lease management for %f seconds.", snooze)
self._stop_event.wait(timeout=snooze)

Expand Down
Expand Up @@ -191,9 +191,20 @@ def ack_deadline(self):
Returns:
int: The ack deadline.
"""
target = min([self._last_histogram_size * 2, self._last_histogram_size + 100])
if len(self.ack_histogram) > target:
self._ack_deadline = self.ack_histogram.percentile(percent=99)
target_size = min(
self._last_histogram_size * 2, self._last_histogram_size + 100
)
hist_size = len(self.ack_histogram)

if hist_size > target_size:
self._last_histogram_size = hist_size
p99 = self.ack_histogram.percentile(percent=99)

if self.flow_control.max_extension_period > 0:
self._ack_deadline = min(p99, self.flow_control.max_extension_period)
else:
self._ack_deadline = p99
pradn marked this conversation as resolved.
Show resolved Hide resolved

return self._ack_deadline

@property
Expand Down
8 changes: 7 additions & 1 deletion google/cloud/pubsub_v1/types.py
Expand Up @@ -87,12 +87,14 @@
# these settings can be altered to tweak Pub/Sub behavior.
# The defaults should be fine for most use cases.
FlowControl = collections.namedtuple(
"FlowControl", ["max_bytes", "max_messages", "max_lease_duration"]
"FlowControl",
["max_bytes", "max_messages", "max_lease_duration", "max_extension_period"],
)
FlowControl.__new__.__defaults__ = (
100 * 1024 * 1024, # max_bytes: 100mb
1000, # max_messages: 1000
1 * 60 * 60, # max_lease_duration: 1 hour.
0, # max_extension_period: disabled
)

if sys.version_info >= (3, 5):
Expand All @@ -112,6 +114,10 @@
"The maximum amount of time in seconds to hold a lease on a message "
"before dropping it from the lease management."
)
FlowControl.max_extension_period.__doc__ = (
"Bounds the maximum amount of time in seconds before a message redelivery "
IlyaFaer marked this conversation as resolved.
Show resolved Hide resolved
"in the event the subscriber fails to extend the deadline."
)


_shared_modules = [
Expand Down
1 change: 1 addition & 0 deletions tests/unit/pubsub_v1/subscriber/test_leaser.py
Expand Up @@ -84,6 +84,7 @@ def create_manager(flow_control=types.FlowControl()):
manager.is_active = True
manager.flow_control = flow_control
manager.ack_histogram = histogram.Histogram()
manager.ack_deadline = 10
IlyaFaer marked this conversation as resolved.
Show resolved Hide resolved
return manager


Expand Down
Expand Up @@ -144,6 +144,15 @@ def test_ack_deadline():
assert manager.ack_deadline == 20


def test_ack_deadline_w_max_extension_period():
manager = make_manager()
manager._flow_control = types.FlowControl(max_extension_period=5)
for _ in range(5):
manager.ack_histogram.add(20)

assert manager.ack_deadline == 5
pradn marked this conversation as resolved.
Show resolved Hide resolved


def test_maybe_pause_consumer_wo_consumer_set():
manager = make_manager(
flow_control=types.FlowControl(max_messages=10, max_bytes=1000)
Expand Down