-
Notifications
You must be signed in to change notification settings - Fork 39
feat: (Python) Add async context manager #487
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: main
Are you sure you want to change the base?
Changes from all commits
2a29c63
dec0921
180ad11
a1cadf2
ff10371
a6a5bff
df9e8c7
713e5c2
0fa4500
efbf4f7
c1546fc
5e0adc8
8bec2d8
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 |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| use crate::*; | ||
| use pyo3_async_runtimes::tokio::future_into_py; | ||
| use std::sync::Arc; | ||
| use std::time::Duration; | ||
|
|
||
| /// Connection to a Fluss cluster | ||
| #[pyclass] | ||
|
|
@@ -82,9 +83,19 @@ impl FlussConnection { | |
| }) | ||
| } | ||
|
|
||
| // Close the connection | ||
| fn close(&mut self) -> PyResult<()> { | ||
| Ok(()) | ||
| /// Close the connection (async). | ||
| /// | ||
| /// Gracefully shuts down the connection by draining any pending write batches. | ||
| /// This method is awaitable. | ||
| fn close<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> { | ||
| let inner = self.inner.clone(); | ||
|
|
||
| future_into_py(py, async move { | ||
| inner | ||
| .close(Duration::MAX) | ||
| .await | ||
| .map_err(|e| FlussError::from_core_error(&e)) | ||
| }) | ||
| } | ||
|
|
||
| // Enter the runtime context (for 'with' statement) | ||
|
|
@@ -100,10 +111,36 @@ impl FlussConnection { | |
| _exc_value: Option<Bound<'_, PyAny>>, | ||
| _traceback: Option<Bound<'_, PyAny>>, | ||
| ) -> PyResult<bool> { | ||
| self.close()?; | ||
| // Sync exit cannot await the graceful drain, so it's a no-op here. | ||
| // Users should use 'async with' for graceful shutdown. | ||
| Ok(false) | ||
| } | ||
|
|
||
| // Enter the async runtime context (for 'async with' statement) | ||
| fn __aenter__<'py>(slf: PyRef<'py, Self>, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> { | ||
| let py_slf = slf.into_pyobject(py)?.unbind(); | ||
| future_into_py(py, async move { Ok(py_slf) }) | ||
| } | ||
|
|
||
| // Exit the async runtime context (for 'async with' statement) | ||
| #[pyo3(signature = (_exc_type=None, _exc_value=None, _traceback=None))] | ||
| fn __aexit__<'py>( | ||
| &self, | ||
| py: Python<'py>, | ||
| _exc_type: Option<Bound<'py, PyAny>>, | ||
| _exc_value: Option<Bound<'py, PyAny>>, | ||
| _traceback: Option<Bound<'py, PyAny>>, | ||
| ) -> PyResult<Bound<'py, PyAny>> { | ||
| let inner = self.inner.clone(); | ||
| future_into_py(py, async move { | ||
| inner | ||
| .close(Duration::MAX) | ||
| .await | ||
| .map_err(|e| FlussError::from_core_error(&e))?; | ||
| Ok(false) | ||
| }) | ||
| } | ||
|
Comment on lines
+125
to
+142
|
||
|
|
||
| fn __repr__(&self) -> String { | ||
| "FlussConnection()".to_string() | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -989,6 +989,32 @@ impl AppendWriter { | |
| }) | ||
| } | ||
|
|
||
| // Enter the async runtime context (for 'async with' statement) | ||
| fn __aenter__<'py>(slf: PyRef<'py, Self>, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> { | ||
| let py_slf = slf.into_pyobject(py)?.unbind(); | ||
| future_into_py(py, async move { Ok(py_slf) }) | ||
| } | ||
|
|
||
| // Exit the async runtime context (for 'async with' statement) | ||
|
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. I don't think we need this logic. The whole idea of context managers is guaranteed cleanup, and here we skip flush() just to return the error faster - that doesn't match. Can we just always call flush() on exit
Contributor
Author
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. Hi @fresh-borzoni, this has been addressed in 5e0adc8. |
||
| /// On exit, the writer is automatically flushed. | ||
| #[pyo3(signature = (_exc_type=None, _exc_value=None, _traceback=None))] | ||
| fn __aexit__<'py>( | ||
| &self, | ||
| py: Python<'py>, | ||
| _exc_type: Option<Bound<'py, PyAny>>, | ||
| _exc_value: Option<Bound<'py, PyAny>>, | ||
| _traceback: Option<Bound<'py, PyAny>>, | ||
| ) -> PyResult<Bound<'py, PyAny>> { | ||
| let inner = self.inner.clone(); | ||
| future_into_py(py, async move { | ||
| inner | ||
| .flush() | ||
| .await | ||
| .map_err(|e| FlussError::from_core_error(&e))?; | ||
|
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. we need to do smth like this: def __exit__(self, exc_type, exc_val, exc_tb):
try:
self.cleanup()
except Exception:
if exc_type is None:
raise # no in-flight exception -> surface cleanup error
# else: don't mask the user's exception
return Falseotherwise we'll swallow exception if there is some in context block and flush raises for whatever reason |
||
| Ok(false) | ||
| }) | ||
| } | ||
|
Comment on lines
+998
to
+1016
|
||
|
|
||
| fn __repr__(&self) -> String { | ||
| "AppendWriter()".to_string() | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -108,6 +108,32 @@ impl UpsertWriter { | |
| }) | ||
| } | ||
|
|
||
| // Enter the async runtime context (for 'async with' statement) | ||
| fn __aenter__<'py>(slf: PyRef<'py, Self>, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> { | ||
| let py_slf = slf.into_pyobject(py)?.unbind(); | ||
| future_into_py(py, async move { Ok(py_slf) }) | ||
| } | ||
|
|
||
| // Exit the async runtime context (for 'async with' statement) | ||
| /// On exit, the writer is automatically flushed. | ||
| #[pyo3(signature = (_exc_type=None, _exc_value=None, _traceback=None))] | ||
| fn __aexit__<'py>( | ||
| &self, | ||
| py: Python<'py>, | ||
| _exc_type: Option<Bound<'py, PyAny>>, | ||
| _exc_value: Option<Bound<'py, PyAny>>, | ||
| _traceback: Option<Bound<'py, PyAny>>, | ||
| ) -> PyResult<Bound<'py, PyAny>> { | ||
| let writer = self.writer.clone(); | ||
| future_into_py(py, async move { | ||
| writer | ||
| .flush() | ||
| .await | ||
| .map_err(|e| FlussError::from_core_error(&e))?; | ||
|
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. ditto |
||
| Ok(false) | ||
| }) | ||
|
Comment on lines
+117
to
+134
|
||
| } | ||
|
|
||
| fn __repr__(&self) -> String { | ||
| "UpsertWriter()".to_string() | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
|
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. For the tests, I think three are enough: one for each async with-enabled type (AppendWriter, UpsertWriter, FlussConnection), each verifying the one behavior the CM adds - that pending writes get flushed/drained on exit.
Contributor
Author
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. Hi @fresh-borzoni, this has been addressed in 5e0adc8. |
||
| # 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 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. do we still need this import? |
||
| import pytest | ||
| import pyarrow as pa | ||
| import time | ||
| import fluss | ||
|
|
||
| def _poll_records(scanner, expected_count, timeout_s=10): | ||
| """Poll a record-based scanner until expected_count records are collected.""" | ||
| collected = [] | ||
| deadline = time.monotonic() + timeout_s | ||
| while len(collected) < expected_count and time.monotonic() < deadline: | ||
| records = scanner.poll(5000) | ||
| collected.extend(records) | ||
| return collected | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_connection_context_manager(plaintext_bootstrap_servers): | ||
| config = fluss.Config({"bootstrap.servers": plaintext_bootstrap_servers}) | ||
| async with await fluss.FlussConnection.create(config) as conn: | ||
| admin = conn.get_admin() | ||
| nodes = await admin.get_server_nodes() | ||
| assert len(nodes) > 0 | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_append_writer_success_flush(connection, admin): | ||
| table_path = fluss.TablePath("fluss", "test_append_ctx_success") | ||
| await admin.drop_table(table_path, ignore_if_not_exists=True) | ||
|
|
||
| schema = fluss.Schema(pa.schema([pa.field("a", pa.int32())])) | ||
| await admin.create_table(table_path, fluss.TableDescriptor(schema)) | ||
|
|
||
| table = await connection.get_table(table_path) | ||
|
|
||
| async with table.new_append().create_writer() as writer: | ||
| writer.append({"a": 1}) | ||
| writer.append({"a": 2}) | ||
| # No explicit flush here | ||
|
|
||
| # After context exit, data should be flushed | ||
| scanner = await table.new_scan().create_log_scanner() | ||
| scanner.subscribe(0, fluss.EARLIEST_OFFSET) | ||
| records = _poll_records(scanner, expected_count=2) | ||
| assert len(records) == 2 | ||
| assert sorted([r.row["a"] for r in records]) == [1, 2] | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_connection_drain_on_close(plaintext_bootstrap_servers, admin): | ||
| table_path = fluss.TablePath("fluss", "test_conn_drain") | ||
| await admin.drop_table(table_path, ignore_if_not_exists=True) | ||
| schema = fluss.Schema(pa.schema([pa.field("a", pa.int32())])) | ||
| await admin.create_table(table_path, fluss.TableDescriptor(schema)) | ||
|
|
||
| config = fluss.Config({"bootstrap.servers": plaintext_bootstrap_servers}) | ||
| async with await fluss.FlussConnection.create(config) as conn: | ||
| table = await conn.get_table(table_path) | ||
| writer = table.new_append().create_writer() | ||
| writer.append({"a": 123}) | ||
| # No explicit flush, no writer context exit. | ||
| # Rely on connection.__aexit__ -> close() to drain. | ||
|
|
||
| # Re-connect with a new connection to verify data arrived | ||
| async with await fluss.FlussConnection.create(config) as conn2: | ||
| table2 = await conn2.get_table(table_path) | ||
| scanner = await table2.new_scan().create_log_scanner() | ||
| scanner.subscribe(0, fluss.EARLIEST_OFFSET) | ||
| records = _poll_records(scanner, expected_count=1) | ||
| assert len(records) == 1 | ||
| assert records[0].row["a"] == 123 | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_upsert_writer_context_manager(connection, admin): | ||
| table_path = fluss.TablePath("fluss", "test_upsert_ctx") | ||
| await admin.drop_table(table_path, ignore_if_not_exists=True) | ||
|
|
||
| schema = fluss.Schema(pa.schema([pa.field("id", pa.int32()), pa.field("v", pa.string())]), primary_keys=["id"]) | ||
| await admin.create_table(table_path, fluss.TableDescriptor(schema)) | ||
|
|
||
| table = await connection.get_table(table_path) | ||
|
|
||
| # Success path: verify it flushes | ||
| async with table.new_upsert().create_writer() as writer: | ||
| writer.upsert({"id": 1, "v": "a"}) | ||
|
|
||
| lookuper = table.new_lookup().create_lookuper() | ||
| res = await lookuper.lookup({"id": 1}) | ||
| assert res is not None | ||
| assert res["v"] == "a" | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_connection_context_manager_exception(plaintext_bootstrap_servers): | ||
| config = fluss.Config({"bootstrap.servers": plaintext_bootstrap_servers}) | ||
| class TestException(Exception): pass | ||
|
|
||
| try: | ||
| async with await fluss.FlussConnection.create(config) as conn: | ||
| raise TestException("connection error") | ||
| except TestException: | ||
| pass | ||
| # If we reach here without hanging, the connection __aexit__ gracefully handled the error | ||
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.
why have we deleted close but left the rest?