private void flushSnapshot() throws ForeignException {
    if (regions.isEmpty()) {
      // No regions on this RS, we are basically done.
      return;
    }

    monitor.rethrowException();

    // assert that the taskManager is empty.
    if (taskManager.hasTasks()) {
      throw new IllegalStateException(
          "Attempting to take snapshot "
              + ClientSnapshotDescriptionUtils.toString(snapshot)
              + " but we currently have outstanding tasks");
    }

    // Add all hfiles already existing in region.
    for (Region region : regions) {
      // submit one task per region for parallelize by region.
      taskManager.submitTask(new RegionSnapshotTask(region));
      monitor.rethrowException();
    }

    // wait for everything to complete.
    LOG.debug("Flush Snapshot Tasks submitted for " + regions.size() + " regions");
    try {
      taskManager.waitForOutstandingTasks();
    } catch (InterruptedException e) {
      LOG.error("got interrupted exception for " + getMemberName());
      throw new ForeignException(getMemberName(), e);
    }
  }
 /** Cancel threads if they haven't finished. */
 @Override
 public void cleanup(Exception e) {
   LOG.info(
       "Aborting all online FLUSH snapshot subprocedure task threads for '"
           + snapshot.getName()
           + "' due to error",
       e);
   try {
     taskManager.cancelTasks();
   } catch (InterruptedException e1) {
     Thread.currentThread().interrupt();
   }
 }