/**
  * Load a region with keys and values. The number of keys and values is specified by the total
  * number of keys in keyIntervals. This can be invoked by several threads to accomplish the work.
  */
 public void loadRegion() {
   final long LOG_INTERVAL_MILLIS = 10000;
   int numKeysToCreate = keyIntervals.getNumKeys();
   long lastLogTime = System.currentTimeMillis();
   long startTime = System.currentTimeMillis();
   SharedCounters sc = CQUtilBB.getBB().getSharedCounters();
   do {
     long shouldAddCount =
         CQUtilBB.getBB().getSharedCounters().incrementAndRead(CQUtilBB.SHOULD_ADD_COUNT);
     if (shouldAddCount > numKeysToCreate) {
       String aStr =
           "In loadRegion, shouldAddCount is "
               + shouldAddCount
               + ", numOriginalKeysCreated is "
               + sc.read(CQUtilBB.NUM_ORIGINAL_KEYS_CREATED)
               + ", numKeysToCreate is "
               + numKeysToCreate
               + ", region size is "
               + aRegion.size();
       Log.getLogWriter().info(aStr);
       NameBB.getBB().printSharedCounters();
       throw new StopSchedulingTaskOnClientOrder(aStr);
     }
     Object key = NameFactory.getNextPositiveObjectName();
     QueryObject value = getValueToAdd(key);
     value.extra = key;
     Log.getLogWriter().info("Creating with put, key " + key + ", value " + value.toStringFull());
     aRegion.put(key, value);
     sc.increment(CQUtilBB.NUM_ORIGINAL_KEYS_CREATED);
     if (System.currentTimeMillis() - lastLogTime > LOG_INTERVAL_MILLIS) {
       Log.getLogWriter()
           .info(
               "Added "
                   + NameFactory.getPositiveNameCounter()
                   + " out of "
                   + numKeysToCreate
                   + " entries into "
                   + TestHelper.regionToString(aRegion, false));
       lastLogTime = System.currentTimeMillis();
     }
   } while ((minTaskGranularitySec == -1)
       || (System.currentTimeMillis() - startTime < minTaskGranularityMS));
 }
 /**
  * Add a new key to REGION_NAME.
  *
  * @return true if all new keys have been added (specified by CQUtilPrms.numNewKeys)
  */
 protected boolean addNewKey() {
   SharedCounters sc = CQUtilBB.getBB().getSharedCounters();
   long numNewKeysCreated = sc.incrementAndRead(CQUtilBB.NUM_NEW_KEYS_CREATED);
   if (numNewKeysCreated > numNewKeys) {
     Log.getLogWriter().info("All new keys created; returning from addNewKey");
     return true;
   }
   Object key = NameFactory.getNextPositiveObjectName();
   checkContainsValueForKey(key, false, "before addNewKey");
   QueryObject value =
       new QueryObject(
           NameFactory.getCounterForName(key), QueryObject.EQUAL_VALUES, -1, queryDepth);
   value.extra = key; // encode the key in the value for later validation
   Log.getLogWriter().info("Adding new key " + key + " with put");
   aRegion.put(key, value);
   Log.getLogWriter()
       .info(
           "Done adding new key "
               + key
               + " with put, "
               + "num remaining: "
               + (numNewKeys - numNewKeysCreated));
   return (numNewKeysCreated >= numNewKeys);
 }
  /* (non-Javadoc)
   * @see com.gemstone.gemfire.cache.execute.FunctionAdapter#execute(com.gemstone.gemfire.cache.execute.FunctionContext)
   */
  @Override
  public void execute(FunctionContext context) {
    // retrieve and log function arguments
    List arguments = (ArrayList) (context.getArguments());
    Object initiatingThreadID = arguments.get(0);
    String task = (String) arguments.get(1);
    Log.getLogWriter()
        .info(
            "In execute with context "
                + context
                + " initiated in hydra thread thr_"
                + initiatingThreadID
                + "_; fcn task is "
                + task);

    if (task.equals(VERIFY_CLASS_AVAILABILITY)) {
      List<String> expectAvailable = (List) arguments.get(2);
      List<String> expectUnavailable = (List) arguments.get(3);
      Log.getLogWriter().info("Expect available: " + Arrays.asList(expectAvailable));
      Log.getLogWriter().info("Expect unavailable: " + Arrays.asList(expectUnavailable));

      // Test that the classes we expect to see are available
      Set<Region<?, ?>> allRegions = getAllRegions();
      RandomValues rv = new RandomValues();
      for (String className : expectAvailable) {
        Log.getLogWriter()
            .info("Attempting to create instance of " + className + "; expect this to succeed");
        String key = NameFactory.getNextPositiveObjectName();
        Object newObj = null;
        try {
          newObj = createValueHolderInstance(className, key, rv);
        } catch (ClassNotFoundException e) {
          throw new TestException("Got unexpected " + TestHelper.getStackTrace(e));
        }
        String newObjStr = TestHelper.toString(newObj);
        Log.getLogWriter().info("Successfully created " + newObjStr);
        for (Region aRegion : allRegions) {
          Log.getLogWriter()
              .info("Putting " + key + ", " + newObjStr + " into " + aRegion.getFullPath());
          aRegion.put(key, newObj);
        }
      }

      // Test that the classes we expect to not see are unavailable
      String key = NameFactory.getNextPositiveObjectName();
      for (String className : expectUnavailable) {
        Log.getLogWriter()
            .info("Attempting to create instance of " + className + "; expect this to fail");
        try {
          Object newObj = createValueHolderInstance(className, key, rv);
          throw new TestException(
              "Expected to not find "
                  + className
                  + " but was able to create "
                  + TestHelper.toString(newObj));
        } catch (ClassNotFoundException e) {
          Log.getLogWriter().info("Test got expected exception for " + className + ": " + e);
        }
      }
      context
          .getResultSender()
          .lastResult("Validation was successful for vm_" + RemoteTestModule.getMyVmid());
    } else {
      throw new TestException("Unknown task specified for function: " + task);
    }
  }