I want to know the Gemfire/Geode cluster size (number of members in the ds) from the clients. What is the simplest way to know this. We can get this using Function Service, but i am looking for the easier solution.
-
The following was written by Charlie Black on publishing stats: There is an easy method to grab stats - I made a quick project that shows how. https://github.com/charliemblack/geode-exposing-metrics-via-JMX The class that does the good stuff is in this class. https://github.com/charliemblack/geode-exposing-metrics-via-JMX/blob/master/src/main/java/demo/geode/DemoInitializer.java – Wes Williams Aug 01 '17 at 15:37
5 Answers
I think FunctionService is probably your best bet. cache.getDistributedSystem() isn't going to tell you anything useful on the client.

- 481
- 2
- 3
Another approach involves using JMX. If you have one or more JMX Managers running in your server cluster then you can have any JVM (including a loner GemFire client) connect as a JMX client and query its mbeans.
The mbean you're interested in is DistributedSystemMXBean. It exposes:
/**
* Returns the number of members in the distributed system.
*/
public int getMemberCount();
It also exposes various other methods for listing members, groups, locators, diskstores, etc. for the entire cluster. The javadocs show all of these attributes and operations: http://gemfire.docs.pivotal.io/latest/javadocs/japi/com/gemstone/gemfire/management/DistributedSystemMXBean.html

- 106
- 6
I haven't tried it yet, but I believe you can execute "cache.getDistributedSystem().getAllOtherMembers()" from the client cache after you connecting. Here's the javadoc for the "DistributedSystem" class: http://gemfire.docs.pivotal.io/latest/javadocs/japi/index.html. Cheers.

- 1,421
- 1
- 8
- 13
Yes, client runs in lonar distributed system thus cache.getDistributedSystem()
from client gives instance of lonar distributed system, which doesn't have information about server cluster.
One way of doing this is using geode region itself i.e. each server puts member-id an entry in some region, then retrieve the same information from the client.
Other way is using Function Service. Below is the code snippet for the same.
Define Function Adapter:
public class CountMemberFunction extends FunctionAdapter {
private static final long serialVersionUID = 1L;
@Override
public void execute(FunctionContext fc) {
Cache c = CacheFactory.getAnyInstance();
String member = c.getDistributedSystem().getDistributedMember().getId();
fc.getResultSender().lastResult(member);
}
@Override
public String getId() {
return getClass().getName();
}
}
Register it in server:
FunctionService.registerFunction(new CountMemberFunction());
Execute the function onServers from client:
Execution execution = FunctionService.onServers(gemCache);
ResultCollector rc = execution.execute(new CountMemberFunction());
List nodes = (List)rc.getResult();
println("Server nodes are " + nodes);

- 60
- 4
Urizen, As Rahul said, client is a "loner distributed system" which is connected to another distributed system (which has multiple servers, locators, peer members), executing "cache.getDistributedSystem.getAllOhterMembers" on client DS, will give you member info of client only and not of server side distributed system.
Rahul's code snippet will provide information about server members only not peer members available in DS.
IMO we need to change Rahul's code slightly to consider peer members, locators too.
public class CountMemberFunction extends FunctionAdapter {
private static final long serialVersionUID = 1L;
@Override
public void execute(FunctionContext fc) {
Cache c = CacheFactory.getAnyInstance();
//Get other members of the system
Set<DistributedMember> dsMembers=
c.getDistributedSystem().getAllOtherMembers();
// add this member
dsMembers.add(c.getDistributedSystem().getDistributedMember)
fc.getResultSender().lastResult(dsMembers);
}
@Override
public String getId() {
return getClass().getName();
}
}
And use onServer instead of onServers
Execution execution = FunctionService.onServer(gemCache);
ResultCollector rc = execution.execute(new CountMemberFunction());
List nodes = (List)rc.getResult();
println("Server nodes are " + nodes);

- 181
- 9