Skip to content

Pubsub batch autocommitting. - #2966

Merged
lukesneeringer merged 4 commits into
googleapis:masterfrom
lukesneeringer:pubsub-batching
Jan 27, 2017
Merged

Pubsub batch autocommitting.#2966
lukesneeringer merged 4 commits into
googleapis:masterfrom
lukesneeringer:pubsub-batching

Conversation

@lukesneeringer

Copy link
Copy Markdown
Contributor

This PR adds some functionality to the Batch object:

  • The ability to specify max_messages and have the batch
    automatically call commit when the number of messages
    gets that high.
  • The ability to specify max_interval and have the batch
    automatically commit when a publish occurs and the batch
    is at least as old as the specified interval.

This is one of two changes requested by the PubSub team.

This PR adds some functionality to the Batch object:
* The ability to specify `max_messages` and have the batch
automatically call `commit` when the number of messages
gets that high.
* The ability to specify `max_interval` and have the batch
automatically commit when a publish occurs and the batch
is at least as old as the specified interval.
This is one of two changes requested by the PubSub team.
@googlebotgooglebot added the cla: yes This human has signed the Contributor License Agreement. label Jan 26, 2017
Comment threadpubsub/google/cloud/pubsub/topic.py Outdated
def __init__(self, topic, client):
INFINITY = float('inf')

def __init__(self, topic, client, max_interval=INFINITY,

This comment was marked as spam.

This comment was marked as spam.

@dhermesdhermes left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly LG, ping me once addressed / we discuss?

Comment threadpubsub/google/cloud/pubsub/topic.py Outdated
:type max_messages: float
"""
def __init__(self, topic, client):
INFINITY = float('inf')

This comment was marked as spam.

Comment threadpubsub/google/cloud/pubsub/topic.py Outdated
self._max_messages = max_messages

# Set the initial starting timestamp (used against the interval).
self._start_timestamp = float(time.time())

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

Comment threadpubsub/google/cloud/pubsub/topic.py Outdated
:type attrs: dict (string -> string)
:param attrs: key-value pairs to send as message attributes

:rtype: None

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

Comment threadpubsub/google/cloud/pubsub/topic.py Outdated
if self._max_interval < self.INFINITY:
if float(time.time()) - self._start_timestamp > self._max_interval:
self._start_timestamp = float(time.time())
return self.commit()

This comment was marked as spam.

This comment was marked as spam.

Comment threadpubsub/google/cloud/pubsub/topic.py Outdated
# If the number of messages on the list is greater than the
# maximum allowed, autocommit (with the batch's client).
if len(self.messages) >= self._max_messages:
return self.commit()

This comment was marked as spam.

This comment was marked as spam.

def test_message_count_autocommit(self):
"""Establish that if the batch is assigned to take a maximum
number of messages, that it commits when it reaches that maximum.
"""

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

topic = _Topic(name='TOPIC')

# Track commits, but do not perform them.
Batch = self._get_target_class()

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

Comment threadpubsub/unit_tests/test_topic.py Outdated
for i in range(0, 4):
batch.publish({
'attributes': {},
'data': 'Batch message %d.' % i,

This comment was marked as spam.

Comment threadpubsub/unit_tests/test_topic.py Outdated
Batch = self._get_target_class()
with mock.patch.object(Batch, 'commit') as commit:
with self._make_one(topic, client=client, max_messages=5) as batch:
self.assertIsInstance(batch, self._get_target_class())

This comment was marked as spam.

This comment was marked as spam.

# Track commits, but do not perform them.
Batch = self._get_target_class()
with mock.patch.object(Batch, 'commit') as commit:
mock_time.return_value = 0.0

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

Comment threadpubsub/google/cloud/pubsub/topic.py Outdated

# If too much time has elapsed since the first message
# was added, autocommit.
if self._max_interval < self.INFINITY:

This comment was marked as spam.


# If the number of messages on the list is greater than the
# maximum allowed, autocommit (with the batch's client).
if len(self.messages) >= self._max_messages:

This comment was marked as spam.

# maximum allowed, autocommit (with the batch's client).
if len(self.messages) >= self._max_messages:
self.commit()
return

This comment was marked as spam.

topic = _Topic(name='TOPIC')

# Track commits, but do not perform them.
Batch = self._get_target_class()

This comment was marked as spam.

# Track commits, but do not perform them.
Batch = self._get_target_class()
with mock.patch.object(Batch, 'commit') as commit:
mock_time.return_value = 0.0

This comment was marked as spam.

@dhermesdhermes left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM once CI goes green

@lukesneeringer
lukesneeringer merged commit 50c8e88 into googleapis:masterJan 27, 2017
@lukesneeringer
lukesneeringer deleted the pubsub-batching branch January 27, 2017 19:11
richkadel pushed a commit to richkadel/google-cloud-python that referenced this pull request May 6, 2017
* Pubsub batch autocommitting.
This PR adds some functionality to the Batch object:
* The ability to specify `max_messages` and have the batch
automatically call `commit` when the number of messages
gets that high.
* The ability to specify `max_interval` and have the batch
automatically commit when a publish occurs and the batch
is at least as old as the specified interval.
This is one of two changes requested by the PubSub team.
* Addressing comments from @dhermes.
* Remove unneeded -lt check @dhermes.
* Make INFINITY have a leading underscore. @dhermes
parthea pushed a commit that referenced this pull request Mar 2, 2026
* Pubsub batch autocommitting.
This PR adds some functionality to the Batch object:
* The ability to specify `max_messages` and have the batch
automatically call `commit` when the number of messages
gets that high.
* The ability to specify `max_interval` and have the batch
automatically commit when a publish occurs and the batch
is at least as old as the specified interval.
This is one of two changes requested by the PubSub team.
* Addressing comments from @dhermes.
* Remove unneeded -lt check @dhermes.
* Make INFINITY have a leading underscore. @dhermes
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yesThis human has signed the Contributor License Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@lukesneeringer@theacodes@dhermes@googlebot