2016-09-27 60 views
0

我已經爲簡單的內存緩存放置操作編寫了以下代碼。使用緩存指標我試圖在當前時刻獲取緩存投入數量。但總是我得到0作爲緩存投入的數量。我是否缺少一些配置設置?在Apache Ignite中未更新緩存度量標準

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

cache.localMetrics()返回其事後又沒有更新的快照。嘗試再次調用它後,你應該得到正確的值在這種情況下。

相關問題