private CompletableFuture<Void> restoreSnapshot(String snapshotName, TableName tableName,
    boolean takeFailSafeSnapshot, boolean restoreAcl) {
    if (takeFailSafeSnapshot) {
      CompletableFuture<Void> future = new CompletableFuture<>();
      // Step.1 Take a snapshot of the current state
      String failSafeSnapshotSnapshotNameFormat =
        this.connection.getConfiguration().get(HConstants.SNAPSHOT_RESTORE_FAILSAFE_NAME,
          HConstants.DEFAULT_SNAPSHOT_RESTORE_FAILSAFE_NAME);
      final String failSafeSnapshotSnapshotName =
        failSafeSnapshotSnapshotNameFormat.replace("{snapshot.name}", snapshotName)
          .replace("{table.name}", tableName.toString().replace(TableName.NAMESPACE_DELIM, '.'))
          .replace("{restore.timestamp}", String.valueOf(EnvironmentEdgeManager.currentTime()));
      LOG.info("Taking restore-failsafe snapshot: " + failSafeSnapshotSnapshotName);
      addListener(snapshot(failSafeSnapshotSnapshotName, tableName), (ret, err) -> {
        if (err != null) {
          future.completeExceptionally(err);
        } else {
          // Step.2 Restore snapshot
          addListener(internalRestoreSnapshot(snapshotName, tableName, restoreAcl, null),
            (void2, err2) -> {
              if (err2 != null) {
                // Step.3.a Something went wrong during the restore and try to rollback.
                addListener(internalRestoreSnapshot(failSafeSnapshotSnapshotName, tableName,
                  restoreAcl, null), (void3, err3) -> {
                    if (err3 != null) {
                      future.completeExceptionally(err3);
                    } else {
                      // If fail to restore snapshot but rollback successfully, delete the
                      // restore-failsafe snapshot.
                      LOG.info(
                        "Deleting restore-failsafe snapshot: " + failSafeSnapshotSnapshotName);
                      addListener(deleteSnapshot(failSafeSnapshotSnapshotName), (ret4, err4) -> {
                        if (err4 != null) {
                          LOG.error("Unable to remove the failsafe snapshot: {}",
                            failSafeSnapshotSnapshotName, err4);
                        }
                        String msg =
                          "Restore snapshot=" + snapshotName + " failed, Rollback to snapshot="
                            + failSafeSnapshotSnapshotName + " succeeded.";
                        LOG.error(msg);
                        future.completeExceptionally(new RestoreSnapshotException(msg, err2));
                      });
                    }
                  });
              } else {
                // Step.3.b If the restore is succeeded, delete the pre-restore snapshot.
                LOG.info("Deleting restore-failsafe snapshot: " + failSafeSnapshotSnapshotName);
                addListener(deleteSnapshot(failSafeSnapshotSnapshotName), (ret3, err3) -> {
                  if (err3 != null) {
                    LOG.error(
                      "Unable to remove the failsafe snapshot: " + failSafeSnapshotSnapshotName,
                      err3);
                  }
                  future.complete(ret3);
                });
              }
            });
        }
      });
      return future;
    } else {
      return internalRestoreSnapshot(snapshotName, tableName, restoreAcl, null);
    }
  }
Compact the region at specific region server.
hbase-client/src/main/java/org/apache/hadoop/hbase/client/RawAsyncHBaseAdmin.java