/**
   * 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();
  }
 /**
  * Do a get on a key in region REGION_NAME. Keys to get are specified in keyIntervals.
  *
  * @return true if all keys to have get performaed have been completed.
  */
 protected boolean get() {
   SharedCounters sc = CQUtilBB.getBB().getSharedCounters();
   long nextKey = sc.incrementAndRead(CQUtilBB.LASTKEY_GET);
   if (!keyIntervals.keyInRange(KeyIntervals.GET, nextKey)) {
     Log.getLogWriter().info("All gets completed; returning from get");
     return true;
   }
   Object key = NameFactory.getObjectNameForCounter(nextKey);
   Log.getLogWriter().info("Getting " + key);
   try {
     Object existingValue = aRegion.get(key);
     Log.getLogWriter()
         .info(
             "Done getting "
                 + key
                 + ", num remaining: "
                 + (keyIntervals.getLastKey(KeyIntervals.GET) - nextKey));
     if (existingValue == null)
       throw new TestException("Get of key " + key + " returned unexpected " + existingValue);
   } catch (TimeoutException e) {
     throw new TestException(TestHelper.getStackTrace(e));
   } catch (CacheLoaderException e) {
     throw new TestException(TestHelper.getStackTrace(e));
   }
   return (nextKey >= keyIntervals.getLastKey(KeyIntervals.GET));
 }
 /**
  * Destroy a key in region REGION_NAME. The keys to destroy are specified in keyIntervals.
  *
  * @return true if all keys to be destroyed have been completed.
  */
 protected boolean destroy() {
   SharedCounters sc = CQUtilBB.getBB().getSharedCounters();
   long nextKey = sc.incrementAndRead(CQUtilBB.LASTKEY_DESTROY);
   if (!keyIntervals.keyInRange(KeyIntervals.DESTROY, nextKey)) {
     Log.getLogWriter().info("All destroys completed; returning from destroy");
     return true;
   }
   Object key = NameFactory.getObjectNameForCounter(nextKey);
   Log.getLogWriter().info("Destroying " + key);
   checkContainsValueForKey(key, true, "before destroy");
   try {
     aRegion.destroy(key);
     Log.getLogWriter()
         .info(
             "Done Destroying "
                 + key
                 + ", num remaining: "
                 + (keyIntervals.getLastKey(KeyIntervals.DESTROY) - nextKey));
   } catch (CacheWriterException e) {
     throw new TestException(TestHelper.getStackTrace(e));
   } catch (TimeoutException e) {
     throw new TestException(TestHelper.getStackTrace(e));
   } catch (EntryNotFoundException e) {
     throw new TestException(TestHelper.getStackTrace(e));
   }
   return (nextKey >= keyIntervals.getLastKey(KeyIntervals.DESTROY));
 }
 /**
  * Invalidate a key in region REGION_NAME. The keys to invalidate are specified in keyIntervals.
  *
  * @return true if all keys to be invalidated have been completed.
  */
 protected boolean invalidate() {
   SharedCounters sc = CQUtilBB.getBB().getSharedCounters();
   long nextKey = sc.incrementAndRead(CQUtilBB.LASTKEY_INVALIDATE);
   if (!keyIntervals.keyInRange(KeyIntervals.INVALIDATE, nextKey)) {
     Log.getLogWriter().info("All existing keys invalidated; returning from invalidate");
     return true;
   }
   Object key = NameFactory.getObjectNameForCounter(nextKey);
   Log.getLogWriter().info("Invalidating " + key);
   checkContainsValueForKey(key, true, "before invalidate");
   try {
     aRegion.invalidate(key);
     Log.getLogWriter()
         .info(
             "Done invalidating "
                 + key
                 + ", num remaining: "
                 + (keyIntervals.getLastKey(KeyIntervals.INVALIDATE) - nextKey));
   } catch (TimeoutException e) {
     throw new TestException(TestHelper.getStackTrace(e));
   } catch (EntryNotFoundException e) {
     throw new TestException(TestHelper.getStackTrace(e));
   }
   return (nextKey >= keyIntervals.getLastKey(KeyIntervals.INVALIDATE));
 }
 /**
  * Update an existing key in region REGION_NAME. The keys to update are specified in keyIntervals.
  *
  * @return true if all keys to be updated have been completed.
  */
 protected boolean updateExistingKey() {
   long nextKey =
       CQUtilBB.getBB().getSharedCounters().incrementAndRead(CQUtilBB.LASTKEY_UPDATE_EXISTING_KEY);
   if (!keyIntervals.keyInRange(KeyIntervals.UPDATE_EXISTING_KEY, nextKey)) {
     Log.getLogWriter().info("All existing keys updated; returning from updateExistingKey");
     return true;
   }
   Object key = NameFactory.getObjectNameForCounter(nextKey);
   QueryObject existingValue = (QueryObject) aRegion.get(key);
   if (existingValue == null)
     throw new TestException("Get of key " + key + " returned unexpected " + existingValue);
   QueryObject newValue = existingValue.modifyWithNewInstance(QueryObject.NEGATE, 0, true);
   newValue.extra = key; // encode the key in the object for later validation
   if (existingValue.aPrimitiveLong < 0)
     throw new TestException(
         "Trying to update a key which was already updated: " + existingValue.toStringFull());
   Log.getLogWriter()
       .info("Updating existing key " + key + " with value " + TestHelper.toString(newValue));
   aRegion.put(key, newValue);
   Log.getLogWriter()
       .info(
           "Done updating existing key "
               + key
               + " with value "
               + TestHelper.toString(newValue)
               + ", num remaining: "
               + (keyIntervals.getLastKey(KeyIntervals.UPDATE_EXISTING_KEY) - nextKey));
   return (nextKey >= keyIntervals.getLastKey(KeyIntervals.UPDATE_EXISTING_KEY));
 }
 /**
  * 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));
 }
 /** Hydra task to execution ops, then stop scheduling. */
 public static void HydraTask_doOps() {
   BitSet availableOps = new BitSet(operations.length);
   availableOps.flip(FIRST_OP, LAST_OP + 1);
   // don't do local ops in bridge configuration
   availableOps.clear(LOCAL_INVALIDATE);
   availableOps.clear(LOCAL_DESTROY);
   testInstance.doOps(availableOps);
   if (availableOps.cardinality() == 0) {
     CQUtilBB.getBB().getSharedCounters().increment(CQUtilBB.TimeToStop);
     throw new StopSchedulingTaskOnClientOrder("Finished with ops");
   }
 }
 /** Log the number of ops that have completed. */
 protected void logNumOps() {
   SharedCounters sc = CQUtilBB.getBB().getSharedCounters();
   int totalOps =
       keyIntervals.getNumKeys() - keyIntervals.getNumKeys(KeyIntervals.NONE) + numNewKeys;
   long numOpsCompleted =
       sc.read(CQUtilBB.LASTKEY_INVALIDATE)
           - keyIntervals.getFirstKey(KeyIntervals.INVALIDATE)
           + sc.read(CQUtilBB.LASTKEY_DESTROY)
           - keyIntervals.getFirstKey(KeyIntervals.DESTROY)
           + sc.read(CQUtilBB.LASTKEY_UPDATE_EXISTING_KEY)
           - keyIntervals.getFirstKey(KeyIntervals.UPDATE_EXISTING_KEY)
           + sc.read(CQUtilBB.LASTKEY_GET)
           - keyIntervals.getFirstKey(KeyIntervals.GET)
           + sc.read(CQUtilBB.NUM_NEW_KEYS_CREATED);
   Log.getLogWriter()
       .info(
           "Total ops is " + totalOps + ", current number of ops completed is " + numOpsCompleted);
 }
 /**
  * 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);
 }
 /** 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);
 }
 /**
  * Hydra task to verify the contents of the region after all ops. This MUST be called as a batched
  * task, and will throw StopSchedulingTaskOnClientOrder when completed. It is necessary to
  * reinitialize the verify state variables if this is called a second time in this VM, however an
  * error is thrown if a second attempt it made without resetting the state variables.
  */
 public static void HydraTask_verifyRegionContents() {
   CQUtilBB.getBB().printSharedCounters();
   NameBB.getBB().printSharedCounters();
   testInstance.verifyRegionContents();
 }