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:41 UTC

[35/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/dht/GridDhtTxAbstractEnlistFuture.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxAbstractEnlistFuture.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxAbstractEnlistFuture.java
new file mode 100644
index 0000000..bb11df5
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxAbstractEnlistFuture.java
@@ -0,0 +1,1139 @@
+/*
+ * 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.dht;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
+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.pagemem.wal.WALPointer;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cache.CacheEntryInfoCollection;
+import org.apache.ignite.internal.processors.cache.CacheObject;
+import org.apache.ignite.internal.processors.cache.GridCacheContext;
+import org.apache.ignite.internal.processors.cache.GridCacheEntryInfo;
+import org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException;
+import org.apache.ignite.internal.processors.cache.GridCacheFutureAdapter;
+import org.apache.ignite.internal.processors.cache.GridCacheMvccEntryInfo;
+import org.apache.ignite.internal.processors.cache.GridCacheUpdateTxResult;
+import org.apache.ignite.internal.processors.cache.KeyCacheObject;
+import org.apache.ignite.internal.processors.cache.distributed.GridDistributedTxMapping;
+import org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxAbstractEnlistFuture;
+import org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxSelectForUpdateFuture;
+import org.apache.ignite.internal.processors.cache.mvcc.MvccSnapshot;
+import org.apache.ignite.internal.processors.cache.mvcc.MvccUtils;
+import org.apache.ignite.internal.processors.cache.mvcc.txlog.TxState;
+import org.apache.ignite.internal.processors.cache.persistence.CacheDataRowAdapter;
+import org.apache.ignite.internal.processors.cache.query.IgniteQueryErrorCode;
+import org.apache.ignite.internal.processors.cache.tree.mvcc.data.MvccDataRow;
+import org.apache.ignite.internal.processors.cache.tree.mvcc.search.MvccLinkAwareSearchRow;
+import org.apache.ignite.internal.processors.cache.version.GridCacheVersion;
+import org.apache.ignite.internal.processors.query.EnlistOperation;
+import org.apache.ignite.internal.processors.query.IgniteSQLException;
+import org.apache.ignite.internal.processors.query.UpdateSourceIterator;
+import org.apache.ignite.internal.processors.timeout.GridTimeoutObjectAdapter;
+import org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException;
+import org.apache.ignite.internal.util.GridLongList;
+import org.apache.ignite.internal.util.tostring.GridToStringExclude;
+import org.apache.ignite.internal.util.typedef.CI1;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.internal.S;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.lang.IgniteBiTuple;
+import org.apache.ignite.lang.IgniteInClosure;
+import org.apache.ignite.lang.IgniteUuid;
+import org.apache.ignite.plugin.extensions.communication.Message;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Abstract future processing transaction enlisting and locking
+ * of entries produced with DML and SELECT FOR UPDATE queries.
+ */
+public abstract class GridDhtTxAbstractEnlistFuture extends GridCacheFutureAdapter<Long>
+    implements DhtLockFuture<Long> {
+    /** Done field updater. */
+    private static final AtomicIntegerFieldUpdater<GridDhtTxAbstractEnlistFuture> DONE_UPD =
+        AtomicIntegerFieldUpdater.newUpdater(GridDhtTxAbstractEnlistFuture.class, "done");
+
+    /** SkipCntr field updater. */
+    private static final AtomicIntegerFieldUpdater<GridDhtTxAbstractEnlistFuture> SKIP_UPD =
+        AtomicIntegerFieldUpdater.newUpdater(GridDhtTxAbstractEnlistFuture.class, "skipCntr");
+
+    /** Marker object. */
+    private static final Object FINISHED = new Object();
+
+    /** */
+    private static final int BATCH_SIZE = 1024;
+
+    /** In-flight batches per node limit. */
+    private static final int BATCHES_PER_NODE = 5;
+
+    /** */
+    private static final int FIRST_BATCH_ID = 0;
+
+    /** Future ID. */
+    protected final IgniteUuid futId;
+
+    /** Cache registry. */
+    @GridToStringExclude
+    protected final GridCacheContext<?, ?> cctx;
+
+    /** Logger. */
+    @GridToStringExclude
+    protected final IgniteLogger log;
+
+    /** Thread. */
+    protected final long threadId;
+
+    /** Future ID. */
+    protected final IgniteUuid nearFutId;
+
+    /** Future ID. */
+    protected final int nearMiniId;
+
+    /** Partitions. */
+    protected final int[] parts;
+
+    /** Transaction. */
+    protected final GridDhtTxLocalAdapter tx;
+
+    /** Lock version. */
+    protected final GridCacheVersion lockVer;
+
+    /** */
+    protected final MvccSnapshot mvccSnapshot;
+
+    /** Processed entries count. */
+    protected long cnt;
+
+    /** Near node ID. */
+    protected final UUID nearNodeId;
+
+    /** Near lock version. */
+    protected final GridCacheVersion nearLockVer;
+
+    /** Timeout object. */
+    @GridToStringExclude
+    protected LockTimeoutObject timeoutObj;
+
+    /** Lock timeout. */
+    protected final long timeout;
+
+    /** Query iterator */
+    private UpdateSourceIterator<?> it;
+
+    /** Row extracted from iterator but not yet used. */
+    private Object peek;
+
+    /** */
+    @SuppressWarnings({"FieldCanBeLocal"})
+    @GridToStringExclude
+    private volatile int skipCntr;
+
+    /** */
+    @SuppressWarnings("unused")
+    @GridToStringExclude
+    private volatile int done;
+
+    /** */
+    @GridToStringExclude
+    private int batchIdCntr;
+
+    /** Batches for sending to remote nodes. */
+    private Map<UUID, Batch> batches;
+
+    /** Batches already sent to remotes, but their acks are not received yet. */
+    private ConcurrentMap<UUID, ConcurrentMap<Integer, Batch>> pending;
+
+    /** */
+    private WALPointer walPtr;
+
+    /** Do not send DHT requests to near node. */
+    protected boolean skipNearNodeUpdates;
+
+    /** There are keys belonging to backup partitions on near node. */
+    protected boolean hasNearNodeUpdates;
+
+    /** Moving partitions. */
+    private Map<Integer, Boolean> movingParts;
+
+    /** Update counters to be sent to the near node in case it is a backup node also. */
+    protected GridLongList nearUpdCntrs;
+
+    /**
+     * @param nearNodeId Near node ID.
+     * @param nearLockVer Near lock version.
+     * @param mvccSnapshot Mvcc snapshot.
+     * @param threadId Thread ID.
+     * @param nearFutId Near future id.
+     * @param nearMiniId Near mini future id.
+     * @param parts Partitions.
+     * @param tx Transaction.
+     * @param timeout Lock acquisition timeout.
+     * @param cctx Cache context.
+     */
+    protected GridDhtTxAbstractEnlistFuture(UUID nearNodeId,
+        GridCacheVersion nearLockVer,
+        MvccSnapshot mvccSnapshot,
+        long threadId,
+        IgniteUuid nearFutId,
+        int nearMiniId,
+        @Nullable int[] parts,
+        GridDhtTxLocalAdapter tx,
+        long timeout,
+        GridCacheContext<?, ?> cctx) {
+        assert tx != null;
+        assert timeout >= 0;
+        assert nearNodeId != null;
+        assert nearLockVer != null;
+        assert threadId == tx.threadId();
+
+        this.threadId = threadId;
+        this.cctx = cctx;
+        this.nearNodeId = nearNodeId;
+        this.nearLockVer = nearLockVer;
+        this.nearFutId = nearFutId;
+        this.nearMiniId = nearMiniId;
+        this.mvccSnapshot = mvccSnapshot;
+        this.timeout = timeout;
+        this.tx = tx;
+        this.parts = parts;
+
+        lockVer = tx.xidVersion();
+
+        futId = IgniteUuid.randomUuid();
+
+        log = cctx.logger(GridDhtTxAbstractEnlistFuture.class);
+    }
+
+    /**
+     * @return iterator.
+     * @throws IgniteCheckedException If failed.
+     */
+    protected abstract UpdateSourceIterator<?> createIterator() throws IgniteCheckedException;
+
+    /**
+     *
+     */
+    public void init() {
+        if (timeout < 0) {
+            // Time is out.
+            onDone(timeoutException());
+
+            return;
+        }
+        else if (timeout > 0)
+            timeoutObj = new LockTimeoutObject();
+
+        while(true) {
+            IgniteInternalFuture<?> fut = tx.lockFut;
+
+            if (fut == GridDhtTxLocalAdapter.ROLLBACK_FUT) {
+                onDone(tx.timedOut() ? tx.timeoutException() : tx.rollbackException());
+
+                return;
+            }
+            else if (fut != null) {
+                // Wait for previous future.
+                assert fut instanceof GridNearTxAbstractEnlistFuture
+                    || fut instanceof GridDhtTxAbstractEnlistFuture
+                    || fut instanceof CompoundLockFuture
+                    || fut instanceof GridNearTxSelectForUpdateFuture : fut;
+
+                // Terminate this future if parent future is terminated by rollback.
+                if (!fut.isDone()) {
+                    fut.listen(new IgniteInClosure<IgniteInternalFuture>() {
+                        @Override public void apply(IgniteInternalFuture fut) {
+                            if (fut.error() != null)
+                                onDone(fut.error());
+                        }
+                    });
+                }
+                else if (fut.error() != null)
+                    onDone(fut.error());
+
+                break;
+            }
+            else if (tx.updateLockFuture(null, this))
+                break;
+        }
+
+        boolean added = cctx.mvcc().addFuture(this, futId);
+
+        assert added;
+
+        if (isDone()) {
+            cctx.mvcc().removeFuture(futId);
+
+            return;
+        }
+
+        if (timeoutObj != null)
+            cctx.time().addTimeoutObject(timeoutObj);
+
+        try {
+            checkPartitions(parts);
+
+            UpdateSourceIterator<?> it = createIterator();
+
+            if (!it.hasNext()) {
+                U.close(it, log);
+
+                onDone(0L);
+
+                return;
+            }
+
+            tx.addActiveCache(cctx, false);
+
+            this.it = it;
+        }
+        catch (Throwable e) {
+            onDone(e);
+
+            if (e instanceof Error)
+                throw (Error)e;
+
+            return;
+        }
+
+        continueLoop(false);
+    }
+
+    /**
+     * Clears lock future.
+     */
+    protected void clearLockFuture() {
+        tx.clearLockFuture(this);
+    }
+
+    /**
+     * Iterates over iterator, applies changes locally and sends it on backups.
+     *
+     * @param ignoreCntr {@code True} if need to ignore skip counter.
+     */
+    private void continueLoop(boolean ignoreCntr) {
+        if (isDone() || (!ignoreCntr && (SKIP_UPD.getAndIncrement(this) != 0)))
+            return;
+
+        GridDhtCacheAdapter cache = cctx.dhtCache();
+        EnlistOperation op = it.operation();
+        AffinityTopologyVersion topVer = tx.topologyVersionSnapshot();
+
+        try {
+            while (true) {
+                while (hasNext0()) {
+                    Object cur = next0();
+
+                    KeyCacheObject key = cctx.toCacheKeyObject(op.isDeleteOrLock() ? cur : ((IgniteBiTuple)cur).getKey());
+
+                    if (!ensureFreeSlot(key)) {
+                        // Can't advance further at the moment.
+                        peek = cur;
+
+                        it.beforeDetach();
+
+                        break;
+                    }
+
+                    GridDhtCacheEntry entry = cache.entryExx(key);
+
+                    if (log.isDebugEnabled())
+                        log.debug("Adding entry: " + entry);
+
+                    assert !entry.detached();
+
+                    CacheObject val = op.isDeleteOrLock() ? null : cctx.toCacheObject(((IgniteBiTuple)cur).getValue());
+
+                    tx.markQueryEnlisted(mvccSnapshot);
+
+                    GridCacheUpdateTxResult res;
+
+                    while (true) {
+                        cctx.shared().database().checkpointReadLock();
+
+                        try {
+                            switch (op) {
+                                case DELETE:
+                                    res = entry.mvccRemove(
+                                        tx,
+                                        cctx.localNodeId(),
+                                        topVer,
+                                        null,
+                                        mvccSnapshot,
+                                        isMoving(key.partition()));
+
+                                    break;
+
+                                case INSERT:
+                                case UPSERT:
+                                case UPDATE:
+                                    res = entry.mvccSet(
+                                        tx,
+                                        cctx.localNodeId(),
+                                        val,
+                                        0,
+                                        topVer,
+                                        null,
+                                        mvccSnapshot,
+                                        op.cacheOperation(),
+                                        isMoving(key.partition()),
+                                        op.noCreate());
+
+                                    break;
+
+                                case LOCK:
+                                    res = entry.mvccLock(
+                                        tx,
+                                        mvccSnapshot);
+
+                                    break;
+
+                                default:
+                                    throw new IgniteSQLException("Cannot acquire lock for operation [op= " + op + "]" +
+                                        "Operation is unsupported at the moment ", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
+                            }
+
+                            break;
+                        }
+                        catch (GridCacheEntryRemovedException ignored) {
+                            entry = cache.entryExx(entry.key(), topVer);
+                        }
+                        finally {
+                            cctx.shared().database().checkpointReadUnlock();
+                        }
+                    }
+
+                    IgniteInternalFuture<GridCacheUpdateTxResult> updateFut = res.updateFuture();
+
+                    if (updateFut != null) {
+                        if (updateFut.isDone())
+                            res = updateFut.get();
+                        else {
+                            CacheObject val0 = val;
+                            GridDhtCacheEntry entry0 = entry;
+
+                            it.beforeDetach();
+
+                            updateFut.listen(new CI1<IgniteInternalFuture<GridCacheUpdateTxResult>>() {
+                                @Override public void apply(IgniteInternalFuture<GridCacheUpdateTxResult> fut) {
+                                    try {
+                                        processEntry(entry0, op, fut.get(), val0);
+
+                                        continueLoop(true);
+                                    }
+                                    catch (Throwable e) {
+                                        onDone(e);
+                                    }
+                                }
+                            });
+
+                            // Can't move further. Exit loop without decrementing the counter.
+                            return;
+                        }
+                    }
+
+                    processEntry(entry, op, res, val);
+                }
+
+                if (!hasNext0()) {
+                    if (walPtr != null && !cctx.tm().logTxRecords()) {
+                        cctx.shared().wal().flush(walPtr, true);
+
+                        walPtr = null; // Avoid additional flushing.
+                    }
+
+                    if (!F.isEmpty(batches)) {
+                        // Flush incomplete batches.
+                        // Need to skip batches for nodes where first request (contains tx info) is still in-flight.
+                        // Otherwise, the regular enlist request (without tx info) may beat it to the primary node.
+                        Iterator<Map.Entry<UUID, Batch>> it = batches.entrySet().iterator();
+
+                        while (it.hasNext()) {
+                            Map.Entry<UUID, Batch> e = it.next();
+
+                            ConcurrentMap<Integer, Batch> pending0 =
+                                pending == null ? null : pending.get(e.getKey());
+
+                            if (pending0 == null || !pending0.containsKey(FIRST_BATCH_ID)) {
+                                it.remove();
+
+                                sendBatch(e.getValue());
+                            }
+                        }
+                    }
+
+                    if (noPendingRequests()) {
+                        onDone(cnt);
+
+                        return;
+                    }
+                }
+
+                if (SKIP_UPD.decrementAndGet(this) == 0)
+                    break;
+
+                skipCntr = 1;
+            }
+        }
+        catch (Throwable e) {
+            onDone(e);
+
+            if (e instanceof Error)
+                throw (Error)e;
+        }
+    }
+
+    /** */
+    private Object next0() {
+        if (!hasNext0())
+            throw new NoSuchElementException();
+
+        Object cur;
+
+        if ((cur = peek) != null)
+            peek = null;
+        else
+            cur = it.next();
+
+        return cur;
+    }
+
+    /** */
+    private boolean hasNext0() {
+        if (peek == null && !it.hasNext())
+            peek = FINISHED;
+
+        return peek != FINISHED;
+    }
+
+    /**
+     * @return {@code True} if in-flight batches map is empty.
+     */
+    private boolean noPendingRequests() {
+        if (F.isEmpty(pending))
+            return true;
+
+        for (ConcurrentMap<Integer, Batch> e : pending.values()) {
+            if (!e.isEmpty())
+                return false;
+        }
+
+        return true;
+    }
+
+    /**
+     * @param entry Cache entry.
+     * @param op Operation.
+     * @param updRes Update result.
+     * @param val New value.
+     * @throws IgniteCheckedException If failed.
+     */
+    private void processEntry(GridDhtCacheEntry entry, EnlistOperation op,
+        GridCacheUpdateTxResult updRes, CacheObject val) throws IgniteCheckedException {
+        checkCompleted();
+
+        assert updRes != null && updRes.updateFuture() == null;
+
+        WALPointer ptr0 = updRes.loggedPointer();
+
+        if (ptr0 != null)
+            walPtr = ptr0;
+
+        if (!updRes.success())
+            return;
+
+        cnt++;
+
+        if (op != EnlistOperation.LOCK)
+            addToBatch(entry.key(), val, updRes.mvccHistory(), updRes.updateCounter(), entry.context().cacheId());
+    }
+
+    /**
+     * Adds row to batch.
+     * <b>IMPORTANT:</b> This method should be called from the critical section in {@link this.sendNextBatches()}
+     *
+     * @param key Key.
+     * @param val Value.
+     * @param hist History rows.
+     * @param updCntr Update counter.
+     */
+    private void addToBatch(KeyCacheObject key, CacheObject val, List<MvccLinkAwareSearchRow> hist, long updCntr,
+        int cacheId) throws IgniteCheckedException {
+        List<ClusterNode> backups = backupNodes(key);
+
+        int part = cctx.affinity().partition(key);
+
+        tx.addPartitionCountersMapping(cacheId, part);
+
+        if (F.isEmpty(backups))
+            return;
+
+        CacheEntryInfoCollection hist0 = null;
+
+        for (ClusterNode node : backups) {
+            assert !node.isLocal();
+
+            boolean moving = isMoving(node, part);
+
+            if (skipNearNodeUpdates && node.id().equals(nearNodeId) && !moving) {
+                updateMappings(node);
+
+                if (newRemoteTx(node))
+                    tx.addLockTransactionNode(node);
+
+                hasNearNodeUpdates = true;
+
+                if (nearUpdCntrs == null)
+                    nearUpdCntrs = new GridLongList();
+
+                nearUpdCntrs.add(updCntr);
+
+                continue;
+            }
+
+            Batch batch = null;
+
+            if (batches == null)
+                batches = new HashMap<>();
+            else
+                batch = batches.get(node.id());
+
+            if (batch == null)
+                batches.put(node.id(), batch = new Batch(node));
+
+            if (moving && hist0 == null) {
+                assert !F.isEmpty(hist);
+
+                hist0 = fetchHistoryInfo(key, hist);
+            }
+
+            batch.add(key, moving ? hist0 : val, updCntr);
+
+            if (batch.size() == BATCH_SIZE) {
+                assert batches != null;
+
+                batches.remove(node.id());
+
+                sendBatch(batch);
+            }
+        }
+    }
+
+    /**
+     *
+     * @param key Key.
+     * @param hist History rows.
+     * @return History entries.
+     * @throws IgniteCheckedException, if failed.
+     */
+    private CacheEntryInfoCollection fetchHistoryInfo(KeyCacheObject key, List<MvccLinkAwareSearchRow> hist)
+        throws IgniteCheckedException {
+        List<GridCacheEntryInfo> res = new ArrayList<>();
+
+        for (int i = 0; i < hist.size(); i++) {
+            MvccLinkAwareSearchRow row0 = hist.get(i);
+
+            MvccDataRow row = new MvccDataRow(cctx.group(),
+                row0.hash(),
+                row0.link(),
+                key.partition(),
+                CacheDataRowAdapter.RowData.NO_KEY,
+                row0.mvccCoordinatorVersion(),
+                row0.mvccCounter(),
+                row0.mvccOperationCounter());
+
+            GridCacheMvccEntryInfo entry = new GridCacheMvccEntryInfo();
+
+            entry.version(row.version());
+            entry.mvccVersion(row);
+            entry.newMvccVersion(row);
+            entry.value(row.value());
+            entry.expireTime(row.expireTime());
+
+            if (MvccUtils.compare(mvccSnapshot, row.mvccCoordinatorVersion(), row.mvccCounter()) != 0) {
+                entry.mvccTxState(row.mvccTxState() != TxState.NA ? row.mvccTxState() :
+                    MvccUtils.state(cctx, row.mvccCoordinatorVersion(), row.mvccCounter(), row.mvccOperationCounter()));
+            }
+
+            if (MvccUtils.compare(mvccSnapshot, row.newMvccCoordinatorVersion(), row.newMvccCounter()) != 0) {
+                entry.newMvccTxState(row.newMvccTxState() != TxState.NA ? row.newMvccTxState() :
+                    MvccUtils.state(cctx, row.newMvccCoordinatorVersion(), row.newMvccCounter(),
+                    row.newMvccOperationCounter()));
+            }
+
+            res.add(entry);
+        }
+
+        return new CacheEntryInfoCollection(res);
+    }
+
+    /** */
+    private boolean newRemoteTx(ClusterNode node) {
+        Set<ClusterNode> nodes = tx.lockTransactionNodes();
+
+        return nodes == null || !nodes.contains(node);
+    }
+
+    /**
+     * Checks if there free space in batches or free slot in in-flight batches is available for the given key.
+     *
+     * @param key Key.
+     * @return {@code True} if there is possible to add this key to batch or send ready batch.
+     */
+    @SuppressWarnings("ForLoopReplaceableByForEach")
+    private boolean ensureFreeSlot(KeyCacheObject key) {
+        if (F.isEmpty(batches) || F.isEmpty(pending))
+            return true;
+
+        // Check possibility of adding to batch and sending.
+        for (ClusterNode node : backupNodes(key)) {
+            if (skipNearNodeUpdates && node.id().equals(nearNodeId) && !isMoving(node, key.partition()))
+                continue;
+
+            Batch batch = batches.get(node.id());
+
+            // We can add key if batch is not full.
+            if (batch == null || batch.size() < BATCH_SIZE - 1)
+                continue;
+
+            ConcurrentMap<Integer, Batch> pending0 = pending.get(node.id());
+
+            assert pending0 == null || pending0.size() <= BATCHES_PER_NODE;
+
+            if (pending0 != null && (pending0.containsKey(FIRST_BATCH_ID) || pending0.size() == BATCHES_PER_NODE))
+                return false;
+        }
+
+        return true;
+    }
+
+    /**
+     * Send batch request to remote data node.
+     *
+     * @param batch Batch.
+     */
+    private void sendBatch(Batch batch) throws IgniteCheckedException {
+        assert batch != null && !batch.node().isLocal();
+
+        ClusterNode node = batch.node();
+
+        updateMappings(node);
+
+        GridDhtTxQueryEnlistRequest req;
+
+        if (newRemoteTx(node)) {
+            tx.addLockTransactionNode(node);
+
+            // If this is a first request to this node, send full info.
+            req = new GridDhtTxQueryFirstEnlistRequest(cctx.cacheId(),
+                futId,
+                cctx.localNodeId(),
+                tx.topologyVersionSnapshot(),
+                lockVer,
+                mvccSnapshot,
+                tx.remainingTime(),
+                tx.taskNameHash(),
+                nearNodeId,
+                nearLockVer,
+                it.operation(),
+                FIRST_BATCH_ID,
+                batch.keys(),
+                batch.values(),
+                batch.updateCounters());
+        }
+        else {
+            // Send only keys, values, LockVersion and batchId if this is not a first request to this backup.
+            req = new GridDhtTxQueryEnlistRequest(cctx.cacheId(),
+                futId,
+                lockVer,
+                it.operation(),
+                ++batchIdCntr,
+                mvccSnapshot.operationCounter(),
+                batch.keys(),
+                batch.values(),
+                batch.updateCounters());
+        }
+
+        ConcurrentMap<Integer, Batch> pending0 = null;
+
+        if (pending == null)
+            pending = new ConcurrentHashMap<>();
+        else
+            pending0 = pending.get(node.id());
+
+        if (pending0 == null)
+            pending.put(node.id(), pending0 = new ConcurrentHashMap<>());
+
+        Batch prev = pending0.put(req.batchId(), batch);
+
+        assert prev == null;
+
+        cctx.io().send(node, req, cctx.ioPolicy());
+    }
+
+    /** */
+    private synchronized void updateMappings(ClusterNode node) throws IgniteCheckedException {
+        checkCompleted();
+
+        Map<UUID, GridDistributedTxMapping> m = tx.dhtMap;
+
+        GridDistributedTxMapping mapping = m.get(node.id());
+
+        if (mapping == null)
+            m.put(node.id(), mapping = new GridDistributedTxMapping(node));
+
+        mapping.markQueryUpdate();
+    }
+
+    /**
+     * @param key Key.
+     * @return Backup nodes for the given key.
+     */
+    @NotNull private List<ClusterNode> backupNodes(KeyCacheObject key) {
+        List<ClusterNode> dhtNodes = cctx.affinity().nodesByKey(key, tx.topologyVersion());
+
+        assert !dhtNodes.isEmpty() && dhtNodes.get(0).id().equals(cctx.localNodeId()) :
+            "localNode = " + cctx.localNodeId() + ", dhtNodes = " + dhtNodes;
+
+        if (dhtNodes.size() == 1)
+            return Collections.emptyList();
+
+        return dhtNodes.subList(1, dhtNodes.size());
+    }
+
+    /**
+     * Checks whether all the necessary partitions are in {@link GridDhtPartitionState#OWNING} state.
+     *
+     * @param parts Partitions.
+     * @throws ClusterTopologyCheckedException If failed.
+     */
+    @SuppressWarnings("ForLoopReplaceableByForEach")
+    private void checkPartitions(@Nullable int[] parts) throws ClusterTopologyCheckedException {
+        if (cctx.isLocal() || !cctx.rebalanceEnabled())
+            return;
+
+        if (parts == null)
+            parts = U.toIntArray(
+                cctx.affinity()
+                    .primaryPartitions(cctx.localNodeId(), tx.topologyVersionSnapshot()));
+
+        GridDhtPartitionTopology top = cctx.topology();
+
+        try {
+            top.readLock();
+
+            for (int i = 0; i < parts.length; i++) {
+                GridDhtLocalPartition p = top.localPartition(parts[i]);
+
+                if (p == null || p.state() != GridDhtPartitionState.OWNING)
+                    throw new ClusterTopologyCheckedException("Cannot run update query. " +
+                        "Node must own all the necessary partitions."); // TODO IGNITE-7185 Send retry instead.
+            }
+        }
+        finally {
+            top.readUnlock();
+        }
+    }
+
+    /**
+     * @param part Partition.
+     * @return {@code true} if the given partition is rebalancing to any backup node.
+     */
+    private boolean isMoving(int part) {
+        if (movingParts == null)
+            movingParts = new HashMap<>();
+
+        Boolean res = movingParts.get(part);
+
+        if (res != null)
+            return res;
+
+        List<ClusterNode> dhtNodes = cctx.affinity().nodesByPartition(part, tx.topologyVersion());
+
+        for (int i = 1; i < dhtNodes.size(); i++) {
+            ClusterNode node = dhtNodes.get(i);
+            if (isMoving(node, part)) {
+                movingParts.put(part, Boolean.TRUE);
+
+                return true;
+            }
+        }
+
+        movingParts.put(part, Boolean.FALSE);
+
+        return false;
+    }
+
+    /**
+     * @param node Cluster node.
+     * @param part Partition.
+     * @return {@code true} if the given partition is rebalancing to the given node.
+     */
+    private boolean isMoving(ClusterNode node, int part) {
+        GridDhtPartitionState partState = cctx.topology().partitionState(node.id(), part);
+
+        return partState != GridDhtPartitionState.OWNING && partState != GridDhtPartitionState.EVICTED;
+    }
+
+    /** */
+    private void checkCompleted() throws IgniteCheckedException {
+        if (isDone())
+            throw new IgniteCheckedException("Future is done.");
+    }
+
+    /**
+     * Callback on backup response.
+     *
+     * @param nodeId Backup node.
+     * @param res Response.
+     */
+    public void onResult(UUID nodeId, GridDhtTxQueryEnlistResponse res) {
+        if (res.error() != null) {
+            onDone(new IgniteCheckedException("Failed to update backup node: [localNodeId=" + cctx.localNodeId() +
+                ", remoteNodeId=" + nodeId + ']', res.error()));
+
+            return;
+        }
+
+        assert pending != null;
+
+        ConcurrentMap<Integer, Batch> pending0 = pending.get(nodeId);
+
+        assert pending0 != null;
+
+        Batch rmv = pending0.remove(res.batchId());
+
+        assert rmv != null;
+
+        continueLoop(false);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean trackable() {
+        return true;
+    }
+
+    /** {@inheritDoc} */
+    @Override public void markNotTrackable() {
+        // No-op.
+    }
+
+    /** {@inheritDoc} */
+    @Override public IgniteUuid futureId() {
+        return futId;
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean onNodeLeft(UUID nodeId) {
+        boolean backupLeft = false;
+
+        Set<ClusterNode> nodes = tx.lockTransactionNodes();
+
+        if (!F.isEmpty(nodes)) {
+            for (ClusterNode node : nodes) {
+                if (node.id().equals(nodeId)) {
+                    backupLeft = true;
+
+                    break;
+                }
+            }
+        }
+
+        return (backupLeft || nearNodeId.equals(nodeId)) && onDone(
+            new ClusterTopologyCheckedException((backupLeft ? "Backup" : "Requesting") +
+                " node left the grid [nodeId=" + nodeId + ']'));
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean onDone(@Nullable Long res, @Nullable Throwable err) {
+        assert res != null || err != null;
+
+        if (!DONE_UPD.compareAndSet(this, 0, 1))
+            return false;
+
+        if (err == null)
+            clearLockFuture();
+
+        // To prevent new remote transactions creation
+        // after future is cancelled by rollback.
+        synchronized (this) {
+            boolean done = super.onDone(res, err);
+
+            assert done;
+
+            if (log.isDebugEnabled())
+                log.debug("Completing future: " + this);
+
+            // Clean up.
+            cctx.mvcc().removeFuture(futId);
+
+            if (timeoutObj != null)
+                cctx.time().removeTimeoutObject(timeoutObj);
+
+            U.close(it, log);
+
+            return true;
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void onError(Throwable error) {
+        onDone(error);
+    }
+
+    /**
+     * @return Timeout exception.
+     */
+    @NotNull protected IgniteTxTimeoutCheckedException timeoutException() {
+        return new IgniteTxTimeoutCheckedException("Failed to acquire lock within provided timeout for " +
+            "transaction [timeout=" + timeout + ", tx=" + tx + ']');
+    }
+
+    /**
+     * A batch of rows
+     */
+    private static class Batch {
+        /** Node ID. */
+        @GridToStringExclude
+        private final ClusterNode node;
+
+        /** */
+        private List<KeyCacheObject> keys;
+
+        /**
+         * Values collection.
+         * Items can be either {@link CacheObject} or preload entries collection {@link CacheEntryInfoCollection}.
+         */
+        private List<Message> vals;
+
+        /** Update counters. */
+        private GridLongList updCntrs;
+
+        /**
+         * @param node Cluster node.
+         */
+        private Batch(ClusterNode node) {
+            this.node = node;
+        }
+
+        /**
+         * @return Node.
+         */
+        public ClusterNode node() {
+            return node;
+        }
+
+        /**
+         * Adds a row to batch.
+         *
+         * @param key Key.
+         * @param val Value or preload entries collection.
+         */
+        public void add(KeyCacheObject key, Message val, long updCntr) {
+            assert val == null || val instanceof CacheObject || val instanceof CacheEntryInfoCollection;
+            assert updCntr > 0;
+
+            if (keys == null)
+                keys = new ArrayList<>();
+
+            keys.add(key);
+
+            if (val != null) {
+                if (vals == null)
+                    vals = new ArrayList<>();
+
+                vals.add(val);
+            }
+
+            if (updCntrs == null)
+                updCntrs = new GridLongList();
+
+            updCntrs.add(updCntr);
+
+            assert (vals == null) || keys.size() == vals.size();
+        }
+
+        /**
+         * @return number of rows.
+         */
+        public int size() {
+            return keys == null ? 0 : keys.size();
+        }
+
+        /**
+         * @return Collection of row keys.
+         */
+        public List<KeyCacheObject> keys() {
+            return keys;
+        }
+
+        /**
+         * @return Collection of row values.
+         */
+        public List<Message> values() {
+            return vals;
+        }
+
+        /**
+         * @return Update counters.
+         */
+        public GridLongList updateCounters() {
+            return updCntrs;
+        }
+    }
+
+    /**
+     * Lock request timeout object.
+     */
+    protected 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/dht/GridDhtTxFinishFuture.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxFinishFuture.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxFinishFuture.java
index 2f36053..c0a3845 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxFinishFuture.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxFinishFuture.java
@@ -26,19 +26,23 @@ 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.InvalidEnvironmentException;
 import org.apache.ignite.internal.IgniteDiagnosticAware;
 import org.apache.ignite.internal.IgniteDiagnosticPrepareContext;
 import org.apache.ignite.internal.IgniteInternalFuture;
+import org.apache.ignite.internal.InvalidEnvironmentException;
 import org.apache.ignite.internal.NodeStoppingException;
 import org.apache.ignite.internal.cluster.ClusterTopologyCheckedException;
 import org.apache.ignite.internal.processors.cache.GridCacheCompoundIdentityFuture;
 import org.apache.ignite.internal.processors.cache.GridCacheFuture;
 import org.apache.ignite.internal.processors.cache.GridCacheSharedContext;
 import org.apache.ignite.internal.processors.cache.distributed.GridDistributedTxMapping;
+import org.apache.ignite.internal.processors.cache.mvcc.MvccCoordinator;
+import org.apache.ignite.internal.processors.cache.mvcc.MvccFuture;
+import org.apache.ignite.internal.processors.cache.mvcc.MvccSnapshot;
 import org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx;
 import org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry;
 import org.apache.ignite.internal.processors.cache.version.GridCacheVersion;
+import org.apache.ignite.internal.util.GridLongList;
 import org.apache.ignite.internal.util.future.GridFutureAdapter;
 import org.apache.ignite.internal.util.tostring.GridToStringExclude;
 import org.apache.ignite.internal.util.tostring.GridToStringInclude;
@@ -284,6 +288,8 @@ public final class GridDhtTxFinishFuture<K, V> extends GridCacheCompoundIdentity
     public void finish(boolean commit) {
         boolean sync;
 
+        assert !tx.queryEnlisted() || tx.mvccSnapshot() != null;
+
         if (!F.isEmpty(dhtMap) || !F.isEmpty(nearMap))
             sync = finish(commit, dhtMap, nearMap);
         else if (!commit && !F.isEmpty(tx.lockTransactionNodes()))
@@ -292,6 +298,22 @@ public final class GridDhtTxFinishFuture<K, V> extends GridCacheCompoundIdentity
             // No backup or near nodes to send commit message to (just complete then).
             sync = false;
 
+        GridLongList waitTxs = tx.mvccWaitTransactions();
+
+        if (waitTxs != null) {
+            MvccSnapshot snapshot = tx.mvccSnapshot();
+
+            assert snapshot != null;
+
+            MvccCoordinator crd = cctx.coordinators().currentCoordinator();
+
+            if (crd != null && crd.coordinatorVersion() == snapshot.coordinatorVersion()) {
+                add((IgniteInternalFuture)cctx.coordinators().waitTxsFuture(crd.nodeId(), waitTxs));
+
+                sync = true;
+            }
+        }
+
         markInitialized();
 
         if (!sync)
@@ -310,7 +332,7 @@ public final class GridDhtTxFinishFuture<K, V> extends GridCacheCompoundIdentity
 
         boolean sync = tx.syncMode() == FULL_SYNC;
 
-        if (tx.explicitLock())
+        if (tx.explicitLock() || tx.queryEnlisted())
             sync = true;
 
         boolean res = false;
@@ -348,7 +370,8 @@ public final class GridDhtTxFinishFuture<K, V> extends GridCacheCompoundIdentity
                 tx.taskNameHash(),
                 tx.activeCachesDeploymentEnabled(),
                 false,
-                false);
+                false,
+                tx.mvccSnapshot());
 
             try {
                 cctx.io().send(n, req, tx.ioPolicy());
@@ -396,15 +419,23 @@ public final class GridDhtTxFinishFuture<K, V> extends GridCacheCompoundIdentity
         if (tx.onePhaseCommit())
             return false;
 
+        assert !commit || !tx.txState().mvccEnabled(cctx) || tx.mvccSnapshot() != null || F.isEmpty(tx.writeEntries());
+
         boolean sync = tx.syncMode() == FULL_SYNC;
 
-        if (tx.explicitLock())
+        if (tx.explicitLock() || tx.queryEnlisted())
             sync = true;
 
         boolean res = false;
 
         int miniId = 0;
 
+        // Do not need process active transactions on backups.
+        MvccSnapshot mvccSnapshot = tx.mvccSnapshot();
+
+        if (mvccSnapshot != null)
+            mvccSnapshot = mvccSnapshot.withoutActiveTransactions();
+
         // Create mini futures.
         for (GridDistributedTxMapping dhtMapping : dhtMap.values()) {
             ClusterNode n = dhtMapping.primary();
@@ -413,7 +444,7 @@ public final class GridDhtTxFinishFuture<K, V> extends GridCacheCompoundIdentity
 
             GridDistributedTxMapping nearMapping = nearMap.get(n.id());
 
-            if (dhtMapping.empty() && nearMapping != null && nearMapping.empty())
+            if (!dhtMapping.queryUpdate() && dhtMapping.empty() && nearMapping != null && nearMapping.empty())
                 // Nothing to send.
                 continue;
 
@@ -426,6 +457,11 @@ public final class GridDhtTxFinishFuture<K, V> extends GridCacheCompoundIdentity
             for (IgniteTxEntry e : dhtMapping.entries())
                 updCntrs.add(e.updateCounter());
 
+            Map<Integer, GridDhtPartitionsUpdateCountersMap> updCntrsMap = null;
+
+            if (dhtMapping.queryUpdate() && commit)
+                updCntrsMap = tx.updateCountersForNode(n);
+
             GridDhtTxFinishRequest req = new GridDhtTxFinishRequest(
                 tx.nearNodeId(),
                 futId,
@@ -451,7 +487,9 @@ public final class GridDhtTxFinishFuture<K, V> extends GridCacheCompoundIdentity
                 tx.activeCachesDeploymentEnabled(),
                 updCntrs,
                 false,
-                false);
+                false,
+                mvccSnapshot,
+                updCntrsMap);
 
             req.writeVersion(tx.writeVersion() != null ? tx.writeVersion() : tx.xidVersion());
 
@@ -520,7 +558,8 @@ public final class GridDhtTxFinishFuture<K, V> extends GridCacheCompoundIdentity
                     tx.taskNameHash(),
                     tx.activeCachesDeploymentEnabled(),
                     false,
-                    false);
+                    false,
+                    mvccSnapshot);
 
                 req.writeVersion(tx.writeVersion());
 
@@ -565,22 +604,35 @@ public final class GridDhtTxFinishFuture<K, V> extends GridCacheCompoundIdentity
         if (!isDone()) {
             for (IgniteInternalFuture fut : futures()) {
                 if (!fut.isDone()) {
-                    MiniFuture f = (MiniFuture)fut;
-
-                    if (!f.node().isLocal()) {
-                        GridCacheVersion dhtVer = tx.xidVersion();
-                        GridCacheVersion nearVer = tx.nearXidVersion();
+                    if (MiniFuture.class.isInstance(fut)) {
+                        MiniFuture f = (MiniFuture)fut;
+
+                        if (!f.node().isLocal()) {
+                            GridCacheVersion dhtVer = tx.xidVersion();
+                            GridCacheVersion nearVer = tx.nearXidVersion();
+
+                            ctx.remoteTxInfo(f.node().id(), dhtVer, nearVer, "GridDhtTxFinishFuture " +
+                                "waiting for response [node=" + f.node().id() +
+                                ", topVer=" + tx.topologyVersion() +
+                                ", dhtVer=" + dhtVer +
+                                ", nearVer=" + nearVer +
+                                ", futId=" + futId +
+                                ", miniId=" + f.futId +
+                                ", tx=" + tx + ']');
+
+                            return;
+                        }
+                    }
+                    else if (fut instanceof MvccFuture) {
+                        MvccFuture f = (MvccFuture)fut;
 
-                        ctx.remoteTxInfo(f.node().id(), dhtVer, nearVer, "GridDhtTxFinishFuture " +
-                            "waiting for response [node=" + f.node().id() +
-                            ", topVer=" + tx.topologyVersion() +
-                            ", dhtVer=" + dhtVer +
-                            ", nearVer=" + nearVer +
-                            ", futId=" + futId +
-                            ", miniId=" + f.futId +
-                            ", tx=" + tx + ']');
+                        if (!cctx.localNodeId().equals(f.coordinatorNodeId())) {
+                            ctx.basicInfo(f.coordinatorNodeId(), "GridDhtTxFinishFuture " +
+                                "waiting for mvcc coordinator reply [mvccCrdNode=" + f.coordinatorNodeId() +
+                                ", loc=" + f.coordinatorNodeId().equals(cctx.localNodeId()) + ']');
 
-                        return;
+                            return;
+                        }
                     }
                 }
             }
@@ -592,9 +644,20 @@ public final class GridDhtTxFinishFuture<K, V> extends GridCacheCompoundIdentity
         Collection<String> futs = F.viewReadOnly(futures(), new C1<IgniteInternalFuture<?>, String>() {
             @SuppressWarnings("unchecked")
             @Override public String apply(IgniteInternalFuture<?> f) {
-                return "[node=" + ((MiniFuture)f).node().id() +
-                    ", loc=" + ((MiniFuture)f).node().isLocal() +
-                    ", done=" + f.isDone() + "]";
+                if (f.getClass() == MiniFuture.class) {
+                    return "[node=" + ((MiniFuture)f).node().id() +
+                        ", loc=" + ((MiniFuture)f).node().isLocal() +
+                        ", done=" + f.isDone() + "]";
+                }
+                else if (f instanceof MvccFuture) {
+                    MvccFuture crdFut = (MvccFuture)f;
+
+                    return "[mvccCrdNode=" + crdFut.coordinatorNodeId() +
+                        ", loc=" + crdFut.coordinatorNodeId().equals(cctx.localNodeId()) +
+                        ", done=" + f.isDone() + "]";
+                }
+                else
+                    return f.toString();
             }
         });
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/00bb4d4d/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxFinishRequest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxFinishRequest.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxFinishRequest.java
index 823b5fe..2696c0e 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxFinishRequest.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxFinishRequest.java
@@ -20,11 +20,14 @@ package org.apache.ignite.internal.processors.cache.distributed.dht;
 import java.io.Externalizable;
 import java.nio.ByteBuffer;
 import java.util.Collection;
+import java.util.Map;
 import java.util.UUID;
 import org.apache.ignite.cache.CacheWriteSynchronizationMode;
 import org.apache.ignite.internal.GridDirectCollection;
+import org.apache.ignite.internal.GridDirectMap;
 import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
 import org.apache.ignite.internal.processors.cache.distributed.GridDistributedTxFinishRequest;
+import org.apache.ignite.internal.processors.cache.mvcc.MvccSnapshot;
 import org.apache.ignite.internal.processors.cache.version.GridCacheVersion;
 import org.apache.ignite.internal.util.GridLongList;
 import org.apache.ignite.internal.util.tostring.GridToStringInclude;
@@ -67,6 +70,12 @@ public class GridDhtTxFinishRequest extends GridDistributedTxFinishRequest {
     /** One phase commit write version. */
     private GridCacheVersion writeVer;
 
+    /** */
+    private MvccSnapshot mvccSnapshot;
+
+    /** Map of update counters made by this tx. Mapping: cacheId -> partId -> updCntr. */
+    @GridDirectMap(keyType = Integer.class, valueType = GridDhtPartitionsUpdateCountersMap.class)
+    private Map<Integer, GridDhtPartitionsUpdateCountersMap> updCntrs;
     /**
      * Empty constructor required for {@link Externalizable}.
      */
@@ -122,7 +131,8 @@ public class GridDhtTxFinishRequest extends GridDistributedTxFinishRequest {
         int taskNameHash,
         boolean addDepInfo,
         boolean retVal,
-        boolean waitRemoteTxs
+        boolean waitRemoteTxs,
+        MvccSnapshot mvccSnapshot
     ) {
         super(
             xidVer,
@@ -151,6 +161,7 @@ public class GridDhtTxFinishRequest extends GridDistributedTxFinishRequest {
         this.nearNodeId = nearNodeId;
         this.isolation = isolation;
         this.miniId = miniId;
+        this.mvccSnapshot = mvccSnapshot;
 
         needReturnValue(retVal);
         waitRemoteTransactions(waitRemoteTxs);
@@ -207,7 +218,9 @@ public class GridDhtTxFinishRequest extends GridDistributedTxFinishRequest {
         boolean addDepInfo,
         Collection<Long> updateIdxs,
         boolean retVal,
-        boolean waitRemoteTxs
+        boolean waitRemoteTxs,
+        MvccSnapshot mvccSnapshot,
+        Map<Integer, GridDhtPartitionsUpdateCountersMap> updCntrs
     ) {
         this(nearNodeId,
             futId,
@@ -232,7 +245,8 @@ public class GridDhtTxFinishRequest extends GridDistributedTxFinishRequest {
             taskNameHash,
             addDepInfo,
             retVal,
-            waitRemoteTxs);
+            waitRemoteTxs,
+            mvccSnapshot);
 
         if (updateIdxs != null && !updateIdxs.isEmpty()) {
             partUpdateCnt = new GridLongList(updateIdxs.size());
@@ -240,6 +254,15 @@ public class GridDhtTxFinishRequest extends GridDistributedTxFinishRequest {
             for (Long idx : updateIdxs)
                 partUpdateCnt.add(idx);
         }
+
+        this.updCntrs = updCntrs;
+    }
+
+    /**
+     * @return Counter.
+     */
+    public MvccSnapshot mvccSnapshot() {
+        return mvccSnapshot;
     }
 
     /**
@@ -339,6 +362,12 @@ public class GridDhtTxFinishRequest extends GridDistributedTxFinishRequest {
     public void needReturnValue(boolean retVal) {
         setFlag(retVal, NEED_RETURN_VALUE_FLAG_MASK);
     }
+    /**
+     * @return Partition update counters map.
+     */
+    public Map<Integer, GridDhtPartitionsUpdateCountersMap> updateCountersMap() {
+        return updCntrs;
+    }
 
     /** {@inheritDoc} */
     @Override public boolean writeTo(ByteBuffer buf, MessageWriter writer) {
@@ -368,24 +397,36 @@ public class GridDhtTxFinishRequest extends GridDistributedTxFinishRequest {
                 writer.incrementState();
 
             case 23:
-                if (!writer.writeUuid("nearNodeId", nearNodeId))
+                if (!writer.writeMessage("mvccSnapshot", mvccSnapshot))
                     return false;
 
                 writer.incrementState();
 
             case 24:
-                if (!writer.writeMessage("partUpdateCnt", partUpdateCnt))
+                if (!writer.writeUuid("nearNodeId", nearNodeId))
                     return false;
 
                 writer.incrementState();
 
             case 25:
-                if (!writer.writeCollection("pendingVers", pendingVers, MessageCollectionItemType.MSG))
+                if (!writer.writeMessage("partUpdateCnt", partUpdateCnt))
                     return false;
 
                 writer.incrementState();
 
             case 26:
+                if (!writer.writeCollection("pendingVers", pendingVers, MessageCollectionItemType.MSG))
+                    return false;
+
+                writer.incrementState();
+
+            case 27:
+                if (!writer.writeMap("updCntrs", updCntrs, MessageCollectionItemType.INT, MessageCollectionItemType.MSG))
+                    return false;
+
+                writer.incrementState();
+
+            case 28:
                 if (!writer.writeMessage("writeVer", writeVer))
                     return false;
 
@@ -428,7 +469,7 @@ public class GridDhtTxFinishRequest extends GridDistributedTxFinishRequest {
                 reader.incrementState();
 
             case 23:
-                nearNodeId = reader.readUuid("nearNodeId");
+                mvccSnapshot = reader.readMessage("mvccSnapshot");
 
                 if (!reader.isLastRead())
                     return false;
@@ -436,7 +477,7 @@ public class GridDhtTxFinishRequest extends GridDistributedTxFinishRequest {
                 reader.incrementState();
 
             case 24:
-                partUpdateCnt = reader.readMessage("partUpdateCnt");
+                nearNodeId = reader.readUuid("nearNodeId");
 
                 if (!reader.isLastRead())
                     return false;
@@ -444,7 +485,7 @@ public class GridDhtTxFinishRequest extends GridDistributedTxFinishRequest {
                 reader.incrementState();
 
             case 25:
-                pendingVers = reader.readCollection("pendingVers", MessageCollectionItemType.MSG);
+                partUpdateCnt = reader.readMessage("partUpdateCnt");
 
                 if (!reader.isLastRead())
                     return false;
@@ -452,6 +493,22 @@ public class GridDhtTxFinishRequest extends GridDistributedTxFinishRequest {
                 reader.incrementState();
 
             case 26:
+                pendingVers = reader.readCollection("pendingVers", MessageCollectionItemType.MSG);
+
+                if (!reader.isLastRead())
+                    return false;
+
+                reader.incrementState();
+
+            case 27:
+                updCntrs = reader.readMap("updCntrs", MessageCollectionItemType.INT, MessageCollectionItemType.MSG, false);
+
+                if (!reader.isLastRead())
+                    return false;
+
+                reader.incrementState();
+
+            case 28:
                 writeVer = reader.readMessage("writeVer");
 
                 if (!reader.isLastRead())
@@ -471,7 +528,7 @@ public class GridDhtTxFinishRequest extends GridDistributedTxFinishRequest {
 
     /** {@inheritDoc} */
     @Override public byte fieldsCount() {
-        return 27;
+        return 29;
     }
 
     /** {@inheritDoc} */

http://git-wip-us.apache.org/repos/asf/ignite/blob/00bb4d4d/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxLocal.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxLocal.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxLocal.java
index 2e19df2..a091d44 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxLocal.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxLocal.java
@@ -160,7 +160,6 @@ public class GridDhtTxLocal extends GridDhtTxLocalAdapter implements GridCacheMa
 
         assert nearNodeId != null;
         assert nearFutId != null;
-        assert nearMiniId != 0;
         assert nearXidVer != null;
 
         this.nearNodeId = nearNodeId;
@@ -424,10 +423,10 @@ public class GridDhtTxLocal extends GridDhtTxLocalAdapter implements GridCacheMa
             final IgniteInternalFuture<?> lockFut = tryRollbackAsync();
 
             if (lockFut != null) {
-                if (lockFut instanceof GridDhtLockFuture)
-                    ((GridDhtLockFuture)lockFut).onError(rollbackException());
-                else {
-                    /**
+                if (lockFut instanceof DhtLockFuture)
+                    ((DhtLockFuture<?>)lockFut).onError(rollbackException());
+                else if (!lockFut.isDone()) {
+                    /*
                      * Prevents race with {@link GridDhtTransactionalCacheAdapter#lockAllAsync
                      * (GridCacheContext, ClusterNode, GridNearLockRequest, CacheEntryPredicate[])}
                      */
@@ -546,6 +545,8 @@ public class GridDhtTxLocal extends GridDhtTxLocalAdapter implements GridCacheMa
     public IgniteInternalFuture<IgniteInternalTx> rollbackDhtLocalAsync() {
         final GridDhtTxFinishFuture fut = new GridDhtTxFinishFuture<>(cctx, this, false);
 
+        rollbackFuture(fut);
+
         cctx.mvcc().addFuture(fut, fut.futureId());
 
         GridDhtTxPrepareFuture prepFut = this.prepFut;

http://git-wip-us.apache.org/repos/asf/ignite/blob/00bb4d4d/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxLocalAdapter.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxLocalAdapter.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxLocalAdapter.java
index 604fe06..11b46a0 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxLocalAdapter.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxLocalAdapter.java
@@ -25,6 +25,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
 import org.apache.ignite.IgniteCheckedException;
 import org.apache.ignite.cluster.ClusterNode;
@@ -59,7 +60,6 @@ import org.apache.ignite.transactions.TransactionConcurrency;
 import org.apache.ignite.transactions.TransactionIsolation;
 import org.apache.ignite.transactions.TransactionState;
 import org.jetbrains.annotations.Nullable;
-import java.util.concurrent.ConcurrentHashMap;
 
 import static org.apache.ignite.internal.processors.cache.GridCacheOperation.NOOP;
 import static org.apache.ignite.transactions.TransactionState.COMMITTED;
@@ -78,7 +78,7 @@ public abstract class GridDhtTxLocalAdapter extends IgniteTxLocalAdapter {
     private static final long serialVersionUID = 0L;
 
     /** Asynchronous rollback marker for lock futures. */
-    protected static final IgniteInternalFuture<Boolean> ROLLBACK_FUT = new GridFutureAdapter<>();
+    public static final IgniteInternalFuture<Boolean> ROLLBACK_FUT = new GridFutureAdapter<>();
 
     /** Lock future updater. */
     private static final AtomicReferenceFieldUpdater<GridDhtTxLocalAdapter, IgniteInternalFuture> LOCK_FUT_UPD =
@@ -108,7 +108,7 @@ public abstract class GridDhtTxLocalAdapter extends IgniteTxLocalAdapter {
     /** Enlist or lock future what is currently in progress. */
     @SuppressWarnings("UnusedDeclaration")
     @GridToStringExclude
-    protected volatile IgniteInternalFuture<Boolean> lockFut;
+    protected volatile IgniteInternalFuture<?> lockFut;
 
     /**
      * Empty constructor required for {@link Externalizable}.
@@ -853,11 +853,17 @@ public abstract class GridDhtTxLocalAdapter extends IgniteTxLocalAdapter {
     }
 
     /**
+     * @return Lock future.
+     */
+    public IgniteInternalFuture<?> lockFuture() {
+        return lockFut;
+    }
+
+    /**
      * Atomically updates lock future.
      *
      * @param oldFut Old future.
      * @param newFut New future.
-     *
      * @return {@code true} If future was changed.
      */
     public boolean updateLockFuture(IgniteInternalFuture<?> oldFut, IgniteInternalFuture<?> newFut) {
@@ -870,20 +876,21 @@ public abstract class GridDhtTxLocalAdapter extends IgniteTxLocalAdapter {
      * @param cond Clear lock condition.
      */
     public void clearLockFuture(@Nullable IgniteInternalFuture cond) {
-        IgniteInternalFuture f = lockFut;
+        while (true) {
+            IgniteInternalFuture f = lockFut;
 
-        if (cond != null && f != cond)
-            return;
-
-        lockFut = null;
+            if (f == null
+                || f == ROLLBACK_FUT
+                || (cond != null && f != cond)
+                || updateLockFuture(f, null))
+                return;
+        }
     }
 
     /**
-     *
      * @param f Future to finish.
      * @param err Error.
      * @param clearLockFut {@code True} if need to clear lock future.
-     *
      * @return Finished future.
      */
     public <T> GridFutureAdapter<T> finishFuture(GridFutureAdapter<T> f, Throwable err, boolean clearLockFut) {
@@ -901,16 +908,13 @@ public abstract class GridDhtTxLocalAdapter extends IgniteTxLocalAdapter {
      * @return Current lock future or null if it's safe to roll back.
      */
     public @Nullable IgniteInternalFuture<?> tryRollbackAsync() {
-        IgniteInternalFuture<Boolean> fut;
-
-        while(true) {
-            fut = lockFut;
-
-            if (fut != null)
-                return fut == ROLLBACK_FUT ? null : fut;
+        while (true) {
+            final IgniteInternalFuture fut = lockFut;
 
-            if (updateLockFuture(null, ROLLBACK_FUT))
+            if (fut == ROLLBACK_FUT)
                 return null;
+            else if (updateLockFuture(fut, ROLLBACK_FUT))
+                return fut;
         }
     }
 
@@ -924,8 +928,7 @@ public abstract class GridDhtTxLocalAdapter extends IgniteTxLocalAdapter {
         if (commitOnPrepare()) {
             return finishFuture().chain(new CX1<IgniteInternalFuture<IgniteInternalTx>, GridNearTxPrepareResponse>() {
                 @Override public GridNearTxPrepareResponse applyx(IgniteInternalFuture<IgniteInternalTx> finishFut)
-                    throws IgniteCheckedException
-                {
+                    throws IgniteCheckedException {
                     return prepFut.get();
                 }
             });

http://git-wip-us.apache.org/repos/asf/ignite/blob/00bb4d4d/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxPrepareFuture.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxPrepareFuture.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxPrepareFuture.java
index d8e204a..8ac5419 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxPrepareFuture.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxPrepareFuture.java
@@ -51,16 +51,21 @@ import org.apache.ignite.internal.processors.cache.GridCacheEntryEx;
 import org.apache.ignite.internal.processors.cache.GridCacheEntryInfo;
 import org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException;
 import org.apache.ignite.internal.processors.cache.GridCacheMvccCandidate;
-import org.apache.ignite.internal.processors.cache.GridCacheVersionedFuture;
 import org.apache.ignite.internal.processors.cache.GridCacheOperation;
 import org.apache.ignite.internal.processors.cache.GridCacheReturn;
 import org.apache.ignite.internal.processors.cache.GridCacheSharedContext;
+import org.apache.ignite.internal.processors.cache.GridCacheVersionedFuture;
 import org.apache.ignite.internal.processors.cache.KeyCacheObject;
 import org.apache.ignite.internal.processors.cache.distributed.GridDistributedCacheEntry;
 import org.apache.ignite.internal.processors.cache.distributed.GridDistributedTxMapping;
 import org.apache.ignite.internal.processors.cache.distributed.near.GridNearCacheAdapter;
 import org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxPrepareRequest;
 import org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxPrepareResponse;
+import org.apache.ignite.internal.processors.cache.mvcc.MvccCoordinator;
+import org.apache.ignite.internal.processors.cache.mvcc.MvccSnapshot;
+import org.apache.ignite.internal.processors.cache.mvcc.MvccUpdateVersionAware;
+import org.apache.ignite.internal.processors.cache.mvcc.MvccVersionAware;
+import org.apache.ignite.internal.processors.cache.mvcc.txlog.TxState;
 import org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx;
 import org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry;
 import org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey;
@@ -85,6 +90,7 @@ import org.apache.ignite.internal.util.typedef.internal.CU;
 import org.apache.ignite.internal.util.typedef.internal.S;
 import org.apache.ignite.internal.util.typedef.internal.U;
 import org.apache.ignite.lang.IgniteFutureCancelledException;
+import org.apache.ignite.lang.IgniteInClosure;
 import org.apache.ignite.lang.IgniteReducer;
 import org.apache.ignite.lang.IgniteUuid;
 import org.apache.ignite.thread.IgniteThread;
@@ -256,6 +262,11 @@ public final class GridDhtTxPrepareFuture extends GridCacheCompoundFuture<Ignite
     }
 
     /** {@inheritDoc} */
+    @Nullable @Override public IgniteLogger logger() {
+        return log;
+    }
+
+    /** {@inheritDoc} */
     @Override public IgniteUuid futureId() {
         return futId;
     }
@@ -394,7 +405,8 @@ public final class GridDhtTxPrepareFuture extends GridCacheCompoundFuture<Ignite
                         entryProc,
                         tx.resolveTaskName(),
                         null,
-                        keepBinary);
+                        keepBinary,
+                        null);  // TODO IGNITE-7371
 
                     if (retVal || txEntry.op() == TRANSFORM) {
                         if (!F.isEmpty(txEntry.entryProcessors())) {
@@ -507,7 +519,8 @@ public final class GridDhtTxPrepareFuture extends GridCacheCompoundFuture<Ignite
                             /*transformClo*/null,
                             /*taskName*/null,
                             /*expiryPlc*/null,
-                            /*keepBinary*/true);
+                            /*keepBinary*/true,
+                            null); // TODO IGNITE-7371
                     }
 
                     if (oldVal != null)
@@ -886,6 +899,8 @@ public final class GridDhtTxPrepareFuture extends GridCacheCompoundFuture<Ignite
             tx.onePhaseCommit(),
             tx.activeCachesDeploymentEnabled());
 
+        res.mvccSnapshot(tx.mvccSnapshot());
+
         if (prepErr == null) {
             if (tx.needReturnValue() || tx.nearOnOriginatingNode() || tx.hasInterceptor())
                 addDhtValues(res);
@@ -1025,7 +1040,7 @@ public final class GridDhtTxPrepareFuture extends GridCacheCompoundFuture<Ignite
     public void prepare(GridNearTxPrepareRequest req) {
         assert req != null;
 
-        if (tx.empty()) {
+        if (tx.empty() && !req.queryUpdate()) {
             tx.setRollbackOnly();
 
             onDone((GridNearTxPrepareResponse)null);
@@ -1205,6 +1220,8 @@ public final class GridDhtTxPrepareFuture extends GridCacheCompoundFuture<Ignite
      *
      */
     private void prepare0() {
+        boolean skipInit = false;
+
         try {
             if (tx.serializable() && tx.optimistic()) {
                 IgniteCheckedException err0;
@@ -1239,6 +1256,29 @@ public final class GridDhtTxPrepareFuture extends GridCacheCompoundFuture<Ignite
                 }
             }
 
+            IgniteInternalFuture<MvccSnapshot> waitCrdCntrFut = null;
+
+            if (req.requestMvccCounter()) {
+                assert last;
+
+                assert tx.txState().mvccEnabled(cctx);
+
+                try {
+                    // Request snapshot locally only because
+                    // Mvcc Coordinator is expected to be local.
+                    MvccSnapshot snapshot = cctx.coordinators().tryRequestSnapshotLocal(tx);
+
+                    assert snapshot != null : tx.topologyVersion();
+
+                    tx.mvccSnapshot(snapshot);
+                }
+                catch (ClusterTopologyCheckedException e) {
+                    onDone(e);
+
+                    return;
+                }
+            }
+
             onEntriesLocked();
 
             // We are holding transaction-level locks for entries here, so we can get next write version.
@@ -1258,11 +1298,37 @@ public final class GridDhtTxPrepareFuture extends GridCacheCompoundFuture<Ignite
             if (isDone())
                 return;
 
-            if (last)
-                sendPrepareRequests();
+            if (last) {
+                if (waitCrdCntrFut != null) {
+                    skipInit = true;
+
+                    waitCrdCntrFut.listen(new IgniteInClosure<IgniteInternalFuture<MvccSnapshot>>() {
+                        @Override public void apply(IgniteInternalFuture<MvccSnapshot> fut) {
+                            try {
+                                fut.get();
+
+                                sendPrepareRequests();
+
+                                markInitialized();
+                            }
+                            catch (Throwable e) {
+                                U.error(log, "Failed to get mvcc version for tx [txId=" + tx.nearXidVersion() +
+                                    ", err=" + e + ']', e);
+
+                                GridNearTxPrepareResponse res = createPrepareResponse(e);
+
+                                onDone(res, res.error());
+                            }
+                        }
+                    });
+                }
+                else
+                    sendPrepareRequests();
+            }
         }
         finally {
-            markInitialized();
+            if (!skipInit)
+                markInitialized();
         }
     }
 
@@ -1280,15 +1346,23 @@ public final class GridDhtTxPrepareFuture extends GridCacheCompoundFuture<Ignite
             }
         }
 
+        assert !tx.txState().mvccEnabled(cctx) || !tx.onePhaseCommit() || tx.mvccSnapshot() != null;
+
         int miniId = 0;
 
         assert tx.transactionNodes() != null;
 
         final long timeout = timeoutObj != null ? timeoutObj.timeout : 0;
 
+        // Do not need process active transactions on backups.
+        MvccSnapshot mvccSnapshot = tx.mvccSnapshot();
+
+        if (mvccSnapshot != null)
+            mvccSnapshot = mvccSnapshot.withoutActiveTransactions();
+
         // Create mini futures.
         for (GridDistributedTxMapping dhtMapping : tx.dhtMap().values()) {
-            assert !dhtMapping.empty();
+            assert !dhtMapping.empty() || dhtMapping.queryUpdate();
 
             ClusterNode n = dhtMapping.primary();
 
@@ -1300,7 +1374,7 @@ public final class GridDhtTxPrepareFuture extends GridCacheCompoundFuture<Ignite
 
             Collection<IgniteTxEntry> dhtWrites = dhtMapping.writes();
 
-            if (F.isEmpty(dhtWrites) && F.isEmpty(nearWrites))
+            if (!dhtMapping.queryUpdate() && F.isEmpty(dhtWrites) && F.isEmpty(nearWrites))
                 continue;
 
             MiniFuture fut = new MiniFuture(n.id(), ++miniId, dhtMapping, nearMapping);
@@ -1325,7 +1399,10 @@ public final class GridDhtTxPrepareFuture extends GridCacheCompoundFuture<Ignite
                 tx.taskNameHash(),
                 tx.activeCachesDeploymentEnabled(),
                 tx.storeWriteThrough(),
-                retVal);
+                retVal,
+                mvccSnapshot);
+
+            req.queryUpdate(dhtMapping.queryUpdate());
 
             int idx = 0;
 
@@ -1428,7 +1505,8 @@ public final class GridDhtTxPrepareFuture extends GridCacheCompoundFuture<Ignite
                     tx.taskNameHash(),
                     tx.activeCachesDeploymentEnabled(),
                     tx.storeWriteThrough(),
-                    retVal);
+                    retVal,
+                    mvccSnapshot);
 
                 for (IgniteTxEntry entry : nearMapping.entries()) {
                     if (CU.writes().apply(entry)) {
@@ -1799,7 +1877,7 @@ public final class GridDhtTxPrepareFuture extends GridCacheCompoundFuture<Ignite
                         }
                     }
 
-                    if (dhtMapping.empty()) {
+                    if (!dhtMapping.queryUpdate() && dhtMapping.empty()) {
                         dhtMap.remove(nodeId);
 
                         if (log.isDebugEnabled())
@@ -1830,6 +1908,10 @@ public final class GridDhtTxPrepareFuture extends GridCacheCompoundFuture<Ignite
                         try {
                             if (entry.initialValue(info.value(),
                                 info.version(),
+                                cacheCtx.mvccEnabled() ? ((MvccVersionAware)info).mvccVersion() : null,
+                                cacheCtx.mvccEnabled() ? ((MvccUpdateVersionAware)info).newMvccVersion() : null,
+                                cacheCtx.mvccEnabled() ? ((MvccVersionAware)info).mvccTxState() : TxState.NA,
+                                cacheCtx.mvccEnabled() ? ((MvccUpdateVersionAware)info).newMvccTxState() : TxState.NA,
                                 info.ttl(),
                                 info.expireTime(),
                                 true,

http://git-wip-us.apache.org/repos/asf/ignite/blob/00bb4d4d/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxPrepareRequest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxPrepareRequest.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxPrepareRequest.java
index 88da7b0..a71fb6c 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxPrepareRequest.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtTxPrepareRequest.java
@@ -34,6 +34,7 @@ import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
 import org.apache.ignite.internal.processors.cache.GridCacheContext;
 import org.apache.ignite.internal.processors.cache.GridCacheSharedContext;
 import org.apache.ignite.internal.processors.cache.distributed.GridDistributedTxPrepareRequest;
+import org.apache.ignite.internal.processors.cache.mvcc.MvccSnapshot;
 import org.apache.ignite.internal.processors.cache.transactions.IgniteTxEntry;
 import org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey;
 import org.apache.ignite.internal.processors.cache.version.GridCacheVersion;
@@ -103,6 +104,9 @@ public class GridDhtTxPrepareRequest extends GridDistributedTxPrepareRequest {
     @GridDirectTransient
     private List<IgniteTxKey> nearWritesCacheMissed;
 
+    /** */
+    private MvccSnapshot mvccSnapshot;
+
     /** {@code True} if remote tx should skip adding itself to completed versions map on finish. */
     private boolean skipCompletedVers;
 
@@ -144,7 +148,8 @@ public class GridDhtTxPrepareRequest extends GridDistributedTxPrepareRequest {
         int taskNameHash,
         boolean addDepInfo,
         boolean storeWriteThrough,
-        boolean retVal) {
+        boolean retVal,
+        MvccSnapshot mvccInfo) {
         super(tx,
             timeout,
             null,
@@ -165,6 +170,7 @@ public class GridDhtTxPrepareRequest extends GridDistributedTxPrepareRequest {
         this.nearXidVer = nearXidVer;
         this.subjId = subjId;
         this.taskNameHash = taskNameHash;
+        this.mvccSnapshot = mvccInfo;
 
         storeWriteThrough(storeWriteThrough);
         needReturnValue(retVal);
@@ -177,6 +183,13 @@ public class GridDhtTxPrepareRequest extends GridDistributedTxPrepareRequest {
     }
 
     /**
+     * @return Mvcc info.
+     */
+    public MvccSnapshot mvccSnapshot() {
+        return mvccSnapshot;
+    }
+
+    /**
      * @return Near cache writes for which cache was not found (possible if client near cache was closed).
      */
     @Nullable public List<IgniteTxKey> nearWritesCacheMissed() {
@@ -414,60 +427,66 @@ public class GridDhtTxPrepareRequest extends GridDistributedTxPrepareRequest {
                 writer.incrementState();
 
             case 23:
-                if (!writer.writeUuid("nearNodeId", nearNodeId))
+                if (!writer.writeMessage("mvccSnapshot", mvccSnapshot))
                     return false;
 
                 writer.incrementState();
 
             case 24:
-                if (!writer.writeCollection("nearWrites", nearWrites, MessageCollectionItemType.MSG))
+                if (!writer.writeUuid("nearNodeId", nearNodeId))
                     return false;
 
                 writer.incrementState();
 
             case 25:
-                if (!writer.writeMessage("nearXidVer", nearXidVer))
+                if (!writer.writeCollection("nearWrites", nearWrites, MessageCollectionItemType.MSG))
                     return false;
 
                 writer.incrementState();
 
             case 26:
-                if (!writer.writeCollection("ownedKeys", ownedKeys, MessageCollectionItemType.MSG))
+                if (!writer.writeMessage("nearXidVer", nearXidVer))
                     return false;
 
                 writer.incrementState();
 
             case 27:
-                if (!writer.writeCollection("ownedVals", ownedVals, MessageCollectionItemType.MSG))
+                if (!writer.writeCollection("ownedKeys", ownedKeys, MessageCollectionItemType.MSG))
                     return false;
 
                 writer.incrementState();
 
             case 28:
-                if (!writer.writeBitSet("preloadKeys", preloadKeys))
+                if (!writer.writeCollection("ownedVals", ownedVals, MessageCollectionItemType.MSG))
                     return false;
 
                 writer.incrementState();
 
             case 29:
-                if (!writer.writeBoolean("skipCompletedVers", skipCompletedVers))
+                if (!writer.writeBitSet("preloadKeys", preloadKeys))
                     return false;
 
                 writer.incrementState();
 
             case 30:
-                if (!writer.writeUuid("subjId", subjId))
+                if (!writer.writeBoolean("skipCompletedVers", skipCompletedVers))
                     return false;
 
                 writer.incrementState();
 
             case 31:
-                if (!writer.writeInt("taskNameHash", taskNameHash))
+                if (!writer.writeUuid("subjId", subjId))
                     return false;
 
                 writer.incrementState();
 
             case 32:
+                if (!writer.writeInt("taskNameHash", taskNameHash))
+                    return false;
+
+                writer.incrementState();
+
+            case 33:
                 if (!writer.writeMessage("topVer", topVer))
                     return false;
 
@@ -514,7 +533,7 @@ public class GridDhtTxPrepareRequest extends GridDistributedTxPrepareRequest {
                 reader.incrementState();
 
             case 23:
-                nearNodeId = reader.readUuid("nearNodeId");
+                mvccSnapshot = reader.readMessage("mvccSnapshot");
 
                 if (!reader.isLastRead())
                     return false;
@@ -522,7 +541,7 @@ public class GridDhtTxPrepareRequest extends GridDistributedTxPrepareRequest {
                 reader.incrementState();
 
             case 24:
-                nearWrites = reader.readCollection("nearWrites", MessageCollectionItemType.MSG);
+                nearNodeId = reader.readUuid("nearNodeId");
 
                 if (!reader.isLastRead())
                     return false;
@@ -530,7 +549,7 @@ public class GridDhtTxPrepareRequest extends GridDistributedTxPrepareRequest {
                 reader.incrementState();
 
             case 25:
-                nearXidVer = reader.readMessage("nearXidVer");
+                nearWrites = reader.readCollection("nearWrites", MessageCollectionItemType.MSG);
 
                 if (!reader.isLastRead())
                     return false;
@@ -538,7 +557,7 @@ public class GridDhtTxPrepareRequest extends GridDistributedTxPrepareRequest {
                 reader.incrementState();
 
             case 26:
-                ownedKeys = reader.readCollection("ownedKeys", MessageCollectionItemType.MSG);
+                nearXidVer = reader.readMessage("nearXidVer");
 
                 if (!reader.isLastRead())
                     return false;
@@ -546,7 +565,7 @@ public class GridDhtTxPrepareRequest extends GridDistributedTxPrepareRequest {
                 reader.incrementState();
 
             case 27:
-                ownedVals = reader.readCollection("ownedVals", MessageCollectionItemType.MSG);
+                ownedKeys = reader.readCollection("ownedKeys", MessageCollectionItemType.MSG);
 
                 if (!reader.isLastRead())
                     return false;
@@ -554,7 +573,7 @@ public class GridDhtTxPrepareRequest extends GridDistributedTxPrepareRequest {
                 reader.incrementState();
 
             case 28:
-                preloadKeys = reader.readBitSet("preloadKeys");
+                ownedVals = reader.readCollection("ownedVals", MessageCollectionItemType.MSG);
 
                 if (!reader.isLastRead())
                     return false;
@@ -562,7 +581,7 @@ public class GridDhtTxPrepareRequest extends GridDistributedTxPrepareRequest {
                 reader.incrementState();
 
             case 29:
-                skipCompletedVers = reader.readBoolean("skipCompletedVers");
+                preloadKeys = reader.readBitSet("preloadKeys");
 
                 if (!reader.isLastRead())
                     return false;
@@ -570,7 +589,7 @@ public class GridDhtTxPrepareRequest extends GridDistributedTxPrepareRequest {
                 reader.incrementState();
 
             case 30:
-                subjId = reader.readUuid("subjId");
+                skipCompletedVers = reader.readBoolean("skipCompletedVers");
 
                 if (!reader.isLastRead())
                     return false;
@@ -578,7 +597,7 @@ public class GridDhtTxPrepareRequest extends GridDistributedTxPrepareRequest {
                 reader.incrementState();
 
             case 31:
-                taskNameHash = reader.readInt("taskNameHash");
+                subjId = reader.readUuid("subjId");
 
                 if (!reader.isLastRead())
                     return false;
@@ -586,6 +605,14 @@ public class GridDhtTxPrepareRequest extends GridDistributedTxPrepareRequest {
                 reader.incrementState();
 
             case 32:
+                taskNameHash = reader.readInt("taskNameHash");
+
+                if (!reader.isLastRead())
+                    return false;
+
+                reader.incrementState();
+
+            case 33:
                 topVer = reader.readMessage("topVer");
 
                 if (!reader.isLastRead())
@@ -605,7 +632,7 @@ public class GridDhtTxPrepareRequest extends GridDistributedTxPrepareRequest {
 
     /** {@inheritDoc} */
     @Override public byte fieldsCount() {
-        return 33;
+        return 34;
     }
 
     /** {@inheritDoc} */