PatersonProjects commented on code in PR #936:
URL: https://github.com/apache/burr/pull/936#discussion_r4212510475


##########
burr/integrations/persisters/b_aerospike.py:
##########
@@ -0,0 +1,1014 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import hashlib
+import json
+import random
+import sys
+import threading
+import time
+from collections import OrderedDict
+from datetime import datetime, timezone
+from typing import Any, Optional
+
+from burr.core import persistence, state
+from burr.integrations import base
+
+if sys.version_info < (3, 10):
+    raise ImportError(
+        "The Aerospike persister requires Python 3.10 or newer. "
+        "Install Burr with the 'aerospike' extra on a supported Python 
version."
+    )
+
+try:
+    import aerospike
+    import aerospike_helpers.expressions as expr
+    from aerospike_helpers.operations import map_operations as map_ops
+    from aerospike_helpers.operations import operations as aero_ops
+except ImportError as e:
+    base.require_plugin(e, "aerospike")
+
+_CODE_VERSION = 1
+_SYSTEM = "burr-as"
+
+# Aerospike bin names are capped at 15 characters.
+_PART_BIN = "partition"
+_PREFIX_BIN = "key_prefix"
+_APP_BIN = "app_id"
+_APPS_BIN = "app_ids"
+_TAIL_BIN = "tail_page"
+_MEMBERSHIP_BIN = "member_confirm"
+_SEQ_BIN = "sequence_id"
+_POS_BIN = "position"
+_STATE_BIN = "state"
+_STATUS_BIN = "status"
+_CREATED_BIN = "created_at"
+
+_MAX_WRITE_ATTEMPTS = 3
+_MAX_TAIL_HINTS = 128
+_MEMBERSHIP_BATCH_SIZE = 1000
+_RECORD_NOT_FOUND_CODE = 2
+_RETRYABLE_CODES = {9, -10, 7, 14}  # Timeout, Connection, ClusterChange, 
KEY_BUSY
+
+
+class AerospikePersistenceError(Exception):
+    """Base class for Aerospike persister errors."""
+
+
+class AerospikePersistenceConsistencyError(AerospikePersistenceError):
+    """Raised when stored data violates the persister's invariants."""
+
+
+class AerospikePersistenceSerializationError(AerospikePersistenceError, 
ValueError):
+    """Raised when state cannot be serialized to strict JSON."""
+
+
+class AerospikePersistenceUncertainOutcomeError(AerospikePersistenceError):
+    """Raised when a write retry budget is exhausted without a definitive 
result."""
+
+
+class AerospikeBasePersister(persistence.BaseStatePersister):
+    """Synchronous Aerospike-backed implementation of Burr's 
``BaseStatePersister``.
+
+    The persister stores one immutable history record per ``(partition_key,
+    app_id, sequence_id)`` and a small mutable head record per 
``(partition_key,
+    app_id)`` that points to the latest sequence. Application IDs within a
+    partition are listed through one materialized membership record per
+    logical partition.
+
+    This optional integration requires Python 3.10+ and the official Aerospike
+    Python client. Burr core remains compatible with Python 3.9+.
+    """
+
+    @classmethod
+    def from_config(cls, config: dict) -> "AerospikeBasePersister":
+        """Create a persister from a configuration dictionary."""
+        return cls.from_values(**config)
+
+    @classmethod
+    def from_values(
+        cls,
+        hosts: Optional[list] = None,
+        client_config: Optional[dict] = None,
+        namespace: str = "test",
+        history_set: str = "burr_state",
+        head_set: str = "burr_head",
+        membership_set: str = "burr_apps",
+        key_prefix: Optional[str] = "",
+        serde_kwargs: Optional[dict] = None,
+    ) -> "AerospikeBasePersister":
+        """Create a persister from seed hosts and client configuration.
+
+        :param hosts: Aerospike seed hosts as ``[(host, port), ...]``.
+        :param client_config: Additional official client configuration options.
+        :param namespace: Aerospike namespace.
+        :param history_set: Set for immutable checkpoint records.
+        :param head_set: Set for mutable latest-sequence heads.
+        :param membership_set: Set for per-partition application membership.
+        :param key_prefix: Optional logical prefix for key isolation.
+        :param serde_kwargs: Kwargs for Burr ``State`` serialization.
+        """
+        if hosts is None:
+            hosts = [("127.0.0.1", 3000)]
+        aerospike_config = {"hosts": hosts}
+        if client_config:
+            aerospike_config.update(client_config)
+
+        client = aerospike.client(aerospike_config)
+
+        return cls(
+            client,
+            namespace=namespace,
+            history_set=history_set,
+            head_set=head_set,
+            membership_set=membership_set,
+            key_prefix=key_prefix if key_prefix is not None else "",
+            serde_kwargs=serde_kwargs,
+            _client_config=aerospike_config,
+            _owned=True,
+        )
+
+    def __init__(
+        self,
+        client,
+        *,
+        namespace: str = "test",
+        history_set: str = "burr_state",
+        head_set: str = "burr_head",
+        membership_set: str = "burr_apps",
+        key_prefix: Optional[str] = "",
+        serde_kwargs: Optional[dict] = None,
+        _client_config: Optional[dict] = None,
+        _owned: bool = False,
+    ):
+        """Initialize the persister with an Aerospike client.
+
+        Direct construction accepts an already-connected caller-owned client.
+        Use :meth:`from_values` or :meth:`from_config` for persister-owned
+        clients.
+        """
+        self._client = client
+        self._owned = _owned
+        self.namespace = namespace
+        self.history_set = history_set
+        self.head_set = head_set
+        self.membership_set = membership_set
+        self.key_prefix = key_prefix if key_prefix is not None else ""
+        self.serde_kwargs = serde_kwargs or {}
+        self._client_config = _client_config
+        self._initialized = False
+        self._closed = False
+        self._tail_hints = OrderedDict()
+        self._tail_hints_lock = threading.Lock()
+
+    def __enter__(self):
+        return self
+
+    def __exit__(self, exc_type, exc_value, traceback):
+        self.cleanup()
+        return False
+
+    def is_async(self) -> bool:
+        return False
+
+    def cleanup(self):
+        """Close the persister-owned client, if any."""
+        if self._owned and not self._closed and self._client is not None:
+            self._client.close()
+            self._closed = True
+
+    def __getstate__(self) -> dict:
+        if not self._owned:
+            raise TypeError(
+                "An AerospikeBasePersister constructed with an injected client 
cannot be pickled"
+            )
+        if self._client_config is None:
+            raise TypeError(
+                "Cannot pickle an AerospikeBasePersister without reconnectable 
client configuration"
+            )
+        state = self.__dict__.copy()
+        del state["_client"]
+        del state["_tail_hints"]
+        del state["_tail_hints_lock"]
+        state["_initialized"] = False
+        state["_closed"] = False
+        return state
+
+    def __setstate__(self, state: dict):
+        client_config = state.get("_client_config")
+        if client_config is None:
+            raise TypeError(
+                "Cannot unpickle an AerospikeBasePersister without client 
configuration"
+            )
+        self.__dict__.update(state)
+        try:
+            self._client = aerospike.client(client_config)
+        except Exception as e:
+            raise AerospikePersistenceError(
+                f"Failed to reconnect Aerospike client from configuration: {e}"
+            ) from e
+        self._owned = True
+        self._initialized = False
+        self._closed = False
+        self._tail_hints = OrderedDict()
+        self._tail_hints_lock = threading.Lock()
+
+    def set_serde_kwargs(self, serde_kwargs: dict):
+        self.serde_kwargs = serde_kwargs
+
+    def initialize(self):
+        """Mark the dynamically provisioned persister ready for use."""
+        self._initialized = True
+
+    def is_initialized(self) -> bool:
+        return self._initialized
+
+    def save(
+        self,
+        partition_key: Optional[str],
+        app_id: str,
+        sequence_id: int,
+        position: str,
+        state: state.State,
+        status: str,
+        **kwargs,
+    ):
+        self._validate_sequence_id(sequence_id)
+
+        try:
+            state_json = json.dumps(state.serialize(**self.serde_kwargs), 
allow_nan=False)
+        except (TypeError, ValueError) as e:
+            raise AerospikePersistenceSerializationError(
+                f"State is not JSON-serializable: {e}"
+            ) from e
+        created_at = 
datetime.now(timezone.utc).isoformat(timespec="microseconds")
+
+        key_prefix = self.key_prefix
+        partition_canonical = self._canonical_partition(partition_key)
+        key_prefix_canonical = self._canonical_key_prefix()
+        membership_key = self._key(
+            self.membership_set,
+            self._derive_membership_key(key_prefix, partition_key),
+        )
+
+        history_key = self._key(
+            self.history_set,
+            self._derive_history_key(key_prefix, partition_key, app_id, 
sequence_id),
+        )
+        head_key = self._key(
+            self.head_set,
+            self._derive_head_key(key_prefix, partition_key, app_id),
+        )
+
+        history_bins = {
+            _PART_BIN: partition_canonical,
+            _PREFIX_BIN: key_prefix_canonical,
+            _APP_BIN: app_id,
+            _SEQ_BIN: sequence_id,
+            _POS_BIN: position,
+            _STATE_BIN: state_json,
+            _STATUS_BIN: status,
+            _CREATED_BIN: created_at,
+        }
+
+        # History must exist before the head can be advanced.
+        self._write_history(history_key, history_bins)
+
+        head_bins = {
+            _PART_BIN: partition_canonical,
+            _PREFIX_BIN: key_prefix_canonical,
+            _APP_BIN: app_id,
+            _SEQ_BIN: sequence_id,
+        }
+        _, membership_registered = self._advance_head(head_key, head_bins)
+        if membership_registered is not True:
+            self._register_membership(membership_key, partition_key, app_id)
+            self._confirm_membership(
+                head_key,
+                partition_canonical,
+                key_prefix_canonical,
+                app_id,
+            )
+
+    def load(
+        self,
+        partition_key: Optional[str],
+        app_id: Optional[str],
+        sequence_id: Optional[int] = None,
+        **kwargs,
+    ) -> Optional[persistence.PersistedStateData]:
+        if app_id is None:
+            raise ValueError("app_id is required for load")
+        if sequence_id is not None:
+            self._validate_sequence_id(sequence_id)
+
+        if sequence_id is None:
+            return self._load_latest(partition_key, app_id)
+
+        # Exact-sequence load.
+        key_prefix = self.key_prefix
+        partition_canonical = self._canonical_partition(partition_key)
+        key_prefix_canonical = self._canonical_key_prefix()
+        history_key = self._key(
+            self.history_set,
+            self._derive_history_key(key_prefix, partition_key, app_id, 
sequence_id),
+        )
+        history_record = self._read_history(history_key)
+        if history_record is None:
+            return None
+        self._validate_history_identity(
+            history_record, partition_canonical, key_prefix_canonical, app_id, 
sequence_id
+        )
+        return self._build_persisted_data(history_record, partition_key, 
app_id, sequence_id)
+
+    def _load_latest(
+        self,
+        partition_key: Optional[str],
+        app_id: str,
+    ) -> Optional[persistence.PersistedStateData]:
+        """Load the latest state for an application by reading the head, then 
the referenced history."""
+        key_prefix = self.key_prefix
+        partition_canonical = self._canonical_partition(partition_key)
+        key_prefix_canonical = self._canonical_key_prefix()
+        head_key = self._key(
+            self.head_set,
+            self._derive_head_key(key_prefix, partition_key, app_id),
+        )
+        head_record = self._read_head(head_key)
+        if head_record is None:
+            return None
+        stored_seq, _ = self._validate_head_identity(
+            head_record,
+            partition_canonical,
+            key_prefix_canonical,
+            app_id,
+        )
+        history_key = self._key(
+            self.history_set,
+            self._derive_history_key(key_prefix, partition_key, app_id, 
stored_seq),
+        )
+        history_record = self._read_history(history_key)
+        if history_record is None:
+            raise AerospikePersistenceConsistencyError(
+                f"Head for app_id={app_id} references missing history sequence 
{stored_seq}"
+            )
+        self._validate_history_identity(
+            history_record, partition_canonical, key_prefix_canonical, app_id, 
stored_seq
+        )
+        return self._build_persisted_data(history_record, partition_key, 
app_id, stored_seq)
+
+    def list_app_ids(self, partition_key: Optional[str], **kwargs) -> 
list[str]:
+        membership_key = self._membership_page_key(partition_key, 0)
+        root = self._read_membership_root(membership_key)
+        if root is None:
+            return []
+        app_ids, tail_page = root
+        if tail_page == 0:
+            return list(app_ids)
+        combined = set(app_ids)
+        for first_page in range(1, tail_page + 1, _MEMBERSHIP_BATCH_SIZE):
+            keys = [
+                self._membership_page_key(partition_key, page)
+                for page in range(
+                    first_page,
+                    min(first_page + _MEMBERSHIP_BATCH_SIZE, tail_page + 1),
+                )
+            ]
+            try:
+                results = self._client.batch_read(
+                    keys,
+                    bins=[_APPS_BIN],
+                    policy={"replica": aerospike.POLICY_REPLICA_MASTER},
+                )
+            except aerospike.exception.AerospikeError as e:
+                raise AerospikePersistenceError(f"Membership batch read 
failed: {e}") from e
+            membership_pages = results.batch_records
+            if len(membership_pages) != len(keys):
+                raise AerospikePersistenceConsistencyError("Membership batch 
read was incomplete")
+            for membership_page in membership_pages:
+                if membership_page.result == _RECORD_NOT_FOUND_CODE:
+                    continue
+                if membership_page.result != 0:
+                    raise AerospikePersistenceError(
+                        f"Membership batch entry failed with result code 
{membership_page.result}"
+                    )
+                if membership_page.record is None:
+                    raise AerospikePersistenceConsistencyError(
+                        "Successful membership batch entry lacks a record"
+                    )
+                
combined.update(self._validate_membership_map(membership_page.record[2]))
+        return list(combined)
+
+    def _canonical_json(self, value: Any) -> str:
+        """Deterministic JSON encoding used for canonical identities and state 
snapshots."""
+        return json.dumps(
+            value,
+            sort_keys=True,
+            separators=(",", ":"),
+            ensure_ascii=False,
+            allow_nan=False,
+        )
+
+    def _identity_history(
+        self, key_prefix: Optional[str], partition_key: Optional[str], app_id: 
str, sequence_id: int
+    ) -> list:
+        return [
+            _SYSTEM,
+            _CODE_VERSION,
+            "history",
+            key_prefix,
+            partition_key,
+            app_id,
+            sequence_id,
+        ]
+
+    def _identity_head(
+        self, key_prefix: Optional[str], partition_key: Optional[str], app_id: 
str
+    ) -> list:
+        return [
+            _SYSTEM,
+            _CODE_VERSION,
+            "head",
+            key_prefix,
+            partition_key,
+            app_id,
+        ]
+
+    def _identity_membership(self, key_prefix: Optional[str], partition_key: 
Optional[str]) -> list:
+        return [
+            _SYSTEM,
+            _CODE_VERSION,
+            "membership",
+            key_prefix,
+            partition_key,
+        ]
+
+    def _identity_membership_page(
+        self, key_prefix: Optional[str], partition_key: Optional[str], page: 
int
+    ) -> list:
+        return [
+            _SYSTEM,
+            _CODE_VERSION,
+            "membership-page",
+            key_prefix,
+            partition_key,
+            page,
+        ]
+
+    def _sha256_hex(self, data: str) -> str:
+        return hashlib.sha256(data.encode("utf-8")).hexdigest()
+
+    def _derive_history_key(
+        self, key_prefix: Optional[str], partition_key: Optional[str], app_id: 
str, sequence_id: int
+    ) -> str:
+        return self._sha256_hex(
+            self._canonical_json(
+                self._identity_history(key_prefix, partition_key, app_id, 
sequence_id)
+            )
+        )
+
+    def _derive_head_key(
+        self, key_prefix: Optional[str], partition_key: Optional[str], app_id: 
str
+    ) -> str:
+        return self._sha256_hex(
+            self._canonical_json(self._identity_head(key_prefix, 
partition_key, app_id))
+        )
+
+    def _derive_membership_key(
+        self, key_prefix: Optional[str], partition_key: Optional[str]
+    ) -> str:
+        return self._sha256_hex(
+            self._canonical_json(self._identity_membership(key_prefix, 
partition_key))
+        )
+
+    def _derive_membership_page_key(
+        self, key_prefix: Optional[str], partition_key: Optional[str], page: 
int
+    ) -> str:
+        if type(page) is not int or isinstance(page, bool) or page <= 0:
+            raise ValueError("membership overflow page must be a positive 
integer")
+        return self._sha256_hex(
+            self._canonical_json(self._identity_membership_page(key_prefix, 
partition_key, page))
+        )
+
+    def _membership_page_key(self, partition_key: Optional[str], page: int):
+        user_key = (
+            self._derive_membership_key(self.key_prefix, partition_key)
+            if page == 0
+            else self._derive_membership_page_key(self.key_prefix, 
partition_key, page)
+        )
+        return self._key(self.membership_set, user_key)
+
+    def _validate_sequence_id(self, sequence_id: Any) -> None:
+        if type(sequence_id) is not int or isinstance(sequence_id, bool):
+            raise ValueError(
+                f"sequence_id must be a 64-bit signed integer, got 
{sequence_id!r} (type {type(sequence_id).__name__})"
+            )
+        if sequence_id < -(2**63) or sequence_id > 2**63 - 1:
+            raise ValueError(
+                f"sequence_id {sequence_id} is outside the signed 64-bit 
integer range"
+            )
+
+    def _key(self, set_name: str, user_key: str):
+        return (self.namespace, set_name, user_key)
+
+    def _canonical_partition(self, partition_key: Optional[str]) -> str:
+        return self._canonical_json(partition_key)
+
+    def _canonical_key_prefix(self) -> str:
+        return self._canonical_json(self.key_prefix)
+
+    def _read_record(self, key, policy=None):
+        read_policy = policy or {"replica": aerospike.POLICY_REPLICA_MASTER}
+        try:
+            return self._client.get(key, policy=read_policy)
+        except aerospike.exception.RecordNotFound:
+            return None
+
+    def _is_retryable(self, exc: aerospike.exception.AerospikeError) -> bool:
+        return getattr(exc, "code", None) in _RETRYABLE_CODES
+
+    def _backoff(self, attempt: int) -> None:
+        # Bounded exponential backoff with jitter (seconds).
+        delay = min(0.05 * (2**attempt), 1.0)
+        time.sleep(delay * (0.5 + random.random()))
+
+    def _read_history(self, key):
+        record = self._read_record(key)
+        if record is None:
+            return None
+        _, _, bins = record
+        return bins
+
+    def _read_head(self, key):
+        return self._read_history(key)
+
+    def _validate_membership_map(self, bins: dict) -> dict:
+        app_ids = bins.get(_APPS_BIN)
+        if not isinstance(app_ids, dict):
+            raise AerospikePersistenceConsistencyError(
+                "Membership page lacks a usable application-ID map"
+            )
+        return app_ids
+
+    def _validate_tail_page(self, value: Any) -> int:
+        if type(value) is not int or isinstance(value, bool) or value < 0 or 
value > 2**63 - 1:
+            raise AerospikePersistenceConsistencyError(
+                "Membership root tail_page must be a non-negative signed 
integer"
+            )
+        return value
+
+    def _read_membership_page(self, key) -> Optional[dict]:
+        try:
+            _, _, bins = self._client.select(
+                key,
+                [_APPS_BIN],
+                policy={"replica": aerospike.POLICY_REPLICA_MASTER},
+            )
+            return self._validate_membership_map(bins)
+        except aerospike.exception.RecordNotFound:
+            return None
+        except aerospike.exception.AerospikeError as e:
+            raise AerospikePersistenceError(f"Membership read failed: {e}") 
from e
+
+    def _read_membership_root(self, key) -> Optional[tuple[dict, int]]:
+        try:
+            _, _, bins = self._client.select(
+                key,
+                [_APPS_BIN, _TAIL_BIN],
+                policy={"replica": aerospike.POLICY_REPLICA_MASTER},
+            )
+            return self._validate_membership_map(bins), 
self._validate_tail_page(
+                bins.get(_TAIL_BIN)
+            )
+        except aerospike.exception.RecordNotFound:
+            return None
+        except aerospike.exception.AerospikeError as e:
+            raise AerospikePersistenceError(f"Membership read failed: {e}") 
from e
+
+    def _advance_membership_tail(self, root_key, expected: int) -> int:
+        policy = {
+            "commit_level": aerospike.POLICY_COMMIT_LEVEL_ALL,
+            "expressions": expr.Eq(expr.IntBin(_TAIL_BIN), expected).compile(),
+            "ttl": aerospike.TTL_NEVER_EXPIRE,
+            "max_retries": 0,
+        }
+        operations = [aero_ops.write(_TAIL_BIN, expected + 1), 
aero_ops.read(_TAIL_BIN)]
+        for attempt in range(1, _MAX_WRITE_ATTEMPTS + 1):
+            try:
+                _, _, bins = self._client.operate(root_key, operations, 
policy=policy)
+                return self._validate_tail_page(bins[_TAIL_BIN])
+            except (aerospike.exception.FilteredOut, 
aerospike.exception.AerospikeError) as e:
+                if not isinstance(e, aerospike.exception.FilteredOut) and not 
self._is_retryable(e):
+                    raise AerospikePersistenceError(
+                        f"Membership tail allocation failed: {e}"
+                    ) from e
+                root = self._read_membership_root(root_key)
+                if root is not None and root[1] > expected:
+                    return root[1]
+                if attempt == _MAX_WRITE_ATTEMPTS:
+                    raise AerospikePersistenceUncertainOutcomeError(
+                        "Membership tail allocation failed with an uncertain 
outcome"
+                    ) from e
+                self._backoff(attempt)
+
+    def _put_membership_page(self, key, app_id: str, root: bool = False) -> 
None:
+        operations = [map_ops.map_put(_APPS_BIN, app_id, 1)]
+        policy = {
+            "commit_level": aerospike.POLICY_COMMIT_LEVEL_ALL,
+            "ttl": aerospike.TTL_NEVER_EXPIRE,
+            "max_retries": 0,
+        }
+        if root:
+            operations.insert(0, aero_ops.write(_TAIL_BIN, 0))
+            policy["expressions"] = expr.Or(
+                expr.Not(expr.BinExists(_TAIL_BIN)), 
expr.Eq(expr.IntBin(_TAIL_BIN), 0)
+            ).compile()
+        self._client.operate(key, operations, policy=policy)
+
+    def _get_tail_hint(self, membership_key) -> Optional[int]:
+        with self._tail_hints_lock:
+            tail_page = self._tail_hints.get(membership_key)
+            if tail_page is not None:
+                self._tail_hints.move_to_end(membership_key)
+            return tail_page
+
+    def _set_tail_hint(self, membership_key, tail_page: int) -> None:
+        if tail_page <= 0:
+            return
+        with self._tail_hints_lock:
+            existing = self._tail_hints.get(membership_key, 0)
+            self._tail_hints[membership_key] = max(existing, tail_page)
+            self._tail_hints.move_to_end(membership_key)
+            while len(self._tail_hints) > _MAX_TAIL_HINTS:
+                self._tail_hints.popitem(last=False)
+
+    def _register_membership(
+        self, membership_key, partition_key: Optional[str], app_id: str
+    ) -> None:
+        hinted_page = self._get_tail_hint(membership_key)
+        current_page = hinted_page if hinted_page is not None else 0
+        current_key = self._membership_page_key(partition_key, current_page)
+        uncertain_attempt = 1
+        rollovers = 0
+        while True:
+            try:
+                self._put_membership_page(current_key, app_id, 
root=current_page == 0)
+                self._set_tail_hint(membership_key, current_page)
+                return
+            except aerospike.exception.FilteredOut:
+                root = self._read_membership_root(membership_key)
+                if root is None:
+                    raise AerospikePersistenceConsistencyError(
+                        "Membership root disappeared after filtering"
+                    )
+                current_page = root[1]
+                self._set_tail_hint(membership_key, current_page)
+                current_key = self._membership_page_key(partition_key, 
current_page)
+                uncertain_attempt = 1
+            except aerospike.exception.RecordTooBig as e:
+                root = self._read_membership_root(membership_key)
+                if root is None:
+                    raise AerospikePersistenceConsistencyError(
+                        "Membership root disappeared during rollover"
+                    )
+                authoritative_tail = root[1]
+                self._set_tail_hint(membership_key, authoritative_tail)
+                if current_page < authoritative_tail:
+                    current_page = authoritative_tail
+                    current_key = self._membership_page_key(partition_key, 
current_page)
+                    uncertain_attempt = 1
+                    continue
+                rollovers += 1
+                if rollovers > _MAX_WRITE_ATTEMPTS:
+                    raise AerospikePersistenceUncertainOutcomeError(
+                        "Membership rollover retry budget exhausted"
+                    ) from e
+                current_page = self._advance_membership_tail(membership_key, 
authoritative_tail)
+                self._set_tail_hint(membership_key, current_page)
+                current_key = self._membership_page_key(partition_key, 
current_page)
+                uncertain_attempt = 1
+            except aerospike.exception.AerospikeError as e:
+                if not self._is_retryable(e):
+                    raise AerospikePersistenceError(f"Membership registration 
failed: {e}") from e
+                app_ids = self._read_membership_page(current_key)
+                if app_ids is not None and app_id in app_ids:
+                    self._set_tail_hint(membership_key, current_page)
+                    return
+                if uncertain_attempt == _MAX_WRITE_ATTEMPTS:
+                    raise AerospikePersistenceUncertainOutcomeError(
+                        "Membership registration failed with an uncertain 
outcome"
+                    ) from e
+                self._backoff(uncertain_attempt)
+                uncertain_attempt += 1
+
+    def _write_history_once(self, history_key, bins):
+        """Attempt one create-only history write."""
+        try:
+            self._client.put(
+                history_key,
+                bins,
+                policy={
+                    "commit_level": aerospike.POLICY_COMMIT_LEVEL_ALL,
+                    "exists": aerospike.POLICY_EXISTS_CREATE,
+                    "ttl": aerospike.TTL_NEVER_EXPIRE,
+                },
+            )
+        except aerospike.exception.RecordTooBig as e:
+            raise AerospikePersistenceSerializationError(
+                "Checkpoint record exceeds the namespace max-record-size"
+            ) from e
+
+    def _write_history(self, history_key, bins):
+        """Create-only history write with bounded retries."""
+        for attempt in range(1, _MAX_WRITE_ATTEMPTS + 1):
+            try:
+                self._write_history_once(history_key, bins)
+                return None
+            except aerospike.exception.RecordExistsError:
+                # The checkpoint already exists; history is immutable, so this 
is idempotent.
+                return None

Review Comment:
   Added _ensure_history_content_matches to check for different content on 
RecordExistsError, rather than just assuming retry



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to