Class SyncMap<K,​V>

  • Type Parameters:
    V - The type of mapped values, serialized via FastMetaType * @see AutoFlushContext

    public abstract class SyncMap<K,​V>
    extends java.lang.Object
    A high-performance, decentralized distributed map providing strongly eventual consistency across the Aether cluster topology without relying on a central database bottleneck. *

    Instead of holding state data directly, SyncMap acts as a network orchestrator and conflict resolver layer over any local storage implementation (e.g., in-memory caches, RocksDB, or relational store) provided via functional hooks. *

    Core Architectural Pillars

    • Decentralized Ownership (Consistent Hashing): Leverages a ConsistentHashRing to deterministically map keys to specific authoritative cluster nodes ("owners"). Every node acts simultaneously as a master for a subset of keys and a replica for others.
    • *
    • Optimized Network Batching (Auto-Flush): By extending AutoFlushContext, outgoing RPC commands (such as SyncPut and SyncPropose) are not transmitted immediately as thousands of expensive network micro-packets. Instead, they are accumulated in a thread-safe buffer and flushed as a single packed binary chunk using remoteDataToArray(), minimizing system calls and bandwidth overhead.
    • *
    • Authoritative Ordering ("Owner-Wins"):
      • When a mutation happens on the owner node, it increments the data version globally, commits it locally, and broadcasts a SyncPut payload to all replica peers.
      • When a mutation happens on a replica (non-owner) node, it applies the update optimistically and dispatches a SyncPropose packet to the designated owner node. The owner evaluates the base version, linearizes the event, and either broadcasts a definitive SyncPut or issues a SyncReject if a race condition occurred.
    *

    Lifecycle & Usage Guidelines

    To integrate SyncMap into your cluster runtime, follow these setup steps:

    *
    
     // 1. Initialize local cache maps/hooks
     Map<String, VersionedValue<String>> cache = new ConcurrentHashMap<>();
     * // 2. Create the SyncMap instance
     SyncMap<String, String> syncMap = new SyncMap<>(
     serverId, scopeId, hashRing,
     FastMeta.META_STRING, FastMeta.META_STRING,
     cache::get, cache::put, dbLoader
     );
     * // 3. Cluster Discovery: Bind transport layers by passing nodes as MetaContexts
     syncMap.addPeer(peerServerId, networkTransportContext);
     * // 4. Perform mutations (Changes queue up internally into the AutoFlush layer)
     syncMap.notifyChanged("session_uuid_123", "worker_node_nodeB");
     syncMap.flush(); // Forces the batch out to the network layer immediately
     
    * @param The type of keys maintained by this map, serialized via FastMetaType
    See Also:
    ConsistentHashRing, SyncMapInternalApi
    • Field Detail

      • ALL_OWNERS

        protected static final int ALL_OWNERS
        Sentinel used by storage adapters to iterate over every owner.
        See Also:
        Constant Field Values
      • dbMaxSeqNoCache

        protected final java.util.Map<java.lang.Integer,​java.lang.Long> dbMaxSeqNoCache
      • onSendToPeer

        public io.aether.utils.interfaces.ABiConsumer<java.lang.Integer,​byte[]> onSendToPeer
    • Constructor Detail

      • SyncMap

        public SyncMap​(int mySid,
                       io.aether.net.fastMeta.FastMetaType<K> keyMeta,
                       io.aether.net.fastMeta.FastMetaType<V> valueMeta)
    • Method Detail

      • close

        public final void close()
      • restoreKnownSeqNo

        protected final void restoreKnownSeqNo​(int ownerSid,
                                               long seqNo)
        Restores durable sequence information loaded from storage. The local counter must never move backwards after a process restart.
      • merge

        protected V merge​(V existing,
                          V incoming)
        Простой вариант: пользователь работает только с V. По умолчанию incoming побеждает existing.
      • mergeContainers

        protected SyncContainer<V> mergeContainers​(SyncContainer<V> existing,
                                                   SyncContainer<V> incoming)
        Продвинутый вариант: пользователь работает с SyncContainer и может принимать решения на основе seqNo, ownerSid, isDeleted. По умолчанию делегирует простому merge.
      • dbGet

        protected abstract io.aether.utils.futures.ARFuture<SyncContainer<V>> dbGet​(K key)
      • dbPut

        protected abstract void dbPut​(K key,
                                      SyncContainer<V> container)
      • dbPutApplied

        protected boolean dbPutApplied​(K key,
                                       SyncContainer<V> container)
      • dbGetMaxSeqNo

        protected long dbGetMaxSeqNo​(int ownerSid)
      • storageGet

        public final io.aether.utils.futures.ARFuture<SyncContainer<V>> storageGet​(K key)
      • storagePut

        public final void storagePut​(K key,
                                     SyncContainer<V> container)
      • forEachStorageEntry

        protected abstract void forEachStorageEntry​(int ownerSid,
                                                    long sinceSeqNo,
                                                    java.util.function.BiConsumer<K,​SyncContainer<V>> consumer)
      • get

        public io.aether.utils.futures.ARFuture<V> get​(K key)
      • sendToPeer

        protected void sendToPeer​(int sid,
                                  byte[] data)
      • receiveFromPeer

        public void receiveFromPeer​(int sid,
                                    byte[] data)
      • persistAsync

        protected io.aether.utils.futures.ARFuture<java.lang.Boolean> persistAsync​(K key,
                                                                                   SyncContainer<V> container)
        Schedules persistence of one immutable version.

        Production adapters may override this hook with a bounded retry pipeline. The default preserves the existing asynchronous behavior while making synchronous adapter failures observable.

      • closePersistence

        protected void closePersistence()
        Lifecycle hook for persistence implementations with owned resources.
      • executeAsync

        protected void executeAsync​(io.aether.utils.interfaces.ARunnable task)
      • putRemote

        public void putRemote​(SyncPut data)
      • syncRequest

        public void syncRequest​(SyncRequest data)
      • sendReconciliationEntry

        protected void sendReconciliationEntry​(int peerSid,
                                               SyncPut data)
      • addPeer

        public void addPeer​(int serverId)
      • requestKey

        public void requestKey​(K key)
      • removePeer

        public void removePeer​(int serverId)
      • put

        public void put​(K key,
                        V newValue)
      • delete

        public void delete​(K key)
        Creates and replicates a durable tombstone for an existing key.

        The previous value is retained inside the container because value serializers are not required to support null. Public reads still return null for deleted containers.

      • requestFullReconciliation

        public final void requestFullReconciliation()
      • getData

        public final V getData​(K key)