You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by sb...@apache.org on 2015/01/16 12:29:16 UTC

[1/7] incubator-ignite git commit: IGNITE-49 Implemented CacheStatisticsMXBean#getCacheEvictions()

Repository: incubator-ignite
Updated Branches:
  refs/heads/ignite-49 [created] 996cc086a


IGNITE-49 Implemented CacheStatisticsMXBean#getCacheEvictions()


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

Branch: refs/heads/ignite-49
Commit: c04013d9b89e94fba80bdcae3723cc337cdf09ea
Parents: 70d98c2
Author: nikolay_tikhonov <nt...@gridgain.com>
Authored: Fri Dec 26 12:58:51 2014 +0300
Committer: nikolay_tikhonov <nt...@gridgain.com>
Committed: Fri Jan 16 14:13:33 2015 +0300

----------------------------------------------------------------------
 .../cache/GridCacheEvictionManager.java         |   2 +
 .../cache/GridCacheMetricsAdapter.java          |  51 +--
 .../processors/cache/GridCacheTtlManager.java   |   2 +
 .../cache/GridCacheAbstractMetricsSelfTest.java | 309 ++++++++++++++++++-
 4 files changed, 344 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c04013d9/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheEvictionManager.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheEvictionManager.java b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheEvictionManager.java
index d05a347..6894ed0 100644
--- a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheEvictionManager.java
+++ b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheEvictionManager.java
@@ -668,6 +668,8 @@ public class GridCacheEvictionManager<K, V> extends GridCacheManagerAdapter<K, V
 
             cache.removeEntry(entry);
 
+            cache.metrics0().onEvict();
+
             if (recordable)
                 cctx.events().addEvent(entry.partition(), entry.key(), cctx.nodeId(), (IgniteUuid)null, null,
                     EVT_CACHE_ENTRY_EVICTED, null, false, oldVal, hasVal, null, null, null);

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c04013d9/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
index 63899f8..4849fa5 100644
--- a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
+++ b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
@@ -64,8 +64,10 @@ public class GridCacheMetricsAdapter implements GridCacheMetrics, Externalizable
     /** Number of transaction rollbacks. */
     private volatile int txRollbacks;
 
-    private volatile long getCnt;
-    private volatile long putCnt;
+    /** Number of evictions. */
+    private volatile long evictCnt;
+
+    /** Number of removed entries. */
     private volatile long rmCnt;
 
     /** Cache metrics. */
@@ -95,6 +97,7 @@ public class GridCacheMetricsAdapter implements GridCacheMetrics, Externalizable
         txCommits = m.txCommits();
         txRollbacks = m.txRollbacks();
         rmCnt = m.getCacheRemovals();
+        evictCnt = m.getCacheEvictions();
     }
 
     /**
@@ -161,7 +164,6 @@ public class GridCacheMetricsAdapter implements GridCacheMetrics, Externalizable
 
     /** {@inheritDoc} */
     @Override public void clear() {
-        //TODO: rewrite
         createTime = U.currentTimeMillis();
         readTime = createTime;
         writeTime = createTime;
@@ -207,39 +209,36 @@ public class GridCacheMetricsAdapter implements GridCacheMetrics, Externalizable
 
     /** {@inheritDoc} */
     @Override public long getCacheGets() {
-        //TODO: split read
-        return 0;
+        return reads;
     }
 
     /** {@inheritDoc} */
     @Override public long getCachePuts() {
-        //TODO: split write
-        return 0;
+        return writes;
     }
 
-    @Override
-    public long getCacheRemovals() {
+    /** {@inheritDoc} */
+    @Override public long getCacheRemovals() {
         return rmCnt;
     }
 
-    @Override
-    public long getCacheEvictions() {
-        //TODO: discuss with Semen
-        return 0;
+    /** {@inheritDoc} */
+    @Override public long getCacheEvictions() {
+        return evictCnt;
     }
 
-    @Override
-    public float getAverageGetTime() {
+    /** {@inheritDoc} */
+    @Override public float getAverageGetTime() {
         return 0;
     }
 
-    @Override
-    public float getAveragePutTime() {
+    /** {@inheritDoc} */
+    @Override public float getAveragePutTime() {
         return 0;
     }
 
-    @Override
-    public float getAverageRemoveTime() {
+    /** {@inheritDoc} */
+    @Override public float getAverageRemoveTime() {
         return 0;
     }
 
@@ -286,6 +285,16 @@ public class GridCacheMetricsAdapter implements GridCacheMetrics, Externalizable
     }
 
     /**
+     * Cache remove callback.
+     */
+    public void onEvict() {
+        evictCnt++;
+
+        if (delegate != null)
+            delegate.onEvict();
+    }
+
+    /**
      * Transaction commit callback.
      */
     public void onTxCommit() {
@@ -336,6 +345,8 @@ public class GridCacheMetricsAdapter implements GridCacheMetrics, Externalizable
         out.writeInt(misses);
         out.writeInt(txCommits);
         out.writeInt(txRollbacks);
+        out.writeLong(rmCnt);
+        out.writeLong(evictCnt);
     }
 
     /** {@inheritDoc} */
@@ -352,6 +363,8 @@ public class GridCacheMetricsAdapter implements GridCacheMetrics, Externalizable
         misses = in.readInt();
         txCommits = in.readInt();
         txRollbacks = in.readInt();
+        rmCnt = in.readLong();
+        evictCnt = in.readLong();
     }
 
     /** {@inheritDoc} */

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c04013d9/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheTtlManager.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheTtlManager.java b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheTtlManager.java
index 20791e2..33ead3a 100644
--- a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheTtlManager.java
+++ b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheTtlManager.java
@@ -127,6 +127,8 @@ public class GridCacheTtlManager<K, V> extends GridCacheManagerAdapter<K, V> {
                         if (wrapper.entry.onTtlExpired(obsoleteVer))
                             wrapper.entry.context().cache().removeEntry(wrapper.entry);
 
+                        wrapper.entry.context().cache().metrics0().onEvict();
+
                         it.remove();
                     }
                     else

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/c04013d9/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
index 33fa252..5173837 100644
--- a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
+++ b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
@@ -18,7 +18,16 @@
 package org.gridgain.grid.kernal.processors.cache;
 
 import org.apache.ignite.*;
+import org.apache.ignite.transactions.*;
 import org.gridgain.grid.cache.*;
+import org.gridgain.grid.util.lang.*;
+import org.gridgain.grid.util.typedef.internal.*;
+import org.gridgain.testframework.*;
+
+import javax.cache.expiry.*;
+import java.util.*;
+
+import static java.util.concurrent.TimeUnit.*;
 
 /**
  * Cache metrics test.
@@ -134,6 +143,41 @@ public abstract class GridCacheAbstractMetricsSelfTest extends GridCacheAbstract
         assertEquals(expMisses, misses);
     }
 
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testMissHitPercentage() throws Exception {
+        GridCache<Integer, Integer> cache0 = grid(0).cache(null);
+
+        int keyCnt = keyCount();
+
+        // Put and get a few keys.
+        for (int i = 0; i < keyCnt; i++) {
+            cache0.put(i, i); // +1 read
+
+            info("Writes: " + cache0.metrics().writes());
+
+            for (int j = 0; j < gridCount(); j++) {
+                GridCache<Integer, Integer> cache = grid(j).cache(null);
+
+                int cacheWrites = cache.metrics().writes();
+
+                assertEquals("Wrong cache metrics [i=" + i + ", grid=" + j + ']', i + 1, cacheWrites);
+            }
+
+            assertEquals("Wrong value for key: " + i, Integer.valueOf(i), cache0.get(i)); // +1 read
+        }
+
+        // Check metrics for the whole cache.
+        for (int i = 0; i < gridCount(); i++) {
+            GridCacheMetrics m = grid(i).cache(null).metrics();
+
+            assertEquals(m.getCacheHits() * 100f / m.getCacheGets(), m.getCacheHitPercentage(), 0.1f);
+            assertEquals(m.getCacheMisses() * 100f / m.getCacheGets(), m.getCacheMissPercentage(), 0.1f);
+        }
+    }
+
     /**
      * @throws Exception If failed.
      */
@@ -216,6 +260,9 @@ public abstract class GridCacheAbstractMetricsSelfTest extends GridCacheAbstract
         assertEquals("Expected 1 hit", 1, cache.metrics().hits());
     }
 
+    /**
+     * @throws Exception If failed.
+     */
     public void testRemoves() throws Exception {
         GridCache<Integer, Integer> cache = grid(0).cache(null);
 
@@ -224,6 +271,266 @@ public abstract class GridCacheAbstractMetricsSelfTest extends GridCacheAbstract
         // +1 remove
         cache.remove(1);
 
-        assert cache.metrics().getCacheRemovals() == 1;
+        assertEquals(1L, cache.metrics().getCacheRemovals());
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testManualEvictions() throws Exception {
+        GridCache<Integer, Integer> cache = grid(0).cache(null);
+
+        if (cache.configuration().getCacheMode() == GridCacheMode.PARTITIONED)
+            return;
+
+        cache.put(1, 1);
+
+        cache.evict(1);
+
+        assertEquals(0L, cache.metrics().getCacheRemovals());
+        assertEquals(1L, cache.metrics().getCacheEvictions());
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testTxEvictions() throws Exception {
+        if (grid(0).cache(null).configuration().getAtomicityMode() != GridCacheAtomicityMode.ATOMIC)
+            checkTtl(true);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testNonTxEvictions() throws Exception {
+        if (grid(0).cache(null).configuration().getAtomicityMode() == GridCacheAtomicityMode.ATOMIC)
+            checkTtl(false);
+    }
+
+    /**
+     * @param inTx
+     * @throws Exception If failed.
+     */
+    private void checkTtl(boolean inTx) throws Exception {
+        int ttl = 1000;
+
+        final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
+
+        final GridCache<Integer, Integer> c = grid(0).cache(null);
+
+        final Integer key = primaryKeysForCache(c, 1, 0).get(0);
+
+        c.put(key, 1);
+
+        GridCacheEntry<Integer, Integer> entry = c.entry(key);
+
+        assert entry != null;
+
+        assertEquals(0, entry.timeToLive());
+        assertEquals(0, entry.expirationTime());
+        assertEquals(0, grid(0).cache(null).metrics().getCacheEvictions());
+
+        long startTime = System.currentTimeMillis();
+
+        if (inTx) {
+            // Rollback transaction for the first time.
+            IgniteTx tx = grid(0).transactions().txStart();
+
+            try {
+                grid(0).jcache(null).withExpiryPolicy(expiry).put(key, 1);
+            }
+            finally {
+                tx.rollback();
+            }
+
+            assertEquals(0, entry.timeToLive());
+            assertEquals(0, entry.expirationTime());
+        }
+
+        // Now commit transaction and check that ttl and expire time have been saved.
+        IgniteTx tx = inTx ? c.txStart() : null;
+
+        try {
+            grid(0).jcache(null).withExpiryPolicy(expiry).put(key, 1);
+        }
+        finally {
+            if (tx != null)
+                tx.commit();
+        }
+
+        long[] expireTimes = new long[gridCount()];
+
+        for (int i = 0; i < gridCount(); i++) {
+            GridCacheEntry<Object, Object> curEntry = grid(i).cache(null).entry(key);
+
+            if (curEntry.primary() || curEntry.backup()) {
+                assertEquals(ttl, curEntry.timeToLive());
+
+                assert curEntry.expirationTime() > startTime;
+
+                expireTimes[i] = curEntry.expirationTime();
+            }
+        }
+
+        // One more update from the same cache entry to ensure that expire time is shifted forward.
+        U.sleep(100);
+
+        tx = inTx ? c.txStart() : null;
+
+        try {
+            grid(0).jcache(null).withExpiryPolicy(expiry).put(key, 2);
+        }
+        finally {
+            if (tx != null)
+                tx.commit();
+        }
+
+        for (int i = 0; i < gridCount(); i++) {
+            GridCacheEntry<Object, Object> curEntry = grid(i).cache(null).entry(key);
+
+            if (curEntry.primary() || curEntry.backup()) {
+                assertEquals(ttl, curEntry.timeToLive());
+
+                assert curEntry.expirationTime() > expireTimes[i];
+
+                expireTimes[i] = curEntry.expirationTime();
+            }
+        }
+
+        // And one more direct update to ensure that expire time is shifted forward.
+        U.sleep(100);
+
+        assertEquals(0, grid(0).cache(null).metrics().getCacheEvictions());
+
+        tx = inTx ? c.txStart() : null;
+
+        try {
+            grid(0).jcache(null).withExpiryPolicy(expiry).put(key, 3);
+        }
+        finally {
+            if (tx != null)
+                tx.commit();
+        }
+
+        for (int i = 0; i < gridCount(); i++) {
+            GridCacheEntry<Object, Object> curEntry = grid(i).cache(null).entry(key);
+
+            if (curEntry.primary() || curEntry.backup()) {
+                assertEquals(ttl, curEntry.timeToLive());
+
+                assert curEntry.expirationTime() > expireTimes[i];
+
+                expireTimes[i] = curEntry.expirationTime();
+            }
+        }
+
+        // And one more update to ensure that ttl is not changed and expire time is not shifted forward.
+        U.sleep(100);
+
+        assertEquals(0, grid(0).cache(null).metrics().getCacheEvictions());
+
+        log.info("Put 4");
+
+        tx = inTx ? c.txStart() : null;
+
+        try {
+            grid(0).jcache(null).put(key, 4);
+        }
+        finally {
+            if (tx != null)
+                tx.commit();
+        }
+
+        log.info("Put 4 done");
+
+        for (int i = 0; i < gridCount(); i++) {
+            GridCacheEntry<Object, Object> curEntry = grid(i).cache(null).entry(key);
+
+            if (curEntry.primary() || curEntry.backup()) {
+                assertEquals(ttl, curEntry.timeToLive());
+                assertEquals(expireTimes[i], curEntry.expirationTime());
+            }
+        }
+
+        assertEquals(0, grid(0).cache(null).metrics().getCacheEvictions());
+
+        // Avoid reloading from store.
+        map.remove(key);
+
+        assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicateX() {
+            @SuppressWarnings("unchecked")
+            @Override
+            public boolean applyx() throws IgniteCheckedException {
+                try {
+                    if (c.get(key) != null)
+                        return false;
+
+                    // Get "cache" field from GridCacheProxyImpl.
+                    GridCacheAdapter c0 = GridTestUtils.getFieldValue(c, "cache");
+
+                    if (!c0.context().deferredDelete()) {
+                        GridCacheEntryEx e0 = c0.peekEx(key);
+
+                        return e0 == null || (e0.rawGet() == null && e0.valueBytes() == null);
+                    } else
+                        return true;
+                } catch (GridCacheEntryRemovedException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+        }, Math.min(ttl * 10, getTestTimeout())));
+
+        // Ensure that old TTL and expire time are not longer "visible".
+        entry = c.entry(key);
+
+        assertEquals(0, entry.timeToLive());
+        assertEquals(0, entry.expirationTime());
+
+        // Ensure that next update will not pick old expire time.
+
+        tx = inTx ? c.txStart() : null;
+
+        try {
+            entry.set(10);
+        }
+        finally {
+            if (tx != null)
+                tx.commit();
+        }
+
+        U.sleep(2000);
+
+        entry = c.entry(key);
+
+        assertEquals((Integer)10, entry.get());
+
+        assertEquals(0, entry.timeToLive());
+        assertEquals(0, entry.expirationTime());
+
+        if (c.configuration().getCacheMode() != GridCacheMode.PARTITIONED && inTx)
+            assertEquals(1, grid(0).cache(null).metrics().getCacheEvictions());
+    }
+
+    /**
+     * @param cache Cache.
+     * @param cnt Keys count.
+     * @param startFrom Start value for keys search.
+     * @return Collection of keys for which given cache is primary.
+     * @throws IgniteCheckedException If failed.
+     */
+    protected List<Integer> primaryKeysForCache(GridCacheProjection<Integer, Integer> cache, int cnt, int startFrom)
+            throws IgniteCheckedException {
+        List<Integer> found = new ArrayList<>(cnt);
+
+        for (int i = startFrom; i < startFrom + 100_000; i++) {
+            if (cache.entry(i).primary()) {
+                found.add(i);
+
+                if (found.size() == cnt)
+                    return found;
+            }
+        }
+
+        throw new IgniteCheckedException("Unable to find " + cnt + " keys as primary for cache.");
     }
 }


[7/7] incubator-ignite git commit: IGNITE-49 Fixed tck tests.

Posted by sb...@apache.org.
IGNITE-49 Fixed tck tests.


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

Branch: refs/heads/ignite-49
Commit: 996cc086ac22b708a5b63cb9a9414e7fe58365fa
Parents: 984a3de
Author: nikolay_tikhonov <nt...@gridgain.com>
Authored: Fri Jan 16 14:13:00 2015 +0300
Committer: nikolay_tikhonov <nt...@gridgain.com>
Committed: Fri Jan 16 14:26:00 2015 +0300

----------------------------------------------------------------------
 .../org/apache/ignite/IgniteCacheMXBean.java    |  4 ++
 .../ignite/cache/CacheConfigurationMXBean.java  | 71 --------------------
 .../apache/ignite/cache/CacheMetricsMXBean.java | 29 ++++----
 3 files changed, 21 insertions(+), 83 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/996cc086/modules/core/src/main/java/org/apache/ignite/IgniteCacheMXBean.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/IgniteCacheMXBean.java b/modules/core/src/main/java/org/apache/ignite/IgniteCacheMXBean.java
index 26cace6..1d91442 100644
--- a/modules/core/src/main/java/org/apache/ignite/IgniteCacheMXBean.java
+++ b/modules/core/src/main/java/org/apache/ignite/IgniteCacheMXBean.java
@@ -21,8 +21,12 @@ import javax.cache.*;
 import javax.cache.configuration.*;
 import javax.cache.management.*;
 
+
 /**
+ * Implementation of {@link CacheMXBean}.
  *
+ * It's a simple wrapper around {@link CompleteConfiguration CompleteConfiguration} for readonly
+ * access to cache configuration.
  */
 public class IgniteCacheMXBean implements CacheMXBean {
     /** */

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/996cc086/modules/core/src/main/java/org/apache/ignite/cache/CacheConfigurationMXBean.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/cache/CacheConfigurationMXBean.java b/modules/core/src/main/java/org/apache/ignite/cache/CacheConfigurationMXBean.java
deleted file mode 100644
index 070780a..0000000
--- a/modules/core/src/main/java/org/apache/ignite/cache/CacheConfigurationMXBean.java
+++ /dev/null
@@ -1,71 +0,0 @@
-// @java.file.header
-
-/*  _________        _____ __________________        _____
- *  __  ____/___________(_)______  /__  ____/______ ____(_)_______
- *  _  / __  __  ___/__  / _  __  / _  / __  _  __ `/__  / __  __ \
- *  / /_/ /  _  /    _  /  / /_/ /  / /_/ /  / /_/ / _  /  _  / / /
- *  \____/   /_/     /_/   \_,__/   \____/   \__,_/  /_/   /_/ /_/
- */
-
-package org.apache.ignite.cache;
-
-import org.gridgain.grid.cache.GridCacheConfiguration;
-
-import javax.cache.management.*;
-
-/**
- * Implementation of {@link CacheMXBean}.
- *
- * It's a simple wrapper around {@link GridCacheConfiguration} for readonly
- * access to cache configuration.
- */
-public class CacheConfigurationMXBean implements CacheMXBean {
-    /**
-     *
-     */
-    private final GridCacheConfiguration cacheCfg;
-
-    /**
-     * Constructor.
-     *
-     * @param cacheCfg The cache configuration.
-     */
-    public CacheConfigurationMXBean(GridCacheConfiguration cacheCfg) {
-        this.cacheCfg = cacheCfg;
-    }
-
-    /** {@inheritDoc} */
-    @Override public String getKeyType() {
-        return cacheCfg.getKeyType().getName();
-    }
-
-    /** {@inheritDoc} */
-    @Override public String getValueType() {
-        return cacheCfg.getValueType().getName();
-    }
-
-    /** {@inheritDoc} */
-    @Override public boolean isReadThrough() {
-        return cacheCfg.isReadThrough();
-    }
-
-    /** {@inheritDoc} */
-    @Override public boolean isWriteThrough() {
-        return cacheCfg.isWriteThrough();
-    }
-
-    /** {@inheritDoc} */
-    @Override public boolean isStoreByValue() {
-        return cacheCfg.isStoreByValue();
-    }
-
-    /** {@inheritDoc} */
-    @Override public boolean isStatisticsEnabled() {
-        return cacheCfg.isStatisticsEnabled();
-    }
-
-    /** {@inheritDoc} */
-    @Override public boolean isManagementEnabled() {
-        return cacheCfg.isManagementEnabled();
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/996cc086/modules/core/src/main/java/org/apache/ignite/cache/CacheMetricsMXBean.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/cache/CacheMetricsMXBean.java b/modules/core/src/main/java/org/apache/ignite/cache/CacheMetricsMXBean.java
index 92fa80e..85fc1f8 100644
--- a/modules/core/src/main/java/org/apache/ignite/cache/CacheMetricsMXBean.java
+++ b/modules/core/src/main/java/org/apache/ignite/cache/CacheMetricsMXBean.java
@@ -1,15 +1,23 @@
-/* @java.file.header */
-
-/*  _________        _____ __________________        _____
- *  __  ____/___________(_)______  /__  ____/______ ____(_)_______
- *  _  / __  __  ___/__  / _  __  / _  / __  _  __ `/__  / __  __ \
- *  / /_/ /  _  /    _  /  / /_/ /  / /_/ /  / /_/ / _  /  _  / / /
- *  \____/   /_/     /_/   \_,__/   \____/   \__,_/  /_/   /_/ /_/
+/*
+ * 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.cache;
 
-import org.gridgain.grid.cache.GridCache;
+import org.gridgain.grid.cache.*;
 
 import javax.cache.management.*;
 
@@ -17,10 +25,7 @@ import javax.cache.management.*;
  * Implementation of {@link CacheStatisticsMXBean}.
  */
 public class CacheMetricsMXBean implements CacheStatisticsMXBean {
-
-    /**
-     * Grid cache.
-     */
+    /** Grid cache. */
     private final GridCache cache;
 
     /**


[3/7] incubator-ignite git commit: IGNITE-49 Implemented MXBean

Posted by sb...@apache.org.
IGNITE-49 Implemented MXBean


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

Branch: refs/heads/ignite-49
Commit: 31733531e1324b4419e072bdfc6567aa13992b6c
Parents: d8ecf41
Author: nikolay_tikhonov <nt...@gridgain.com>
Authored: Mon Jan 12 12:05:06 2015 +0300
Committer: nikolay_tikhonov <nt...@gridgain.com>
Committed: Fri Jan 16 14:13:34 2015 +0300

----------------------------------------------------------------------
 .../org/apache/ignite/IgniteCacheManager.java   | 119 ++++++++++++++++++-
 .../ignite/cache/CacheConfigurationMXBean.java  |  33 ++---
 .../apache/ignite/cache/CacheMetricsMXBean.java |  94 +++++++++++++++
 .../cache/GridCacheMetricsAdapter.java          |   1 -
 .../cache/GridCacheAbstractMetricsSelfTest.java |  80 +++++++++++--
 5 files changed, 301 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/31733531/modules/core/src/main/java/org/apache/ignite/IgniteCacheManager.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/IgniteCacheManager.java b/modules/core/src/main/java/org/apache/ignite/IgniteCacheManager.java
index fa4530b..dbe6838 100644
--- a/modules/core/src/main/java/org/apache/ignite/IgniteCacheManager.java
+++ b/modules/core/src/main/java/org/apache/ignite/IgniteCacheManager.java
@@ -316,14 +316,127 @@ public class IgniteCacheManager implements CacheManager {
         }
     }
 
+
     /** {@inheritDoc} */
     @Override public void enableStatistics(String cacheName, boolean enabled) {
-        ensureNotClosed();
+        Ignite ignite = findIgnite(cacheName);
+
+        GridCache cache = ignite.cache(cacheName);
+
+        GridCacheConfiguration configuration = ignite.jcache(cacheName).getConfiguration(GridCacheConfiguration.class);
+
+        MBeanServer mBeanSrv = ignite.configuration().getMBeanServer();
 
         if (cacheName == null)
-            throw new NullPointerException();
+            cacheName = "null";
+
+        if (enabled) {
+            CacheMetricsMXBean mxBean = new CacheMetricsMXBean(cache);
+
+            registerCacheObject(mBeanSrv, mxBean, uri.toString(), cacheName, true);
+
+            configuration.setStatisticsEnabled(true);
+        }
+        else {
+            unregisterCacheObject(mBeanSrv, uri.toString(), cacheName, true);
+
+            configuration.setStatisticsEnabled(false);
+        }
+    }
+
+    /**
+     *
+     */
+    private static String mbeanSafe(String string) {
+        return string == null ? "" : string.replaceAll(",|:|=|\n", ".");
+    }
+
+    /**
+     * Finds node which has the cache.
+     *
+     * @param cacheName Cache name.
+     * @return Ignite node.
+     */
+    private Ignite findIgnite(String cacheName) {
+        for (Ignite ignite : Ignition.allGrids())
+            if (ignite.jcache(cacheName) != null)
+                return ignite;
+
+        return null;
+    }
+
+    /**
+     * @param mxbean MXBean.
+     * @param cacheManagerName name generated by URI and classloader.
+     * @param name cache name.
+     */
+    public static void registerCacheObject(MBeanServer mBeanServer, Object mxbean, String cacheManagerName, String name,
+                                           boolean stats) {
+        ObjectName registeredObjectName = calculateObjectName(cacheManagerName, name, stats);
 
-        throw new UnsupportedOperationException();
+        try {
+            if (!isRegistered(mBeanServer, registeredObjectName)) {
+                mBeanServer.registerMBean(mxbean, registeredObjectName);
+
+                System.out.println("Registered mbean with name " + registeredObjectName.toString());
+            }
+        }
+        catch (Exception e) {
+            throw new CacheException("Error registering cache MXBeans for CacheManager " + registeredObjectName
+                    + " . Error was " + e.getMessage(), e);
+        }
+    }
+
+    /**
+     * @return {@code True} if MBean registered.
+     */
+    private static boolean isRegistered(MBeanServer mBeanServer, ObjectName objectName) {
+        return !mBeanServer.queryNames(objectName, null).isEmpty();
+    }
+
+    /**
+     * UnRegisters the mxbean if registered already.
+     *
+     * @param mBeanSrv MBean server
+     * @param cacheManagerName name generated by URI and classloader.
+     * @param name cache name.
+     * @param stats is mxbean, a statistics mxbean.
+     */
+    public static void unregisterCacheObject(MBeanServer mBeanSrv, String cacheManagerName, String name, boolean stats) {
+        Set<ObjectName> registeredObjectNames = null;
+
+        ObjectName objectName = calculateObjectName(cacheManagerName, name, stats);
+        registeredObjectNames = mBeanSrv.queryNames(objectName, null);
+
+        System.out.println("Try UnRegistered mbean with name " + objectName.toString());
+
+        //should just be one
+        for (ObjectName registeredObjectName : registeredObjectNames) {
+            try {
+                mBeanSrv.unregisterMBean(registeredObjectName);
+            } catch (Exception e) {
+                throw new CacheException("Error unregistering object instance " + registeredObjectName
+                        + " . Error was " + e.getMessage(), e);
+            }
+        }
+    }
+
+    /**
+     * Creates an object name.
+     */
+    private static ObjectName calculateObjectName(String cacheManagerName, String name, boolean stats) {
+        String cacheManagerNameSafe = mbeanSafe(cacheManagerName);
+        String cacheName = mbeanSafe(name);
+
+        try {
+            String objectNameType = stats ? "Statistics" : "Configuration";
+            return new ObjectName(
+                    "javax.cache:type=Cache" + objectNameType + ",CacheManager=" + cacheManagerNameSafe + ",Cache=" + cacheName);
+        } catch (MalformedObjectNameException e) {
+            throw new CacheException(
+                    "Illegal ObjectName for Management Bean. " + "CacheManager=[" + cacheManagerNameSafe + "], Cache=["
+                            + cacheName + "]", e);
+        }
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/31733531/modules/core/src/main/java/org/apache/ignite/cache/CacheConfigurationMXBean.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/cache/CacheConfigurationMXBean.java b/modules/core/src/main/java/org/apache/ignite/cache/CacheConfigurationMXBean.java
index 2f04cc8..070780a 100644
--- a/modules/core/src/main/java/org/apache/ignite/cache/CacheConfigurationMXBean.java
+++ b/modules/core/src/main/java/org/apache/ignite/cache/CacheConfigurationMXBean.java
@@ -9,60 +9,63 @@
 
 package org.apache.ignite.cache;
 
-import javax.cache.*;
-import javax.cache.configuration.*;
+import org.gridgain.grid.cache.GridCacheConfiguration;
+
 import javax.cache.management.*;
 
 /**
- * TODO: Add class description.
+ * Implementation of {@link CacheMXBean}.
+ *
+ * It's a simple wrapper around {@link GridCacheConfiguration} for readonly
+ * access to cache configuration.
  */
-public class CacheConfigurationMXBean<K, V> implements CacheMXBean {
+public class CacheConfigurationMXBean implements CacheMXBean {
     /**
      *
      */
-    private final Cache<K, V> cache;
+    private final GridCacheConfiguration cacheCfg;
 
     /**
      * Constructor.
      *
-     * @param cache The cache.
+     * @param cacheCfg The cache configuration.
      */
-    public CacheConfigurationMXBean(Cache<K, V> cache) {
-        this.cache = cache;
+    public CacheConfigurationMXBean(GridCacheConfiguration cacheCfg) {
+        this.cacheCfg = cacheCfg;
     }
 
     /** {@inheritDoc} */
     @Override public String getKeyType() {
-        return cache.getConfiguration(CompleteConfiguration.class).getKeyType().getName();
+        return cacheCfg.getKeyType().getName();
     }
 
     /** {@inheritDoc} */
     @Override public String getValueType() {
-        return cache.getConfiguration(CompleteConfiguration.class).getValueType().getName();
+        return cacheCfg.getValueType().getName();
     }
 
     /** {@inheritDoc} */
     @Override public boolean isReadThrough() {
-        return cache.getConfiguration(CompleteConfiguration.class).isReadThrough();
+        return cacheCfg.isReadThrough();
     }
 
     /** {@inheritDoc} */
     @Override public boolean isWriteThrough() {
-        return cache.getConfiguration(CompleteConfiguration.class).isWriteThrough();
+        return cacheCfg.isWriteThrough();
     }
 
     /** {@inheritDoc} */
     @Override public boolean isStoreByValue() {
-        return cache.getConfiguration(Configuration.class).isStoreByValue();
+        return cacheCfg.isStoreByValue();
     }
 
     /** {@inheritDoc} */
     @Override public boolean isStatisticsEnabled() {
-        return cache.getConfiguration(CompleteConfiguration.class).isStatisticsEnabled();
+        return cacheCfg.isStatisticsEnabled();
     }
 
     /** {@inheritDoc} */
     @Override public boolean isManagementEnabled() {
-        return cache.getConfiguration(CompleteConfiguration.class).isManagementEnabled();
+        return cacheCfg.isManagementEnabled();
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/31733531/modules/core/src/main/java/org/apache/ignite/cache/CacheMetricsMXBean.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/cache/CacheMetricsMXBean.java b/modules/core/src/main/java/org/apache/ignite/cache/CacheMetricsMXBean.java
new file mode 100644
index 0000000..92fa80e
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/cache/CacheMetricsMXBean.java
@@ -0,0 +1,94 @@
+/* @java.file.header */
+
+/*  _________        _____ __________________        _____
+ *  __  ____/___________(_)______  /__  ____/______ ____(_)_______
+ *  _  / __  __  ___/__  / _  __  / _  / __  _  __ `/__  / __  __ \
+ *  / /_/ /  _  /    _  /  / /_/ /  / /_/ /  / /_/ / _  /  _  / / /
+ *  \____/   /_/     /_/   \_,__/   \____/   \__,_/  /_/   /_/ /_/
+ */
+
+package org.apache.ignite.cache;
+
+import org.gridgain.grid.cache.GridCache;
+
+import javax.cache.management.*;
+
+/**
+ * Implementation of {@link CacheStatisticsMXBean}.
+ */
+public class CacheMetricsMXBean implements CacheStatisticsMXBean {
+
+    /**
+     * Grid cache.
+     */
+    private final GridCache cache;
+
+    /**
+     * Constructor.
+     *
+     * @param cache GridCache
+     */
+    public CacheMetricsMXBean(GridCache cache) {
+        this.cache = cache;
+    }
+
+    /** {@inheritDoc} */
+    @Override public void clear() {
+        cache.resetMetrics();
+    }
+
+    /** {@inheritDoc} */
+    @Override public long getCacheHits() {
+        return cache.metrics().hits();
+    }
+
+    /** {@inheritDoc} */
+    @Override public float getCacheHitPercentage() {
+        return cache.metrics().getCacheHitPercentage();
+    }
+
+    /** {@inheritDoc} */
+    @Override public long getCacheMisses() {
+        return cache.metrics().getCacheMisses();
+    }
+
+    /** {@inheritDoc} */
+    @Override public float getCacheMissPercentage() {
+        return cache.metrics().getCacheMissPercentage();
+    }
+
+    /** {@inheritDoc} */
+    @Override public long getCacheGets() {
+        return cache.metrics().getCacheGets();
+    }
+
+    /** {@inheritDoc} */
+    @Override public long getCachePuts() {
+        return cache.metrics().getCachePuts();
+    }
+
+    /** {@inheritDoc} */
+    @Override public long getCacheRemovals() {
+        return cache.metrics().getCacheRemovals();
+    }
+
+    /** {@inheritDoc} */
+    @Override public long getCacheEvictions() {
+        return cache.metrics().getCacheEvictions();
+    }
+
+    /** {@inheritDoc} */
+    @Override public float getAverageGetTime() {
+        return cache.metrics().getAverageGetTime();
+    }
+
+    /** {@inheritDoc} */
+    @Override public float getAveragePutTime() {
+        return cache.metrics().getAveragePutTime();
+    }
+
+    /** {@inheritDoc} */
+    @Override public float getAverageRemoveTime() {
+        return cache.metrics().getAverageRemoveTime();
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/31733531/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
index a878972..ae539d5 100644
--- a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
+++ b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
@@ -20,7 +20,6 @@ package org.gridgain.grid.kernal.processors.cache;
 import org.gridgain.grid.cache.*;
 import org.gridgain.grid.util.tostring.*;
 import org.gridgain.grid.util.typedef.internal.*;
-import org.jdk8.backport.*;
 import org.jetbrains.annotations.*;
 
 import java.io.*;

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/31733531/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
index 82aa122..74e6068 100644
--- a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
+++ b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
@@ -25,6 +25,8 @@ import org.gridgain.grid.util.typedef.internal.*;
 import org.gridgain.testframework.*;
 
 import javax.cache.expiry.*;
+import javax.management.*;
+import java.net.*;
 import java.util.*;
 
 import static java.util.concurrent.TimeUnit.*;
@@ -114,11 +116,11 @@ public abstract class GridCacheAbstractMetricsSelfTest extends GridCacheAbstract
 
         float times = (System.nanoTime() - start) * 1.f / 1000;
 
-        float averageRemoveTime = cache.metrics().getAverageRemoveTime();
+        float avgRmvTime = cache.metrics().getAverageRemoveTime();
 
-        assert averageRemoveTime > 0;
+        assert avgRmvTime > 0;
 
-        assertEquals(times, averageRemoveTime, times / 10);
+        assertEquals(times, avgRmvTime, times / 10);
 
         jcache.remove(2);
 
@@ -188,6 +190,71 @@ public abstract class GridCacheAbstractMetricsSelfTest extends GridCacheAbstract
     /**
      * @throws Exception If failed.
      */
+    public void testStatisticMXBean() throws Exception {
+        IgniteCache<Integer, Integer> jcache = grid(0).jcache(null);
+
+        URI uri = URI.create("ignite://default");
+
+        IgniteCacheManager cacheManager = new IgniteCacheManager(uri);
+
+        cacheManager.enableStatistics(null, true);
+
+        ObjectName objectName =
+            new ObjectName("javax.cache:type=CacheStatistics,CacheManager=ignite.//default,Cache=null");
+
+        long cachePuts = (long)getConfiguration().getMBeanServer().getAttribute(objectName, "CachePuts");
+
+        assertEquals(0L, cachePuts);
+
+        jcache.put(1, 1);
+
+        cachePuts = (long)getConfiguration().getMBeanServer().getAttribute(objectName, "CachePuts");
+
+        assertEquals(1, cachePuts);
+
+        cacheManager.enableStatistics(null, false);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testCacheMxBean() throws Exception {
+        URI uri = URI.create("ignite://default");
+
+        IgniteCacheManager cacheManager = new IgniteCacheManager(uri);
+
+        cacheManager.enableManagement(null, true);
+
+        ObjectName objectName =
+            new ObjectName("javax.cache:type=CacheConfiguration,CacheManager=ignite.//default,Cache=null");
+
+        String keyType = (String)getConfiguration().getMBeanServer().getAttribute(objectName, "KeyType");
+        assertEquals("java.lang.Object", keyType);
+
+        String valueType = (String)getConfiguration().getMBeanServer().getAttribute(objectName, "ValueType");
+        assertEquals("java.lang.Object", valueType);
+
+        boolean readThrough = (boolean)getConfiguration().getMBeanServer().getAttribute(objectName, "ReadThrough");
+        assertEquals(false, readThrough);
+
+        boolean writeThrough = (boolean)getConfiguration().getMBeanServer().getAttribute(objectName, "WriteThrough");
+        assertEquals(false, writeThrough);
+
+        boolean storeByValue = (boolean)getConfiguration().getMBeanServer().getAttribute(objectName, "StoreByValue");
+        assertEquals(true, storeByValue);
+
+        boolean isStat = (boolean)getConfiguration().getMBeanServer().getAttribute(objectName, "StatisticsEnabled");
+        assertEquals(true, isStat);
+
+        boolean isMng = (boolean)getConfiguration().getMBeanServer().getAttribute(objectName, "ManagementEnabled");
+        assertEquals(true, isMng);
+
+        cacheManager.enableManagement(null, false);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
     public void testGetAllAvgTime() throws Exception {
         IgniteCache<Integer, Integer> jcache = grid(0).jcache(null);
         GridCache<Object, Object> cache = grid(0).cache(null);
@@ -233,12 +300,12 @@ public abstract class GridCacheAbstractMetricsSelfTest extends GridCacheAbstract
 
         float times = (System.nanoTime() - start) * 1.f / 1000;
 
-        float averagePutTime = cache.metrics().getAveragePutTime();
+        float avgPutTime = cache.metrics().getAveragePutTime();
 
-        assert averagePutTime > 0;
+        assert avgPutTime > 0;
 
         assertEquals(1, cache.metrics().writes());
-        assertEquals(times, averagePutTime, times / 3);
+        assertEquals(times, avgPutTime, times / 3);
 
         jcache.put(2, 2);
 
@@ -333,7 +400,6 @@ public abstract class GridCacheAbstractMetricsSelfTest extends GridCacheAbstract
         assertEquals(expMisses, misses);
     }
 
-
     /**
      * @throws Exception If failed.
      */


[2/7] incubator-ignite git commit: IGNITE-49 Implemented CacheStatisticsMXBean#getCacheRemovals()

Posted by sb...@apache.org.
IGNITE-49 Implemented CacheStatisticsMXBean#getCacheRemovals()


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

Branch: refs/heads/ignite-49
Commit: 70d98c20922d1cb9c31c3ef7b7d131acbcea2459
Parents: 9e7f696
Author: nikolay_tikhonov <nt...@gridgain.com>
Authored: Wed Dec 24 12:00:34 2014 +0300
Committer: nikolay_tikhonov <nt...@gridgain.com>
Committed: Fri Jan 16 14:13:33 2015 +0300

----------------------------------------------------------------------
 .../ignite/cache/CacheConfigurationMXBean.java  |  68 ++++++++++++
 .../gridgain/grid/cache/GridCacheMetrics.java   |   3 +-
 .../processors/cache/GridCacheMapEntry.java     |  23 ++++-
 .../cache/GridCacheMetricsAdapter.java          | 103 ++++++++++++++++++-
 .../cache/GridCacheAbstractMetricsSelfTest.java |  11 ++
 5 files changed, 201 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/70d98c20/modules/core/src/main/java/org/apache/ignite/cache/CacheConfigurationMXBean.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/cache/CacheConfigurationMXBean.java b/modules/core/src/main/java/org/apache/ignite/cache/CacheConfigurationMXBean.java
new file mode 100644
index 0000000..2f04cc8
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/cache/CacheConfigurationMXBean.java
@@ -0,0 +1,68 @@
+// @java.file.header
+
+/*  _________        _____ __________________        _____
+ *  __  ____/___________(_)______  /__  ____/______ ____(_)_______
+ *  _  / __  __  ___/__  / _  __  / _  / __  _  __ `/__  / __  __ \
+ *  / /_/ /  _  /    _  /  / /_/ /  / /_/ /  / /_/ / _  /  _  / / /
+ *  \____/   /_/     /_/   \_,__/   \____/   \__,_/  /_/   /_/ /_/
+ */
+
+package org.apache.ignite.cache;
+
+import javax.cache.*;
+import javax.cache.configuration.*;
+import javax.cache.management.*;
+
+/**
+ * TODO: Add class description.
+ */
+public class CacheConfigurationMXBean<K, V> implements CacheMXBean {
+    /**
+     *
+     */
+    private final Cache<K, V> cache;
+
+    /**
+     * Constructor.
+     *
+     * @param cache The cache.
+     */
+    public CacheConfigurationMXBean(Cache<K, V> cache) {
+        this.cache = cache;
+    }
+
+    /** {@inheritDoc} */
+    @Override public String getKeyType() {
+        return cache.getConfiguration(CompleteConfiguration.class).getKeyType().getName();
+    }
+
+    /** {@inheritDoc} */
+    @Override public String getValueType() {
+        return cache.getConfiguration(CompleteConfiguration.class).getValueType().getName();
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isReadThrough() {
+        return cache.getConfiguration(CompleteConfiguration.class).isReadThrough();
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isWriteThrough() {
+        return cache.getConfiguration(CompleteConfiguration.class).isWriteThrough();
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isStoreByValue() {
+        return cache.getConfiguration(Configuration.class).isStoreByValue();
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isStatisticsEnabled() {
+        return cache.getConfiguration(CompleteConfiguration.class).isStatisticsEnabled();
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isManagementEnabled() {
+        return cache.getConfiguration(CompleteConfiguration.class).isManagementEnabled();
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/70d98c20/modules/core/src/main/java/org/gridgain/grid/cache/GridCacheMetrics.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/gridgain/grid/cache/GridCacheMetrics.java b/modules/core/src/main/java/org/gridgain/grid/cache/GridCacheMetrics.java
index c6a5302..d523c25 100644
--- a/modules/core/src/main/java/org/gridgain/grid/cache/GridCacheMetrics.java
+++ b/modules/core/src/main/java/org/gridgain/grid/cache/GridCacheMetrics.java
@@ -17,13 +17,14 @@
 
 package org.gridgain.grid.cache;
 
+import javax.cache.management.*;
 import java.io.*;
 
 /**
  * Cache metrics used to obtain statistics on cache itself.
  * Use {@link GridCache#metrics()} to obtain metrics for a cache.
  */
-public interface GridCacheMetrics extends Serializable {
+public interface GridCacheMetrics extends CacheStatisticsMXBean, Serializable {
     /**
      * Gets create time of the owning entity (either cache or entry).
      *

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/70d98c20/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMapEntry.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMapEntry.java b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMapEntry.java
index 9b8945a..46dc22d 100644
--- a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMapEntry.java
+++ b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMapEntry.java
@@ -1293,7 +1293,7 @@ public abstract class GridCacheMapEntry<K, V> implements GridCacheEntryEx<K, V>
                 drReplicate(drType, null, null, newVer);
 
                 if (metrics)
-                    cctx.cache().metrics0().onWrite();
+                    cctx.cache().metrics0().onRemove();
 
                 if (tx == null)
                     obsoleteVer = newVer;
@@ -1588,8 +1588,7 @@ public abstract class GridCacheMapEntry<K, V> implements GridCacheEntryEx<K, V>
                 res = hadVal;
             }
 
-            if (metrics)
-                cctx.cache().metrics0().onWrite();
+            updateMetrics(op, metrics);
 
             cctx.continuousQueries().onEntryUpdate(this, key, val, valueBytesUnlocked(), old, oldBytes);
 
@@ -2057,8 +2056,7 @@ public abstract class GridCacheMapEntry<K, V> implements GridCacheEntryEx<K, V>
                 newDrExpireTime = -1L;
             }
 
-            if (metrics)
-                cctx.cache().metrics0().onWrite();
+            updateMetrics(op, metrics);
 
             if (primary || cctx.isReplicated())
                 cctx.continuousQueries().onEntryUpdate(this, key, val, valueBytesUnlocked(), old, oldBytes);
@@ -4130,6 +4128,21 @@ public abstract class GridCacheMapEntry<K, V> implements GridCacheEntryEx<K, V>
     }
 
     /**
+     * Updates metrics.
+     *
+     * @param op Operation.
+     * @param metrics Update merics flag.
+     */
+    private void updateMetrics(GridCacheOperation op, boolean metrics) {
+        if (metrics) {
+            if (op == DELETE)
+                cctx.cache().metrics0().onRemove();
+            else
+                cctx.cache().metrics0().onWrite();
+        }
+    }
+
+    /**
      * @return TTL.
      */
     public long ttlExtras() {

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/70d98c20/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
index 9a01ef5..63899f8 100644
--- a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
+++ b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
@@ -64,6 +64,10 @@ public class GridCacheMetricsAdapter implements GridCacheMetrics, Externalizable
     /** Number of transaction rollbacks. */
     private volatile int txRollbacks;
 
+    private volatile long getCnt;
+    private volatile long putCnt;
+    private volatile long rmCnt;
+
     /** Cache metrics. */
     @GridToStringExclude
     private transient GridCacheMetricsAdapter delegate;
@@ -90,6 +94,7 @@ public class GridCacheMetricsAdapter implements GridCacheMetrics, Externalizable
         misses = m.misses();
         txCommits = m.txCommits();
         txRollbacks = m.txRollbacks();
+        rmCnt = m.getCacheRemovals();
     }
 
     /**
@@ -131,7 +136,7 @@ public class GridCacheMetricsAdapter implements GridCacheMetrics, Externalizable
 
     /** {@inheritDoc} */
     @Override public int writes() {
-        return writes;
+        return (int)(writes + rmCnt);
     }
 
     /** {@inheritDoc} */
@@ -154,6 +159,90 @@ public class GridCacheMetricsAdapter implements GridCacheMetrics, Externalizable
         return txRollbacks;
     }
 
+    /** {@inheritDoc} */
+    @Override public void clear() {
+        //TODO: rewrite
+        createTime = U.currentTimeMillis();
+        readTime = createTime;
+        writeTime = createTime;
+        commitTime = createTime;
+        rollbackTime = createTime;
+        reads = 0;
+        writes = 0;
+        hits = 0;
+        misses = 0;
+        txCommits = 0;
+        txRollbacks = 0;
+    }
+
+    /** {@inheritDoc} */
+    @Override public long getCacheHits() {
+        return hits;
+    }
+
+    /** {@inheritDoc} */
+    @Override public float getCacheHitPercentage() {
+        long hits0 = hits;
+
+        if (hits0 == 0)
+            return 0;
+
+        return (float) hits0 / getCacheGets() * 100.0f;
+    }
+
+    /** {@inheritDoc} */
+    @Override public long getCacheMisses() {
+        return misses;
+    }
+
+    /** {@inheritDoc} */
+    @Override public float getCacheMissPercentage() {
+        long misses0 = misses;
+
+        if (misses0 == 0)
+            return 0;
+
+        return (float) misses0 / getCacheGets() * 100.0f;
+    }
+
+    /** {@inheritDoc} */
+    @Override public long getCacheGets() {
+        //TODO: split read
+        return 0;
+    }
+
+    /** {@inheritDoc} */
+    @Override public long getCachePuts() {
+        //TODO: split write
+        return 0;
+    }
+
+    @Override
+    public long getCacheRemovals() {
+        return rmCnt;
+    }
+
+    @Override
+    public long getCacheEvictions() {
+        //TODO: discuss with Semen
+        return 0;
+    }
+
+    @Override
+    public float getAverageGetTime() {
+        return 0;
+    }
+
+    @Override
+    public float getAveragePutTime() {
+        return 0;
+    }
+
+    @Override
+    public float getAverageRemoveTime() {
+        return 0;
+    }
+
     /**
      * Cache read callback.
      * @param isHit Hit or miss flag.
@@ -185,6 +274,18 @@ public class GridCacheMetricsAdapter implements GridCacheMetrics, Externalizable
     }
 
     /**
+     * Cache remove callback.
+     */
+    public void onRemove(){
+        writeTime = U.currentTimeMillis();
+
+        rmCnt++;
+
+        if (delegate != null)
+            delegate.onRemove();
+    }
+
+    /**
      * Transaction commit callback.
      */
     public void onTxCommit() {

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/70d98c20/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
index 54a2b2d..33fa252 100644
--- a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
+++ b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
@@ -215,4 +215,15 @@ public abstract class GridCacheAbstractMetricsSelfTest extends GridCacheAbstract
         assertEquals("Expected 2 misses", 2, cache.metrics().misses());
         assertEquals("Expected 1 hit", 1, cache.metrics().hits());
     }
+
+    public void testRemoves() throws Exception {
+        GridCache<Integer, Integer> cache = grid(0).cache(null);
+
+        cache.put(1, 1);
+
+        // +1 remove
+        cache.remove(1);
+
+        assert cache.metrics().getCacheRemovals() == 1;
+    }
 }


[5/7] incubator-ignite git commit: IGNITE-49 Implemented disabled/enabled statistics.

Posted by sb...@apache.org.
IGNITE-49 Implemented disabled/enabled statistics.


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

Branch: refs/heads/ignite-49
Commit: 345936732622ed445082a535be352f000e0e745b
Parents: 3173353
Author: nikolay_tikhonov <nt...@gridgain.com>
Authored: Mon Jan 12 17:26:19 2015 +0300
Committer: nikolay_tikhonov <nt...@gridgain.com>
Committed: Fri Jan 16 14:13:35 2015 +0300

----------------------------------------------------------------------
 .../org/apache/ignite/IgniteCacheManager.java   | 107 ++++++-------------
 .../processors/cache/IgniteCacheProxy.java      |  42 ++++++--
 .../cache/GridCacheEvictionManager.java         |   3 +-
 .../processors/cache/GridCacheMapEntry.java     |  14 +--
 .../cache/GridCacheMetricsAdapter.java          |  12 +--
 .../processors/cache/GridCacheTtlManager.java   |   3 +-
 .../distributed/near/GridNearCacheEntry.java    |   3 +-
 .../distributed/near/GridNearGetFuture.java     |   8 +-
 .../distributed/near/GridNearLockFuture.java    |   6 +-
 .../local/atomic/GridLocalAtomicCache.java      |   2 +-
 .../cache/transactions/IgniteTxManager.java     |   6 +-
 .../cache/GridCacheAbstractMetricsSelfTest.java |  65 ++++++++---
 .../near/GridCacheNearMetricsSelfTest.java      |  11 ++
 13 files changed, 165 insertions(+), 117 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/34593673/modules/core/src/main/java/org/apache/ignite/IgniteCacheManager.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/IgniteCacheManager.java b/modules/core/src/main/java/org/apache/ignite/IgniteCacheManager.java
index dbe6838..c255517 100644
--- a/modules/core/src/main/java/org/apache/ignite/IgniteCacheManager.java
+++ b/modules/core/src/main/java/org/apache/ignite/IgniteCacheManager.java
@@ -17,6 +17,7 @@
 
 package org.apache.ignite;
 
+import org.apache.ignite.cache.CacheMetricsMXBean;
 import org.apache.ignite.configuration.*;
 import org.apache.ignite.lang.*;
 import org.gridgain.grid.cache.*;
@@ -147,6 +148,9 @@ public class IgniteCacheManager implements CacheManager {
         if (((CompleteConfiguration)cacheCfg).isManagementEnabled())
             enableManagement(cacheName, true);
 
+        if (((CompleteConfiguration)cacheCfg).isStatisticsEnabled())
+            enableStatistics(cacheName, true);
+
         return res;
     }
 
@@ -250,26 +254,21 @@ public class IgniteCacheManager implements CacheManager {
 
             }
 
-            ObjectName objName = getObjectName(cacheName);
-
             MBeanServer mBeanSrv = tuple.get1().configuration().getMBeanServer();
 
-            for (ObjectName n : mBeanSrv.queryNames(objName, null)) {
-                try {
-                    mBeanSrv.unregisterMBean(n);
-                }
-                catch (Exception ignored) {
+            unregisterCacheObject(mBeanSrv, cacheName, true);
 
-                }
-            }
+            unregisterCacheObject(mBeanSrv, cacheName, false);
         }
     }
 
     /**
      * @param cacheName Cache name.
      */
-    private ObjectName getObjectName(String cacheName) {
-        String mBeanName = "javax.cache:type=CacheConfiguration,CacheManager="
+    private ObjectName getObjectName(String cacheName, boolean isStatistic) {
+        String mbType = isStatistic ? "CacheStatistics" : "CacheConfiguration";
+
+        String mBeanName = "javax.cache:type=" + mbType + ",CacheManager="
             + uri.toString().replaceAll(",|:|=|\n", ".")
             + ",Cache=" + cacheName.replaceAll(",|:|=|\n", ".");
 
@@ -294,7 +293,7 @@ public class IgniteCacheManager implements CacheManager {
             tuple = igniteMap.get(cacheName);
         }
 
-        ObjectName objName = getObjectName(cacheName);
+        ObjectName objName = getObjectName(cacheName, false);
         MBeanServer mBeanSrv = tuple.get1().configuration().getMBeanServer();
 
         try {
@@ -319,7 +318,18 @@ public class IgniteCacheManager implements CacheManager {
 
     /** {@inheritDoc} */
     @Override public void enableStatistics(String cacheName, boolean enabled) {
-        Ignite ignite = findIgnite(cacheName);
+        ensureNotClosed();
+
+        if (cacheName == null)
+            throw new NullPointerException();
+
+        IgniteBiTuple<Ignite, IgniteCacheMXBean> tuple;
+
+        synchronized (igniteMap) {
+            tuple = igniteMap.get(cacheName);
+        }
+
+        Ignite ignite = tuple.get1();
 
         GridCache cache = ignite.cache(cacheName);
 
@@ -327,63 +337,34 @@ public class IgniteCacheManager implements CacheManager {
 
         MBeanServer mBeanSrv = ignite.configuration().getMBeanServer();
 
-        if (cacheName == null)
-            cacheName = "null";
-
         if (enabled) {
             CacheMetricsMXBean mxBean = new CacheMetricsMXBean(cache);
 
-            registerCacheObject(mBeanSrv, mxBean, uri.toString(), cacheName, true);
+            registerCacheObject(mBeanSrv, mxBean, cacheName, true);
 
             configuration.setStatisticsEnabled(true);
         }
         else {
-            unregisterCacheObject(mBeanSrv, uri.toString(), cacheName, true);
+            unregisterCacheObject(mBeanSrv, cacheName, true);
 
             configuration.setStatisticsEnabled(false);
         }
     }
 
     /**
-     *
-     */
-    private static String mbeanSafe(String string) {
-        return string == null ? "" : string.replaceAll(",|:|=|\n", ".");
-    }
-
-    /**
-     * Finds node which has the cache.
-     *
-     * @param cacheName Cache name.
-     * @return Ignite node.
-     */
-    private Ignite findIgnite(String cacheName) {
-        for (Ignite ignite : Ignition.allGrids())
-            if (ignite.jcache(cacheName) != null)
-                return ignite;
-
-        return null;
-    }
-
-    /**
      * @param mxbean MXBean.
-     * @param cacheManagerName name generated by URI and classloader.
      * @param name cache name.
      */
-    public static void registerCacheObject(MBeanServer mBeanServer, Object mxbean, String cacheManagerName, String name,
-                                           boolean stats) {
-        ObjectName registeredObjectName = calculateObjectName(cacheManagerName, name, stats);
+    public void registerCacheObject(MBeanServer mBeanServer, Object mxbean, String name, boolean isStatistic) {
+        ObjectName registeredObjectName = getObjectName(name, isStatistic);
 
         try {
-            if (!isRegistered(mBeanServer, registeredObjectName)) {
+            if (!isRegistered(mBeanServer, registeredObjectName))
                 mBeanServer.registerMBean(mxbean, registeredObjectName);
-
-                System.out.println("Registered mbean with name " + registeredObjectName.toString());
-            }
         }
         catch (Exception e) {
-            throw new CacheException("Error registering cache MXBeans for CacheManager " + registeredObjectName
-                    + " . Error was " + e.getMessage(), e);
+            throw new CacheException("Error registering cache MXBeans for CacheManager "
+                + registeredObjectName.toString() + " . Error was " + e.getMessage(), e);
         }
     }
 
@@ -398,17 +379,15 @@ public class IgniteCacheManager implements CacheManager {
      * UnRegisters the mxbean if registered already.
      *
      * @param mBeanSrv MBean server
-     * @param cacheManagerName name generated by URI and classloader.
      * @param name cache name.
      * @param stats is mxbean, a statistics mxbean.
      */
-    public static void unregisterCacheObject(MBeanServer mBeanSrv, String cacheManagerName, String name, boolean stats) {
-        Set<ObjectName> registeredObjectNames = null;
+    public void unregisterCacheObject(MBeanServer mBeanSrv, String name, boolean stats) {
+        Set<ObjectName> registeredObjectNames;
 
-        ObjectName objectName = calculateObjectName(cacheManagerName, name, stats);
-        registeredObjectNames = mBeanSrv.queryNames(objectName, null);
+        ObjectName objectName = getObjectName(name, stats);
 
-        System.out.println("Try UnRegistered mbean with name " + objectName.toString());
+        registeredObjectNames = mBeanSrv.queryNames(objectName, null);
 
         //should just be one
         for (ObjectName registeredObjectName : registeredObjectNames) {
@@ -422,24 +401,6 @@ public class IgniteCacheManager implements CacheManager {
     }
 
     /**
-     * Creates an object name.
-     */
-    private static ObjectName calculateObjectName(String cacheManagerName, String name, boolean stats) {
-        String cacheManagerNameSafe = mbeanSafe(cacheManagerName);
-        String cacheName = mbeanSafe(name);
-
-        try {
-            String objectNameType = stats ? "Statistics" : "Configuration";
-            return new ObjectName(
-                    "javax.cache:type=Cache" + objectNameType + ",CacheManager=" + cacheManagerNameSafe + ",Cache=" + cacheName);
-        } catch (MalformedObjectNameException e) {
-            throw new CacheException(
-                    "Illegal ObjectName for Management Bean. " + "CacheManager=[" + cacheManagerNameSafe + "], Cache=["
-                            + cacheName + "]", e);
-        }
-    }
-
-    /**
      *
      */
     private void ensureNotClosed() throws IllegalStateException {

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/34593673/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxy.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxy.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxy.java
index 10ff00b..198a715 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxy.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxy.java
@@ -415,6 +415,7 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
     @Override public Map<K, V> getAll(Set<? extends K> keys) {
         try {
             boolean statisticsEnabled = ctx.config().isStatisticsEnabled();
+
             long start = statisticsEnabled ? System.nanoTime() : 0L;
 
             GridCacheProjectionImpl<K, V> prev = gate.enter(prj);
@@ -444,17 +445,25 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
      */
     public Map<K, V> getAll(Collection<? extends K> keys) {
         try {
+            boolean statisticsEnabled = ctx.config().isStatisticsEnabled();
+
+            long start = statisticsEnabled ? System.nanoTime() : 0L;
+
             GridCacheProjectionImpl<K, V> prev = gate.enter(prj);
 
+            Map<K, V> res;
+
             try {
-                return delegate.getAll(keys);
+                res = delegate.getAll(keys);
             }
             finally {
                 gate.leave(prev);
             }
 
             if (statisticsEnabled)
-                ctx.cache().metrics0().addPutTimeNanos(System.nanoTime() - start);
+                ctx.cache().metrics0().addGetTimeNanos(System.nanoTime() - start);
+
+            return res;
         }
         catch (IgniteCheckedException e) {
             throw cacheException(e);
@@ -519,6 +528,7 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
     @Override public void put(K key, V val) {
         try {
             boolean statisticsEnabled = ctx.config().isStatisticsEnabled();
+
             long start = statisticsEnabled ? System.nanoTime() : 0L;
 
             GridCacheProjectionImpl<K, V> prev = gate.enter(prj);
@@ -571,6 +581,7 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
     @Override public void putAll(Map<? extends K, ? extends V> map) {
         try {
             boolean statisticsEnabled = ctx.config().isStatisticsEnabled();
+
             long start = statisticsEnabled ? System.nanoTime() : 0L;
 
             GridCacheProjectionImpl<K, V> prev = gate.enter(prj);
@@ -581,6 +592,9 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
             finally {
                 gate.leave(prev);
             }
+
+            if (statisticsEnabled)
+                ctx.cache().metrics0().addPutTimeNanos(System.nanoTime() - start);
         }
         catch (IgniteCheckedException e) {
             throw cacheException(e);
@@ -591,6 +605,7 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
     @Override public boolean putIfAbsent(K key, V val) {
         try {
             boolean statisticsEnabled = ctx.config().isStatisticsEnabled();
+
             long start = statisticsEnabled ? System.nanoTime() : 0L;
 
             GridCacheProjectionImpl<K, V> prev = gate.enter(prj);
@@ -618,6 +633,7 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
     @Override public boolean remove(K key) {
         try {
             boolean statisticsEnabled = ctx.config().isStatisticsEnabled();
+
             long start = statisticsEnabled ? System.nanoTime() : 0L;
 
             GridCacheProjectionImpl<K, V> prev = gate.enter(prj);
@@ -631,7 +647,7 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
                 gate.leave(prev);
             }
 
-            if (statisticsEnabled)
+            if (statisticsEnabled && removed)
                 ctx.cache().metrics0().addRemoveTimeNanos(System.nanoTime() - start);
 
             return removed;
@@ -645,6 +661,7 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
     @Override public boolean remove(K key, V oldVal) {
         try {
             boolean statisticsEnabled = ctx.config().isStatisticsEnabled();
+
             long start = statisticsEnabled ? System.nanoTime() : 0L;
 
             GridCacheProjectionImpl<K, V> prev = gate.enter(prj);
@@ -672,6 +689,7 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
     @Override public V getAndRemove(K key) {
         try {
             boolean statisticsEnabled = ctx.config().isStatisticsEnabled();
+
             long start = statisticsEnabled ? System.nanoTime() : 0L;
 
             GridCacheProjectionImpl<K, V> prev = gate.enter(prj);
@@ -685,8 +703,10 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
                 gate.leave(prev);
             }
 
-            if (statisticsEnabled)
+            if (statisticsEnabled) {
                 ctx.cache().metrics0().addRemoveTimeNanos(System.nanoTime() - start);
+                ctx.cache().metrics0().addGetTimeNanos(System.nanoTime() - start);
+            }
 
             return removed;
         }
@@ -706,9 +726,6 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
             finally {
                 gate.leave(prev);
             }
-
-            if (statisticsEnabled)
-                ctx.cache().metrics0().addRemoveTimeNanos(System.nanoTime() - start);
         }
         catch (IgniteCheckedException e) {
             throw cacheException(e);
@@ -753,6 +770,7 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
     @Override public void removeAll(Set<? extends K> keys) {
         try {
             boolean statisticsEnabled = ctx.config().isStatisticsEnabled();
+
             long start = statisticsEnabled ? System.nanoTime() : 0L;
 
             GridCacheProjectionImpl<K, V> prev = gate.enter(prj);
@@ -763,6 +781,9 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
             finally {
                 gate.leave(prev);
             }
+
+            if (statisticsEnabled)
+                ctx.cache().metrics0().addRemoveTimeNanos(System.nanoTime() - start);
         }
         catch (IgniteCheckedException e) {
             throw cacheException(e);
@@ -774,6 +795,10 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
      */
     public void removeAll(Collection<? extends K> keys) {
         try {
+            boolean statisticsEnabled = ctx.config().isStatisticsEnabled();
+
+            long start = statisticsEnabled ? System.nanoTime() : 0L;
+
             GridCacheProjectionImpl<K, V> prev = gate.enter(prj);
 
             try {
@@ -782,6 +807,9 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
             finally {
                 gate.leave(prev);
             }
+
+            if (statisticsEnabled)
+                ctx.cache().metrics0().addRemoveTimeNanos(System.nanoTime() - start);
         }
         catch (IgniteCheckedException e) {
             throw cacheException(e);

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/34593673/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheEvictionManager.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheEvictionManager.java b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheEvictionManager.java
index 6894ed0..e585499 100644
--- a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheEvictionManager.java
+++ b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheEvictionManager.java
@@ -668,7 +668,8 @@ public class GridCacheEvictionManager<K, V> extends GridCacheManagerAdapter<K, V
 
             cache.removeEntry(entry);
 
-            cache.metrics0().onEvict();
+            if (cache.configuration().isStatisticsEnabled())
+                cache.metrics0().onEvict();
 
             if (recordable)
                 cctx.events().addEvent(entry.partition(), entry.key(), cctx.nodeId(), (IgniteUuid)null, null,

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/34593673/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMapEntry.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMapEntry.java b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMapEntry.java
index 46dc22d..2f58ec9 100644
--- a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMapEntry.java
+++ b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMapEntry.java
@@ -772,11 +772,11 @@ public abstract class GridCacheMapEntry<K, V> implements GridCacheEntryEx<K, V>
             }
 
             if (old == null && !hasOldBytes) {
-                if (updateMetrics)
+                if (updateMetrics && cctx.cache().configuration().isStatisticsEnabled())
                     cctx.cache().metrics0().onRead(false);
             }
             else {
-                if (updateMetrics)
+                if (updateMetrics && cctx.cache().configuration().isStatisticsEnabled())
                     cctx.cache().metrics0().onRead(true);
 
                 // Set retVal here for event notification.
@@ -1140,7 +1140,7 @@ public abstract class GridCacheMapEntry<K, V> implements GridCacheEntryEx<K, V>
 
             recordNodeId(affNodeId);
 
-            if (metrics)
+            if (metrics && cctx.cache().configuration().isStatisticsEnabled())
                 cctx.cache().metrics0().onWrite();
 
             if (evt && newVer != null && cctx.events().isRecordable(EVT_CACHE_OBJECT_PUT)) {
@@ -1292,7 +1292,7 @@ public abstract class GridCacheMapEntry<K, V> implements GridCacheEntryEx<K, V>
 
                 drReplicate(drType, null, null, newVer);
 
-                if (metrics)
+                if (metrics && cctx.cache().configuration().isStatisticsEnabled())
                     cctx.cache().metrics0().onRemove();
 
                 if (tx == null)
@@ -1444,7 +1444,7 @@ public abstract class GridCacheMapEntry<K, V> implements GridCacheEntryEx<K, V>
             }
 
             // Apply metrics.
-            if (metrics && needVal)
+            if (metrics && cctx.cache().configuration().isStatisticsEnabled() && needVal)
                 cctx.cache().metrics0().onRead(old != null);
 
             String transformCloClsName = null;
@@ -1800,7 +1800,7 @@ public abstract class GridCacheMapEntry<K, V> implements GridCacheEntryEx<K, V>
             }
 
             // Apply metrics.
-            if (metrics && needVal)
+            if (metrics && cctx.cache().configuration().isStatisticsEnabled() && needVal)
                 cctx.cache().metrics0().onRead(old != null);
 
             // Calculate new value.
@@ -4134,7 +4134,7 @@ public abstract class GridCacheMapEntry<K, V> implements GridCacheEntryEx<K, V>
      * @param metrics Update merics flag.
      */
     private void updateMetrics(GridCacheOperation op, boolean metrics) {
-        if (metrics) {
+        if (metrics && cctx.cache().configuration().isStatisticsEnabled()) {
             if (op == DELETE)
                 cctx.cache().metrics0().onRemove();
             else

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/34593673/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
index ae539d5..dc1faf2 100644
--- a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
+++ b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
@@ -269,8 +269,8 @@ public class GridCacheMetricsAdapter implements GridCacheMetrics, Externalizable
         removeTimeNanos += duration;
     }
 
-    @Override
-    public float getAverageGetTime() {
+    /** {@inheritDoc} */
+    @Override public float getAverageGetTime() {
         long timeNanos = getTimeNanos;
         long readsCnt = reads;
 
@@ -280,8 +280,8 @@ public class GridCacheMetricsAdapter implements GridCacheMetrics, Externalizable
         return ((1f * timeNanos) / readsCnt) / NANOS_IN_MICROSECOND;
     }
 
-    @Override
-    public float getAveragePutTime() {
+    /** {@inheritDoc} */
+    @Override public float getAveragePutTime() {
         long timeNanos = putTimeNanos;
         long putsCnt = writes;
 
@@ -291,8 +291,8 @@ public class GridCacheMetricsAdapter implements GridCacheMetrics, Externalizable
         return ((1f * timeNanos) / putsCnt) / NANOS_IN_MICROSECOND;
     }
 
-    @Override
-    public float getAverageRemoveTime() {
+    /** {@inheritDoc} */
+    @Override public float getAverageRemoveTime() {
         long timeNanos = removeTimeNanos;
         long removesCnt = rmCnt;
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/34593673/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheTtlManager.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheTtlManager.java b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheTtlManager.java
index 33ead3a..0ec3fe3 100644
--- a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheTtlManager.java
+++ b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheTtlManager.java
@@ -127,7 +127,8 @@ public class GridCacheTtlManager<K, V> extends GridCacheManagerAdapter<K, V> {
                         if (wrapper.entry.onTtlExpired(obsoleteVer))
                             wrapper.entry.context().cache().removeEntry(wrapper.entry);
 
-                        wrapper.entry.context().cache().metrics0().onEvict();
+                        if (wrapper.entry.context().cache().configuration().isStatisticsEnabled())
+                            wrapper.entry.context().cache().metrics0().onEvict();
 
                         it.remove();
                     }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/34593673/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridNearCacheEntry.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridNearCacheEntry.java b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridNearCacheEntry.java
index c29f8e2..aa27e6f 100644
--- a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridNearCacheEntry.java
+++ b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridNearCacheEntry.java
@@ -360,7 +360,8 @@ public class GridNearCacheEntry<K, V> extends GridDistributedCacheEntry<K, V> {
             synchronized (this) {
                 checkObsolete();
 
-                cctx.cache().metrics0().onRead(false);
+                if (cctx.cache().configuration().isStatisticsEnabled())
+                    cctx.cache().metrics0().onRead(false);
 
                 boolean ret = false;
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/34593673/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridNearGetFuture.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridNearGetFuture.java b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridNearGetFuture.java
index 7184c7b..818612a 100644
--- a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridNearGetFuture.java
+++ b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridNearGetFuture.java
@@ -465,12 +465,14 @@ public final class GridNearGetFuture<K, V> extends GridCompoundIdentityFuture<Ma
                                 dht.removeIfObsolete(key);
                         }
 
-                        if (v != null)
-                            near.metrics0().onRead(true);
+                        if (v != null) {
+                            if (cctx.cache().configuration().isStatisticsEnabled())
+                                near.metrics0().onRead(true);
+                        }
                         else {
                             primary = cctx.affinity().primary(key, topVer);
 
-                            if (!primary.isLocal())
+                            if (!primary.isLocal() && cctx.cache().configuration().isStatisticsEnabled())
                                 near.metrics0().onRead(false);
                         }
                     }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/34593673/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridNearLockFuture.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridNearLockFuture.java b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridNearLockFuture.java
index 200e695..9dc5234 100644
--- a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridNearLockFuture.java
+++ b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridNearLockFuture.java
@@ -1063,7 +1063,8 @@ public final class GridNearLockFuture<K, V> extends GridCompoundIdentityFuture<B
                                                     null,
                                                     inTx() ? tx.resolveTaskName() : null);
 
-                                            cctx.cache().metrics0().onRead(oldVal != null);
+                                            if (cctx.cache().configuration().isStatisticsEnabled())
+                                                cctx.cache().metrics0().onRead(oldVal != null);
                                         }
 
                                         if (log.isDebugEnabled())
@@ -1424,7 +1425,8 @@ public final class GridNearLockFuture<K, V> extends GridCompoundIdentityFuture<B
                                         null,
                                         inTx() ? tx.resolveTaskName() : null);
 
-                                cctx.cache().metrics0().onRead(false);
+                                if (cctx.cache().configuration().isStatisticsEnabled())
+                                    cctx.cache().metrics0().onRead(false);
                             }
 
                             if (log.isDebugEnabled())

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/34593673/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/local/atomic/GridLocalAtomicCache.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/local/atomic/GridLocalAtomicCache.java b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/local/atomic/GridLocalAtomicCache.java
index 4312c81..fca2dd5 100644
--- a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/local/atomic/GridLocalAtomicCache.java
+++ b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/local/atomic/GridLocalAtomicCache.java
@@ -602,7 +602,7 @@ public class GridLocalAtomicCache<K, V> extends GridCacheAdapter<K, V> {
                             success = false;
                     }
                     else {
-                        if (!storeEnabled)
+                        if (!storeEnabled && configuration().isStatisticsEnabled())
                             metrics0().onRead(false);
 
                         success = false;

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/34593673/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/transactions/IgniteTxManager.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/transactions/IgniteTxManager.java b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/transactions/IgniteTxManager.java
index c2a117f..bc78303 100644
--- a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/transactions/IgniteTxManager.java
+++ b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/transactions/IgniteTxManager.java
@@ -1256,7 +1256,8 @@ public class IgniteTxManager<K, V> extends GridCacheSharedManagerAdapter<K, V> {
                 for (int cacheId : tx.activeCacheIds()) {
                     GridCacheContext<K, V> cacheCtx = cctx.cacheContext(cacheId);
 
-                    cacheCtx.cache().metrics0().onTxCommit();
+                    if (cacheCtx.cache().configuration().isStatisticsEnabled())
+                        cacheCtx.cache().metrics0().onTxCommit();
                 }
             }
 
@@ -1329,7 +1330,8 @@ public class IgniteTxManager<K, V> extends GridCacheSharedManagerAdapter<K, V> {
                 for (int cacheId : tx.activeCacheIds()) {
                     GridCacheContext<K, V> cacheCtx = cctx.cacheContext(cacheId);
 
-                    cacheCtx.cache().metrics0().onTxRollback();
+                    if (cacheCtx.cache().configuration().isStatisticsEnabled())
+                        cacheCtx.cache().metrics0().onTxRollback();
                 }
             }
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/34593673/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
index 74e6068..3a1f4c0 100644
--- a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
+++ b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
@@ -20,16 +20,21 @@ package org.gridgain.grid.kernal.processors.cache;
 import org.apache.ignite.*;
 import org.apache.ignite.transactions.*;
 import org.gridgain.grid.cache.*;
+import org.gridgain.grid.kernal.*;
 import org.gridgain.grid.util.lang.*;
 import org.gridgain.grid.util.typedef.internal.*;
 import org.gridgain.testframework.*;
 
+import javax.cache.*;
 import javax.cache.expiry.*;
+import javax.cache.spi.*;
 import javax.management.*;
 import java.net.*;
 import java.util.*;
+import java.util.concurrent.*;
 
 import static java.util.concurrent.TimeUnit.*;
+import static org.gridgain.grid.util.GridUtils.*;
 
 /**
  * Cache metrics test.
@@ -38,6 +43,9 @@ public abstract class GridCacheAbstractMetricsSelfTest extends GridCacheAbstract
     /** */
     private static final int KEY_CNT = 50;
 
+    /** */
+    private static final String CACHE_NAME = "mbean_test";
+
     /** {@inheritDoc} */
     @Override protected boolean swapEnabled() {
         return false;
@@ -187,20 +195,38 @@ public abstract class GridCacheAbstractMetricsSelfTest extends GridCacheAbstract
         assert cache.metrics().getAverageGetTime() > 0;
     }
 
+    private String getUriForMbean() throws Exception {
+        String uri;
+
+        try{
+            uri = resolveGridGainUrl(GridGainEx.DFLT_CFG).toURI().toString();
+        }
+        catch (Exception e){
+            uri = URI.create("ignite://default").toString();
+        }
+
+        return uri.replaceAll(",|:|=|\n", ".");
+    }
+
     /**
      * @throws Exception If failed.
      */
     public void testStatisticMXBean() throws Exception {
-        IgniteCache<Integer, Integer> jcache = grid(0).jcache(null);
+        CachingProvider cachingProvider = Caching.getCachingProvider();
+
+        CacheManager cacheMgr = cachingProvider.getCacheManager();
 
-        URI uri = URI.create("ignite://default");
+        GridCacheConfiguration cfg = new GridCacheConfiguration();
 
-        IgniteCacheManager cacheManager = new IgniteCacheManager(uri);
+        cfg.setManagementEnabled(true);
 
-        cacheManager.enableStatistics(null, true);
+        cfg.setStatisticsEnabled(true);
 
-        ObjectName objectName =
-            new ObjectName("javax.cache:type=CacheStatistics,CacheManager=ignite.//default,Cache=null");
+        Cache jcache = cacheMgr.createCache(CACHE_NAME, cfg);
+
+        String beanName = "javax.cache:type=CacheStatistics,CacheManager=" + getUriForMbean() + ",Cache=" + CACHE_NAME;
+
+        ObjectName objectName = new ObjectName(beanName);
 
         long cachePuts = (long)getConfiguration().getMBeanServer().getAttribute(objectName, "CachePuts");
 
@@ -212,21 +238,29 @@ public abstract class GridCacheAbstractMetricsSelfTest extends GridCacheAbstract
 
         assertEquals(1, cachePuts);
 
-        cacheManager.enableStatistics(null, false);
+        cacheMgr.destroyCache(CACHE_NAME);
     }
 
     /**
      * @throws Exception If failed.
      */
     public void testCacheMxBean() throws Exception {
-        URI uri = URI.create("ignite://default");
+        CachingProvider cachingProvider = Caching.getCachingProvider();
+
+        CacheManager cacheMgr = cachingProvider.getCacheManager();
+
+        GridCacheConfiguration cfg = new GridCacheConfiguration();
+
+        cfg.setManagementEnabled(true);
 
-        IgniteCacheManager cacheManager = new IgniteCacheManager(uri);
+        cfg.setStatisticsEnabled(true);
 
-        cacheManager.enableManagement(null, true);
+        cacheMgr.createCache(CACHE_NAME, cfg);
 
-        ObjectName objectName =
-            new ObjectName("javax.cache:type=CacheConfiguration,CacheManager=ignite.//default,Cache=null");
+        String beanName = "javax.cache:type=CacheConfiguration,CacheManager=" + getUriForMbean()
+            + ",Cache=" + CACHE_NAME;
+
+        ObjectName objectName = new ObjectName(beanName);
 
         String keyType = (String)getConfiguration().getMBeanServer().getAttribute(objectName, "KeyType");
         assertEquals("java.lang.Object", keyType);
@@ -249,7 +283,7 @@ public abstract class GridCacheAbstractMetricsSelfTest extends GridCacheAbstract
         boolean isMng = (boolean)getConfiguration().getMBeanServer().getAttribute(objectName, "ManagementEnabled");
         assertEquals(true, isMng);
 
-        cacheManager.enableManagement(null, false);
+        cacheMgr.destroyCache(CACHE_NAME);
     }
 
     /**
@@ -789,4 +823,9 @@ public abstract class GridCacheAbstractMetricsSelfTest extends GridCacheAbstract
 
         throw new IgniteCheckedException("Unable to find " + cnt + " keys as primary for cache.");
     }
+
+    /** {@inheritDoc} */
+    @Override protected long getTestTimeout() {
+        return TimeUnit.MINUTES.toMillis(10L);
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/34593673/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridCacheNearMetricsSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridCacheNearMetricsSelfTest.java b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridCacheNearMetricsSelfTest.java
index 5fbcd9f..0e1b41e 100644
--- a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridCacheNearMetricsSelfTest.java
+++ b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridCacheNearMetricsSelfTest.java
@@ -74,6 +74,17 @@ public class GridCacheNearMetricsSelfTest extends GridCacheAbstractSelfTest {
     }
 
     /** {@inheritDoc} */
+    @Override protected void beforeTest() throws Exception {
+        super.beforeTest();
+
+        for (int i = 0; i < gridCount(); i++) {
+            Ignite g = grid(i);
+
+            g.cache(null).configuration().setStatisticsEnabled(true);
+        }
+    }
+
+    /** {@inheritDoc} */
     @Override protected GridCacheConfiguration cacheConfiguration(String gridName) throws Exception {
         GridCacheConfiguration cc = super.cacheConfiguration(gridName);
 


[6/7] incubator-ignite git commit: IGNITE-49 Fixed tck tests.

Posted by sb...@apache.org.
IGNITE-49 Fixed tck tests.

Adapt to work with Ignite.


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

Branch: refs/heads/ignite-49
Commit: 984a3dea818f8a1a9b43a02bdc5f6c496121962e
Parents: 3459367
Author: nikolay_tikhonov <nt...@gridgain.com>
Authored: Fri Jan 16 13:59:41 2015 +0300
Committer: nikolay_tikhonov <nt...@gridgain.com>
Committed: Fri Jan 16 14:13:35 2015 +0300

----------------------------------------------------------------------
 .../org/apache/ignite/IgniteCacheManager.java   |  23 +--
 .../processors/cache/GridCacheMapEntry.java     |  28 ++-
 .../cache/GridCacheMetricsAdapter.java          |  32 ++--
 .../cache/GridCacheAbstractMetricsSelfTest.java |   4 +-
 ...AtomicPartitionedTckMetricsSelfTestImpl.java | 183 +++++++++++++++++++
 ...dCacheAtomicLocalTckMetricsSelfTestImpl.java | 178 ++++++++++++++++++
 .../GridCacheMetricsSelfTestSuite.java          |   2 +
 7 files changed, 406 insertions(+), 44 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/984a3dea/modules/core/src/main/java/org/apache/ignite/IgniteCacheManager.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/IgniteCacheManager.java b/modules/core/src/main/java/org/apache/ignite/IgniteCacheManager.java
index c255517..fea39ca 100644
--- a/modules/core/src/main/java/org/apache/ignite/IgniteCacheManager.java
+++ b/modules/core/src/main/java/org/apache/ignite/IgniteCacheManager.java
@@ -17,7 +17,7 @@
 
 package org.apache.ignite;
 
-import org.apache.ignite.cache.CacheMetricsMXBean;
+import org.apache.ignite.cache.*;
 import org.apache.ignite.configuration.*;
 import org.apache.ignite.lang.*;
 import org.gridgain.grid.cache.*;
@@ -293,25 +293,16 @@ public class IgniteCacheManager implements CacheManager {
             tuple = igniteMap.get(cacheName);
         }
 
-        ObjectName objName = getObjectName(cacheName, false);
         MBeanServer mBeanSrv = tuple.get1().configuration().getMBeanServer();
 
-        try {
-            if (enabled) {
-                if(mBeanSrv.queryNames(objName, null).isEmpty())
-                    mBeanSrv.registerMBean(tuple.get2(), objName);
-            }
-            else {
-                for (ObjectName n : mBeanSrv.queryNames(objName, null))
-                    mBeanSrv.unregisterMBean(n);
+        if (enabled) {
+            registerCacheObject(mBeanSrv, tuple.get2(), cacheName, false);
 
-            }
-        }
-        catch (InstanceAlreadyExistsException | InstanceNotFoundException ignored) {
+            tuple.get1().cache(cacheName).configuration().setManagementEnabled(true);
+        } else {
+            unregisterCacheObject(mBeanSrv, cacheName, false);
 
-        }
-        catch (MBeanRegistrationException | NotCompliantMBeanException e) {
-            throw new CacheException(e);
+            tuple.get1().cache(cacheName).configuration().setManagementEnabled(false);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/984a3dea/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMapEntry.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMapEntry.java b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMapEntry.java
index 2f58ec9..b0b543b 100644
--- a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMapEntry.java
+++ b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMapEntry.java
@@ -1427,6 +1427,13 @@ public abstract class GridCacheMapEntry<K, V> implements GridCacheEntryEx<K, V>
                 update(old, null, 0, 0, ver);
             }
 
+            // Apply metrics.
+            if (metrics && cctx.cache().configuration().isStatisticsEnabled() && needVal) {
+                // PutIfAbsent methods mustn't update hit/miss statistics
+                if (op != UPDATE || F.isEmpty(filter) || filter != cctx.noPeekArray())
+                    cctx.cache().metrics0().onRead(old != null);
+            }
+
             // Check filter inside of synchronization.
             if (!F.isEmpty(filter)) {
                 boolean pass = cctx.isAll(wrapFilterLocked(), filter);
@@ -1443,10 +1450,6 @@ public abstract class GridCacheMapEntry<K, V> implements GridCacheEntryEx<K, V>
                 }
             }
 
-            // Apply metrics.
-            if (metrics && cctx.cache().configuration().isStatisticsEnabled() && needVal)
-                cctx.cache().metrics0().onRead(old != null);
-
             String transformCloClsName = null;
 
             V updated;
@@ -1588,7 +1591,8 @@ public abstract class GridCacheMapEntry<K, V> implements GridCacheEntryEx<K, V>
                 res = hadVal;
             }
 
-            updateMetrics(op, metrics);
+            if (res)
+                updateMetrics(op, metrics);
 
             cctx.continuousQueries().onEntryUpdate(this, key, val, valueBytesUnlocked(), old, oldBytes);
 
@@ -1769,6 +1773,13 @@ public abstract class GridCacheMapEntry<K, V> implements GridCacheEntryEx<K, V>
                     deletedUnlocked(false);
             }
 
+            // Apply metrics.
+            if (metrics && cctx.cache().configuration().isStatisticsEnabled() && needVal) {
+                // PutIfAbsent methods mustn't update hit/miss statistics
+                if (op != UPDATE || F.isEmpty(filter) || filter != cctx.noPeekArray())
+                    cctx.cache().metrics0().onRead(old != null);
+            }
+
             // Check filter inside of synchronization.
             if (!F.isEmptyOrNulls(filter)) {
                 boolean pass = cctx.isAll(wrapFilterLocked(), filter);
@@ -1799,10 +1810,6 @@ public abstract class GridCacheMapEntry<K, V> implements GridCacheEntryEx<K, V>
                 }
             }
 
-            // Apply metrics.
-            if (metrics && cctx.cache().configuration().isStatisticsEnabled() && needVal)
-                cctx.cache().metrics0().onRead(old != null);
-
             // Calculate new value.
             if (op == GridCacheOperation.TRANSFORM) {
                 transformClo = writeObj;
@@ -2056,7 +2063,8 @@ public abstract class GridCacheMapEntry<K, V> implements GridCacheEntryEx<K, V>
                 newDrExpireTime = -1L;
             }
 
-            updateMetrics(op, metrics);
+            if (res)
+                updateMetrics(op, metrics);
 
             if (primary || cctx.isReplicated())
                 cctx.continuousQueries().onEntryUpdate(this, key, val, valueBytesUnlocked(), old, oldBytes);

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/984a3dea/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
index dc1faf2..ada12ff 100644
--- a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
+++ b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
@@ -97,22 +97,22 @@ public class GridCacheMetricsAdapter implements GridCacheMetrics, Externalizable
      * @param m Metrics to copy from.
      */
     public GridCacheMetricsAdapter(GridCacheMetricsAdapter m) {
-        createTime = m.createTime();
-        readTime = m.readTime();
-        writeTime = m.writeTime();
-        commitTime = m.commitTime();
-        rollbackTime = m.rollbackTime();
-        reads = m.reads();
-        writes = m.writes();
-        hits = m.hits();
-        misses = m.misses();
-        txCommits = m.txCommits();
-        txRollbacks = m.txRollbacks();
-        rmCnt = m.getCacheRemovals();
-        evictCnt = m.getCacheEvictions();
-        getTimeNanos = m.getTimeNanos();
-        putTimeNanos = m.putTimeNanos();
-        removeTimeNanos = m.removeTimeNanos();
+        createTime = m.createTime;
+        readTime = m.readTime;
+        writeTime = m.writeTime;
+        commitTime = m.commitTime;
+        rollbackTime = m.rollbackTime;
+        reads = m.reads;
+        writes = m.writes;
+        hits = m.hits;
+        misses = m.misses;
+        txCommits = m.txCommits;
+        txRollbacks = m.txRollbacks;
+        rmCnt = m.rmCnt;
+        evictCnt = m.evictCnt;
+        getTimeNanos = m.getTimeNanos;
+        putTimeNanos = m.putTimeNanos;
+        removeTimeNanos = m.removeTimeNanos;
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/984a3dea/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
index 3a1f4c0..4254d4a 100644
--- a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
+++ b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
@@ -41,10 +41,10 @@ import static org.gridgain.grid.util.GridUtils.*;
  */
 public abstract class GridCacheAbstractMetricsSelfTest extends GridCacheAbstractSelfTest {
     /** */
-    private static final int KEY_CNT = 50;
+    public static final String CACHE_NAME = "metric_test";
 
     /** */
-    private static final String CACHE_NAME = "mbean_test";
+    private static final int KEY_CNT = 50;
 
     /** {@inheritDoc} */
     @Override protected boolean swapEnabled() {

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/984a3dea/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridCacheAtomicPartitionedTckMetricsSelfTestImpl.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridCacheAtomicPartitionedTckMetricsSelfTestImpl.java b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridCacheAtomicPartitionedTckMetricsSelfTestImpl.java
new file mode 100644
index 0000000..951aa53
--- /dev/null
+++ b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridCacheAtomicPartitionedTckMetricsSelfTestImpl.java
@@ -0,0 +1,183 @@
+/*
+ * 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.gridgain.grid.kernal.processors.cache.distributed.near;
+
+import org.apache.ignite.*;
+
+import javax.cache.processor.*;
+
+/**
+ * Partitioned atomic cache metrics test.
+ */
+public class GridCacheAtomicPartitionedTckMetricsSelfTestImpl extends GridCacheAtomicPartitionedMetricsSelfTest {
+    /** {@inheritDoc} */
+    @Override protected int gridCount() {
+        return 1;
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testEntryProcessorRemove() throws Exception {
+        IgniteCache<Integer, Integer> jcache = grid(0).jcache(null);
+
+        jcache.put(1, 20);
+
+        int result = jcache.invoke(1, new EntryProcessor<Integer, Integer, Integer>() {
+            @Override public Integer process(MutableEntry<Integer, Integer> entry, Object... arguments)
+                    throws EntryProcessorException {
+                Integer result = entry.getValue();
+
+                entry.remove();
+
+                return result;
+            }
+        });
+
+        assertEquals(1L, cache().metrics().getCachePuts());
+
+        assertEquals(20, result);
+        assertEquals(1L, cache().metrics().getCacheHits());
+        assertEquals(100.0f, cache().metrics().getCacheHitPercentage());
+        assertEquals(0L, cache().metrics().getCacheMisses());
+        assertEquals(0f, cache().metrics().getCacheMissPercentage());
+        assertEquals(1L, cache().metrics().getCachePuts());
+        assertEquals(1L, cache().metrics().getCacheRemovals());
+        assertEquals(0L, cache().metrics().getCacheEvictions());
+        assert cache().metrics().getAveragePutTime() >= 0;
+        assert cache().metrics().getAverageGetTime() >= 0;
+        assert cache().metrics().getAverageRemoveTime() >= 0;
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testCacheStatistics() throws Exception {
+        IgniteCache<Integer, Integer> jcache = grid(0).jcache(null);
+
+        jcache.put(1, 10);
+
+        assertEquals(0, cache().metrics().getCacheRemovals());
+        assertEquals(1, cache().metrics().getCachePuts());
+
+        jcache.remove(1);
+
+        assertEquals(0, cache().metrics().getCacheHits());
+        assertEquals(1, cache().metrics().getCacheRemovals());
+        assertEquals(1, cache().metrics().getCachePuts());
+
+        jcache.remove(1);
+
+        assertEquals(0, cache().metrics().getCacheHits());
+        assertEquals(0, cache().metrics().getCacheMisses());
+        assertEquals(1, cache().metrics().getCacheRemovals());
+        assertEquals(1, cache().metrics().getCachePuts());
+
+        jcache.put(1, 10);
+        assertTrue(jcache.remove(1, 10));
+
+        assertEquals(1, cache().metrics().getCacheHits());
+        assertEquals(0, cache().metrics().getCacheMisses());
+        assertEquals(2, cache().metrics().getCacheRemovals());
+        assertEquals(2, cache().metrics().getCachePuts());
+
+        assertFalse(jcache.remove(1, 10));
+
+        assertEquals(1, cache().metrics().getCacheHits());
+        assertEquals(1, cache().metrics().getCacheMisses());
+        assertEquals(2, cache().metrics().getCacheRemovals());
+        assertEquals(2, cache().metrics().getCachePuts());
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testConditionReplace() throws Exception {
+        IgniteCache<Integer, Integer> jcache = grid(0).jcache(null);
+
+        long hitCount = 0;
+        long missCount = 0;
+        long putCount = 0;
+
+        boolean result = jcache.replace(1, 0, 10);
+
+        ++missCount;
+        assertFalse(result);
+
+        assertEquals(missCount, cache().metrics().getCacheMisses());
+        assertEquals(hitCount, cache().metrics().getCacheHits());
+        assertEquals(putCount, cache().metrics().getCachePuts());
+
+        assertFalse(jcache.containsKey(1));
+
+        jcache.put(1, 10);
+        ++putCount;
+
+        assertEquals(missCount, cache().metrics().getCacheMisses());
+        assertEquals(hitCount, cache().metrics().getCacheHits());
+        assertEquals(putCount, cache().metrics().getCachePuts());
+
+        assertTrue(jcache.containsKey(1));
+
+        result = jcache.replace(1, 10, 20);
+
+        assertTrue(result);
+        ++hitCount;
+        ++putCount;
+
+        assertEquals(missCount, cache().metrics().getCacheMisses());
+        assertEquals(hitCount, cache().metrics().getCacheHits());
+        assertEquals(putCount, cache().metrics().getCachePuts());
+
+        result = jcache.replace(1, 40, 50);
+
+        assertFalse(result);
+        ++hitCount;
+
+        assertEquals(hitCount, cache().metrics().getCacheHits());
+        assertEquals(putCount, cache().metrics().getCachePuts());
+        assertEquals(missCount, cache().metrics().getCacheMisses());
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testPutIfAbsent() throws Exception {
+        IgniteCache<Integer, Integer> jcache = grid(0).jcache(null);
+
+        long hitCount = 0;
+        long missCount = 0;
+        long putCount = 0;
+
+        boolean result = jcache.putIfAbsent(1, 1);
+
+        ++putCount;
+        assertTrue(result);
+
+        assertEquals(missCount, cache().metrics().getCacheMisses());
+        assertEquals(hitCount, cache().metrics().getCacheHits());
+        assertEquals(putCount, cache().metrics().getCachePuts());
+
+        result = jcache.putIfAbsent(1, 1);
+
+        assertFalse(result);
+        assertEquals(hitCount, cache().metrics().getCacheHits());
+        assertEquals(putCount, cache().metrics().getCachePuts());
+        assertEquals(missCount, cache().metrics().getCacheMisses());
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/984a3dea/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/local/GridCacheAtomicLocalTckMetricsSelfTestImpl.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/local/GridCacheAtomicLocalTckMetricsSelfTestImpl.java b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/local/GridCacheAtomicLocalTckMetricsSelfTestImpl.java
new file mode 100644
index 0000000..5f61f23
--- /dev/null
+++ b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/local/GridCacheAtomicLocalTckMetricsSelfTestImpl.java
@@ -0,0 +1,178 @@
+/*
+ * 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.gridgain.grid.kernal.processors.cache.local;
+
+import org.apache.ignite.*;
+
+import javax.cache.processor.*;
+
+/**
+ * Local atomic cache metrics test with tck specific.
+ */
+public class GridCacheAtomicLocalTckMetricsSelfTestImpl extends GridCacheAtomicLocalMetricsSelfTest {
+    /**
+     * @throws Exception If failed.
+     */
+    public void testEntryProcessorRemove() throws Exception {
+        IgniteCache<Integer, Integer> jcache = grid(0).jcache(null);
+
+        jcache.put(1, 20);
+
+        int result = jcache.invoke(1, new EntryProcessor<Integer, Integer, Integer>() {
+            @Override public Integer process(MutableEntry<Integer, Integer> entry, Object... arguments)
+                    throws EntryProcessorException {
+                Integer result = entry.getValue();
+
+                entry.remove();
+
+                return result;
+            }
+        });
+
+        assertEquals(1L, cache().metrics().getCachePuts());
+
+        assertEquals(20, result);
+        assertEquals(1L, cache().metrics().getCacheHits());
+        assertEquals(100.0f, cache().metrics().getCacheHitPercentage());
+        assertEquals(0L, cache().metrics().getCacheMisses());
+        assertEquals(0f, cache().metrics().getCacheMissPercentage());
+        assertEquals(1L, cache().metrics().getCachePuts());
+        assertEquals(1L, cache().metrics().getCacheRemovals());
+        assertEquals(0L, cache().metrics().getCacheEvictions());
+        assert cache().metrics().getAveragePutTime() >= 0;
+        assert cache().metrics().getAverageGetTime() >= 0;
+        assert cache().metrics().getAverageRemoveTime() >= 0;
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testCacheStatistics() throws Exception {
+        IgniteCache<Integer, Integer> jcache = grid(0).jcache(null);
+
+        jcache.put(1, 10);
+
+        assertEquals(0, cache().metrics().getCacheRemovals());
+        assertEquals(1, cache().metrics().getCachePuts());
+
+        jcache.remove(1);
+
+        assertEquals(0, cache().metrics().getCacheHits());
+        assertEquals(1, cache().metrics().getCacheRemovals());
+        assertEquals(1, cache().metrics().getCachePuts());
+
+        jcache.remove(1);
+
+        assertEquals(0, cache().metrics().getCacheHits());
+        assertEquals(0, cache().metrics().getCacheMisses());
+        assertEquals(1, cache().metrics().getCacheRemovals());
+        assertEquals(1, cache().metrics().getCachePuts());
+
+        jcache.put(1, 10);
+        assertTrue(jcache.remove(1, 10));
+
+        assertEquals(1, cache().metrics().getCacheHits());
+        assertEquals(0, cache().metrics().getCacheMisses());
+        assertEquals(2, cache().metrics().getCacheRemovals());
+        assertEquals(2, cache().metrics().getCachePuts());
+
+        assertFalse(jcache.remove(1, 10));
+
+        assertEquals(1, cache().metrics().getCacheHits());
+        assertEquals(1, cache().metrics().getCacheMisses());
+        assertEquals(2, cache().metrics().getCacheRemovals());
+        assertEquals(2, cache().metrics().getCachePuts());
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testConditionReplace() throws Exception {
+        IgniteCache<Integer, Integer> jcache = grid(0).jcache(null);
+
+        long hitCount = 0;
+        long missCount = 0;
+        long putCount = 0;
+
+        boolean result = jcache.replace(1, 0, 10);
+
+        ++missCount;
+        assertFalse(result);
+
+        assertEquals(missCount, cache().metrics().getCacheMisses());
+        assertEquals(hitCount, cache().metrics().getCacheHits());
+        assertEquals(putCount, cache().metrics().getCachePuts());
+
+        assertFalse(jcache.containsKey(1));
+
+        jcache.put(1, 10);
+        ++putCount;
+
+        assertEquals(missCount, cache().metrics().getCacheMisses());
+        assertEquals(hitCount, cache().metrics().getCacheHits());
+        assertEquals(putCount, cache().metrics().getCachePuts());
+
+        assertTrue(jcache.containsKey(1));
+
+        result = jcache.replace(1, 10, 20);
+
+        assertTrue(result);
+        ++hitCount;
+        ++putCount;
+
+        assertEquals(missCount, cache().metrics().getCacheMisses());
+        assertEquals(hitCount, cache().metrics().getCacheHits());
+        assertEquals(putCount, cache().metrics().getCachePuts());
+
+        result = jcache.replace(1, 40, 50);
+
+        assertFalse(result);
+        ++hitCount;
+
+        assertEquals(hitCount, cache().metrics().getCacheHits());
+        assertEquals(putCount, cache().metrics().getCachePuts());
+        assertEquals(missCount, cache().metrics().getCacheMisses());
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testPutIfAbsent() throws Exception {
+        IgniteCache<Integer, Integer> jcache = grid(0).jcache(null);
+
+        long hitCount = 0;
+        long missCount = 0;
+        long putCount = 0;
+
+        boolean result = jcache.putIfAbsent(1, 1);
+
+        ++putCount;
+        assertTrue(result);
+
+        assertEquals(missCount, cache().metrics().getCacheMisses());
+        assertEquals(hitCount, cache().metrics().getCacheHits());
+        assertEquals(putCount, cache().metrics().getCachePuts());
+
+        result = jcache.putIfAbsent(1, 1);
+
+        assertFalse(result);
+        assertEquals(hitCount, cache().metrics().getCacheHits());
+        assertEquals(putCount, cache().metrics().getCachePuts());
+        assertEquals(missCount, cache().metrics().getCacheMisses());
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/984a3dea/modules/core/src/test/java/org/gridgain/testsuites/GridCacheMetricsSelfTestSuite.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/gridgain/testsuites/GridCacheMetricsSelfTestSuite.java b/modules/core/src/test/java/org/gridgain/testsuites/GridCacheMetricsSelfTestSuite.java
index f872e52..24dca75 100644
--- a/modules/core/src/test/java/org/gridgain/testsuites/GridCacheMetricsSelfTestSuite.java
+++ b/modules/core/src/test/java/org/gridgain/testsuites/GridCacheMetricsSelfTestSuite.java
@@ -43,6 +43,8 @@ public class GridCacheMetricsSelfTestSuite extends TestSuite {
         suite.addTest(new TestSuite(GridCacheAtomicLocalMetricsNoStoreSelfTest.class));
         suite.addTest(new TestSuite(GridCacheAtomicReplicatedMetricsSelfTest.class));
         suite.addTest(new TestSuite(GridCacheAtomicPartitionedMetricsSelfTest.class));
+        suite.addTest(new TestSuite(GridCacheAtomicPartitionedTckMetricsSelfTestImpl.class));
+        suite.addTest(new TestSuite(GridCacheAtomicLocalTckMetricsSelfTestImpl.class));
 
         return suite;
     }


[4/7] incubator-ignite git commit: IGNITE-49 Implemented avg statistics

Posted by sb...@apache.org.
IGNITE-49 Implemented avg statistics


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

Branch: refs/heads/ignite-49
Commit: d8ecf41a7f4ae42fb3becad1a9de61a62b1d26d5
Parents: c04013d
Author: nikolay_tikhonov <nt...@gridgain.com>
Authored: Mon Dec 29 19:07:24 2014 +0300
Committer: nikolay_tikhonov <nt...@gridgain.com>
Committed: Fri Jan 16 14:13:34 2015 +0300

----------------------------------------------------------------------
 .../processors/cache/IgniteCacheProxy.java      | 104 +++++++++-
 .../cache/GridCacheMetricsAdapter.java          | 110 +++++++++--
 .../cache/GridCacheAbstractMetricsSelfTest.java | 190 +++++++++++++++++++
 .../GridCachePartitionedMetricsSelfTest.java    |  10 +
 4 files changed, 396 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d8ecf41a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxy.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxy.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxy.java
index f95cd2a..10ff00b 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxy.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheProxy.java
@@ -387,14 +387,24 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
     /** {@inheritDoc} */
     @Override public V get(K key) {
         try {
+            boolean statisticsEnabled = ctx.config().isStatisticsEnabled();
+            long start = statisticsEnabled ? System.nanoTime() : 0L;
+
             GridCacheProjectionImpl<K, V> prev = gate.enter(prj);
 
+            V res;
+
             try {
-                return delegate.get(key);
+                res = delegate.get(key);
             }
             finally {
                 gate.leave(prev);
             }
+
+            if (statisticsEnabled)
+                ctx.cache().metrics0().addGetTimeNanos(System.nanoTime() - start);
+
+            return res;
         }
         catch (IgniteCheckedException e) {
             throw cacheException(e);
@@ -404,14 +414,24 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
     /** {@inheritDoc} */
     @Override public Map<K, V> getAll(Set<? extends K> keys) {
         try {
+            boolean statisticsEnabled = ctx.config().isStatisticsEnabled();
+            long start = statisticsEnabled ? System.nanoTime() : 0L;
+
             GridCacheProjectionImpl<K, V> prev = gate.enter(prj);
 
+            Map<K, V> res;
+
             try {
-                return delegate.getAll(keys);
+                res = delegate.getAll(keys);
             }
             finally {
                 gate.leave(prev);
             }
+
+            if (statisticsEnabled)
+                ctx.cache().metrics0().addGetTimeNanos(System.nanoTime() - start);
+
+            return res;
         }
         catch (IgniteCheckedException e) {
             throw cacheException(e);
@@ -432,6 +452,9 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
             finally {
                 gate.leave(prev);
             }
+
+            if (statisticsEnabled)
+                ctx.cache().metrics0().addPutTimeNanos(System.nanoTime() - start);
         }
         catch (IgniteCheckedException e) {
             throw cacheException(e);
@@ -495,6 +518,9 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
     /** {@inheritDoc} */
     @Override public void put(K key, V val) {
         try {
+            boolean statisticsEnabled = ctx.config().isStatisticsEnabled();
+            long start = statisticsEnabled ? System.nanoTime() : 0L;
+
             GridCacheProjectionImpl<K, V> prev = gate.enter(prj);
 
             try {
@@ -503,6 +529,9 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
             finally {
                 gate.leave(prev);
             }
+
+            if (statisticsEnabled)
+                ctx.cache().metrics0().addPutTimeNanos(System.nanoTime() - start);
         }
         catch (IgniteCheckedException e) {
             throw cacheException(e);
@@ -512,14 +541,26 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
     /** {@inheritDoc} */
     @Override public V getAndPut(K key, V val) {
         try {
+            boolean statisticsEnabled = ctx.config().isStatisticsEnabled();
+            long start = statisticsEnabled ? System.nanoTime() : 0L;
+
             GridCacheProjectionImpl<K, V> prev = gate.enter(prj);
 
+            V ret;
+
             try {
-                return delegate.put(key, val);
+                ret = delegate.put(key, val);
             }
             finally {
                 gate.leave(prev);
             }
+
+            if (statisticsEnabled) {
+                ctx.cache().metrics0().addPutTimeNanos(System.nanoTime() - start);
+                ctx.cache().metrics0().addGetTimeNanos(System.nanoTime() - start);
+            }
+
+            return ret;
         }
         catch (IgniteCheckedException e) {
             throw cacheException(e);
@@ -529,6 +570,9 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
     /** {@inheritDoc} */
     @Override public void putAll(Map<? extends K, ? extends V> map) {
         try {
+            boolean statisticsEnabled = ctx.config().isStatisticsEnabled();
+            long start = statisticsEnabled ? System.nanoTime() : 0L;
+
             GridCacheProjectionImpl<K, V> prev = gate.enter(prj);
 
             try {
@@ -546,14 +590,24 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
     /** {@inheritDoc} */
     @Override public boolean putIfAbsent(K key, V val) {
         try {
+            boolean statisticsEnabled = ctx.config().isStatisticsEnabled();
+            long start = statisticsEnabled ? System.nanoTime() : 0L;
+
             GridCacheProjectionImpl<K, V> prev = gate.enter(prj);
 
+            boolean stored;
+
             try {
-                return delegate.putxIfAbsent(key, val);
+                stored = delegate.putxIfAbsent(key, val);
             }
             finally {
                 gate.leave(prev);
             }
+
+            if (statisticsEnabled && stored)
+                ctx.cache().metrics0().addPutTimeNanos(System.nanoTime() - start);
+
+            return stored;
         }
         catch (IgniteCheckedException e) {
             throw cacheException(e);
@@ -563,14 +617,24 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
     /** {@inheritDoc} */
     @Override public boolean remove(K key) {
         try {
+            boolean statisticsEnabled = ctx.config().isStatisticsEnabled();
+            long start = statisticsEnabled ? System.nanoTime() : 0L;
+
             GridCacheProjectionImpl<K, V> prev = gate.enter(prj);
 
+            boolean removed;
+
             try {
-                return delegate.removex(key);
+                removed = delegate.removex(key);
             }
             finally {
                 gate.leave(prev);
             }
+
+            if (statisticsEnabled)
+                ctx.cache().metrics0().addRemoveTimeNanos(System.nanoTime() - start);
+
+            return removed;
         }
         catch (IgniteCheckedException e) {
             throw cacheException(e);
@@ -580,14 +644,24 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
     /** {@inheritDoc} */
     @Override public boolean remove(K key, V oldVal) {
         try {
+            boolean statisticsEnabled = ctx.config().isStatisticsEnabled();
+            long start = statisticsEnabled ? System.nanoTime() : 0L;
+
             GridCacheProjectionImpl<K, V> prev = gate.enter(prj);
 
+            boolean removed;
+
             try {
-                return delegate.remove(key, oldVal);
+                removed = delegate.remove(key, oldVal);
             }
             finally {
                 gate.leave(prev);
             }
+
+            if (statisticsEnabled && removed)
+                ctx.cache().metrics0().addRemoveTimeNanos(System.nanoTime() - start);
+
+            return removed;
         }
         catch (IgniteCheckedException e) {
             throw cacheException(e);
@@ -597,14 +671,24 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
     /** {@inheritDoc} */
     @Override public V getAndRemove(K key) {
         try {
+            boolean statisticsEnabled = ctx.config().isStatisticsEnabled();
+            long start = statisticsEnabled ? System.nanoTime() : 0L;
+
             GridCacheProjectionImpl<K, V> prev = gate.enter(prj);
 
+            V removed;
+
             try {
-                return delegate.remove(key);
+                removed = delegate.remove(key);
             }
             finally {
                 gate.leave(prev);
             }
+
+            if (statisticsEnabled)
+                ctx.cache().metrics0().addRemoveTimeNanos(System.nanoTime() - start);
+
+            return removed;
         }
         catch (IgniteCheckedException e) {
             throw cacheException(e);
@@ -622,6 +706,9 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
             finally {
                 gate.leave(prev);
             }
+
+            if (statisticsEnabled)
+                ctx.cache().metrics0().addRemoveTimeNanos(System.nanoTime() - start);
         }
         catch (IgniteCheckedException e) {
             throw cacheException(e);
@@ -665,6 +752,9 @@ public class IgniteCacheProxy<K, V> extends IgniteAsyncSupportAdapter implements
     /** {@inheritDoc} */
     @Override public void removeAll(Set<? extends K> keys) {
         try {
+            boolean statisticsEnabled = ctx.config().isStatisticsEnabled();
+            long start = statisticsEnabled ? System.nanoTime() : 0L;
+
             GridCacheProjectionImpl<K, V> prev = gate.enter(prj);
 
             try {

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d8ecf41a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
index 4849fa5..a878972 100644
--- a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
+++ b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/cache/GridCacheMetricsAdapter.java
@@ -20,6 +20,7 @@ package org.gridgain.grid.kernal.processors.cache;
 import org.gridgain.grid.cache.*;
 import org.gridgain.grid.util.tostring.*;
 import org.gridgain.grid.util.typedef.internal.*;
+import org.jdk8.backport.*;
 import org.jetbrains.annotations.*;
 
 import java.io.*;
@@ -29,6 +30,9 @@ import java.io.*;
  */
 public class GridCacheMetricsAdapter implements GridCacheMetrics, Externalizable {
     /** */
+    private static final long NANOS_IN_MICROSECOND = 1000L;
+
+    /** */
     private static final long serialVersionUID = 0L;
 
     /** Create time. */
@@ -70,6 +74,15 @@ public class GridCacheMetricsAdapter implements GridCacheMetrics, Externalizable
     /** Number of removed entries. */
     private volatile long rmCnt;
 
+    /** Put time taken nanos. */
+    private volatile long putTimeNanos;
+
+    /** Get time taken nanos. */
+    private volatile long getTimeNanos;
+
+    /** Remove time taken nanos. */
+    private volatile long removeTimeNanos;
+
     /** Cache metrics. */
     @GridToStringExclude
     private transient GridCacheMetricsAdapter delegate;
@@ -84,7 +97,7 @@ public class GridCacheMetricsAdapter implements GridCacheMetrics, Externalizable
     /**
      * @param m Metrics to copy from.
      */
-    public GridCacheMetricsAdapter(GridCacheMetrics m) {
+    public GridCacheMetricsAdapter(GridCacheMetricsAdapter m) {
         createTime = m.createTime();
         readTime = m.readTime();
         writeTime = m.writeTime();
@@ -98,6 +111,9 @@ public class GridCacheMetricsAdapter implements GridCacheMetrics, Externalizable
         txRollbacks = m.txRollbacks();
         rmCnt = m.getCacheRemovals();
         evictCnt = m.getCacheEvictions();
+        getTimeNanos = m.getTimeNanos();
+        putTimeNanos = m.putTimeNanos();
+        removeTimeNanos = m.removeTimeNanos();
     }
 
     /**
@@ -227,19 +243,64 @@ public class GridCacheMetricsAdapter implements GridCacheMetrics, Externalizable
         return evictCnt;
     }
 
-    /** {@inheritDoc} */
-    @Override public float getAverageGetTime() {
-        return 0;
+    /**
+     * Increments the get time accumulator.
+     *
+     * @param duration the time taken in nanoseconds.
+     */
+    public void addGetTimeNanos(long duration) {
+        getTimeNanos += duration;
     }
 
-    /** {@inheritDoc} */
-    @Override public float getAveragePutTime() {
-        return 0;
+    /**
+     * Increments the put time accumulator.
+     *
+     * @param duration the time taken in nanoseconds.
+     */
+    public void addPutTimeNanos(long duration) {
+        putTimeNanos += duration;
     }
 
-    /** {@inheritDoc} */
-    @Override public float getAverageRemoveTime() {
-        return 0;
+    /**
+     * Increments the remove time accumulator.
+     *
+     * @param duration the time taken in nanoseconds.
+     */
+    public void addRemoveTimeNanos(long duration) {
+        removeTimeNanos += duration;
+    }
+
+    @Override
+    public float getAverageGetTime() {
+        long timeNanos = getTimeNanos;
+        long readsCnt = reads;
+
+        if (timeNanos == 0 || readsCnt == 0)
+            return 0;
+
+        return ((1f * timeNanos) / readsCnt) / NANOS_IN_MICROSECOND;
+    }
+
+    @Override
+    public float getAveragePutTime() {
+        long timeNanos = putTimeNanos;
+        long putsCnt = writes;
+
+        if (timeNanos == 0 || putsCnt == 0)
+            return 0;
+
+        return ((1f * timeNanos) / putsCnt) / NANOS_IN_MICROSECOND;
+    }
+
+    @Override
+    public float getAverageRemoveTime() {
+        long timeNanos = removeTimeNanos;
+        long removesCnt = rmCnt;
+
+        if (timeNanos == 0 || removesCnt == 0)
+            return 0;
+
+        return ((1f * timeNanos) / removesCnt) / NANOS_IN_MICROSECOND;
     }
 
     /**
@@ -319,12 +380,39 @@ public class GridCacheMetricsAdapter implements GridCacheMetrics, Externalizable
     }
 
     /**
+     * Gets remove time.
+     *
+     * @return Remove time taken nanos.
+     */
+    public long removeTimeNanos() {
+        return removeTimeNanos;
+    }
+
+    /**
+     * Gets get time.
+     *
+     * @return Get time taken nanos.
+     */
+    public long getTimeNanos() {
+        return getTimeNanos;
+    }
+
+    /**
+     * Gets put time.
+     *
+     * @return Get time taken nanos.
+     */
+    public long putTimeNanos() {
+        return putTimeNanos;
+    }
+
+    /**
      * Create a copy of given metrics object.
      *
      * @param m Metrics to copy from.
      * @return Copy of given metrics.
      */
-    @Nullable public static GridCacheMetricsAdapter copyOf(@Nullable GridCacheMetrics m) {
+    @Nullable public static GridCacheMetricsAdapter copyOf(@Nullable GridCacheMetricsAdapter m) {
         if (m == null)
             return null;
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d8ecf41a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
index 5173837..82aa122 100644
--- a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
+++ b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java
@@ -85,6 +85,196 @@ public abstract class GridCacheAbstractMetricsSelfTest extends GridCacheAbstract
         }
     }
 
+    /** {@inheritDoc} */
+    @Override protected void beforeTest() throws Exception {
+        super.beforeTest();
+
+        for (int i = 0; i < gridCount(); i++) {
+            Ignite g = grid(i);
+
+            g.cache(null).configuration().setStatisticsEnabled(true);
+        }
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testRemoveAvgTime() throws Exception {
+        IgniteCache<Integer, Integer> jcache = grid(0).jcache(null);
+        GridCache<Object, Object> cache = grid(0).cache(null);
+
+        jcache.put(1, 1);
+        jcache.put(2, 2);
+
+        assertEquals(cache.metrics().getAverageRemoveTime(), 0.0, 0.0);
+
+        long start = System.nanoTime();
+
+        jcache.remove(1);
+
+        float times = (System.nanoTime() - start) * 1.f / 1000;
+
+        float averageRemoveTime = cache.metrics().getAverageRemoveTime();
+
+        assert averageRemoveTime > 0;
+
+        assertEquals(times, averageRemoveTime, times / 10);
+
+        jcache.remove(2);
+
+        assert cache.metrics().getAverageRemoveTime() > 0;
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testRemoveAllAvgTime() throws Exception {
+        IgniteCache<Integer, Integer> jcache = grid(0).jcache(null);
+        GridCache<Object, Object> cache = grid(0).cache(null);
+
+        jcache.put(1, 1);
+        jcache.put(2, 2);
+        jcache.put(3, 3);
+
+        assertEquals(cache.metrics().getAverageRemoveTime(), 0.0, 0.0);
+
+        long start = System.nanoTime();
+
+        Set<Integer> keys = new HashSet<>(4, 1);
+        keys.add(1);
+        keys.add(2);
+        keys.add(3);
+
+        jcache.removeAll(keys);
+
+        float times = (System.nanoTime() - start) * 1.f / 3 / 1000;
+
+        float averageRemoveTime = cache.metrics().getAverageRemoveTime();
+
+        assert averageRemoveTime > 0;
+        assertEquals(times, averageRemoveTime, times / 10);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testGetAvgTime() throws Exception {
+        IgniteCache<Integer, Integer> jcache = grid(0).jcache(null);
+        GridCache<Object, Object> cache = grid(0).cache(null);
+
+        jcache.put(1, 1);
+
+        assertEquals(0.0, cache.metrics().getAverageGetTime(), 0.0);
+        assertEquals(0, cache.metrics().reads());
+
+        long start = System.nanoTime();
+
+        jcache.get(1);
+
+        float times = (System.nanoTime() - start) * 1.f / 1000;
+
+        float averageGetTime = cache.metrics().getAverageGetTime();
+
+        assert averageGetTime > 0;
+
+        assertEquals(1, cache.metrics().reads());
+        assertEquals(times, averageGetTime, times / 3);
+
+        jcache.get(2);
+
+        assert cache.metrics().getAverageGetTime() > 0;
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testGetAllAvgTime() throws Exception {
+        IgniteCache<Integer, Integer> jcache = grid(0).jcache(null);
+        GridCache<Object, Object> cache = grid(0).cache(null);
+
+        jcache.put(1, 1);
+        jcache.put(2, 2);
+        jcache.put(3, 3);
+
+        assertEquals(0.0, cache.metrics().getAverageGetTime(), 0.0);
+        assertEquals(0, cache.metrics().reads());
+
+        long start = System.nanoTime();
+
+        Set<Integer> keys = new HashSet<>();
+        keys.add(1);
+        keys.add(2);
+        keys.add(3);
+
+        jcache.getAll(keys);
+
+        float times = (System.nanoTime() - start) * 1.f / 3 / 1000;
+
+        float averageGetTime = cache.metrics().getAverageGetTime();
+
+        assert averageGetTime > 0;
+        assertEquals(3, cache.metrics().reads());
+        assertEquals(times, averageGetTime, times / 3);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testPutAvgTime() throws Exception {
+        IgniteCache<Integer, Integer> jcache = grid(0).jcache(null);
+        GridCache<Object, Object> cache = grid(0).cache(null);
+
+        assertEquals(0.0, cache.metrics().getAveragePutTime(), 0.0);
+        assertEquals(0, cache.metrics().writes());
+
+        long start = System.nanoTime();
+
+        jcache.put(1, 1);
+
+        float times = (System.nanoTime() - start) * 1.f / 1000;
+
+        float averagePutTime = cache.metrics().getAveragePutTime();
+
+        assert averagePutTime > 0;
+
+        assertEquals(1, cache.metrics().writes());
+        assertEquals(times, averagePutTime, times / 3);
+
+        jcache.put(2, 2);
+
+        assert cache.metrics().getAveragePutTime() > 0;
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
+    public void testPutAllAvgTime() throws Exception {
+        IgniteCache<Integer, Integer> jcache = grid(0).jcache(null);
+        GridCache<Object, Object> cache = grid(0).cache(null);
+
+        assertEquals(0.0, cache.metrics().getAveragePutTime(), 0.0);
+        assertEquals(0, cache.metrics().writes());
+
+        Map<Integer, Integer> values = new HashMap<>();
+
+        values.put(1, 1);
+        values.put(2, 2);
+        values.put(3, 3);
+
+        long start = System.nanoTime();
+
+        jcache.putAll(values);
+
+        float times = (System.nanoTime() - start) * 1.f / 1000 / values.size();
+
+        float averagePutTime = cache.metrics().getAveragePutTime();
+
+        assert averagePutTime > 0;
+
+        assertEquals(values.size(), cache.metrics().writes());
+        assertEquals(times, averagePutTime, times / 3);
+    }
+
     /**
      * @throws Exception If failed.
      */

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d8ecf41a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridCachePartitionedMetricsSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridCachePartitionedMetricsSelfTest.java b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridCachePartitionedMetricsSelfTest.java
index 472f544..d7b37c4 100644
--- a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridCachePartitionedMetricsSelfTest.java
+++ b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/distributed/near/GridCachePartitionedMetricsSelfTest.java
@@ -57,4 +57,14 @@ public class GridCachePartitionedMetricsSelfTest extends GridCacheTransactionalA
     @Override protected int gridCount() {
         return GRID_CNT;
     }
+
+    /** {@inheritDoc} */
+    @Override public void testGetAllAvgTime() throws Exception {
+        //TODO: GG-7578
+    }
+
+    /** {@inheritDoc} */
+    @Override public void testGetAvgTime() throws Exception {
+        //TODO: GG-7578
+    }
 }