Skip to content
Draft
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
2 changes: 1 addition & 1 deletion src/apify/_charging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
21 changes: 16 additions & 5 deletions src/apify/storage_clients/_apify/_alias_resolving.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.

Expand Down
37 changes: 36 additions & 1 deletion tests/unit/storage_clients/test_alias_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading