You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by vo...@apache.org on 2016/05/04 14:18:06 UTC

[01/10] ignite git commit: Added a test for IGNITE-3077

Repository: ignite
Updated Branches:
  refs/heads/ignite-1786 8d824ab59 -> ce2764637


Added a test for IGNITE-3077


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/e78ed8e2
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/e78ed8e2
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/e78ed8e2

Branch: refs/heads/ignite-1786
Commit: e78ed8e278e36fe293c66b5efdd78801edc6539c
Parents: 9771833
Author: Alexey Goncharuk <al...@gmail.com>
Authored: Fri Apr 29 18:14:46 2016 -0700
Committer: Alexey Goncharuk <al...@gmail.com>
Committed: Fri Apr 29 18:14:46 2016 -0700

----------------------------------------------------------------------
 .../org/apache/ignite/spark/IgniteRDDSpec.scala | 40 +++++++++++++++++++-
 1 file changed, 39 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/e78ed8e2/modules/spark/src/test/scala/org/apache/ignite/spark/IgniteRDDSpec.scala
----------------------------------------------------------------------
diff --git a/modules/spark/src/test/scala/org/apache/ignite/spark/IgniteRDDSpec.scala b/modules/spark/src/test/scala/org/apache/ignite/spark/IgniteRDDSpec.scala
index 071897a..a41839d 100644
--- a/modules/spark/src/test/scala/org/apache/ignite/spark/IgniteRDDSpec.scala
+++ b/modules/spark/src/test/scala/org/apache/ignite/spark/IgniteRDDSpec.scala
@@ -17,6 +17,8 @@
 
 package org.apache.ignite.spark
 
+import java.time.LocalDate
+
 import org.apache.ignite.Ignition
 import org.apache.ignite.cache.query.annotations.{QueryTextField, QuerySqlField}
 import org.apache.ignite.configuration.{CacheConfiguration, IgniteConfiguration}
@@ -165,6 +167,33 @@ class IgniteRDDSpec extends FunSpec with Matchers with BeforeAndAfterAll with Be
                 sc.stop()
             }
         }
+
+        it("should successfully query complex object fields") {
+            assert(false, "https://issues.apache.org/jira/browse/IGNITE-3077")
+
+            val sc = new SparkContext("local[*]", "test")
+
+            try {
+                val ic = new IgniteContext[Integer, WithObjectField](sc,
+                    () \u21d2 configuration("client", client = true))
+
+                val cache: IgniteRDD[Integer, WithObjectField] = ic.fromCache(PARTITIONED_CACHE_NAME)
+
+                cache.savePairs(sc.parallelize(0 to 1000, 2).map(i \u21d2 (i:java.lang.Integer, new WithObjectField(i, new Entity(i, "", i)))))
+
+                val df = cache.sql("select i, ts from WithLocalDate where i = ?", 50)
+
+                df.printSchema()
+
+                val res = df.collect()
+
+                assert(res.length == 1, "Invalid result length")
+                assert(50 == res(0)(0), "Invalid result")
+            }
+            finally {
+                sc.stop()
+            }
+        }
     }
 
     override protected def beforeEach() = {
@@ -188,6 +217,12 @@ class IgniteRDDSpec extends FunSpec with Matchers with BeforeAndAfterAll with Be
     }
 }
 
+case class WithObjectField(
+    @(QuerySqlField @field)(index = true) val i : Int,
+    @(QuerySqlField @field)(index = false) val ts : Object
+) {
+}
+
 /**
  * Constants and utility methods.
  */
@@ -242,7 +277,10 @@ object IgniteRDDSpec {
 
         ccfg.setName(PARTITIONED_CACHE_NAME)
 
-        ccfg.setIndexedTypes(classOf[String], classOf[Entity])
+        ccfg.setIndexedTypes(
+            classOf[String], classOf[Entity],
+            classOf[Integer], classOf[WithObjectField]
+        )
 
         ccfg
     }


[05/10] ignite git commit: IGNITE-3024 - Fixed deadlock in ServiceProcessor

Posted by vo...@apache.org.
IGNITE-3024 - Fixed deadlock in ServiceProcessor


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/718cf78b
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/718cf78b
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/718cf78b

Branch: refs/heads/ignite-1786
Commit: 718cf78b626d45e29f02e0ba8e50ecc1d0f27676
Parents: f0daea9
Author: sboikov <sb...@gridgain.com>
Authored: Wed May 4 12:12:42 2016 +0300
Committer: sboikov <sb...@gridgain.com>
Committed: Wed May 4 12:12:42 2016 +0300

----------------------------------------------------------------------
 .../service/GridServiceNotFoundException.java   |   4 +-
 .../service/GridServiceProcessor.java           | 199 +++++++++------
 .../processors/service/GridServiceProxy.java    |  15 +-
 .../processors/service/ServiceContextImpl.java  |  29 ++-
 .../GridServiceInjectionSpringResourceTest.java | 245 +++++++++++++++++++
 .../spring/injection/spring-resource.tmpl.xml   |  66 +++++
 .../testsuites/IgniteSpringTestSuite.java       |   3 +
 7 files changed, 463 insertions(+), 98 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/718cf78b/modules/core/src/main/java/org/apache/ignite/internal/processors/service/GridServiceNotFoundException.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/service/GridServiceNotFoundException.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/service/GridServiceNotFoundException.java
index e23db7c..f41bceb 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/service/GridServiceNotFoundException.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/service/GridServiceNotFoundException.java
@@ -30,11 +30,11 @@ public class GridServiceNotFoundException extends IgniteCheckedException {
      * @param name Service name.
      */
     public GridServiceNotFoundException(String name) {
-        super("Service node found: " + name);
+        super("Service not found: " + name);
     }
 
     /** {@inheritDoc} */
     @Override public String toString() {
         return getClass() + ": " + getMessage();
     }
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/718cf78b/modules/core/src/main/java/org/apache/ignite/internal/processors/service/GridServiceProcessor.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/service/GridServiceProcessor.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/service/GridServiceProcessor.java
index eded5e1..abfc325 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/service/GridServiceProcessor.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/service/GridServiceProcessor.java
@@ -240,7 +240,11 @@ public class GridServiceProcessor extends GridProcessorAdapter {
 
         for (ServiceContextImpl ctx : ctxs) {
             ctx.setCancelled(true);
-            ctx.service().cancel(ctx);
+
+            Service svc = ctx.service();
+
+            if (svc != null)
+                svc.cancel(ctx);
 
             ctx.executor().shutdownNow();
         }
@@ -653,7 +657,14 @@ public class GridServiceProcessor extends GridProcessorAdapter {
             if (ctxs.isEmpty())
                 return null;
 
-            return (T)ctxs.iterator().next().service();
+            for (ServiceContextImpl ctx : ctxs) {
+                Service svc = ctx.service();
+
+                if (svc != null)
+                    return (T)svc;
+            }
+
+            return null;
         }
     }
 
@@ -675,7 +686,12 @@ public class GridServiceProcessor extends GridProcessorAdapter {
             if (ctxs.isEmpty())
                 return null;
 
-            return ctxs.iterator().next();
+            for (ServiceContextImpl ctx : ctxs) {
+                if (ctx.service() != null)
+                    return ctx;
+            }
+
+            return null;
         }
     }
 
@@ -695,11 +711,15 @@ public class GridServiceProcessor extends GridProcessorAdapter {
             ServiceContextImpl ctx = serviceContext(name);
 
             if (ctx != null) {
-                if (!svcItf.isAssignableFrom(ctx.service().getClass()))
-                    throw new IgniteException("Service does not implement specified interface [svcItf=" +
-                        svcItf.getName() + ", svcCls=" + ctx.service().getClass().getName() + ']');
+                Service svc = ctx.service();
+
+                if (svc != null) {
+                    if (!svcItf.isAssignableFrom(svc.getClass()))
+                        throw new IgniteException("Service does not implement specified interface [svcItf=" +
+                            svcItf.getName() + ", svcCls=" + svc.getClass().getName() + ']');
 
-                return (T)ctx.service();
+                    return (T)svc;
+                }
             }
         }
 
@@ -738,8 +758,12 @@ public class GridServiceProcessor extends GridProcessorAdapter {
         synchronized (ctxs) {
             Collection<T> res = new ArrayList<>(ctxs.size());
 
-            for (ServiceContextImpl ctx : ctxs)
-                res.add((T)ctx.service());
+            for (ServiceContextImpl ctx : ctxs) {
+                Service svc = ctx.service();
+
+                if (svc != null)
+                    res.add((T)svc);
+            }
 
             return res;
         }
@@ -911,8 +935,6 @@ public class GridServiceProcessor extends GridProcessorAdapter {
         if (assignCnt == null)
             assignCnt = 0;
 
-        Service svc = assigns.service();
-
         Collection<ServiceContextImpl> ctxs;
 
         synchronized (locSvcs) {
@@ -922,6 +944,8 @@ public class GridServiceProcessor extends GridProcessorAdapter {
                 locSvcs.put(svcName, ctxs = new ArrayList<>());
         }
 
+        Collection<ServiceContextImpl> toInit = new ArrayList<>();
+
         synchronized (ctxs) {
             if (ctxs.size() > assignCnt) {
                 int cancelCnt = ctxs.size() - assignCnt;
@@ -932,75 +956,86 @@ public class GridServiceProcessor extends GridProcessorAdapter {
                 int createCnt = assignCnt - ctxs.size();
 
                 for (int i = 0; i < createCnt; i++) {
-                    final Service cp = copyAndInject(svc);
+                    ServiceContextImpl svcCtx = new ServiceContextImpl(assigns.name(),
+                        UUID.randomUUID(),
+                        assigns.cacheName(),
+                        assigns.affinityKey(),
+                        Executors.newSingleThreadExecutor(threadFactory));
 
-                    final ExecutorService exe = Executors.newSingleThreadExecutor(threadFactory);
+                    ctxs.add(svcCtx);
 
-                    final ServiceContextImpl svcCtx = new ServiceContextImpl(assigns.name(),
-                        UUID.randomUUID(), assigns.cacheName(), assigns.affinityKey(), cp, exe);
+                    toInit.add(svcCtx);
+                }
+            }
+        }
 
-                    ctxs.add(svcCtx);
+        for (final ServiceContextImpl svcCtx : toInit) {
+            final Service svc = copyAndInject(assigns.service());
 
-                    try {
-                        // Initialize service.
-                        cp.init(svcCtx);
-                    }
-                    catch (Throwable e) {
-                        log.error("Failed to initialize service (service will not be deployed): " + assigns.name(), e);
+            try {
+                // Initialize service.
+                svc.init(svcCtx);
 
-                        ctxs.remove(svcCtx);
+                svcCtx.service(svc);
+            }
+            catch (Throwable e) {
+                log.error("Failed to initialize service (service will not be deployed): " + assigns.name(), e);
 
-                        if (e instanceof Error)
-                            throw (Error)e;
+                synchronized (ctxs) {
+                    ctxs.removeAll(toInit);
+                }
 
-                        if (e instanceof RuntimeException)
-                            throw (RuntimeException)e;
+                if (e instanceof Error)
+                    throw (Error)e;
 
-                        return;
-                    }
+                if (e instanceof RuntimeException)
+                    throw (RuntimeException)e;
 
-                    if (log.isInfoEnabled())
-                        log.info("Starting service instance [name=" + svcCtx.name() + ", execId=" +
-                            svcCtx.executionId() + ']');
+                return;
+            }
 
-                    // Start service in its own thread.
-                    exe.submit(new Runnable() {
-                        @Override public void run() {
-                            try {
-                                cp.execute(svcCtx);
-                            }
-                            catch (InterruptedException | IgniteInterruptedCheckedException ignore) {
-                                if (log.isDebugEnabled())
-                                    log.debug("Service thread was interrupted [name=" + svcCtx.name() + ", execId=" +
-                                        svcCtx.executionId() + ']');
-                            }
-                            catch (IgniteException e) {
-                                if (e.hasCause(InterruptedException.class) ||
-                                    e.hasCause(IgniteInterruptedCheckedException.class)) {
-                                    if (log.isDebugEnabled())
-                                        log.debug("Service thread was interrupted [name=" + svcCtx.name() +
-                                            ", execId=" + svcCtx.executionId() + ']');
-                                }
-                                else {
-                                    U.error(log, "Service execution stopped with error [name=" + svcCtx.name() +
-                                        ", execId=" + svcCtx.executionId() + ']', e);
-                                }
-                            }
-                            catch (Throwable e) {
-                                log.error("Service execution stopped with error [name=" + svcCtx.name() +
-                                    ", execId=" + svcCtx.executionId() + ']', e);
+            if (log.isInfoEnabled())
+                log.info("Starting service instance [name=" + svcCtx.name() + ", execId=" +
+                    svcCtx.executionId() + ']');
 
-                                if (e instanceof Error)
-                                    throw (Error)e;
-                            }
-                            finally {
-                                // Suicide.
-                                exe.shutdownNow();
-                            }
+            // Start service in its own thread.
+            final ExecutorService exe = svcCtx.executor();
+
+            exe.submit(new Runnable() {
+                @Override public void run() {
+                    try {
+                        svc.execute(svcCtx);
+                    }
+                    catch (InterruptedException | IgniteInterruptedCheckedException ignore) {
+                        if (log.isDebugEnabled())
+                            log.debug("Service thread was interrupted [name=" + svcCtx.name() + ", execId=" +
+                                svcCtx.executionId() + ']');
+                    }
+                    catch (IgniteException e) {
+                        if (e.hasCause(InterruptedException.class) ||
+                            e.hasCause(IgniteInterruptedCheckedException.class)) {
+                            if (log.isDebugEnabled())
+                                log.debug("Service thread was interrupted [name=" + svcCtx.name() +
+                                    ", execId=" + svcCtx.executionId() + ']');
                         }
-                    });
+                        else {
+                            U.error(log, "Service execution stopped with error [name=" + svcCtx.name() +
+                                ", execId=" + svcCtx.executionId() + ']', e);
+                        }
+                    }
+                    catch (Throwable e) {
+                        log.error("Service execution stopped with error [name=" + svcCtx.name() +
+                            ", execId=" + svcCtx.executionId() + ']', e);
+
+                        if (e instanceof Error)
+                            throw (Error)e;
+                    }
+                    finally {
+                        // Suicide.
+                        exe.shutdownNow();
+                    }
                 }
-            }
+            });
         }
     }
 
@@ -1040,22 +1075,26 @@ public class GridServiceProcessor extends GridProcessorAdapter {
             svcCtx.setCancelled(true);
 
             // Notify service about cancellation.
-            try {
-                svcCtx.service().cancel(svcCtx);
-            }
-            catch (Throwable e) {
-                log.error("Failed to cancel service (ignoring) [name=" + svcCtx.name() +
-                    ", execId=" + svcCtx.executionId() + ']', e);
+            Service svc = svcCtx.service();
 
-                if (e instanceof Error)
-                    throw e;
-            }
-            finally {
+            if (svc != null) {
                 try {
-                    ctx.resource().cleanup(svcCtx.service());
+                    svc.cancel(svcCtx);
                 }
-                catch (IgniteCheckedException e) {
-                    log.error("Failed to clean up service (will ignore): " + svcCtx.name(), e);
+                catch (Throwable e) {
+                    log.error("Failed to cancel service (ignoring) [name=" + svcCtx.name() +
+                        ", execId=" + svcCtx.executionId() + ']', e);
+
+                    if (e instanceof Error)
+                        throw e;
+                }
+                finally {
+                    try {
+                        ctx.resource().cleanup(svc);
+                    }
+                    catch (IgniteCheckedException e) {
+                        U.error(log, "Failed to clean up service (will ignore): " + svcCtx.name(), e);
+                    }
                 }
             }
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/718cf78b/modules/core/src/main/java/org/apache/ignite/internal/processors/service/GridServiceProxy.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/service/GridServiceProxy.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/service/GridServiceProxy.java
index 578c549..564a13a 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/service/GridServiceProxy.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/service/GridServiceProxy.java
@@ -40,7 +40,7 @@ import org.apache.ignite.cluster.ClusterGroup;
 import org.apache.ignite.cluster.ClusterNode;
 import org.apache.ignite.internal.GridClosureCallMode;
 import org.apache.ignite.internal.GridKernalContext;
-import org.apache.ignite.internal.IgniteKernal;
+import org.apache.ignite.internal.IgniteEx;
 import org.apache.ignite.internal.cluster.ClusterTopologyCheckedException;
 import org.apache.ignite.internal.util.tostring.GridToStringExclude;
 import org.apache.ignite.internal.util.typedef.F;
@@ -48,6 +48,7 @@ import org.apache.ignite.internal.util.typedef.internal.S;
 import org.apache.ignite.internal.util.typedef.internal.U;
 import org.apache.ignite.lang.IgniteCallable;
 import org.apache.ignite.resources.IgniteInstanceResource;
+import org.apache.ignite.services.Service;
 import org.jsr166.ThreadLocalRandom8;
 
 /**
@@ -157,8 +158,12 @@ public class GridServiceProxy<T> implements Serializable {
                     if (node.isLocal()) {
                         ServiceContextImpl svcCtx = ctx.service().serviceContext(name);
 
-                        if (svcCtx != null)
-                            return mtd.invoke(svcCtx.service(), args);
+                        if (svcCtx != null) {
+                            Service svc = svcCtx.service();
+
+                            if (svc != null)
+                                return mtd.invoke(svc, args);
+                        }
                     }
                     else {
                         // Execute service remotely.
@@ -372,9 +377,9 @@ public class GridServiceProxy<T> implements Serializable {
 
         /** {@inheritDoc} */
         @Override public Object call() throws Exception {
-            ServiceContextImpl svcCtx = ((IgniteKernal) ignite).context().service().serviceContext(svcName);
+            ServiceContextImpl svcCtx = ((IgniteEx)ignite).context().service().serviceContext(svcName);
 
-            if (svcCtx == null)
+            if (svcCtx == null || svcCtx.service() == null)
                 throw new GridServiceNotFoundException(svcName);
 
             GridServiceMethodReflectKey key = new GridServiceMethodReflectKey(mtdName, argTypes);

http://git-wip-us.apache.org/repos/asf/ignite/blob/718cf78b/modules/core/src/main/java/org/apache/ignite/internal/processors/service/ServiceContextImpl.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/service/ServiceContextImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/service/ServiceContextImpl.java
index 6746e29..66061d5 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/service/ServiceContextImpl.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/service/ServiceContextImpl.java
@@ -50,10 +50,6 @@ public class ServiceContextImpl implements ServiceContext {
     /** Affinity key. */
     private final Object affKey;
 
-    /** Service. */
-    @GridToStringExclude
-    private final Service svc;
-
     /** Executor service. */
     @GridToStringExclude
     private final ExecutorService exe;
@@ -61,25 +57,29 @@ public class ServiceContextImpl implements ServiceContext {
     /** Methods reflection cache. */
     private final ConcurrentMap<GridServiceMethodReflectKey, Method> mtds = new ConcurrentHashMap<>();
 
+    /** Service. */
+    @GridToStringExclude
+    private volatile Service svc;
+
     /** Cancelled flag. */
     private volatile boolean isCancelled;
 
-
     /**
      * @param name Service name.
      * @param execId Execution ID.
      * @param cacheName Cache name.
      * @param affKey Affinity key.
-     * @param svc Service.
      * @param exe Executor service.
      */
-    ServiceContextImpl(String name, UUID execId, String cacheName, Object affKey, Service svc,
+    ServiceContextImpl(String name,
+        UUID execId,
+        String cacheName,
+        Object affKey,
         ExecutorService exe) {
         this.name = name;
         this.execId = execId;
         this.cacheName = cacheName;
         this.affKey = affKey;
-        this.svc = svc;
         this.exe = exe;
     }
 
@@ -110,9 +110,16 @@ public class ServiceContextImpl implements ServiceContext {
     }
 
     /**
-     * @return Service instance.
+     * @param svc Service instance.
+     */
+    void service(Service svc) {
+        this.svc = svc;
+    }
+
+    /**
+     * @return Service instance or {@code null} if service initialization is not finished yet.
      */
-    Service service() {
+    @Nullable Service service() {
         return svc;
     }
 
@@ -136,7 +143,7 @@ public class ServiceContextImpl implements ServiceContext {
 
                 mtd.setAccessible(true);
             }
-            catch (NoSuchMethodException e) {
+            catch (NoSuchMethodException ignored) {
                 mtd = NULL_METHOD;
             }
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/718cf78b/modules/spring/src/test/java/org/apache/ignite/spring/injection/GridServiceInjectionSpringResourceTest.java
----------------------------------------------------------------------
diff --git a/modules/spring/src/test/java/org/apache/ignite/spring/injection/GridServiceInjectionSpringResourceTest.java b/modules/spring/src/test/java/org/apache/ignite/spring/injection/GridServiceInjectionSpringResourceTest.java
new file mode 100644
index 0000000..c6a6d05
--- /dev/null
+++ b/modules/spring/src/test/java/org/apache/ignite/spring/injection/GridServiceInjectionSpringResourceTest.java
@@ -0,0 +1,245 @@
+/*
+ * 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.spring.injection;
+
+import java.io.InputStreamReader;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.internal.util.typedef.G;
+import org.apache.ignite.lang.IgniteCallable;
+import org.apache.ignite.resources.ServiceResource;
+import org.apache.ignite.resources.SpringResource;
+import org.apache.ignite.services.Service;
+import org.apache.ignite.services.ServiceContext;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.springframework.context.support.AbstractApplicationContext;
+import org.springframework.context.support.FileSystemXmlApplicationContext;
+
+import javax.annotation.PostConstruct;
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.nio.file.FileSystem;
+import java.nio.file.FileSystems;
+import java.nio.file.Files;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+/**
+ * Tests for injected service.
+ */
+public class GridServiceInjectionSpringResourceTest extends GridCommonAbstractTest {
+    /** Service name. */
+    private static final String SERVICE_NAME = "testService";
+
+    /** Bean name. */
+    private static final String DUMMY_BEAN = "dummyResourceBean";
+
+    /** */
+    private static final int NODES = 8;
+
+    /** */
+    private static final int TEST_ITERATIONS = 5;
+
+    /** */
+    private static FileSystem FS = FileSystems.getDefault();
+
+    /** */
+    private static final String springCfgFileTemplate = "spring-resource.tmpl.xml";
+
+    /** */
+    private static String springCfgFileOutTmplName = "spring-resource.xml-";
+
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        stopAllGrids();
+
+        for (int i = 0; i < NODES; ++i)
+            Files.deleteIfExists(FS.getPath(springCfgFileOutTmplName + i));
+
+        super.afterTest();
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testDeployServiceWithSpring() throws Exception {
+        generateConfigXmls(NODES);
+
+        for (int i = 0; i < TEST_ITERATIONS; ++i) {
+            log.info("Iteration: " + i);
+
+            doOneTestIteration();
+        }
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    private void doOneTestIteration() throws Exception {
+        startNodes();
+
+        for (int i = 0; i < 10; ++i) {
+            for (Ignite ignite : G.allGrids())
+                ignite.compute().call(new TestJob());
+        }
+
+        stopAllGrids();
+    }
+
+    /**
+     *
+     * @throws Exception If failed.
+     */
+    private void startNodes() throws Exception {
+        AbstractApplicationContext ctxSpring = new FileSystemXmlApplicationContext(springCfgFileOutTmplName + 0);
+
+        // We have to deploy Services for service is available at the bean creation time for other nodes.
+        Ignite ignite = (Ignite)ctxSpring.getBean("testIgnite");
+
+        ignite.services().deployMultiple(SERVICE_NAME, new DummyServiceImpl(), NODES, 1);
+
+        // Add other nodes.
+        for (int i = 1; i < NODES; ++i)
+            new FileSystemXmlApplicationContext(springCfgFileOutTmplName + i);
+
+        assertEquals(NODES, G.allGrids().size());
+        assertEquals(NODES, ignite.cluster().nodes().size());
+    }
+
+    /**
+     * @param nodes Nodes.
+     * @throws IOException on read/write config error
+     */
+    private void generateConfigXmls(int nodes) throws IOException {
+        StringBuilder cfg = new StringBuilder();
+
+        try (BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(springCfgFileTemplate)))) {
+            while (true) {
+                String str = br.readLine();
+
+                if (str == null)
+                    break;
+
+                cfg.append(str);
+            }
+        }
+
+        for (int i = 0; i < nodes; ++i) {
+            try (BufferedWriter bw = Files.newBufferedWriter(FS.getPath(springCfgFileOutTmplName + i), UTF_8)) {
+                String str = cfg.toString();
+
+                str = str.replaceAll("@GRID_IDX@", Integer.toString(i));
+
+                bw.write(str);
+            }
+        }
+    }
+
+    /**
+     *
+     */
+    private static class TestJob implements IgniteCallable {
+        /** */
+        @ServiceResource(serviceName = SERVICE_NAME, proxyInterface = DummyService.class)
+        private DummyService svc;
+
+        /** {@inheritDoc} */
+        @Override public Object call() throws Exception {
+            assertNotNull(svc);
+
+            svc.noop();
+
+            return null;
+        }
+    }
+
+    /**
+     * Dummy Service.
+     */
+    public interface DummyService {
+        /**
+         *
+         */
+        void noop();
+    }
+
+    /**
+     * No-op test service.
+     */
+    public static class DummyServiceImpl implements DummyService, Service {
+        /** */
+        private static final long serialVersionUID = 0L;
+
+        /** */
+        @SpringResource(resourceName = DUMMY_BEAN)
+        private transient DummyResourceBean dummyRsrcBean;
+
+        /** {@inheritDoc} */
+        @Override public void noop() {
+            System.out.println("DummyServiceImpl.noop()");
+        }
+
+        /** {@inheritDoc} */
+        @Override public void cancel(ServiceContext ctx) {
+            System.out.println("Cancelling service: " + ctx.name());
+        }
+
+        /** {@inheritDoc} */
+        @Override public void init(ServiceContext ctx) throws Exception {
+            System.out.println("Initializing service: " + ctx.name());
+        }
+
+        /** {@inheritDoc} */
+        @Override public void execute(ServiceContext ctx) {
+            System.out.println("Executing service: " + ctx.name());
+        }
+    }
+
+    /**
+     * Dummy resource bean.
+     */
+    public static class DummyResourceBean {
+        /** */
+        private transient Ignite ignite;
+
+        /**
+         * @return Ignite.
+         */
+        public Ignite getIgnite() {
+            return ignite;
+        }
+
+        /**
+         * @param ignite Ignite.
+         */
+        public void setIgnite(Ignite ignite) {
+            this.ignite = ignite;
+        }
+
+        /**
+         * @throws Exception If failed.
+         */
+        @PostConstruct
+        public void init() throws Exception {
+            DummyService srv = ignite.services().serviceProxy(SERVICE_NAME, DummyService.class, false);
+
+            assertNotNull(srv);
+        }
+    }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ignite/blob/718cf78b/modules/spring/src/test/java/org/apache/ignite/spring/injection/spring-resource.tmpl.xml
----------------------------------------------------------------------
diff --git a/modules/spring/src/test/java/org/apache/ignite/spring/injection/spring-resource.tmpl.xml b/modules/spring/src/test/java/org/apache/ignite/spring/injection/spring-resource.tmpl.xml
new file mode 100644
index 0000000..aeee4ec
--- /dev/null
+++ b/modules/spring/src/test/java/org/apache/ignite/spring/injection/spring-resource.tmpl.xml
@@ -0,0 +1,66 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+-->
+
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xmlns:context="http://www.springframework.org/schema/context"
+       xsi:schemaLocation="
+        http://www.springframework.org/schema/beans
+        http://www.springframework.org/schema/beans/spring-beans.xsd
+        http://www.springframework.org/schema/context
+        http://www.springframework.org/schema/context/spring-context.xsd">
+
+    <context:annotation-config/>
+
+    <bean id="dummyResourceBean"
+          class="org.apache.ignite.spring.injection.GridServiceInjectionSpringResourceTest$DummyResourceBean">
+        <property name="ignite" ref="testIgnite"/>
+    </bean>
+
+
+    <bean id="testIgnite" class="org.apache.ignite.IgniteSpringBean">
+        <property name="configuration">
+            <bean id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration">
+                <property name="localHost" value="127.0.0.1"/>
+
+                <property name="gridName" value="springTest@GRID_IDX@"/>
+
+                <property name="discoverySpi">
+                    <bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
+                        <property name="ipFinder">
+                            <bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder">
+                                <property name="addresses">
+                                    <list>
+                                        <value>127.0.0.1:47500</value>
+                                        <value>127.0.0.1:47501</value>
+                                        <value>127.0.0.1:47502</value>
+                                        <value>127.0.0.1:47503</value>
+                                        <value>127.0.0.1:47504</value>
+                                        <value>127.0.0.1:47505</value>
+                                        <value>127.0.0.1:47506</value>
+                                        <value>127.0.0.1:47507</value>
+                                    </list>
+                                </property>
+                            </bean>
+                        </property>
+                    </bean>
+                </property>
+            </bean>
+        </property>
+    </bean>
+</beans>

http://git-wip-us.apache.org/repos/asf/ignite/blob/718cf78b/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java
----------------------------------------------------------------------
diff --git a/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java b/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java
index c42c7e0..4fc159e 100644
--- a/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java
+++ b/modules/spring/src/test/java/org/apache/ignite/testsuites/IgniteSpringTestSuite.java
@@ -28,6 +28,7 @@ import org.apache.ignite.p2p.GridP2PUserVersionChangeSelfTest;
 import org.apache.ignite.spring.GridSpringCacheManagerSelfTest;
 import org.apache.ignite.spring.IgniteExcludeInConfigurationTest;
 import org.apache.ignite.spring.IgniteStartFromStreamConfigurationTest;
+import org.apache.ignite.spring.injection.GridServiceInjectionSpringResourceTest;
 import org.apache.ignite.transactions.spring.GridSpringTransactionManagerSelfTest;
 
 /**
@@ -65,6 +66,8 @@ public class IgniteSpringTestSuite extends TestSuite {
 
         suite.addTest(new TestSuite(GridSpringTransactionManagerSelfTest.class));
 
+        suite.addTestSuite(GridServiceInjectionSpringResourceTest.class);
+
         return suite;
     }
 }


[03/10] ignite git commit: IGNITE-3057 - Optimization for transactions that do not acquire locks (cherry picked from commit b476fdf)

Posted by vo...@apache.org.
IGNITE-3057 - Optimization for transactions that do not acquire locks
(cherry picked from commit b476fdf)


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/fc0dd9a4
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/fc0dd9a4
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/fc0dd9a4

Branch: refs/heads/ignite-1786
Commit: fc0dd9a48079f99622b533ca0f4037e3e4185bd1
Parents: 34c03833
Author: sboikov <sb...@gridgain.com>
Authored: Wed Apr 27 14:04:24 2016 +0300
Committer: sboikov <sb...@gridgain.com>
Committed: Wed May 4 08:58:37 2016 +0300

----------------------------------------------------------------------
 .../cache/distributed/near/GridNearTxLocal.java |  32 +++
 .../cache/transactions/IgniteTxManager.java     |  40 +++
 .../processors/cache/CacheTxFastFinishTest.java | 253 +++++++++++++++++++
 .../ignite/testsuites/IgniteCacheTestSuite.java |   3 +
 4 files changed, 328 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/fc0dd9a4/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearTxLocal.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearTxLocal.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearTxLocal.java
index 4aee6ad..7f3ecd0 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearTxLocal.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearTxLocal.java
@@ -72,6 +72,7 @@ import org.jetbrains.annotations.Nullable;
 
 import static org.apache.ignite.transactions.TransactionState.COMMITTED;
 import static org.apache.ignite.transactions.TransactionState.COMMITTING;
+import static org.apache.ignite.transactions.TransactionState.PREPARED;
 import static org.apache.ignite.transactions.TransactionState.PREPARING;
 import static org.apache.ignite.transactions.TransactionState.ROLLED_BACK;
 import static org.apache.ignite.transactions.TransactionState.ROLLING_BACK;
@@ -824,6 +825,18 @@ public class GridNearTxLocal extends GridDhtTxLocalAdapter {
         if (log.isDebugEnabled())
             log.debug("Committing near local tx: " + this);
 
+        if (fastFinish()) {
+            state(PREPARING);
+            state(PREPARED);
+            state(COMMITTING);
+
+            cctx.tm().fastFinishTx(this, true);
+
+            state(COMMITTED);
+
+            return new GridFinishedFuture<>((IgniteInternalTx)this);
+        }
+
         prepareAsync();
 
         GridNearTxFinishFuture fut = commitFut;
@@ -869,6 +882,18 @@ public class GridNearTxLocal extends GridDhtTxLocalAdapter {
         if (log.isDebugEnabled())
             log.debug("Rolling back near tx: " + this);
 
+        if (fastFinish()) {
+            state(PREPARING);
+            state(PREPARED);
+            state(ROLLING_BACK);
+
+            cctx.tm().fastFinishTx(this, false);
+
+            state(ROLLED_BACK);
+
+            return new GridFinishedFuture<>((IgniteInternalTx)this);
+        }
+
         GridNearTxFinishFuture fut = rollbackFut;
 
         if (fut != null)
@@ -917,6 +942,13 @@ public class GridNearTxLocal extends GridDhtTxLocalAdapter {
     }
 
     /**
+     * @return {@code True} if 'fast finish' path can be used for transaction completion.
+     */
+    private boolean fastFinish() {
+        return writeMap().isEmpty() && ((optimistic() && !serializable()) || readMap().isEmpty());
+    }
+
+    /**
      * Prepares next batch of entries in dht transaction.
      *
      * @param reads Read entries.

http://git-wip-us.apache.org/repos/asf/ignite/blob/fc0dd9a4/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/IgniteTxManager.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/IgniteTxManager.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/IgniteTxManager.java
index 7fbf253..ccfc4de 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/IgniteTxManager.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/IgniteTxManager.java
@@ -1186,6 +1186,46 @@ public class IgniteTxManager extends GridCacheSharedManagerAdapter {
     }
 
     /**
+     * Fast finish transaction. Can be used only if no locks were acquired.
+     *
+     * @param tx Transaction to finish.
+     * @param commit {@code True} if transaction is committed, {@code false} if rolled back.
+     */
+    public void fastFinishTx(IgniteInternalTx tx, boolean commit) {
+        assert tx != null;
+        assert tx.writeMap().isEmpty();
+        assert tx.optimistic() || tx.readMap().isEmpty();
+
+        ConcurrentMap<GridCacheVersion, IgniteInternalTx> txIdMap = transactionMap(tx);
+
+        if (txIdMap.remove(tx.xidVersion(), tx)) {
+            // 1. Notify evictions.
+            notifyEvitions(tx);
+
+            // 2. Remove obsolete entries.
+            removeObsolete(tx);
+
+            // 3. Remove from per-thread storage.
+            clearThreadMap(tx);
+
+            // 4. Clear context.
+            resetContext();
+
+            // 5. Update metrics.
+            if (!tx.dht() && tx.local()) {
+                if (!tx.system()) {
+                    if (commit)
+                        cctx.txMetrics().onTxCommit();
+                    else
+                        cctx.txMetrics().onTxRollback();
+                }
+
+                tx.txState().onTxEnd(cctx, tx, commit);
+            }
+        }
+    }
+
+    /**
      * Tries to minimize damage from partially-committed transaction.
      *
      * @param tx Tx to uncommit.

http://git-wip-us.apache.org/repos/asf/ignite/blob/fc0dd9a4/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/CacheTxFastFinishTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/CacheTxFastFinishTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/CacheTxFastFinishTest.java
new file mode 100644
index 0000000..35b1405
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/CacheTxFastFinishTest.java
@@ -0,0 +1,253 @@
+/*
+ * 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;
+
+import java.util.concurrent.atomic.AtomicReference;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.IgniteTransactions;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.configuration.NearCacheConfiguration;
+import org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx;
+import org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl;
+import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
+import org.apache.ignite.spi.discovery.tcp.ipfinder.TcpDiscoveryIpFinder;
+import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.apache.ignite.transactions.Transaction;
+import org.apache.ignite.transactions.TransactionConcurrency;
+import org.apache.ignite.transactions.TransactionIsolation;
+
+import static org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL;
+import static org.apache.ignite.cache.CacheMode.PARTITIONED;
+import static org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC;
+import static org.apache.ignite.transactions.TransactionConcurrency.OPTIMISTIC;
+import static org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC;
+import static org.apache.ignite.transactions.TransactionIsolation.READ_COMMITTED;
+import static org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ;
+import static org.apache.ignite.transactions.TransactionIsolation.SERIALIZABLE;
+
+/**
+ *
+ */
+public class CacheTxFastFinishTest extends GridCommonAbstractTest {
+    /** */
+    private static final TcpDiscoveryIpFinder IP_FINDER = new TcpDiscoveryVmIpFinder(true);
+
+    /** */
+    private boolean client;
+
+    /** */
+    private boolean nearCache;
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception {
+        IgniteConfiguration cfg = super.getConfiguration(gridName);
+
+        ((TcpDiscoverySpi)cfg.getDiscoverySpi()).setIpFinder(IP_FINDER);
+
+        CacheConfiguration ccfg = new CacheConfiguration();
+
+        ccfg.setCacheMode(PARTITIONED);
+        ccfg.setAtomicityMode(TRANSACTIONAL);
+        ccfg.setBackups(1);
+        ccfg.setWriteSynchronizationMode(FULL_SYNC);
+
+        if (nearCache)
+            ccfg.setNearConfiguration(new NearCacheConfiguration());
+
+        cfg.setCacheConfiguration(ccfg);
+
+        cfg.setClientMode(client);
+
+        return cfg;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        super.afterTest();
+
+        stopAllGrids();
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testFastFinishTxNearCache() throws Exception {
+        nearCache = true;
+
+        fastFinishTx();
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testFastFinishTx() throws Exception {
+        fastFinishTx();
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    private void fastFinishTx() throws Exception {
+        startGrid(0);
+
+        fastFinishTx(ignite(0));
+
+        client = true;
+
+        startGrid(1);
+
+        for (int i = 0; i < 2; i++)
+            fastFinishTx(ignite(i));
+
+        client = false;
+
+        startGrid(2);
+
+        for (int i = 0; i < 3; i++)
+            fastFinishTx(ignite(i));
+
+        startGrid(3);
+
+        for (int i = 0; i < 4; i++)
+            fastFinishTx(ignite(i));
+
+        stopGrid(1);
+
+        for (int i = 0; i < 4; i++) {
+            if (i != 1)
+                fastFinishTx(ignite(i));
+        }
+    }
+
+    /**
+     * @param ignite Node.
+     */
+    private void fastFinishTx(Ignite ignite) {
+        IgniteTransactions txs = ignite.transactions();
+
+        IgniteCache cache = ignite.cache(null);
+
+        for (boolean commit : new boolean[]{true, false}) {
+            for (TransactionConcurrency c : TransactionConcurrency.values()) {
+                for (TransactionIsolation isolation : TransactionIsolation.values()) {
+                    try (Transaction tx = txs.txStart(c, isolation)) {
+                        checkFastTxFinish(tx, commit);
+                    }
+                }
+            }
+
+            for (int i = 0; i < 100; i++) {
+                try (Transaction tx = txs.txStart(OPTIMISTIC, REPEATABLE_READ)) {
+                    cache.get(i);
+
+                    checkFastTxFinish(tx, commit);
+                }
+
+                try (Transaction tx = txs.txStart(OPTIMISTIC, READ_COMMITTED)) {
+                    cache.get(i);
+
+                    checkFastTxFinish(tx, commit);
+                }
+            }
+
+            for (int i = 0; i < 100; i++) {
+                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
+                    cache.get(i);
+
+                    checkNormalTxFinish(tx, commit);
+                }
+
+                try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
+                    cache.get(i);
+
+                    checkNormalTxFinish(tx, commit);
+                }
+            }
+
+            for (int i = 0; i < 100; i++) {
+                for (TransactionConcurrency c : TransactionConcurrency.values()) {
+                    for (TransactionIsolation isolation : TransactionIsolation.values()) {
+                        try (Transaction tx = txs.txStart(c, isolation)) {
+                            cache.put(i, i);
+
+                            checkNormalTxFinish(tx, commit);
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    /**
+     * @param tx Transaction.
+     * @param commit Commit flag.
+     */
+    private void checkFastTxFinish(Transaction tx, boolean commit) {
+        if (commit)
+            tx.commit();
+        else
+            tx.rollback();
+
+        IgniteInternalTx tx0 = ((TransactionProxyImpl)tx).tx();
+
+        assertNull(fieldValue(tx0, "prepFut"));
+        assertNull(fieldValue(tx0, "commitFut"));
+        assertNull(fieldValue(tx0, "rollbackFut"));
+    }
+
+    /**
+     * @param tx Transaction.
+     * @param commit Commit flag.
+     */
+    private void checkNormalTxFinish(Transaction tx, boolean commit) {
+        IgniteInternalTx tx0 = ((TransactionProxyImpl)tx).tx();
+
+        if (commit) {
+            tx.commit();
+
+            assertNotNull(fieldValue(tx0, "prepFut"));
+            assertNotNull(fieldValue(tx0, "commitFut"));
+        }
+        else {
+            tx.rollback();
+
+            assertNotNull(fieldValue(tx0, "rollbackFut"));
+        }
+    }
+
+    /**
+     * @param obj Obejct.
+     * @param fieldName Field name.
+     * @return Field value.
+     */
+    private Object fieldValue(Object obj, String fieldName) {
+        Object val = GridTestUtils.getFieldValue(obj, fieldName);
+
+        if (val == null)
+            return null;
+
+        if (val instanceof AtomicReference)
+            return ((AtomicReference)val).get();
+
+        return val;
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/fc0dd9a4/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite.java b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite.java
index 7edd0a0..3238786 100755
--- a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite.java
+++ b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite.java
@@ -44,6 +44,7 @@ import org.apache.ignite.internal.processors.cache.CacheFutureExceptionSelfTest;
 import org.apache.ignite.internal.processors.cache.CacheNamesSelfTest;
 import org.apache.ignite.internal.processors.cache.CacheNamesWithSpecialCharactersTest;
 import org.apache.ignite.internal.processors.cache.CachePutEventListenerErrorSelfTest;
+import org.apache.ignite.internal.processors.cache.CacheTxFastFinishTest;
 import org.apache.ignite.internal.processors.cache.GridCacheAffinityApiSelfTest;
 import org.apache.ignite.internal.processors.cache.GridCacheAffinityMapperSelfTest;
 import org.apache.ignite.internal.processors.cache.GridCacheAffinityRoutingSelfTest;
@@ -302,6 +303,8 @@ public class IgniteCacheTestSuite extends TestSuite {
 
         suite.addTestSuite(IgniteTxConfigCacheSelfTest.class);
 
+        suite.addTestSuite(CacheTxFastFinishTest.class);
+
         return suite;
     }
 }


[07/10] ignite git commit: ignite-2854 Deadlock detection for pessimistic transactions

Posted by vo...@apache.org.
http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/TxDeadlockDetectionTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/TxDeadlockDetectionTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/TxDeadlockDetectionTest.java
new file mode 100644
index 0000000..87539b2
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/TxDeadlockDetectionTest.java
@@ -0,0 +1,495 @@
+/*
+ * 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.transactions;
+
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.CyclicBarrier;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.cache.CacheMode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.IgniteInternalFuture;
+import org.apache.ignite.internal.IgniteKernal;
+import org.apache.ignite.internal.managers.communication.GridIoMessage;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.lang.IgniteInClosure;
+import org.apache.ignite.plugin.extensions.communication.Message;
+import org.apache.ignite.spi.IgniteSpiException;
+import org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi;
+import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.apache.ignite.transactions.Transaction;
+import org.apache.ignite.transactions.TransactionDeadlockException;
+import org.apache.ignite.transactions.TransactionTimeoutException;
+import org.jsr166.ThreadLocalRandom8;
+
+import static org.apache.ignite.internal.util.typedef.X.hasCause;
+import static org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC;
+import static org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ;
+
+/**
+ *
+ */
+public class TxDeadlockDetectionTest extends GridCommonAbstractTest {
+    /** Nodes count. */
+    private static final int NODES_CNT = 3;
+
+    /** Cache. */
+    private static final String CACHE = "cache";
+
+    /** {@inheritDoc} */
+    @SuppressWarnings("unchecked")
+    @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception {
+        IgniteConfiguration cfg = super.getConfiguration(gridName);
+
+        if (isDebug()) {
+            TcpDiscoverySpi discoSpi = new TcpDiscoverySpi();
+
+            discoSpi.failureDetectionTimeoutEnabled(false);
+
+            cfg.setDiscoverySpi(discoSpi);
+        }
+
+        TcpCommunicationSpi commSpi = new TestCommunicationSpi();
+
+        cfg.setCommunicationSpi(commSpi);
+
+        CacheConfiguration ccfg = defaultCacheConfiguration();
+
+        ccfg.setName(CACHE);
+        ccfg.setCacheMode(CacheMode.PARTITIONED);
+        ccfg.setBackups(1);
+        ccfg.setNearConfiguration(null);
+
+        cfg.setCacheConfiguration(ccfg);
+
+        return cfg;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTestsStarted() throws Exception {
+        super.beforeTestsStarted();
+
+        startGrids(NODES_CNT);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTestsStopped() throws Exception {
+        super.afterTestsStopped();
+
+        stopAllGrids();
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testNoHangs() throws Exception {
+        final AtomicBoolean stop = new AtomicBoolean();
+
+        IgniteInternalFuture<Long> restartFut = null;
+
+        try {
+            restartFut = GridTestUtils.runMultiThreadedAsync(new Runnable() {
+                @Override public void run() {
+                    while (!stop.get()) {
+                        try {
+                            U.sleep(500);
+
+                            startGrid(NODES_CNT);
+
+                            awaitPartitionMapExchange();
+
+                            U.sleep(500);
+
+                            stopGrid(NODES_CNT);
+                        }
+                        catch (Exception e) {
+                            // No-op.
+                        }
+                    }
+                }
+            }, 1, "restart-thread");
+
+            long stopTime = System.currentTimeMillis() + 2 * 60_000L;
+
+            for (int i = 0; System.currentTimeMillis() < stopTime; i++) {
+                log.info(">>> Iteration " + i);
+
+                final AtomicInteger threadCnt = new AtomicInteger();
+
+                IgniteInternalFuture<Long> fut = GridTestUtils.runMultiThreadedAsync(new Runnable() {
+                    @Override public void run() {
+                        int threadNum = threadCnt.getAndIncrement();
+
+                        Ignite ignite = ignite(threadNum % NODES_CNT);
+
+                        IgniteCache<Integer, Integer> cache = ignite.cache(CACHE);
+
+                        try (Transaction tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ, 500, 0)) {
+                            ThreadLocalRandom8 rnd = ThreadLocalRandom8.current();
+
+                            for (int i = 0; i < 50; i++) {
+                                int key = rnd.nextInt(50);
+
+                                if (log.isDebugEnabled()) {
+                                    log.info(">>> Performs put [node=" + ((IgniteKernal)ignite).localNode() +
+                                        ", tx=" + tx + ", key=" + key + ']');
+                                }
+
+                                cache.put(key, 0);
+                            }
+
+                            tx.commit();
+                        }
+                        catch (Exception e) {
+                            e.printStackTrace();
+                        }
+                    }
+                }, NODES_CNT * 3, "tx-thread");
+
+                fut.get();
+            }
+        }
+        finally {
+            stop.set(true);
+
+            if (restartFut != null)
+                restartFut.get();
+
+            checkDetectionFuts();
+        }
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testNoDeadlockSimple() throws Exception {
+        final AtomicInteger threadCnt = new AtomicInteger();
+
+        final AtomicBoolean deadlock = new AtomicBoolean();
+
+        final AtomicBoolean timedOut = new AtomicBoolean();
+
+        final CyclicBarrier barrier = new CyclicBarrier(2);
+
+        final int timeout = 500;
+
+        IgniteInternalFuture<Long> fut = GridTestUtils.runMultiThreadedAsync(new Runnable() {
+            @Override public void run() {
+                int threadNum = threadCnt.getAndIncrement();
+
+                Ignite ignite = ignite(threadNum);
+
+                IgniteCache<Integer, Integer> cache = ignite.cache(CACHE);
+
+                try (Transaction tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ, timeout, 0)) {
+                    int key = 42;
+
+                    if (log.isDebugEnabled())
+                        log.debug(">>> Performs put [node=" + ((IgniteKernal)ignite).localNode() +
+                            ", tx=" + tx + ", key=" + key + ']');
+
+                    cache.put(key, 0);
+
+                    barrier.await(timeout + 100, TimeUnit.MILLISECONDS);
+
+                    tx.commit();
+                }
+                catch (Exception e) {
+                    if (hasCause(e, TransactionTimeoutException.class))
+                        timedOut.set(true);
+
+                    if (hasCause(e, TransactionDeadlockException.class))
+                        deadlock.set(true);
+                }
+            }
+        }, 2, "tx-thread");
+
+        fut.get();
+
+        assertTrue(timedOut.get());
+
+        assertFalse(deadlock.get());
+
+        checkDetectionFuts();
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testNoDeadlock() throws Exception {
+        for (int i = 2; i <= 10; i++) {
+            final int threads = i;
+
+            log.info(">>> Test with " + threads + " transactions.");
+
+            final AtomicInteger threadCnt = new AtomicInteger();
+
+            final AtomicBoolean deadlock = new AtomicBoolean();
+
+            final AtomicBoolean timedOut = new AtomicBoolean();
+
+            final CyclicBarrier barrier = new CyclicBarrier(threads);
+
+            final int timeout = 500;
+
+            IgniteInternalFuture<Long> fut = GridTestUtils.runMultiThreadedAsync(new Runnable() {
+                @Override public void run() {
+                    int threadNum = threadCnt.incrementAndGet();
+
+                    Ignite ignite = ignite(threadNum % NODES_CNT);
+
+                    IgniteCache<Integer, Integer> cache = ignite.cache(CACHE);
+
+                    try (Transaction tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ, timeout, 0)) {
+                        int key1 = threadNum;
+
+                        log.info(">>> Performs put [node=" + ((IgniteKernal)ignite).localNode() +
+                            ", tx=" + tx + ", key=" + key1 + ']');
+
+                        cache.put(key1, 0);
+
+                        barrier.await();
+
+                        if (threadNum == threads) {
+                            log.info(">>> Performs sleep. [node=" + ((IgniteKernal)ignite).localNode() +
+                                ", tx=" + tx + ']');
+
+                            U.sleep(timeout * 2);
+                        }
+                        else {
+                            int key2 = threadNum + 1;
+
+                            log.info(">>> Performs put [node=" + ((IgniteKernal)ignite).localNode() +
+                                ", tx=" + tx + ", key2=" + key2 + ']');
+
+                            cache.put(key2, 1);
+                        }
+
+                        tx.commit();
+                    }
+                    catch (Exception e) {
+                        if (hasCause(e, TransactionTimeoutException.class))
+                            timedOut.set(true);
+
+                        if (hasCause(e, TransactionDeadlockException.class))
+                            deadlock.set(true);
+                    }
+                }
+            }, threads, "tx-thread");
+
+            fut.get();
+
+            assertTrue(timedOut.get());
+
+            assertFalse(deadlock.get());
+
+            checkDetectionFuts();
+        }
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testFailedTxLocksRequest() throws Exception {
+        doTestFailedMessage(TxLocksRequest.class);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testFailedTxLocksResponse() throws Exception {
+        doTestFailedMessage(TxLocksResponse.class);
+    }
+
+    /**
+     * @param failCls Failing message class.
+     * @throws Exception If failed.
+     */
+    private void doTestFailedMessage(Class failCls) throws Exception {
+        try {
+            final int txCnt = 2;
+
+            final CyclicBarrier barrier = new CyclicBarrier(2);
+
+            final AtomicInteger threadCnt = new AtomicInteger();
+
+            final AtomicBoolean deadlock = new AtomicBoolean();
+
+            final AtomicBoolean timeout = new AtomicBoolean();
+
+            TestCommunicationSpi.failCls = failCls;
+
+            IgniteInternalFuture<Long> fut = GridTestUtils.runMultiThreadedAsync(new Runnable() {
+                @Override public void run() {
+                    int num = threadCnt.getAndIncrement();
+
+                    Ignite ignite = ignite(num);
+
+                    IgniteCache<Object, Integer> cache = ignite.cache(CACHE);
+
+                    try (Transaction tx =
+                             ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ, num == 0 ? 500 : 1500, 0)
+                    ) {
+                        int key1 = primaryKey(ignite((num + 1) % txCnt).cache(CACHE));
+
+                        log.info(">>> Performs put [node=" + ((IgniteKernal)ignite).localNode() +
+                            ", tx=" + tx + ", key=" + key1 + ']');
+
+                        cache.put(new TestKey(key1), 1);
+
+                        barrier.await();
+
+                        int key2 = primaryKey(cache);
+
+                        log.info(">>> Performs put [node=" + ((IgniteKernal)ignite).localNode() +
+                            ", tx=" + tx + ", key=" + key2 + ']');
+
+                        cache.put(new TestKey(key2), 2);
+
+                        tx.commit();
+                    }
+                    catch (Exception e) {
+                        timeout.compareAndSet(false, hasCause(e, TransactionTimeoutException.class));
+
+                        deadlock.compareAndSet(false, hasCause(e, TransactionDeadlockException.class));
+                    }
+                }
+            }, 2, "tx-thread");
+
+            fut.get();
+
+            assertFalse(deadlock.get());
+
+            assertTrue(timeout.get());
+
+            checkDetectionFuts();
+        }
+        finally {
+            TestCommunicationSpi.failCls = null;
+            TestKey.failSer = false;
+        }
+    }
+
+    /**
+     *
+     */
+    private void checkDetectionFuts() {
+        for (int i = 0; i < NODES_CNT ; i++) {
+            Ignite ignite = ignite(i);
+
+            IgniteTxManager txMgr = ((IgniteKernal)ignite).context().cache().context().tm();
+
+            ConcurrentMap<Long, TxDeadlockDetection.TxDeadlockFuture> futs =
+                GridTestUtils.getFieldValue(txMgr, IgniteTxManager.class, "deadlockDetectFuts");
+
+            assertTrue(futs.isEmpty());
+        }
+    }
+
+    /**
+     *
+     */
+    private static class TestKey implements Externalizable {
+        /** Fail request. */
+        private static volatile boolean failSer = false;
+
+        /** Id. */
+        private int id;
+
+        /**
+         * Default constructor (required by Externalizable).
+         */
+        public TestKey() {
+            // No-op.
+        }
+
+        /**
+         * @param id Id.
+         */
+        public TestKey(int id) {
+            this.id = id;
+        }
+
+        /** {@inheritDoc} */
+        @Override public void writeExternal(ObjectOutput out) throws IOException {
+            out.writeInt(id);
+        }
+
+        /** {@inheritDoc} */
+        @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+            if (failSer) {
+                TestCommunicationSpi.failCls = null;
+                failSer = false;
+
+                throw new IOException();
+            }
+
+            id = in.readInt();
+        }
+
+        /** {@inheritDoc} */
+        @Override public boolean equals(Object o) {
+            if (this == o)
+                return true;
+            if (o == null || getClass() != o.getClass())
+                return false;
+
+            TestKey key = (TestKey)o;
+
+            return id == key.id;
+        }
+
+        /** {@inheritDoc} */
+        @Override public int hashCode() {
+            return id;
+        }
+    }
+
+    /**
+     *
+     */
+    private static class TestCommunicationSpi extends TcpCommunicationSpi {
+        /** Fail response. */
+        private static volatile Class failCls;
+
+        /** {@inheritDoc} */
+        @Override public void sendMessage(
+            ClusterNode node,
+            Message msg,
+            IgniteInClosure<IgniteException> ackC
+        ) throws IgniteSpiException {
+            if (failCls != null && msg instanceof GridIoMessage &&
+                ((GridIoMessage)msg).message().getClass() == failCls)
+                TestKey.failSer = true;
+
+            super.sendMessage(node, msg, ackC);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/TxPessimisticDeadlockDetectionCrossCacheTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/TxPessimisticDeadlockDetectionCrossCacheTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/TxPessimisticDeadlockDetectionCrossCacheTest.java
new file mode 100644
index 0000000..abbefd0
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/TxPessimisticDeadlockDetectionCrossCacheTest.java
@@ -0,0 +1,165 @@
+/*
+ * 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.transactions;
+
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.CyclicBarrier;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.cache.CacheMode;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.IgniteInternalFuture;
+import org.apache.ignite.internal.IgniteKernal;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.apache.ignite.transactions.Transaction;
+import org.apache.ignite.transactions.TransactionDeadlockException;
+import org.apache.ignite.transactions.TransactionTimeoutException;
+
+import static org.apache.ignite.internal.util.typedef.X.hasCause;
+import static org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC;
+import static org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ;
+
+/**
+ *
+ */
+public class TxPessimisticDeadlockDetectionCrossCacheTest extends GridCommonAbstractTest {
+    /** Nodes count. */
+    private static final int NODES_CNT = 2;
+
+    /** {@inheritDoc} */
+    @SuppressWarnings("unchecked")
+    @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception {
+        IgniteConfiguration cfg = super.getConfiguration(gridName);
+
+        if (isDebug()) {
+            TcpDiscoverySpi discoSpi = new TcpDiscoverySpi();
+
+            discoSpi.failureDetectionTimeoutEnabled(false);
+
+            cfg.setDiscoverySpi(discoSpi);
+        }
+
+        CacheConfiguration ccfg0 = defaultCacheConfiguration();
+
+        ccfg0.setName("cache0");
+        ccfg0.setCacheMode(CacheMode.PARTITIONED);
+        ccfg0.setBackups(1);
+        ccfg0.setNearConfiguration(null);
+
+        CacheConfiguration ccfg1 = defaultCacheConfiguration();
+
+        ccfg1.setName("cache1");
+        ccfg1.setCacheMode(CacheMode.PARTITIONED);
+        ccfg1.setBackups(1);
+        ccfg1.setNearConfiguration(null);
+
+        cfg.setCacheConfiguration(ccfg0, ccfg1);
+
+        return cfg;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTestsStarted() throws Exception {
+        super.beforeTestsStarted();
+
+        startGrids(NODES_CNT);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTestsStopped() throws Exception {
+        super.afterTestsStopped();
+
+        stopAllGrids();
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testDeadlock() throws Exception {
+        final CyclicBarrier barrier = new CyclicBarrier(2);
+
+        final AtomicInteger threadCnt = new AtomicInteger();
+
+        final AtomicBoolean deadlock = new AtomicBoolean();
+
+        IgniteInternalFuture<Long> fut = GridTestUtils.runMultiThreadedAsync(new Runnable() {
+            @Override public void run() {
+                int threadNum = threadCnt.getAndIncrement();
+
+                Ignite ignite = ignite(0);
+
+                IgniteCache<Integer, Integer> cache1 = ignite.cache("cache" + (threadNum == 0 ? 0 : 1));
+
+                IgniteCache<Integer, Integer> cache2 = ignite.cache("cache" + (threadNum == 0 ? 1 : 0));
+
+                try (Transaction tx =
+                         ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ, 500, 0)
+                ) {
+                    int key1 = primaryKey(cache1);
+
+                    log.info(">>> Performs put [node=" + ((IgniteKernal)ignite).localNode() +
+                        ", tx=" + tx + ", key=" + key1 + ", cache=" + cache1.getName() + ']');
+
+                    cache1.put(key1, 0);
+
+                    barrier.await();
+
+                    int key2 = primaryKey(cache2);
+
+                    log.info(">>> Performs put [node=" + ((IgniteKernal)ignite).localNode() +
+                        ", tx=" + tx + ", key=" + key2 + ", cache=" + cache2.getName() + ']');
+
+                    cache2.put(key2, 1);
+
+                    tx.commit();
+                }
+                catch (Throwable e) {
+                    // At least one stack trace should contain TransactionDeadlockException.
+                    if (hasCause(e, TransactionTimeoutException.class) &&
+                        hasCause(e, TransactionDeadlockException.class)
+                        ) {
+                        if (deadlock.compareAndSet(false, true))
+                            U.error(log, "At least one stack trace should contain " +
+                                TransactionDeadlockException.class.getSimpleName(), e);
+                    }
+                }
+            }
+        }, 2, "tx-thread");
+
+        fut.get();
+
+        assertTrue(deadlock.get());
+
+        for (int i = 0; i < NODES_CNT ; i++) {
+            Ignite ignite = ignite(i);
+
+            IgniteTxManager txMgr = ((IgniteKernal)ignite).context().cache().context().tm();
+
+            ConcurrentMap<Long, TxDeadlockDetection.TxDeadlockFuture> futs =
+                GridTestUtils.getFieldValue(txMgr, IgniteTxManager.class, "deadlockDetectFuts");
+
+            assertTrue(futs.isEmpty());
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/TxPessimisticDeadlockDetectionTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/TxPessimisticDeadlockDetectionTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/TxPessimisticDeadlockDetectionTest.java
new file mode 100644
index 0000000..ee1a989
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/TxPessimisticDeadlockDetectionTest.java
@@ -0,0 +1,487 @@
+/*
+ * 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.transactions;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.CyclicBarrier;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.cache.CacheMode;
+import org.apache.ignite.cache.CacheWriteSynchronizationMode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.configuration.NearCacheConfiguration;
+import org.apache.ignite.internal.IgniteInternalFuture;
+import org.apache.ignite.internal.IgniteKernal;
+import org.apache.ignite.internal.processors.cache.GridCacheAdapter;
+import org.apache.ignite.internal.processors.cache.GridCacheConcurrentMap;
+import org.apache.ignite.internal.processors.cache.GridCacheMapEntry;
+import org.apache.ignite.internal.processors.cache.IgniteCacheProxy;
+import org.apache.ignite.internal.processors.cache.KeyCacheObject;
+import org.apache.ignite.internal.util.GridConcurrentHashSet;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.lang.IgniteClosure;
+import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.apache.ignite.transactions.Transaction;
+import org.apache.ignite.transactions.TransactionDeadlockException;
+import org.apache.ignite.transactions.TransactionTimeoutException;
+
+import static org.apache.ignite.cache.CacheMode.LOCAL;
+import static org.apache.ignite.cache.CacheMode.PARTITIONED;
+import static org.apache.ignite.cache.CacheMode.REPLICATED;
+import static org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion.NONE;
+import static org.apache.ignite.internal.util.typedef.X.cause;
+import static org.apache.ignite.internal.util.typedef.X.hasCause;
+import static org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC;
+import static org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ;
+
+/**
+ * Tests deadlock detection for pessimistic transactions.
+ */
+public class TxPessimisticDeadlockDetectionTest extends GridCommonAbstractTest {
+    /** Cache name. */
+    private static final String CACHE_NAME = "cache";
+
+    /** Nodes count (actually two times more nodes will started: server + client). */
+    private static final int NODES_CNT = 4;
+
+    /** No op transformer. */
+    private static final NoOpTransformer NO_OP_TRANSFORMER = new NoOpTransformer();
+
+    /** Wrapping transformer. */
+    private static final WrappingTransformer WRAPPING_TRANSFORMER = new WrappingTransformer();
+
+    /** Client mode flag. */
+    private static boolean client;
+
+    /** {@inheritDoc} */
+    @SuppressWarnings("unchecked")
+    @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception {
+        IgniteConfiguration cfg = super.getConfiguration(gridName);
+
+        if (isDebug()) {
+            TcpDiscoverySpi discoSpi = new TcpDiscoverySpi();
+
+            discoSpi.failureDetectionTimeoutEnabled(false);
+
+            cfg.setDiscoverySpi(discoSpi);
+        }
+
+        cfg.setClientMode(client);
+
+        return cfg;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTestsStarted() throws Exception {
+        super.beforeTestsStarted();
+
+        client = false;
+
+        startGrids(NODES_CNT);
+
+        client = true;
+
+        for (int i = 0; i < NODES_CNT; i++)
+            startGrid(i + NODES_CNT);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTestsStopped() throws Exception {
+        super.afterTestsStopped();
+
+        stopAllGrids();
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testDeadlocksPartitioned() throws Exception {
+        for (CacheWriteSynchronizationMode syncMode : CacheWriteSynchronizationMode.values()) {
+            doTestDeadlocks(createCache(PARTITIONED, syncMode, false), NO_OP_TRANSFORMER);
+            doTestDeadlocks(createCache(PARTITIONED, syncMode, false), WRAPPING_TRANSFORMER);
+        }
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void _testDeadlocksPartitionedNear() throws Exception {
+        for (CacheWriteSynchronizationMode syncMode : CacheWriteSynchronizationMode.values()) {
+            doTestDeadlocks(createCache(PARTITIONED, syncMode, true), NO_OP_TRANSFORMER);
+            doTestDeadlocks(createCache(PARTITIONED, syncMode, true), WRAPPING_TRANSFORMER);
+        }
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testDeadlocksReplicated() throws Exception {
+        for (CacheWriteSynchronizationMode syncMode : CacheWriteSynchronizationMode.values()) {
+            doTestDeadlocks(createCache(REPLICATED, syncMode, false), NO_OP_TRANSFORMER);
+            doTestDeadlocks(createCache(REPLICATED, syncMode, false), WRAPPING_TRANSFORMER);
+        }
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testDeadlocksLocal() throws Exception {
+        for (CacheWriteSynchronizationMode syncMode : CacheWriteSynchronizationMode.values()) {
+            IgniteCache cache = null;
+
+            try {
+                cache = createCache(LOCAL, syncMode, false);
+
+                awaitPartitionMapExchange();
+
+                doTestDeadlock(2, true, true, false, NO_OP_TRANSFORMER);
+                doTestDeadlock(2, true, true, false, WRAPPING_TRANSFORMER);
+            }
+            finally {
+                if (cache != null)
+                    cache.destroy();
+            }
+        }
+    }
+
+    /**
+     * @param cacheMode Cache mode.
+     * @param syncMode Write sync mode.
+     * @param near Near.
+     */
+    @SuppressWarnings("unchecked")
+    private IgniteCache createCache(CacheMode cacheMode, CacheWriteSynchronizationMode syncMode, boolean near) {
+        CacheConfiguration ccfg = defaultCacheConfiguration();
+
+        ccfg.setName(CACHE_NAME);
+        ccfg.setCacheMode(cacheMode);
+        ccfg.setBackups(1);
+        ccfg.setNearConfiguration(near ? new NearCacheConfiguration() : null);
+        ccfg.setWriteSynchronizationMode(syncMode);
+
+        return ignite(0).getOrCreateCache(ccfg);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    private void doTestDeadlocks(IgniteCache cache, IgniteClosure<Integer, Object> transformer) throws Exception {
+        try {
+            awaitPartitionMapExchange();
+
+            doTestDeadlock(2, false, true, true, transformer);
+            doTestDeadlock(2, false, false, false, transformer);
+            doTestDeadlock(2, false, false, true, transformer);
+
+            doTestDeadlock(3, false, true, true, transformer);
+            doTestDeadlock(3, false, false, false, transformer);
+            doTestDeadlock(3, false, false, true, transformer);
+
+            doTestDeadlock(4, false, true, true, transformer);
+            doTestDeadlock(4, false, false, false, transformer);
+            doTestDeadlock(4, false, false, true, transformer);
+        }
+        catch (Exception e) {
+            U.error(log, "Unexpected exception: ", e);
+
+            fail();
+        }
+        finally {
+            if (cache != null)
+                cache.destroy();
+        }
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    private void doTestDeadlock(
+        final int txCnt,
+        final boolean loc,
+        boolean lockPrimaryFirst,
+        final boolean clientTx,
+        final IgniteClosure<Integer, Object> transformer
+    ) throws Exception {
+        log.info(">>> Test deadlock [txCnt=" + txCnt + ", loc=" + loc + ", lockPrimaryFirst=" + lockPrimaryFirst +
+            ", clientTx=" + clientTx + ", transformer=" + transformer.getClass().getName() + ']');
+
+        final AtomicInteger threadCnt = new AtomicInteger();
+
+        final CyclicBarrier barrier = new CyclicBarrier(txCnt);
+
+        final AtomicReference<TransactionDeadlockException> deadlockErr = new AtomicReference<>();
+
+        final List<List<Integer>> keySets = generateKeys(txCnt, loc, !lockPrimaryFirst);
+
+        final Set<Integer> involvedKeys = new GridConcurrentHashSet<>();
+        final Set<Integer> involvedLockedKeys = new GridConcurrentHashSet<>();
+        final Set<IgniteInternalTx> involvedTxs = new GridConcurrentHashSet<>();
+
+        IgniteInternalFuture<Long> fut = GridTestUtils.runMultiThreadedAsync(new Runnable() {
+            @Override public void run() {
+                int threadNum = threadCnt.incrementAndGet();
+
+                Ignite ignite = loc ? ignite(0) : ignite(clientTx ? threadNum - 1 + txCnt : threadNum - 1);
+
+                IgniteCache<Object, Integer> cache = ignite.cache(CACHE_NAME);
+
+                List<Integer> keys = keySets.get(threadNum - 1);
+
+                int txTimeout = 500 + txCnt * 100;
+
+                try (Transaction tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ, txTimeout, 0)) {
+                    involvedTxs.add(((TransactionProxyImpl)tx).tx());
+
+                    Integer key = keys.get(0);
+
+                    involvedKeys.add(key);
+
+                    Object k;
+
+                    log.info(">>> Performs put [node=" + ((IgniteKernal)ignite).localNode() +
+                        ", tx=" + tx + ", key=" + transformer.apply(key) + ']');
+
+                    cache.put(transformer.apply(key), 0);
+
+                    involvedLockedKeys.add(key);
+
+                    barrier.await();
+
+                    key = keys.get(1);
+
+                    ClusterNode primaryNode =
+                        ((IgniteCacheProxy)cache).context().affinity().primary(key, NONE);
+
+                    List<Integer> primaryKeys =
+                        primaryKeys(grid(primaryNode).cache(CACHE_NAME), 5, key + (100 * threadNum));
+
+                    Map<Object, Integer> entries = new HashMap<>();
+
+                    involvedKeys.add(key);
+
+                    entries.put(transformer.apply(key), 0);
+
+                    for (Integer i : primaryKeys) {
+                        involvedKeys.add(i);
+
+                        entries.put(transformer.apply(i), 1);
+
+                        k = transformer.apply(i + 13);
+
+                        involvedKeys.add(i + 13);
+
+                        entries.put(k, 2);
+                    }
+
+                    log.info(">>> Performs put [node=" + ((IgniteKernal)ignite).localNode() +
+                        ", tx=" + tx + ", entries=" + entries + ']');
+
+                    cache.putAll(entries);
+
+                    tx.commit();
+                }
+                catch (Throwable e) {
+                    // At least one stack trace should contain TransactionDeadlockException.
+                    if (hasCause(e, TransactionTimeoutException.class) &&
+                        hasCause(e, TransactionDeadlockException.class)
+                        ) {
+                        if (deadlockErr.compareAndSet(null, cause(e, TransactionDeadlockException.class)))
+                            U.error(log, "At least one stack trace should contain " +
+                                TransactionDeadlockException.class.getSimpleName(), e);
+                    }
+                }
+            }
+        }, loc ? 2 : txCnt, "tx-thread");
+
+        fut.get();
+
+        U.sleep(1000);
+
+        TransactionDeadlockException deadlockE = deadlockErr.get();
+
+        assertNotNull(deadlockE);
+
+        // Check transactions, futures and entry locks state.
+        for (int i = 0; i < NODES_CNT * 2; i++) {
+            Ignite ignite = ignite(i);
+
+            int cacheId = ((IgniteCacheProxy)ignite.cache(CACHE_NAME)).context().cacheId();
+
+            IgniteTxManager txMgr = ((IgniteKernal)ignite).context().cache().context().tm();
+
+            Collection<IgniteInternalTx> activeTxs = txMgr.activeTransactions();
+
+            for (IgniteInternalTx tx : activeTxs) {
+                Collection<IgniteTxEntry> entries = tx.allEntries();
+
+                for (IgniteTxEntry entry : entries) {
+                    if (entry.cacheId() == cacheId)
+                        fail("Transaction still exists: " + tx);
+                }
+            }
+
+            ConcurrentMap<Long, TxDeadlockDetection.TxDeadlockFuture> futs =
+                GridTestUtils.getFieldValue(txMgr, IgniteTxManager.class, "deadlockDetectFuts");
+
+            assertTrue(futs.isEmpty());
+
+            GridCacheAdapter<Object, Integer> intCache = internalCache(i, CACHE_NAME);
+
+            GridCacheConcurrentMap map = intCache.map();
+
+            for (Integer key : involvedKeys) {
+                Object key0 = transformer.apply(key);
+
+                KeyCacheObject keyCacheObj = intCache.context().toCacheKeyObject(key0);
+
+                GridCacheMapEntry entry = map.getEntry(keyCacheObj);
+
+                if (entry != null)
+                    assertNull("Entry still has locks " + entry, entry.mvccAllLocal());
+            }
+        }
+
+        // Check deadlock report
+        String msg = deadlockE.getMessage();
+
+        for (IgniteInternalTx tx : involvedTxs)
+            assertTrue(msg.contains(
+                "[txId=" + tx.xidVersion() + ", nodeId=" + tx.nodeId() + ", threadId=" + tx.threadId() + ']'));
+
+        for (Integer key : involvedKeys) {
+            if (involvedLockedKeys.contains(key))
+                assertTrue(msg.contains("[key=" + transformer.apply(key) + ", cache=" + CACHE_NAME + ']'));
+            else
+                assertFalse(msg.contains("[key=" + transformer.apply(key)));
+        }
+    }
+
+    /**
+     * @param nodesCnt Nodes count.
+     * @param loc Local cache.
+     */
+    private List<List<Integer>> generateKeys(int nodesCnt, boolean loc, boolean reverse) throws IgniteCheckedException {
+        List<List<Integer>> keySets = new ArrayList<>();
+
+        if (loc) {
+            List<Integer> keys = primaryKeys(ignite(0).cache(CACHE_NAME), 2);
+
+            keySets.add(new ArrayList<>(keys));
+
+            Collections.reverse(keys);
+
+            keySets.add(keys);
+        }
+        else {
+            for (int i = 0; i < nodesCnt; i++) {
+                List<Integer> keys = new ArrayList<>(2);
+
+                keys.add(primaryKey(ignite(i).cache(CACHE_NAME)));
+                keys.add(primaryKey(ignite(i == nodesCnt - 1 ? 0 : i + 1).cache(CACHE_NAME)));
+
+                if (reverse)
+                    Collections.reverse(keys);
+
+                keySets.add(keys);
+            }
+        }
+
+        return keySets;
+    }
+
+    /**
+     *
+     */
+    private static class NoOpTransformer implements IgniteClosure<Integer, Object> {
+        /** {@inheritDoc} */
+        @Override public Object apply(Integer val) {
+            return val;
+        }
+    }
+
+    /**
+     *
+     */
+    private static class WrappingTransformer implements IgniteClosure<Integer, Object> {
+        /** {@inheritDoc} */
+        @Override public Object apply(Integer val) {
+            return new KeyObject(val);
+        }
+    }
+
+    /**
+     *
+     */
+    private static class KeyObject implements Serializable {
+        /** Id. */
+        private int id;
+
+        /** Name. */
+        private String name;
+
+        /**
+         * @param id Id.
+         */
+        public KeyObject(int id) {
+            this.id = id;
+            this.name = "KeyObject" + id;
+        }
+
+        /** {@inheritDoc} */
+        @Override public String toString() {
+            return "KeyObject{" +
+                "id=" + id +
+                ", name='" + name + '\'' +
+                '}';
+        }
+
+        /** {@inheritDoc} */
+        @Override public boolean equals(Object o) {
+            if (this == o)
+                return true;
+
+            if (o == null || getClass() != o.getClass())
+                return false;
+
+            KeyObject obj = (KeyObject)o;
+
+            return id == obj.id && name.equals(obj.name);
+
+        }
+
+        /** {@inheritDoc} */
+        @Override public int hashCode() {
+            return id;
+        }
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/test/java/org/apache/ignite/testsuites/BinaryObjectsTxDeadlockDetectionTestSuite.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/testsuites/BinaryObjectsTxDeadlockDetectionTestSuite.java b/modules/core/src/test/java/org/apache/ignite/testsuites/BinaryObjectsTxDeadlockDetectionTestSuite.java
new file mode 100644
index 0000000..45afba2
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/testsuites/BinaryObjectsTxDeadlockDetectionTestSuite.java
@@ -0,0 +1,37 @@
+/*
+ * 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.testsuites;
+
+import junit.framework.TestSuite;
+import org.apache.ignite.internal.binary.BinaryMarshaller;
+import org.apache.ignite.testframework.config.GridTestProperties;
+
+/**
+ *
+ */
+public class BinaryObjectsTxDeadlockDetectionTestSuite {
+    /**
+     * @return Test suite.
+     * @throws Exception If failed.
+     */
+    public static TestSuite suite() throws Exception {
+        GridTestProperties.setProperty(GridTestProperties.MARSH_CLASS_NAME, BinaryMarshaller.class.getName());
+
+        return TxDeadlockDetectionTestSuite.suite();
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/test/java/org/apache/ignite/testsuites/TxDeadlockDetectionTestSuite.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/testsuites/TxDeadlockDetectionTestSuite.java b/modules/core/src/test/java/org/apache/ignite/testsuites/TxDeadlockDetectionTestSuite.java
new file mode 100644
index 0000000..c057e55
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/testsuites/TxDeadlockDetectionTestSuite.java
@@ -0,0 +1,44 @@
+/*
+ * 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.testsuites;
+
+import junit.framework.TestSuite;
+import org.apache.ignite.internal.processors.cache.transactions.DepthFirstSearchTest;
+import org.apache.ignite.internal.processors.cache.transactions.TxDeadlockDetectionTest;
+import org.apache.ignite.internal.processors.cache.transactions.TxPessimisticDeadlockDetectionCrossCacheTest;
+import org.apache.ignite.internal.processors.cache.transactions.TxPessimisticDeadlockDetectionTest;
+
+/**
+ * Deadlock detection related tests.
+ */
+public class TxDeadlockDetectionTestSuite extends TestSuite {
+    /**
+     * @return Test suite.
+     * @throws Exception If failed.
+     */
+    public static TestSuite suite() throws Exception {
+        TestSuite suite = new TestSuite("Ignite Deadlock Detection Test Suite");
+
+        suite.addTestSuite(DepthFirstSearchTest.class);
+        suite.addTestSuite(TxPessimisticDeadlockDetectionTest.class);
+        suite.addTestSuite(TxPessimisticDeadlockDetectionCrossCacheTest.class);
+        suite.addTestSuite(TxDeadlockDetectionTest.class);
+
+        return suite;
+    }
+}


[10/10] ignite git commit: Merge branch 'master' into ignite-1786

Posted by vo...@apache.org.
Merge branch 'master' into ignite-1786


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/ce276463
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/ce276463
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/ce276463

Branch: refs/heads/ignite-1786
Commit: ce2764637220b7923f8e924d016f562b0a11bc18
Parents: 8d824ab 0f2b753
Author: vozerov-gridgain <vo...@gridgain.com>
Authored: Wed May 4 17:17:45 2016 +0300
Committer: vozerov-gridgain <vo...@gridgain.com>
Committed: Wed May 4 17:17:45 2016 +0300

----------------------------------------------------------------------
 .../java/org/apache/ignite/IgniteCache.java     | 197 ++++--
 .../apache/ignite/IgniteSystemProperties.java   |  11 +
 .../org/apache/ignite/internal/GridTopic.java   |   5 +-
 .../ignite/internal/IgniteComponentType.java    |   4 +-
 .../communication/GridIoMessageFactory.java     |  24 +
 .../processors/cache/GridCacheMapEntry.java     |  16 +
 .../processors/cache/GridCacheMvcc.java         |   7 +
 .../processors/cache/GridCacheMvccManager.java  |  20 +-
 .../processors/cache/GridCacheProcessor.java    |   9 +-
 .../cache/GridCacheSharedContext.java           |   2 +-
 .../distributed/dht/GridDhtLockFuture.java      |  22 +-
 .../distributed/dht/GridDhtTxLocalAdapter.java  |  11 +-
 .../colocated/GridDhtColocatedLockFuture.java   |  71 ++-
 .../distributed/near/GridNearLockFuture.java    |   3 +-
 .../cache/distributed/near/GridNearTxLocal.java |  39 +-
 .../near/GridNearTxPrepareFutureAdapter.java    |   1 +
 .../cache/local/GridLocalLockFuture.java        |  74 ++-
 .../continuous/CacheContinuousQueryHandler.java |   9 +
 .../cache/transactions/IgniteTxAdapter.java     |  17 +-
 .../transactions/IgniteTxLocalAdapter.java      |  44 +-
 .../cache/transactions/IgniteTxManager.java     | 456 +++++++++++++-
 .../cache/transactions/TxDeadlock.java          | 159 +++++
 .../cache/transactions/TxDeadlockDetection.java | 599 +++++++++++++++++++
 .../processors/cache/transactions/TxLock.java   | 225 +++++++
 .../cache/transactions/TxLockList.java          | 134 +++++
 .../cache/transactions/TxLocksRequest.java      | 205 +++++++
 .../cache/transactions/TxLocksResponse.java     | 318 ++++++++++
 .../service/GridServiceNotFoundException.java   |   4 +-
 .../service/GridServiceProcessor.java           | 199 +++---
 .../processors/service/GridServiceProxy.java    |  15 +-
 .../processors/service/ServiceContextImpl.java  |  29 +-
 .../ignite/internal/util/IgniteUtils.java       |   4 +
 .../ignite/spi/discovery/tcp/ServerImpl.java    | 214 ++++---
 .../spi/discovery/tcp/TcpDiscoverySpi.java      |   5 +-
 .../TcpDiscoveryClientHeartbeatMessage.java     |   1 +
 .../TcpDiscoveryClientReconnectMessage.java     |  16 +
 .../apache/ignite/transactions/Transaction.java |  19 +-
 .../TransactionDeadlockException.java           |  42 ++
 .../TransactionTimeoutException.java            |   5 +-
 .../processors/cache/CacheTxFastFinishTest.java | 253 ++++++++
 .../transactions/DepthFirstSearchTest.java      | 252 ++++++++
 .../transactions/TxDeadlockDetectionTest.java   | 495 +++++++++++++++
 ...simisticDeadlockDetectionCrossCacheTest.java | 165 +++++
 .../TxPessimisticDeadlockDetectionTest.java     | 487 +++++++++++++++
 .../tcp/TcpClientDiscoverySpiSelfTest.java      |  44 +-
 ...naryObjectsTxDeadlockDetectionTestSuite.java |  37 ++
 .../ignite/testsuites/IgniteCacheTestSuite.java |   3 +
 .../TxDeadlockDetectionTestSuite.java           |  44 ++
 .../IgniteCacheStarvationOnRebalanceTest.java   | 166 +++++
 .../IgniteCacheWithIndexingTestSuite.java       |   2 +
 .../org/apache/ignite/spark/IgniteRDDSpec.scala |  38 +-
 .../GridServiceInjectionSpringResourceTest.java | 245 ++++++++
 .../spring/injection/spring-resource.tmpl.xml   |  66 ++
 .../testsuites/IgniteSpringTestSuite.java       |   3 +
 54 files changed, 5220 insertions(+), 315 deletions(-)
----------------------------------------------------------------------



[02/10] ignite git commit: Fixed compilation.

Posted by vo...@apache.org.
Fixed compilation.


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/34c03833
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/34c03833
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/34c03833

Branch: refs/heads/ignite-1786
Commit: 34c038337813e37d3aecc6a8f8520a9cbfc4ee01
Parents: e78ed8e
Author: Alexey Kuznetsov <ak...@apache.org>
Authored: Wed May 4 10:23:45 2016 +0700
Committer: Alexey Kuznetsov <ak...@apache.org>
Committed: Wed May 4 10:23:45 2016 +0700

----------------------------------------------------------------------
 .../src/test/scala/org/apache/ignite/spark/IgniteRDDSpec.scala     | 2 --
 1 file changed, 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/34c03833/modules/spark/src/test/scala/org/apache/ignite/spark/IgniteRDDSpec.scala
----------------------------------------------------------------------
diff --git a/modules/spark/src/test/scala/org/apache/ignite/spark/IgniteRDDSpec.scala b/modules/spark/src/test/scala/org/apache/ignite/spark/IgniteRDDSpec.scala
index a41839d..a25eaac 100644
--- a/modules/spark/src/test/scala/org/apache/ignite/spark/IgniteRDDSpec.scala
+++ b/modules/spark/src/test/scala/org/apache/ignite/spark/IgniteRDDSpec.scala
@@ -17,8 +17,6 @@
 
 package org.apache.ignite.spark
 
-import java.time.LocalDate
-
 import org.apache.ignite.Ignition
 import org.apache.ignite.cache.query.annotations.{QueryTextField, QuerySqlField}
 import org.apache.ignite.configuration.{CacheConfiguration, IgniteConfiguration}


[06/10] ignite git commit: ignite-3073 Possible thread starvation due to rebalancing (no need to wait for utility cache rebalance to get metadata)

Posted by vo...@apache.org.
ignite-3073 Possible thread starvation due to rebalancing (no need to wait for utility cache rebalance to get metadata)


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/f3eb94e0
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/f3eb94e0
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/f3eb94e0

Branch: refs/heads/ignite-1786
Commit: f3eb94e0ffb935546be0288e0520f3e406a2a543
Parents: 718cf78
Author: sboikov <sb...@gridgain.com>
Authored: Wed May 4 14:19:50 2016 +0300
Committer: sboikov <sb...@gridgain.com>
Committed: Wed May 4 14:19:50 2016 +0300

----------------------------------------------------------------------
 .../processors/cache/GridCacheProcessor.java    |   9 +-
 .../IgniteCacheStarvationOnRebalanceTest.java   | 166 +++++++++++++++++++
 .../IgniteCacheWithIndexingTestSuite.java       |   2 +
 3 files changed, 172 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/f3eb94e0/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java
index 3d5052b..218d313 100755
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java
@@ -821,6 +821,9 @@ public class GridCacheProcessor extends GridProcessorAdapter {
         for (GridCacheAdapter<?, ?> cache : caches.values())
             onKernalStart(cache);
 
+        if (!ctx.config().isDaemon())
+            ctx.cacheObjects().onUtilityCacheStarted();
+
         // Wait for caches in SYNC preload mode.
         for (CacheConfiguration cfg : ctx.config().getCacheConfiguration()) {
             GridCacheAdapter cache = caches.get(maskNull(cfg.getName()));
@@ -831,12 +834,8 @@ public class GridCacheProcessor extends GridProcessorAdapter {
                         (cfg.getCacheMode() == PARTITIONED && cfg.getRebalanceDelay() >= 0)) {
                         boolean utilityCache = CU.isUtilityCache(cache.name());
 
-                        if (utilityCache || CU.isMarshallerCache(cache.name())) {
+                        if (utilityCache || CU.isMarshallerCache(cache.name()))
                             cache.preloader().initialRebalanceFuture().get();
-
-                            if (utilityCache)
-                                ctx.cacheObjects().onUtilityCacheStarted();
-                        }
                         else
                             cache.preloader().syncFuture().get();
                     }

http://git-wip-us.apache.org/repos/asf/ignite/blob/f3eb94e0/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheStarvationOnRebalanceTest.java
----------------------------------------------------------------------
diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheStarvationOnRebalanceTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheStarvationOnRebalanceTest.java
new file mode 100644
index 0000000..17e0c74
--- /dev/null
+++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheStarvationOnRebalanceTest.java
@@ -0,0 +1,166 @@
+/*
+ * 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;
+
+import java.util.concurrent.Callable;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.atomic.AtomicBoolean;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.cache.CacheAtomicityMode;
+import org.apache.ignite.cache.CacheMode;
+import org.apache.ignite.cache.query.annotations.QuerySqlField;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.IgniteInternalFuture;
+import org.apache.ignite.internal.binary.BinaryMarshaller;
+import org.apache.ignite.internal.util.typedef.internal.S;
+import org.apache.ignite.testframework.GridTestUtils;
+
+import static org.apache.ignite.cache.CacheAtomicityMode.ATOMIC;
+import static org.apache.ignite.cache.CacheMode.PARTITIONED;
+
+/**
+ * Test to reproduce https://issues.apache.org/jira/browse/IGNITE-3073.
+ */
+public class IgniteCacheStarvationOnRebalanceTest extends GridCacheAbstractSelfTest {
+    /** Grid count. */
+    private static final int GRID_CNT = 4;
+
+    /** Test timeout. */
+    private static final long TEST_TIMEOUT = 3 * 60 * 1000;
+
+    /** Use small system thread pool to reproduce the issue. */
+    private static final int IGNITE_THREAD_POOL_SIZE = 5;
+
+    /** {@inheritDoc} */
+    @Override protected long getTestTimeout() {
+        return TEST_TIMEOUT;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception {
+        IgniteConfiguration cfg = super.getConfiguration(gridName);
+
+        // Use small system thread pool to reproduce the issue.
+        cfg.setSystemThreadPoolSize(IGNITE_THREAD_POOL_SIZE);
+
+        cfg.setMarshaller(new BinaryMarshaller());
+
+        return cfg;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected Class<?>[] indexedTypes() {
+        return new Class<?>[] {Integer.class, CacheValue.class};
+    }
+
+    /** {@inheritDoc} */
+    @Override protected CacheAtomicityMode atomicityMode() {
+        return ATOMIC;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected int gridCount() {
+        return GRID_CNT;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected CacheMode cacheMode() {
+        return PARTITIONED;
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testLoadSystemWithPutAndStartRebalancing() throws Exception {
+        final IgniteCache<Integer, CacheValue> cache = grid(0).cache(null);
+
+        final long endTime = System.currentTimeMillis() + TEST_TIMEOUT - 60_000;
+
+        int iter = 0;
+
+        while (System.currentTimeMillis() < endTime) {
+            info("Iteration: " + iter++);
+
+            final AtomicBoolean stop = new AtomicBoolean();
+
+            IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {
+                @Override public Void call() throws Exception {
+                    ThreadLocalRandom rnd = ThreadLocalRandom.current();
+
+                    while (!stop.get() && System.currentTimeMillis() < endTime) {
+                        int key = rnd.nextInt(100_000);
+
+                        cache.put(key, new CacheValue(key));
+                    }
+
+                    return null;
+                }
+            }, IGNITE_THREAD_POOL_SIZE * 4, "put-thread");
+
+            try {
+                Thread.sleep(500);
+
+                info("Initial set of keys is loaded.");
+
+                info("Starting new node...");
+
+                startGrid(GRID_CNT + 1);
+
+                info("New node is started.");
+
+                Thread.sleep(500);
+            }
+            finally {
+                stop.set(true);
+            }
+
+            // Wait for put tasks. If put() is blocked the test is timed out.
+            fut.get();
+
+            stopGrid(GRID_CNT + 1);
+        }
+    }
+
+    /**
+     * Test cache value.
+     */
+    private static class CacheValue {
+        /** */
+        @QuerySqlField(index = true)
+        private final int val;
+
+        /**
+         * @param val Value.
+         */
+        CacheValue(int val) {
+            this.val = val;
+        }
+
+        /**
+         * @return Value.
+         */
+        int value() {
+            return val;
+        }
+
+        /** {@inheritDoc} */
+        @Override public String toString() {
+            return S.toString(CacheValue.class, this);
+        }
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ignite/blob/f3eb94e0/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheWithIndexingTestSuite.java
----------------------------------------------------------------------
diff --git a/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheWithIndexingTestSuite.java b/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheWithIndexingTestSuite.java
index 9ff7520..929fb37 100644
--- a/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheWithIndexingTestSuite.java
+++ b/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheWithIndexingTestSuite.java
@@ -28,6 +28,7 @@ import org.apache.ignite.internal.processors.cache.GridCacheOffheapIndexGetSelfT
 import org.apache.ignite.internal.processors.cache.GridCacheSwapSelfTest;
 import org.apache.ignite.internal.processors.cache.GridIndexingWithNoopSwapSelfTest;
 import org.apache.ignite.internal.processors.cache.IgniteCacheConfigurationPrimitiveTypesSelfTest;
+import org.apache.ignite.internal.processors.cache.IgniteCacheStarvationOnRebalanceTest;
 import org.apache.ignite.internal.processors.cache.IgniteClientReconnectQueriesTest;
 import org.apache.ignite.internal.processors.cache.ttl.CacheTtlOffheapAtomicLocalSelfTest;
 import org.apache.ignite.internal.processors.cache.ttl.CacheTtlOffheapAtomicPartitionedSelfTest;
@@ -72,6 +73,7 @@ public class IgniteCacheWithIndexingTestSuite extends TestSuite {
         suite.addTestSuite(IgniteCacheConfigurationPrimitiveTypesSelfTest.class);
         suite.addTestSuite(IgniteClientReconnectQueriesTest.class);
         suite.addTestSuite(CacheRandomOperationsMultithreadedTest.class);
+        suite.addTestSuite(IgniteCacheStarvationOnRebalanceTest.class);
 
         return suite;
     }


[08/10] ignite git commit: ignite-2854 Deadlock detection for pessimistic transactions

Posted by vo...@apache.org.
http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/TxDeadlock.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/TxDeadlock.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/TxDeadlock.java
new file mode 100644
index 0000000..f8130e1
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/TxDeadlock.java
@@ -0,0 +1,159 @@
+/*
+ * 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.transactions;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import org.apache.ignite.internal.processors.cache.GridCacheContext;
+import org.apache.ignite.internal.processors.cache.GridCacheSharedContext;
+import org.apache.ignite.internal.processors.cache.version.GridCacheVersion;
+import org.apache.ignite.internal.util.typedef.T2;
+import org.apache.ignite.internal.util.typedef.internal.CU;
+import org.apache.ignite.internal.util.typedef.internal.U;
+
+/**
+ * Information about found deadlock.
+ */
+public class TxDeadlock {
+    /** Key prefix. */
+    private static final String KEY_PREFIX = "K";
+
+    /** Tx prefix. */
+    private static final String TX_PREFIX = "TX";
+
+    /** Tx locked keys. */
+    private final Map<GridCacheVersion, Set<IgniteTxKey>> txLockedKeys;
+
+    /** Tx requested keys. */
+    private final Map<IgniteTxKey, Set<GridCacheVersion>> txRequestedKeys;
+
+    /** Cycle. */
+    private final List<GridCacheVersion> cycle;
+
+    /** Transactions data: nearNodeId and threadId. */
+    private final Map<GridCacheVersion, T2<UUID, Long>> txs;
+
+    /**
+     * @param cycle Cycle.
+     * @param txs Transactions.
+     * @param txLockedKeys Tx locked keys.
+     * @param txRequestedKeys Tx requested keys.
+     */
+    public TxDeadlock(
+        List<GridCacheVersion> cycle,
+        Map<GridCacheVersion, T2<UUID, Long>> txs,
+        Map<GridCacheVersion, Set<IgniteTxKey>> txLockedKeys,
+        Map<IgniteTxKey, Set<GridCacheVersion>> txRequestedKeys
+    ) {
+        this.cycle = cycle;
+        this.txLockedKeys = txLockedKeys;
+        this.txRequestedKeys = txRequestedKeys;
+        this.txs = txs;
+    }
+
+    /**
+     * @return Deadlock represented as cycle of transaction in wait-for-graph.
+     */
+    public List<GridCacheVersion> cycle() {
+        return cycle;
+    }
+
+    /**
+     * @param ctx Context.
+     */
+    public String toString(GridCacheSharedContext ctx) {
+        assert cycle != null && !cycle.isEmpty();
+
+        assert cycle.size() >= 3; // At least 2 transactions in cycle and the last is waiting for the first.
+
+        Map<IgniteTxKey, String> keyLabels = U.newLinkedHashMap(cycle.size() - 1);
+
+        Map<GridCacheVersion, String> txLabels = U.newLinkedHashMap(cycle.size() - 1);
+
+        StringBuilder sb = new StringBuilder("\nDeadlock detected:\n\n");
+
+        for (int i = cycle.size() - 1; i > 0; i--) {
+            GridCacheVersion txId = cycle.get(i);
+
+            Set<IgniteTxKey> keys = txLockedKeys.get(txId);
+
+            for (IgniteTxKey key : keys) {
+                Set<GridCacheVersion> txIds = txRequestedKeys.get(key);
+
+                if (txIds == null || txIds.isEmpty())
+                    continue;
+
+                GridCacheVersion waitsTx = null;
+
+                for (GridCacheVersion ver : txIds) {
+                    if (cycle.contains(ver)) {
+                        waitsTx = ver;
+
+                        break;
+                    }
+                }
+
+                if (waitsTx != null) {
+                    sb.append(label(key, KEY_PREFIX, keyLabels)).append(": ")
+                        .append(label(txId, TX_PREFIX, txLabels)).append(" holds lock, ")
+                        .append(label(waitsTx, TX_PREFIX, txLabels)).append(" waits lock.\n");
+                }
+            }
+        }
+
+        sb.append("\nTransactions:\n\n");
+
+        for (Map.Entry<GridCacheVersion, String> e : txLabels.entrySet()) {
+            T2<UUID, Long> tx = txs.get(e.getKey());
+
+            sb.append(e.getValue()).append(" [txId=").append(e.getKey())
+                .append(", nodeId=").append(tx.get1()).append(", threadId=").append(tx.get2())
+                .append("]\n");
+        }
+
+        sb.append("\nKeys:\n\n");
+
+        for (Map.Entry<IgniteTxKey, String> e : keyLabels.entrySet()) {
+            IgniteTxKey txKey = e.getKey();
+
+            GridCacheContext cctx = ctx.cacheContext(txKey.cacheId());
+
+            Object val = CU.value(txKey.key(), cctx, true);
+
+            sb.append(e.getValue()).append(" [key=").append(val).append(", cache=").append(cctx.namexx()).append("]\n");
+        }
+
+        return sb.toString();
+    }
+
+    /**
+     * @param id Id.
+     * @param prefix Prefix.
+     * @param map Map.
+     */
+    private static <T> String label(T id, String prefix, Map<T, String> map) {
+        String lb = map.get(id);
+
+        if (lb == null)
+            map.put(id, lb = prefix + (map.size() + 1));
+
+        return lb;
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/TxDeadlockDetection.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/TxDeadlockDetection.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/TxDeadlockDetection.java
new file mode 100644
index 0000000..36843dd
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/TxDeadlockDetection.java
@@ -0,0 +1,599 @@
+/*
+ * 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.transactions;
+
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.atomic.AtomicLong;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.ClusterNode;
+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.version.GridCacheVersion;
+import org.apache.ignite.internal.processors.timeout.GridTimeoutObjectAdapter;
+import org.apache.ignite.internal.util.future.GridFutureAdapter;
+import org.apache.ignite.internal.util.typedef.T2;
+import org.apache.ignite.internal.util.typedef.internal.S;
+import org.jetbrains.annotations.Nullable;
+
+import static org.apache.ignite.IgniteSystemProperties.IGNITE_TX_DEADLOCK_DETECTION_TIMEOUT;
+import static org.apache.ignite.IgniteSystemProperties.getInteger;
+import static org.apache.ignite.internal.processors.cache.transactions.IgniteTxManager.DEADLOCK_MAX_ITERS;
+
+/**
+ * Transactions deadlock detection.
+ */
+public class TxDeadlockDetection {
+    /** Deadlock detection maximum iterations. */
+    private static final int DEADLOCK_TIMEOUT = getInteger(IGNITE_TX_DEADLOCK_DETECTION_TIMEOUT, 60000);
+
+    /** Sequence. */
+    private static final AtomicLong SEQ = new AtomicLong();
+
+    /** Cctx. */
+    private final GridCacheSharedContext cctx;
+
+    /** Logger. */
+    private final IgniteLogger log;
+
+    /**
+     * @param cctx Context.
+     */
+    public TxDeadlockDetection(GridCacheSharedContext<?, ?> cctx) {
+        this.cctx = cctx;
+        this.log = cctx.logger(TxDeadlockDetection.class);
+    }
+
+    /**
+     * Detects deadlock starting from given keys.
+     *
+     * @param tx Target tx.
+     * @param keys Keys.
+     * @return {@link TxDeadlock} if found, otherwise - {@code null}.
+     */
+    TxDeadlockFuture detectDeadlock(IgniteInternalTx tx, Set<IgniteTxKey> keys) {
+        GridCacheVersion txId = tx.nearXidVersion();
+
+        if (log.isDebugEnabled()) {
+            log.debug("Deadlock detection started " +
+                "[nodeId=" + cctx.localNodeId() + ", xidVersion=" + txId + ", keys=" + keys + ']');
+        }
+
+        TxDeadlockFuture fut = new TxDeadlockFuture(cctx, txId, tx.topologyVersion(), keys);
+
+        fut.init();
+
+        return fut;
+    }
+
+    /**
+     * @param wfg Wait-for-graph.
+     * @param txId Tx ID - start vertex for cycle search in graph.
+     */
+    static List<GridCacheVersion> findCycle(Map<GridCacheVersion, Set<GridCacheVersion>> wfg, GridCacheVersion txId) {
+        if (wfg == null || wfg.isEmpty())
+            return null;
+
+        ArrayDeque<GridCacheVersion> stack = new ArrayDeque<>();
+        Set<GridCacheVersion> inPath = new HashSet<>();
+        Set<GridCacheVersion> visited = new HashSet<>();
+        Map<GridCacheVersion, GridCacheVersion> edgeTo = new HashMap<>();
+
+        stack.push(txId);
+
+        while (!stack.isEmpty()) {
+            GridCacheVersion v = stack.pop();
+
+            if (visited.contains(v))
+                continue;
+
+            visited.add(v);
+
+            Set<GridCacheVersion> children = wfg.get(v);
+
+            if (children == null || children.isEmpty())
+                continue;
+
+            inPath.add(v);
+
+            for (GridCacheVersion w : children) {
+                if (inPath.contains(w)) {
+                    List<GridCacheVersion> cycle = new ArrayList<>();
+
+                    for (GridCacheVersion x = v; !x.equals(w); x = edgeTo.get(x))
+                        cycle.add(x);
+
+                    cycle.add(w);
+                    cycle.add(v);
+
+                    return cycle;
+                }
+
+                edgeTo.put(w, v);
+                stack.push(w);
+            }
+        }
+
+        return null;
+    }
+
+    /**
+     *
+     */
+    static class TxDeadlockFuture extends GridFutureAdapter<TxDeadlock> {
+        /** Context. */
+        private final GridCacheSharedContext cctx;
+
+        /** Future ID. */
+        private final long futId = SEQ.incrementAndGet();
+
+        /** Tx ID. */
+        private final GridCacheVersion txId;
+
+        /** Keys. */
+        private final Set<IgniteTxKey> keys;
+
+        /** Processed keys. */
+        private final Set<IgniteTxKey> processedKeys = new HashSet<>();
+
+        /** Processed nodes. */
+        private final Set<UUID> processedNodes = new HashSet<>();
+
+        /** Pending keys. */
+        private Map<UUID, Set<IgniteTxKey>> pendingKeys = new HashMap<>();
+
+        /** Nodes queue. */
+        private final UniqueDeque<UUID> nodesQueue = new UniqueDeque<>();
+
+        /** Preferred nodes. */
+        private final Set<UUID> preferredNodes = new HashSet<>();
+
+        /** Tx locked keys. */
+        private final Map<GridCacheVersion, Set<IgniteTxKey>> txLockedKeys = new HashMap<>();
+
+        /** Tx requested keys. */
+        private final Map<IgniteTxKey, Set<GridCacheVersion>> txRequestedKeys = new HashMap<>();
+
+        /** Wait-for-graph. */
+        private final Map<GridCacheVersion, Set<GridCacheVersion>> wfg = new HashMap<>();
+
+        /** Topology version. */
+        private final AffinityTopologyVersion topVer;
+
+        /** Transactions. */
+        private final Map<GridCacheVersion, T2<UUID, Long>> txs = new HashMap<>();
+
+        /** Current processing node ID. */
+        private UUID curNodeId;
+
+        /** Iterations count. */
+        private int itersCnt;
+
+        /** Timeout object. */
+        private DeadlockTimeoutObject timeoutObj;
+
+        /** Timed out flag. */
+        private volatile boolean timedOut;
+
+        /** Mutex. */
+        private final Object mux = new Object();
+
+        /**
+         * @param cctx Context.
+         * @param txId Tx ID.
+         * @param topVer Transaction topology version.
+         * @param keys Keys.
+         */
+        @SuppressWarnings("unchecked")
+        private TxDeadlockFuture(GridCacheSharedContext cctx,
+            GridCacheVersion txId,
+            AffinityTopologyVersion topVer,
+            Set<IgniteTxKey> keys) {
+            this.cctx = cctx;
+            this.txId = txId;
+            this.topVer = topVer;
+            this.keys = keys;
+
+            if (DEADLOCK_TIMEOUT > 0) {
+                timeoutObj = new DeadlockTimeoutObject();
+
+                cctx.time().addTimeoutObject(timeoutObj);
+            }
+        }
+
+        /**
+         * @return Future ID.
+         */
+        long futureId() {
+            return futId;
+        }
+
+        /**
+         * @param nodeId Node ID.
+         */
+        public void onNodeLeft(UUID nodeId) {
+            if (compareAndSet(nodeId, null)) {
+                IgniteLogger log = cctx.logger(TxDeadlockDetection.class);
+
+                if (log.isDebugEnabled())
+                    log.debug("Failed to finish deadlock detection, node left: " + nodeId);
+
+                onDone();
+            }
+        }
+
+        /** */
+        private void init() {
+            cctx.tm().addFuture(this);
+
+            if (topVer == null) // Tx manager already stopped
+                onDone();
+
+            map(keys, Collections.<IgniteTxKey, TxLockList>emptyMap());
+        }
+
+        /**
+         * @param keys Keys.
+         * @param txLocks Tx locks.
+         */
+        private void map(@Nullable Set<IgniteTxKey> keys, Map<IgniteTxKey, TxLockList> txLocks) {
+            mapTxKeys(keys, txLocks);
+
+            UUID nodeId = nodesQueue.pollFirst();
+
+            boolean set = compareAndSet(null, nodeId);
+
+            assert set;
+
+            if (nodeId == null || itersCnt++ >= DEADLOCK_MAX_ITERS || timedOut)
+                onDone();
+            else {
+                final Set<IgniteTxKey> txKeys = pendingKeys.get(nodeId);
+
+                processedKeys.addAll(txKeys);
+                processedNodes.add(nodeId);
+                pendingKeys.remove(nodeId);
+
+                cctx.tm().txLocksInfo(nodeId, this, txKeys);
+            }
+        }
+
+        /**
+         * @param res Response.
+         */
+        private void detect(TxLocksResponse res) {
+            assert res != null;
+
+            merge(res);
+
+            updateWaitForGraph(res.txLocks());
+
+            List<GridCacheVersion> cycle = findCycle(wfg, txId);
+
+            if (cycle != null)
+                onDone(new TxDeadlock(cycle, txs, txLockedKeys, txRequestedKeys));
+            else
+                map(res.keys(), res.txLocks());
+        }
+
+        /**
+         * Maps tx keys on nodes. Key can be mapped on some node if this node is primary for given key or
+         * node is near for transaction that holds or requests lock for key.
+         *
+         * Key will not be be mapped to node if both key and node are already handled.
+         *
+         * @param txKeys Tx keys.
+         * @param txLocks Tx locks.
+         */
+        @SuppressWarnings("ForLoopReplaceableByForEach")
+        private void mapTxKeys(@Nullable Set<IgniteTxKey> txKeys, Map<IgniteTxKey, TxLockList> txLocks) {
+            for (Map.Entry<IgniteTxKey, TxLockList> e : txLocks.entrySet()) {
+                List<TxLock> locks = e.getValue().txLocks();
+
+                for (int i = 0; i < locks.size(); i++) {
+                    TxLock txLock = locks.get(i);
+
+                    UUID nearNodeId = txLock.nearNodeId();
+
+                    IgniteTxKey txKey = e.getKey();
+
+                    if (processedKeys.contains(txKey) && processedNodes.contains(nearNodeId))
+                        continue;
+
+                    if (txLock.requested()) {
+                        UUID nodeId = primary(txKey);
+
+                        // Process this node earlier than other in order to optimize amount of requests.
+                        preferredNodes.add(nodeId);
+
+                        Set<IgniteTxKey> mappedKeys = pendingKeys.get(nodeId);
+
+                        if (mappedKeys == null)
+                            pendingKeys.put(nodeId, mappedKeys = new HashSet<>());
+
+                        mappedKeys.add(txKey);
+                    }
+                    else {
+                        if (txLock.owner()) {
+                            if (!preferredNodes.contains(nearNodeId))
+                                nodesQueue.addFirst(nearNodeId);
+                        }
+                        else
+                            nodesQueue.addLast(nearNodeId);
+
+                        Set<IgniteTxKey> mappedKeys = pendingKeys.get(nearNodeId);
+
+                        if (mappedKeys == null)
+                            pendingKeys.put(nearNodeId, mappedKeys = new HashSet<>());
+
+                        mappedKeys.add(txKey);
+                    }
+                }
+            }
+
+            for (UUID nodeId : preferredNodes)
+                nodesQueue.addFirst(nodeId);
+
+            preferredNodes.clear();
+
+            if (txKeys != null) {
+                for (IgniteTxKey txKey : txKeys) {
+                    UUID nodeId = primary(txKey);
+
+                    if (processedKeys.contains(txKey) && processedNodes.contains(nodeId))
+                        continue;
+
+                    nodesQueue.addLast(nodeId);
+
+                    Set<IgniteTxKey> mappedKeys = pendingKeys.get(nodeId);
+
+                    if (mappedKeys == null)
+                        pendingKeys.put(nodeId, mappedKeys = new HashSet<>());
+
+                    mappedKeys.add(txKey);
+                }
+            }
+        }
+
+        /**
+         * @param txKey Tx key.
+         * @return Primary node ID.
+         */
+        private UUID primary(IgniteTxKey txKey) {
+            GridCacheContext ctx = cctx.cacheContext(txKey.cacheId());
+
+            ClusterNode node = ctx.affinity().primary(txKey.key(), topVer);
+
+            assert node != null : topVer;
+
+            return node.id();
+        }
+
+        /**
+         * @param res Tx locks.
+         */
+        private void merge(TxLocksResponse res) {
+            Map<IgniteTxKey, TxLockList> txLocks = res.txLocks();
+
+            if (txLocks == null || txLocks.isEmpty())
+                return;
+
+            for (Map.Entry<IgniteTxKey, TxLockList> e : txLocks.entrySet()) {
+                IgniteTxKey txKey = e.getKey();
+
+                TxLockList lockList = e.getValue();
+
+                if (lockList != null && !lockList.isEmpty()) {
+                    for (TxLock lock : lockList.txLocks()) {
+                        if (lock.owner() || lock.candiate()) {
+                            if (txs.get(lock.txId()) == null)
+                                txs.put(lock.txId(), new T2<>(lock.nearNodeId(), lock.threadId()));
+                        }
+
+                        if (lock.owner()) {
+                            GridCacheVersion txId = lock.txId();
+
+                            Set<IgniteTxKey> keys = txLockedKeys.get(txId);
+
+                            if (keys == null)
+                                txLockedKeys.put(txId, keys = new HashSet<>());
+
+                            keys.add(txKey);
+                        }
+                        else if (lock.candiate()) {
+                            Set<GridCacheVersion> txs = txRequestedKeys.get(txKey);
+
+                            if (txs == null)
+                                txRequestedKeys.put(txKey, txs = new HashSet<>());
+
+                            txs.add(lock.txId());
+                        }
+                    }
+                }
+            }
+        }
+
+        /**
+         * @param txLocks Tx locks.
+         */
+        private void updateWaitForGraph(Map<IgniteTxKey, TxLockList> txLocks) {
+            for (Map.Entry<IgniteTxKey, TxLockList> e : txLocks.entrySet()) {
+
+                GridCacheVersion txOwner = null;
+
+                for (TxLock lock : e.getValue().txLocks()) {
+                    if (lock.owner()) {
+                        assert txOwner == null;
+
+                        txOwner = lock.txId();
+
+                        if (keys.contains(e.getKey()) && !txId.equals(lock.txId())) {
+                            Set<GridCacheVersion> waitingTxs = wfg.get(txId);
+
+                            if (waitingTxs == null)
+                                wfg.put(txId, waitingTxs = new HashSet<>());
+
+                            waitingTxs.add(lock.txId());
+                        }
+
+                        continue;
+                    }
+
+                    if (lock.candiate()) {
+                        GridCacheVersion txId0 = lock.txId();
+
+                        Set<GridCacheVersion> waitForTxs = wfg.get(txId0);
+
+                        if (waitForTxs == null)
+                            wfg.put(txId0, waitForTxs = new HashSet<>());
+
+                        waitForTxs.add(txOwner);
+                    }
+                }
+            }
+        }
+
+        /**
+         * @param res Response.
+         */
+        public void onResult(UUID nodeId, TxLocksResponse res) {
+            boolean set = compareAndSet(nodeId, null);
+
+            if (res != null && set) {
+                if (res.classError() != null) {
+                    IgniteLogger log = cctx.logger(TxDeadlockDetection.class);
+
+                    log.warning("Failed to finish deadlock detection due to an error: " + nodeId);
+
+                    onDone();
+                }
+                else
+                    detect(res);
+            }
+            else
+                onDone();
+        }
+
+        /**
+         * @param exp Expected.
+         * @param val Value.
+         */
+        private boolean compareAndSet(UUID exp, UUID val) {
+            synchronized (mux) {
+                if (Objects.equals(curNodeId, exp)) {
+                    curNodeId = val;
+
+                    return true;
+                }
+            }
+
+            return false;
+        }
+
+        /** {@inheritDoc} */
+        @Override public boolean onDone(@Nullable TxDeadlock res, @Nullable Throwable err) {
+            if (super.onDone(res, err)) {
+                cctx.tm().removeFuture(futId);
+
+                if (timeoutObj != null)
+                    cctx.time().removeTimeoutObject(timeoutObj);
+
+                return true;
+            }
+
+            return false;
+        }
+
+        /**
+         * Lock request timeout object.
+         */
+        private class DeadlockTimeoutObject extends GridTimeoutObjectAdapter {
+            /**
+             * Default constructor.
+             */
+            DeadlockTimeoutObject() {
+                super(DEADLOCK_TIMEOUT);
+            }
+
+            /** {@inheritDoc} */
+            @Override public void onTimeout() {
+                timedOut = true;
+
+                onDone();
+            }
+
+            /** {@inheritDoc} */
+            @Override public String toString() {
+                return S.toString(DeadlockTimeoutObject.class, this);
+            }
+        }
+    }
+
+    /**
+     * Deque with Set semantic.
+     * Only overridden methods can be used.
+     */
+    private static class UniqueDeque<E> extends ArrayDeque<E> {
+        /** Serial version UID. */
+        private static final long serialVersionUID = 0L;
+
+        /** Items. */
+        private final Set<E> items = new HashSet<>();
+
+        /** {@inheritDoc} */
+        @Override public void addFirst(E e) {
+            boolean contains, first = false;
+
+            if ((contains = items.contains(e)) && !(first = getFirst().equals(e)))
+                remove(e);
+
+            if (!contains)
+                items.add(e);
+
+            if (!first)
+                super.addFirst(e);
+        }
+
+        /** {@inheritDoc} */
+        @Override public void addLast(E e) {
+            if (!items.contains(e)) {
+                super.addLast(e);
+
+                items.add(e);
+            }
+        }
+
+        /** {@inheritDoc} */
+        @Override public E pollFirst() {
+            E e = super.pollFirst();
+
+            items.remove(e);
+
+            return e;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/TxLock.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/TxLock.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/TxLock.java
new file mode 100644
index 0000000..af85735
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/TxLock.java
@@ -0,0 +1,225 @@
+/*
+ * 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.transactions;
+
+import java.nio.ByteBuffer;
+import java.util.UUID;
+import org.apache.ignite.internal.processors.cache.GridCacheMvccCandidate;
+import org.apache.ignite.internal.processors.cache.version.GridCacheVersion;
+import org.apache.ignite.internal.util.typedef.internal.S;
+import org.apache.ignite.plugin.extensions.communication.Message;
+import org.apache.ignite.plugin.extensions.communication.MessageReader;
+import org.apache.ignite.plugin.extensions.communication.MessageWriter;
+
+/**
+ * Corresponds to one {@link GridCacheMvccCandidate} from local MVCC candidates queue.
+ * There is one exclusion: {@link TxLock} instance with {@link #OWNERSHIP_REQUESTED} corresponds to lock request
+ * to remote node from near node that isn't primary node for key.
+ */
+public class TxLock implements Message {
+    /** Serial version UID. */
+    private static final long serialVersionUID = 0L;
+
+    /** Ownership owner. */
+    static final byte OWNERSHIP_OWNER = 1;
+
+    /** Ownership candidate. */
+    static final byte OWNERSHIP_CANDIDATE = 2;
+
+    /** Ownership requested. */
+    static final byte OWNERSHIP_REQUESTED = 3;
+
+    /** Near node ID. */
+    private UUID nearNodeId;
+
+    /** Tx ID. */
+    private GridCacheVersion txId;
+
+    /** Thread ID. */
+    private long threadId;
+
+    /** Ownership. */
+    private byte ownership;
+
+    /**
+     * Default constructor.
+     */
+    public TxLock() {
+        // No-op.
+    }
+
+    /**
+     * @param txId Tx ID.
+     * @param nearNodeId Near node ID.
+     * @param threadId Thread ID.
+     * @param ownership Ownership.
+     */
+    public TxLock(GridCacheVersion txId, UUID nearNodeId, long threadId, byte ownership) {
+        this.txId = txId;
+        this.nearNodeId = nearNodeId;
+        this.threadId = threadId;
+        this.ownership = ownership;
+    }
+
+    /**
+     * @return Near node ID.
+     */
+    public UUID nearNodeId() {
+        return nearNodeId;
+    }
+
+    /**
+     * @return Transaction ID.
+     */
+    public GridCacheVersion txId() {
+        return txId;
+    }
+
+    /**
+     * @return Thread ID.
+     */
+    public long threadId() {
+        return threadId;
+    }
+
+    /**
+     * @return {@code True} if transaction hold lock on the key, otherwise {@code false}.
+     */
+    public boolean owner() {
+        return ownership == OWNERSHIP_OWNER;
+    }
+
+    /**
+     * @return {@code True} if there is MVCC candidate for this transaction and key, otherwise {@code false}.
+     */
+    public boolean candiate() {
+        return ownership == OWNERSHIP_CANDIDATE;
+    }
+
+    /**
+     * @return {@code True} if transaction requested lock for key from primary remote node
+     * but response isn't received because other transaction hold lock on the key.
+     */
+    public boolean requested() {
+        return ownership == OWNERSHIP_REQUESTED;
+    }
+
+    /** {@inheritDoc} */
+    @Override public String toString() {
+        return S.toString(TxLock.class, this);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean writeTo(ByteBuffer buf, MessageWriter writer) {
+        writer.setBuffer(buf);
+
+        if (!writer.isHeaderWritten()) {
+            if (!writer.writeHeader(directType(), fieldsCount()))
+                return false;
+
+            writer.onHeaderWritten();
+        }
+
+        switch (writer.state()) {
+            case 0:
+                if (!writer.writeUuid("nearNodeId", nearNodeId))
+                    return false;
+
+                writer.incrementState();
+
+            case 1:
+                if (!writer.writeByte("ownership", ownership))
+                    return false;
+
+                writer.incrementState();
+
+            case 2:
+                if (!writer.writeLong("threadId", threadId))
+                    return false;
+
+                writer.incrementState();
+
+            case 3:
+                if (!writer.writeMessage("txId", txId))
+                    return false;
+
+                writer.incrementState();
+        }
+
+        return true;
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean readFrom(ByteBuffer buf, MessageReader reader) {
+        reader.setBuffer(buf);
+
+        if (!reader.beforeMessageRead())
+            return false;
+
+        switch (reader.state()) {
+            case 0:
+                nearNodeId = reader.readUuid("nearNodeId");
+
+                if (!reader.isLastRead())
+                    return false;
+
+                reader.incrementState();
+
+            case 1:
+                ownership = reader.readByte("ownership");
+
+                if (!reader.isLastRead())
+                    return false;
+
+                reader.incrementState();
+
+            case 2:
+                threadId = reader.readLong("threadId");
+
+                if (!reader.isLastRead())
+                    return false;
+
+                reader.incrementState();
+
+            case 3:
+                txId = reader.readMessage("txId");
+
+                if (!reader.isLastRead())
+                    return false;
+
+                reader.incrementState();
+        }
+
+        return reader.afterMessageRead(TxLock.class);
+    }
+
+    /** {@inheritDoc} */
+    @Override public byte directType() {
+        return -25;
+    }
+
+    /** {@inheritDoc} */
+    @Override public byte fieldsCount() {
+        return 4;
+    }
+
+    /** {@inheritDoc} */
+    @Override public void onAckReceived() {
+        // No-op.
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/TxLockList.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/TxLockList.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/TxLockList.java
new file mode 100644
index 0000000..8b1c2dd
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/TxLockList.java
@@ -0,0 +1,134 @@
+/*
+ * 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.transactions;
+
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.ignite.internal.GridDirectCollection;
+import org.apache.ignite.internal.util.tostring.GridToStringInclude;
+import org.apache.ignite.internal.util.typedef.internal.S;
+import org.apache.ignite.plugin.extensions.communication.Message;
+import org.apache.ignite.plugin.extensions.communication.MessageCollectionItemType;
+import org.apache.ignite.plugin.extensions.communication.MessageReader;
+import org.apache.ignite.plugin.extensions.communication.MessageWriter;
+
+/**
+ * List of transaction locks for particular key.
+ */
+public class TxLockList implements Message {
+    /** Serial version UID. */
+    private static final long serialVersionUID = 0L;
+
+    /** Tx locks. */
+    @GridToStringInclude
+    @GridDirectCollection(value = TxLock.class)
+    private List<TxLock> txLocks = new ArrayList<>();
+
+    /**
+     * Default constructor.
+     */
+    public TxLockList() {
+        // No-op.
+    }
+
+    /**
+     * @return Lock list.
+     */
+    public List<TxLock> txLocks() {
+        return txLocks;
+    }
+
+    /**
+     * @param txLock Tx lock.
+     */
+    public void add(TxLock txLock) {
+        txLocks.add(txLock);
+    }
+
+    /**
+     * @return {@code True} if lock list is empty.
+     */
+    public boolean isEmpty() {
+        return txLocks.isEmpty();
+    }
+
+    /** {@inheritDoc} */
+    @Override public String toString() {
+        return S.toString(TxLockList.class, this);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean writeTo(ByteBuffer buf, MessageWriter writer) {
+        writer.setBuffer(buf);
+
+        if (!writer.isHeaderWritten()) {
+            if (!writer.writeHeader(directType(), fieldsCount()))
+                return false;
+
+            writer.onHeaderWritten();
+        }
+
+        switch (writer.state()) {
+            case 0:
+                if (!writer.writeCollection("txLocks", txLocks, MessageCollectionItemType.MSG))
+                    return false;
+
+                writer.incrementState();
+
+        }
+
+        return true;
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean readFrom(ByteBuffer buf, MessageReader reader) {
+        reader.setBuffer(buf);
+
+        if (!reader.beforeMessageRead())
+            return false;
+
+        switch (reader.state()) {
+            case 0:
+                txLocks = reader.readCollection("txLocks", MessageCollectionItemType.MSG);
+
+                if (!reader.isLastRead())
+                    return false;
+
+                reader.incrementState();
+
+        }
+
+        return reader.afterMessageRead(TxLockList.class);
+    }
+
+    /** {@inheritDoc} */
+    @Override public byte directType() {
+        return -26;
+    }
+
+    /** {@inheritDoc} */
+    @Override public byte fieldsCount() {
+        return 1;
+    }
+
+    /** {@inheritDoc} */
+    @Override public void onAckReceived() {
+        // No-op.
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/TxLocksRequest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/TxLocksRequest.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/TxLocksRequest.java
new file mode 100644
index 0000000..20afcc6
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/TxLocksRequest.java
@@ -0,0 +1,205 @@
+/*
+ * 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.transactions;
+
+import java.nio.ByteBuffer;
+import java.util.Collection;
+import java.util.Set;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.internal.GridDirectTransient;
+import org.apache.ignite.internal.processors.cache.GridCacheMessage;
+import org.apache.ignite.internal.processors.cache.GridCacheSharedContext;
+import org.apache.ignite.internal.util.tostring.GridToStringExclude;
+import org.apache.ignite.internal.util.tostring.GridToStringInclude;
+import org.apache.ignite.internal.util.typedef.internal.A;
+import org.apache.ignite.internal.util.typedef.internal.S;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.plugin.extensions.communication.MessageCollectionItemType;
+import org.apache.ignite.plugin.extensions.communication.MessageReader;
+import org.apache.ignite.plugin.extensions.communication.MessageWriter;
+
+/**
+ * Transactions lock list request.
+ */
+public class TxLocksRequest extends GridCacheMessage {
+    /** Serial version UID. */
+    private static final long serialVersionUID = 0L;
+
+    /** Future ID. */
+    private long futId;
+
+    /** Tx keys. */
+    @GridToStringInclude
+    @GridDirectTransient
+    private Set<IgniteTxKey> txKeys;
+
+    /** Array of txKeys from {@link #txKeys}. Used during marshalling and unmarshalling. */
+    @GridToStringExclude
+    private IgniteTxKey[] txKeysArr;
+
+    /**
+     * Default constructor.
+     */
+    public TxLocksRequest() {
+        // No-op.
+    }
+
+    /**
+     * @param futId Future ID.
+     * @param txKeys Target tx keys.
+     */
+    public TxLocksRequest(long futId, Set<IgniteTxKey> txKeys) {
+        A.notEmpty(txKeys, "txKeys");
+
+        this.futId = futId;
+        this.txKeys = txKeys;
+    }
+
+    /**
+     * @return Future ID.
+     */
+    public long futureId() {
+        return futId;
+    }
+
+    /**
+     * @return Tx keys.
+     */
+    public Collection<IgniteTxKey> txKeys() {
+        return txKeys;
+    }
+
+    /** {@inheritDoc} */
+    @Override public String toString() {
+        return S.toString(TxLocksRequest.class, this);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean addDeploymentInfo() {
+        return addDepInfo;
+    }
+
+    /** {@inheritDoc} */
+    @Override public void prepareMarshal(GridCacheSharedContext ctx) throws IgniteCheckedException {
+        super.prepareMarshal(ctx);
+
+        txKeysArr = new IgniteTxKey[txKeys.size()];
+
+        int i = 0;
+
+        for (IgniteTxKey key : txKeys) {
+            key.prepareMarshal(ctx.cacheContext(key.cacheId()));
+
+            txKeysArr[i++] = key;
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void finishUnmarshal(GridCacheSharedContext ctx, ClassLoader ldr) throws IgniteCheckedException {
+        super.finishUnmarshal(ctx, ldr);
+
+        txKeys = U.newHashSet(txKeysArr.length);
+
+        for (IgniteTxKey key : txKeysArr) {
+            key.finishUnmarshal(ctx.cacheContext(key.cacheId()), ldr);
+
+            txKeys.add(key);
+        }
+
+        txKeysArr = null;
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean writeTo(ByteBuffer buf, MessageWriter writer) {
+        writer.setBuffer(buf);
+
+        if (!super.writeTo(buf, writer))
+            return false;
+
+        if (!writer.isHeaderWritten()) {
+            if (!writer.writeHeader(directType(), fieldsCount()))
+                return false;
+
+            writer.onHeaderWritten();
+        }
+
+        switch (writer.state()) {
+            case 3:
+                if (!writer.writeLong("futId", futId))
+                    return false;
+
+                writer.incrementState();
+
+            case 4:
+                if (!writer.writeObjectArray("txKeysArr", txKeysArr, MessageCollectionItemType.MSG))
+                    return false;
+
+                writer.incrementState();
+
+        }
+
+        return true;
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean readFrom(ByteBuffer buf, MessageReader reader) {
+        reader.setBuffer(buf);
+
+        if (!reader.beforeMessageRead())
+            return false;
+
+        if (!super.readFrom(buf, reader))
+            return false;
+
+        switch (reader.state()) {
+            case 3:
+                futId = reader.readLong("futId");
+
+                if (!reader.isLastRead())
+                    return false;
+
+                reader.incrementState();
+
+            case 4:
+                txKeysArr = reader.readObjectArray("txKeysArr", MessageCollectionItemType.MSG, IgniteTxKey.class);
+
+                if (!reader.isLastRead())
+                    return false;
+
+                reader.incrementState();
+
+        }
+
+        return reader.afterMessageRead(TxLocksRequest.class);
+    }
+
+    /** {@inheritDoc} */
+    @Override public byte directType() {
+        return -24;
+    }
+
+    /** {@inheritDoc} */
+    @Override public byte fieldsCount() {
+        return 5;
+    }
+
+    /** {@inheritDoc} */
+    @Override public void onAckReceived() {
+        // No-op.
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/TxLocksResponse.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/TxLocksResponse.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/TxLocksResponse.java
new file mode 100644
index 0000000..fa6afdd
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/TxLocksResponse.java
@@ -0,0 +1,318 @@
+/*
+ * 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.transactions;
+
+import java.nio.ByteBuffer;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.internal.GridDirectTransient;
+import org.apache.ignite.internal.processors.cache.GridCacheMessage;
+import org.apache.ignite.internal.processors.cache.GridCacheSharedContext;
+import org.apache.ignite.internal.util.tostring.GridToStringExclude;
+import org.apache.ignite.internal.util.tostring.GridToStringInclude;
+import org.apache.ignite.internal.util.typedef.internal.S;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.plugin.extensions.communication.MessageCollectionItemType;
+import org.apache.ignite.plugin.extensions.communication.MessageReader;
+import org.apache.ignite.plugin.extensions.communication.MessageWriter;
+
+/**
+ * Transactions lock list response.
+ */
+public class TxLocksResponse extends GridCacheMessage {
+    /** Serial version UID. */
+    private static final long serialVersionUID = 0L;
+
+    /** Future ID. */
+    private long futId;
+
+    /** Locks for near txKeys of near transactions. */
+    @GridToStringInclude
+    @GridDirectTransient
+    private Map<IgniteTxKey, TxLockList> nearTxKeyLocks = new HashMap<>();
+
+    /** Remote keys involved into transactions. Doesn't include near keys. */
+    @GridToStringInclude
+    @GridDirectTransient
+    private Set<IgniteTxKey> txKeys;
+
+    /** Array of txKeys from {@link #nearTxKeyLocks}. Used during marshalling and unmarshalling. */
+    @GridToStringExclude
+    private IgniteTxKey[] nearTxKeysArr;
+
+    /** Array of txKeys from {@link #txKeys}. Used during marshalling and unmarshalling. */
+    @GridToStringExclude
+    private IgniteTxKey[] txKeysArr;
+
+    /** Array of locksArr from {@link #nearTxKeyLocks}. Used during marshalling and unmarshalling. */
+    @GridToStringExclude
+    private TxLockList[] locksArr;
+
+    /**
+     * Default constructor.
+     */
+    public TxLocksResponse() {
+        // No-op.
+    }
+
+    /**
+     * @return Future ID.
+     */
+    public long futureId() {
+        return futId;
+    }
+
+    /**
+     * @param futId Future ID.
+     */
+    public void futureId(long futId) {
+        this.futId = futId;
+    }
+
+    /**
+     * @return Lock lists for all tx nearTxKeysArr.
+     */
+    public Map<IgniteTxKey, TxLockList> txLocks() {
+        return nearTxKeyLocks;
+    }
+
+    /**
+     * @param txKey Tx key.
+     * @return Lock list for given tx key.
+     */
+    public TxLockList txLocks(IgniteTxKey txKey) {
+        return nearTxKeyLocks.get(txKey);
+    }
+
+    /**
+     * @param txKey Tx key.
+     * @param txLock Tx lock.
+     */
+    public void addTxLock(IgniteTxKey txKey, TxLock txLock) {
+        TxLockList lockList = nearTxKeyLocks.get(txKey);
+
+        if (lockList == null)
+            nearTxKeyLocks.put(txKey, lockList = new TxLockList());
+
+        lockList.add(txLock);
+    }
+
+    /**
+     * @return Remote txKeys involved into tx.
+     */
+    public Set<IgniteTxKey> keys() {
+        return txKeys;
+    }
+
+    /**
+     * @param key Key.
+     */
+    public void addKey(IgniteTxKey key) {
+        if (txKeys == null)
+            txKeys = new HashSet<>();
+
+        txKeys.add(key);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean addDeploymentInfo() {
+        return addDepInfo;
+    }
+
+    /** {@inheritDoc} */
+    @Override public String toString() {
+        return S.toString(TxLocksResponse.class, this);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void prepareMarshal(GridCacheSharedContext ctx) throws IgniteCheckedException {
+        super.prepareMarshal(ctx);
+
+        if (nearTxKeyLocks != null && !nearTxKeyLocks.isEmpty()) {
+            int len = nearTxKeyLocks.size();
+
+            nearTxKeysArr = new IgniteTxKey[len];
+            locksArr = new TxLockList[len];
+
+            int i = 0;
+
+            for (Map.Entry<IgniteTxKey, TxLockList> entry : nearTxKeyLocks.entrySet()) {
+                IgniteTxKey key = entry.getKey();
+
+                key.prepareMarshal(ctx.cacheContext(key.cacheId()));
+
+                nearTxKeysArr[i] = key;
+                locksArr[i] = entry.getValue();
+
+                i++;
+            }
+        }
+
+        if (txKeys != null && !txKeys.isEmpty()) {
+            txKeysArr = new IgniteTxKey[txKeys.size()];
+
+            int i = 0;
+
+            for (IgniteTxKey key : txKeys) {
+                key.prepareMarshal(ctx.cacheContext(key.cacheId()));
+
+                txKeysArr[i++] = key;
+            }
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void finishUnmarshal(GridCacheSharedContext ctx, ClassLoader ldr) throws IgniteCheckedException {
+        super.finishUnmarshal(ctx, ldr);
+
+        if (nearTxKeysArr != null) {
+            for (int i = 0; i < nearTxKeysArr.length; i++) {
+                IgniteTxKey key = nearTxKeysArr[i];
+
+                key.finishUnmarshal(ctx.cacheContext(key.cacheId()), ldr);
+
+                txLocks().put(key, locksArr[i]);
+            }
+
+            nearTxKeysArr = null;
+            locksArr = null;
+        }
+
+        if (txKeysArr != null) {
+            txKeys = U.newHashSet(txKeysArr.length);
+
+            for (IgniteTxKey key : txKeysArr) {
+                key.finishUnmarshal(ctx.cacheContext(key.cacheId()), ldr);
+
+                txKeys.add(key);
+            }
+
+            txKeysArr = null;
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean writeTo(ByteBuffer buf, MessageWriter writer) {
+        writer.setBuffer(buf);
+
+        if (!super.writeTo(buf, writer))
+            return false;
+
+        if (!writer.isHeaderWritten()) {
+            if (!writer.writeHeader(directType(), fieldsCount()))
+                return false;
+
+            writer.onHeaderWritten();
+        }
+
+        switch (writer.state()) {
+            case 3:
+                if (!writer.writeLong("futId", futId))
+                    return false;
+
+                writer.incrementState();
+
+            case 4:
+                if (!writer.writeObjectArray("locksArr", locksArr, MessageCollectionItemType.MSG))
+                    return false;
+
+                writer.incrementState();
+
+            case 5:
+                if (!writer.writeObjectArray("nearTxKeysArr", nearTxKeysArr, MessageCollectionItemType.MSG))
+                    return false;
+
+                writer.incrementState();
+
+            case 6:
+                if (!writer.writeObjectArray("txKeysArr", txKeysArr, MessageCollectionItemType.MSG))
+                    return false;
+
+                writer.incrementState();
+
+        }
+
+        return true;
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean readFrom(ByteBuffer buf, MessageReader reader) {
+        reader.setBuffer(buf);
+
+        if (!reader.beforeMessageRead())
+            return false;
+
+        if (!super.readFrom(buf, reader))
+            return false;
+
+        switch (reader.state()) {
+            case 3:
+                futId = reader.readLong("futId");
+
+                if (!reader.isLastRead())
+                    return false;
+
+                reader.incrementState();
+
+            case 4:
+                locksArr = reader.readObjectArray("locksArr", MessageCollectionItemType.MSG, TxLockList.class);
+
+                if (!reader.isLastRead())
+                    return false;
+
+                reader.incrementState();
+
+            case 5:
+                nearTxKeysArr = reader.readObjectArray("nearTxKeysArr", MessageCollectionItemType.MSG, IgniteTxKey.class);
+
+                if (!reader.isLastRead())
+                    return false;
+
+                reader.incrementState();
+
+            case 6:
+                txKeysArr = reader.readObjectArray("txKeysArr", MessageCollectionItemType.MSG, IgniteTxKey.class);
+
+                if (!reader.isLastRead())
+                    return false;
+
+                reader.incrementState();
+
+        }
+
+        return reader.afterMessageRead(TxLocksResponse.class);
+    }
+
+    /** {@inheritDoc} */
+    @Override public byte directType() {
+        return -23;
+    }
+
+    /** {@inheritDoc} */
+    @Override public byte fieldsCount() {
+        return 7;
+    }
+
+    /** {@inheritDoc} */
+    @Override public void onAckReceived() {
+        // No-op.
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java b/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java
index 6958b9e..ed8e100 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java
@@ -215,6 +215,7 @@ import org.apache.ignite.spi.IgniteSpi;
 import org.apache.ignite.spi.IgniteSpiException;
 import org.apache.ignite.spi.discovery.DiscoverySpi;
 import org.apache.ignite.spi.discovery.DiscoverySpiOrderSupport;
+import org.apache.ignite.transactions.TransactionDeadlockException;
 import org.apache.ignite.transactions.TransactionHeuristicException;
 import org.apache.ignite.transactions.TransactionOptimisticException;
 import org.apache.ignite.transactions.TransactionRollbackException;
@@ -808,6 +809,9 @@ public abstract class IgniteUtils {
 
         m.put(IgniteTxTimeoutCheckedException.class, new C1<IgniteCheckedException, IgniteException>() {
             @Override public IgniteException apply(IgniteCheckedException e) {
+                if (e.getCause() instanceof TransactionDeadlockException)
+                    return new TransactionTimeoutException(e.getMessage(), e.getCause());
+
                 return new TransactionTimeoutException(e.getMessage(), e);
             }
         });

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/main/java/org/apache/ignite/transactions/Transaction.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/transactions/Transaction.java b/modules/core/src/main/java/org/apache/ignite/transactions/Transaction.java
index 6c4e894..e2e7100 100644
--- a/modules/core/src/main/java/org/apache/ignite/transactions/Transaction.java
+++ b/modules/core/src/main/java/org/apache/ignite/transactions/Transaction.java
@@ -25,7 +25,7 @@ import org.apache.ignite.lang.IgniteAsyncSupported;
 import org.apache.ignite.lang.IgniteUuid;
 
 /**
- * Grid cache transaction. Cache transactions have a default 2PC (two-phase-commit) behavior and
+ * Ignite cache transaction. Cache transactions have a default 2PC (two-phase-commit) behavior and
  * can be plugged into ongoing {@code JTA} transaction by properly implementing
  * {@ignitelink org.apache.ignite.cache.jta.CacheTmLookup}
  * interface. Cache transactions can also be started explicitly directly from {@link IgniteTransactions} API
@@ -96,17 +96,19 @@ import org.apache.ignite.lang.IgniteUuid;
  * <h1 class="header">Usage</h1>
  * You can use cache transactions as follows:
  * <pre name="code" class="java">
- * Cache&lt;String, Integer&gt; cache = Ignition.ignite().cache();
+ * Ignite ignite = Ignition.ignite();
  *
- * try (GridCacheTx tx = cache.txStart()) {
+ * IgniteCache&lt;String, Integer&gt; cache = ignite.cache(cacheName);
+ *
+ * try (Transaction tx = ignite.transactions().txStart()) {
  *     // Perform transactional operations.
  *     Integer v1 = cache.get("k1");
  *
  *     // Check if v1 satisfies some condition before doing a put.
  *     if (v1 != null && v1 > 0)
- *         cache.putx("k1", 2);
+ *         cache.put("k1", 2);
  *
- *     cache.removex("k2");
+ *     cache.remove("k2");
  *
  *     // Commit the transaction.
  *     tx.commit();
@@ -188,7 +190,7 @@ public interface Transaction extends AutoCloseable, IgniteAsyncSupport {
 
     /**
      * Gets timeout value in milliseconds for this transaction. If transaction times
-     * out prior to it's completion, {@link org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException} will be thrown.
+     * out prior to it's completion, {@link org.apache.ignite.transactions.TransactionTimeoutException} will be thrown.
      *
      * @return Transaction timeout value.
      */
@@ -225,6 +227,11 @@ public interface Transaction extends AutoCloseable, IgniteAsyncSupport {
      * Commits this transaction by initiating {@code two-phase-commit} process.
      *
      * @throws IgniteException If commit failed.
+     * @throws TransactionTimeoutException If transaction is timed out.
+     * @throws TransactionRollbackException If transaction is automatically rolled back.
+     * @throws TransactionOptimisticException If transaction concurrency is {@link TransactionConcurrency#OPTIMISTIC}
+     * and commit is optimistically failed.
+     * @throws TransactionHeuristicException If transaction has entered an unknown state.
      */
     @IgniteAsyncSupported
     public void commit() throws IgniteException;

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/main/java/org/apache/ignite/transactions/TransactionDeadlockException.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/transactions/TransactionDeadlockException.java b/modules/core/src/main/java/org/apache/ignite/transactions/TransactionDeadlockException.java
new file mode 100644
index 0000000..4dc13fe
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/transactions/TransactionDeadlockException.java
@@ -0,0 +1,42 @@
+/*
+ * 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.transactions;
+
+import org.apache.ignite.IgniteException;
+
+/**
+ * Transaction deadlock exception.
+ * <p>
+ * This exception can be thrown from any cache method that modifies or reads data within transaction
+ * (explicit or implicit) with timeout in case when deadlock detection is enabled (enabled by default).
+ * <p>
+ * Usually this exception is cause for {@link TransactionTimeoutException}.
+ */
+public class TransactionDeadlockException extends IgniteException {
+    /** Serial version UID. */
+    private static final long serialVersionUID = 0L;
+
+    /**
+     * Creates new deadlock exception with given error message.
+     *
+     * @param msg Error message.
+     */
+    public TransactionDeadlockException(String msg) {
+        super(msg);
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/main/java/org/apache/ignite/transactions/TransactionTimeoutException.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/transactions/TransactionTimeoutException.java b/modules/core/src/main/java/org/apache/ignite/transactions/TransactionTimeoutException.java
index 970672e..ab76cf7 100644
--- a/modules/core/src/main/java/org/apache/ignite/transactions/TransactionTimeoutException.java
+++ b/modules/core/src/main/java/org/apache/ignite/transactions/TransactionTimeoutException.java
@@ -20,7 +20,8 @@ package org.apache.ignite.transactions;
 import org.apache.ignite.IgniteException;
 
 /**
- * Exception thrown whenever grid transactions time out.
+ * Exception thrown whenever transactions time out. Because transaction can be timed out due to a deadlock
+ * this exception can contain {@link TransactionDeadlockException} as cause.
  */
 public class TransactionTimeoutException extends IgniteException {
     /** */
@@ -39,7 +40,7 @@ public class TransactionTimeoutException extends IgniteException {
      * Creates new timeout exception with given error message and optional nested exception.
      *
      * @param msg Error message.
-     * @param cause Optional nested exception (can be <tt>null</tt>).
+     * @param cause Optional nested exception (can be {@code null}).
      */
     public TransactionTimeoutException(String msg, Throwable cause) {
         super(msg, cause);

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/DepthFirstSearchTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/DepthFirstSearchTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/DepthFirstSearchTest.java
new file mode 100644
index 0000000..20467c2
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/transactions/DepthFirstSearchTest.java
@@ -0,0 +1,252 @@
+/*
+ * 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.transactions;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import junit.framework.TestCase;
+import org.apache.ignite.internal.processors.cache.version.GridCacheVersion;
+import org.apache.ignite.internal.util.typedef.F;
+
+import static org.apache.ignite.internal.processors.cache.transactions.TxDeadlockDetection.findCycle;
+
+/**
+ * DFS test for search cycle in wait-for-graph.
+ */
+public class DepthFirstSearchTest extends TestCase {
+    /** Tx 1. */
+    private static final GridCacheVersion T1 = new GridCacheVersion(1, 0, 0, 0);
+
+    /** Tx 2. */
+    private static final GridCacheVersion T2 = new GridCacheVersion(2, 0, 0, 0);
+
+    /** Tx 3. */
+    private static final GridCacheVersion T3 = new GridCacheVersion(3, 0, 0, 0);
+
+    /** Tx 4. */
+    private static final GridCacheVersion T4 = new GridCacheVersion(4, 0, 0, 0);
+
+    /** Tx 5. */
+    private static final GridCacheVersion T5 = new GridCacheVersion(5, 0, 0, 0);
+
+    /** Tx 6. */
+    private static final GridCacheVersion T6 = new GridCacheVersion(6, 0, 0, 0);
+
+    /** All transactions. */
+    private static final List<GridCacheVersion> ALL = Arrays.asList(T1, T2, T3, T4, T5, T6);
+
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testNoCycle() throws Exception {
+        assertNull(findCycle(Collections.<GridCacheVersion, Set<GridCacheVersion>>emptyMap(), T1));
+
+        HashMap<GridCacheVersion, Set<GridCacheVersion>> wfg;
+
+        wfg = new HashMap<GridCacheVersion, Set<GridCacheVersion>>() {{
+            put(T1, null);
+        }};
+
+        assertAllNull(wfg);
+
+        wfg = new HashMap<GridCacheVersion, Set<GridCacheVersion>>() {{
+            put(T1, null);
+            put(T2, null);
+        }};
+
+        assertAllNull(wfg);
+
+        wfg = new HashMap<GridCacheVersion, Set<GridCacheVersion>>() {{
+            put(T1, Collections.singleton(T2));
+            put(T3, Collections.singleton(T4));
+        }};
+
+        assertAllNull(wfg);
+
+        wfg = new HashMap<GridCacheVersion, Set<GridCacheVersion>>() {{
+            put(T1, new HashSet<GridCacheVersion>(){{add(T2);}});
+            put(T2, new HashSet<GridCacheVersion>(){{add(T3);}});
+            put(T4, new HashSet<GridCacheVersion>(){{add(T1); add(T3);}});
+        }};
+
+        assertAllNull(wfg);
+
+        wfg = new HashMap<GridCacheVersion, Set<GridCacheVersion>>() {{
+            put(T1, new HashSet<GridCacheVersion>(){{add(T2);}});
+            put(T3, new HashSet<GridCacheVersion>(){{add(T4);}});
+            put(T4, new HashSet<GridCacheVersion>(){{add(T1);}});
+        }};
+
+        assertAllNull(wfg);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testFindCycle2() throws Exception {
+        Map<GridCacheVersion, Set<GridCacheVersion>> wfg = new HashMap<GridCacheVersion, Set<GridCacheVersion>>() {{
+            put(T1, Collections.singleton(T2));
+            put(T2, Collections.singleton(T1));
+        }};
+
+        assertEquals(F.asList(T2, T1, T2), findCycle(wfg, T1));
+        assertEquals(F.asList(T1, T2, T1), findCycle(wfg, T2));
+        assertAllNull(wfg, T1, T2);
+
+        wfg = new HashMap<GridCacheVersion, Set<GridCacheVersion>>() {{
+            put(T1, Collections.singleton(T2));
+            put(T2, Collections.singleton(T3));
+            put(T3, asLinkedHashSet(T2, T4));
+        }};
+
+        assertEquals(F.asList(T3, T2, T3), findCycle(wfg, T1));
+        assertEquals(F.asList(T3, T2, T3), findCycle(wfg, T2));
+        assertEquals(F.asList(T2, T3, T2), findCycle(wfg, T3));
+        assertAllNull(wfg, T1, T2, T3);
+
+        wfg = new HashMap<GridCacheVersion, Set<GridCacheVersion>>() {{
+            put(T1, Collections.singleton(T2));
+            put(T2, asLinkedHashSet(T3, T1));
+            put(T3, Collections.singleton(T2));
+        }};
+
+        assertEquals(F.asList(T2, T1, T2), findCycle(wfg, T1));
+        assertEquals(F.asList(T1, T2, T1), findCycle(wfg, T2));
+        assertEquals(F.asList(T2, T3, T2), findCycle(wfg, T3));
+        assertAllNull(wfg, T1, T2, T3);
+
+        wfg = new HashMap<GridCacheVersion, Set<GridCacheVersion>>() {{
+            put(T1, Collections.singleton(T2));
+            put(T2, asLinkedHashSet(T1, T3));
+            put(T3, Collections.singleton(T4));
+            put(T4, Collections.singleton(T3));
+        }};
+
+        assertEquals(F.asList(T2, T1, T2), findCycle(wfg, T1));
+        assertEquals(F.asList(T4, T3, T4), findCycle(wfg, T2));
+        assertEquals(F.asList(T4, T3, T4), findCycle(wfg, T3));
+        assertEquals(F.asList(T3, T4, T3), findCycle(wfg, T4));
+        assertAllNull(wfg, T1, T2, T3, T4);
+
+        wfg = new HashMap<GridCacheVersion, Set<GridCacheVersion>>() {{
+            put(T1, Collections.singleton(T2));
+            put(T2, Collections.singleton(T3));
+            put(T3, Collections.singleton(T4));
+            put(T4, Collections.singleton(T5));
+            put(T5, Collections.singleton(T6));
+            put(T6, Collections.singleton(T5));
+        }};
+
+        assertEquals(F.asList(T6, T5, T6), findCycle(wfg, T1));
+        assertEquals(F.asList(T6, T5, T6), findCycle(wfg, T2));
+        assertEquals(F.asList(T6, T5, T6), findCycle(wfg, T3));
+        assertEquals(F.asList(T6, T5, T6), findCycle(wfg, T4));
+        assertEquals(F.asList(T6, T5, T6), findCycle(wfg, T5));
+        assertEquals(F.asList(T5, T6, T5), findCycle(wfg, T6));
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testFindCycle3() throws Exception {
+        Map<GridCacheVersion, Set<GridCacheVersion>> wfg = new HashMap<GridCacheVersion, Set<GridCacheVersion>>() {{
+            put(T1, Collections.singleton(T2));
+            put(T2, Collections.singleton(T3));
+            put(T3, Collections.singleton(T1));
+        }};
+
+        assertEquals(F.asList(T3, T2, T1, T3), findCycle(wfg, T1));
+        assertEquals(F.asList(T1, T3, T2, T1), findCycle(wfg, T2));
+        assertEquals(F.asList(T2, T1, T3, T2), findCycle(wfg, T3));
+        assertAllNull(wfg, T1, T2, T3);
+
+        wfg = new HashMap<GridCacheVersion, Set<GridCacheVersion>>() {{
+            put(T1, Collections.singleton(T2));
+            put(T2, Collections.singleton(T3));
+            put(T3, Collections.singleton(T4));
+            put(T4, asLinkedHashSet(T2, T5));
+        }};
+
+        assertEquals(F.asList(T4, T3, T2, T4), findCycle(wfg, T1));
+        assertEquals(F.asList(T4, T3, T2, T4), findCycle(wfg, T2));
+        assertEquals(F.asList(T2, T4, T3, T2), findCycle(wfg, T3));
+        assertEquals(F.asList(T3, T2, T4, T3), findCycle(wfg, T4));
+        assertAllNull(wfg, T1, T2, T3, T4);
+
+        wfg = new HashMap<GridCacheVersion, Set<GridCacheVersion>>() {{
+            put(T1, Collections.singleton(T2));
+            put(T2, asLinkedHashSet(T3, T4));
+            put(T3, Collections.singleton(T1));
+            put(T4, Collections.singleton(T5));
+            put(T5, Collections.singleton(T6));
+            put(T6, Collections.singleton(T4));
+
+        }};
+
+        assertEquals(F.asList(T6, T5, T4, T6), findCycle(wfg, T1));
+        assertEquals(F.asList(T6, T5, T4, T6), findCycle(wfg, T2));
+        assertEquals(F.asList(T2, T1, T3, T2), findCycle(wfg, T3));
+
+        wfg = new HashMap<GridCacheVersion, Set<GridCacheVersion>>() {{
+            put(T1, Collections.singleton(T2));
+            put(T2, Collections.singleton(T3));
+            put(T3, Collections.singleton(T4));
+            put(T4, Collections.singleton(T5));
+            put(T5, Collections.singleton(T6));
+            put(T6, Collections.singleton(T4));
+        }};
+
+        assertEquals(F.asList(T6, T5, T4, T6), findCycle(wfg, T1));
+        assertEquals(F.asList(T6, T5, T4, T6), findCycle(wfg, T2));
+        assertEquals(F.asList(T6, T5, T4, T6), findCycle(wfg, T3));
+        assertEquals(F.asList(T6, T5, T4, T6), findCycle(wfg, T4));
+        assertEquals(F.asList(T4, T6, T5, T4), findCycle(wfg, T5));
+        assertEquals(F.asList(T5, T4, T6, T5), findCycle(wfg, T6));
+
+    }
+
+    /**
+     * @param wfg Wait-for-graph.
+     */
+    private static void assertAllNull(Map<GridCacheVersion, Set<GridCacheVersion>> wfg, GridCacheVersion... ignore) {
+        Set<GridCacheVersion> excl = F.asSet(ignore);
+
+        for (GridCacheVersion tx : ALL) {
+            if (!excl.contains(tx))
+                assertNull(tx + " could not be part of cycle", findCycle(wfg, tx));
+        }
+    }
+
+    /**
+     * @param txs Txs.
+     */
+    private static Set<GridCacheVersion> asLinkedHashSet(GridCacheVersion... txs) {
+        Set<GridCacheVersion> set = new LinkedHashSet<>();
+
+        Collections.addAll(set, txs);
+
+        return set;
+    }
+}
\ No newline at end of file


[04/10] ignite git commit: IGNITE-3060 Discovery: optimize resource usage for client connections (cherry picked from commits 7b0edfb, 8324233)

Posted by vo...@apache.org.
IGNITE-3060 Discovery: optimize resource usage for client connections
(cherry picked from commits 7b0edfb, 8324233)


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/f0daea95
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/f0daea95
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/f0daea95

Branch: refs/heads/ignite-1786
Commit: f0daea95dfbb1d6aca3a722773001c5ec0e0f5d6
Parents: fc0dd9a
Author: sboikov <sb...@gridgain.com>
Authored: Fri Apr 29 09:57:10 2016 +0300
Committer: sboikov <sb...@gridgain.com>
Committed: Wed May 4 09:12:24 2016 +0300

----------------------------------------------------------------------
 .../ignite/internal/IgniteComponentType.java    |   4 +-
 .../continuous/CacheContinuousQueryHandler.java |   9 +
 .../ignite/spi/discovery/tcp/ServerImpl.java    | 214 ++++++++++++-------
 .../spi/discovery/tcp/TcpDiscoverySpi.java      |   5 +-
 .../TcpDiscoveryClientHeartbeatMessage.java     |   1 +
 .../TcpDiscoveryClientReconnectMessage.java     |  16 ++
 .../tcp/TcpClientDiscoverySpiSelfTest.java      |  44 +++-
 7 files changed, 202 insertions(+), 91 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/f0daea95/modules/core/src/main/java/org/apache/ignite/internal/IgniteComponentType.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/IgniteComponentType.java b/modules/core/src/main/java/org/apache/ignite/internal/IgniteComponentType.java
index 01872b6..76e495f 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/IgniteComponentType.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/IgniteComponentType.java
@@ -285,7 +285,7 @@ public enum IgniteComponentType {
                 return (T)ctor.newInstance(ctx);
             }
         }
-        catch (Exception e) {
+        catch (Throwable e) {
             throw componentException(e);
         }
     }
@@ -309,7 +309,7 @@ public enum IgniteComponentType {
      * @param err Creation error.
      * @return Component creation exception.
      */
-    private IgniteCheckedException componentException(Exception err) {
+    private IgniteCheckedException componentException(Throwable err) {
         return new IgniteCheckedException("Failed to create Ignite component (consider adding " + module +
             " module to classpath) [component=" + this + ", cls=" + clsName + ']', err);
     }

http://git-wip-us.apache.org/repos/asf/ignite/blob/f0daea95/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/query/continuous/CacheContinuousQueryHandler.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/query/continuous/CacheContinuousQueryHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/query/continuous/CacheContinuousQueryHandler.java
index 9ae2972..3b77d48 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/query/continuous/CacheContinuousQueryHandler.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/query/continuous/CacheContinuousQueryHandler.java
@@ -638,6 +638,15 @@ public class CacheContinuousQueryHandler<K, V> implements GridContinuousHandler
         Collection<CacheContinuousQueryEntry> entries) {
         final GridCacheContext cctx = cacheContext(ctx);
 
+        if (cctx == null) {
+            IgniteLogger log = ctx.log(CacheContinuousQueryHandler.class);
+
+            if (log.isDebugEnabled())
+                log.debug("Failed to notify callback, cache is not found: " + cacheId);
+
+            return;
+        }
+
         final Collection<CacheEntryEvent<? extends K, ? extends V>> entries0 = new ArrayList<>(entries.size());
 
         for (CacheContinuousQueryEntry e : entries) {

http://git-wip-us.apache.org/repos/asf/ignite/blob/f0daea95/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/ServerImpl.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/ServerImpl.java b/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/ServerImpl.java
index 05bb1e6..84400ed 100644
--- a/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/ServerImpl.java
+++ b/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/ServerImpl.java
@@ -162,7 +162,7 @@ import static org.apache.ignite.spi.discovery.tcp.messages.TcpDiscoveryStatusChe
  */
 class ServerImpl extends TcpDiscoveryImpl {
     /** */
-    private static final int ENSURED_MSG_HIST_SIZE = getInteger(IGNITE_DISCOVERY_HISTORY_SIZE, 1024 * 10);
+    private static final int ENSURED_MSG_HIST_SIZE = getInteger(IGNITE_DISCOVERY_HISTORY_SIZE, 1024);
 
     /** */
     private static final IgniteProductVersion CUSTOM_MSG_ALLOW_JOINING_FOR_VERIFIED_SINCE =
@@ -1412,6 +1412,8 @@ class ServerImpl extends TcpDiscoveryImpl {
                 ", topSize=" + ring.allNodes().size() +
                 ", leavingNodesSize=" + leavingNodesSize + ", failedNodesSize=" + failedNodesSize +
                 ", msgWorker.queue.size=" + (msgWorker != null ? msgWorker.queueSize() : "N/A") +
+                ", clients=" + ring.clientNodes().size() +
+                ", clientWorkers=" + clientMsgWorkers.size() +
                 ", lastUpdate=" + (locNode != null ? U.format(locNode.lastUpdateTime()) : "N/A") +
                 ", heapFree=" + runtime.freeMemory() / (1024 * 1024) +
                 "M, heapTotal=" + runtime.maxMemory() / (1024 * 1024) + "M]");
@@ -2114,7 +2116,7 @@ class ServerImpl extends TcpDiscoveryImpl {
     /**
      * Message worker thread for messages processing.
      */
-    private class RingMessageWorker extends MessageWorkerAdapter {
+    private class RingMessageWorker extends MessageWorkerAdapter<TcpDiscoveryAbstractMessage> {
         /** Next node. */
         @SuppressWarnings({"FieldAccessedSynchronizedAndUnsynchronized"})
         private TcpDiscoveryNode next;
@@ -2172,6 +2174,32 @@ class ServerImpl extends TcpDiscoveryImpl {
             initConnectionCheckFrequency();
         }
 
+        /**
+         * Adds message to queue.
+         *
+         * @param msg Message to add.
+         */
+        void addMessage(TcpDiscoveryAbstractMessage msg) {
+            if ((msg instanceof TcpDiscoveryStatusCheckMessage ||
+                msg instanceof TcpDiscoveryJoinRequestMessage ||
+                msg instanceof TcpDiscoveryCustomEventMessage ||
+                msg instanceof TcpDiscoveryClientReconnectMessage) &&
+                queue.contains(msg)) {
+                if (log.isDebugEnabled())
+                    log.debug("Ignoring duplicate message: " + msg);
+
+                return;
+            }
+
+            if (msg.highPriority())
+                queue.addFirst(msg);
+            else
+                queue.add(msg);
+
+            if (log.isDebugEnabled())
+                log.debug("Message has been added to queue: " + msg);
+        }
+
         /** {@inheritDoc} */
         @Override protected void body() throws InterruptedException {
             try {
@@ -2277,9 +2305,6 @@ class ServerImpl extends TcpDiscoveryImpl {
             else if (msg instanceof TcpDiscoveryNodeFailedMessage)
                 processNodeFailedMessage((TcpDiscoveryNodeFailedMessage)msg);
 
-            else if (msg instanceof TcpDiscoveryClientHeartbeatMessage)
-                processClientHeartbeatMessage((TcpDiscoveryClientHeartbeatMessage)msg);
-
             else if (msg instanceof TcpDiscoveryHeartbeatMessage)
                 processHeartbeatMessage((TcpDiscoveryHeartbeatMessage)msg);
 
@@ -2333,28 +2358,44 @@ class ServerImpl extends TcpDiscoveryImpl {
          */
         private void sendMessageToClients(TcpDiscoveryAbstractMessage msg) {
             if (redirectToClients(msg)) {
-                byte[] marshalledMsg = null;
+                byte[] msgBytes = null;
 
                 for (ClientMessageWorker clientMsgWorker : clientMsgWorkers.values()) {
-                    // Send a clone to client to avoid ConcurrentModificationException
-                    TcpDiscoveryAbstractMessage msgClone;
-
-                    try {
-                        if (marshalledMsg == null)
-                            marshalledMsg = spi.marsh.marshal(msg);
+                    if (msgBytes == null) {
+                        try {
+                            msgBytes = spi.marsh.marshal(msg);
+                        }
+                        catch (IgniteCheckedException e) {
+                            U.error(log, "Failed to marshal message: " + msg, e);
 
-                        msgClone = spi.marsh.unmarshal(marshalledMsg,
-                            U.resolveClassLoader(spi.ignite().configuration()));
+                            break;
+                        }
                     }
-                    catch (IgniteCheckedException e) {
-                        U.error(log, "Failed to marshal message: " + msg, e);
 
-                        msgClone = msg;
-                    }
+                    TcpDiscoveryAbstractMessage msg0 = msg;
+                    byte[] msgBytes0 = msgBytes;
 
-                    prepareNodeAddedMessage(msgClone, clientMsgWorker.clientNodeId, null, null, null);
+                    if (msg instanceof TcpDiscoveryNodeAddedMessage) {
+                        TcpDiscoveryNodeAddedMessage nodeAddedMsg = (TcpDiscoveryNodeAddedMessage)msg;
+
+                        TcpDiscoveryNode node = nodeAddedMsg.node();
+
+                        if (clientMsgWorker.clientNodeId.equals(node.id())) {
+                            try {
+                                msg0 = spi.marsh.unmarshal(msgBytes,
+                                    U.resolveClassLoader(spi.ignite().configuration()));
+
+                                prepareNodeAddedMessage(msg0, clientMsgWorker.clientNodeId, null, null, null);
 
-                    clientMsgWorker.addMessage(msgClone);
+                                msgBytes0 = null;
+                            }
+                            catch (IgniteCheckedException e) {
+                                U.error(log, "Failed to create message copy: " + msg, e);
+                            }
+                        }
+                    }
+
+                    clientMsgWorker.addMessage(msg0, msgBytes0);
                 }
             }
         }
@@ -4528,22 +4569,6 @@ class ServerImpl extends TcpDiscoveryImpl {
         }
 
         /**
-         * Processes client heartbeat message.
-         *
-         * @param msg Heartbeat message.
-         */
-        private void processClientHeartbeatMessage(TcpDiscoveryClientHeartbeatMessage msg) {
-            assert msg.client();
-
-            ClientMessageWorker wrk = clientMsgWorkers.get(msg.creatorNodeId());
-
-            if (wrk != null)
-                wrk.metrics(msg.metrics());
-            else if (log.isDebugEnabled())
-                log.debug("Received heartbeat message from unknown client node: " + msg);
-        }
-
-        /**
          * @param nodeId Node ID.
          * @param metrics Metrics.
          * @param cacheMetrics Cache metrics.
@@ -5448,7 +5473,12 @@ class ServerImpl extends TcpDiscoveryImpl {
                             continue;
                         }
 
-                        msgWorker.addMessage(msg);
+                        TcpDiscoveryClientHeartbeatMessage heartbeatMsg = null;
+
+                        if (msg instanceof TcpDiscoveryClientHeartbeatMessage)
+                            heartbeatMsg = (TcpDiscoveryClientHeartbeatMessage)msg;
+                        else
+                            msgWorker.addMessage(msg);
 
                         // Send receipt back.
                         if (clientMsgWrk != null) {
@@ -5460,6 +5490,9 @@ class ServerImpl extends TcpDiscoveryImpl {
                         }
                         else
                             spi.writeToSocket(msg, sock, RES_OK, socketTimeout);
+
+                        if (heartbeatMsg != null)
+                            processClientHeartbeatMessage(heartbeatMsg);
                     }
                     catch (IgniteCheckedException e) {
                         if (log.isDebugEnabled())
@@ -5527,6 +5560,22 @@ class ServerImpl extends TcpDiscoveryImpl {
         }
 
         /**
+         * Processes client heartbeat message.
+         *
+         * @param msg Heartbeat message.
+         */
+        private void processClientHeartbeatMessage(TcpDiscoveryClientHeartbeatMessage msg) {
+            assert msg.client();
+
+            ClientMessageWorker wrk = clientMsgWorkers.get(msg.creatorNodeId());
+
+            if (wrk != null)
+                wrk.metrics(msg.metrics());
+            else if (log.isDebugEnabled())
+                log.debug("Received heartbeat message from unknown client node: " + msg);
+        }
+
+        /**
          * @param msg Join request message.
          * @param clientMsgWrk Client message worker to start.
          * @return Whether connection was successful.
@@ -5653,16 +5702,13 @@ class ServerImpl extends TcpDiscoveryImpl {
 
     /**
      */
-    private class ClientMessageWorker extends MessageWorkerAdapter {
+    private class ClientMessageWorker extends MessageWorkerAdapter<T2<TcpDiscoveryAbstractMessage, byte[]>> {
         /** Node ID. */
         private final UUID clientNodeId;
 
         /** Socket. */
         private final Socket sock;
 
-        /** Output stream. */
-        private final OutputStream out;
-
         /** Current client metrics. */
         private volatile ClusterMetrics metrics;
 
@@ -5681,8 +5727,6 @@ class ServerImpl extends TcpDiscoveryImpl {
 
             this.sock = sock;
             this.clientNodeId = clientNodeId;
-
-            out = new BufferedOutputStream(sock.getOutputStream(), sock.getSendBufferSize());
         }
 
         /**
@@ -5706,11 +5750,43 @@ class ServerImpl extends TcpDiscoveryImpl {
             this.metrics = metrics;
         }
 
+        /**
+         * @param msg Message.
+         */
+        public void addMessage(TcpDiscoveryAbstractMessage msg) {
+            addMessage(msg, null);
+        }
+
+        /**
+         * @param msg Message.
+         * @param msgBytes Optional message bytes.
+         */
+        public void addMessage(TcpDiscoveryAbstractMessage msg, @Nullable byte[] msgBytes) {
+            T2 t = new T2<>(msg, msgBytes);
+
+            if (msg.highPriority())
+                queue.addFirst(t);
+            else
+                queue.add(t);
+
+            if (log.isDebugEnabled())
+                log.debug("Message has been added to client queue: " + msg);
+        }
+
         /** {@inheritDoc} */
-        @Override protected void processMessage(TcpDiscoveryAbstractMessage msg) {
+        @Override protected void processMessage(T2<TcpDiscoveryAbstractMessage, byte[]> msgT) {
+            boolean success = false;
+
+            TcpDiscoveryAbstractMessage msg = msgT.get1();
+
             try {
                 assert msg.verified() : msg;
 
+                byte[] msgBytes = msgT.get2();
+
+                if (msgBytes == null)
+                    msgBytes = spi.marsh.marshal(msg);
+
                 if (msg instanceof TcpDiscoveryClientAckResponse) {
                     if (clientVer == null) {
                         ClusterNode node = spi.getNode(clientNodeId);
@@ -5729,7 +5805,7 @@ class ServerImpl extends TcpDiscoveryImpl {
                             log.debug("Sending message ack to client [sock=" + sock + ", locNodeId="
                                 + getLocalNodeId() + ", rmtNodeId=" + clientNodeId + ", msg=" + msg + ']');
 
-                        spi.writeToSocket(sock, out, msg, spi.failureDetectionTimeoutEnabled() ?
+                        spi.writeToSocket(sock, msg, msgBytes, spi.failureDetectionTimeoutEnabled() ?
                             spi.failureDetectionTimeout() : spi.getSocketTimeout());
                     }
                 }
@@ -5740,9 +5816,11 @@ class ServerImpl extends TcpDiscoveryImpl {
 
                     assert topologyInitialized(msg) : msg;
 
-                    spi.writeToSocket(sock, out, msg, spi.failureDetectionTimeoutEnabled() ?
+                    spi.writeToSocket(sock, msg, msgBytes, spi.failureDetectionTimeoutEnabled() ?
                         spi.failureDetectionTimeout() : spi.getSocketTimeout());
                 }
+
+                success = true;
             }
             catch (IgniteCheckedException | IOException e) {
                 if (log.isDebugEnabled())
@@ -5751,12 +5829,15 @@ class ServerImpl extends TcpDiscoveryImpl {
 
                 onException("Client connection failed [sock=" + sock + ", locNodeId="
                     + getLocalNodeId() + ", rmtNodeId=" + clientNodeId + ", msg=" + msg + ']', e);
+            }
+            finally {
+                if (!success) {
+                    clientMsgWorkers.remove(clientNodeId, this);
 
-                clientMsgWorkers.remove(clientNodeId, this);
-
-                U.interrupt(this);
+                    U.interrupt(this);
 
-                U.closeQuiet(sock);
+                    U.closeQuiet(sock);
+                }
             }
         }
 
@@ -5846,9 +5927,9 @@ class ServerImpl extends TcpDiscoveryImpl {
     /**
      * Base class for message workers.
      */
-    protected abstract class MessageWorkerAdapter extends IgniteSpiThread {
+    protected abstract class MessageWorkerAdapter<T> extends IgniteSpiThread {
         /** Message queue. */
-        private final BlockingDeque<TcpDiscoveryAbstractMessage> queue = new LinkedBlockingDeque<>();
+        protected final BlockingDeque<T> queue = new LinkedBlockingDeque<>();
 
         /** Backed interrupted flag. */
         private volatile boolean interrupted;
@@ -5874,7 +5955,7 @@ class ServerImpl extends TcpDiscoveryImpl {
                 log.debug("Message worker started [locNodeId=" + getConfiguredNodeId() + ']');
 
             while (!isInterrupted()) {
-                TcpDiscoveryAbstractMessage msg = queue.poll(pollingTimeout, TimeUnit.MILLISECONDS);
+                T msg = queue.poll(pollingTimeout, TimeUnit.MILLISECONDS);
 
                 if (msg == null)
                     noMessageLoop();
@@ -5903,34 +5984,9 @@ class ServerImpl extends TcpDiscoveryImpl {
         }
 
         /**
-         * Adds message to queue.
-         *
-         * @param msg Message to add.
-         */
-        void addMessage(TcpDiscoveryAbstractMessage msg) {
-            if ((msg instanceof TcpDiscoveryStatusCheckMessage ||
-                msg instanceof TcpDiscoveryJoinRequestMessage ||
-                msg instanceof TcpDiscoveryCustomEventMessage) &&
-                queue.contains(msg)) {
-                if (log.isDebugEnabled())
-                    log.debug("Ignoring duplicate message: " + msg);
-
-                return;
-            }
-
-            if (msg.highPriority())
-                queue.addFirst(msg);
-            else
-                queue.add(msg);
-
-            if (log.isDebugEnabled())
-                log.debug("Message has been added to queue: " + msg);
-        }
-
-        /**
          * @param msg Message.
          */
-        protected abstract void processMessage(TcpDiscoveryAbstractMessage msg);
+        protected abstract void processMessage(T msg);
 
         /**
          * Called when there is no message to process giving ability to perform other activity.

http://git-wip-us.apache.org/repos/asf/ignite/blob/f0daea95/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/TcpDiscoverySpi.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/TcpDiscoverySpi.java b/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/TcpDiscoverySpi.java
index 4351c64..c135b83 100644
--- a/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/TcpDiscoverySpi.java
+++ b/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/TcpDiscoverySpi.java
@@ -1267,7 +1267,7 @@ public class TcpDiscoverySpi extends IgniteSpiAdapter implements DiscoverySpi, T
 
         sock.connect(resolved, (int)timeoutHelper.nextTimeoutChunk(sockTimeout));
 
-        writeToSocket(sock, U.IGNITE_HEADER, timeoutHelper.nextTimeoutChunk(sockTimeout));
+        writeToSocket(sock, null, U.IGNITE_HEADER, timeoutHelper.nextTimeoutChunk(sockTimeout));
 
         return sock;
     }
@@ -1297,12 +1297,13 @@ public class TcpDiscoverySpi extends IgniteSpiAdapter implements DiscoverySpi, T
      * Writes message to the socket.
      *
      * @param sock Socket.
+     * @param msg Message.
      * @param data Raw data to write.
      * @param timeout Socket write timeout.
      * @throws IOException If IO failed or write timed out.
      */
     @SuppressWarnings("ThrowFromFinallyBlock")
-    private void writeToSocket(Socket sock, byte[] data, long timeout) throws IOException {
+    protected void writeToSocket(Socket sock, TcpDiscoveryAbstractMessage msg, byte[] data, long timeout) throws IOException {
         assert sock != null;
         assert data != null;
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/f0daea95/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/messages/TcpDiscoveryClientHeartbeatMessage.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/messages/TcpDiscoveryClientHeartbeatMessage.java b/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/messages/TcpDiscoveryClientHeartbeatMessage.java
index 37b578c..3993de0 100644
--- a/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/messages/TcpDiscoveryClientHeartbeatMessage.java
+++ b/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/messages/TcpDiscoveryClientHeartbeatMessage.java
@@ -38,6 +38,7 @@ public class TcpDiscoveryClientHeartbeatMessage extends TcpDiscoveryAbstractMess
      * Constructor.
      *
      * @param creatorNodeId Creator node.
+     * @param metrics Metrics.
      */
     public TcpDiscoveryClientHeartbeatMessage(UUID creatorNodeId, ClusterMetrics metrics) {
         super(creatorNodeId);

http://git-wip-us.apache.org/repos/asf/ignite/blob/f0daea95/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/messages/TcpDiscoveryClientReconnectMessage.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/messages/TcpDiscoveryClientReconnectMessage.java b/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/messages/TcpDiscoveryClientReconnectMessage.java
index 7c0cd5d..7cce78b 100644
--- a/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/messages/TcpDiscoveryClientReconnectMessage.java
+++ b/modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/messages/TcpDiscoveryClientReconnectMessage.java
@@ -20,6 +20,7 @@ package org.apache.ignite.spi.discovery.tcp.messages;
 import java.util.Collection;
 import java.util.UUID;
 import org.apache.ignite.internal.util.tostring.GridToStringExclude;
+import org.apache.ignite.internal.util.typedef.F;
 import org.apache.ignite.internal.util.typedef.internal.S;
 import org.apache.ignite.lang.IgniteUuid;
 
@@ -96,6 +97,21 @@ public class TcpDiscoveryClientReconnectMessage extends TcpDiscoveryAbstractMess
     }
 
     /** {@inheritDoc} */
+    @Override public boolean equals(Object obj) {
+        // NOTE!
+        // Do not call super. As IDs will differ, but we can ignore this.
+
+        if (!(obj instanceof TcpDiscoveryClientReconnectMessage))
+            return false;
+
+        TcpDiscoveryClientReconnectMessage other = (TcpDiscoveryClientReconnectMessage)obj;
+
+        return F.eq(creatorNodeId(), other.creatorNodeId()) &&
+            F.eq(routerNodeId, other.routerNodeId) &&
+            F.eq(lastMsgId, other.lastMsgId);
+    }
+
+    /** {@inheritDoc} */
     @Override public String toString() {
         return S.toString(TcpDiscoveryClientReconnectMessage.class, this, "super", super.toString());
     }

http://git-wip-us.apache.org/repos/asf/ignite/blob/f0daea95/modules/core/src/test/java/org/apache/ignite/spi/discovery/tcp/TcpClientDiscoverySpiSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/spi/discovery/tcp/TcpClientDiscoverySpiSelfTest.java b/modules/core/src/test/java/org/apache/ignite/spi/discovery/tcp/TcpClientDiscoverySpiSelfTest.java
index e01094c..331b581 100644
--- a/modules/core/src/test/java/org/apache/ignite/spi/discovery/tcp/TcpClientDiscoverySpiSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/spi/discovery/tcp/TcpClientDiscoverySpiSelfTest.java
@@ -1736,8 +1736,7 @@ public class TcpClientDiscoverySpiSelfTest extends GridCommonAbstractTest {
         final AtomicBoolean err = new AtomicBoolean(false);
 
         client.events().localListen(new IgnitePredicate<Event>() {
-            @Override
-            public boolean apply(Event evt) {
+            @Override public boolean apply(Event evt) {
                 if (evt.type() == EVT_CLIENT_NODE_DISCONNECTED) {
                     log.info("Disconnected event.");
 
@@ -2158,17 +2157,49 @@ public class TcpClientDiscoverySpiSelfTest extends GridCommonAbstractTest {
         }
 
         /** {@inheritDoc} */
-        @Override protected void writeToSocket(Socket sock, OutputStream out, TcpDiscoveryAbstractMessage msg,
+        @Override protected void writeToSocket(Socket sock,
+            OutputStream out,
+            TcpDiscoveryAbstractMessage msg,
             long timeout) throws IOException, IgniteCheckedException {
             waitFor(writeLock);
 
+            if (!onMessage(sock, msg))
+                return;
+
+            super.writeToSocket(sock, out, msg, timeout);
+
+            if (afterWrite != null)
+                afterWrite.apply(msg, sock);
+        }
+
+        /** {@inheritDoc} */
+        @Override protected void writeToSocket(Socket sock, TcpDiscoveryAbstractMessage msg, byte[] msgBytes,
+            long timeout) throws IOException {
+            waitFor(writeLock);
+
+            if (!onMessage(sock, msg))
+                return;
+
+            super.writeToSocket(sock, msg, msgBytes, timeout);
+
+            if (afterWrite != null)
+                afterWrite.apply(msg, sock);
+        }
+
+        /**
+         * @param sock Socket.
+         * @param msg Message.
+         * @return {@code False} if should not further process message.
+         * @throws IOException If failed.
+         */
+        private boolean onMessage(Socket sock, TcpDiscoveryAbstractMessage msg) throws IOException {
             boolean fail = false;
 
             if (skipNodeAdded &&
                 (msg instanceof TcpDiscoveryNodeAddedMessage || msg instanceof TcpDiscoveryNodeAddFinishedMessage)) {
                 log.info("Skip message: " + msg);
 
-                return;
+                return false;
             }
 
             if (msg instanceof TcpDiscoveryNodeAddedMessage)
@@ -2184,10 +2215,7 @@ public class TcpClientDiscoverySpiSelfTest extends GridCommonAbstractTest {
                 sock.close();
             }
 
-            super.writeToSocket(sock, out, msg, timeout);
-
-            if (afterWrite != null)
-                afterWrite.apply(msg, sock);
+            return true;
         }
 
         /** {@inheritDoc} */


[09/10] ignite git commit: ignite-2854 Deadlock detection for pessimistic transactions

Posted by vo...@apache.org.
ignite-2854 Deadlock detection for pessimistic transactions


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/0f2b7532
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/0f2b7532
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/0f2b7532

Branch: refs/heads/ignite-1786
Commit: 0f2b7532d47230946b4991ac2211949f9b8d37b6
Parents: f3eb94e
Author: agura <ag...@gridgain.com>
Authored: Fri Apr 8 18:31:40 2016 +0300
Committer: agura <ag...@gridgain.com>
Committed: Wed May 4 15:27:06 2016 +0300

----------------------------------------------------------------------
 .../java/org/apache/ignite/IgniteCache.java     | 197 ++++--
 .../apache/ignite/IgniteSystemProperties.java   |  11 +
 .../org/apache/ignite/internal/GridTopic.java   |   5 +-
 .../communication/GridIoMessageFactory.java     |  24 +
 .../processors/cache/GridCacheMapEntry.java     |  16 +
 .../processors/cache/GridCacheMvcc.java         |   7 +
 .../processors/cache/GridCacheMvccManager.java  |  20 +-
 .../cache/GridCacheSharedContext.java           |   2 +-
 .../distributed/dht/GridDhtLockFuture.java      |  22 +-
 .../distributed/dht/GridDhtTxLocalAdapter.java  |  11 +-
 .../colocated/GridDhtColocatedLockFuture.java   |  71 ++-
 .../distributed/near/GridNearLockFuture.java    |   3 +-
 .../cache/distributed/near/GridNearTxLocal.java |   7 +-
 .../near/GridNearTxPrepareFutureAdapter.java    |   1 +
 .../cache/local/GridLocalLockFuture.java        |  74 ++-
 .../cache/transactions/IgniteTxAdapter.java     |  17 +-
 .../transactions/IgniteTxLocalAdapter.java      |  44 +-
 .../cache/transactions/IgniteTxManager.java     | 416 ++++++++++++-
 .../cache/transactions/TxDeadlock.java          | 159 +++++
 .../cache/transactions/TxDeadlockDetection.java | 599 +++++++++++++++++++
 .../processors/cache/transactions/TxLock.java   | 225 +++++++
 .../cache/transactions/TxLockList.java          | 134 +++++
 .../cache/transactions/TxLocksRequest.java      | 205 +++++++
 .../cache/transactions/TxLocksResponse.java     | 318 ++++++++++
 .../ignite/internal/util/IgniteUtils.java       |   4 +
 .../apache/ignite/transactions/Transaction.java |  19 +-
 .../TransactionDeadlockException.java           |  42 ++
 .../TransactionTimeoutException.java            |   5 +-
 .../transactions/DepthFirstSearchTest.java      | 252 ++++++++
 .../transactions/TxDeadlockDetectionTest.java   | 495 +++++++++++++++
 ...simisticDeadlockDetectionCrossCacheTest.java | 165 +++++
 .../TxPessimisticDeadlockDetectionTest.java     | 487 +++++++++++++++
 ...naryObjectsTxDeadlockDetectionTestSuite.java |  37 ++
 .../TxDeadlockDetectionTestSuite.java           |  44 ++
 34 files changed, 4018 insertions(+), 120 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/main/java/org/apache/ignite/IgniteCache.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/IgniteCache.java b/modules/core/src/main/java/org/apache/ignite/IgniteCache.java
index 581f091..3a76dd3 100644
--- a/modules/core/src/main/java/org/apache/ignite/IgniteCache.java
+++ b/modules/core/src/main/java/org/apache/ignite/IgniteCache.java
@@ -58,6 +58,9 @@ import org.apache.ignite.lang.IgniteBiInClosure;
 import org.apache.ignite.lang.IgniteBiPredicate;
 import org.apache.ignite.lang.IgniteFuture;
 import org.apache.ignite.mxbean.CacheMetricsMXBean;
+import org.apache.ignite.transactions.TransactionHeuristicException;
+import org.apache.ignite.transactions.TransactionRollbackException;
+import org.apache.ignite.transactions.TransactionTimeoutException;
 import org.jetbrains.annotations.Nullable;
 
 /**
@@ -233,6 +236,9 @@ public interface IgniteCache<K, V> extends javax.cache.Cache<K, V>, IgniteAsyncS
      *      previous value).
      * @throws NullPointerException If either key or value are {@code null}.
      * @throws CacheException If put operation failed.
+     * @throws TransactionTimeoutException If operation performs within transaction and timeout occurred.
+     * @throws TransactionRollbackException If operation performs within transaction that automatically rolled back.
+     * @throws TransactionHeuristicException If operation performs within transaction that entered an unknown state.
      */
     @IgniteAsyncSupported
     public V getAndPutIfAbsent(K key, V val) throws CacheException;
@@ -390,12 +396,20 @@ public interface IgniteCache<K, V> extends javax.cache.Cache<K, V>, IgniteAsyncS
      * if any, defined by the {@link EntryProcessor} implementation.  No mappings
      * will be returned for {@link EntryProcessor}s that return a
      * <code>null</code> value for a key.
+     * @throws TransactionTimeoutException If operation performs within transaction and timeout occurred.
+     * @throws TransactionRollbackException If operation performs within transaction that automatically rolled back.
+     * @throws TransactionHeuristicException If operation performs within transaction that entered an unknown state.
      */
     @IgniteAsyncSupported
     public <T> Map<K, EntryProcessorResult<T>> invokeAll(Map<? extends K, ? extends EntryProcessor<K, V, T>> map,
         Object... args);
 
-    /** {@inheritDoc} */
+    /**
+     * {@inheritDoc}
+     * @throws TransactionTimeoutException If operation performs within transaction and timeout occurred.
+     * @throws TransactionRollbackException If operation performs within transaction that automatically rolled back.
+     * @throws TransactionHeuristicException If operation performs within transaction that entered an unknown state.
+     */
     @IgniteAsyncSupported
     @Override public V get(K key);
 
@@ -406,19 +420,27 @@ public interface IgniteCache<K, V> extends javax.cache.Cache<K, V>, IgniteAsyncS
      * because the entry is missing from the cache, the Cache's {@link CacheLoader}
      * is called in an attempt to load the entry.
      *
-     * @param key the key whose associated value is to be returned
-     * @return the element, or null, if it does not exist.
-     * @throws IllegalStateException if the cache is {@link #isClosed()}
-     * @throws NullPointerException  if the key is null
-     * @throws CacheException        if there is a problem fetching the value
-     * @throws ClassCastException    if the implementation is configured to perform
+     * @param key The key whose associated value is to be returned.
+     * @return The element, or null, if it does not exist.
+     * @throws IllegalStateException If the cache is {@link #isClosed()}.
+     * @throws NullPointerException If the key is {@code null}.
+     * @throws CacheException If there is a problem fetching the value.
+     * @throws ClassCastException If the implementation is configured to perform
      * runtime-type-checking, and the key or value types are incompatible with those that have been
-     * configured for the {@link Cache}
+     * configured for the {@link Cache}.
+     * @throws TransactionTimeoutException If operation performs within transaction and timeout occurred.
+     * @throws TransactionRollbackException If operation performs within transaction that automatically rolled back.
+     * @throws TransactionHeuristicException If operation performs within transaction that entered an unknown state.
      */
     @IgniteAsyncSupported
     public CacheEntry<K, V> getEntry(K key);
 
-    /** {@inheritDoc} */
+    /**
+     * {@inheritDoc}
+     * @throws TransactionTimeoutException If operation performs within transaction and timeout occurred.
+     * @throws TransactionRollbackException If operation performs within transaction that automatically rolled back.
+     * @throws TransactionHeuristicException If operation performs within transaction that entered an unknown state.
+     */
     @IgniteAsyncSupported
     @Override public Map<K, V> getAll(Set<? extends K> keys);
 
@@ -434,12 +456,15 @@ public interface IgniteCache<K, V> extends javax.cache.Cache<K, V>, IgniteAsyncS
      * @param keys The keys whose associated values are to be returned.
      * @return A collection of entries that were found for the given keys. Entries not found
      *         in the cache are not in the returned collection.
-     * @throws NullPointerException  if keys is null or if keys contains a null
-     * @throws IllegalStateException if the cache is {@link #isClosed()}
-     * @throws CacheException        if there is a problem fetching the values
-     * @throws ClassCastException    if the implementation is configured to perform
+     * @throws NullPointerException If keys is null or if keys contains a {@code null}.
+     * @throws IllegalStateException If the cache is {@link #isClosed()}.
+     * @throws CacheException If there is a problem fetching the values.
+     * @throws ClassCastException If the implementation is configured to perform
      * runtime-type-checking, and the key or value types are incompatible with those that have been
-     * configured for the {@link Cache}
+     * configured for the {@link Cache}.
+     * @throws TransactionTimeoutException If operation performs within transaction and timeout occurred.
+     * @throws TransactionRollbackException If operation performs within transaction that automatically rolled back.
+     * @throws TransactionHeuristicException If operation performs within transaction that entered an unknown state.
      */
     @IgniteAsyncSupported
     public Collection<CacheEntry<K, V>> getEntries(Set<? extends K> keys);
@@ -454,7 +479,12 @@ public interface IgniteCache<K, V> extends javax.cache.Cache<K, V>, IgniteAsyncS
     @IgniteAsyncSupported
     public Map<K, V> getAllOutTx(Set<? extends K> keys);
 
-    /** {@inheritDoc} */
+    /**
+     * {@inheritDoc}
+     * @throws TransactionTimeoutException If operation performs within transaction and timeout occurred.
+     * @throws TransactionRollbackException If operation performs within transaction that automatically rolled back.
+     * @throws TransactionHeuristicException If operation performs within transaction that entered an unknown state.
+     */
     @IgniteAsyncSupported
     @Override public boolean containsKey(K key);
 
@@ -463,51 +493,109 @@ public interface IgniteCache<K, V> extends javax.cache.Cache<K, V>, IgniteAsyncS
      *
      * @param keys Key whose presence in this cache is to be tested.
      * @return {@code True} if this cache contains a mapping for the specified keys.
+     * @throws TransactionTimeoutException If operation performs within transaction and timeout occurred.
+     * @throws TransactionRollbackException If operation performs within transaction that automatically rolled back.
+     * @throws TransactionHeuristicException If operation performs within transaction that entered an unknown state.
      */
     @IgniteAsyncSupported
     public boolean containsKeys(Set<? extends K> keys);
 
-    /** {@inheritDoc} */
+    /**
+     * {@inheritDoc}
+     * @throws TransactionTimeoutException If operation performs within transaction and timeout occurred.
+     * @throws TransactionRollbackException If operation performs within transaction that automatically rolled back.
+     * @throws TransactionHeuristicException If operation performs within transaction that entered an unknown state.
+     */
     @IgniteAsyncSupported
     @Override public void put(K key, V val);
 
-    /** {@inheritDoc} */
+    /**
+     * {@inheritDoc}
+     * @throws TransactionTimeoutException If operation performs within transaction and timeout occurred.
+     * @throws TransactionRollbackException If operation performs within transaction that automatically rolled back.
+     * @throws TransactionHeuristicException If operation performs within transaction that entered an unknown state.
+     */
     @IgniteAsyncSupported
     @Override public V getAndPut(K key, V val);
 
-    /** {@inheritDoc} */
+    /**
+     * {@inheritDoc}
+     * @throws TransactionTimeoutException If operation performs within transaction and timeout occurred.
+     * @throws TransactionRollbackException If operation performs within transaction that automatically rolled back.
+     * @throws TransactionHeuristicException If operation performs within transaction that entered an unknown state.
+     */
     @IgniteAsyncSupported
     @Override public void putAll(Map<? extends K, ? extends V> map);
 
-    /** {@inheritDoc} */
+    /**
+     * {@inheritDoc}
+     * @throws TransactionTimeoutException If operation performs within transaction and timeout occurred.
+     * @throws TransactionRollbackException If operation performs within transaction that automatically rolled back.
+     * @throws TransactionHeuristicException If operation performs within transaction that entered an unknown state.
+     */
     @IgniteAsyncSupported
     @Override public boolean putIfAbsent(K key, V val);
 
-    /** {@inheritDoc} */
+    /**
+     * {@inheritDoc}
+     * @throws TransactionTimeoutException If operation performs within transaction and timeout occurred.
+     * @throws TransactionRollbackException If operation performs within transaction that automatically rolled back.
+     * @throws TransactionHeuristicException If operation performs within transaction that entered an unknown state.
+     */
     @IgniteAsyncSupported
     @Override public boolean remove(K key);
 
-    /** {@inheritDoc} */
+    /**
+     * {@inheritDoc}
+     * @throws TransactionTimeoutException If operation performs within transaction and timeout occurred.
+     * @throws TransactionRollbackException If operation performs within transaction that automatically rolled back.
+     * @throws TransactionHeuristicException If operation performs within transaction that entered an unknown state.
+     */
     @IgniteAsyncSupported
     @Override public boolean remove(K key, V oldVal);
 
-    /** {@inheritDoc} */
+    /**
+     * {@inheritDoc}
+     * @throws TransactionTimeoutException If operation performs within transaction and timeout occurred.
+     * @throws TransactionRollbackException If operation performs within transaction that automatically rolled back.
+     * @throws TransactionHeuristicException If operation performs within transaction that entered an unknown state.
+     */
     @IgniteAsyncSupported
     @Override public V getAndRemove(K key);
 
-    /** {@inheritDoc} */
+    /**
+     * {@inheritDoc}
+     * @throws TransactionTimeoutException If operation performs within transaction and timeout occurred.
+     * @throws TransactionRollbackException If operation performs within transaction that automatically rolled back.
+     * @throws TransactionHeuristicException If operation performs within transaction that entered an unknown state.
+     */
     @IgniteAsyncSupported
     @Override public boolean replace(K key, V oldVal, V newVal);
 
-    /** {@inheritDoc} */
+    /**
+     * {@inheritDoc}
+     * @throws TransactionTimeoutException If operation performs within transaction and timeout occurred.
+     * @throws TransactionRollbackException If operation performs within transaction that automatically rolled back.
+     * @throws TransactionHeuristicException If operation performs within transaction that entered an unknown state.
+     */
     @IgniteAsyncSupported
     @Override public boolean replace(K key, V val);
 
-    /** {@inheritDoc} */
+    /**
+     * {@inheritDoc}
+     * @throws TransactionTimeoutException If operation performs within transaction and timeout occurred.
+     * @throws TransactionRollbackException If operation performs within transaction that automatically rolled back.
+     * @throws TransactionHeuristicException If operation performs within transaction that entered an unknown state.
+     */
     @IgniteAsyncSupported
     @Override public V getAndReplace(K key, V val);
 
-    /** {@inheritDoc} */
+    /**
+     * {@inheritDoc}
+     * @throws TransactionTimeoutException If operation performs within transaction and timeout occurred.
+     * @throws TransactionRollbackException If operation performs within transaction that automatically rolled back.
+     * @throws TransactionHeuristicException If operation performs within transaction that entered an unknown state.
+     */
     @IgniteAsyncSupported
     @Override public void removeAll(Set<? extends K> keys);
 
@@ -591,7 +679,12 @@ public interface IgniteCache<K, V> extends javax.cache.Cache<K, V>, IgniteAsyncS
      */
     public void localClearAll(Set<? extends K> keys);
 
-    /** {@inheritDoc} */
+    /**
+     * {@inheritDoc}
+     * @throws TransactionTimeoutException If operation performs within transaction and timeout occurred.
+     * @throws TransactionRollbackException If operation performs within transaction that automatically rolled back.
+     * @throws TransactionHeuristicException If operation performs within transaction that entered an unknown state.
+     */
     @IgniteAsyncSupported
     @Override public <T> T invoke(K key, EntryProcessor<K, V, T> entryProcessor, Object... arguments);
 
@@ -605,28 +698,34 @@ public interface IgniteCache<K, V> extends javax.cache.Cache<K, V>, IgniteAsyncS
      * backup nodes in the cache. It is guaranteed that the value passed to the entry processor will be always
      * the same.
      *
-     * @param key            the key to the entry
-     * @param entryProcessor the {@link CacheEntryProcessor} to invoke
-     * @param arguments      additional arguments to pass to the
-     *                       {@link CacheEntryProcessor}
-     * @return the result of the processing, if any, defined by the
-     *         {@link CacheEntryProcessor} implementation
-     * @throws NullPointerException    if key or {@link CacheEntryProcessor} is null
-     * @throws IllegalStateException   if the cache is {@link #isClosed()}
-     * @throws ClassCastException    if the implementation is configured to perform
+     * @param key The key to the entry.
+     * @param entryProcessor The {@link CacheEntryProcessor} to invoke.
+     * @param arguments Additional arguments to pass to the {@link CacheEntryProcessor}.
+     * @return The result of the processing, if any, defined by the {@link CacheEntryProcessor} implementation.
+     * @throws NullPointerException If key or {@link CacheEntryProcessor} is null
+     * @throws IllegalStateException If the cache is {@link #isClosed()}
+     * @throws ClassCastException If the implementation is configured to perform
      *                               runtime-type-checking, and the key or value
      *                               types are incompatible with those that have been
-     *                               configured for the {@link Cache}
-     * @throws EntryProcessorException if an exception is thrown by the {@link
+     *                               configured for the {@link Cache}.
+     * @throws EntryProcessorException If an exception is thrown by the {@link
      *                                 CacheEntryProcessor}, a Caching Implementation
      *                                 must wrap any {@link Exception} thrown
      *                                 wrapped in an {@link EntryProcessorException}.
+     * @throws TransactionTimeoutException If operation performs within transaction and timeout occurred.
+     * @throws TransactionRollbackException If operation performs within transaction that automatically rolled back.
+     * @throws TransactionHeuristicException If operation performs within transaction that entered an unknown state.
      * @see CacheEntryProcessor
      */
     @IgniteAsyncSupported
     public <T> T invoke(K key, CacheEntryProcessor<K, V, T> entryProcessor, Object... arguments);
 
-    /** {@inheritDoc} */
+    /**
+     * {@inheritDoc}
+     * @throws TransactionTimeoutException If operation performs within transaction and timeout occurred.
+     * @throws TransactionRollbackException If operation performs within transaction that automatically rolled back.
+     * @throws TransactionHeuristicException If operation performs within transaction that entered an unknown state.
+     */
     @IgniteAsyncSupported
     @Override public <T> Map<K, EntryProcessorResult<T>> invokeAll(Set<? extends K> keys,
         EntryProcessor<K, V, T> entryProcessor, Object... args);
@@ -655,20 +754,22 @@ public interface IgniteCache<K, V> extends javax.cache.Cache<K, V>, IgniteAsyncS
      * backup nodes in the cache. It is guaranteed that the value passed to the entry processor will be always
      * the same.
      *
-     * @param keys           the set of keys for entries to process
-     * @param entryProcessor the {@link CacheEntryProcessor} to invoke
-     * @param args      additional arguments to pass to the
-     *                       {@link CacheEntryProcessor}
-     * @return the map of {@link EntryProcessorResult}s of the processing per key,
+     * @param keys The set of keys for entries to process.
+     * @param entryProcessor The {@link CacheEntryProcessor} to invoke.
+     * @param args Additional arguments to pass to the {@link CacheEntryProcessor}.
+     * @return The map of {@link EntryProcessorResult}s of the processing per key,
      * if any, defined by the {@link CacheEntryProcessor} implementation.  No mappings
      * will be returned for {@link CacheEntryProcessor}s that return a
      * <code>null</code> value for a key.
-     * @throws NullPointerException    if keys or {@link CacheEntryProcessor} are null
-     * @throws IllegalStateException   if the cache is {@link #isClosed()}
-     * @throws ClassCastException    if the implementation is configured to perform
+     * @throws NullPointerException If keys or {@link CacheEntryProcessor} are {#code null}.
+     * @throws IllegalStateException If the cache is {@link #isClosed()}.
+     * @throws ClassCastException If the implementation is configured to perform
      *                               runtime-type-checking, and the key or value
      *                               types are incompatible with those that have been
-     *                               configured for the {@link Cache}
+     *                               configured for the {@link Cache}.
+     * @throws TransactionTimeoutException If operation performs within transaction and timeout occurred.
+     * @throws TransactionRollbackException If operation performs within transaction that automatically rolled back.
+     * @throws TransactionHeuristicException If operation performs within transaction that entered an unknown state.
      * @see CacheEntryProcessor
      */
     @IgniteAsyncSupported

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/main/java/org/apache/ignite/IgniteSystemProperties.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/IgniteSystemProperties.java b/modules/core/src/main/java/org/apache/ignite/IgniteSystemProperties.java
index c8a2566..b203a82 100644
--- a/modules/core/src/main/java/org/apache/ignite/IgniteSystemProperties.java
+++ b/modules/core/src/main/java/org/apache/ignite/IgniteSystemProperties.java
@@ -190,6 +190,17 @@ public final class IgniteSystemProperties {
     public static final String IGNITE_TX_SALVAGE_TIMEOUT = "IGNITE_TX_SALVAGE_TIMEOUT";
 
     /**
+     * Specifies maximum number of iterations for deadlock detection procedure.
+     * If value of this property is less then or equal to zero then deadlock detection will be disabled.
+     */
+    public static final String IGNITE_TX_DEADLOCK_DETECTION_MAX_ITERS = "IGNITE_TX_DEADLOCK_DETECTION_MAX_ITERS";
+
+    /**
+     * Specifies timeout for deadlock detection procedure.
+     */
+    public static final String IGNITE_TX_DEADLOCK_DETECTION_TIMEOUT = "IGNITE_TX_DEADLOCK_DETECTION_TIMEOUT";
+
+    /**
      * System property to override multicast group taken from configuration.
      * Used for testing purposes.
      */

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/main/java/org/apache/ignite/internal/GridTopic.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/GridTopic.java b/modules/core/src/main/java/org/apache/ignite/internal/GridTopic.java
index fbf2b18..248f75b 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/GridTopic.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/GridTopic.java
@@ -91,7 +91,10 @@ public enum GridTopic {
     TOPIC_HADOOP,
 
     /** */
-    TOPIC_QUERY;
+    TOPIC_QUERY,
+
+    /** */
+    TOPIC_TX;
 
     /** Enum values. */
     private static final GridTopic[] VALS = values();

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/main/java/org/apache/ignite/internal/managers/communication/GridIoMessageFactory.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/managers/communication/GridIoMessageFactory.java b/modules/core/src/main/java/org/apache/ignite/internal/managers/communication/GridIoMessageFactory.java
index 47b1c5f..5f60215 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/managers/communication/GridIoMessageFactory.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/managers/communication/GridIoMessageFactory.java
@@ -99,6 +99,10 @@ import org.apache.ignite.internal.processors.cache.query.continuous.CacheContinu
 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.transactions.TxEntryValueHolder;
+import org.apache.ignite.internal.processors.cache.transactions.TxLock;
+import org.apache.ignite.internal.processors.cache.transactions.TxLockList;
+import org.apache.ignite.internal.processors.cache.transactions.TxLocksRequest;
+import org.apache.ignite.internal.processors.cache.transactions.TxLocksResponse;
 import org.apache.ignite.internal.processors.cache.version.GridCacheRawVersionedEntry;
 import org.apache.ignite.internal.processors.cache.version.GridCacheVersion;
 import org.apache.ignite.internal.processors.cache.version.GridCacheVersionEx;
@@ -156,6 +160,26 @@ public class GridIoMessageFactory implements MessageFactory {
         Message msg = null;
 
         switch (type) {
+            case -26:
+                msg = new TxLockList();
+
+                break;
+
+            case -25:
+                msg = new TxLock();
+
+                break;
+
+            case -24:
+                msg = new TxLocksRequest();
+
+                break;
+
+            case -23:
+                msg = new TxLocksResponse();
+
+                break;
+
             case TcpCommunicationSpi.NODE_ID_MSG_TYPE:
                 msg = new TcpCommunicationSpi.NodeIdMessage();
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheMapEntry.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheMapEntry.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheMapEntry.java
index c9ff138..c5be93e 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheMapEntry.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheMapEntry.java
@@ -18,8 +18,10 @@
 package org.apache.ignite.internal.processors.cache;
 
 import java.nio.ByteBuffer;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 import java.util.concurrent.atomic.AtomicReference;
@@ -4320,6 +4322,20 @@ public abstract class GridCacheMapEntry extends GridMetadataAwareAdapter impleme
     }
 
     /**
+     * @return All MVCC local candidates.
+     */
+    @Nullable public synchronized List<GridCacheMvccCandidate> mvccAllLocal() {
+        GridCacheMvcc mvcc = extras != null ? extras.mvcc() : null;
+
+        if (mvcc == null)
+            return null;
+
+        List<GridCacheMvccCandidate> locs = mvcc.allLocal();
+
+        return (locs == null || locs.isEmpty()) ? null : new ArrayList<>(locs);
+    }
+
+    /**
      * @param mvcc MVCC.
      */
     protected void mvccExtras(@Nullable GridCacheMvcc mvcc) {

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheMvcc.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheMvcc.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheMvcc.java
index adcbf92..507a2c9 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheMvcc.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheMvcc.java
@@ -1357,6 +1357,13 @@ public final class GridCacheMvcc {
     }
 
     /**
+     * @return Local MVCC candidates.
+     */
+    @Nullable public List<GridCacheMvccCandidate> allLocal() {
+        return locs;
+    }
+
+    /**
      * @param ver Version to check for ownership.
      * @return {@code True} if lock is owned by the specified version.
      */

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheMvccManager.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheMvccManager.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheMvccManager.java
index afba4bc..c4db01e 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheMvccManager.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheMvccManager.java
@@ -31,7 +31,6 @@ import java.util.concurrent.ConcurrentLinkedQueue;
 import java.util.concurrent.ConcurrentMap;
 import org.apache.ignite.IgniteCheckedException;
 import org.apache.ignite.IgniteLogger;
-import org.apache.ignite.IgniteSystemProperties;
 import org.apache.ignite.events.DiscoveryEvent;
 import org.apache.ignite.events.Event;
 import org.apache.ignite.internal.IgniteClientDisconnectedCheckedException;
@@ -660,6 +659,25 @@ public class GridCacheMvccManager extends GridCacheSharedManagerAdapter {
     }
 
     /**
+     * Gets futures for given lock ID.
+     *
+     * @param ver Lock ID.
+     * @return Futures.
+     */
+    @SuppressWarnings({"unchecked"})
+    @Nullable public Collection<GridCacheMvccFuture<?>> mvccFutures(GridCacheVersion ver) {
+        Collection<GridCacheMvccFuture<?>> futs = this.mvccFuts.get(ver);
+
+        if (futs != null) {
+            synchronized (futs) {
+                return new ArrayList<>(futs);
+            }
+        }
+
+        return null;
+    }
+
+    /**
      * @param futId Future ID.
      * @return Found future.
      */

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheSharedContext.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheSharedContext.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheSharedContext.java
index 01ca9f7..9c7a047 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheSharedContext.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheSharedContext.java
@@ -483,7 +483,7 @@ public class GridCacheSharedContext<K, V> {
      * @param nodeId Node ID.
      * @return Node or {@code null}.
      */
-    public ClusterNode node(UUID nodeId) {
+    @Nullable public ClusterNode node(UUID nodeId) {
         return kernalCtx.discovery().node(nodeId);
     }
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtLockFuture.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtLockFuture.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtLockFuture.java
index a3e94a4..b52376c 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtLockFuture.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/GridDhtLockFuture.java
@@ -132,7 +132,7 @@ public final class GridDhtLockFuture extends GridCompoundIdentityFuture<Boolean>
     private LockTimeoutObject timeoutObj;
 
     /** Lock timeout. */
-    private long timeout;
+    private final long timeout;
 
     /** Filter. */
     private CacheEntryPredicate[] filter;
@@ -199,6 +199,7 @@ public final class GridDhtLockFuture extends GridCompoundIdentityFuture<Boolean>
         assert nearNodeId != null;
         assert nearLockVer != null;
         assert topVer.topologyVersion() > 0;
+        assert (tx != null && timeout >= 0) || tx == null;
 
         this.cctx = cctx;
         this.nearNodeId = nearNodeId;
@@ -475,7 +476,7 @@ public final class GridDhtLockFuture extends GridCompoundIdentityFuture<Boolean>
     private void onFailed(boolean dist) {
         undoLocks(dist);
 
-        onComplete(false, false);
+        onComplete(false, false, true);
     }
 
     /**
@@ -614,7 +615,7 @@ public final class GridDhtLockFuture extends GridCompoundIdentityFuture<Boolean>
             err = t;
         }
 
-        onComplete(false, false);
+        onComplete(false, false, true);
     }
 
     /**
@@ -677,7 +678,7 @@ public final class GridDhtLockFuture extends GridCompoundIdentityFuture<Boolean>
     /** {@inheritDoc} */
     @Override public boolean cancel() {
         if (onCancelled())
-            onComplete(false, false);
+            onComplete(false, false, true);
 
         return isCancelled();
     }
@@ -707,7 +708,7 @@ public final class GridDhtLockFuture extends GridCompoundIdentityFuture<Boolean>
                 this.err = err;
         }
 
-        return onComplete(success, err instanceof NodeStoppingException);
+        return onComplete(success, err instanceof NodeStoppingException, true);
     }
 
     /**
@@ -715,13 +716,14 @@ public final class GridDhtLockFuture extends GridCompoundIdentityFuture<Boolean>
      *
      * @param success {@code True} if lock was acquired.
      * @param stopping {@code True} if node is stopping.
+     * @param unlock {@code True} if locks should be released.
      * @return {@code True} if complete by this operation.
      */
-    private boolean onComplete(boolean success, boolean stopping) {
+    private boolean onComplete(boolean success, boolean stopping, boolean unlock) {
         if (log.isDebugEnabled())
             log.debug("Received onComplete(..) callback [success=" + success + ", fut=" + this + ']');
 
-        if (!success && !stopping)
+        if (!success && !stopping && unlock)
             undoLocks(true);
 
         boolean set = false;
@@ -762,7 +764,7 @@ public final class GridDhtLockFuture extends GridCompoundIdentityFuture<Boolean>
      */
     public void map() {
         if (F.isEmpty(entries)) {
-            onComplete(true, false);
+            onComplete(true, false, true);
 
             return;
         }
@@ -1083,7 +1085,9 @@ public final class GridDhtLockFuture extends GridCompoundIdentityFuture<Boolean>
 
             timedOut = true;
 
-            onComplete(false, false);
+            boolean releaseLocks = !(inTx() && cctx.tm().deadlockDetectionEnabled());
+
+            onComplete(false, false, releaseLocks);
         }
 
         /** {@inheritDoc} */

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/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 34ba87b..35dfb62 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
@@ -42,6 +42,7 @@ 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.IgniteTxLocalAdapter;
 import org.apache.ignite.internal.processors.cache.version.GridCacheVersion;
+import org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException;
 import org.apache.ignite.internal.util.F0;
 import org.apache.ignite.internal.util.GridLeanMap;
 import org.apache.ignite.internal.util.GridLeanSet;
@@ -690,10 +691,16 @@ public abstract class GridDhtTxLocalAdapter extends IgniteTxLocalAdapter {
         if (passedKeys.isEmpty())
             return new GridFinishedFuture<>(ret);
 
-        GridDhtTransactionalCacheAdapter<?, ?> dhtCache = cacheCtx.isNear() ? cacheCtx.nearTx().dht() : cacheCtx.dhtTx();
+        GridDhtTransactionalCacheAdapter<?, ?> dhtCache =
+            cacheCtx.isNear() ? cacheCtx.nearTx().dht() : cacheCtx.dhtTx();
+
+        long timeout = remainingTime();
+
+        if (timeout == -1)
+            return new GridFinishedFuture<>(timeoutException());
 
         IgniteInternalFuture<Boolean> fut = dhtCache.lockAllAsyncInternal(passedKeys,
-            lockTimeout(),
+            timeout,
             this,
             isInvalidate(),
             read,

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/colocated/GridDhtColocatedLockFuture.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/colocated/GridDhtColocatedLockFuture.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/colocated/GridDhtColocatedLockFuture.java
index 5810028..3b8b9b8 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/colocated/GridDhtColocatedLockFuture.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/colocated/GridDhtColocatedLockFuture.java
@@ -21,8 +21,10 @@ import java.util.ArrayDeque;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Deque;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.Set;
 import java.util.UUID;
 import java.util.concurrent.atomic.AtomicReference;
 import org.apache.ignite.IgniteCheckedException;
@@ -50,8 +52,10 @@ import org.apache.ignite.internal.processors.cache.distributed.near.GridNearLock
 import org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal;
 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.transactions.TxDeadlock;
 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.GridCompoundIdentityFuture;
 import org.apache.ignite.internal.util.future.GridEmbeddedFuture;
 import org.apache.ignite.internal.util.future.GridFutureAdapter;
@@ -65,7 +69,9 @@ 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.IgniteBiTuple;
+import org.apache.ignite.lang.IgniteInClosure;
 import org.apache.ignite.lang.IgniteUuid;
+import org.apache.ignite.transactions.TransactionDeadlockException;
 import org.apache.ignite.transactions.TransactionIsolation;
 import org.jetbrains.annotations.Nullable;
 import org.jsr166.ConcurrentHashMap8;
@@ -438,6 +444,28 @@ public final class GridDhtColocatedLockFuture extends GridCompoundIdentityFuture
     }
 
     /**
+     * @return Keys for which locks requested from remote nodes but response isn't received.
+     */
+    public Set<KeyCacheObject> requestedKeys() {
+        Set<KeyCacheObject> requestedKeys = null;
+
+        for (IgniteInternalFuture<Boolean> miniFut : futures()) {
+            if (isMini(miniFut) && !miniFut.isDone()) {
+                if (requestedKeys == null)
+                    requestedKeys = new HashSet<>();
+
+                MiniFuture mini = (MiniFuture)miniFut;
+
+                requestedKeys.addAll(mini.keys);
+
+                return requestedKeys;
+            }
+        }
+
+        return requestedKeys;
+    }
+
+    /**
      * Finds pending mini future by the given mini ID.
      *
      * @param miniId Mini ID to find.
@@ -489,6 +517,10 @@ public final class GridDhtColocatedLockFuture extends GridCompoundIdentityFuture
         if (log.isDebugEnabled())
             log.debug("Received onDone(..) callback [success=" + success + ", err=" + err + ", fut=" + this + ']');
 
+        // Local GridDhtLockFuture
+        if (inTx() && this.err instanceof IgniteTxTimeoutCheckedException && cctx.tm().deadlockDetectionEnabled())
+            return false;
+
         if (isDone())
             return false;
 
@@ -1275,7 +1307,36 @@ public final class GridDhtColocatedLockFuture extends GridCompoundIdentityFuture
             if (log.isDebugEnabled())
                 log.debug("Timed out waiting for lock response: " + this);
 
-            onComplete(false, true);
+            if (inTx() && cctx.tm().deadlockDetectionEnabled()) {
+                Set<IgniteTxKey> keys = new HashSet<>();
+
+                for (IgniteTxEntry txEntry : tx.allEntries()) {
+                    if (!txEntry.locked())
+                        keys.add(txEntry.txKey());
+                }
+
+                IgniteInternalFuture<TxDeadlock> fut = cctx.tm().detectDeadlock(tx, keys);
+
+                fut.listen(new IgniteInClosure<IgniteInternalFuture<TxDeadlock>>() {
+                    @Override public void apply(IgniteInternalFuture<TxDeadlock> fut) {
+                        try {
+                            TxDeadlock deadlock = fut.get();
+
+                            if (deadlock != null)
+                                err = new TransactionDeadlockException(deadlock.toString(cctx.shared()));
+                        }
+                        catch (IgniteCheckedException e) {
+                            err = e;
+
+                            U.warn(log, "Failed to detect deadlock.", e);
+                        }
+
+                        onComplete(false, true);
+                    }
+                });
+            }
+            else
+                onComplete(false, true);
         }
 
         /** {@inheritDoc} */
@@ -1297,11 +1358,11 @@ public final class GridDhtColocatedLockFuture extends GridCompoundIdentityFuture
 
         /** Node ID. */
         @GridToStringExclude
-        private ClusterNode node;
+        private final ClusterNode node;
 
         /** Keys. */
         @GridToStringInclude
-        private Collection<KeyCacheObject> keys;
+        private final Collection<KeyCacheObject> keys;
 
         /** */
         private boolean rcvRes;
@@ -1377,6 +1438,10 @@ public final class GridDhtColocatedLockFuture extends GridCompoundIdentityFuture
             }
 
             if (res.error() != null) {
+                if (inTx() && res.error() instanceof IgniteTxTimeoutCheckedException &&
+                    cctx.tm().deadlockDetectionEnabled())
+                    return;
+
                 if (log.isDebugEnabled())
                     log.debug("Finishing mini future with an error due to error in response [miniFut=" + this +
                         ", res=" + res + ']');

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearLockFuture.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearLockFuture.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearLockFuture.java
index 0d17bd8..4b6448b 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearLockFuture.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearLockFuture.java
@@ -119,7 +119,7 @@ public final class GridNearLockFuture extends GridCompoundIdentityFuture<Boolean
     private LockTimeoutObject timeoutObj;
 
     /** Lock timeout. */
-    private long timeout;
+    private final long timeout;
 
     /** Filter. */
     private final CacheEntryPredicate[] filter;
@@ -181,6 +181,7 @@ public final class GridNearLockFuture extends GridCompoundIdentityFuture<Boolean
         super(CU.boolReducer());
 
         assert keys != null;
+        assert (tx != null && timeout >= 0) || tx == null;
 
         this.cctx = cctx;
         this.keys = keys;

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearTxLocal.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearTxLocal.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearTxLocal.java
index 7f3ecd0..7500d8f 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearTxLocal.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearTxLocal.java
@@ -1180,8 +1180,13 @@ public class GridNearTxLocal extends GridDhtTxLocalAdapter {
         if (log.isDebugEnabled())
             log.debug("Before acquiring transaction lock on keys: " + keys);
 
+        long timeout = remainingTime();
+
+        if (timeout == -1)
+            return new GridFinishedFuture<>(timeoutException());
+
         IgniteInternalFuture<Boolean> fut = cacheCtx.colocated().lockAllAsyncInternal(keys,
-            lockTimeout(),
+            timeout,
             this,
             isInvalidate(),
             read,

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearTxPrepareFutureAdapter.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearTxPrepareFutureAdapter.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearTxPrepareFutureAdapter.java
index 03e1034..fd959b4 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearTxPrepareFutureAdapter.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/near/GridNearTxPrepareFutureAdapter.java
@@ -78,6 +78,7 @@ public abstract class GridNearTxPrepareFutureAdapter extends
     protected GridCacheSharedContext<?, ?> cctx;
 
     /** Future ID. */
+    @GridToStringInclude
     protected IgniteUuid futId;
 
     /** Transaction. */

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/local/GridLocalLockFuture.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/local/GridLocalLockFuture.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/local/GridLocalLockFuture.java
index c69759b..c5bd71a 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/local/GridLocalLockFuture.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/local/GridLocalLockFuture.java
@@ -19,12 +19,15 @@ package org.apache.ignite.internal.processors.cache.local;
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 import java.util.UUID;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
 import org.apache.ignite.IgniteCheckedException;
 import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.internal.IgniteInternalFuture;
 import org.apache.ignite.internal.processors.cache.CacheEntryPredicate;
 import org.apache.ignite.internal.processors.cache.GridCacheContext;
 import org.apache.ignite.internal.processors.cache.GridCacheEntryEx;
@@ -32,14 +35,18 @@ import org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedExceptio
 import org.apache.ignite.internal.processors.cache.GridCacheMvccCandidate;
 import org.apache.ignite.internal.processors.cache.GridCacheMvccFuture;
 import org.apache.ignite.internal.processors.cache.KeyCacheObject;
+import org.apache.ignite.internal.processors.cache.transactions.IgniteTxKey;
 import org.apache.ignite.internal.processors.cache.transactions.IgniteTxLocalEx;
+import org.apache.ignite.internal.processors.cache.transactions.TxDeadlock;
 import org.apache.ignite.internal.processors.cache.version.GridCacheVersion;
 import org.apache.ignite.internal.processors.timeout.GridTimeoutObjectAdapter;
+import org.apache.ignite.transactions.TransactionDeadlockException;
 import org.apache.ignite.internal.util.future.GridFutureAdapter;
 import org.apache.ignite.internal.util.tostring.GridToStringExclude;
 import org.apache.ignite.internal.util.tostring.GridToStringInclude;
 import org.apache.ignite.internal.util.typedef.internal.S;
 import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.lang.IgniteInClosure;
 import org.apache.ignite.lang.IgniteUuid;
 import org.jetbrains.annotations.Nullable;
 
@@ -92,7 +99,7 @@ public final class GridLocalLockFuture<K, V> extends GridFutureAdapter<Boolean>
     private LockTimeoutObject timeoutObj;
 
     /** Lock timeout. */
-    private long timeout;
+    private final long timeout;
 
     /** Filter. */
     private CacheEntryPredicate[] filter;
@@ -120,6 +127,7 @@ public final class GridLocalLockFuture<K, V> extends GridFutureAdapter<Boolean>
         CacheEntryPredicate[] filter) {
         assert keys != null;
         assert cache != null;
+        assert (tx != null && timeout >= 0) || tx == null;
 
         this.cctx = cctx;
         this.cache = cache;
@@ -173,6 +181,13 @@ public final class GridLocalLockFuture<K, V> extends GridFutureAdapter<Boolean>
     }
 
     /**
+     * @return Entries.
+     */
+    private List<GridLocalCacheEntry> entries() {
+        return entries;
+    }
+
+    /**
      * @return {@code True} if transaction is not {@code null}.
      */
     private boolean inTx() {
@@ -395,18 +410,6 @@ public final class GridLocalLockFuture<K, V> extends GridFutureAdapter<Boolean>
         }
     }
 
-    /**
-     * Checks for errors.
-     *
-     * @throws IgniteCheckedException If execution failed.
-     */
-    private void checkError() throws IgniteCheckedException {
-        Throwable err0 = err;
-
-        if (err0 != null)
-            throw U.cast(err0);
-    }
-
     /** {@inheritDoc} */
     @Override public int hashCode() {
         return futId.hashCode();
@@ -429,12 +432,53 @@ public final class GridLocalLockFuture<K, V> extends GridFutureAdapter<Boolean>
         }
 
         /** {@inheritDoc} */
-        @SuppressWarnings({"ThrowableInstanceNeverThrown"})
+        @SuppressWarnings({"ThrowableInstanceNeverThrown", "ForLoopReplaceableByForEach"})
         @Override public void onTimeout() {
             if (log.isDebugEnabled())
                 log.debug("Timed out waiting for lock response: " + this);
 
-            onComplete(false);
+            if (inTx() && cctx.tm().deadlockDetectionEnabled()) {
+                Set<IgniteTxKey> keys = new HashSet<>();
+
+                List<GridLocalCacheEntry> entries = entries();
+
+                for (int i = 0; i < entries.size(); i++) {
+                    GridLocalCacheEntry e = entries.get(i);
+
+                    List<GridCacheMvccCandidate> mvcc = e.mvccAllLocal();
+
+                    if (mvcc == null)
+                        continue;
+
+                    GridCacheMvccCandidate cand = mvcc.get(0);
+
+                    if (cand.owner() && cand.tx() && !cand.version().equals(tx.xidVersion()))
+                        keys.add(e.txKey());
+                }
+
+                IgniteInternalFuture<TxDeadlock> fut = cctx.tm().detectDeadlock(tx, keys);
+
+                fut.listen(new IgniteInClosure<IgniteInternalFuture<TxDeadlock>>() {
+                    @Override public void apply(IgniteInternalFuture<TxDeadlock> fut) {
+                        try {
+                            TxDeadlock deadlock = fut.get();
+
+                            if (deadlock != null)
+                                ERR_UPD.compareAndSet(GridLocalLockFuture.this, null,
+                                    new TransactionDeadlockException(deadlock.toString(cctx.shared())));
+                        }
+                        catch (IgniteCheckedException e) {
+                            U.warn(log, "Failed to detect deadlock.", e);
+
+                            ERR_UPD.compareAndSet(GridLocalLockFuture.this, null, e);
+                        }
+
+                        onComplete(false);
+                    }
+                });
+            }
+            else
+                onComplete(false);
         }
 
         /** {@inheritDoc} */

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/IgniteTxAdapter.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/IgniteTxAdapter.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/IgniteTxAdapter.java
index b8447f0..ebddd50 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/IgniteTxAdapter.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/IgniteTxAdapter.java
@@ -692,27 +692,24 @@ public abstract class IgniteTxAdapter extends GridMetadataAwareAdapter implement
     /**
      * Gets remaining allowed transaction time.
      *
-     * @return Remaining transaction time.
+     * @return Remaining transaction time. {@code 0} if timeout isn't specified. {@code -1} if time is out.
      */
     @Override public long remainingTime() {
         if (timeout() <= 0)
-            return -1;
+            return 0;
 
         long timeLeft = timeout() - (U.currentTimeMillis() - startTime());
 
-        if (timeLeft < 0)
-            return 0;
+        return timeLeft <= 0 ? -1 : timeLeft;
 
-        return timeLeft;
     }
 
     /**
-     * @return Lock timeout.
+     * @return Transaction timeout exception.
      */
-    protected long lockTimeout() {
-        long timeout = remainingTime();
-
-        return timeout < 0 ? 0 : timeout == 0 ? -1 : timeout;
+    protected final IgniteCheckedException timeoutException() {
+        return new IgniteTxTimeoutCheckedException("Failed to acquire lock within provided timeout " +
+            "for transaction [timeout=" + timeout() + ", tx=" + this + ']');
     }
 
     /** {@inheritDoc} */

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/IgniteTxLocalAdapter.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/IgniteTxLocalAdapter.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/IgniteTxLocalAdapter.java
index 71f74a8..e8aa7c8 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/IgniteTxLocalAdapter.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/IgniteTxLocalAdapter.java
@@ -67,6 +67,7 @@ import org.apache.ignite.internal.processors.dr.GridDrType;
 import org.apache.ignite.internal.transactions.IgniteTxHeuristicCheckedException;
 import org.apache.ignite.internal.transactions.IgniteTxRollbackCheckedException;
 import org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException;
+import org.apache.ignite.transactions.TransactionDeadlockException;
 import org.apache.ignite.internal.util.GridLeanMap;
 import org.apache.ignite.internal.util.future.GridEmbeddedFuture;
 import org.apache.ignite.internal.util.future.GridFinishedFuture;
@@ -1829,8 +1830,13 @@ public abstract class IgniteTxLocalAdapter extends IgniteTxAdapter implements Ig
 
                 long accessTtl = expiryPlc != null ? CU.toTtl(expiryPlc.getExpiryForAccess()) : CU.TTL_NOT_CHANGED;
 
+                long timeout = remainingTime();
+
+                if (timeout == -1)
+                    return new GridFinishedFuture<>(timeoutException());
+
                 IgniteInternalFuture<Boolean> fut = cacheCtx.cache().txLockAsync(lockKeys,
-                    lockTimeout(),
+                    timeout,
                     this,
                     true,
                     true,
@@ -3143,8 +3149,13 @@ public abstract class IgniteTxLocalAdapter extends IgniteTxAdapter implements Ig
                 if (log.isDebugEnabled())
                     log.debug("Before acquiring transaction lock for put on key: " + enlisted);
 
+                long timeout = remainingTime();
+
+                if (timeout == -1)
+                    return new GridFinishedFuture<>(timeoutException());
+
                 IgniteInternalFuture<Boolean> fut = cacheCtx.cache().txLockAsync(enlisted,
-                    lockTimeout(),
+                    timeout,
                     this,
                     false,
                     retval,
@@ -3316,8 +3327,13 @@ public abstract class IgniteTxLocalAdapter extends IgniteTxAdapter implements Ig
                 if (log.isDebugEnabled())
                     log.debug("Before acquiring transaction lock for put on keys: " + enlisted);
 
+                long timeout = remainingTime();
+
+                if (timeout == -1)
+                    return new GridFinishedFuture<>(timeoutException());
+
                 IgniteInternalFuture<Boolean> fut = cacheCtx.cache().txLockAsync(enlisted,
-                    lockTimeout(),
+                    timeout,
                     this,
                     false,
                     retval,
@@ -3603,8 +3619,13 @@ public abstract class IgniteTxLocalAdapter extends IgniteTxAdapter implements Ig
             if (log.isDebugEnabled())
                 log.debug("Before acquiring transaction lock for remove on keys: " + enlisted);
 
+            long timeout = remainingTime();
+
+            if (timeout == -1)
+                return new GridFinishedFuture<>(timeoutException());
+
             IgniteInternalFuture<Boolean> fut = cacheCtx.cache().txLockAsync(enlisted,
-                lockTimeout(),
+                timeout,
                 this,
                 false,
                 retval,
@@ -3758,7 +3779,7 @@ public abstract class IgniteTxLocalAdapter extends IgniteTxAdapter implements Ig
             throw new IgniteCheckedException("Cache transaction marked as rollback-only: " + this);
         }
 
-        if (remainingTime() == 0 && setRollbackOnly())
+        if (remainingTime() == -1 && setRollbackOnly())
             throw new IgniteTxTimeoutCheckedException("Cache transaction timed out " +
                 "(was rolled back automatically): " + this);
     }
@@ -4121,7 +4142,9 @@ public abstract class IgniteTxLocalAdapter extends IgniteTxAdapter implements Ig
 
         /** {@inheritDoc} */
         @Override public final IgniteInternalFuture<T> apply(Boolean locked, @Nullable final Exception e) {
-            if (e != null) {
+            TransactionDeadlockException deadlockErr = X.cause(e, TransactionDeadlockException.class);
+
+            if (e != null && deadlockErr == null) {
                 setRollbackOnly();
 
                 if (commit && commitAfterLock())
@@ -4134,12 +4157,13 @@ public abstract class IgniteTxLocalAdapter extends IgniteTxAdapter implements Ig
                 throw new GridClosureException(e);
             }
 
-            if (!locked) {
+            if (deadlockErr != null || !locked) {
                 setRollbackOnly();
 
-                final GridClosureException ex = new GridClosureException(new IgniteTxTimeoutCheckedException("Failed to " +
-                    "acquire lock within provided timeout for transaction [timeout=" + timeout() +
-                    ", tx=" + this + ']'));
+                final GridClosureException ex = new GridClosureException(
+                    new IgniteTxTimeoutCheckedException("Failed to acquire lock within provided timeout " +
+                        "for transaction [timeout=" + timeout() + ", tx=" + this + ']', deadlockErr)
+                );
 
                 if (commit && commitAfterLock())
                     return rollbackAsync().chain(new C1<IgniteInternalFuture<IgniteInternalTx>, T>() {

http://git-wip-us.apache.org/repos/asf/ignite/blob/0f2b7532/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/IgniteTxManager.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/IgniteTxManager.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/IgniteTxManager.java
index ccfc4de..40733cc 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/IgniteTxManager.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/transactions/IgniteTxManager.java
@@ -24,6 +24,7 @@ import java.util.Collections;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedHashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
@@ -31,17 +32,25 @@ import java.util.concurrent.ConcurrentMap;
 import org.apache.ignite.IgniteCheckedException;
 import org.apache.ignite.IgniteClientDisconnectedException;
 import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteSystemProperties;
+import org.apache.ignite.binary.BinaryObjectException;
+import org.apache.ignite.cluster.ClusterNode;
 import org.apache.ignite.events.DiscoveryEvent;
 import org.apache.ignite.events.Event;
 import org.apache.ignite.internal.IgniteInternalFuture;
-import org.apache.ignite.internal.managers.communication.GridIoPolicy;
+import org.apache.ignite.internal.cluster.ClusterTopologyCheckedException;
+import org.apache.ignite.internal.managers.communication.GridMessageListener;
 import org.apache.ignite.internal.managers.eventstorage.GridLocalEventListener;
 import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
 import org.apache.ignite.internal.processors.cache.GridCacheContext;
 import org.apache.ignite.internal.processors.cache.GridCacheEntryEx;
 import org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException;
+import org.apache.ignite.internal.processors.cache.GridCacheMapEntry;
+import org.apache.ignite.internal.processors.cache.GridCacheMessage;
 import org.apache.ignite.internal.processors.cache.GridCacheMvccCandidate;
+import org.apache.ignite.internal.processors.cache.GridCacheMvccFuture;
 import org.apache.ignite.internal.processors.cache.GridCacheSharedManagerAdapter;
+import org.apache.ignite.internal.processors.cache.KeyCacheObject;
 import org.apache.ignite.internal.processors.cache.distributed.GridCacheMappedVersion;
 import org.apache.ignite.internal.processors.cache.distributed.GridCacheTxFinishSync;
 import org.apache.ignite.internal.processors.cache.distributed.GridCacheTxRecoveryFuture;
@@ -50,9 +59,11 @@ import org.apache.ignite.internal.processors.cache.distributed.GridDistributedTx
 import org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtInvalidPartitionException;
 import org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxLocal;
 import org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtTxRemote;
+import org.apache.ignite.internal.processors.cache.distributed.dht.colocated.GridDhtColocatedLockFuture;
 import org.apache.ignite.internal.processors.cache.distributed.near.GridNearCacheAdapter;
 import org.apache.ignite.internal.processors.cache.distributed.near.GridNearCacheEntry;
 import org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal;
+import org.apache.ignite.internal.processors.cache.transactions.TxDeadlockDetection.TxDeadlockFuture;
 import org.apache.ignite.internal.processors.cache.version.GridCacheVersion;
 import org.apache.ignite.internal.processors.timeout.GridTimeoutObjectAdapter;
 import org.apache.ignite.internal.transactions.IgniteTxOptimisticCheckedException;
@@ -67,6 +78,7 @@ 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.U;
 import org.apache.ignite.lang.IgniteFuture;
+import org.apache.ignite.lang.IgniteProductVersion;
 import org.apache.ignite.lang.IgniteReducer;
 import org.apache.ignite.lang.IgniteUuid;
 import org.apache.ignite.transactions.TransactionConcurrency;
@@ -78,22 +90,22 @@ import org.jsr166.ConcurrentLinkedHashMap;
 
 import static org.apache.ignite.IgniteSystemProperties.IGNITE_MAX_COMPLETED_TX_COUNT;
 import static org.apache.ignite.IgniteSystemProperties.IGNITE_SLOW_TX_WARN_TIMEOUT;
+import static org.apache.ignite.IgniteSystemProperties.IGNITE_TX_DEADLOCK_DETECTION_MAX_ITERS;
 import static org.apache.ignite.IgniteSystemProperties.IGNITE_TX_SALVAGE_TIMEOUT;
 import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
 import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.GridTopic.TOPIC_TX;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.SYSTEM_POOL;
 import static org.apache.ignite.internal.processors.cache.GridCacheUtils.isNearEnabled;
 import static org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx.FinalizationStatus.RECOVERY_FINISH;
 import static org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx.FinalizationStatus.USER_FINISH;
 import static org.apache.ignite.internal.util.GridConcurrentFactory.newMap;
-import static org.apache.ignite.transactions.TransactionConcurrency.OPTIMISTIC;
-import static org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC;
 import static org.apache.ignite.transactions.TransactionState.ACTIVE;
 import static org.apache.ignite.transactions.TransactionState.COMMITTED;
 import static org.apache.ignite.transactions.TransactionState.COMMITTING;
 import static org.apache.ignite.transactions.TransactionState.MARKED_ROLLBACK;
 import static org.apache.ignite.transactions.TransactionState.PREPARED;
 import static org.apache.ignite.transactions.TransactionState.PREPARING;
-import static org.apache.ignite.transactions.TransactionState.ROLLED_BACK;
 import static org.apache.ignite.transactions.TransactionState.UNKNOWN;
 import static org.jsr166.ConcurrentLinkedHashMap.QueuePolicy.PER_SEGMENT_Q;
 
@@ -110,6 +122,13 @@ public class IgniteTxManager extends GridCacheSharedManagerAdapter {
     /** Tx salvage timeout (default 3s). */
     private static final int TX_SALVAGE_TIMEOUT = Integer.getInteger(IGNITE_TX_SALVAGE_TIMEOUT, 100);
 
+    /** Version in which deadlock detection introduced. */
+    public static final IgniteProductVersion TX_DEADLOCK_DETECTION_SINCE = IgniteProductVersion.fromString("1.5.15");
+
+    /** Deadlock detection maximum iterations. */
+    static final int DEADLOCK_MAX_ITERS =
+        IgniteSystemProperties.getInteger(IGNITE_TX_DEADLOCK_DETECTION_MAX_ITERS, 1000);
+
     /** Committing transactions. */
     private final ThreadLocal<IgniteInternalTx> threadCtx = new ThreadLocal<>();
 
@@ -128,6 +147,9 @@ public class IgniteTxManager extends GridCacheSharedManagerAdapter {
     /** Per-ID map for near transactions. */
     private final ConcurrentMap<GridCacheVersion, IgniteInternalTx> nearIdMap = newMap();
 
+    /** Deadlock detection futures. */
+    private final ConcurrentMap<Long, TxDeadlockFuture> deadlockDetectFuts = new ConcurrentHashMap8<>();
+
     /** TX handler. */
     private IgniteTxHandler txHnd;
 
@@ -161,6 +183,9 @@ public class IgniteTxManager extends GridCacheSharedManagerAdapter {
     private final ConcurrentMap<GridCacheVersion, GridCacheVersion> mappedVers =
         new ConcurrentHashMap8<>(5120);
 
+    /** TxDeadlock detection. */
+    private TxDeadlockDetection txDeadlockDetection;
+
     /** {@inheritDoc} */
     @Override protected void onKernalStart0(boolean reconnect) {
         if (reconnect)
@@ -174,14 +199,23 @@ public class IgniteTxManager extends GridCacheSharedManagerAdapter {
 
                     DiscoveryEvent discoEvt = (DiscoveryEvent)evt;
 
-                    cctx.time().addTimeoutObject(new NodeFailureTimeoutObject(discoEvt.eventNode().id()));
+                    UUID nodeId = discoEvt.eventNode().id();
+
+                    cctx.time().addTimeoutObject(new NodeFailureTimeoutObject(nodeId));
 
                     if (txFinishSync != null)
-                        txFinishSync.onNodeLeft(discoEvt.eventNode().id());
+                        txFinishSync.onNodeLeft(nodeId);
+
+                    for (TxDeadlockFuture fut : deadlockDetectFuts.values())
+                        fut.onNodeLeft(nodeId);
                 }
             },
             EVT_NODE_FAILED, EVT_NODE_LEFT);
 
+        this.txDeadlockDetection = new TxDeadlockDetection(cctx);
+
+        cctx.gridIO().addMessageListener(TOPIC_TX, new DeadlockDetectionListener());
+
         for (IgniteInternalTx tx : idMap.values()) {
             if ((!tx.local() || tx.dht()) && !cctx.discovery().aliveAll(tx.masterNodeIds())) {
                 if (log.isDebugEnabled())
@@ -193,6 +227,11 @@ public class IgniteTxManager extends GridCacheSharedManagerAdapter {
     }
 
     /** {@inheritDoc} */
+    @Override protected void onKernalStop0(boolean cancel) {
+        cctx.gridIO().removeMessageListener(TOPIC_TX);
+    }
+
+    /** {@inheritDoc} */
     @Override protected void start0() throws IgniteCheckedException {
         txFinishSync = new GridCacheTxFinishSync<>(cctx);
 
@@ -205,6 +244,12 @@ public class IgniteTxManager extends GridCacheSharedManagerAdapter {
 
         for (Map.Entry<Long, IgniteInternalTx> e : threadMap.entrySet())
             rollbackTx(e.getValue());
+
+        IgniteClientDisconnectedException err =
+            new IgniteClientDisconnectedException(reconnectFut, "Client node disconnected.");
+
+        for (TxDeadlockFuture fut : deadlockDetectFuts.values())
+            fut.onDone(err);
     }
 
     /**
@@ -377,7 +422,7 @@ public class IgniteTxManager extends GridCacheSharedManagerAdapter {
             implicit,
             implicitSingle,
             sysCacheCtx != null,
-            sysCacheCtx != null ? sysCacheCtx.ioPolicy() : GridIoPolicy.SYSTEM_POOL,
+            sysCacheCtx != null ? sysCacheCtx.ioPolicy() : SYSTEM_POOL,
             concurrency,
             isolation,
             timeout,
@@ -743,7 +788,7 @@ public class IgniteTxManager extends GridCacheSharedManagerAdapter {
             throw new IgniteCheckedException("Transaction is marked for rollback: " + tx);
         }
 
-        if (tx.remainingTime() == 0) {
+        if (tx.remainingTime() == -1) {
             tx.setRollbackOnly();
 
             throw new IgniteTxTimeoutCheckedException("Transaction timed out: " + this);
@@ -1421,12 +1466,12 @@ public class IgniteTxManager extends GridCacheSharedManagerAdapter {
         throws IgniteCheckedException {
         assert tx.optimistic() || !tx.local();
 
-        long remainingTime = U.currentTimeMillis() - (tx.startTime() + tx.timeout());
+        long remainingTime = tx.remainingTime();
 
         // For serializable transactions, failure to acquire lock means
         // that there is a serializable conflict. For all other isolation levels,
         // we wait for the lock.
-        long timeout = tx.timeout() == 0 ? 0 : remainingTime;
+        long timeout = remainingTime < 0 ? 0 : remainingTime;
 
         GridCacheVersion serOrder = (tx.serializable() && tx.optimistic()) ? tx.nearXidVersion() : null;
 
@@ -1848,6 +1893,217 @@ public class IgniteTxManager extends GridCacheSharedManagerAdapter {
     }
 
     /**
+     * @return {@code True} if deadlock detection is enabled.
+     */
+    public boolean deadlockDetectionEnabled() {
+        return DEADLOCK_MAX_ITERS > 0;
+    }
+
+    /**
+     * Performs deadlock detection for given keys.
+     *
+     * @param tx Target tx.
+     * @param keys Keys.
+     * @return Detection result.
+     */
+    public IgniteInternalFuture<TxDeadlock> detectDeadlock(
+        IgniteInternalTx tx,
+        Set<IgniteTxKey> keys
+    ) {
+        return txDeadlockDetection.detectDeadlock(tx, keys);
+    }
+
+    /**
+     * @param nodeId Node ID.
+     * @param fut Future.
+     * @param txKeys Tx keys.
+     */
+    void txLocksInfo(UUID nodeId, TxDeadlockFuture fut, Set<IgniteTxKey> txKeys) {
+        ClusterNode node = cctx.node(nodeId);
+
+        if (node == null) {
+            if (log.isDebugEnabled())
+                log.debug("Failed to finish deadlock detection, node left: " + nodeId);
+
+            fut.onDone();
+
+            return;
+        }
+
+        if (supportsDeadlockDetection(node)) {
+            TxLocksRequest req = new TxLocksRequest(fut.futureId(), txKeys);
+
+            try {
+                if (!cctx.localNodeId().equals(nodeId))
+                    req.prepareMarshal(cctx);
+
+                cctx.gridIO().send(node, TOPIC_TX, req, SYSTEM_POOL);
+            }
+            catch (IgniteCheckedException e) {
+                if (e instanceof ClusterTopologyCheckedException) {
+                    if (log.isDebugEnabled())
+                        log.debug("Failed to finish deadlock detection, node left: " + nodeId);
+                }
+                else
+                    U.warn(log, "Failed to finish deadlock detection: " + e, e);
+
+                fut.onDone();
+            }
+        }
+        else {
+            if (log.isDebugEnabled())
+                log.debug("Failed to finish deadlock detection, node does not support deadlock detection: " + node);
+
+            fut.onDone();
+        }
+    }
+
+    /**
+     * @param node Node.
+     * @return {@code True} if node supports deadlock detection protocol.
+     */
+    private boolean supportsDeadlockDetection(ClusterNode node) {
+        return TX_DEADLOCK_DETECTION_SINCE.compareToIgnoreTimestamp(node.version()) <= 0;
+    }
+
+    /**
+     * @param tx Tx.
+     * @param txKeys Tx keys.
+     * @return {@code True} if key is involved into tx.
+     */
+    private boolean hasKeys(IgniteInternalTx tx, Collection<IgniteTxKey> txKeys) {
+        for (IgniteTxKey key : txKeys) {
+            if (tx.txState().entry(key) != null)
+                return true;
+        }
+
+        return false;
+    }
+
+    /**
+     * @param txKeys Tx keys.
+     * @return Transactions locks and nodes.
+     */
+    private TxLocksResponse txLocksInfo(Collection<IgniteTxKey> txKeys) {
+        TxLocksResponse res = new TxLocksResponse();
+
+        Collection<IgniteInternalTx> txs = activeTransactions();
+
+        for (IgniteInternalTx tx : txs) {
+            boolean nearTxLoc = tx instanceof GridNearTxLocal;
+
+            if (!(nearTxLoc || tx instanceof GridDhtTxLocal) || !hasKeys(tx, txKeys))
+                continue;
+
+            Collection<IgniteTxEntry> txEntries = tx.allEntries();
+
+            Set<KeyCacheObject> requestedKeys = null;
+
+            // Try to get info about requested keys for detached entries in case of GridNearTxLocal transaction
+            // in order to reduce amount of requests to remote nodes.
+            if (nearTxLoc) {
+                GridDhtColocatedLockFuture fut = colocatedLockFuture(tx);
+
+                if (fut != null)
+                    requestedKeys = fut.requestedKeys();
+            }
+
+            for (IgniteTxEntry txEntry : txEntries) {
+                IgniteTxKey txKey = txEntry.txKey();
+
+                if (res.txLocks(txKey) == null) {
+                    GridCacheMapEntry e = (GridCacheMapEntry)txEntry.cached();
+
+                    List<GridCacheMvccCandidate> locs = e.mvccAllLocal();
+
+                    if (locs != null) {
+                        boolean owner = false;
+
+                        for (GridCacheMvccCandidate loc : locs) {
+                            if (!owner && loc.owner() && loc.tx())
+                                owner = true;
+
+                            if (!owner) // Skip all candidates in case when no tx that owns lock.
+                                break;
+
+                            if (loc.tx()) {
+                                UUID nearNodeId = loc.otherNodeId();
+
+                                GridCacheVersion txId = loc.otherVersion();
+
+                                TxLock txLock = new TxLock(
+                                    txId == null ? loc.version() : txId,
+                                    nearNodeId == null ? loc.nodeId() : nearNodeId,
+                                    loc.threadId(),
+                                    loc.owner() ? TxLock.OWNERSHIP_OWNER : TxLock.OWNERSHIP_CANDIDATE);
+
+                                res.addTxLock(txKey, txLock);
+                            }
+                        }
+                    }
+                    // Special case for optimal sequence of nodes processing.
+                    else if (nearTxLoc && requestedKeys != null && requestedKeys.contains(txKey.key())) {
+                        TxLock txLock = new TxLock(
+                            tx.nearXidVersion(),
+                            tx.nodeId(),
+                            tx.threadId(),
+                            TxLock.OWNERSHIP_REQUESTED);
+
+                        res.addTxLock(txKey, txLock);
+                    }
+                    else
+                        res.addKey(txKey);
+                }
+            }
+        }
+
+        return res;
+    }
+
+    /**
+     * @param tx Tx. Must be instance of {@link GridNearTxLocal}.
+     * @return Colocated future.
+     */
+    private GridDhtColocatedLockFuture colocatedLockFuture(IgniteInternalTx tx) {
+        assert tx instanceof GridNearTxLocal : tx;
+
+        Collection<GridCacheMvccFuture<?>> futs = cctx.mvcc().mvccFutures(tx.nearXidVersion());
+
+        if (futs != null) {
+            for (GridCacheMvccFuture<?> fut : futs) {
+                if (fut instanceof GridDhtColocatedLockFuture)
+                    return (GridDhtColocatedLockFuture)fut;
+            }
+        }
+
+        return null;
+    }
+
+    /**
+     * @param fut Future.
+     */
+    public void addFuture(TxDeadlockFuture fut) {
+        TxDeadlockFuture old = deadlockDetectFuts.put(fut.futureId(), fut);
+
+        assert old == null : old;
+    }
+
+    /**
+     * @param futId Future ID.
+     * @return Found future.
+     */
+    @Nullable public TxDeadlockFuture future(long futId) {
+        return deadlockDetectFuts.get(futId);
+    }
+
+    /**
+     * @param futId Future ID.
+     */
+    public void removeFuture(long futId) {
+        deadlockDetectFuts.remove(futId);
+    }
+
+    /**
      * Timeout object for node failure handler.
      */
     private final class NodeFailureTimeoutObject extends GridTimeoutObjectAdapter {
@@ -2037,4 +2293,144 @@ public class IgniteTxManager extends GridCacheSharedManagerAdapter {
             }
         }
     }
+
+    /**
+     * Transactions deadlock detection process message listener.
+     */
+    private class DeadlockDetectionListener implements GridMessageListener {
+        /** {@inheritDoc} */
+        @SuppressWarnings("unchecked")
+        @Override public void onMessage(UUID nodeId, Object msg) {
+            GridCacheMessage cacheMsg = (GridCacheMessage)msg;
+
+            unmarshall(nodeId, cacheMsg);
+
+            if (cacheMsg.classError() != null) {
+                try {
+                    processFailedMessage(nodeId, cacheMsg);
+                }
+                catch(Throwable e){
+                    U.error(log, "Failed to process message [senderId=" + nodeId +
+                        ", messageType=" + cacheMsg.getClass() + ']', e);
+
+                    if (e instanceof Error)
+                        throw (Error)e;
+                }
+            }
+            else {
+                if (log.isDebugEnabled())
+                    log.debug("Message received [locNodeId=" + cctx.localNodeId() +
+                        ", rmtNodeId=" + nodeId + ", msg=" + msg + ']');
+
+                if (msg instanceof TxLocksRequest) {
+                    TxLocksRequest req = (TxLocksRequest)msg;
+
+                    TxLocksResponse res = txLocksInfo(req.txKeys());
+
+                    res.futureId(req.futureId());
+
+                    try {
+                        if (!cctx.localNodeId().equals(nodeId))
+                            res.prepareMarshal(cctx);
+
+                        cctx.gridIO().send(nodeId, TOPIC_TX, res, SYSTEM_POOL);
+                    }
+                    catch (IgniteCheckedException e) {
+                        U.error(log, "Failed to send response to node [node=" + nodeId + ", res=" + res + ']', e);
+                    }
+                }
+                else if (msg instanceof TxLocksResponse) {
+                    TxLocksResponse res = (TxLocksResponse)msg;
+
+                    long futId = res.futureId();
+
+                    TxDeadlockFuture fut = future(futId);
+
+                    if (fut != null)
+                        fut.onResult(nodeId, res);
+                    else
+                        U.warn(log, "Unexpected response received " + res);
+                }
+                else
+                    throw new IllegalArgumentException("Unknown message [msg=" + msg + ']');
+            }
+        }
+
+        /**
+         * @param nodeId Node ID.
+         * @param msg Message.
+         */
+        private void processFailedMessage(UUID nodeId, GridCacheMessage msg) throws IgniteCheckedException {
+            switch (msg.directType()) {
+                case -24: {
+                    TxLocksRequest req = (TxLocksRequest)msg;
+
+                    TxLocksResponse res = new TxLocksResponse();
+
+                    res.futureId(req.futureId());
+
+                    try {
+                        cctx.gridIO().send(nodeId, TOPIC_TX, res, SYSTEM_POOL);
+                    }
+                    catch (IgniteCheckedException e) {
+                        U.error(log, "Failed to send response to node (is node still alive?) [nodeId=" + nodeId +
+                            ", res=" + res + ']', e);
+                    }
+                }
+
+                break;
+
+                case -23: {
+                    TxLocksResponse res = (TxLocksResponse)msg;
+
+                    TxDeadlockFuture fut = future(res.futureId());
+
+                    if (fut == null) {
+                        if (log.isDebugEnabled())
+                            log.debug("Failed to find future for response [sender=" + nodeId + ", res=" + res + ']');
+
+                        return;
+                    }
+
+                    fut.onResult(nodeId, res);
+                }
+
+                break;
+
+                default:
+                    throw new IgniteCheckedException("Failed to process message. Unsupported direct type [msg=" +
+                        msg + ']', msg.classError());
+            }
+
+        }
+
+        /**
+         * @param nodeId Sender node ID.
+         * @param cacheMsg Message.
+         */
+        private void unmarshall(UUID nodeId, GridCacheMessage cacheMsg) {
+            if (cctx.localNodeId().equals(nodeId))
+                return;
+
+            try {
+                cacheMsg.finishUnmarshal(cctx, cctx.deploy().globalLoader());
+            }
+            catch (IgniteCheckedException e) {
+                cacheMsg.onClassError(e);
+            }
+            catch (BinaryObjectException e) {
+                cacheMsg.onClassError(new IgniteCheckedException(e));
+            }
+            catch (Error e) {
+                if (cacheMsg.ignoreClassErrors() &&
+                    X.hasCause(e, NoClassDefFoundError.class, UnsupportedClassVersionError.class)) {
+                    cacheMsg.onClassError(
+                        new IgniteCheckedException("Failed to load class during unmarshalling: " + e, e)
+                    );
+                }
+                else
+                    throw e;
+            }
+        }
+    }
 }