Search code examples
cachingcluster-computinggridgain

How to get keySet() and size() for entire GridGain cluster?


GridCache.keySet(), .primarySize(), and .size() only return information for that node.

How do I get these information but for the whole cluster?

Scanning the entire cluster "works", but all I need is the keys or the count, not the values.

The problem is SQL query works if I want to find based on an indexed field, but I can't find based on the grid cache entry key itself.

My workaround that works but far from elegant and performant is:

Set<String> ruleIds = FluentIterable.from(cache.queries().createSqlFieldsQuery("SELECT property FROM YagoRule").execute().get())
        .<String>transform((it) -> (String) it.iterator().next()).toSet();

This requires the key is the same as one of the field, and the field need to be indexed for performance reasons.


Solution

  • Next release of GridGain (6.2.0) will have globalSize() and globalPrimarySize() methods which will ask the cluster for the sizes.

    For now you can use the following code:

    // Only grab nodes on which cache "mycache" is started.
    GridCompute compute = grid.forCache("mycache").compute();
    
    Collection<Integer> res = compute.broadcast(
        // This code will execute on every caching node.
        new GridCallable<Integer>() {
             @Override public Integer call() {
                 return grid.cache("mycache").size();
             }
        }
    ).get();
    
    int sum = 0;
    
    for (Integer i : res)
        sum += i;