Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions tests/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,45 @@ def test_controller_clear_meta_idempotent(self, ray_setup):

print("✓ Clear meta idempotent correct")

def test_controller_empty_field_fetch_after_insert(self, ray_setup):
"""Empty-field fetch sees all inserted rows consistently before and after read-only queries."""
tq_controller = TransferQueueController.remote()
partition_id = "insert_growth_consistency"
schema = {"x": {"dtype": "torch.int64", "shape": (1,)}}

for _ in range(3):
meta = ray.get(tq_controller.get_metadata.remote(["x"], partition_id, mode="insert", batch_size=1))
assert ray.get(tq_controller.update_production_status.remote(partition_id, meta.global_indexes, schema))

consumed = ray.get(
tq_controller.get_metadata.remote(["x"], partition_id, mode="fetch", task_name="reader", batch_size=3)
)
assert consumed.global_indexes == [0, 1, 2]

inserted = ray.get(tq_controller.get_metadata.remote(["x"], partition_id, mode="insert", batch_size=2))
assert inserted.global_indexes == [3, 4]

visible_before = ray.get(tq_controller.scan_data_status.remote(partition_id, [], "reader"))
assert visible_before == [3, 4]

# Read-only status queries from another task must not change visibility or hit capacity bounds.
_, observer_consumption = ray.get(tq_controller.get_consumption_status.remote(partition_id, "observer"))
assert observer_consumption.tolist() == [0, 0, 0, 0, 0]
_, prod_status = ray.get(tq_controller.get_production_status.remote(partition_id, ["x"]))
assert prod_status.flatten().tolist() == [1, 1, 1, 0, 0]

visible_after = ray.get(tq_controller.scan_data_status.remote(partition_id, [], "reader"))
assert visible_after == visible_before

fetched = ray.get(
tq_controller.get_metadata.remote([], partition_id, mode="fetch", task_name="reader", batch_size=2)
)
assert fetched.global_indexes == [3, 4]
empty = ray.get(
tq_controller.get_metadata.remote([], partition_id, mode="fetch", task_name="reader", batch_size=1)
)
assert empty.global_indexes == []


class TestTransferQueueControllerCustomMeta:
"""Integration tests for TransferQueueController custom_meta and custom_backend_meta methods.
Expand Down
20 changes: 20 additions & 0 deletions tests/test_controller_data_partitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,26 @@ def test_cleared_partition_does_not_observe_reused_index_from_other_partition():
assert partition.scan_data_status(["x"], task_name="consumer_after_reuse") == []


def test_sample_capacity_grows_geometrically(monkeypatch):
from transfer_queue.controller import DataPartitionStatus

monkeypatch.setattr(DataPartitionStatus, "TQ_PRE_ALLOC_SAMPLE_NUM", 2)
partition = DataPartitionStatus(partition_id="growth")
schema = {"x": {"dtype": "torch.int64", "shape": (1,)}}
partition.update_production_status([0, 1], ["x"], schema)
partition.mark_consumed("reader", [1])

partition.update_production_status([2], ["x"], schema)
assert partition.allocated_samples_num == 4
assert partition.scan_data_status(["x"], "reader") == [0, 2]
assert partition.scan_data_status([], "reader") == [0, 2]

storage = partition.production_status
partition.update_production_status([3], ["x"], schema)
assert partition.production_status is storage
assert partition.scan_data_status(["x"], "reader") == [0, 2, 3]


def test_partition_interface():
"""Test the partition interface design."""
print("Testing partition interface design...")
Expand Down
44 changes: 19 additions & 25 deletions transfer_queue/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,8 @@ def ensure_samples_capacity(self, required_samples: int) -> None:

current_sample_space = self.allocated_samples_num
if required_samples > current_sample_space:
# Expand rows
expansion_needed = required_samples - current_sample_space
new_samples = current_sample_space + expansion_needed
# Leave room for incremental inserts so they do not copy every status tensor on each append.
new_samples = max(required_samples, 2 * max(current_sample_space, 1))
new_fields = self.production_status.shape[1]

expanded_tensor = torch.zeros(new_samples, new_fields, dtype=torch.int8)
Expand Down Expand Up @@ -817,32 +816,24 @@ def scan_data_status(self, field_names: list[str], task_name: str) -> list[int]:
if field_name not in self.field_name_mapping:
return []

row_mask = torch.ones(self.allocated_samples_num, dtype=torch.bool)
consumption_status = self.consumption_status.get(task_name)
if consumption_status is None:
consumption_status = torch.zeros(self.allocated_samples_num, dtype=torch.int8)
self.consumption_status[task_name] = consumption_status

# Apply consumption filter (exclude already consumed samples)
_, consumption_status = self.get_consumption_status(task_name, mask=False)
if consumption_status is not None:
unconsumed_mask = consumption_status == 0
row_mask &= unconsumed_mask

# Create column mask for requested fields
col_mask = torch.zeros(self.allocated_fields_num, dtype=torch.bool)
field_indices = [self.field_name_mapping[field] for field in field_names]
if field_indices:
col_mask[field_indices] = True
if not self.global_indexes:
return []

# Filter production status by masks
relevant_status = self.production_status[row_mask][:, col_mask]
# Scan active rows in index order so spare capacity never enters filtering or empty-field fetches.
active = torch.tensor(sorted(self.global_indexes), dtype=torch.long)
unconsumed = active[consumption_status[active] == 0]
if not field_names or unconsumed.numel() == 0:
return unconsumed.tolist()

# Check if all required fields are ready for each sample
field_indices = [self.field_name_mapping[field] for field in field_names]
relevant_status = self.production_status[unconsumed[:, None], field_indices]
all_fields_ready = torch.all(relevant_status, dim=1)
ready_indices_in_filtered = torch.nonzero(all_fields_ready, as_tuple=False).flatten()

# Map back to original sample indices
all_indices = torch.where(row_mask)[0]
ready_sample_indices = all_indices[ready_indices_in_filtered].tolist()

return ready_sample_indices
return unconsumed[all_fields_ready].tolist()

# ==================== Metadata Methods ====================
def get_field_schema(
Expand Down Expand Up @@ -1472,6 +1463,9 @@ def get_metadata(

# register global_indexes in partition
partition.global_indexes.update(batch_global_indexes)
if batch_global_indexes:
# Keep status tensors sized to all active rows before production is committed.
partition.ensure_samples_capacity(max(batch_global_indexes) + 1)

return self.generate_batch_meta(partition_id, batch_global_indexes, data_fields, mode)

Expand Down
Loading