org.apache.ignite.internal.processors.cache

## Class GridCacheMvcc

- java.lang.Object
- - org.apache.ignite.internal.processors.cache.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()` for information on how lock IDs are generated to prevent starvation.

### Constructor Summary

Constructors
Constructor and Description
`GridCacheMvcc(GridCacheContext<?,?> cctx)`

### Method Summary

Methods
`GridCacheMvccCandidate` `addLocal(GridCacheEntryEx parent, long threadId, GridCacheVersion ver, long timeout, boolean reenter, boolean tx, boolean implicitSingle)`
`GridCacheMvccCandidate` `addLocal(GridCacheEntryEx parent, UUID nearNodeId, GridCacheVersion nearVer, long threadId, GridCacheVersion ver, long timeout, boolean reenter, boolean tx, boolean implicitSingle, boolean dhtLoc)`
`GridCacheMvccCandidate` `addNearLocal(GridCacheEntryEx parent, UUID nodeId, UUID otherNodeId, long threadId, GridCacheVersion ver, long timeout, boolean tx, boolean implicitSingle)` Adds new near local lock candidate.
`GridCacheMvccCandidate` `addRemote(GridCacheEntryEx parent, UUID nodeId, UUID otherNodeId, long threadId, GridCacheVersion ver, long timeout, boolean tx, boolean implicitSingle, boolean nearLoc)` Adds new remote lock candidate (either near remote or dht remote).
`void` `addRemote(GridCacheMvccCandidate cand)`
`GridCacheMvccCandidate` `anyOwner()`
`GridCacheMvccCandidate` `candidate(GridCacheVersion ver)` Gets candidate for lock ID.
`GridCacheMvccCandidate` `doneRemote(GridCacheVersion ver, Collection<GridCacheVersion> pending, Collection<GridCacheVersion> committed, Collection<GridCacheVersion> rolledback)` Sets remote candidate to done.
`GridCacheMvccCandidate` `firstLocal()`
`GridCacheMvccCandidate` `firstRemote()`
`boolean` `hasCandidate(GridCacheVersion ver)`
`boolean` `isEmpty(GridCacheVersion... exclude)`
`boolean` `isLocallyOwned(GridCacheVersion lockVer)`
`boolean` `isLocallyOwnedByCurrentThread()`
`boolean` `isLocallyOwnedByIdOrThread(GridCacheVersion lockVer, long threadId)`
`boolean` `isLocallyOwnedByThread(long threadId, boolean allowDhtLoc, GridCacheVersion... exclude)`
`boolean` `isLockedByThread(long threadId, UUID nodeId)`
`boolean` `isOwnedBy(GridCacheVersion ver)`
`boolean` `isOwnedByAny()`
`GridCacheMvccCandidate` `localCandidate(long threadId)` Gets candidate for lock ID.
`GridCacheMvccCandidate` `localCandidate(UUID nodeId, long threadId)` Near local candidate.
`List<GridCacheMvccCandidate>` `localCandidates(boolean reentries, GridCacheVersion... excludeVers)`
`Collection<GridCacheMvccCandidate>` `localCandidates(GridCacheVersion... excludeVers)`
`List<GridCacheMvccCandidate>` `localCandidatesNoCopy(boolean reentry)`
`GridCacheMvccCandidate` `localOwner()`
`GridCacheMvccCandidate` `markOwned(GridCacheVersion baseVer, GridCacheVersion owned)` Puts owned versions in front of base.
`GridCacheMvccCandidate` `orderCompleted(GridCacheVersion baseVer, Collection<GridCacheVersion> committedVers, Collection<GridCacheVersion> rolledbackVers)` Moves completed candidates right before the base one.
`GridCacheMvccCandidate` `readyLocal(GridCacheMvccCandidate cand)`
`GridCacheMvccCandidate` `readyLocal(GridCacheVersion ver)`
`GridCacheMvccCandidate` `readyNearLocal(GridCacheVersion ver, GridCacheVersion mappedVer, Collection<GridCacheVersion> committedVers, Collection<GridCacheVersion> rolledBackVers, Collection<GridCacheVersion> pending)` Marks near-local candidate as ready and makes locks reassignment.
`GridCacheMvccCandidate` `recheck()` Checks if lock should be assigned.
`GridCacheMvccCandidate` `releaseLocal()` Local local release.
`GridCacheMvccCandidate` `releaseLocal(long threadId)` Local release.
`GridCacheMvccCandidate` `remoteCandidate(UUID nodeId, long threadId)`
`List<GridCacheMvccCandidate>` `remoteCandidates(GridCacheVersion... excludeVers)`
`GridCacheMvccCandidate` `remoteOwner()`
`GridCacheMvccCandidate` `remove(GridCacheVersion ver)` Removes lock even if it is not owner.
`GridCacheMvccCandidate` `removeExplicitNodeCandidates(UUID nodeId)` Removes all candidates for node.
`void` `salvageRemote(GridCacheVersion ver)` For all remote candidates standing behind the candidate being salvaged marks their transactions as system invalidate and marks these candidates as owned and used.
`String` `toString()`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Constructor Detail

#### GridCacheMvcc

    public GridCacheMvcc(GridCacheContext<?,?> cctx)

Parameters:
  `cctx` - Cache context.

### Method Detail

#### anyOwner

    @Nullable
    public GridCacheMvccCandidate anyOwner()

Returns:
  Any owner.

#### remoteOwner

    @Nullable
    public GridCacheMvccCandidate remoteOwner()

Returns:
  Remote candidate only if it's first in the list and is marked as `'used'`.

#### localOwner

    @Nullable
    public GridCacheMvccCandidate localOwner()

Returns:
  Local candidate only if it's first in the list and is marked as `'owner'`.

#### isEmpty

    public boolean isEmpty(GridCacheVersion... exclude)

Parameters:
  `exclude` - Versions to exclude form check.
Returns:
  `True` if lock is empty.

#### orderCompleted

    @Nullable
    public GridCacheMvccCandidate 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.
Returns:
  Lock owner.

#### markOwned

    @Nullable
    public GridCacheMvccCandidate markOwned(GridCacheVersion baseVer,
                                            GridCacheVersion owned)

Puts owned versions in front of base.

Parameters:
  `baseVer` - Base version.
  `owned` - Owned list.
Returns:
  Current owner.

#### addLocal

    @Nullable
    public GridCacheMvccCandidate addLocal(GridCacheEntryEx parent,
                                           long threadId,
                                           GridCacheVersion ver,
                                           long timeout,
                                           boolean reenter,
                                           boolean tx,
                                           boolean implicitSingle)

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.
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 GridCacheMvccCandidate addLocal(GridCacheEntryEx parent,
                                           @Nullable
                                           UUID nearNodeId,
                                           @Nullable
                                           GridCacheVersion nearVer,
                                           long threadId,
                                           GridCacheVersion ver,
                                           long timeout,
                                           boolean reenter,
                                           boolean tx,
                                           boolean implicitSingle,
                                           boolean dhtLoc)

Parameters:
  `parent` - Parent entry.
  `nearNodeId` - Near node ID.
  `nearVer` - Near version.
  `threadId` - Thread ID.
  `ver` - Lock version.
  `timeout` - Lock acquisition timeout.
  `reenter` - Reentry flag (`true` if reentry is allowed).
  `tx` - Transaction flag.
  `implicitSingle` - Implicit flag.
  `dhtLoc` - DHT local 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
                                   UUID otherNodeId,
                                   long threadId,
                                   GridCacheVersion ver,
                                   long timeout,
                                   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.
  `timeout` - Lock acquire timeout.
  `tx` - Transaction flag.
  `implicitSingle` - Implicit flag.
  `nearLoc` - Near local flag.
Returns:
  Add remote candidate.

#### addNearLocal

    public GridCacheMvccCandidate addNearLocal(GridCacheEntryEx parent,
                                      UUID nodeId,
                                      @Nullable
                                      UUID otherNodeId,
                                      long threadId,
                                      GridCacheVersion ver,
                                      long timeout,
                                      boolean tx,
                                      boolean implicitSingle)

Adds new near local lock candidate.

Parameters:
  `parent` - Parent entry.
  `nodeId` - Node ID.
  `otherNodeId` - Other node ID.
  `threadId` - Thread ID.
  `ver` - Lock version.
  `timeout` - Lock acquire timeout.
  `tx` - Transaction flag.
  `implicitSingle` - Implicit flag.
Returns:
  Add remote candidate.

#### addRemote

    public void addRemote(GridCacheMvccCandidate cand)

Parameters:
  `cand` - Remote candidate.

#### readyLocal

    @Nullable
    public GridCacheMvccCandidate readyLocal(GridCacheVersion ver)

Parameters:
  `ver` - Lock version to acquire or set to ready.
Returns:
  Current owner.

#### readyLocal

    @Nullable
    public GridCacheMvccCandidate readyLocal(GridCacheMvccCandidate cand)

Parameters:
  `cand` - Local candidate added in any of the `addLocal(..)` methods.
Returns:
  Current lock owner.

#### readyNearLocal

    @Nullable
    public GridCacheMvccCandidate 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.

#### doneRemote

    @Nullable
    public GridCacheMvccCandidate doneRemote(GridCacheVersion ver,
                                             Collection<GridCacheVersion> pending,
                                             Collection<GridCacheVersion> committed,
                                             Collection<GridCacheVersion> rolledback)

Sets remote candidate to done.

Parameters:
  `ver` - Version.
  `pending` - Pending versions.
  `committed` - Committed versions.
  `rolledback` - Rolledback versions.
Returns:
  Lock owner.

#### salvageRemote

    public void salvageRemote(GridCacheVersion ver)

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.

#### recheck

    @Nullable
    public GridCacheMvccCandidate recheck()

Checks if lock should be assigned.

Returns:
  Owner.

#### releaseLocal

    @Nullable
    public GridCacheMvccCandidate releaseLocal()

Local local release.

Returns:
  Removed lock candidate or `null` if candidate was not removed.

#### releaseLocal

    @Nullable
    public GridCacheMvccCandidate releaseLocal(long threadId)

Local release.

Parameters:
  `threadId` - ID of the thread.
Returns:
  Current owner.

#### remove

    @Nullable
    public GridCacheMvccCandidate remove(GridCacheVersion ver)

Removes lock even if it is not owner.

Parameters:
  `ver` - Lock version.
Returns:
  Current owner.

#### removeExplicitNodeCandidates

    @Nullable
    public GridCacheMvccCandidate removeExplicitNodeCandidates(UUID nodeId)

Removes all candidates for node.

Parameters:
  `nodeId` - Node ID.
Returns:
  Current owner.

#### candidate

    @Nullable
    public 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 GridCacheMvccCandidate localCandidate(long threadId)

Gets candidate for lock ID.

Parameters:
  `threadId` - Thread ID.
Returns:
  Candidate or `null` if there is no candidate for given ID.

#### remoteCandidate

    @Nullable
    public GridCacheMvccCandidate remoteCandidate(UUID nodeId,
                                                  long threadId)

Parameters:
  `nodeId` - Node ID.
  `threadId` - Thread ID.
Returns:
  Remote candidate.

#### localCandidate

    @Nullable
    public GridCacheMvccCandidate localCandidate(UUID nodeId,
                                                 long threadId)

Near local candidate.

Parameters:
  `nodeId` - Node ID.
  `threadId` - Thread ID.
Returns:
  Remote candidate.

#### hasCandidate

    public boolean hasCandidate(GridCacheVersion ver)

Parameters:
  `ver` - Version.
Returns:
  `True` if candidate with given version exists.

#### localCandidatesNoCopy

    public List<GridCacheMvccCandidate> localCandidatesNoCopy(boolean reentry)

Parameters:
  `reentry` - Reentry flag.
Returns:
  Collection of local candidates.

#### localCandidates

    public Collection<GridCacheMvccCandidate> localCandidates(GridCacheVersion... excludeVers)

Parameters:
  `excludeVers` - Exclude versions.
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.

#### remoteCandidates

    public List<GridCacheMvccCandidate> remoteCandidates(GridCacheVersion... excludeVers)

Parameters:
  `excludeVers` - Exclude versions.
Returns:
  Collection of remote candidates.

#### isLocallyOwnedByCurrentThread

    public boolean isLocallyOwnedByCurrentThread()

Returns:
  `True` if lock is owner by current thread.

#### isLocallyOwnedByThread

    public boolean isLocallyOwnedByThread(long threadId,
                                 boolean allowDhtLoc,
                                 GridCacheVersion... exclude)

Parameters:
  `threadId` - Thread ID to check.
  `exclude` - Versions to ignore.
Returns:
  `True` if lock is owned by the thread with given ID.

#### isLockedByThread

    public boolean isLockedByThread(long threadId,
                           UUID nodeId)

Parameters:
  `threadId` - Thread ID.
  `nodeId` - Node ID.
Returns:
  `True` if lock is held by given thread and node IDs.

#### isOwnedByAny

    public boolean isOwnedByAny()

Returns:
  `True` if lock is owned by any thread or node.

#### isLocallyOwned

    public boolean isLocallyOwned(GridCacheVersion lockVer)

Parameters:
  `lockVer` - ID of lock candidate.
Returns:
  `True` if candidate is owner.

#### isLocallyOwnedByIdOrThread

    public boolean isLocallyOwnedByIdOrThread(GridCacheVersion lockVer,
                                     long threadId)

Parameters:
  `lockVer` - Lock ID.
  `threadId` - Thread ID.
Returns:
  `True` if locked by lock ID or thread ID.

#### firstRemote

    @Nullable
    public GridCacheMvccCandidate firstRemote()

Returns:
  First remote entry or `null`.

#### firstLocal

    @Nullable
    public GridCacheMvccCandidate firstLocal()

Returns:
  First local entry or `null`.

#### isOwnedBy

    public boolean isOwnedBy(GridCacheVersion ver)

Parameters:
  `ver` - Version to check for ownership.
Returns:
  `True` if lock is owned by the specified version.

#### toString

    public String toString()

Overrides:
  `toString` in class `Object`

2015 Copyright © Apache Software Foundation

Follow @ApacheIgnite

Ignite Fabric | : ver. 1.3.0-incubating
Release Date | : July 10 2015