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
28 changes: 18 additions & 10 deletions python/ray/data/_internal/block_batching/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,22 +232,30 @@ def __init__(self):
self._thread.start()

def _run(self):
while True:
while not self._stopped:
try:
blocks_to_wait = []
with self._condition:
if len(self._blocks) > 0:
blocks_to_wait, self._blocks = self._blocks[:], []
else:
if self._stopped:
return
blocks_to_wait = []
if len(self._blocks) == 0:
# Park, waiting for notification that prefetching
# should resume
self._condition.wait()
if len(blocks_to_wait) > 0:
ray.wait(blocks_to_wait, num_returns=1, fetch_local=True)

blocks_to_fetch, self._blocks = self._blocks[:], []

if len(blocks_to_fetch) > 0:
ray.wait(
blocks_to_fetch,
num_returns=1,
# NOTE: We deliberately setting timeout to 0 to avoid
# blocking the fetching thread unnecessarily
timeout=0,
fetch_local=True,
)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Prefetcher Thread Continues After Stop

After self._condition.wait() returns, the prefetcher thread doesn't re-check self._stopped or self._blocks. This can cause it to call ray.wait() with an empty list or continue processing briefly after stop() is called, leading to unnecessary work and a race condition.

Fix in Cursor Fix in Web

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PTAL the cursor comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait will unblock only after we set the blocks, which will be taken at 243.

There's no issue here

except Exception:
logger.exception("Error in prefetcher thread.")

logger.info("Exiting prefetcher's background thread")

def prefetch_blocks(self, blocks: List[ObjectRef[Block]]):
with self._condition:
if self._stopped:
Expand Down
51 changes: 44 additions & 7 deletions python/ray/data/_internal/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,17 +278,45 @@ def __init__(self, max_stats=1000):
self.per_node_metrics = self._create_prometheus_metrics_for_per_node_metrics()

iter_tag_keys = ("dataset",)
self.iter_total_blocked_s = Gauge(
"data_iter_total_blocked_seconds",
description="Seconds user thread is blocked by iter_batches()",
tag_keys=iter_tag_keys,
)

self.time_to_first_batch_s = Gauge(
"data_iter_time_to_first_batch_seconds",
description="Total time spent waiting for the first batch after starting iteration. "
"This includes the dataset pipeline warmup time. This metric is accumulated across different epochs.",
tag_keys=iter_tag_keys,
)

self.iter_block_fetching_s = Gauge(
"data_iter_block_fetching_seconds",
description="Seconds taken to fetch (with ray.get) blocks by iter_batches()",
tag_keys=iter_tag_keys,
)
self.iter_batch_shaping_s = Gauge(
"data_iter_batch_shaping_seconds",
description="Seconds taken to shape batch from incoming blocks by iter_batches()",
tag_keys=iter_tag_keys,
)
self.iter_batch_formatting_s = Gauge(
"data_iter_batch_formatting_seconds",
description="Seconds taken to format batches by iter_batches()",
tag_keys=iter_tag_keys,
)
self.iter_batch_collating_s = Gauge(
"data_iter_batch_collating_seconds",
description="Seconds taken to collate batches by iter_batches()",
tag_keys=iter_tag_keys,
)
self.iter_batch_finalizing_s = Gauge(
"data_iter_batch_finalizing_seconds",
description="Seconds taken to collate batches by iter_batches()",
tag_keys=iter_tag_keys,
)

self.iter_total_blocked_s = Gauge(
"data_iter_total_blocked_seconds",
description="Seconds user thread is blocked by iter_batches()",
tag_keys=iter_tag_keys,
)
self.iter_user_s = Gauge(
"data_iter_user_seconds",
description="Seconds spent in user code",
Expand Down Expand Up @@ -477,10 +505,19 @@ def update_iteration_metrics(
dataset_tag,
):
tags = self._create_tags(dataset_tag)
self.iter_total_blocked_s.set(stats.iter_total_blocked_s.get(), tags)

self.iter_initialize_s.set(stats.iter_initialize_s.get(), tags)

self.iter_block_fetching_s.set(stats.iter_get_s.get(), tags)
self.iter_batch_shaping_s.set(stats.iter_next_batch_s.get(), tags)
self.iter_batch_formatting_s.set(stats.iter_format_batch_s.get(), tags)
self.iter_batch_collating_s.set(stats.iter_collate_batch_s.get(), tags)
self.iter_batch_finalizing_s.set(stats.iter_finalize_batch_s.get(), tags)

self.time_to_first_batch_s.set(stats.iter_time_to_first_batch_s.get(), tags)

self.iter_total_blocked_s.set(stats.iter_total_blocked_s.get(), tags)
self.iter_user_s.set(stats.iter_user_s.get(), tags)
self.iter_initialize_s.set(stats.iter_initialize_s.get(), tags)

def register_dataset(
self,
Expand Down