@Override
 public String toString() {
   String expireTime = "<unavailable>";
   try {
     expireTime = String.valueOf(getExpirationTime());
   } catch (VirtualMachineError err) {
     SystemFailure.initiateFailure(err);
     // If this ever returns, rethrow the error.  We're poisoned
     // now, so don't let this thread continue.
     throw err;
   } catch (Throwable e) {
     // Whenever you catch Error or Throwable, you must also
     // catch VirtualMachineError (see above).  However, there is
     // _still_ a possibility that you are dealing with a cascading
     // error condition, so you also need to check to see if the JVM
     // is still usable:
     SystemFailure.checkFailure();
   }
   return super.toString()
       + " for "
       + getLocalRegion().getFullPath()
       + ", expiration time: "
       + expireTime
       + " [now: "
       + getNow()
       + "]";
 }
 public static int compare(Number param, Number threshold) throws Throwable {
   try {
     int eval = 0;
     if (threshold instanceof Double) {
       eval = Double.compare(param.doubleValue(), threshold.doubleValue());
     } else if (threshold instanceof Float) {
       eval = Float.compare(param.floatValue(), threshold.floatValue());
     } else if (threshold instanceof Long) {
       eval = (Long.valueOf(param.longValue())).compareTo(Long.valueOf(threshold.longValue()));
     } else if (threshold instanceof Integer) {
       eval = param.intValue() > threshold.intValue() ? 1 : -1;
     }
     return eval;
   } catch (VirtualMachineError err) {
     SystemFailure.initiateFailure(err);
     // If this ever returns, rethrow the error.  We're poisoned
     // now, so don't let this thread continue.
     throw err;
   } catch (Throwable e) {
     // Whenever you catch Error or Throwable, you must also
     // catch VirtualMachineError (see above).  However, there is
     // _still_ a possibility that you are dealing with a cascading
     // error condition, so you also need to check to see if the JVM
     // is still usable:
     SystemFailure.checkFailure();
     throw e;
   }
 }
 @Override
 protected void process(DistributionManager dm) {
   try {
     PartitionedRegion pr = PartitionedRegion.getPRFromId(this.prId);
     for (Map.Entry<Integer, BucketAdvisor.BucketProfile> profile : this.profiles.entrySet()) {
       pr.getRegionAdvisor().putBucketProfile(profile.getKey(), profile.getValue());
     }
   } catch (PRLocallyDestroyedException fre) {
     if (logger.isDebugEnabled()) logger.debug("<region locally destroyed> ///{}", this);
   } catch (RegionDestroyedException e) {
     if (logger.isDebugEnabled()) logger.debug("<region destroyed> ///{}", this);
   } catch (CancelException e) {
     if (logger.isDebugEnabled()) logger.debug("<cache closed> ///{}", this);
   } catch (VirtualMachineError err) {
     SystemFailure.initiateFailure(err);
     // If this ever returns, rethrow the error.  We're poisoned
     // now, so don't let this thread continue.
     throw err;
   } catch (Throwable ignore) {
     // Whenever you catch Error or Throwable, you must also
     // catch VirtualMachineError (see above).  However, there is
     // _still_ a possibility that you are dealing with a cascading
     // error condition, so you also need to check to see if the JVM
     // is still usable:
     SystemFailure.checkFailure();
   } finally {
     if (this.processorId != 0) {
       ReplyMessage.send(getSender(), this.processorId, null, dm);
     }
   }
 }
 /* (non-Javadoc)
  * @see com.gemstone.gemfire.distributed.internal.DistributionMessage#process(com.gemstone.gemfire.distributed.internal.DistributionManager)
  */
 @Override
 protected void process(DistributionManager dm) {
   Throwable thr = null;
   JmxManagerProfile p = null;
   try {
     final GemFireCacheImpl cache = GemFireCacheImpl.getInstance();
     if (cache != null && !cache.isClosed()) {
       final JmxManagerAdvisor adv = cache.getJmxManagerAdvisor();
       p = this.profile;
       if (p != null) {
         adv.putProfile(p);
       }
     } else {
       if (logger.isDebugEnabled()) {
         logger.debug("No cache {}", this);
       }
     }
   } catch (CancelException e) {
     if (logger.isDebugEnabled()) {
       logger.debug("Cache closed, ", this);
     }
   } catch (VirtualMachineError err) {
     SystemFailure.initiateFailure(err);
     // If this ever returns, rethrow the error.  We're poisoned
     // now, so don't let this thread continue.
     throw err;
   } catch (Throwable t) {
     // Whenever you catch Error or Throwable, you must also
     // catch VirtualMachineError (see above).  However, there is
     // _still_ a possibility that you are dealing with a cascading
     // error condition, so you also need to check to see if the JVM
     // is still usable:
     SystemFailure.checkFailure();
     thr = t;
   } finally {
     if (thr != null) {
       dm.getCancelCriterion().checkCancelInProgress(null);
       logger.info(
           LocalizedMessage.create(
               LocalizedStrings.ResourceAdvisor_MEMBER_CAUGHT_EXCEPTION_PROCESSING_PROFILE,
               new Object[] {p, toString()},
               thr));
     }
   }
 }
 public static boolean isLessThan(Number param, Number threshold) {
   try {
     int eval = compare(param, threshold);
     return eval < 0;
   } catch (VirtualMachineError err) {
     SystemFailure.initiateFailure(err);
     // If this ever returns, rethrow the error.  We're poisoned
     // now, so don't let this thread continue.
     throw err;
   } catch (Throwable e) {
     // Whenever you catch Error or Throwable, you must also
     // catch VirtualMachineError (see above).  However, there is
     // _still_ a possibility that you are dealing with a cascading
     // error condition, so you also need to check to see if the JVM
     // is still usable:
     SystemFailure.checkFailure();
     return false;
   }
 }
  protected void run(final String[] args) {
    try {
      final String command = args[0];

      if (command.equalsIgnoreCase("start")) {
        start(args);
      } else if (command.equalsIgnoreCase("server")) {
        server(args);
      } else if (command.equalsIgnoreCase("stop")) {
        stop(args);
      } else if (command.equalsIgnoreCase("status")) {
        status(args);
      } else if (command.toLowerCase().matches("-{0,2}help")) {
        if (args.length > 1) {
          final String topic = args[1];

          if (topic.equals("config")) {
            configHelp();
          } else {
            usage(LocalizedStrings.AgentLauncher_NO_HELP_AVAILABLE_FOR_0.toLocalizedString(topic));
          }
        }

        usage(LocalizedStrings.AgentLauncher_AGENT_HELP.toLocalizedString());
      } else {
        usage(LocalizedStrings.AgentLauncher_UNKNOWN_COMMAND_0.toLocalizedString(command));
      }
    } catch (Throwable t) {
      Error err;
      if (t instanceof Error && SystemFailure.isJVMFailureError(err = (Error) t)) {
        SystemFailure.initiateFailure(err);
        // If this ever returns, rethrow the error. We're poisoned
        // now, so don't let this thread continue.
        throw err;
      }
      SystemFailure.checkFailure();
      t.printStackTrace();
      System.err.println(
          LocalizedStrings.AgentLauncher_ERROR_0.toLocalizedString(t.getLocalizedMessage()));
      System.exit(1);
    }
  }
  /**
   * Bootstrap method to launch the GemFire JMX Agent process to monitor and manage a GemFire
   * Distributed System/Cache. Main will read the arguments passed on the command line and dispatch
   * the command to the appropriate handler.
   */
  public static void main(final String[] args) {
    if (args.length < 1) {
      usage(LocalizedStrings.AgentLauncher_MISSING_COMMAND.toLocalizedString());
    }

    // TODO is this only needed on 'agent server'?  'agent {start|stop|status}' technically do no
    // run any GemFire Cache
    // or DS code inside the current process.
    SystemFailure.loadEmergencyClasses();

    final AgentLauncher launcher = new AgentLauncher("Agent");

    launcher.run(args);
  }
  private Agent createAgent(final Properties props) throws IOException, AdminException {
    DistributionManager.isDedicatedAdminVM = true;
    SystemFailure.setExitOK(true);

    final AgentConfigImpl config = createAgentConfig(props);

    // see bug 43760
    if (config.getLogFile() == null || "".equals(config.getLogFile().trim())) {
      config.setLogFile(AgentConfigImpl.DEFAULT_LOG_FILE);
    }

    logger = config.getLogWriter();
    OSProcess.redirectOutput(
        new File(config.getLogFile())); // redirect output to the configured log file

    return AgentFactory.getAgent(config);
  }
  /**
   * Upon receipt of the message, both process the message and send an acknowledgement, not
   * necessarily in that order. Note: Any hang in this message may cause a distributed deadlock for
   * those threads waiting for an acknowledgement.
   *
   * @throws PartitionedRegionException if the region does not exist (typically, if it has been
   *     destroyed)
   */
  @Override
  public void process(final DistributionManager dm) {
    Throwable thr = null;
    boolean sendReply = true;
    LocalRegion r = null;
    long startTime = 0;
    try {
      if (checkCacheClosing(dm) || checkDSClosing(dm)) {
        thr =
            new CacheClosedException(
                LocalizedStrings.PartitionMessage_REMOTE_CACHE_IS_CLOSED_0.toLocalizedString(
                    dm.getId()));
        return;
      }
      GemFireCacheImpl gfc = (GemFireCacheImpl) CacheFactory.getInstance(dm.getSystem());
      r = gfc.getRegionByPathForProcessing(this.regionPath);
      if (r == null && failIfRegionMissing()) {
        // if the distributed system is disconnecting, don't send a reply saying
        // the partitioned region can't be found (bug 36585)
        thr =
            new RegionDestroyedException(
                LocalizedStrings.RemoteOperationMessage_0_COULD_NOT_FIND_REGION_1.toLocalizedString(
                    new Object[] {dm.getDistributionManagerId(), regionPath}),
                regionPath);
        return; // reply sent in finally block below
      }

      thr = UNHANDLED_EXCEPTION;

      // [bruce] r might be null here, so we have to go to the cache instance to get the txmgr
      TXManagerImpl txMgr = GemFireCacheImpl.getInstance().getTxManager();
      TXStateProxy tx = null;
      try {
        tx = txMgr.masqueradeAs(this);
        sendReply = operateOnRegion(dm, r, startTime);
      } finally {
        txMgr.unmasquerade(tx);
      }
      thr = null;

    } catch (RemoteOperationException fre) {
      thr = fre;
    } catch (DistributedSystemDisconnectedException se) {
      // bug 37026: this is too noisy...
      //      throw new CacheClosedException("remote system shutting down");
      //      thr = se; cache is closed, no point trying to send a reply
      thr = null;
      sendReply = false;
      if (logger.isDebugEnabled()) {
        logger.debug("shutdown caught, abandoning message: {}", se.getMessage(), se);
      }
    } catch (RegionDestroyedException rde) {
      // [bruce] RDE does not always mean that the sender's region is also
      //         destroyed, so we must send back an exception.  If the sender's
      //         region is also destroyed, who cares if we send it an exception
      // if (pr != null && pr.isClosed) {
      thr =
          new ForceReattemptException(
              LocalizedStrings.PartitionMessage_REGION_IS_DESTROYED_IN_0.toLocalizedString(
                  dm.getDistributionManagerId()),
              rde);
      // }
    } catch (VirtualMachineError err) {
      SystemFailure.initiateFailure(err);
      // If this ever returns, rethrow the error.  We're poisoned
      // now, so don't let this thread continue.
      throw err;
    } catch (Throwable t) {
      // Whenever you catch Error or Throwable, you must also
      // catch VirtualMachineError (see above).  However, there is
      // _still_ a possibility that you are dealing with a cascading
      // error condition, so you also need to check to see if the JVM
      // is still usable:
      SystemFailure.checkFailure();
      // log the exception at fine level if there is no reply to the message
      thr = null;
      if (sendReply) {
        if (!checkDSClosing(dm)) {
          thr = t;
        } else {
          // don't pass arbitrary runtime exceptions and errors back if this
          // cache/vm is closing
          thr =
              new ForceReattemptException(
                  LocalizedStrings.PartitionMessage_DISTRIBUTED_SYSTEM_IS_DISCONNECTING
                      .toLocalizedString());
        }
      }
      if (logger.isTraceEnabled(LogMarker.DM) && (t instanceof RuntimeException)) {
        logger.trace(LogMarker.DM, "Exception caught while processing message", t);
      }
    } finally {
      if (sendReply) {
        ReplyException rex = null;

        if (thr != null) {
          // don't transmit the exception if this message was to a listener
          // and this listener is shutting down
          rex = new ReplyException(thr);
        }

        // Send the reply if the operateOnPartitionedRegion returned true
        sendReply(getSender(), this.processorId, dm, rex, r, startTime);
      }
    }
  }
 @Override
 protected void process(DistributionManager dm) {
   logger.trace(LogMarker.STATE_FLUSH_OP, "Processing {}", this);
   if (dm.getDistributionManagerId().equals(relayRecipient)) {
     // no need to send a relay request to this process - just send the
     // ack back to the sender
     StateStabilizedMessage ga = new StateStabilizedMessage();
     ga.sendingMember = relayRecipient;
     ga.setRecipient(this.getSender());
     ga.setProcessorId(processorId);
     dm.putOutgoing(ga);
   } else {
     // 1) wait for all messages based on the membership version (or older)
     //    at which the sender "joined" this region to be put on the pipe
     // 2) record the state of all communication channels from this process
     //    to the relay point
     // 3) send a stabilization message to the relay point that holds the
     //    communication channel state information
     StateStabilizationMessage gr = new StateStabilizationMessage();
     gr.setRecipient((InternalDistributedMember) relayRecipient);
     gr.requestingMember = this.getSender();
     gr.processorId = processorId;
     try {
       Set<DistributedRegion> regions;
       if (this.allRegions) {
         regions = getAllRegions(dm);
       } else {
         regions = Collections.singleton(this.getRegion(dm));
       }
       for (DistributedRegion r : regions) {
         if (r == null) {
           if (logger.isTraceEnabled(LogMarker.DM)) {
             logger.trace(LogMarker.DM, "Region not found - skipping channel state assessment");
           }
         }
         if (r != null) {
           if (this.allRegions && r.doesNotDistribute()) {
             // no need to flush a region that does no distribution
             continue;
           }
           boolean initialized = r.isInitialized();
           if (initialized) {
             if (this.flushNewOps) {
               r.getDistributionAdvisor()
                   .forceNewMembershipVersion(); // force a new "view" so we can track current
               // ops
             }
             try {
               r.getDistributionAdvisor().waitForCurrentOperations();
             } catch (RegionDestroyedException e) {
               // continue with the next region
             }
           }
           boolean useMulticast =
               r.getMulticastEnabled() && r.getSystem().getConfig().getMcastPort() != 0;
           if (initialized) {
             Map channelStates =
                 dm.getMembershipManager().getMessageState(relayRecipient, useMulticast);
             if (gr.channelState != null) {
               gr.channelState.putAll(channelStates);
             } else {
               gr.channelState = channelStates;
             }
             if (logger.isTraceEnabled(LogMarker.STATE_FLUSH_OP)
                 && ((gr.channelState != null) && (gr.channelState.size() > 0))) {
               logger.trace(
                   LogMarker.STATE_FLUSH_OP,
                   "channel states: {}",
                   gr.channelStateDescription(gr.channelState));
             }
           }
         }
       }
     } catch (CancelException cce) {
       // cache is closed - no distribution advisor available for the region so nothing to do but
       // send the stabilization message
     } catch (Exception e) {
       logger.fatal(
           LocalizedMessage.create(
               LocalizedStrings
                   .StateFlushOperation_0__EXCEPTION_CAUGHT_WHILE_DETERMINING_CHANNEL_STATE,
               this),
           e);
     } catch (ThreadDeath td) {
       throw td;
     } catch (VirtualMachineError err) {
       SystemFailure.initiateFailure(err);
       // If this ever returns, rethrow the error.  We're poisoned
       // now, so don't let this thread continue.
       throw err;
     } catch (Throwable t) {
       // Whenever you catch Error or Throwable, you must also
       // catch VirtualMachineError (see above).  However, there is
       // _still_ a possibility that you are dealing with a cascading
       // error condition, so you also need to check to see if the JVM
       // is still usable:
       SystemFailure.checkFailure();
       logger.fatal(
           LocalizedMessage.create(
               LocalizedStrings
                   .StateFlushOperation_0__THROWABLE_CAUGHT_WHILE_DETERMINING_CHANNEL_STATE,
               this),
           t);
     } finally {
       if (logger.isTraceEnabled(LogMarker.STATE_FLUSH_OP)) {
         logger.trace(LogMarker.STATE_FLUSH_OP, "Sending {}", gr);
       }
       dm.putOutgoing(gr);
     }
   }
 }