-
Notifications
You must be signed in to change notification settings - Fork 78
[HZ-5407] Topic for Asyncio #797
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
7 commits
Select commit
Hold shift + click to select a range
e8795b9
ported Topic proxy to asyncio
yuce 700a28d
Merge branch 'master' into asyncio-topic
yuce 846f5ce
Merge branch 'master' into asyncio-topic
yuce a8606f1
Merge branch 'master' into asyncio-topic
yuce 4eb867f
Remove slack link
yuce 458f656
Merge branch 'master' into asyncio-topic
yuce 570d825
Addressed review comments
yuce 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
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
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
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
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
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
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,117 @@ | ||
| import typing | ||
|
|
||
| from hazelcast.protocol.codec import ( | ||
| topic_add_message_listener_codec, | ||
| topic_publish_codec, | ||
| topic_publish_all_codec, | ||
| topic_remove_message_listener_codec, | ||
| ) | ||
| from hazelcast.internal.asyncio_proxy.base import PartitionSpecificProxy | ||
| from hazelcast.proxy.base import TopicMessage | ||
| from hazelcast.serialization.compact import SchemaNotReplicatedError | ||
| from hazelcast.types import MessageType | ||
| from hazelcast.util import check_not_none | ||
|
|
||
|
|
||
| class Topic(PartitionSpecificProxy, typing.Generic[MessageType]): | ||
| """Hazelcast provides distribution mechanism for publishing messages that | ||
| are delivered to multiple subscribers, which is also known as a | ||
| publish/subscribe (pub/sub) messaging model. | ||
|
|
||
| Publish and subscriptions are cluster-wide. When a member subscribes to | ||
| a topic, it is actually registering for messages published by any member | ||
| in the cluster, including the new members joined after you added the | ||
| listener. | ||
|
|
||
| Messages are ordered, meaning that listeners(subscribers) will process the | ||
| messages in the order they are actually published. | ||
|
|
||
| Example: | ||
| >>> my_topic = await client.get_topic("my_topic") | ||
| >>> await my_topic.publish("hello") | ||
|
|
||
| Warning: | ||
| Asyncio client topic proxy is not thread-safe, do not access it from other threads. | ||
| """ | ||
|
|
||
| async def add_listener( | ||
| self, on_message: typing.Callable[[TopicMessage[MessageType]], None] = None | ||
| ) -> str: | ||
| """Subscribes to this topic. | ||
|
|
||
| When someone publishes a message on this topic, ``on_message`` function | ||
| is called if provided. | ||
|
|
||
| Args: | ||
| on_message: Function to be called when a message is published. This function must not block. | ||
|
|
||
| Returns: | ||
| A registration id which is used as a key to remove the listener. | ||
| """ | ||
| check_not_none(on_message, "on_message can't be None") | ||
| codec = topic_add_message_listener_codec | ||
| request = codec.encode_request(self.name, self._is_smart) | ||
|
|
||
| def handle(item_data, publish_time, uuid): | ||
| member = self._context.cluster_service.get_member(uuid) | ||
| item_event = TopicMessage( | ||
| self.name, self._to_object(item_data), publish_time / 1000.0, member | ||
| ) | ||
| on_message(item_event) | ||
|
|
||
| return await self._register_listener( | ||
| request, | ||
| lambda r: codec.decode_response(r), | ||
| lambda reg_id: topic_remove_message_listener_codec.encode_request(self.name, reg_id), | ||
| lambda m: codec.handle(m, handle), | ||
| ) | ||
|
|
||
| async def publish(self, message: MessageType) -> None: | ||
| """Publishes the message to all subscribers of this topic. | ||
|
|
||
| Args: | ||
| message: The message to be published. | ||
| """ | ||
| try: | ||
| message_data = self._to_data(message) | ||
| except SchemaNotReplicatedError as e: | ||
| return await self._send_schema_and_retry(e, self.publish, message) | ||
|
|
||
| request = topic_publish_codec.encode_request(self.name, message_data) | ||
| return await self._invoke(request) | ||
|
|
||
| async def publish_all(self, messages: typing.Sequence[MessageType]) -> None: | ||
| """Publishes the messages to all subscribers of this topic. | ||
|
|
||
| Args: | ||
| messages: The messages to be published. | ||
| """ | ||
| check_not_none(messages, "Messages cannot be None") | ||
| try: | ||
| topic_messages = [] | ||
| for m in messages: | ||
| check_not_none(m, "Message cannot be None") | ||
| data = self._to_data(m) | ||
| topic_messages.append(data) | ||
| except SchemaNotReplicatedError as e: | ||
| return await self._send_schema_and_retry(e, self.publish_all, messages) | ||
|
|
||
| request = topic_publish_all_codec.encode_request(self.name, topic_messages) | ||
| return await self._invoke(request) | ||
|
|
||
| async def remove_listener(self, registration_id: str) -> bool: | ||
| """Stops receiving messages for the given message listener. | ||
|
|
||
| If the given listener already removed, this method does nothing. | ||
|
|
||
| Args: | ||
| registration_id: Registration id of the listener to be removed. | ||
|
|
||
| Returns: | ||
| ``True`` if the listener is removed, ``False`` otherwise. | ||
| """ | ||
| return await self._deregister_listener(registration_id) | ||
|
|
||
|
|
||
| async def create_topic_proxy(service_name, name, context): | ||
| return Topic(service_name, name, context) | ||
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
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,87 @@ | ||
| from tests.integration.asyncio.base import SingleMemberTestCase | ||
| from tests.util import ( | ||
| random_string, | ||
| event_collector, | ||
| skip_if_client_version_older_than, | ||
| skip_if_server_version_older_than, | ||
| ) | ||
|
|
||
|
|
||
| class TopicTest(SingleMemberTestCase): | ||
| @classmethod | ||
| def configure_client(cls, config): | ||
| config["cluster_name"] = cls.cluster.id | ||
| return config | ||
|
|
||
| async def asyncSetUp(self): | ||
| await super().asyncSetUp() | ||
| self.topic = await self.client.get_topic(random_string()) | ||
|
|
||
| async def asyncTearDown(self): | ||
| await self.topic.destroy() | ||
| await super().asyncTearDown() | ||
|
|
||
| async def test_add_listener(self): | ||
| collector = event_collector() | ||
| await self.topic.add_listener(on_message=collector) | ||
| await self.topic.publish("item-value") | ||
|
|
||
| def assert_event(): | ||
| self.assertEqual(len(collector.events), 1) | ||
| event = collector.events[0] | ||
| self.assertEqual(event.message, "item-value") | ||
| self.assertGreater(event.publish_time, 0) | ||
|
|
||
| await self.assertTrueEventually(assert_event, 5) | ||
|
|
||
| async def test_remove_listener(self): | ||
| collector = event_collector() | ||
| reg_id = await self.topic.add_listener(on_message=collector) | ||
| await self.topic.remove_listener(reg_id) | ||
| await self.topic.publish("item-value") | ||
|
|
||
| def assert_event(): | ||
| self.assertEqual(len(collector.events), 0) | ||
| if len(collector.events) > 0: | ||
| event = collector.events[0] | ||
| self.assertEqual(event.message, "item-value") | ||
| self.assertGreater(event.publish_time, 0) | ||
|
|
||
| await self.assertTrueEventually(assert_event, 5) | ||
|
|
||
| async def test_str(self): | ||
| self.assertTrue(str(self.topic).startswith("Topic")) | ||
|
|
||
| async def test_publish_all(self): | ||
| skip_if_client_version_older_than(self, "5.2") | ||
| skip_if_server_version_older_than(self, self.client, "4.1") | ||
|
|
||
| collector = event_collector() | ||
| await self.topic.add_listener(on_message=collector) | ||
|
|
||
| messages = ["message1", "message2", "message3"] | ||
| await self.topic.publish_all(messages) | ||
|
|
||
| def assert_event(): | ||
| self.assertEqual(len(collector.events), 3) | ||
|
|
||
| await self.assertTrueEventually(assert_event, 5) | ||
|
|
||
| async def test_publish_all_none_messages(self): | ||
| skip_if_client_version_older_than(self, "5.2") | ||
| skip_if_server_version_older_than(self, self.client, "4.1") | ||
|
|
||
| with self.assertRaises(AssertionError): | ||
| await self.topic.publish_all(None) | ||
|
|
||
| async def test_publish_all_none_message(self): | ||
| skip_if_client_version_older_than(self, "5.2") | ||
| skip_if_server_version_older_than(self, self.client, "4.1") | ||
|
|
||
| messages = ["message1", None, "message3"] | ||
| with self.assertRaises(AssertionError): | ||
| await self.topic.publish_all(messages) | ||
|
|
||
| async def test_ensure_on_messsage_is_not_none(self): | ||
| with self.assertRaises(AssertionError): | ||
| await self.topic.add_listener(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
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.
Uh oh!
There was an error while loading. Please reload this page.