-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add heartbeating to the streaming pull manager #5413
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
pubsub/google/cloud/pubsub_v1/subscriber/_protocol/heartbeater.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| # Copyright 2018, 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 | ||
|
|
||
| import logging | ||
| import threading | ||
|
|
||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
| _HEARTBEAT_WORKER_NAME = 'Thread-Heartbeater' | ||
| # How often to send heartbeats in seconds. Determined as half the period of | ||
| # time where the Pub/Sub server will close the stream as inactive, which is | ||
| # 60 seconds. | ||
| _DEFAULT_PERIOD = 30 | ||
|
|
||
|
|
||
| class Heartbeater(object): | ||
| def __init__(self, manager, period=_DEFAULT_PERIOD): | ||
| self._thread = None | ||
| self._operational_lock = threading.Lock() | ||
| self._manager = manager | ||
| self._stop_event = threading.Event() | ||
| self._period = period | ||
|
|
||
| def heartbeat(self): | ||
| """Periodically send heartbeats.""" | ||
| while self._manager.is_active and not self._stop_event.is_set(): | ||
| self._manager.heartbeat() | ||
| _LOGGER.debug('Sent heartbeat.') | ||
| self._stop_event.wait(timeout=self._period) | ||
|
|
||
| _LOGGER.info('%s exiting.', _HEARTBEAT_WORKER_NAME) | ||
|
|
||
| def start(self): | ||
| with self._operational_lock: | ||
| if self._thread is not None: | ||
| raise ValueError('Heartbeater is already running.') | ||
|
|
||
| # Create and start the helper thread. | ||
| self._stop_event.clear() | ||
| thread = threading.Thread( | ||
| name=_HEARTBEAT_WORKER_NAME, | ||
| target=self.heartbeat) | ||
| thread.daemon = True | ||
| thread.start() | ||
| _LOGGER.debug('Started helper thread %s', thread.name) | ||
| self._thread = thread | ||
|
|
||
| def stop(self): | ||
| with self._operational_lock: | ||
| self._stop_event.set() | ||
|
|
||
| if self._thread is not None: | ||
| # The thread should automatically exit when the consumer is | ||
| # inactive. | ||
| self._thread.join() | ||
|
|
||
| self._thread = None | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
pubsub/tests/unit/pubsub_v1/subscriber/test_heartbeater.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| # Copyright 2018, 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. | ||
|
|
||
| import logging | ||
| import threading | ||
|
|
||
| from google.cloud.pubsub_v1.subscriber._protocol import heartbeater | ||
| from google.cloud.pubsub_v1.subscriber._protocol import streaming_pull_manager | ||
|
|
||
| import mock | ||
| import pytest | ||
|
|
||
|
|
||
| def test_heartbeat_inactive(caplog): | ||
| caplog.set_level(logging.INFO) | ||
| manager = mock.create_autospec( | ||
| streaming_pull_manager.StreamingPullManager, instance=True) | ||
| manager.is_active = False | ||
|
|
||
| heartbeater_ = heartbeater.Heartbeater(manager) | ||
|
|
||
| heartbeater_.heartbeat() | ||
|
|
||
| assert 'exiting' in caplog.text | ||
|
|
||
|
|
||
| def test_heartbeat_stopped(caplog): | ||
| caplog.set_level(logging.INFO) | ||
| manager = mock.create_autospec( | ||
| streaming_pull_manager.StreamingPullManager, instance=True) | ||
|
|
||
| heartbeater_ = heartbeater.Heartbeater(manager) | ||
| heartbeater_.stop() | ||
|
|
||
| heartbeater_.heartbeat() | ||
|
|
||
| assert 'exiting' in caplog.text | ||
|
|
||
|
|
||
| def make_sleep_mark_manager_as_inactive(heartbeater): | ||
| # Make sleep mark the manager as inactive so that heartbeat() | ||
| # exits at the end of the first run. | ||
| def trigger_inactive(timeout): | ||
| assert timeout | ||
| heartbeater._manager.is_active = False | ||
|
|
||
| heartbeater._stop_event.wait = trigger_inactive | ||
|
|
||
|
|
||
| def test_heartbeat_once(): | ||
| manager = mock.create_autospec( | ||
| streaming_pull_manager.StreamingPullManager, instance=True) | ||
| heartbeater_ = heartbeater.Heartbeater(manager) | ||
| make_sleep_mark_manager_as_inactive(heartbeater_) | ||
|
|
||
| heartbeater_.heartbeat() | ||
|
|
||
| manager.heartbeat.assert_called_once() | ||
|
|
||
|
|
||
| @mock.patch('threading.Thread', autospec=True) | ||
| def test_start(thread): | ||
| manager = mock.create_autospec( | ||
| streaming_pull_manager.StreamingPullManager, instance=True) | ||
| heartbeater_ = heartbeater.Heartbeater(manager) | ||
|
|
||
| heartbeater_.start() | ||
|
|
||
| thread.assert_called_once_with( | ||
| name=heartbeater._HEARTBEAT_WORKER_NAME, | ||
| target=heartbeater_.heartbeat) | ||
|
|
||
| thread.return_value.start.assert_called_once() | ||
|
|
||
| assert heartbeater_._thread is not None | ||
|
|
||
|
|
||
| @mock.patch('threading.Thread', autospec=True) | ||
| def test_start_already_started(thread): | ||
| manager = mock.create_autospec( | ||
| streaming_pull_manager.StreamingPullManager, instance=True) | ||
| heartbeater_ = heartbeater.Heartbeater(manager) | ||
| heartbeater_._thread = mock.sentinel.thread | ||
|
|
||
| with pytest.raises(ValueError): | ||
| heartbeater_.start() | ||
|
|
||
| thread.assert_not_called() | ||
|
|
||
|
|
||
| def test_stop(): | ||
| manager = mock.create_autospec( | ||
| streaming_pull_manager.StreamingPullManager, instance=True) | ||
| heartbeater_ = heartbeater.Heartbeater(manager) | ||
| thread = mock.create_autospec(threading.Thread, instance=True) | ||
| heartbeater_._thread = thread | ||
|
|
||
| heartbeater_.stop() | ||
|
|
||
| assert heartbeater_._stop_event.is_set() | ||
| thread.join.assert_called_once() | ||
| assert heartbeater_._thread is None | ||
|
|
||
|
|
||
| def test_stop_no_join(): | ||
| heartbeater_ = heartbeater.Heartbeater(mock.sentinel.manager) | ||
|
|
||
| heartbeater_.stop() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.