Skip to content

Commit 5963d58

Browse files
rustyconoverclaude
andcommitted
storage: delete legacy worker_*/stream_state_*/aggregate_state_*/aggregate_window_partition_*/transaction_state_* (Turn 5)
Final turn of the storage refactor. The unified composite-key state_* API landed in earlier turns; every caller in vgi-python has been migrated. This turn rips the legacy method families out of: * FunctionStorage Protocol * FunctionStorageSqlite * FunctionStorageAzureSql (incl. worker_state schema) * FunctionStorageCfDo (HTTP client) * BoundStorage / TransactionBoundStorage facade Also migrates vgi/table_in_out_function.py — the SimpleDistributed shape's process()/finalize() pair was still calling BoundStorage.put/.collect, which were thin wrappers over worker_put/worker_collect. They now use state_put / state_drain under namespace b"sd", keyed by os.getpid(). The 4 SimpleDistributed test cases exercise this path and all pass. Test deletions: legacy worker_*/stream_state_*/aggregate_state_*/transaction_state_*/ aggregate_window_partition_* unit tests are removed across SQLite, Azure SQL, CfDo, CfDo-integration, and aggregate-function test files. Cleanup_old_entries sweeps shrink to the remaining table set. Test count drops by ~60 (deletions only); failure count unchanged vs pre-Turn-5 baseline. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 58d19ac commit 5963d58

9 files changed

Lines changed: 61 additions & 2242 deletions

‎tests/test_aggregate_function.py‎

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from __future__ import annotations
55

66
from dataclasses import dataclass
7-
from pathlib import Path
87
from typing import Annotated, Any
98

109
import pyarrow as pa
@@ -378,97 +377,6 @@ def finalize(
378377
assert StateExtractAgg.state_class is TwoFieldState # type: ignore[misc]
379378

380379

381-
# =========================================================================
382-
# Test Group 2: FunctionStorage aggregate methods
383-
# =========================================================================
384-
385-
386-
class TestFunctionStorageAggregate:
387-
"""Tests for FunctionStorageSqlite aggregate_state_get/put/clear."""
388-
389-
@pytest.fixture
390-
def storage(self, tmp_path: Path) -> Any:
391-
from vgi.function_storage import FunctionStorageSqlite
392-
393-
return FunctionStorageSqlite(str(tmp_path / "test_agg.db"))
394-
395-
def _exec_id(self) -> bytes:
396-
return b"test_execution_001"
397-
398-
def test_put_and_get(self, storage: Any) -> None:
399-
"""Store states and retrieve them; verify data matches."""
400-
eid = self._exec_id()
401-
storage.aggregate_state_put(eid, [(1, b"state_a"), (2, b"state_b"), (3, b"state_c")])
402-
403-
result = storage.aggregate_state_get(eid, [1, 2, 3])
404-
assert len(result) == 3
405-
assert result[0] == (1, b"state_a")
406-
assert result[1] == (2, b"state_b")
407-
assert result[2] == (3, b"state_c")
408-
409-
def test_get_missing_group_ids(self, storage: Any) -> None:
410-
"""Request group_ids not in storage returns None for those."""
411-
eid = self._exec_id()
412-
result = storage.aggregate_state_get(eid, [99, 100])
413-
assert result == [None, None]
414-
415-
def test_get_mixed(self, storage: Any) -> None:
416-
"""Some group_ids exist, some do not; correct mix of data and None."""
417-
eid = self._exec_id()
418-
storage.aggregate_state_put(eid, [(1, b"exists"), (3, b"also_exists")])
419-
420-
result = storage.aggregate_state_get(eid, [1, 2, 3, 4])
421-
assert result[0] == (1, b"exists")
422-
assert result[1] is None
423-
assert result[2] == (3, b"also_exists")
424-
assert result[3] is None
425-
426-
def test_get_empty_list(self, storage: Any) -> None:
427-
"""Empty group_ids returns empty list."""
428-
eid = self._exec_id()
429-
result = storage.aggregate_state_get(eid, [])
430-
assert result == []
431-
432-
def test_put_empty_list(self, storage: Any) -> None:
433-
"""Put with empty data does not error."""
434-
eid = self._exec_id()
435-
storage.aggregate_state_put(eid, [])
436-
# No error is the success condition
437-
438-
def test_clear(self, storage: Any) -> None:
439-
"""Put states, clear, then get returns None."""
440-
eid = self._exec_id()
441-
storage.aggregate_state_put(eid, [(1, b"data1"), (2, b"data2")])
442-
storage.aggregate_state_clear(eid)
443-
result = storage.aggregate_state_get(eid, [1, 2])
444-
assert result == [None, None]
445-
446-
def test_batching_exceeds_chunk_size(self, storage: Any) -> None:
447-
"""Put 600+ group_ids (exceeds 500 chunk size), get all back correctly."""
448-
eid = self._exec_id()
449-
count = 600
450-
data = [(i, f"state_{i}".encode()) for i in range(count)]
451-
storage.aggregate_state_put(eid, data)
452-
453-
group_ids = list(range(count))
454-
result = storage.aggregate_state_get(eid, group_ids)
455-
assert len(result) == count
456-
for i in range(count):
457-
assert result[i] is not None
458-
gid, state_bytes = result[i]
459-
assert gid == i
460-
assert state_bytes == f"state_{i}".encode()
461-
462-
def test_overwrite(self, storage: Any) -> None:
463-
"""Put a group_id twice with different data; get returns latest."""
464-
eid = self._exec_id()
465-
storage.aggregate_state_put(eid, [(1, b"old_data")])
466-
storage.aggregate_state_put(eid, [(1, b"new_data")])
467-
468-
result = storage.aggregate_state_get(eid, [1])
469-
assert result == [(1, b"new_data")]
470-
471-
472380
# =========================================================================
473381
# Test Group 3: Error paths
474382
# =========================================================================

‎tests/test_function_storage.py‎

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -16,65 +16,6 @@ def storage(self, tmp_path: Path) -> FunctionStorageSqlite:
1616
db_path = str(tmp_path / "test_storage.db")
1717
return FunctionStorageSqlite(db_path)
1818

19-
# --- Worker State Tests ---
20-
21-
def test_worker_put_and_collect(self, storage: FunctionStorageSqlite) -> None:
22-
"""Test storing and collecting worker states."""
23-
invocation_id = b"inv123"
24-
25-
# Store states from multiple workers
26-
storage.worker_put(invocation_id, worker_id=1, state=b"state1")
27-
storage.worker_put(invocation_id, worker_id=2, state=b"state2")
28-
storage.worker_put(invocation_id, worker_id=3, state=b"state3")
29-
30-
# Collect all states
31-
states = storage.worker_collect(invocation_id)
32-
33-
assert len(states) == 3
34-
assert set(states) == {b"state1", b"state2", b"state3"}
35-
36-
# Verify collect is atomic - second collect should return empty
37-
states2 = storage.worker_collect(invocation_id)
38-
assert states2 == []
39-
40-
def test_worker_put_replaces_existing(self, storage: FunctionStorageSqlite) -> None:
41-
"""Test that worker_put replaces existing state for same worker."""
42-
invocation_id = b"inv123"
43-
44-
storage.worker_put(invocation_id, worker_id=1, state=b"old_state")
45-
storage.worker_put(invocation_id, worker_id=1, state=b"new_state")
46-
47-
states = storage.worker_collect(invocation_id)
48-
assert states == [b"new_state"]
49-
50-
def test_worker_scan_non_destructive(self, storage: FunctionStorageSqlite) -> None:
51-
"""worker_scan returns (worker_id, state) pairs without deleting."""
52-
invocation_id = b"inv-scan"
53-
54-
storage.worker_put(invocation_id, worker_id=11, state=b"a")
55-
storage.worker_put(invocation_id, worker_id=22, state=b"b")
56-
57-
first = sorted(storage.worker_scan(invocation_id))
58-
assert first == [(11, b"a"), (22, b"b")]
59-
60-
# Repeat read returns the same data — non-destructive.
61-
second = sorted(storage.worker_scan(invocation_id))
62-
assert second == first
63-
64-
# And worker_collect after scan still drains.
65-
collected = sorted(storage.worker_collect(invocation_id))
66-
assert collected == [b"a", b"b"]
67-
assert storage.worker_scan(invocation_id) == []
68-
69-
def test_worker_scan_isolates_executions(self, storage: FunctionStorageSqlite) -> None:
70-
"""worker_scan only returns rows for the given execution_id."""
71-
storage.worker_put(b"exec-A", worker_id=1, state=b"a1")
72-
storage.worker_put(b"exec-B", worker_id=1, state=b"b1")
73-
74-
assert storage.worker_scan(b"exec-A") == [(1, b"a1")]
75-
assert storage.worker_scan(b"exec-B") == [(1, b"b1")]
76-
assert storage.worker_scan(b"exec-missing") == []
77-
7819
# --- Work Queue Tests ---
7920

8021
def test_queue_push_and_pop(self, storage: FunctionStorageSqlite) -> None:

‎tests/test_function_storage_azure_sql.py‎

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -87,53 +87,6 @@ def storage(mock_cursor: _MockCursor) -> FunctionStorageAzureSql:
8787
class TestFunctionStorageAzureSql:
8888
"""Tests for FunctionStorageAzureSql."""
8989

90-
# --- Worker State Tests ---
91-
92-
def test_worker_put_and_collect(self, storage: FunctionStorageAzureSql, mock_cursor: _MockCursor) -> None:
93-
"""Test storing and collecting worker states."""
94-
execution_id = b"\x01" * 16
95-
96-
# Put states from 3 workers
97-
storage.worker_put(execution_id, worker_id=1, state=b"state1")
98-
storage.worker_put(execution_id, worker_id=2, state=b"state2")
99-
storage.worker_put(execution_id, worker_id=3, state=b"state3")
100-
101-
# Verify MERGE was used (3 puts)
102-
merge_calls = [sql for sql, _ in mock_cursor.executed if "MERGE worker_state" in sql]
103-
assert len(merge_calls) == 3
104-
105-
# Simulate collect returning states
106-
mock_cursor.executed.clear()
107-
mock_cursor.set_results([(b"state1",), (b"state2",), (b"state3",)])
108-
109-
states = storage.worker_collect(execution_id)
110-
assert len(states) == 3
111-
assert set(states) == {b"state1", b"state2", b"state3"}
112-
113-
# Verify DELETE OUTPUT was used
114-
delete_calls = [sql for sql, _ in mock_cursor.executed if "DELETE FROM worker_state" in sql]
115-
assert len(delete_calls) == 1
116-
assert "OUTPUT deleted.state_data" in delete_calls[0]
117-
118-
def test_worker_put_replaces_existing(self, storage: FunctionStorageAzureSql, mock_cursor: _MockCursor) -> None:
119-
"""Test that worker_put replaces existing state for same worker."""
120-
execution_id = b"\x01" * 16
121-
122-
storage.worker_put(execution_id, worker_id=1, state=b"old_state")
123-
storage.worker_put(execution_id, worker_id=1, state=b"new_state")
124-
125-
# Both should use MERGE (upsert semantics)
126-
merge_calls = [sql for sql, _ in mock_cursor.executed if "MERGE worker_state" in sql]
127-
assert len(merge_calls) == 2
128-
# Second call has new_state
129-
assert mock_cursor.executed[-1][1] == (execution_id, 1, b"new_state")
130-
131-
def test_worker_collect_empty(self, storage: FunctionStorageAzureSql, mock_cursor: _MockCursor) -> None:
132-
"""Test collecting when no states exist."""
133-
mock_cursor.set_results([])
134-
states = storage.worker_collect(b"\x01" * 16)
135-
assert states == []
136-
13790
# --- Work Queue Tests ---
13891

13992
def test_queue_push_and_pop(self, storage: FunctionStorageAzureSql, mock_cursor: _MockCursor) -> None:
@@ -240,12 +193,12 @@ def test_cleanup_old_entries(self, storage: FunctionStorageAzureSql, mock_cursor
240193
deleted = storage.cleanup_old_entries(max_age_days=1.0)
241194
assert deleted >= 0
242195

243-
# Verify every state-bearing table was cleaned: legacy three plus
196+
# Verify every state-bearing table was cleaned: queue/registry plus
244197
# the unified state_* pair.
245198
delete_calls = [sql for sql, _ in mock_cursor.executed if "DELETE FROM" in sql]
246-
assert len(delete_calls) == 5
199+
assert len(delete_calls) == 4
247200
tables = {sql.split("DELETE FROM ")[1].split(" ")[0] for sql, _ in mock_cursor.executed if "DELETE FROM" in sql}
248-
assert tables == {"worker_state", "work_queue", "invocation_registry",
201+
assert tables == {"work_queue", "invocation_registry",
249202
"function_state", "function_state_log"}
250203

251204
# --- Factory Tests ---
@@ -301,7 +254,6 @@ def test_ensure_tables_creates_schema(self, mock_cursor: _MockCursor) -> None:
301254

302255
# Verify table creation SQL was executed
303256
sql_statements = [sql for sql, _ in mock_cursor.executed]
304-
assert any("worker_state" in sql and "CREATE TABLE" in sql for sql in sql_statements)
305257
assert any("work_queue" in sql and "CREATE TABLE" in sql for sql in sql_statements)
306258
assert any("invocation_registry" in sql and "CREATE TABLE" in sql for sql in sql_statements)
307259
assert any("idx_work_queue_execution" in sql for sql in sql_statements)
@@ -451,9 +403,3 @@ def test_execution_clear_wipes_both_tables(
451403
assert any("DELETE FROM function_state " in s for s in sqls)
452404
assert any("DELETE FROM function_state_log " in s for s in sqls)
453405

454-
def test_legacy_aggregate_state_get_now_raises_with_migration_hint(
455-
self, storage: FunctionStorageAzureSql,
456-
) -> None:
457-
"""Legacy aggregate_state_get raises with a clear migration message."""
458-
with pytest.raises(NotImplementedError, match="state_get_many"):
459-
storage.aggregate_state_get(b"e", [1, 2])

0 commit comments

Comments
 (0)