Skip to content
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

Search local shards directly to find shard to evict #791

Closed
wants to merge 1 commit into from
Closed
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
34 changes: 9 additions & 25 deletions streaming/base/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1087,31 +1087,15 @@ def _evict_coldest_shard(self) -> None:
This method is called internally by ``prepare_shard`` to clear space for more downloads.
"""
while True:
# Find the shard with the oldest last access time.
shard_id = int(self._shard_access_times.numpy().argmin())

# Check the shard's last access time. If it is NEVER, there are no downloaded shards to
# evict. If any shards are currently being downloaded, wait, else raise an error.
if self._shard_access_times[shard_id] == NEVER:
if (self._shard_states.numpy() == _ShardState.PREPARING).any():
sleep(TICK)
continue
else:
raise ValueError(
f'Tried to evict a shard {shard_id}, but no shards are present to evict ' +
f'(cache usage {self.cache_usage} of {self.cache_limit})')

# The shard has a valid timestamp. Now, verify that it is actually present. There is an
# edge case where it may not be present (see the note in get_item()). If not present,
# pick the next lowest shard.
if self._shard_states[shard_id] != _ShardState.LOCAL:
self._shard_access_times[shard_id] = NEVER
continue

# Break on success.
break

states = self._shard_states.numpy()
access_times = self._shard_access_times.numpy()
# Filter indices to include only local shards
indices = np.where(states == 3)[0]
snarayan21 marked this conversation as resolved.
Show resolved Hide resolved
if indices.size == 0:
raise ValueError('Could not evict because no local shards.')
local_times = access_times[indices]
# Find local shard with oldest last access time
shard_id = indices[np.argmin(local_times)]
# Evict that shard.
self._evict_shard(shard_id)

Expand Down
Loading