Apache Ignite中未更新缓存度量标准

时间:2016-09-27 16:30:23

标签: gridgain ignite

我已经为简单的内存缓存放置操作编写了以下代码。使用缓存指标我试图在当前时刻获得缓存放置次数。但总是我得到0作为缓存放置的数量。我错过了一些配置设置吗?

import java.io.Serializable;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.IgniteException;
import org.apache.ignite.Ignition;
import org.apache.ignite.cache.CacheMetrics;
import org.apache.ignite.cache.query.annotations.QuerySqlField;
import org.apache.ignite.cluster.ClusterMetrics;
import org.apache.ignite.cluster.ClusterNode;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;


class Person implements Serializable 
{
     /** Will be indexed in ascending order. */
     @QuerySqlField(index = true)
     private long id;

     /** Will be visible in SQL, but not indexed. */
     @QuerySqlField
     private String name;

     /** Will be indexed in descending order. */
     @QuerySqlField(index = true, descending = true)
     private int age;

     Person(long id1, String str, int age1)
     {
          id = id1; name = str; age = age1;
     }

     void get_details()
     {
          System.out.println(id+" "+name+" "+age);
     }

     public long key()
     {                
          return id % 10; 
     }
 }

 public class profile_test 
 {
     public static void main(String[] args) throws IgniteException, InterruptedException 
     {

         final IgniteConfiguration igniteConfiguration = new IgniteConfiguration().setGridName("experiments").setMetricsUpdateFrequency(1); // every millisecond 


         try (Ignite ignite = Ignition.start(igniteConfiguration)) 
         { 
              // Local Ignite node.
              ClusterNode localNode = ignite.cluster().localNode();

              // Node metrics.
              ClusterMetrics metrics = localNode.metrics();


              CacheConfiguration<Object,Object> ccfg = new CacheConfiguration< ();


              ccfg.setIndexedTypes(Long.class, Person.class);
              ccfg.setName("mycache");
              ccfg.setStatisticsEnabled(true);
              IgniteCache<Object, Object> cache = ignite.getOrCreateCache(ccfg);


              CacheMetrics cmetrics = cache.localMetrics();


              Person p = null;
              p = new Person(1, "aaaa", 1);
              cache.put(p.key(), p);
              p = new Person(2, "bbbb", 40);
              cache.put(p.key(), p);
              p = new Person(3, "cccc", 20);
              cache.put(p.key(), p);
              p = new Person(4, "dddd", 26);
              cache.put(p.key(), p);
              p = new Person(5, "eeee", 77);
              cache.put(p.key(), p);
              p = new Person(6, "ffff", 35);
              cache.put(p.key(), p);
              p = new Person(7, "gggg", 65);
              cache.put(p.key(), p);

              //sleep to update metrics
              Thread.sleep(10);
              long puts = cmetrics.getCacheGets();
              long gets = cmetrics.getCachePuts();

              System.out.println(" puts = "+puts+" gets = "+gets);
              p = new Person(8, "hhhh", 15);
              cache.put(p.key(), p);
              p = new Person(9, "iiii", 5);
              cache.put(p.key(), p);
              p = new Person(10, "jjjj", 24);
              cache.put(p.key(), p);


              puts = cmetrics.getCacheGets();
              gets = cmetrics.getCachePuts();
              Thread.sleep(10);
              System.out.println(" puts = "+puts+" gets = "+gets);
              for (int i = 0; i < 10; i++)
                  cache.remove(i);
         }      
    }    
}

以下是我的代码输出:

[18:45:30]    __________  ________________ 
[18:45:30]   /  _/ ___/ |/ /  _/_  __/ __/ 
[18:45:30]  _/ // (7 7    // /  / / / _/   
[18:45:30] /___/\___/_/|_/___/ /_/ /___/  
[18:45:30] 
[18:45:30] ver. 1.7.0#20160801-sha1:383273e3
[18:45:30] 2016 Copyright(C) Apache Software Foundation
[18:45:30] 
[18:45:30] Ignite documentation: http://ignite.apache.org
[18:45:30] 
[18:45:30] Quiet mode.
[18:45:30]   ^-- Logging to file '/opt/apache-ignite-fabric-1.7.0-    bin/work/log/ignite-048ea583.0.log'
[18:45:30]   ^-- To see **FULL** console log here add -DIGNITE_QUIET=false or "-v" to ignite.{sh|bat}
[18:45:30] 
[18:45:30] OS: Linux 2.6.32-431.el6.x86_64 amd64
[18:45:30] VM information: Java(TM) SE Runtime Environment 1.8.0_71-b15 Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.71-b15
[18:45:30] Configured plugins:
[18:45:30]   ^-- None
[18:45:30] 
[18:45:31] Security status [authentication=off, tls/ssl=off]
[18:45:32] To start Console Management & Monitoring run ignitevisorcmd.{sh|bat}
[18:45:32] 
[18:45:32] Ignite node started OK (id=048ea583, grid=experiments)
[18:45:32] Topology snapshot [ver=56, servers=1, clients=0, CPUs=48, heap=14.0GB]
 puts = 0 gets = 0
 puts = 0 gets = 0
[18:45:32] Ignite node stopped OK [name=experiments, uptime=00:00:00:213]

1 个答案:

答案 0 :(得分:1)

cache.localMetrics()返回之后未更新的快照。在完成看跌后尝试再次调用它,在这种情况下你应该得到正确的值。