Skip to content

Commit 4729dd1

Browse files
authored
fix(redis): Preserve shared Redis index hashes when dropping a storage (#1942)
### Description `RedisClientMixin._drop` called `pipe.delete(f'{MAIN_KEY}:id_to_name', storage_id)` (and the same for `name_to_id`), but Redis `DEL` deletes whole top-level keys — it does not remove a field from a hash. Since `id_to_name` and `name_to_id` are shared hashes holding the mappings for every storage of a given type, dropping any single Dataset/KVS/RequestQueue wiped the entire index, breaking subsequent `open(id=...)` calls for all sibling storages (open-by-name kept working, since it reads metadata directly). This replaces the two `DEL` calls with `HDEL`, removing only the dropped storage's fields, and adds a regression test verifying that a sibling storage's index entries survive a drop and that it can still be opened by ID.
1 parent 5a7d72a commit 4729dd1

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

‎src/crawlee/storage_clients/_redis/_client_mixin.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ async def _create_metadata_and_storage(self, storage_name: str, metadata: dict)
220220
async def _drop(self, extra_keys: list[str]) -> None:
221221
async with self._get_pipeline() as pipe:
222222
await pipe.delete(self.metadata_key)
223-
await pipe.delete(f'{self._MAIN_KEY}:id_to_name', self._storage_id)
224-
await pipe.delete(f'{self._MAIN_KEY}:name_to_id', self._storage_name)
223+
await await_redis_response(pipe.hdel(f'{self._MAIN_KEY}:id_to_name', self._storage_id))
224+
await await_redis_response(pipe.hdel(f'{self._MAIN_KEY}:name_to_id', self._storage_name))
225225
await pipe.delete(f'{self._MAIN_KEY}:{self._storage_name}:created_signal')
226226
for key in extra_keys:
227227
await pipe.delete(key)

‎tests/unit/storage_clients/_redis/test_redis_dataset_client.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,31 @@ async def test_drop_removes_records(dataset_client: RedisDatasetClient) -> None:
113113
assert items_after_drop is None
114114

115115

116+
async def test_drop_preserves_sibling_index_entries(
117+
dataset_client: RedisDatasetClient,
118+
redis_client: FakeAsyncRedis,
119+
) -> None:
120+
"""Test that dropping a dataset does not wipe the shared index entries of other datasets."""
121+
storage_client = RedisStorageClient(redis=redis_client)
122+
sibling_client = await storage_client.create_dataset_client(name='sibling_dataset')
123+
sibling_metadata = await sibling_client.get_metadata()
124+
125+
await dataset_client.drop()
126+
127+
# The sibling's entries in the shared id_to_name and name_to_id hashes must remain intact.
128+
sibling_name = await await_redis_response(redis_client.hget('datasets:id_to_name', sibling_metadata.id))
129+
sibling_id = await await_redis_response(redis_client.hget('datasets:name_to_id', 'sibling_dataset'))
130+
131+
assert sibling_name is not None
132+
assert (sibling_name.decode() if isinstance(sibling_name, bytes) else sibling_name) == 'sibling_dataset'
133+
assert sibling_id is not None
134+
assert (sibling_id.decode() if isinstance(sibling_id, bytes) else sibling_id) == sibling_metadata.id
135+
136+
# Opening the sibling by ID must still work.
137+
reopened = await storage_client.create_dataset_client(id=sibling_metadata.id)
138+
assert (await reopened.get_metadata()).id == sibling_metadata.id
139+
140+
116141
async def test_metadata_record_updates(dataset_client: RedisDatasetClient) -> None:
117142
"""Test that metadata record is updated correctly after operations."""
118143
# Record initial timestamps

0 commit comments

Comments
 (0)