-
Notifications
You must be signed in to change notification settings - Fork 411
feat(python): expose partition management #4017
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -777,6 +777,74 @@ impl IggyClient { | |
| }) | ||
| } | ||
|
|
||
| /// Create partitions for a topic. | ||
| /// | ||
| /// Args: | ||
| /// stream_id: Stream identifier as `str | int`. | ||
| /// topic_id: Topic identifier as `str | int`. | ||
| /// partitions_count: Number of partitions to create. | ||
|
Contributor
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. warning: also at line 819. |
||
| /// | ||
| /// Returns: | ||
| /// An awaitable that resolves to `None` when the partitions are created. | ||
|
Contributor
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. nit: nothing says which ids the new partitions get, and the caller needs them for |
||
| /// | ||
| /// Raises: | ||
|
Contributor
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. nit: also at line 824. |
||
| /// ValueError: If an identifier is invalid. | ||
| /// RuntimeError: If the request fails. | ||
| #[gen_stub(override_return_type(type_repr="collections.abc.Awaitable[None]", imports=("collections.abc")))] | ||
| fn create_partitions<'a>( | ||
| &self, | ||
| py: Python<'a>, | ||
| stream_id: PyIdentifier, | ||
| topic_id: PyIdentifier, | ||
| partitions_count: u32, | ||
| ) -> PyResult<Bound<'a, PyAny>> { | ||
| let stream_id = Identifier::try_from(stream_id)?; | ||
| let topic_id = Identifier::try_from(topic_id)?; | ||
| let inner = self.inner.clone(); | ||
|
|
||
| future_into_py(py, async move { | ||
| inner | ||
| .create_partitions(&stream_id, &topic_id, partitions_count) | ||
| .await | ||
| .map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))?; | ||
|
Contributor
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. simplification: this makes 38 copies of the same also at line 843. |
||
| Ok(()) | ||
| }) | ||
| } | ||
|
|
||
| /// Delete the last partitions from a topic, including all messages stored in them. | ||
|
Contributor
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. nit: neither docstring mentions the consumer group side effect - delete rebalances members off the removed partitions and drops their stats, create leaves the new ones unassigned until the next rebalance. one line on each. also at line 780. |
||
| /// | ||
| /// Args: | ||
| /// stream_id: Stream identifier as `str | int`. | ||
| /// topic_id: Topic identifier as `str | int`. | ||
| /// partitions_count: Number of partitions to delete from the end of the topic. | ||
|
Contributor
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. nit: also at line 785. |
||
| /// | ||
| /// Returns: | ||
| /// An awaitable that resolves to `None` when the partitions are deleted. | ||
|
Contributor
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. nit: the awaitable resolves on metadata commit, not after teardown - the unlink runs later in the reconciler and can back off. say "when the deletion is accepted; storage teardown completes asynchronously". |
||
| /// | ||
| /// Raises: | ||
| /// ValueError: If an identifier is invalid. | ||
| /// RuntimeError: If the request fails. | ||
| #[gen_stub(override_return_type(type_repr="collections.abc.Awaitable[None]", imports=("collections.abc")))] | ||
| fn delete_partitions<'a>( | ||
| &self, | ||
| py: Python<'a>, | ||
| stream_id: PyIdentifier, | ||
| topic_id: PyIdentifier, | ||
| partitions_count: u32, | ||
| ) -> PyResult<Bound<'a, PyAny>> { | ||
| let stream_id = Identifier::try_from(stream_id)?; | ||
| let topic_id = Identifier::try_from(topic_id)?; | ||
| let inner = self.inner.clone(); | ||
|
|
||
| future_into_py(py, async move { | ||
| inner | ||
| .delete_partitions(&stream_id, &topic_id, partitions_count) | ||
| .await | ||
| .map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))?; | ||
| Ok(()) | ||
| }) | ||
| } | ||
|
|
||
| /// Create a consumer group for a stream and topic. | ||
| /// | ||
| /// Args: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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 pytest | ||
|
|
||
| from apache_iggy import IggyClient | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
|
Contributor
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. nit: module-level test functions - 9 of the 10 other suites group into |
||
| async def test_create_and_delete_partitions(iggy_client: IggyClient, unique_name): | ||
|
Contributor
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. nit: no test in this file ever sends a message, so delete is never exercised with data present and the new partitions are never shown to be usable. |
||
| stream_name = unique_name() | ||
|
Contributor
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. simplification: the same 7-line create_stream + create_topic preamble is copy-pasted in four tests. pull it into a module-level helper like |
||
| topic_name = unique_name() | ||
|
|
||
| await iggy_client.create_stream(stream_name) | ||
| await iggy_client.create_topic( | ||
| stream=stream_name, name=topic_name, partitions_count=2 | ||
| ) | ||
|
|
||
| await iggy_client.create_partitions(stream_name, topic_name, 2) | ||
| topic = await iggy_client.get_topic(stream_name, topic_name) | ||
| assert topic is not None | ||
| assert topic.partitions_count == 4 | ||
| assert [partition.id for partition in topic.partitions] == [0, 1, 2, 3] | ||
|
|
||
| await iggy_client.delete_partitions(stream_name, topic_name, 2) | ||
| topic = await iggy_client.get_topic(stream_name, topic_name) | ||
| assert topic is not None | ||
| assert topic.partitions_count == 2 | ||
| assert [partition.id for partition in topic.partitions] == [0, 1] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_partition_management_accepts_numeric_ids( | ||
| iggy_client: IggyClient, unique_name | ||
| ): | ||
| stream_name = unique_name() | ||
| topic_name = unique_name() | ||
|
|
||
| await iggy_client.create_stream(stream_name) | ||
| stream = await iggy_client.get_stream(stream_name) | ||
| assert stream is not None | ||
| await iggy_client.create_topic( | ||
| stream=stream.id, name=topic_name, partitions_count=2 | ||
| ) | ||
| topic = await iggy_client.get_topic(stream.id, topic_name) | ||
| assert topic is not None | ||
|
|
||
| await iggy_client.create_partitions(stream.id, topic.id, 1) | ||
| await iggy_client.delete_partitions(stream.id, topic.id, 1) | ||
|
|
||
| topic = await iggy_client.get_topic(stream.id, topic.id) | ||
| assert topic is not None | ||
| assert [partition.id for partition in topic.partitions] == [0, 1] | ||
|
Contributor
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. nit: this asserts |
||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_partition_management_rejects_zero_count( | ||
| iggy_client: IggyClient, unique_name | ||
| ): | ||
| stream_name = unique_name() | ||
| topic_name = unique_name() | ||
|
|
||
| await iggy_client.create_stream(stream_name) | ||
| await iggy_client.create_topic( | ||
| stream=stream_name, name=topic_name, partitions_count=2 | ||
| ) | ||
|
|
||
| with pytest.raises(RuntimeError): | ||
|
Contributor
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. warning: bare also at lines 84, 100, 117, 119, 121, 123. |
||
| await iggy_client.create_partitions(stream_name, topic_name, 0) | ||
| with pytest.raises(RuntimeError): | ||
| await iggy_client.delete_partitions(stream_name, topic_name, 0) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_delete_partitions_rejects_count_larger_than_topic( | ||
| iggy_client: IggyClient, unique_name | ||
| ): | ||
| stream_name = unique_name() | ||
| topic_name = unique_name() | ||
|
|
||
| await iggy_client.create_stream(stream_name) | ||
| await iggy_client.create_topic( | ||
| stream=stream_name, name=topic_name, partitions_count=2 | ||
| ) | ||
|
|
||
| with pytest.raises(RuntimeError): | ||
| await iggy_client.delete_partitions(stream_name, topic_name, 3) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_partition_management_rejects_missing_stream_or_topic( | ||
|
Contributor
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. nit: the documented |
||
| iggy_client: IggyClient, unique_name | ||
| ): | ||
| stream_name = unique_name() | ||
| topic_name = unique_name() | ||
| missing_name = unique_name() | ||
|
|
||
| await iggy_client.create_stream(stream_name) | ||
| await iggy_client.create_topic( | ||
| stream=stream_name, name=topic_name, partitions_count=2 | ||
| ) | ||
|
|
||
| with pytest.raises(RuntimeError): | ||
|
Contributor
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. simplification: these four raises blocks only differ in which identifier is missing. one |
||
| await iggy_client.create_partitions(missing_name, topic_name, 1) | ||
| with pytest.raises(RuntimeError): | ||
| await iggy_client.create_partitions(stream_name, missing_name, 1) | ||
| with pytest.raises(RuntimeError): | ||
| await iggy_client.delete_partitions(missing_name, topic_name, 1) | ||
| with pytest.raises(RuntimeError): | ||
| await iggy_client.delete_partitions(stream_name, missing_name, 1) | ||
Uh oh!
There was an error while loading. Please reload this page.