You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by ag...@apache.org on 2018/08/30 12:24:36 UTC
[30/45] ignite git commit: IGNITE-4191: MVCC and transactional SQL
support. Joint multi-man-years efforts of Semen Boikov, Igor Seliverstov,
Alexander Paschenko, Igor Sapego, Sergey Kalashnikov, Roman Kondakov,
Pavel Kuznetsov, Ivan Pavlukhin, Andrey Mas
http://git-wip-us.apache.org/repos/asf/ignite/blob/00bb4d4d/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearTxSelectForUpdateFuture.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearTxSelectForUpdateFuture.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearTxSelectForUpdateFuture.java
new file mode 100644
index 0000000..a5ab7cd
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearTxSelectForUpdateFuture.java
@@ -0,0 +1,462 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.internal.processors.cache.distributed.near;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
+import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.internal.IgniteInternalFuture;
+import org.apache.ignite.internal.cluster.ClusterTopologyCheckedException;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cache.GridCacheCompoundIdentityFuture;
+import org.apache.ignite.internal.processors.cache.GridCacheContext;
+import org.apache.ignite.internal.processors.cache.GridCacheEntryEx;
+import org.apache.ignite.internal.processors.cache.GridCacheMvccCandidate;
+import org.apache.ignite.internal.processors.cache.GridCacheVersionedFuture;
+import org.apache.ignite.internal.processors.cache.distributed.GridDistributedTxMapping;
+import org.apache.ignite.internal.processors.cache.version.GridCacheVersion;
+import org.apache.ignite.internal.processors.timeout.GridTimeoutObjectAdapter;
+import org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException;
+import org.apache.ignite.internal.util.future.GridFutureAdapter;
+import org.apache.ignite.internal.util.tostring.GridToStringExclude;
+import org.apache.ignite.internal.util.typedef.X;
+import org.apache.ignite.internal.util.typedef.internal.CU;
+import org.apache.ignite.internal.util.typedef.internal.S;
+import org.apache.ignite.lang.IgniteUuid;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * A future tracking requests for remote nodes transaction enlisting and locking
+ * of entries produced with complex DML queries requiring reduce step.
+ */
+public class GridNearTxSelectForUpdateFuture extends GridCacheCompoundIdentityFuture<Long>
+ implements GridCacheVersionedFuture<Long> {
+ /** */
+ private static final long serialVersionUID = 6931664882548658420L;
+
+ /** Done field updater. */
+ private static final AtomicIntegerFieldUpdater<GridNearTxSelectForUpdateFuture> DONE_UPD =
+ AtomicIntegerFieldUpdater.newUpdater(GridNearTxSelectForUpdateFuture.class, "done");
+
+ /** Done field updater. */
+ private static final AtomicReferenceFieldUpdater<GridNearTxSelectForUpdateFuture, Throwable> EX_UPD =
+ AtomicReferenceFieldUpdater.newUpdater(GridNearTxSelectForUpdateFuture.class, Throwable.class, "ex");
+
+ /** */
+ @SuppressWarnings("unused")
+ @GridToStringExclude
+ private volatile int done;
+
+ /** */
+ @SuppressWarnings("unused")
+ @GridToStringExclude
+ private volatile Throwable ex;
+
+ /** Cache context. */
+ @GridToStringExclude
+ private final GridCacheContext<?, ?> cctx;
+
+ /** Transaction. */
+ private final GridNearTxLocal tx;
+
+ /** Mvcc future id. */
+ private final IgniteUuid futId;
+
+ /** Lock version. */
+ private final GridCacheVersion lockVer;
+
+ /** */
+ private AffinityTopologyVersion topVer;
+
+ /** */
+ private final long timeout;
+
+ /** Logger. */
+ @GridToStringExclude
+ private final IgniteLogger log;
+
+ /** Timeout object. */
+ @GridToStringExclude
+ private LockTimeoutObject timeoutObj;
+
+ /** Ids of mini futures. */
+ private final Map<UUID, Integer> miniFutIds = new HashMap<>();
+
+ /**
+ * @param cctx Cache context.
+ * @param tx Transaction.
+ * @param timeout Timeout.
+ */
+ public GridNearTxSelectForUpdateFuture(GridCacheContext<?, ?> cctx,
+ GridNearTxLocal tx,
+ long timeout) {
+ super(CU.longReducer());
+
+ this.cctx = cctx;
+ this.tx = tx;
+ this.timeout = timeout;
+
+ futId = IgniteUuid.randomUuid();
+ lockVer = tx.xidVersion();
+
+ log = cctx.logger(GridNearTxSelectForUpdateFuture.class);
+ }
+
+ /**
+ * @return Cache context.
+ */
+ public GridCacheContext<?, ?> cache() {
+ return cctx;
+ }
+
+ /**
+ * @param node Node.
+ */
+ private void map(ClusterNode node) {
+ GridDistributedTxMapping mapping = tx.mappings().get(node.id());
+
+ if (mapping == null)
+ tx.mappings().put(mapping = new GridDistributedTxMapping(node));
+
+ mapping.markQueryUpdate();
+
+ if (node.isLocal())
+ tx.colocatedLocallyMapped(true);
+
+ int futId = futuresCountNoLock();
+
+ miniFutIds.put(node.id(), futId);
+
+ add(new NodeFuture(node));
+ }
+
+ /**
+ * Process result of query execution on given
+ * @param nodeId Node id.
+ * @param cnt Total rows counter on given node.
+ * @param removeMapping Whether transaction mapping should be removed for node.
+ * @param err Error.
+ */
+ public void onResult(UUID nodeId, Long cnt, boolean removeMapping, @Nullable Throwable err) {
+ NodeFuture nodeFut = mapFuture(nodeId);
+
+ if (nodeFut != null)
+ nodeFut.onResult(cnt, removeMapping, err);
+ }
+
+ /** {@inheritDoc} */
+ @Override protected boolean processFailure(Throwable err, IgniteInternalFuture<Long> fut) {
+ if (ex != null || !EX_UPD.compareAndSet(this, null, err))
+ ex.addSuppressed(err);
+
+ return true;
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean onDone(@Nullable Long res, @Nullable Throwable err, boolean cancelled) {
+ if (!DONE_UPD.compareAndSet(this, 0, 1))
+ return false;
+
+ cctx.tm().txContext(tx);
+
+ Throwable ex0 = ex;
+
+ if (ex0 != null) {
+ if (err != null)
+ ex0.addSuppressed(err);
+
+ err = ex0;
+ }
+
+ if (!cancelled && err == null)
+ tx.clearLockFuture(this);
+ else
+ tx.setRollbackOnly();
+
+ boolean done = super.onDone(res, err, cancelled);
+
+ assert done;
+
+ // Clean up.
+ cctx.mvcc().removeVersionedFuture(this);
+
+ if (timeoutObj != null)
+ cctx.time().removeTimeoutObject(timeoutObj);
+
+ return true;
+ }
+
+ /**
+ * Finds pending map node future by the given ID.
+ *
+ * @param nodeId Node id.
+ * @return Map node future.
+ */
+ private NodeFuture mapFuture(UUID nodeId) {
+ synchronized (this) {
+ Integer idx = miniFutIds.get(nodeId);
+
+ if (idx == null)
+ throw new IllegalStateException("SELECT FOR UPDATE node future not found [nodeId=" + nodeId + "].");
+
+ assert idx >= 0 && idx < futuresCountNoLock();
+
+ IgniteInternalFuture<Long> fut = future(idx);
+
+ if (!fut.isDone())
+ return (NodeFuture)fut;
+ }
+
+ return null;
+ }
+
+ /** {@inheritDoc} */
+ @Override public GridCacheVersion version() {
+ return lockVer;
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean onOwnerChanged(GridCacheEntryEx entry, GridCacheMvccCandidate owner) {
+ return false;
+ }
+
+ /** {@inheritDoc} */
+ @Override public IgniteUuid futureId() {
+ return futId;
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean onNodeLeft(UUID nodeId) {
+ if (topVer == null)
+ return false; // Local query, do nothing.
+
+ for (IgniteInternalFuture<?> fut : futures()) {
+ NodeFuture f = (NodeFuture)fut;
+
+ if (f.node.id().equals(nodeId)) {
+ if (log.isDebugEnabled())
+ log.debug("Found mini-future for left node [nodeId=" + nodeId + ", mini=" + f + ", fut=" +
+ this + ']');
+
+ ClusterTopologyCheckedException topEx = new ClusterTopologyCheckedException("Failed to enlist keys " +
+ "(primary node left grid, retry transaction if possible) [node=" + nodeId + ']');
+
+ topEx.retryReadyFuture(cctx.shared().nextAffinityReadyFuture(topVer));
+
+ return f.onResult(0, false, topEx);
+ }
+ }
+
+ if (log.isDebugEnabled())
+ log.debug("Future does not have mapping for left node (ignoring) [nodeId=" + nodeId +
+ ", fut=" + this + ']');
+
+ return false;
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean trackable() {
+ return true;
+ }
+
+ /** {@inheritDoc} */
+ @Override public void markNotTrackable() {
+ // No-op.
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void logError(IgniteLogger log, String msg, Throwable e) {
+ // no-op
+ }
+
+ /** {@inheritDoc} */
+ @Override protected void logDebug(IgniteLogger log, String msg) {
+ // no-op
+ }
+
+ /** {@inheritDoc} */
+ @Override public String toString() {
+ return S.toString(GridNearTxSelectForUpdateFuture.class, this, super.toString());
+ }
+
+ /**
+ * Initialize this future for distributed execution.
+ * @param topVer Topology version.
+ * @param nodes Nodes to run query on.
+ */
+ public synchronized void init(AffinityTopologyVersion topVer, Collection<ClusterNode> nodes) {
+ doInit(topVer, nodes, false);
+ }
+
+ /**
+ * Initialize this future for local execution.
+ */
+ public synchronized void initLocal() {
+ doInit(null, Collections.singletonList(cctx.localNode()), true);
+ }
+
+ /**
+ * Initialize this future for distributed or local execution.
+ * @param topVer Topology version ({@code null} for local case).
+ * @param nodes Nodes to run query on.
+ * @param loc Local query flag.
+ */
+ private void doInit(@Nullable AffinityTopologyVersion topVer, Collection<ClusterNode> nodes, boolean loc) {
+ assert !loc || (topVer == null && nodes.size() == 1 && nodes.iterator().next().isLocal());
+ if (initialized())
+ throw new IllegalStateException("SELECT FOR UPDATE future has been initialized already.");
+
+ tx.init();
+
+ if (timeout < 0) {
+ // Time is out.
+ onDone(timeoutException());
+
+ return;
+ }
+ else if (timeout > 0)
+ timeoutObj = new LockTimeoutObject();
+
+ if (!tx.updateLockFuture(null, this)) {
+ onDone(tx.timedOut() ? tx.timeoutException() : tx.rollbackException());
+
+ return;
+ }
+
+ boolean added = cctx.mvcc().addFuture(this);
+
+ assert added : this;
+
+ try {
+ tx.addActiveCache(cctx, false);
+ }
+ catch (IgniteCheckedException e) {
+ onDone(e);
+
+ return;
+ }
+
+ if (timeoutObj != null)
+ cctx.time().addTimeoutObject(timeoutObj);
+
+ this.topVer = topVer;
+
+ for (ClusterNode n : nodes)
+ map(n);
+
+ markInitialized();
+ }
+
+ /**
+ * @return Timeout exception.
+ */
+ @NotNull private IgniteTxTimeoutCheckedException timeoutException() {
+ return new IgniteTxTimeoutCheckedException("Failed to acquire lock within provided timeout for " +
+ "transaction [timeout=" + timeout + ", tx=" + tx + ']');
+ }
+
+ /**
+ * A future tracking a single MAP request to be enlisted in transaction and locked on data node.
+ */
+ private class NodeFuture extends GridFutureAdapter<Long> {
+ /** */
+ private boolean completed;
+
+ /** Node ID. */
+ @GridToStringExclude
+ private final ClusterNode node;
+
+ /**
+ * @param node Cluster node.
+ *
+ */
+ private NodeFuture(ClusterNode node) {
+ this.node = node;
+ }
+
+ /**
+ * @return Node.
+ */
+ public ClusterNode node() {
+ return node;
+ }
+
+ /**
+ * @param cnt Total rows counter on given node.
+ * @param removeMapping Whether transaction mapping should be removed for node.
+ * @param err Exception.
+ * @return {@code True} if future was completed by this call.
+ */
+ public boolean onResult(long cnt, boolean removeMapping, Throwable err) {
+ synchronized (this) {
+ if (completed)
+ return false;
+
+ completed = true;
+ }
+
+ if (X.hasCause(err, ClusterTopologyCheckedException.class) || removeMapping) {
+ GridDistributedTxMapping m = tx.mappings().get(node.id());
+
+ assert m != null && m.empty();
+
+ tx.removeMapping(node.id());
+
+ if (node.isLocal())
+ tx.colocatedLocallyMapped(false);
+ }
+ else if (err == null && cnt > 0 && !node.isLocal())
+ tx.hasRemoteLocks(true);
+
+ return onDone(cnt, err);
+ }
+ }
+
+ /**
+ * Lock request timeout object.
+ */
+ private class LockTimeoutObject extends GridTimeoutObjectAdapter {
+ /**
+ * Default constructor.
+ */
+ LockTimeoutObject() {
+ super(timeout);
+ }
+
+ /** {@inheritDoc} */
+ @Override public void onTimeout() {
+ if (log.isDebugEnabled())
+ log.debug("Timed out waiting for lock response: " + this);
+
+ onDone(timeoutException());
+ }
+
+ /** {@inheritDoc} */
+ @Override public String toString() {
+ return S.toString(LockTimeoutObject.class, this);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/00bb4d4d/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/NearTxFinishFuture.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/NearTxFinishFuture.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/NearTxFinishFuture.java
index 132c754..44a87f2 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/NearTxFinishFuture.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/NearTxFinishFuture.java
@@ -17,15 +17,34 @@
package org.apache.ignite.internal.processors.cache.distributed.near;
+import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.internal.IgniteInternalFuture;
import org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx;
/**
*
*/
-public interface NearTxFinishFuture extends IgniteInternalFuture<IgniteInternalTx> {
+public interface NearTxFinishFuture extends IgniteInternalFuture<IgniteInternalTx> {
/**
* @return Commit flag.
*/
boolean commit();
+
+ /**
+ * @return Transaction.
+ */
+ GridNearTxLocal tx();
+
+ /**
+ *
+ * @param commit Commit flag.
+ * @param clearThreadMap If {@code true} removes {@link GridNearTxLocal} from thread map.
+ * @param onTimeout If {@code true} called from timeout handler.
+ */
+ public void finish(boolean commit, boolean clearThreadMap, boolean onTimeout);
+
+ /**
+ * @param e Error.
+ */
+ public void onNodeStop(IgniteCheckedException e);
}
http://git-wip-us.apache.org/repos/asf/ignite/blob/00bb4d4d/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/TxTopologyVersionFuture.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/TxTopologyVersionFuture.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/TxTopologyVersionFuture.java
new file mode 100644
index 0000000..b5e3883
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/TxTopologyVersionFuture.java
@@ -0,0 +1,155 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.internal.processors.cache.distributed.near;
+
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.internal.IgniteInternalFuture;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cache.CacheStoppedException;
+import org.apache.ignite.internal.processors.cache.GridCacheContext;
+import org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTopologyFuture;
+import org.apache.ignite.internal.util.future.GridFutureAdapter;
+import org.apache.ignite.internal.util.typedef.CI1;
+
+/**
+ * Future to obtain/lock topology version for SELECT FOR UPDATE.
+ */
+public class TxTopologyVersionFuture extends GridFutureAdapter<AffinityTopologyVersion> {
+ /** Transaction. */
+ private final GridNearTxLocal tx;
+
+ /** Target cache context. */
+ private final GridCacheContext<?, ?> cctx;
+
+ /** Topology locked flag. */
+ private boolean topLocked;
+
+ /**
+ * @param tx Transaction.
+ * @param cctx Target cache context.
+ */
+ public TxTopologyVersionFuture(GridNearTxLocal tx, GridCacheContext cctx) {
+ this.tx = tx;
+ this.cctx = cctx;
+
+ init();
+ }
+
+ /** */
+ private void init() {
+ // Obtain the topology version to use.
+ long threadId = Thread.currentThread().getId();
+
+ AffinityTopologyVersion topVer = cctx.mvcc().lastExplicitLockTopologyVersion(threadId);
+
+ // If there is another system transaction in progress, use it's topology version to prevent deadlock.
+ if (topVer == null && tx.system())
+ topVer = cctx.tm().lockedTopologyVersion(threadId, tx);
+
+ if (topVer != null)
+ tx.topologyVersion(topVer);
+
+ if (topVer == null)
+ topVer = tx.topologyVersionSnapshot();
+
+ if (topVer != null) {
+ for (GridDhtTopologyFuture fut : cctx.shared().exchange().exchangeFutures()) {
+ if (fut.exchangeDone() && fut.topologyVersion().equals(topVer)) {
+ Throwable err = fut.validateCache(cctx, false, false, null, null);
+
+ if (err != null) {
+ onDone(err);
+
+ return;
+ }
+
+ break;
+ }
+ }
+
+ onDone(topVer);
+
+ topLocked = true;
+
+ return;
+ }
+
+ acquireTopologyVersion();
+ }
+
+ /**
+ * Acquire topology future and wait for its completion.
+ */
+ private void acquireTopologyVersion() {
+ cctx.topology().readLock();
+
+ try {
+ if (cctx.topology().stopping()) {
+ onDone(new CacheStoppedException(cctx.name()));
+
+ return;
+ }
+
+ GridDhtTopologyFuture fut = cctx.topologyVersionFuture();
+
+ if (fut.isDone()) {
+ Throwable err = fut.validateCache(cctx, false, false, null, null);
+
+ if (err != null) {
+ onDone(err);
+
+ return;
+ }
+
+ AffinityTopologyVersion topVer = fut.topologyVersion();
+
+ if (tx != null)
+ tx.topologyVersion(topVer);
+
+ onDone(topVer);
+ }
+ else {
+ fut.listen(new CI1<IgniteInternalFuture<AffinityTopologyVersion>>() {
+ @Override public void apply(IgniteInternalFuture<AffinityTopologyVersion> fut) {
+ try {
+ fut.get();
+
+ acquireTopologyVersion();
+ }
+ catch (IgniteCheckedException e) {
+ onDone(e);
+ }
+ finally {
+ cctx.shared().txContextReset();
+ }
+ }
+ });
+ }
+ }
+ finally {
+ cctx.topology().readUnlock();
+ }
+ }
+
+ /**
+ * @return Client first flag.
+ */
+ public boolean clientFirst() {
+ return cctx.localNode().isClient() && !topLocked && !tx.hasRemoteLocks();
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/00bb4d4d/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/dr/GridCacheDrManager.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/dr/GridCacheDrManager.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/dr/GridCacheDrManager.java
index f2a4b30..f362374 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/dr/GridCacheDrManager.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/dr/GridCacheDrManager.java
@@ -22,6 +22,7 @@ import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
import org.apache.ignite.internal.processors.cache.CacheObject;
import org.apache.ignite.internal.processors.cache.GridCacheManager;
import org.apache.ignite.internal.processors.cache.KeyCacheObject;
+import org.apache.ignite.internal.processors.cache.mvcc.MvccVersion;
import org.apache.ignite.internal.processors.cache.version.GridCacheVersion;
import org.apache.ignite.internal.processors.dr.GridDrType;
import org.jetbrains.annotations.Nullable;
@@ -56,6 +57,36 @@ public interface GridCacheDrManager extends GridCacheManager {
AffinityTopologyVersion topVer)throws IgniteCheckedException;
/**
+ * Enlist for DR.
+ *
+ * @param key Key.
+ * @param val Value.
+ * @param ttl TTL.
+ * @param expireTime Expire time.
+ * @param ver Version.
+ * @param drType Replication type.
+ * @param topVer Topology version.
+ * @param mvccVer Tx mvcc version.
+ * @throws IgniteCheckedException If failed.
+ */
+ void mvccReplicate(KeyCacheObject key,
+ @Nullable CacheObject val,
+ long ttl,
+ long expireTime,
+ GridCacheVersion ver,
+ GridDrType drType,
+ AffinityTopologyVersion topVer,
+ MvccVersion mvccVer) throws IgniteCheckedException;
+
+ /**
+ * @param mvccVer Tx mvcc version.
+ * @param commit {@code True} if tx committed, {@code False} otherwise.
+ * @param topVer Tx snapshot affinity version.
+ * @throws IgniteCheckedException If failed.
+ */
+ void onTxFinished(MvccVersion mvccVer, boolean commit, AffinityTopologyVersion topVer);
+
+ /**
* Process partitions exchange event.
*
* @param topVer Topology version.
http://git-wip-us.apache.org/repos/asf/ignite/blob/00bb4d4d/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/dr/GridOsCacheDrManager.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/dr/GridOsCacheDrManager.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/dr/GridOsCacheDrManager.java
index f3c1b23..8d7e4d8 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/dr/GridOsCacheDrManager.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/dr/GridOsCacheDrManager.java
@@ -22,6 +22,7 @@ import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
import org.apache.ignite.internal.processors.cache.CacheObject;
import org.apache.ignite.internal.processors.cache.GridCacheContext;
import org.apache.ignite.internal.processors.cache.KeyCacheObject;
+import org.apache.ignite.internal.processors.cache.mvcc.MvccVersion;
import org.apache.ignite.internal.processors.cache.version.GridCacheVersion;
import org.apache.ignite.internal.processors.dr.GridDrType;
import org.apache.ignite.lang.IgniteFuture;
@@ -78,6 +79,18 @@ public class GridOsCacheDrManager implements GridCacheDrManager {
}
/** {@inheritDoc} */
+ @Override public void mvccReplicate(KeyCacheObject key, @Nullable CacheObject val, long ttl, long expireTime,
+ GridCacheVersion ver, GridDrType drType, AffinityTopologyVersion topVer,
+ MvccVersion mvccVer) throws IgniteCheckedException {
+ // No-op.
+ }
+
+ /** {@inheritDoc} */
+ @Override public void onTxFinished(MvccVersion mvccVer, boolean commit, AffinityTopologyVersion topVer) {
+ // No-op.
+ }
+
+ /** {@inheritDoc} */
@Override public void onExchange(AffinityTopologyVersion topVer, boolean left) throws IgniteCheckedException {
// No-op.
}
http://git-wip-us.apache.org/repos/asf/ignite/blob/00bb4d4d/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/local/atomic/GridLocalAtomicCache.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/local/atomic/GridLocalAtomicCache.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/local/atomic/GridLocalAtomicCache.java
index ef64ed8..b615952 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/local/atomic/GridLocalAtomicCache.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/local/atomic/GridLocalAtomicCache.java
@@ -54,8 +54,8 @@ import org.apache.ignite.internal.processors.cache.GridCachePreloaderAdapter;
import org.apache.ignite.internal.processors.cache.GridCacheReturn;
import org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy;
import org.apache.ignite.internal.processors.cache.KeyCacheObject;
-import org.apache.ignite.internal.processors.cache.persistence.CacheDataRow;
import org.apache.ignite.internal.processors.cache.local.GridLocalCache;
+import org.apache.ignite.internal.processors.cache.persistence.CacheDataRow;
import org.apache.ignite.internal.processors.cache.transactions.IgniteTxLocalEx;
import org.apache.ignite.internal.processors.cache.version.GridCacheVersion;
import org.apache.ignite.internal.processors.resource.GridResourceIoc;
@@ -461,6 +461,7 @@ public class GridLocalAtomicCache<K, V> extends GridLocalCache<K, V> {
taskName,
expiry,
!deserializeBinary,
+ null,
null);
if (res != null) {
@@ -488,7 +489,8 @@ public class GridLocalAtomicCache<K, V> extends GridLocalCache<K, V> {
null,
taskName,
expiry,
- !deserializeBinary);
+ !deserializeBinary,
+ null);
if (v != null) {
ctx.addResult(vals,
@@ -1096,7 +1098,8 @@ public class GridLocalAtomicCache<K, V> extends GridLocalCache<K, V> {
entryProcessor,
taskName,
null,
- keepBinary);
+ keepBinary,
+ null);
Object oldVal = null;
@@ -1238,7 +1241,8 @@ public class GridLocalAtomicCache<K, V> extends GridLocalCache<K, V> {
null,
taskName,
null,
- keepBinary);
+ keepBinary,
+ null);
Object interceptorVal = ctx.config().getInterceptor().onBeforePut(new CacheLazyEntry(
ctx, entry.key(), old, keepBinary), val);
@@ -1273,7 +1277,8 @@ public class GridLocalAtomicCache<K, V> extends GridLocalCache<K, V> {
null,
taskName,
null,
- keepBinary);
+ keepBinary,
+ null);
IgniteBiTuple<Boolean, ?> interceptorRes = ctx.config().getInterceptor()
.onBeforeRemove(new CacheLazyEntry(ctx, entry.key(), old, keepBinary));
http://git-wip-us.apache.org/repos/asf/ignite/blob/00bb4d4d/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccCoordinator.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccCoordinator.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccCoordinator.java
new file mode 100644
index 0000000..045177a
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccCoordinator.java
@@ -0,0 +1,104 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.internal.processors.cache.mvcc;
+
+import java.io.Serializable;
+import java.util.UUID;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.util.tostring.GridToStringInclude;
+import org.apache.ignite.internal.util.typedef.internal.S;
+
+/**
+ *
+ */
+public class MvccCoordinator implements Serializable {
+ /** */
+ private static final long serialVersionUID = 0L;
+
+ /** */
+ private final UUID nodeId;
+
+ /**
+ * Unique coordinator version, increases when new coordinator is assigned,
+ * can differ from topVer if we decide to assign coordinator manually.
+ */
+ private final long crdVer;
+
+ /** */
+ @GridToStringInclude
+ private final AffinityTopologyVersion topVer;
+
+ /**
+ * @param nodeId Coordinator node ID.
+ * @param crdVer Coordinator version.
+ * @param topVer Topology version when coordinator was assigned.
+ */
+ public MvccCoordinator(UUID nodeId, long crdVer, AffinityTopologyVersion topVer) {
+ assert nodeId != null;
+ assert crdVer > 0 : crdVer;
+ assert topVer != null;
+
+ this.nodeId = nodeId;
+ this.crdVer = crdVer;
+ this.topVer = topVer;
+ }
+
+ /**
+ * @return Unique coordinator version.
+ */
+ public long coordinatorVersion() {
+ return crdVer;
+ }
+
+ /**
+ * @return Coordinator node ID.
+ */
+ public UUID nodeId() {
+ return nodeId;
+ }
+
+ /**
+ * @return Topology version when coordinator was assigned.
+ */
+ public AffinityTopologyVersion topologyVersion() {
+ return topVer;
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean equals(Object o) {
+ if (this == o)
+ return true;
+
+ if (o == null || getClass() != o.getClass())
+ return false;
+
+ MvccCoordinator that = (MvccCoordinator)o;
+
+ return crdVer == that.crdVer;
+ }
+
+ /** {@inheritDoc} */
+ @Override public int hashCode() {
+ return (int)(crdVer ^ (crdVer >>> 32));
+ }
+
+ /** {@inheritDoc} */
+ @Override public String toString() {
+ return S.toString(MvccCoordinator.class, this);
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/00bb4d4d/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccDiscoveryData.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccDiscoveryData.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccDiscoveryData.java
new file mode 100644
index 0000000..d2e936f
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccDiscoveryData.java
@@ -0,0 +1,52 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.internal.processors.cache.mvcc;
+
+import org.apache.ignite.internal.util.typedef.internal.S;
+
+import java.io.Serializable;
+
+/**
+ * MVCC discovery data to be shared between nodes on join.
+ */
+public class MvccDiscoveryData implements Serializable {
+ /** */
+ private static final long serialVersionUID = 0L;
+
+ /** Current coordinator. */
+ private MvccCoordinator crd;
+
+ /**
+ * @param crd Coordinator.
+ */
+ public MvccDiscoveryData(MvccCoordinator crd) {
+ this.crd = crd;
+ }
+
+ /**
+ * @return Current coordinator.
+ */
+ public MvccCoordinator coordinator() {
+ return crd;
+ }
+
+ /** {@inheritDoc} */
+ @Override public String toString() {
+ return S.toString(MvccDiscoveryData.class, this);
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/00bb4d4d/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccEmptyLongList.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccEmptyLongList.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccEmptyLongList.java
new file mode 100644
index 0000000..7963685
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccEmptyLongList.java
@@ -0,0 +1,53 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.internal.processors.cache.mvcc;
+
+/**
+ *
+ */
+public class MvccEmptyLongList implements MvccLongList {
+ /** */
+ public static MvccEmptyLongList INSTANCE = new MvccEmptyLongList();
+
+ /**
+ *
+ */
+ private MvccEmptyLongList() {
+ // No-op.
+ }
+
+ /** {@inheritDoc} */
+ @Override public int size() {
+ return 0;
+ }
+
+ /** {@inheritDoc} */
+ @Override public long get(int i) {
+ throw new IndexOutOfBoundsException();
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean contains(long val) {
+ return false;
+ }
+
+ /** {@inheritDoc} */
+ @Override public String toString() {
+ return "MvccEmptyLongList[]";
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/00bb4d4d/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccFuture.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccFuture.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccFuture.java
new file mode 100644
index 0000000..0ca82d3
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccFuture.java
@@ -0,0 +1,66 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.internal.processors.cache.mvcc;
+
+import java.util.UUID;
+import org.apache.ignite.internal.util.future.GridFutureAdapter;
+import org.apache.ignite.internal.util.typedef.internal.S;
+
+/**
+ *
+ */
+public class MvccFuture<T> extends GridFutureAdapter<T> {
+ /** */
+ protected UUID crdId;
+
+ /**
+ * Default constructor.
+ */
+ public MvccFuture() {
+ }
+
+ /**
+ * @param crdId MVCC coordinator node ID.
+ */
+ public MvccFuture(UUID crdId) {
+ assert crdId != null;
+
+ this.crdId = crdId;
+ }
+
+ /**
+ * @return MVCC coordinator node ID.
+ */
+ public UUID coordinatorNodeId() {
+ return crdId;
+ }
+
+ /**
+ * @param crdId MVCC coordinator node ID.
+ */
+ public void coordinatorNodeId(UUID crdId) {
+ assert crdId != null;
+
+ this.crdId = crdId;
+ }
+
+ /** {@inheritDoc} */
+ @Override public String toString() {
+ return S.toString(MvccFuture.class, this, super.toString());
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/00bb4d4d/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccLongList.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccLongList.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccLongList.java
new file mode 100644
index 0000000..8b580ed
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccLongList.java
@@ -0,0 +1,29 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.internal.processors.cache.mvcc;
+
+/**
+ *
+ */
+public interface MvccLongList {
+ public int size();
+
+ public long get(int i);
+
+ public boolean contains(long val);
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/00bb4d4d/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccPreviousCoordinatorQueries.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccPreviousCoordinatorQueries.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccPreviousCoordinatorQueries.java
new file mode 100644
index 0000000..024ef70
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccPreviousCoordinatorQueries.java
@@ -0,0 +1,222 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.internal.processors.cache.mvcc;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.discovery.GridDiscoveryManager;
+import org.apache.ignite.internal.util.GridLongList;
+import org.apache.ignite.internal.util.typedef.F;
+import org.jetbrains.annotations.Nullable;
+
+import static org.apache.ignite.internal.processors.cache.mvcc.MvccQueryTracker.MVCC_TRACKER_ID_NA;
+
+/**
+ *
+ */
+class MvccPreviousCoordinatorQueries {
+ /** */
+ private volatile boolean prevQueriesDone;
+
+ /** Map of nodes to active {@link MvccQueryTracker} IDs list. */
+ private final ConcurrentHashMap<UUID, Set<Long>> activeQueries = new ConcurrentHashMap<>();
+
+ /** */
+ private final ConcurrentHashMap<UUID, Set<Long>> rcvdAcks = new ConcurrentHashMap<>();
+
+ /** */
+ private Set<UUID> rcvd;
+
+ /** */
+ private Set<UUID> waitNodes;
+
+ /** */
+ private boolean initDone;
+
+ /**
+ * @param nodeQueries Active queries map.
+ * @param discoCache Discovery data.
+ * @param mgr Discovery manager.
+ */
+ void init(Map<UUID, GridLongList> nodeQueries, DiscoCache discoCache, GridDiscoveryManager mgr) {
+ synchronized (this) {
+ assert !initDone;
+ assert waitNodes == null;
+
+ waitNodes = new HashSet<>();
+
+ for (ClusterNode node : discoCache.allNodes()) {
+ if ((nodeQueries == null || !nodeQueries.containsKey(node.id())) &&
+ mgr.alive(node) &&
+ !F.contains(rcvd, node.id()))
+ waitNodes.add(node.id());
+ }
+
+ initDone = waitNodes.isEmpty();
+
+ if (nodeQueries != null) {
+ for (Map.Entry<UUID, GridLongList> e : nodeQueries.entrySet())
+ mergeToActiveQueries(e.getKey(), e.getValue());
+ }
+
+ if (initDone && !prevQueriesDone)
+ prevQueriesDone = activeQueries.isEmpty() && rcvdAcks.isEmpty();
+ }
+ }
+
+ /**
+ * @return {@code True} if all queries on
+ */
+ boolean previousQueriesDone() {
+ return prevQueriesDone;
+ }
+
+ /**
+ * Merges current node active queries with the given ones.
+ *
+ * @param nodeId Node ID.
+ * @param nodeTrackers Active query trackers started on node.
+ */
+ private void mergeToActiveQueries(UUID nodeId, GridLongList nodeTrackers) {
+ if (nodeTrackers == null || nodeTrackers.isEmpty() || prevQueriesDone)
+ return;
+
+ Set<Long> currTrackers = activeQueries.get(nodeId);
+
+ if (currTrackers == null)
+ activeQueries.put(nodeId, currTrackers = addAll(nodeTrackers, null));
+ else
+ addAll(nodeTrackers, currTrackers);
+
+ // Check if there were any acks had been arrived before.
+ Set<Long> currAcks = rcvdAcks.get(nodeId);
+
+ if (!currTrackers.isEmpty() && currAcks != null && !currAcks.isEmpty()) {
+ Collection<Long> intersection = new HashSet<>(currAcks);
+
+ intersection.retainAll(currTrackers);
+
+ currAcks.removeAll(intersection);
+ currTrackers.removeAll(intersection);
+
+ if (currTrackers.isEmpty())
+ activeQueries.remove(nodeId);
+
+ if (currAcks.isEmpty())
+ rcvdAcks.remove(nodeId);
+ }
+
+ if (initDone && !prevQueriesDone)
+ prevQueriesDone = activeQueries.isEmpty() && rcvdAcks.isEmpty();
+ }
+
+ /**
+ * @param nodeId Node ID.
+ * @param nodeTrackers Active query trackers started on node.
+ */
+ void addNodeActiveQueries(UUID nodeId, @Nullable GridLongList nodeTrackers) {
+ synchronized (this) {
+ if (initDone)
+ return;
+
+ if (waitNodes == null) {
+ if (rcvd == null)
+ rcvd = new HashSet<>();
+
+ rcvd.add(nodeId);
+ }
+ else {
+ waitNodes.remove(nodeId);
+
+ initDone = waitNodes.isEmpty();
+ }
+
+ mergeToActiveQueries(nodeId, nodeTrackers);
+
+ if (initDone && !prevQueriesDone)
+ prevQueriesDone = activeQueries.isEmpty() && rcvdAcks.isEmpty();
+ }
+ }
+
+ /**
+ * @param nodeId Failed node ID.
+ */
+ void onNodeFailed(UUID nodeId) {
+ synchronized (this) {
+ if (waitNodes != null) {
+ waitNodes.remove(nodeId);
+
+ initDone = waitNodes.isEmpty();
+ }
+
+ if (initDone && !prevQueriesDone && activeQueries.remove(nodeId) != null)
+ prevQueriesDone = activeQueries.isEmpty() && rcvdAcks.isEmpty();
+ }
+ }
+
+ /**
+ * @param nodeId Node ID.
+ * @param qryTrackerId Query tracker Id.
+ */
+ void onQueryDone(UUID nodeId, long qryTrackerId) {
+ if (qryTrackerId == MVCC_TRACKER_ID_NA)
+ return;
+
+ synchronized (this) {
+ Set<Long> nodeTrackers = activeQueries.get(nodeId);
+
+ if (nodeTrackers == null || !nodeTrackers.remove(qryTrackerId)) {
+ Set<Long> nodeAcks = rcvdAcks.get(nodeId);
+
+ if (nodeAcks == null)
+ rcvdAcks.put(nodeId, nodeAcks = new HashSet<>());
+
+ // We received qry done ack before the active qry message. Need to save it.
+ nodeAcks.add(qryTrackerId);
+ }
+
+ if (nodeTrackers != null && nodeTrackers.isEmpty())
+ activeQueries.remove(nodeId);
+
+ if (initDone && !prevQueriesDone)
+ prevQueriesDone = activeQueries.isEmpty() && rcvdAcks.isEmpty();
+ }
+ }
+
+ /**
+ * @param from Long list.
+ * @param to Set.
+ */
+ private Set<Long> addAll(GridLongList from, Set<Long> to) {
+ assert from != null;
+
+ if (to == null)
+ to = new HashSet<>(from.size());
+
+ for (int i = 0; i < from.size(); i++)
+ to.add(from.get(i));
+
+ return to;
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/00bb4d4d/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccProcessor.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccProcessor.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccProcessor.java
new file mode 100644
index 0000000..bce61a1
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/mvcc/MvccProcessor.java
@@ -0,0 +1,250 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.internal.processors.cache.mvcc;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.UUID;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.internal.IgniteDiagnosticPrepareContext;
+import org.apache.ignite.internal.IgniteInternalFuture;
+import org.apache.ignite.internal.cluster.ClusterTopologyCheckedException;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.processors.GridProcessor;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cache.ExchangeContext;
+import org.apache.ignite.internal.processors.cache.GridCacheContext;
+import org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx;
+import org.apache.ignite.internal.util.GridLongList;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ *
+ */
+public interface MvccProcessor extends GridProcessor {
+ /**
+ * @param evtType Event type.
+ * @param nodes Current nodes.
+ * @param topVer Topology version.
+ */
+ void onDiscoveryEvent(int evtType, Collection<ClusterNode> nodes, long topVer);
+
+ /**
+ * Exchange start callback.
+ *
+ * @param mvccCrd Mvcc coordinator.
+ * @param exchCtx Exchange context.
+ * @param exchCrd Exchange coordinator.
+ */
+ void onExchangeStart(MvccCoordinator mvccCrd, ExchangeContext exchCtx, ClusterNode exchCrd);
+
+ /**
+ * Exchange done callback.
+ *
+ * @param newCoord New coordinator flag.
+ * @param discoCache Disco cache.
+ * @param activeQueries Active queries.
+ */
+ void onExchangeDone(boolean newCoord, DiscoCache discoCache, Map<UUID, GridLongList> activeQueries);
+
+ /**
+ * @param nodeId Node ID
+ * @param activeQueries Active queries.
+ */
+ void processClientActiveQueries(UUID nodeId, @Nullable GridLongList activeQueries);
+
+ /**
+ * @return Coordinator.
+ */
+ @Nullable MvccCoordinator currentCoordinator();
+
+ /**
+ * Check that the given topology is greater or equals to coordinator's one and returns current coordinator.
+ * @param topVer Topology version.
+ * @return Mvcc coordinator.
+ */
+ @Nullable MvccCoordinator currentCoordinator(AffinityTopologyVersion topVer);
+
+ /**
+ * @return Mvcc coordinator received from discovery event.
+ */
+ @Nullable MvccCoordinator coordinatorFromDiscoveryEvent();
+
+ /**
+ * @return Current coordinator node ID.
+ */
+ UUID currentCoordinatorId();
+
+ /**
+ * @param curCrd Coordinator.
+ */
+ void updateCoordinator(MvccCoordinator curCrd);
+
+ /**
+ * @param crdVer Mvcc coordinator version.
+ * @param cntr Mvcc counter.
+ * @return State for given mvcc version.
+ * @throws IgniteCheckedException If fails.
+ */
+ byte state(long crdVer, long cntr) throws IgniteCheckedException;
+
+ /**
+ * @param ver Version to check.
+ * @return State for given mvcc version.
+ * @throws IgniteCheckedException If fails.
+ */
+ byte state(MvccVersion ver) throws IgniteCheckedException;
+
+ /**
+ * @param ver Version.
+ * @param state State.
+ * @throws IgniteCheckedException If fails;
+ */
+ void updateState(MvccVersion ver, byte state) throws IgniteCheckedException;
+
+ /**
+ * @param ver Version.
+ * @param state State.
+ * @param primary Flag if this is primary node.
+ * @throws IgniteCheckedException If fails;
+ */
+ void updateState(MvccVersion ver, byte state, boolean primary) throws IgniteCheckedException;
+
+ /**
+ * @param crd Mvcc coordinator version.
+ * @param cntr Mvcc counter.
+ */
+ void registerLocalTransaction(long crd, long cntr);
+
+ /**
+ * @param crd Mvcc coordinator version.
+ * @param cntr Mvcc counter.
+ * @return {@code True} if there is an active local transaction with given version.
+ */
+ boolean hasLocalTransaction(long crd, long cntr);
+
+ /**
+ * @param cctx Cache context.
+ * @param locked Version the entry is locked by.
+ * @return Future, which is completed as soon as the lock is released.
+ * @throws IgniteCheckedException If failed.
+ */
+ IgniteInternalFuture<Void> waitFor(GridCacheContext cctx, MvccVersion locked) throws IgniteCheckedException;
+
+ /**
+ * @param tracker Query tracker.
+ */
+ void addQueryTracker(MvccQueryTracker tracker);
+
+ /**
+ * @param id Query tracker id.
+ */
+ void removeQueryTracker(Long id);
+
+ /**
+ * @return {@link MvccSnapshot} if this is a coordinator node and coordinator is initialized.
+ * {@code Null} in other cases.
+ * @throws ClusterTopologyCheckedException If coordinator doesn't match locked topology or not assigned.
+ */
+ MvccSnapshot tryRequestSnapshotLocal() throws ClusterTopologyCheckedException;
+
+ /**
+ * @param tx Transaction.
+ * @return {@link MvccSnapshot} if this is a coordinator node and coordinator is initialized.
+ * {@code Null} in other cases.
+ * @throws ClusterTopologyCheckedException If coordinator doesn't match locked topology or not assigned.
+ */
+ MvccSnapshot tryRequestSnapshotLocal(@Nullable IgniteInternalTx tx) throws ClusterTopologyCheckedException;
+
+ /**
+ * Requests snapshot on Mvcc coordinator.
+ *
+ * @return Snapshot future.
+ */
+ IgniteInternalFuture<MvccSnapshot> requestSnapshotAsync();
+
+ /**
+ * Requests snapshot on Mvcc coordinator.
+ *
+ * @param tx Transaction.
+ * @return Snapshot future.
+ */
+ IgniteInternalFuture<MvccSnapshot> requestSnapshotAsync(IgniteInternalTx tx);
+
+ /**
+ * Requests snapshot on Mvcc coordinator.
+ *
+ * @param lsnr Request listener.
+ */
+ void requestSnapshotAsync(MvccSnapshotResponseListener lsnr);
+
+ /**
+ * Requests snapshot on Mvcc coordinator.
+ *
+ * @param tx Transaction
+ * @param lsnr Request listener.
+ */
+ void requestSnapshotAsync(IgniteInternalTx tx, MvccSnapshotResponseListener lsnr);
+
+ /**
+ * @param updateVer Transaction update version.
+ * @return Acknowledge future.
+ */
+ IgniteInternalFuture<Void> ackTxCommit(MvccSnapshot updateVer);
+
+ /**
+ * @param updateVer Transaction update version.
+ * @param readSnapshot Transaction read version.
+ * @param qryId Query tracker id.
+ * @return Acknowledge future.
+ */
+ IgniteInternalFuture<Void> ackTxCommit(MvccVersion updateVer, MvccSnapshot readSnapshot, long qryId);
+
+ /**
+ * @param updateVer Transaction update version.
+ */
+ void ackTxRollback(MvccVersion updateVer);
+
+ /**
+ * @param updateVer Transaction update version.
+ * @param readSnapshot Transaction read version.
+ * @param qryTrackerId Query tracker id.
+ */
+ void ackTxRollback(MvccVersion updateVer, MvccSnapshot readSnapshot, long qryTrackerId);
+
+ /**
+ * @param snapshot Query version.
+ * @param qryId Query tracker ID.
+ */
+ void ackQueryDone(MvccSnapshot snapshot, long qryId);
+
+ /**
+ * @param crdId Coordinator ID.
+ * @param txs Transaction IDs.
+ * @return Future.
+ */
+ IgniteInternalFuture<Void> waitTxsFuture(UUID crdId, GridLongList txs);
+
+ /**
+ * @param log Logger.
+ * @param diagCtx Diagnostic request.
+ */
+ void dumpDebugInfo(IgniteLogger log, @Nullable IgniteDiagnosticPrepareContext diagCtx);
+}