private void expandPoolIfNecessary(int channelCount) {
   try {
     this.channelPool.ensureChannelCount(channelCount);
   } catch (IOException e) {
     LOG.info("Could not expand the channel pool.", e);
   }
 }
 private void markOperationComplete(Long operationSequenceId) {
   Long heapSize = pendingOperationsWithSize.remove(operationSequenceId);
   if (heapSize != null) {
     currentWriteBufferSize -= heapSize;
   } else {
     LOG.warn(
         "An operation completed successfully but provided multiple completion notifications."
             + " Please notify Google that this occurred.");
   }
 }
 /**
  * Calls {@link OAuth2Credentials#refreshAccessToken()}. In case of an IOException, retry the call
  * as per the {@link BackOff} policy defined by {@link RetryOptions#createBackoff()}.
  *
  * <p>This method retries until one of the following conditions occurs:
  *
  * <ol>
  *   <li>An OAuth request was completed. If the value is null, return an exception.
  *   <li>A non-IOException Exception is thrown - return an error status
  *   <li>All retries have been exhausted, i.e. when the Backoff.nextBackOffMillis() returns
  *       BackOff.STOP
  *   <li>An interrupt occurs.
  * </ol>
  *
  * @return HeaderCacheElement containing either a valid {@link AccessToken} or an exception.
  */
 protected HeaderCacheElement refreshCredentialsWithRetry() {
   BackOff backoff = null;
   while (true) {
     try {
       logger.info("Refreshing the OAuth token");
       AccessToken newToken = credentials.refreshAccessToken();
       if (newToken == null) {
         // The current implementations of refreshAccessToken() throws an IOException or return
         // a valid token. This handling is future proofing just in case credentials returns null
         // for
         // some reason. This case was caught by a poorly coded unit test.
         logger.info("Refreshed the OAuth token");
         return new HeaderCacheElement(
             new IOException("Could not load the token for credentials: " + credentials));
       } else {
         // Success!
         return new HeaderCacheElement(newToken);
       }
     } catch (IOException exception) {
       logger.warn("Got an unexpected IOException when refreshing google credentials.", exception);
       // An IOException occurred. Retry with backoff.
       if (backoff == null) {
         // initialize backoff.
         backoff = retryOptions.createBackoff();
       }
       // Given the backoff, either sleep for a short duration, or terminate if the backoff has
       // reached its configured timeout limit.
       try {
         RetryState retryState = getRetryState(backoff);
         if (retryState != RetryState.PerformRetry) {
           return new HeaderCacheElement(exception);
         } // else Retry.
       } catch (IOException e) {
         logger.warn("Got an exception while trying to run backoff.nextBackOffMillis()", e);
         return new HeaderCacheElement(exception);
       }
     } catch (Exception e) {
       logger.warn("Got an unexpected exception while trying to refresh google credentials.", e);
       return new HeaderCacheElement(new IOException("Could not read headers", e));
     }
   }
 }
 /**
  * Sleep and/or determine if the backoff has timed out.
  *
  * @param backoff
  * @return RetryState indicating the current state of the retry logic.
  * @throws IOException in some cases from {@link BackOff#nextBackOffMillis()}
  */
 protected RetryState getRetryState(BackOff backoff) throws IOException {
   long nextBackOffMillis = backoff.nextBackOffMillis();
   if (nextBackOffMillis == BackOff.STOP) {
     logger.warn(
         "Exhausted the number of retries for credentials refresh after "
             + this.retryOptions.getMaxElaspedBackoffMillis()
             + " milliseconds.");
     return RetryState.RetriesExhausted;
   }
   try {
     sleeper.sleep(nextBackOffMillis);
     // Try to perform another call.
     return RetryState.PerformRetry;
   } catch (InterruptedException e) {
     logger.warn("Interrupted while trying to refresh credentials.");
     Thread.interrupted();
     // If the thread is interrupted, terminate immediately.
     return RetryState.Interrupted;
   }
 }
  public static void main(String[] args) throws IOException {
    Logger logger = new Logger(CheckConfig.class);
    GenericOptionsParser optionsParser =
        new GenericOptionsParser(HBaseConfiguration.create(), args);
    Configuration fullConfiguration = optionsParser.getConfiguration();

    BigtableOptions options;
    try {
      options = BigtableOptionsFactory.fromConfiguration(fullConfiguration);
    } catch (IOException | RuntimeException exc) {
      logger.warn("Encountered errors attempting to parse configuration.", exc);
      return;
    }

    System.out.println(String.format("User Agent: %s", options.getChannelOptions().getUserAgent()));
    System.out.println(String.format("Project ID: %s", options.getProjectId()));
    System.out.println(String.format("Cluster Name: %s", options.getCluster()));
    System.out.println(String.format("Zone: %s", options.getZone()));
    System.out.println(String.format("Cluster admin host: %s", options.getClusterAdminHost()));
    System.out.println(String.format("Table admin host: %s", options.getTableAdminHost()));
    System.out.println(String.format("Data host: %s", options.getDataHost()));

    Credentials credentials = options.getChannelOptions().getCredential();
    try {
      System.out.println("Attempting credential refresh...");
      credentials.refresh();
    } catch (IOException ioe) {
      logger.warn("Encountered errors attempting to refresh credentials.", ioe);
      return;
    }

    String configuredConnectionClass =
        fullConfiguration.get(HConnection.HBASE_CLIENT_CONNECTION_IMPL);

    boolean isCorrectClassSpecified = false;

    if (!Strings.isNullOrEmpty(configuredConnectionClass)) {
      try {
        Class<?> connectionClass = Class.forName(configuredConnectionClass);
        isCorrectClassSpecified =
            AbstractBigtableConnection.class.isAssignableFrom(connectionClass);
      } catch (Exception e) {
        // Ignore. Problems will be logged in the println below.
      }
    }
    // We can actually determine if this value is correct (disregarding custom subclasses).
    System.out.println(
        String.format(
            "HBase Connection Class = %s %s",
            configuredConnectionClass, isCorrectClassSpecified ? "(OK)" : "(Configuration error)"));

    System.out.println("Opening table admin connection...");
    try (Connection conn = ConnectionFactory.createConnection(fullConfiguration)) {
      try (Admin admin = conn.getAdmin()) {
        System.out.println(String.format("Tables in cluster %s:", options.getCluster()));
        TableName[] tableNames = admin.listTableNames();
        if (tableNames.length == 0) {
          System.out.println("No tables found.");
        } else {
          for (TableName table : tableNames) {
            System.out.println(table.getNameAsString());
          }
        }
      }
      System.out.println("Closing connection...");
    }
  }