Class GridCacheMvcc


  • public final class GridCacheMvcc
    extends Object
    Replicated lock based on MVCC paradigm. This class ensures that locks are acquired in proper order and that there is no more than only one active lock present at all times. It also ensures that new generated lock candidates will appear after old ones in the pending set, hence preventing lock starvation. See GridCacheVersionManager.next(long topVer) for information on how lock IDs are generated to prevent starvation.
    • Constructor Detail

      • GridCacheMvcc

        public GridCacheMvcc​(GridCacheContext<?,​?> cctx)
        Parameters:
        cctx - Cache context.
    • Method Detail

      • localOwners

        @Nullable
        public @Nullable CacheLockCandidates localOwners()
        Returns:
        All local owners.
      • isEmpty

        public boolean isEmpty​(GridCacheVersion... exclude)
        Parameters:
        exclude - Versions to exclude form check.
        Returns:
        True if lock is empty.
      • orderCompleted

        public void orderCompleted​(GridCacheVersion baseVer,
                                   Collection<GridCacheVersion> committedVers,
                                   Collection<GridCacheVersion> rolledbackVers)
        Moves completed candidates right before the base one. Note that if base is not found, then nothing happens and false is returned.
        Parameters:
        baseVer - Base version.
        committedVers - Committed versions relative to base.
        rolledbackVers - Rolled back versions relative to base.
      • markOwned

        public void markOwned​(GridCacheVersion baseVer,
                              GridCacheVersion owned)
        Puts owned versions in front of base.
        Parameters:
        baseVer - Base version.
        owned - Owned list.
      • addLocal

        @Nullable
        public @Nullable GridCacheMvccCandidate addLocal​(GridCacheEntryEx parent,
                                                         long threadId,
                                                         GridCacheVersion ver,
                                                         long timeout,
                                                         boolean reenter,
                                                         boolean tx,
                                                         boolean implicitSingle,
                                                         boolean read)
        Parameters:
        parent - Parent entry.
        threadId - Thread ID.
        ver - Lock version.
        timeout - Lock acquisition timeout.
        reenter - Reentry flag (true if reentry is allowed).
        tx - Transaction flag.
        implicitSingle - Implicit transaction flag.
        read - Read lock flag.
        Returns:
        New lock candidate if lock was added, or current owner if lock was reentered, or null if lock was owned by another thread and timeout is negative.
      • addLocal

        @Nullable
        public @Nullable GridCacheMvccCandidate addLocal​(GridCacheEntryEx parent,
                                                         @Nullable
                                                         @Nullable UUID nearNodeId,
                                                         @Nullable
                                                         @Nullable GridCacheVersion nearVer,
                                                         long threadId,
                                                         GridCacheVersion ver,
                                                         long timeout,
                                                         @Nullable
                                                         @Nullable GridCacheVersion serOrder,
                                                         boolean reenter,
                                                         boolean tx,
                                                         boolean implicitSingle,
                                                         boolean dhtLoc,
                                                         boolean read)
        Parameters:
        parent - Parent entry.
        nearNodeId - Near node ID.
        nearVer - Near version.
        threadId - Thread ID.
        ver - Lock version.
        timeout - Lock acquisition timeout.
        serOrder - Version for serializable transactions ordering.
        reenter - Reentry flag (true if reentry is allowed).
        tx - Transaction flag.
        implicitSingle - Implicit flag.
        dhtLoc - DHT local flag.
        read - Read lock flag.
        Returns:
        New lock candidate if lock was added, or current owner if lock was reentered, or null if lock was owned by another thread and timeout is negative.
      • addRemote

        public GridCacheMvccCandidate addRemote​(GridCacheEntryEx parent,
                                                UUID nodeId,
                                                @Nullable
                                                @Nullable UUID otherNodeId,
                                                long threadId,
                                                GridCacheVersion ver,
                                                boolean tx,
                                                boolean implicitSingle,
                                                boolean nearLoc)
        Adds new remote lock candidate (either near remote or dht remote).
        Parameters:
        parent - Parent entry.
        nodeId - Node ID.
        otherNodeId - Other node ID.
        threadId - Thread ID.
        ver - Lock version.
        tx - Transaction flag.
        implicitSingle - Implicit flag.
        nearLoc - Near local flag.
        Returns:
        Add remote candidate.
      • addNearLocal

        public GridCacheMvccCandidate addNearLocal​(GridCacheEntryEx parent,
                                                   UUID nodeId,
                                                   @Nullable
                                                   @Nullable UUID otherNodeId,
                                                   long threadId,
                                                   GridCacheVersion ver,
                                                   boolean tx,
                                                   boolean implicitSingle,
                                                   boolean read)
        Adds new near local lock candidate.
        Parameters:
        parent - Parent entry.
        nodeId - Node ID.
        otherNodeId - Other node ID.
        threadId - Thread ID.
        ver - Lock version.
        tx - Transaction flag.
        implicitSingle - Implicit flag.
        read - Read lock flag.
        Returns:
        Add remote candidate.
      • readyLocal

        @Nullable
        public @Nullable CacheLockCandidates readyLocal​(GridCacheVersion ver)
        Parameters:
        ver - Lock version to acquire or set to ready.
        Returns:
        Current owner.
      • readyLocal

        @Nullable
        public @Nullable CacheLockCandidates readyLocal​(GridCacheMvccCandidate cand)
        Parameters:
        cand - Local candidate added in any of the addLocal(..) methods.
        Returns:
        Current lock owner.
      • readyNearLocal

        @Nullable
        public @Nullable CacheLockCandidates readyNearLocal​(GridCacheVersion ver,
                                                            GridCacheVersion mappedVer,
                                                            Collection<GridCacheVersion> committedVers,
                                                            Collection<GridCacheVersion> rolledBackVers,
                                                            Collection<GridCacheVersion> pending)
        Marks near-local candidate as ready and makes locks reassignment. Following reorderings are performed when candidate is marked ready:
        • All candidates preceding ready one are moved right after it.
        • Near local candidate is assigned a mapped dht version. All remote non-pending candidates with version less then mapped dht version are marked as owned.
        Parameters:
        ver - Version to mark as ready.
        mappedVer - Mapped dht version.
        committedVers - Committed versions.
        rolledBackVers - Rolled back versions.
        pending - Pending dht versions that are not owned and which version is less then mapped.
        Returns:
        Lock owner after reassignment.
      • salvageRemote

        public void salvageRemote​(GridCacheVersion ver,
                                  boolean near)
        For all remote candidates standing behind the candidate being salvaged marks their transactions as system invalidate and marks these candidates as owned and used.
        Parameters:
        ver - Version to salvage.
        near - True If salvage near cache candidate.
      • recheck

        @Nullable
        public @Nullable CacheLockCandidates recheck()
        Checks if lock should be assigned.
        Returns:
        Owner.
      • releaseLocal

        @Nullable
        public @Nullable GridCacheMvccCandidate releaseLocal()
        Local local release.
        Returns:
        Removed candidate.
      • releaseLocal

        @Nullable
        public @Nullable GridCacheMvccCandidate releaseLocal​(long threadId)
        Local release.
        Parameters:
        threadId - ID of the thread.
        Returns:
        Removed candidate.
      • remove

        public void remove​(GridCacheVersion ver)
        Removes lock even if it is not owner.
        Parameters:
        ver - Lock version.
      • removeExplicitNodeCandidates

        @Nullable
        public @Nullable CacheLockCandidates removeExplicitNodeCandidates​(UUID nodeId)
        Removes all candidates for node.
        Parameters:
        nodeId - Node ID.
        Returns:
        Current owner.
      • candidate

        @Nullable
        public @Nullable GridCacheMvccCandidate candidate​(GridCacheVersion ver)
        Gets candidate for lock ID.
        Parameters:
        ver - Lock version.
        Returns:
        Candidate or null if there is no candidate for given ID.
      • localCandidate

        @Nullable
        public @Nullable GridCacheMvccCandidate localCandidate​(UUID nodeId,
                                                               long threadId)
        Local candidate.
        Parameters:
        nodeId - Node ID.
        threadId - Thread ID.
        Returns:
        Local candidate.
      • localCandidateByThreadOrVer

        @Nullable
        public @Nullable GridCacheMvccCandidate localCandidateByThreadOrVer​(UUID nodeId,
                                                                            long threadId,
                                                                            GridCacheVersion ver)
        Local candidate.
        Parameters:
        nodeId - Node ID.
        threadId - Thread ID.
        ver - Lock version.
        Returns:
        Local candidate.
      • localCandidatesNoCopy

        public List<GridCacheMvccCandidate> localCandidatesNoCopy​(boolean reentry)
        Parameters:
        reentry - Reentry flag.
        Returns:
        Collection of local candidates.
      • localCandidates

        public List<GridCacheMvccCandidate> localCandidates​(boolean reentries,
                                                            GridCacheVersion... excludeVers)
        Parameters:
        reentries - Flag to include reentries.
        excludeVers - Exclude versions.
        Returns:
        Collection of local candidates.