/** * 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. */ 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)); }
/** {@inheritDoc} */ @Override public void snapshot(String snapshotName, TableName tableName) throws IOException, SnapshotCreationException, IllegalArgumentException { Operation operation = snapshotTable(snapshotName, tableName); try { connection.getSession().getInstanceAdminClient().waitForOperation(operation); } catch (TimeoutException e) { throw new IOException("Timed out waiting for snapshot creation to finish", e); } }
/** * Gets a {@link ListClustersResponse} that contains all of the clusters for the * projectId/instanceId configuration. * @return all clusters in the instance if the instance ID is provided; otherwise, all clusters in * project are returned. */ public ListClustersResponse getClusters() { logger.info("Reading clusters."); return client.listCluster( ListClustersRequest.newBuilder().setParent(instanceName.getInstanceName()).build()); }
/** * Update a specific cluster's server node count to the number specified */ private void setClusterSize(String clusterName, int newSize) throws InterruptedException { Preconditions.checkArgument(newSize > 0, "Cluster size must be > 0"); logger.info("Updating cluster %s to size %d", clusterName, newSize); Operation operation = client.updateCluster(Cluster.newBuilder() .setName(clusterName) .setServeNodes(newSize) .build()); waitForOperation(operation.getName(), 60); logger.info("Done updating cluster %s.", clusterName); }
/** {@inheritDoc} */ @Override public void snapshot(String snapshotName, TableName tableName) throws IOException, SnapshotCreationException, IllegalArgumentException { Operation operation = snapshotTable(snapshotName, tableName); try { connection.getSession().getInstanceAdminClient().waitForOperation(operation); } catch (TimeoutException e) { throw new IOException("Timed out waiting for snapshot creation to finish", e); } }
/** {@inheritDoc} */ @Override public void cloneSnapshot(String snapshotName, TableName tableName) throws IOException, TableExistsException, RestoreSnapshotException { CreateTableFromSnapshotRequest request = CreateTableFromSnapshotRequest.newBuilder() .setParent(options.getInstanceName().toString()) .setTableId(tableName.getNameAsString()) .setSourceSnapshot(getClusterName().toSnapshotName(snapshotName)) .build(); Operation operation = Futures .getChecked(bigtableTableAdminClient.createTableFromSnapshotAsync(request), IOException.class); try { connection.getSession().getInstanceAdminClient(). waitForOperation(operation); } catch (TimeoutException e) { throw new IOException("Timed out waiting for cloneSnapshot operation to finish", e); } }
/** {@inheritDoc} */ @Override public void cloneSnapshot(String snapshotName, TableName tableName) throws IOException, TableExistsException, RestoreSnapshotException { CreateTableFromSnapshotRequest request = CreateTableFromSnapshotRequest.newBuilder() .setParent(options.getInstanceName().toString()) .setTableId(tableName.getNameAsString()) .setSourceSnapshot(getClusterName().toSnapshotName(snapshotName)) .build(); Operation operation = Futures .getChecked(bigtableTableAdminClient.createTableFromSnapshotAsync(request), IOException.class); try { connection.getSession().getInstanceAdminClient(). waitForOperation(operation); } catch (TimeoutException e) { throw new IOException("Timed out waiting for cloneSnapshot operation to finish", e); } }