/**
  * Create a region with the given region description name.
  *
  * @param regDescriptName The name of a region description.
  */
 protected void initializeRegion(String regDescriptName) {
   CacheHelper.createCache("cache1");
   String key = VmIDStr + RemoteTestModule.getMyVmid();
   String xmlFile = key + ".xml";
   try {
     CacheHelper.generateCacheXmlFile("cache1", regDescriptName, xmlFile);
   } catch (HydraRuntimeException e) {
     if (e.toString().indexOf("Cache XML file was already created") >= 0) {
       // this can occur when reinitializing after a stop-start because
       // the cache xml file is written during the first init tasks
     } else {
       throw new TestException(TestHelper.getStackTrace(e));
     }
   }
   aRegion = RegionHelper.createRegion(regDescriptName);
 }
  /**
   * Do operations and pause when directed by the snapshotController Also, writes the snapshot to
   * the blackboard and exports snapshot to disk
   */
  protected static void snapshotResponder(RecoveryTest testInstance) throws Exception {
    long pausing = RecoveryBB.getBB().getSharedCounters().read(RecoveryBB.pausing);
    if (pausing > 0) { // controller has signaled to pause
      if ((Boolean)
          (testInstance.threadIsPaused
              .get())) { // this thread has already paused, so don't increment counter
        Log.getLogWriter().info("Thread has paused");
      } else {
        Log.getLogWriter().info("This thread is pausing");
        testInstance.threadIsPaused.set(new Boolean(true));
        RecoveryBB.getBB().getSharedCounters().incrementAndRead(RecoveryBB.pausing);
      }
      long writeSnapshot = RecoveryBB.getBB().getSharedCounters().read(RecoveryBB.writeSnapshot);
      if (writeSnapshot > 0) {
        long leader = RecoveryBB.getBB().getSharedCounters().incrementAndRead(RecoveryBB.leader);
        if (leader == 1) { // this is the thread to write the snapshot
          Log.getLogWriter().info("This thread is the leader; it will write the snapshot");
          testInstance.writeSnapshot(true); // include non-persistent regions
          long executionNumber =
              RecoveryBB.getBB().getSharedCounters().read(RecoveryBB.executionNumber);

          // Add numFilterObjects entries to each region (SnapshotFilter tests)
          if (SnapshotPrms.useFilterOnExport() || SnapshotPrms.useFilterOnImport()) {
            Map allRegionsSnapshot = new HashMap();
            int numToCreate = SnapshotPrms.numFilterObjects();
            for (Region aRegion : testInstance.allRegions) {
              for (int i = 1; i <= numToCreate; i++) {
                String key = "FilterObject_" + i;
                String value =
                    "object to be filtered via snapshot.save() or snapshot.load(): this should never be a value in the cache once snapshot restored";
                aRegion.put(key, value);
              }
            }
            Log.getLogWriter()
                .info("Wrote " + numToCreate + " FilterObject entries to each region");
          }

          CacheSnapshotService snapshot = CacheHelper.getCache().getSnapshotService();
          SnapshotOptions options = snapshot.createOptions();
          if (SnapshotPrms.useFilterOnExport()) {
            options.setFilter(getSnapshotFilter());
          }

          String currDirName = System.getProperty("user.dir");
          String snapshotDirName =
              currDirName + File.separator + "cacheSnapshotDir_" + executionNumber;
          Log.getLogWriter().info("Starting cacheSnapshot to " + snapshotDirName);
          snapshot.save(new File(snapshotDirName), SnapshotFormat.GEMFIRE, options);
          Log.getLogWriter().info("Completed cacheSnapshot to " + snapshotDirName);
          RecoveryBB.getBB().getSharedCounters().increment(RecoveryBB.snapshotWritten);
        }
      }
      MasterController.sleepForMs(5000);
    } else { // not pausing
      long minTaskGranularitySec = TestConfig.tab().longAt(TestHelperPrms.minTaskGranularitySec);
      long minTaskGranularityMS = minTaskGranularitySec * TestHelper.SEC_MILLI_FACTOR;
      testInstance.doOperations(minTaskGranularityMS);
    }
  }
 /**
  * Return a Set of all Regions defined in the cache
  *
  * @return The set of all Regions defined in the cache
  */
 protected Set<Region<?, ?>> getAllRegions() {
   Set<Region<?, ?>> regionSet = new HashSet(CacheHelper.getCache().rootRegions());
   Set<Region<?, ?>> rootRegions = new HashSet(regionSet);
   for (Region aRegion : rootRegions) {
     regionSet.addAll(aRegion.subregions(true));
   }
   if (regionSet.size() == 0) {
     throw new TestException("Test problem; expected regions to be defined");
   }
   return regionSet;
 }
 /**
  * Create the DiskStore if the RegionAttributes specifies it.
  *
  * @param attr The RegionAttributes that could specify a DiskStore.
  * @return A String description of the DiskStore, useful for logging.
  */
 public static String createDiskStore(RegionAttributes attr) {
   String diskDirsStr = "";
   Log.getLogWriter().info("DiskStore name in attributes is " + attr.getDiskStoreName());
   if ((attr.getDiskStoreName() != null) && (!attr.getDiskStoreName().equals("notUsed"))) {
     Log.getLogWriter().info("Creating diskStore " + attr.getDiskStoreName());
     DiskStoreFactory dsFactory = DiskStoreHelper.getDiskStoreFactory(attr.getDiskStoreName());
     dsFactory.create(attr.getDiskStoreName());
     File[] diskDirs = CacheHelper.getCache().findDiskStore(attr.getDiskStoreName()).getDiskDirs();
     for (File diskDir : diskDirs) {
       diskDirsStr = diskDirsStr + diskDir.getName() + " ";
     }
     diskDirsStr = " with disk dirs " + diskDirsStr;
   }
   return "disk store name is " + attr.getDiskStoreName() + " " + diskDirsStr;
 }
 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 and initializes a server or peer. */
 public static synchronized void HydraTask_initializeExpectException() throws Throwable {
   if (RecoveryTest.testInstance == null) {
     RecoveryTest.testInstance = new RecoveryTest();
     int numRootRegions = RecoveryPrms.getNumRootRegions();
     int numSubregions = RecoveryPrms.getNumSubregions();
     int hierDepth = RecoveryPrms.getRegionHierarchyDepth();
     if ((numSubregions != 0) || (hierDepth != 1)) {
       throw new TestException("Unable to handle subregions in this test");
     }
     CacheHelper.createCache("cache1");
     final String regionConfigName =
         TestConfig.tasktab()
             .stringAt(
                 RecoveryPrms.regionConfigNames,
                 TestConfig.tab().stringAt(RecoveryPrms.regionConfigNames, null));
     String createdRegions = "";
     for (int i = 1; i <= numRootRegions; i++) {
       try {
         final String regionName = "Region_" + i;
         createdRegions =
             createdRegions
                 + RegionHelper.createRegion(regionName, regionConfigName).getFullPath()
                 + " ";
       } catch (ConflictingPersistentDataException e) {
         Log.getLogWriter().info("Caught expected exception " + TestHelper.getStackTrace(e));
       }
     }
     if (createdRegions.length() > 0) {
       throw new TestException(
           "Expected to get "
               + ConflictingPersistentDataException.class.getName()
               + " on region creation, but the following regions were successfully created: "
               + createdRegions);
     }
   }
 }
  /** One thread imports ... everyone validates against blackboard snapshot */
  protected static void importAndVerify(RecoveryTest testInstance) {
    long counter =
        RecoveryBB.getBB().getSharedCounters().incrementAndRead(RecoveryBB.importSnapshot);
    if (counter == 1) {
      Log.getLogWriter().info("This thread is the leader; it will import the snapshot");
      long executionNumber =
          RecoveryBB.getBB().getSharedCounters().read(RecoveryBB.executionNumber);
      String currDirName = System.getProperty("user.dir");
      String snapshotDirName = currDirName + File.separator + "cacheSnapshotDir_" + executionNumber;
      CacheSnapshotService snapshot = CacheHelper.getCache().getSnapshotService();
      long startTime = System.currentTimeMillis();
      try {
        if (SnapshotPrms.useFilterOnImport()) {
          SnapshotOptions options = snapshot.createOptions();
          options.setFilter(getSnapshotFilter());

          File dir = new File(snapshotDirName);
          File[] files = dir.listFiles();
          StringBuffer aStr = new StringBuffer();
          aStr.append("Invoking snapshot.load() with files:\n");
          for (int i = 0; i < files.length; i++) {
            aStr.append("   " + files[i] + "\n");
          }
          aStr.append("Options.getFilter() = " + options.getFilter().getClass().getName());
          Log.getLogWriter().info(aStr.toString());
          snapshot.load(files, SnapshotFormat.GEMFIRE, options);
        } else {
          Log.getLogWriter().info("Starting CacheSnapshotService.load() from " + snapshotDirName);
          snapshot.load(new File(snapshotDirName), SnapshotFormat.GEMFIRE);
        }
      } catch (IOException ioe) {
        throw new TestException(
            "Caught "
                + ioe
                + " while importing region snapshot from "
                + snapshotDirName
                + " "
                + TestHelper.getStackTrace(ioe));
      } catch (ClassNotFoundException e) {
        throw new TestException(
            "Caught "
                + e
                + " while importing region snapshot from "
                + snapshotDirName
                + " "
                + TestHelper.getStackTrace(e));
      }
      long endTime = System.currentTimeMillis();
      Log.getLogWriter()
          .info(
              "CacheSnapshotService.load() of "
                  + snapshotDirName
                  + " took "
                  + (endTime - startTime)
                  + " ms.");
      Log.getLogWriter().info(testInstance.regionHierarchyToString());
      RecoveryBB.getBB().getSharedCounters().increment(RecoveryBB.snapshotImported);
    } else {
      TestHelper.waitForCounter(
          RecoveryBB.getBB(),
          "RecoveryBB.snapshotImported",
          RecoveryBB.snapshotImported,
          1,
          true,
          -1,
          2000);
      testInstance.verifyFromSnapshot();
      RecoveryBB.getBB().getSharedCounters().increment(RecoveryBB.doneVerifyingCounter);
    }
    TestHelper.waitForCounter(
        RecoveryBB.getBB(),
        "RecoveryBB.doneVerifyingCounter",
        RecoveryBB.doneVerifyingCounter,
        RemoteTestModule.getCurrentThread().getCurrentTask().getTotalThreads() - 1,
        true,
        -1,
        2000);
  }