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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add support for increasing partitions in python #74
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -19,6 +19,7 @@ develop-eggs | |
.installed.cfg | ||
lib | ||
lib64 | ||
venv | ||
__pycache__ | ||
|
||
# Installer logs | ||
|
This file contains 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
22 changes: 22 additions & 0 deletions
22
google/cloud/pubsublite/internal/wire/partition_count_watcher.py
This file contains 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,22 @@ | ||
# Copyright 2020 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 | ||
# | ||
# http://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 abc import abstractmethod | ||
from typing import AsyncContextManager | ||
|
||
|
||
class PartitionCountWatcher(AsyncContextManager): | ||
@abstractmethod | ||
async def get_partition_count(self) -> int: | ||
raise NotImplementedError() |
75 changes: 75 additions & 0 deletions
75
google/cloud/pubsublite/internal/wire/partition_count_watcher_impl.py
This file contains 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,75 @@ | ||
# Copyright 2020 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 | ||
# | ||
# http://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 | ||
from concurrent.futures.thread import ThreadPoolExecutor | ||
import asyncio | ||
|
||
from google.cloud.pubsublite import AdminClientInterface | ||
from google.cloud.pubsublite.internal.wait_ignore_cancelled import wait_ignore_cancelled | ||
from google.cloud.pubsublite.internal.wire.partition_count_watcher import ( | ||
PartitionCountWatcher, | ||
) | ||
from google.cloud.pubsublite.internal.wire.permanent_failable import PermanentFailable | ||
from google.cloud.pubsublite.types import TopicPath | ||
from google.api_core.exceptions import GoogleAPICallError | ||
|
||
|
||
class PartitionCountWatcherImpl(PartitionCountWatcher, PermanentFailable): | ||
_admin: AdminClientInterface | ||
_topic_path: TopicPath | ||
_duration: float | ||
_any_success: bool | ||
_thread: ThreadPoolExecutor | ||
_queue: asyncio.Queue | ||
_poll_partition_loop: asyncio.Future | ||
|
||
def __init__( | ||
self, admin: AdminClientInterface, topic_path: TopicPath, duration: float | ||
): | ||
super().__init__() | ||
self._admin = admin | ||
self._topic_path = topic_path | ||
self._duration = duration | ||
self._any_success = False | ||
palmere-google marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
async def __aenter__(self): | ||
self._thread = ThreadPoolExecutor(max_workers=1) | ||
self._queue = asyncio.Queue(maxsize=1) | ||
self._poll_partition_loop = asyncio.ensure_future( | ||
self.run_poller(self._poll_partition_loop) | ||
) | ||
|
||
async def __aexit__(self, exc_type, exc_val, exc_tb): | ||
self._poll_partition_loop.cancel() | ||
await wait_ignore_cancelled(self._poll_partition_loop) | ||
self._thread.shutdown(wait=False) | ||
|
||
def _get_partition_count_sync(self) -> int: | ||
return self._admin.get_topic_partition_count(self._topic_path) | ||
|
||
async def _poll_partition_loop(self): | ||
try: | ||
partition_count = await asyncio.get_event_loop().run_in_executor( | ||
self._thread, self._get_partition_count_sync | ||
) | ||
self._any_success = True | ||
await self._queue.put(partition_count) | ||
except GoogleAPICallError as e: | ||
if not self._any_success: | ||
dpcollins-google marked this conversation as resolved.
Show resolved
Hide resolved
|
||
raise e | ||
logging.exception("Failed to retrieve partition count") | ||
await asyncio.sleep(self._duration) | ||
|
||
async def get_partition_count(self) -> int: | ||
return await self.await_unless_failed(self._queue.get()) |
94 changes: 94 additions & 0 deletions
94
google/cloud/pubsublite/internal/wire/partition_count_watching_publisher.py
This file contains 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,94 @@ | ||
# Copyright 2020 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 | ||
# | ||
# http://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 asyncio | ||
import sys | ||
from typing import Callable, Dict | ||
|
||
from google.cloud.pubsublite.internal.wait_ignore_cancelled import wait_ignore_cancelled | ||
from google.cloud.pubsublite.internal.wire.partition_count_watcher import ( | ||
PartitionCountWatcher, | ||
) | ||
from google.cloud.pubsublite.internal.wire.publisher import Publisher | ||
from google.cloud.pubsublite.internal.wire.routing_policy import RoutingPolicy | ||
from google.cloud.pubsublite.types import PublishMetadata, Partition | ||
from google.cloud.pubsublite_v1 import PubSubMessage | ||
|
||
|
||
class PartitionCountWatchingPublisher(Publisher): | ||
_publishers: Dict[Partition, Publisher] | ||
_publisher_factory: Callable[[Partition], Publisher] | ||
_policy_factory: Callable[[int], RoutingPolicy] | ||
_watcher: PartitionCountWatcher | ||
_partition_count_poller: asyncio.Future | ||
|
||
def __init__( | ||
self, | ||
watcher: PartitionCountWatcher, | ||
publisher_factory: Callable[[Partition], Publisher], | ||
policy_factory: Callable[[int], RoutingPolicy], | ||
): | ||
self._publishers = {} | ||
self._publisher_factory = publisher_factory | ||
self._policy_factory = policy_factory | ||
self._watcher = watcher | ||
|
||
async def __aenter__(self): | ||
try: | ||
await self._watcher.__aenter__() | ||
await self._poll_partition_count_action() | ||
except Exception: | ||
await self._watcher.__aexit__(*sys.exc_info()) | ||
raise | ||
self._partition_count_poller = asyncio.ensure_future( | ||
self._watch_partition_count() | ||
) | ||
return self | ||
|
||
async def __aexit__(self, exc_type, exc_val, exc_tb): | ||
self._partition_count_poller.cancel() | ||
await wait_ignore_cancelled(self._partition_count_poller) | ||
await self._watcher.__aexit__(exc_type, exc_val, exc_tb) | ||
for publisher in self._publishers.values(): | ||
await publisher.__aexit__(exc_type, exc_val, exc_tb) | ||
|
||
async def _poll_partition_count_action(self): | ||
partition_count = await self._watcher.get_partition_count() | ||
await self._handle_partition_count_update(partition_count) | ||
|
||
async def _watch_partition_count(self): | ||
while True: | ||
await self._poll_partition_count_action() | ||
|
||
async def _handle_partition_count_update(self, partition_count: int): | ||
current_count = len(self._publishers) | ||
if current_count == partition_count: | ||
return | ||
if current_count > partition_count: | ||
return | ||
|
||
new_publishers = { | ||
Partition(index): self._publisher_factory(Partition(index)) | ||
for index in range(current_count, partition_count) | ||
} | ||
await asyncio.gather(*[p.__aenter__() for p in new_publishers.values()]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. run |
||
routing_policy = self._policy_factory(partition_count) | ||
|
||
self._publishers.update(new_publishers) | ||
self._routing_policy = routing_policy | ||
|
||
async def publish(self, message: PubSubMessage) -> PublishMetadata: | ||
partition = self._routing_policy.route(message) | ||
assert partition in self._publishers | ||
publisher = self._publishers[partition] | ||
return await publisher.publish(message) |
106 changes: 106 additions & 0 deletions
106
tests/unit/pubsublite/internal/wire/partition_count_watcher_impl_test.py
This file contains 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,106 @@ | ||
# Copyright 2020 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 | ||
# | ||
# http://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 asyncio | ||
import queue | ||
import threading | ||
from asynctest.mock import MagicMock | ||
import pytest | ||
|
||
from google.cloud.pubsublite import AdminClientInterface | ||
from google.cloud.pubsublite.internal.wire.partition_count_watcher_impl import ( | ||
PartitionCountWatcherImpl, | ||
) | ||
from google.cloud.pubsublite.internal.wire.publisher import Publisher | ||
from google.cloud.pubsublite.testing.test_utils import Box | ||
from google.cloud.pubsublite.types import Partition, TopicPath, CloudZone, CloudRegion | ||
from google.api_core.exceptions import GoogleAPICallError | ||
|
||
pytestmark = pytest.mark.asyncio | ||
|
||
|
||
@pytest.fixture() | ||
def mock_publishers(): | ||
return {Partition(i): MagicMock(spec=Publisher) for i in range(10)} | ||
|
||
|
||
@pytest.fixture() | ||
def topic(): | ||
zone = CloudZone(region=CloudRegion("a"), zone_id="a") | ||
return TopicPath(project_number=1, location=zone, name="c") | ||
|
||
|
||
@pytest.fixture() | ||
def mock_admin(): | ||
admin = MagicMock(spec=AdminClientInterface) | ||
return admin | ||
|
||
|
||
@pytest.fixture() | ||
def watcher(mock_admin, topic): | ||
box = Box() | ||
|
||
def set_box(): | ||
box.val = PartitionCountWatcherImpl(mock_admin, topic, 0.001) | ||
|
||
# Initialize watcher on another thread with a different event loop. | ||
thread = threading.Thread(target=set_box) | ||
thread.start() | ||
thread.join() | ||
return box.val | ||
|
||
|
||
async def test_init(watcher, mock_admin, topic): | ||
mock_admin.get_topic_partition_count.return_value = 2 | ||
async with watcher: | ||
pass | ||
|
||
|
||
async def test_get_count_first_failure(watcher, mock_admin, topic): | ||
mock_admin.get_topic_partition_count.side_effect = GoogleAPICallError("error") | ||
with pytest.raises(GoogleAPICallError): | ||
async with watcher: | ||
await watcher.get_partition_count() | ||
|
||
|
||
async def test_get_multiple_counts(watcher, mock_admin, topic): | ||
q = queue.Queue() | ||
mock_admin.get_topic_partition_count.side_effect = q.get | ||
async with watcher: | ||
task1 = asyncio.ensure_future(watcher.get_partition_count()) | ||
task2 = asyncio.ensure_future(watcher.get_partition_count()) | ||
assert not task1.done() | ||
assert not task2.done() | ||
q.put(3) | ||
assert await task1 == 3 | ||
assert not task2.done() | ||
q.put(4) | ||
assert await task2 == 4 | ||
|
||
|
||
async def test_subsequent_failures_ignored(watcher, mock_admin, topic): | ||
q = queue.Queue() | ||
|
||
def side_effect(): | ||
value = q.get() | ||
if isinstance(value, Exception): | ||
raise value | ||
return value | ||
|
||
mock_admin.get_topic_partition_count.side_effect = lambda x: side_effect() | ||
async with watcher: | ||
q.put(3) | ||
assert await watcher.get_partition_count() == 3 | ||
q.put(GoogleAPICallError("error")) | ||
q.put(4) | ||
assert await watcher.get_partition_count() == 4 |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
put this down with 'env/' and add the slash.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done