This is an automated email from the ASF dual-hosted git repository.

HyukjinKwon pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark-connect-rust.git


The following commit(s) were added to refs/heads/master by this push:
     new 873725d8 [MINOR][CONNECT][TESTS] Do not count the Deletes of the 
models of an ended ML offline test in a later test
873725d8 is described below

commit 873725d8d75ec179a69d8dca60fbba49ed1193f5
Author: Hyukjin Kwon <[email protected]>
AuthorDate: Fri Nov 13 15:36:36 2026 +0900

    [MINOR][CONNECT][TESTS] Do not count the Deletes of the models of an ended 
ML offline test in a later test
    
    ### What changes were proposed in this pull request?
    
    This fixes a flaky failure of the "Measure and publish coverage badges" 
job. The job failed twice on #615, a docs-only PR, and once on #620, each time 
in the same test:
    
    ```
    
test_ml_connect_logistic_regression_checks_its_labels_before_its_fit_as_pyspark
    >       assert len(service.execute_requests) == sent + 1, classes
    E       assert 3 == (1 + 1)
    ```
    
    The extra requests are Deletes of `model-1` from a session of an earlier 
test. A native model sends the Delete of its cached object through its session 
without waiting for it (`CachedModelRef::drop` in 
`crates/spark-connect/src/ml.rs`). So the Delete of a model of one test can 
reach the mock server while the next test runs, and a test that counts the 
exact requests that it sends then sees one too many. Some checks already skip 
Deletes (`_commands_but_deletes`), but most of the exact c [...]
    
    Instead of filtering each check, the mock server now drops these requests 
at the source:
    - `RemoteService.forget_session(session_id)`: from then on, the server 
still answers the ExecutePlanRequests of that session, but does not record 
them. It does count them, in `forgotten_requests`.
    - The `ml_mock` fixture forgets the session of a test when the test ends, 
before it stops the session.
    
    A model released after its session has stopped sends no Delete, so the late 
Deletes all come from sessions of ended tests, which are now forgotten. Other 
users of the mock server don't call `forget_session`, so nothing changes for 
them.
    
    ### Why are the changes needed?
    
    The coverage job runs the offline suites more slowly, so a late Delete 
reaches the next test more often there. Any PR could fail this job without a 
cause of its own.
    
    ### Does this PR introduce _any_ user-facing change?
    
    No. Tests only.
    
    ### How was this patch tested?
    
    - A new test, 
`test_the_deletes_of_the_models_of_an_ended_test_are_not_recorded_in_a_later_test`,
 releases a model of a forgotten session. It checks that the server answers the 
model's Delete without recording it, and that the requests of a live session 
are still recorded. The test fails with the server's filter disabled (`assert 0 
== 1` on `forgotten_requests`) and passes with it.
    - `tests/test_ml_offline.py` and `tests/test_dropin_offline.py`: 2123 
passed, 8 skipped. `ruff check` passes.
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    Generated-by: Claude Opus 5.5
    
    This pull request and its description were written by Isaac.
    
    Closes #623 from HyukjinKwon/MINOR-ml-offline-stray-deletes.
    
    Authored-by: Hyukjin Kwon <[email protected]>
    Signed-off-by: Hyukjin Kwon <[email protected]>
---
 python/tests/_mock_connect_server.py | 33 ++++++++++++++++++++++++++++-----
 python/tests/test_ml_offline.py      | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+), 5 deletions(-)

diff --git a/python/tests/_mock_connect_server.py 
b/python/tests/_mock_connect_server.py
index b08e8c4e..36c9d9f9 100644
--- a/python/tests/_mock_connect_server.py
+++ b/python/tests/_mock_connect_server.py
@@ -291,6 +291,9 @@ def serve(connection):
         def __init__(self):
             self.listener_condition = Condition()
             self.listener_epoch = 0
+            # The sessions whose ExecutePlanRequests are answered but no 
longer recorded (see
+            # RemoteService.forget_session). A reset keeps them.
+            self.forgotten_sessions = set()
 
         def reset(self):
             # Back to the state of a new server: the tests of a module share 
one.
@@ -308,6 +311,8 @@ def serve(connection):
             self.local = None
             self.local_failure = False
             self.execute_requests = []
+            # The ExecutePlanRequests of forgotten sessions that were answered.
+            self.forgotten_requests = 0
             self.release_execute_requests = []
             self.execute_payload = b""
             self.execute_payloads = None
@@ -575,18 +580,23 @@ def serve(connection):
                 return reached and epoch == self.listener_epoch and 
self.execute_started >= count
 
         def execute(self, request, context):
+            fields = parse_message(request)
+            # ExecutePlanRequest.session_id=1.
+            request_session = fields.get(1, [b""])[0].decode()
             with self.listener_condition:
                 # Capture these before yielding: an earlier call can resume 
after reset.
                 block_result_complete = self.block_result_complete
                 result_complete_operations = self.result_complete_operations
-                # Counter waiters on another thread must see both records when 
ready.
-                self.execute_requests.append(request)
-                self.rpc_events.append("execute")
-                self.execute_calls += 1
+                if request_session in self.forgotten_sessions:
+                    self.forgotten_requests += 1
+                else:
+                    # Counter waiters on another thread must see both records 
when ready.
+                    self.execute_requests.append(request)
+                    self.rpc_events.append("execute")
+                    self.execute_calls += 1
                 execute_epoch = self.listener_epoch
                 response_gate = self.response_gate
                 blocked = self.block_execute
-            fields = parse_message(request)
             plan = parse_message(fields.get(3, [b""])[0])
             command = parse_message(plan.get(2, [b""])[0])
             listener = parse_message(command.get(11, [b""])[0])
@@ -930,6 +940,10 @@ def serve(connection):
                 elif command[0] == "reset":
                     service.reset()
                     connection.send(None)
+                elif command[0] == "forget_session":
+                    with service.listener_condition:
+                        service.forgotten_sessions.add(command[1])
+                    connection.send(None)
                 elif command[0] == "listener_event":
                     _, session_id, event_type, event_json = command
                     with service.listener_condition:
@@ -1001,6 +1015,15 @@ class RemoteService:
         assert self.connection.poll(10), "Mock server did not respond"
         self.connection.recv()
 
+    def forget_session(self, session_id):
+        # From now on, the server answers the ExecutePlanRequests of the 
session but does not
+        # record them, for a test that ends with the session: a request that 
it sent without
+        # waiting for it, such as the Delete of a released model, can reach 
the server during a
+        # later test, which would count it as one of its own.
+        self.connection.send(("forget_session", session_id))
+        assert self.connection.poll(10), "Mock server did not respond"
+        self.connection.recv()
+
     def listener_event(self, session_id, event_type, event_json):
         self.listener_event_json(session_id, event_type, 
json.dumps(event_json))
 
diff --git a/python/tests/test_ml_offline.py b/python/tests/test_ml_offline.py
index bd3f47b6..52d575f2 100644
--- a/python/tests/test_ml_offline.py
+++ b/python/tests/test_ml_offline.py
@@ -63,6 +63,9 @@ def ml_mock(_ml_mock_server):
     service.reset()
     spark = SparkSession.builder.remote(service.remote).getOrCreate()
     yield spark, service
+    # A model sends its Delete without waiting for it, so the Delete of a 
model of this test can
+    # reach the server during a later test, which would count it as a request 
of its own.
+    service.forget_session(spark.session_id)
     spark.stop()
 
 
@@ -1391,6 +1394,38 @@ def 
test_native_models_released_while_their_session_stops_send_their_delete_as_p
     assert "model-released-once-stopped" not in deleted()
 
 
+def 
test_the_deletes_of_the_models_of_an_ended_test_are_not_recorded_in_a_later_test(ml_mock):
+    # The fixture forgets the session of a test when the test ends. A model 
sends its Delete
+    # without waiting for it, so the Delete of a model of the session can 
reach the server while
+    # a later test runs: the server answers it and does not record it, so the 
later test counts
+    # only its own requests.
+    import time
+
+    from pyspark.ml.connect import KMeans
+
+    spark, service = ml_mock
+    ended = SparkSession.builder.remote(service.remote).create()
+    try:
+        service.execute_payload = _operator_info("model-of-an-ended-test")
+        model = KMeans(k=2).fit(ended.range(1))
+        service.forget_session(ended.session_id)
+        service.reset()
+        del model
+        gc.collect()
+        deadline = time.time() + 5
+        while service.forgotten_requests == 0 and time.time() < deadline:
+            time.sleep(0.05)
+        assert service.forgotten_requests == 1
+        assert service.execute_requests == []
+        # The requests of a session that is not forgotten are still recorded.
+        service.execute_payload = _operator_info("a")
+        assert spark.client._delete_ml_cache(["a"]) == ["a"]
+        (command,) = _commands(service)
+        assert [ref.id for ref in command.ml_command.delete.obj_refs] == ["a"]
+    finally:
+        ended.stop()
+
+
 @pytest.mark.parametrize(
     "value, expected",
     [


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to