How to get GemFire HTTP Session information from GemFire servers using gfsh
search cancel

How to get GemFire HTTP Session information from GemFire servers using gfsh

book

Article ID: 294359

calendar_today

Updated On:

Products

VMware Tanzu Gemfire

Issue/Introduction

This article describes to get GemFire HTTP Session information from GemFire servers using gfsh.

Environment

Product Version: 9.9

Resolution

Execute one of the following functions, once registered on the server, to get the GemFire Session on the GemFire server.
 

  • This function is an on-region function and should be executed as follows:
execute function --id=DumpSessionsFunction --region=/gemfire_modules_sessions
    import org.apache.geode.cache.Declarable;
    ​
    import org.apache.geode.cache.execute.Function;
    import org.apache.geode.cache.execute.FunctionContext;
    import org.apache.geode.cache.execute.RegionFunctionContext;
    ​
    import org.apache.geode.cache.partition.PartitionRegionHelper;
    ​
    import org.apache.geode.modules.session.catalina.DeltaSession;
    ​
    import java.util.Map;
    ​
    import java.util.stream.Collectors;
    ​
    public class DumpSessionsFunction implements Function, Declarable {
      
      public void execute(FunctionContext context) {
        RegionFunctionContext rfc = (RegionFunctionContext) context;
        Map<String,DeltaSession> region = PartitionRegionHelper.getLocalDataForContext(rfc);
        String sessions = getSessions(region);
        context.getCache().getLogger().info(sessions);
        context.getResultSender().lastResult(true);
      }
      
      private String getSessions(Map<String,DeltaSession> region) {
        String header = "This member contains the following " + region.size() + " primary sessions";
        return region.entrySet()
          .stream()
          .sorted(Map.Entry.comparingByKey())
          .map(entry -> getEntry(entry))
          .collect(Collectors.joining("\n", header, ""));
      }
    ​
      private String getEntry(Map.Entry<String,DeltaSession> entry) {
        DeltaSession session = entry.getValue();
        return new StringBuilder()
          .append("\t\t")
          .append(session.getClass().getSimpleName())
          .append("[")
          .append("id=").append(session.getId())
          .append("; creationTime=").append(session.getCreationTime())
          .append("; lastAccessedTime=").append(session.getLastAccessedTime())
          .append("; maxInactiveInterval=").append(session.getMaxInactiveInterval())
          .append("; sizeInBytes=").append(session.getSizeInBytes())
          .append("]")
          .toString();
      }
    ​
      public String getId() {
        return getClass().getSimpleName();
      }
    ​
      public boolean optimizeForWrite() {
        return true;
      }
    }
    • This function is an on-server function and should be executed as follows:
    execute function --id=DumpSessionsOnServersFunction --arguments=/gemfire_modules_sessions
    import org.apache.geode.cache.Declarable;
    import org.apache.geode.cache.Region;
    ​
    import org.apache.geode.cache.execute.Function;
    import org.apache.geode.cache.execute.FunctionContext;
    ​
    import org.apache.geode.cache.partition.PartitionRegionHelper;
    ​
    import org.apache.geode.modules.session.catalina.DeltaSession;
    ​
    import java.util.Map;
    ​
    import java.util.stream.Collectors;
    ​
    public class DumpSessionsOnServersFunction implements Function, Declarable {
      
      public void execute(FunctionContext context) {
        Object[] arguments = (Object[]) context.getArguments();
        String regionPath = (String) arguments[0];
        Region pr = context.getCache().getRegion(regionPath);
        Map<String,DeltaSession> lds = PartitionRegionHelper.getLocalData(pr);
        String sessions = getSessions(lds);
        context.getCache().getLogger().info(sessions);
        context.getResultSender().lastResult(true);
      }
      
      private String getSessions(Map<String,DeltaSession> region) {
        String header = "This member contains the following " + region.size() + " sessions:\n\n";
        return region.entrySet()
          .stream()
          .sorted(Map.Entry.comparingByKey())
          .map(entry -> getEntry(entry))
          .collect(Collectors.joining("\n", header, ""));
      }
    ​
      private String getEntry(Map.Entry<String,DeltaSession> entry) {
        DeltaSession session = entry.getValue();
        return new StringBuilder()
          .append("\t")
          .append(session.getClass().getSimpleName())
          .append("[")
          .append("id=").append(session.getId())
          .append("; creationTime=").append(session.getCreationTime())
          .append("; lastAccessedTime=").append(session.getLastAccessedTime())
          .append("; maxInactiveInterval=").append(session.getMaxInactiveInterval())
          .append("; sizeInBytes=").append(session.getSizeInBytes())
          .append("]")
          .toString();
      }
    ​
      public String getId() {
        return getClass().getSimpleName();
      }
    ​
      public boolean optimizeForWrite() {
        return true;
      }
    }
    

     

    • Executing the function will log a message such as below on GemFire Servers:
    [info 2020/10/20 16:05:42.064 PDT <Function Execution Processor3> tid=0x63] This member contains the following 179 primary sessions		DeltaSession8[id=0194B9F5042979F190F495035BD5D77C; creationTime=1603235055989; lastAccessedTime=1603235055998; maxInactiveInterval=300; sizeInBytes=9181]
      DeltaSession8[id=01A1F9C0FD3F6EA0D252566185334E2E; creationTime=1603235059051; lastAccessedTime=1603235059060; maxInactiveInterval=300; sizeInBytes=9357]
      DeltaSession8[id=0353F8F6479EB9100CBDC06991FBE111; creationTime=1603235057442; lastAccessedTime=1603235057451; maxInactiveInterval=300; sizeInBytes=29382]
      DeltaSession8[id=0395408AAE89C428C368FD81A782689D; creationTime=1603235066342; lastAccessedTime=1603235066350; maxInactiveInterval=300; sizeInBytes=3837]
      DeltaSession8[id=08D8D6ABC62507C5DC051E8FCC3B3469; creationTime=1603235063275; lastAccessedTime=1603235063284; maxInactiveInterval=300; sizeInBytes=14972]
      DeltaSession8[id=09B9C260071666E9B62D275DD8F89E30; creationTime=1603235051629; lastAccessedTime=1603235051639; maxInactiveInterval=300; sizeInBytes=0]
      DeltaSession8[id=09FD30D29D4702D6E1E66295F1C701D9; creationTime=1603235060011; lastAccessedTime=1603235060020; maxInactiveInterval=300; sizeInBytes=9704]
      DeltaSession8[id=0A7CDF709E1591CD25F831E2CFC2B2B7; creationTime=1603235063794; lastAccessedTime=1603235063803; maxInactiveInterval=300; sizeInBytes=0]
      DeltaSession8[id=0BA96F44AE36878CFF862FB93E2E9798; creationTime=1603235063760; lastAccessedTime=1603235063769; maxInactiveInterval=300; sizeInBytes=19866]
      DeltaSession8[id=0F1748A03760CDDD78E2D8AA62317FD6; creationTime=1603235064985; lastAccessedTime=1603235064994; maxInactiveInterval=300; sizeInBytes=11868]
      ...
    

    Please refer to the following link on the way to register and execute the function:
    https://gemfire.docs.pivotal.io/910/geode/developing/function_exec/function_execution.html