code: public void waitForOperation(String operationName, int maxSeconds) throws InterruptedException {
      long endTimeMillis = TimeUnit.SECONDS.toMillis(maxSeconds) + System.currentTimeMillis();
  
      GetOperationRequest request = GetOperationRequest.newBuilder().setName(operationName).build();
      do {
        Thread.sleep(500);
        Operation response = client.getOperation(request);
        if (response.getDone()) {
          switch (response.getResultCase()) {
          case RESPONSE:
            return;
          case ERROR:
            throw new RuntimeException("Cluster could not be resized: " + response.getError());
          case RESULT_NOT_SET:
            throw new IllegalStateException(
                "System returned invalid response for Operation check: " + response);
          }
        }
      } while (System.currentTimeMillis() < endTimeMillis);
  
      throw new IllegalStateException(
          String.format("Waited %d seconds and cluster was not resized yet.", maxSeconds));
    }
docs: Waits for an operation like cluster resizing to complete.
  @param operationName The fully qualified name of the operation
  @param maxSeconds The maximum amount of seconds to wait for the operation to complete.
  @throws InterruptedException if a user interrupts the process, usually with a ^C.