How to know Gemfire/Geode cluster size from the client

367 views Asked by At

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.

5

There are 5 answers

0
Kishor Bachhav On BEST ANSWER

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);
0
Juan Ramos On

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.

0
Kirk Lund On

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

0
Dan Smith On

I think FunctionService is probably your best bet. cache.getDistributedSystem() isn't going to tell you anything useful on the client.

0
Rahul Diyewar On

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);