From bef9ab5cb6fc85d2c664eb8a1144445edf24a6a2 Mon Sep 17 00:00:00 2001 From: Vlada Dusek Date: Wed, 25 Nov 2026 17:12:39 +0100 Subject: [PATCH] refactor: rename internal key-value store records --- src/apify/_charging.py | 2 +- .../_apify/_alias_resolving.py | 21 ++++++++--- .../storage_clients/test_alias_resolver.py | 37 ++++++++++++++++++- 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/src/apify/_charging.py b/src/apify/_charging.py index de9b5c95..8faa8f0e 100644 --- a/src/apify/_charging.py +++ b/src/apify/_charging.py @@ -43,7 +43,7 @@ DEFAULT_DATASET_ITEM_EVENT = 'apify-default-dataset-item' """Name of the synthetic event charged for each item pushed to the default dataset.""" -CHARGING_STATE_KEY = '__ACTOR_CHARGING_STATE' +CHARGING_STATE_KEY = 'APIFY_ACTOR_CHARGING_STATE' """Key of the default key-value store record that carries the charged event counts across a container restart.""" PricingModel = Literal['PAY_PER_EVENT', 'PRICE_PER_DATASET_ITEM', 'FLAT_PRICE_PER_MONTH', 'FREE'] diff --git a/src/apify/storage_clients/_apify/_alias_resolving.py b/src/apify/storage_clients/_apify/_alias_resolving.py index 29c38f56..7f619f46 100644 --- a/src/apify/storage_clients/_apify/_alias_resolving.py +++ b/src/apify/storage_clients/_apify/_alias_resolving.py @@ -128,9 +128,15 @@ class AliasResolver: Apify platform, the storages created with alias are accessible by the same alias even after migration or reboot. """ - _ALIAS_MAPPING_KEY = '__STORAGE_ALIASES_MAPPING' + _ALIAS_MAPPING_KEY = 'APIFY_STORAGE_ALIASES_MAPPING' """Key used for storing the alias mapping in the default kvs.""" + _LEGACY_ALIAS_MAPPING_KEY = '__STORAGE_ALIASES_MAPPING' + """Key used by older SDK versions, read when `_ALIAS_MAPPING_KEY` has no record. + + A metamorph keeps the default kvs, so the target Actor can find a mapping written by an older SDK version. + """ + _ALIAS_STORAGE_KEY_SEPARATOR = ',' """Separator used in the storage key for storing the alias mapping.""" @@ -194,8 +200,7 @@ async def _get_alias_map(self) -> dict[str, str]: if not AliasResolver._alias_map_loaded and self._configuration.is_at_home: default_kvs_client = self._get_default_kvs_client() - record = await default_kvs_client.get_record(self._ALIAS_MAPPING_KEY) - AliasResolver._alias_map = record.get('value', {}) if record else {} + AliasResolver._alias_map = await self._get_stored_alias_map(default_kvs_client) AliasResolver._alias_map_loaded = True return AliasResolver._alias_map @@ -235,8 +240,7 @@ async def store_mapping(self, storage_id: str) -> None: default_kvs_client = self._get_default_kvs_client() try: - record = await default_kvs_client.get_record(self._ALIAS_MAPPING_KEY) - value = record.get('value', {}) if record else {} + value = await self._get_stored_alias_map(default_kvs_client) value[self._storage_key] = storage_id # Store the mapping back in the KVS. @@ -272,6 +276,13 @@ def _credentials_hash(self) -> str: return compute_short_hash(f'{configuration.api_public_base_url}{identity}'.encode()) + async def _get_stored_alias_map(self, default_kvs_client: KeyValueStoreClientAsync) -> dict[str, str]: + """Read the alias mapping from the default kvs, from the legacy key when the current one has no record.""" + record = await default_kvs_client.get_record(self._ALIAS_MAPPING_KEY) + if record is None: + record = await default_kvs_client.get_record(self._LEGACY_ALIAS_MAPPING_KEY) + return record.get('value', {}) if record else {} + def _get_default_kvs_client(self) -> KeyValueStoreClientAsync: """Get a client for the default key-value store. diff --git a/tests/unit/storage_clients/test_alias_resolver.py b/tests/unit/storage_clients/test_alias_resolver.py index 2c2d3aac..4fd31e27 100644 --- a/tests/unit/storage_clients/test_alias_resolver.py +++ b/tests/unit/storage_clients/test_alias_resolver.py @@ -109,10 +109,45 @@ async def test_get_alias_map_loads_from_kvs_only_once_when_empty() -> None: await resolver._get_alias_map() await resolver._get_alias_map() - assert fake_kvs_client.get_record.await_count == 1 + # The first load reads the current key and then the legacy one. + assert fake_kvs_client.get_record.await_count == 2 assert AliasResolver._alias_map == {} +async def test_get_alias_map_reads_legacy_key() -> None: + """A mapping stored only under the legacy key is loaded.""" + config = Configuration(is_at_home=True, token='test-token', default_key_value_store_id='default-kvs-id') + resolver = AliasResolver(storage_type='Dataset', alias='test', configuration=config, api_client=_api_client()) + records = {'__STORAGE_ALIASES_MAPPING': {'key': '__STORAGE_ALIASES_MAPPING', 'value': {'legacy-key': 'legacy-id'}}} + + fake_kvs_client = AsyncMock() + fake_kvs_client.get_record = AsyncMock(side_effect=records.get) + + with patch.object(AliasResolver, '_get_default_kvs_client', return_value=fake_kvs_client): + result = await resolver._get_alias_map() + + assert result == {'legacy-key': 'legacy-id'} + + +async def test_store_mapping_moves_legacy_mapping_to_current_key() -> None: + """`store_mapping` writes the entries from the legacy key together with the new one under the current key.""" + config = Configuration(is_at_home=True, token='test-token', default_key_value_store_id='default-kvs-id') + resolver = AliasResolver(storage_type='Dataset', alias='test', configuration=config, api_client=_api_client()) + records = {'__STORAGE_ALIASES_MAPPING': {'key': '__STORAGE_ALIASES_MAPPING', 'value': {'legacy-key': 'legacy-id'}}} + + fake_kvs_client = AsyncMock() + fake_kvs_client.get_record = AsyncMock(side_effect=records.get) + fake_kvs_client.set_record = AsyncMock(return_value=None) + + with patch.object(AliasResolver, '_get_default_kvs_client', return_value=fake_kvs_client): + await resolver.store_mapping(storage_id='new-id') + + fake_kvs_client.set_record.assert_awaited_once_with( + key='APIFY_STORAGE_ALIASES_MAPPING', + value={'legacy-key': 'legacy-id', resolver._storage_key: 'new-id'}, + ) + + async def test_store_mapping_uses_injected_configuration_is_at_home() -> None: """`store_mapping` gates on the injected configuration's `is_at_home`, not the global one.""" # Global `is_at_home` defaults to False; injected config says True — the KVS write must still happen.