/**
   * Hydra start task to initialize key intervals, which are ranges of keys which are to have an
   * operation done on them (invalidate, destroy, etc)
   */
  public static synchronized void StartTask_initialize() {
    int numKeys = TestConfig.tab().intAt(CQUtilPrms.numKeys);
    KeyIntervals intervals =
        new KeyIntervals(
            new int[] {
              KeyIntervals.NONE, KeyIntervals.INVALIDATE,
              KeyIntervals.DESTROY, KeyIntervals.UPDATE_EXISTING_KEY,
              KeyIntervals.GET
            },
            numKeys);
    CQUtilBB.getBB().getSharedMap().put(CQUtilBB.KeyIntervals, intervals);
    Log.getLogWriter().info("Created keyIntervals: " + intervals);

    // Set the counters for the next keys to use for each operation
    hydra.blackboard.SharedCounters sc = CQUtilBB.getBB().getSharedCounters();
    sc.setIfLarger(CQUtilBB.LASTKEY_INVALIDATE, intervals.getFirstKey(KeyIntervals.INVALIDATE) - 1);
    sc.setIfLarger(
        CQUtilBB.LASTKEY_LOCAL_INVALIDATE,
        intervals.getFirstKey(KeyIntervals.LOCAL_INVALIDATE) - 1);
    sc.setIfLarger(CQUtilBB.LASTKEY_DESTROY, intervals.getFirstKey(KeyIntervals.DESTROY) - 1);
    sc.setIfLarger(
        CQUtilBB.LASTKEY_LOCAL_DESTROY, intervals.getFirstKey(KeyIntervals.LOCAL_DESTROY) - 1);
    sc.setIfLarger(
        CQUtilBB.LASTKEY_UPDATE_EXISTING_KEY,
        intervals.getFirstKey(KeyIntervals.UPDATE_EXISTING_KEY) - 1);
    sc.setIfLarger(CQUtilBB.LASTKEY_GET, intervals.getFirstKey(KeyIntervals.GET) - 1);

    // show the blackboard
    CQUtilBB.getBB().printSharedMap();
    CQUtilBB.getBB().printSharedCounters();
  }
 /**
  * Get a random operation to perform. The set of operations available are in the BitSet. These
  * bits correspond to the operations defined in this class.
  *
  * @param bs True bits correspond to operations available to be chosen.
  * @param bsSize The number of bits to consider.
  * @return An operation as defined in this class.
  */
 protected int getOp(BitSet bs, int bsSize) {
   int randInt;
   do {
     randInt = TestConfig.tab().getRandGen().nextInt(1, bsSize);
   } while (!bs.get(randInt));
   return randInt;
 }
 /**
  * Get a random key from the given region that is within this instance's range. If no keys qualify
  * in the region, return null.
  *
  * @param aRegion - The region to get the key from.
  * @param excludeKey - The region to get the key from.
  * @returns A key from aRegion, or null.
  */
 public Object getRandomKey(Region aRegion, Object excludeKey) {
   long start = System.currentTimeMillis();
   int lower = ((Integer) (lowerKeyRange.get())).intValue();
   int upper = ((Integer) (upperKeyRange.get())).intValue();
   long randomKeyIndex = TestConfig.tab().getRandGen().nextLong(lower, upper);
   long startKeyIndex = randomKeyIndex;
   Object key = NameFactory.getObjectNameForCounter(randomKeyIndex);
   do {
     boolean done = false;
     if ((!(key.equals(excludeKey))) && (aRegion.containsKey(key))) done = true;
     if (done) break;
     randomKeyIndex++; // go to the next key
     if (randomKeyIndex > upper) randomKeyIndex = lower;
     if (randomKeyIndex == startKeyIndex) { // considered all keys
       key = null;
       break;
     }
     key = NameFactory.getObjectNameForCounter(randomKeyIndex);
   } while (true);
   long end = System.currentTimeMillis();
   Log.getLogWriter()
       .info(
           "Done in TxUtilKeyRange:getRandomKey, key is "
               + key
               + " "
               + aRegion.getFullPath()
               + " getRandomKey took "
               + (end - start)
               + " millis");
   return key;
 }
 /** Initialize fields for this instance */
 public void initializeInstance() {
   numNewKeys = TestConfig.tab().intAt(CQUtilPrms.numNewKeys, -1);
   keyIntervals = (KeyIntervals) (CQUtilBB.getBB().getSharedMap().get(CQUtilBB.KeyIntervals));
   Log.getLogWriter()
       .info("initInstance, keyIntervals read from blackboard = " + keyIntervals.toString());
   int numDestroyed = keyIntervals.getNumKeys(KeyIntervals.DESTROY);
   int numKeyIntervals = keyIntervals.getNumKeys();
   totalNumKeys = numKeyIntervals + numNewKeys - numDestroyed;
   minTaskGranularitySec = TestConfig.tab().longAt(TestHelperPrms.minTaskGranularitySec, -1);
   minTaskGranularityMS = -1;
   if (minTaskGranularitySec != -1) {
     minTaskGranularityMS = minTaskGranularitySec * TestHelper.SEC_MILLI_FACTOR;
   }
   queryDepth = TestConfig.tab().intAt(CQUtilPrms.queryDepth, 1);
   Vector bridgeNames = TestConfig.tab().vecAt(BridgePrms.names, null);
   isBridgeConfiguration = bridgeNames != null;
   CQsOn = TestConfig.tab().booleanAt(CQUtilPrms.CQsOn, true);
   CQTestInstance = new CQTest();
   CQTestInstance.initializeInstance();
   Log.getLogWriter().info("numKeyIntervals is " + numKeyIntervals);
   Log.getLogWriter().info("numNewKeys is " + numNewKeys);
   Log.getLogWriter().info("numDestroyed is " + numDestroyed);
   Log.getLogWriter().info("totalNumKeys is " + totalNumKeys);
 }
 protected void initializeQueryService() {
   try {
     String usingPool = TestConfig.tab().stringAt(CQUtilPrms.QueryServiceUsingPool, "false");
     boolean queryServiceUsingPool = Boolean.valueOf(usingPool).booleanValue();
     if (queryServiceUsingPool) {
       Pool pool = PoolHelper.createPool(CQUtilPrms.getQueryServicePoolName());
       qService = pool.getQueryService();
       Log.getLogWriter()
           .info("Initializing QueryService using Pool. PoolName: " + pool.getName());
     } else {
       qService = CacheHelper.getCache().getQueryService();
       Log.getLogWriter().info("Initializing QueryService using Cache.");
     }
   } catch (Exception e) {
     throw new TestException(TestHelper.getStackTrace(e));
   }
 }
 /**
  * Creates a new key/value in the given region by creating a new key within the range and a random
  * value.
  *
  * @param aRegion The region to create the new key in.
  * @param exists Not used in this overridden method; this test wants to use unique keys even on
  *     creates, so we don't do anything different here based on the value of exists.
  * @return An instance of Operation describing the create operation.
  */
 @Override
 public Operation createEntry(Region aRegion, boolean exists) {
   int lower = ((Integer) (lowerKeyRange.get())).intValue();
   int upper = ((Integer) (upperKeyRange.get())).intValue();
   long keyIndex = TestConfig.tab().getRandGen().nextInt(lower, upper);
   long startKeyIndex = keyIndex;
   Object key = NameFactory.getObjectNameForCounter(keyIndex);
   boolean containsKey = aRegion.containsKey(key);
   while (containsKey) { // looking for a key that does not exist
     keyIndex++; // go to the next key
     if (keyIndex > upper) keyIndex = lower;
     if (keyIndex == startKeyIndex) { // considered all keys
       return null;
     }
     key = NameFactory.getObjectNameForCounter(keyIndex);
     containsKey = aRegion.containsKey(key);
   }
   BaseValueHolder vh = new ValueHolder(key, randomValues, new Integer(modValInitializer++));
   try {
     Log.getLogWriter()
         .info(
             "createEntryKeyRange: putting key "
                 + key
                 + ", object "
                 + vh.toString()
                 + " in region "
                 + aRegion.getFullPath());
     aRegion.put(key, vh);
     Log.getLogWriter()
         .info(
             "createEntryKeyRange: done putting key "
                 + key
                 + ", object "
                 + vh.toString()
                 + " in region "
                 + aRegion.getFullPath());
   } catch (Exception e) {
     throw new TestException(TestHelper.getStackTrace(e));
   }
   return new Operation(aRegion.getFullPath(), key, Operation.ENTRY_CREATE, null, vh.modVal);
 }
  /**
   * Verify the contents of the region, taking into account the keys that were destroyed,
   * invalidated, etc (as specified in keyIntervals) Throw an error of any problems are detected.
   * This must be called repeatedly by the same thread until StopSchedulingTaskOnClientOrder is
   * thrown.
   */
  public void verifyRegionContents() {
    final long LOG_INTERVAL_MILLIS = 10000;
    // we already completed this check once; we can't do it again without reinitializing the
    // verify state variables
    if (verifyRegionContentsCompleted) {
      throw new TestException(
          "Test configuration problem; already verified region contents, "
              + "cannot call this task again without resetting batch variables");
    }

    // iterate keys
    long lastLogTime = System.currentTimeMillis();
    long minTaskGranularitySec = TestConfig.tab().longAt(TestHelperPrms.minTaskGranularitySec);
    long minTaskGranularityMS = minTaskGranularitySec * TestHelper.SEC_MILLI_FACTOR;
    long startTime = System.currentTimeMillis();
    long size = aRegion.size();
    boolean first = true;
    int numKeysToCheck = keyIntervals.getNumKeys() + numNewKeys;
    while (verifyRegionContentsIndex < numKeysToCheck) {
      verifyRegionContentsIndex++;
      if (first) {
        Log.getLogWriter()
            .info(
                "In verifyRegionContents, region has "
                    + size
                    + " keys; starting verify at verifyRegionContentsIndex "
                    + verifyRegionContentsIndex
                    + "; verifying key names with indexes through (and including) "
                    + numKeysToCheck);
        first = false;
      }

      // check region size the first time through the loop to avoid it being called
      // multiple times when this is batched
      if (verifyRegionContentsIndex == 1) {
        if (totalNumKeys != size) {
          String tmpStr = "Expected region size to be " + totalNumKeys + ", but it is size " + size;
          Log.getLogWriter().info(tmpStr);
          verifyRegionContentsErrStr.append(tmpStr + "\n");
        }
      }

      Object key = NameFactory.getObjectNameForCounter(verifyRegionContentsIndex);
      try {
        if (((verifyRegionContentsIndex >= keyIntervals.getFirstKey(KeyIntervals.NONE))
                && (verifyRegionContentsIndex <= keyIntervals.getLastKey(KeyIntervals.NONE)))
            || ((verifyRegionContentsIndex >= keyIntervals.getFirstKey(KeyIntervals.GET))
                && (verifyRegionContentsIndex <= keyIntervals.getLastKey(KeyIntervals.GET)))) {
          // this key was untouched after its creation
          checkContainsKey(key, true, "key was untouched");
          checkContainsValueForKey(key, true, "key was untouched");
          Object value = aRegion.get(key);
          checkValue(key, value);
        } else if ((verifyRegionContentsIndex >= keyIntervals.getFirstKey(KeyIntervals.INVALIDATE))
            && (verifyRegionContentsIndex <= keyIntervals.getLastKey(KeyIntervals.INVALIDATE))) {
          checkContainsKey(key, true, "key was invalidated");
          checkContainsValueForKey(key, false, "key was invalidated");
        } else if ((verifyRegionContentsIndex
                >= keyIntervals.getFirstKey(KeyIntervals.LOCAL_INVALIDATE))
            && (verifyRegionContentsIndex
                <= keyIntervals.getLastKey(KeyIntervals.LOCAL_INVALIDATE))) {
          // this key was locally invalidated
          checkContainsKey(key, true, "key was locally invalidated");
          checkContainsValueForKey(key, true, "key was locally invalidated");
          Object value = aRegion.get(key);
          checkValue(key, value);
        } else if ((verifyRegionContentsIndex >= keyIntervals.getFirstKey(KeyIntervals.DESTROY))
            && (verifyRegionContentsIndex <= keyIntervals.getLastKey(KeyIntervals.DESTROY))) {
          // this key was destroyed
          checkContainsKey(key, false, "key was destroyed");
          checkContainsValueForKey(key, false, "key was destroyed");
        } else if ((verifyRegionContentsIndex
                >= keyIntervals.getFirstKey(KeyIntervals.LOCAL_DESTROY))
            && (verifyRegionContentsIndex <= keyIntervals.getLastKey(KeyIntervals.LOCAL_DESTROY))) {
          // this key was locally destroyed
          checkContainsKey(key, true, "key was locally destroyed");
          checkContainsValueForKey(key, true, "key was locally destroyed");
          Object value = aRegion.get(key);
          checkValue(key, value);
        } else if ((verifyRegionContentsIndex
                >= keyIntervals.getFirstKey(KeyIntervals.UPDATE_EXISTING_KEY))
            && (verifyRegionContentsIndex
                <= keyIntervals.getLastKey(KeyIntervals.UPDATE_EXISTING_KEY))) {
          // this key was updated
          checkContainsKey(key, true, "key was updated");
          checkContainsValueForKey(key, true, "key was updated");
          Object value = aRegion.get(key);
          checkUpdatedValue(key, value);
        } else if (verifyRegionContentsIndex > keyIntervals.getNumKeys()) {
          // key was newly added
          checkContainsKey(key, true, "key was new");
          checkContainsValueForKey(key, true, "key was new");
          Object value = aRegion.get(key);
          checkValue(key, value);
        }
      } catch (TestException e) {
        Log.getLogWriter().info(TestHelper.getStackTrace(e));
        verifyRegionContentsErrStr.append(e.getMessage() + "\n");
      }

      if (System.currentTimeMillis() - lastLogTime > LOG_INTERVAL_MILLIS) {
        Log.getLogWriter()
            .info("Verified key " + verifyRegionContentsIndex + " out of " + totalNumKeys);
        lastLogTime = System.currentTimeMillis();
      }

      if (System.currentTimeMillis() - startTime >= minTaskGranularityMS) {
        Log.getLogWriter()
            .info(
                "In HydraTask_verifyRegionContents, returning before completing verify "
                    + "because of task granularity (this task must be batched to complete); last key verified is "
                    + key);
        return; // task is batched; we are done with this batch
      }
    }
    verifyRegionContentsCompleted = true;
    if (verifyRegionContentsErrStr.length() > 0) {
      throw new TestException(verifyRegionContentsErrStr.toString());
    }
    String aStr =
        "In HydraTask_verifyRegionContents, verified " + verifyRegionContentsIndex + " keys/values";
    Log.getLogWriter().info(aStr);
    throw new StopSchedulingTaskOnClientOrder(aStr);
  }