I have a configuration where cache is kept in one node and accessed from another. Although I am able to get() and put() perfectly fine, some operations like size(), keySet() etc don't return me correct results.
Test1 Client node cache config
<bean id="test-cache" class="org.gridgain.grid.cache.GridCacheConfiguration">
<property name="name" value="testCache"/>
<property name="cacheMode" value="PARTITIONED"/>
<property name="distributionMode" value="CLIENT_ONLY" />
<property name="swapEnabled" value="true"/>
</bean>
Test1 Client Node Class
public class GridGainTest1
{
public static void main(String[] args) throws Exception
{
//Client Mode
Grid g = GridGain.start("etc/config/grid-test1.xml");
//Put in Remote Cache
g.cache("testCache").put(1, "ABC");
g.cache("testCache").put(2, "XYZ");
System.out.println("Size of Cache :- " + g.cache("testCache").size());
System.out.println("Value for 1 :- " + g.cache("testCache").get(1));
System.out.println("Value for 2 :- " + g.cache("testCache").get(2));
}
Test2 data node cache config
<bean id="test-cache" class="org.gridgain.grid.cache.GridCacheConfiguration">
<property name="name" value="testCache"/>
<property name="cacheMode" value="PARTITIONED"/>
<property name="swapEnabled" value="true"/>
</bean>
Test2 Data Node Class
public class GridGainTest2
{
public static void main(String[] args) throws Exception
{
Grid g = GridGain.start("etc/config/grid-test2.xml");
}
}
The output from Node 1 is as follows where size comes in as 0 even though there are entries in the map. I am not sure if this is due to some misconfiguration.
Size of Cache :- 0
Value for 1 :- ABC
Value for 2 :- XYZ
In GridGain cache API methods size()
, primarySize()
, nearSize()
, keySet()
, primaryKeySet()
, values()
, primaryValues()
, entrySet()
, primaryEntrySet()
are local, they will return sizes or collections only for keys stored on the local node.
In your case you started the cache on Test1 in CLIENT_ONLY
mode, so that node does not store any keys. That is the reason why you always see 0
as cache size.
If you need global cache size, you can use the following code:
GridCallable<Integer> sizeCallable = new GridCallable<Integer>() {
@Override public Integer call() throws Exception {
return g.cache("testCache").size();
}
};
Collection<Integer> sizes = g.forCache("testCache").compute()
.broadcast(sizeCallable).get();
int globalSize = 0;
for (Integer s : sizes)
globalSize += s;
Convenience GridCache.globalSize()
method will be added in the upcoming GridGain 6.2 release.