@Override public CompletableFuture<List<SnapshotDescription>> listSnapshots() { return CompletableFuture.supplyAsync(() -> { try { return ListSnapshotsRequest.newBuilder() .setParent(getSnapshotClusterName().toString()) .build(); } catch (IOException e) { throw new CompletionException(e); } }).thenCompose(request -> bigtableTableAdminClient.listSnapshotsAsync(request) .thenApply(r -> r.getSnapshotsList() .stream() .map(BigtableAsyncAdmin::toSnapshotDescription) .collect(Collectors.toList()) ) ); }
/** * @return The instance id associated with the given project, zone and cluster. We expect instance * and cluster to have one-to-one relationship. * * @throws IllegalStateException if the cluster is not found */ public static String lookupInstanceId(String projectId, String clusterId, String zoneId) throws IOException { BigtableClusterUtilities utils; try { utils = BigtableClusterUtilities.forAllInstances(projectId); } catch (GeneralSecurityException e) { throw new RuntimeException("Could not initialize BigtableClusterUtilities", e); } try { Cluster cluster = utils.getCluster(clusterId, zoneId); return new BigtableClusterName(cluster.getName()).getInstanceId(); } finally { try { utils.close(); } catch (Exception e) { logger.warn("Error closing BigtableClusterUtilities: ", e); } } }
@Override public CompletableFuture<Void> deleteSnapshot(String snapshotName) { return CompletableFuture.supplyAsync(() -> { try { return DeleteSnapshotRequest.newBuilder() .setName(getClusterName().toSnapshotName(snapshotName)) .build(); } catch (IOException e) { throw new CompletionException(e); } }).thenCompose( d -> bigtableTableAdminClient.deleteSnapshotAsync(d).thenApply(r -> null)); }
/** * Constructor for the auto-scaler with the minimum required information: project and instance ids. * @param projectId * @param instanceId * @throws GeneralSecurityException * @throws IOException */ public MetricScaler(String projectId, String instanceId) throws GeneralSecurityException, IOException { clusterUtility = BigtableClusterUtilities.forInstance(projectId, instanceId); Cluster cluster = clusterUtility.getSingleCluster(); this.clusterId = new BigtableClusterName(cluster.getName()).getClusterId(); this.zoneId = BigtableClusterUtilities.getZoneId(cluster); // Instantiates a client metricServiceClient = MetricServiceClient.create(); projectName = ProjectName.create(projectId); }
public BigtableClusterName toClusterName(String clusterId) { return new BigtableClusterName(instanceName + "/clusters/" + clusterId); } }
/** * Sets a cluster size to a specific size. * @param clusterId * @param zoneId * @param newSize * @throws InterruptedException if the cluster is in the middle of updating, and an interrupt was * received */ public void setClusterSize(String clusterId, String zoneId, int newSize) throws InterruptedException { setClusterSize(instanceName.toClusterName(clusterId).getClusterName(), newSize); }
/** * Snapshot operations need various aspects of a {@link BigtableClusterName}. This method gets a * clusterId from either a lookup (projectId and instanceId translate to a single clusterId when * an instance has only one cluster). */ public synchronized BigtableClusterName getClusterName() throws IOException { if (this.clusterName == null) { try (BigtableClusterUtilities util = new BigtableClusterUtilities(options)) { ListClustersResponse clusters = util.getClusters(); Preconditions.checkState(clusters.getClustersCount() == 1, String.format( "Project '%s' / Instance '%s' has %d clusters. There must be exactly 1 for this operation to work.", options.getProjectId(), options.getInstanceId(), clusters.getClustersCount())); clusterName = new BigtableClusterName(clusters.getClusters(0).getName()); } catch (GeneralSecurityException e) { throw new IOException("Could not get cluster Id.", e); } } return clusterName; }
@Override public CompletableFuture<List<SnapshotDescription>> listSnapshots() { return CompletableFuture.supplyAsync(() -> { try { return ListSnapshotsRequest.newBuilder() .setParent(getSnapshotClusterName().toString()) .build(); } catch (IOException e) { throw new CompletionException(e); } }).thenCompose(request -> bigtableTableAdminClient.listSnapshotsAsync(request) .thenApply(r -> r.getSnapshotsList() .stream() .map(BigtableAsyncAdmin::toSnapshotDescription) .collect(Collectors.toList()) ) ); }
@Override public CompletableFuture<Void> deleteSnapshot(String snapshotName) { return CompletableFuture.supplyAsync(() -> { try { return DeleteSnapshotRequest.newBuilder() .setName(getClusterName().toSnapshotName(snapshotName)) .build(); } catch (IOException e) { throw new CompletionException(e); } }).thenCompose( d -> bigtableTableAdminClient.deleteSnapshotAsync(d).thenApply(r -> null)); }
@Override public CompletableFuture<Void> snapshot(String snapshotName, TableName tableName) { return CompletableFuture.supplyAsync(() -> { try { return SnapshotTableRequest.newBuilder() .setCluster(getSnapshotClusterName().toString()) .setSnapshotId(snapshotName) .setName(options.getInstanceName().toTableNameStr(tableName.getNameAsString())).build(); } catch (IOException e) { throw new CompletionException(e); } }).thenCompose( c -> bigtableTableAdminClient.snapshotTableAsync(c).thenApply(r -> null)); }
@Override public CompletableFuture<Void> cloneSnapshot(String snapshotName, TableName tableName) { return CompletableFuture.supplyAsync(() -> { try { return CreateTableFromSnapshotRequest.newBuilder() .setParent(options.getInstanceName().toString()) .setTableId(tableName.getNameAsString()) .setSourceSnapshot(getClusterName().toSnapshotName(snapshotName)).build(); } catch (IOException e) { throw new CompletionException(e); } }).thenCompose( c -> bigtableTableAdminClient.createTableFromSnapshotAsync(c).thenApply(r -> null)); }
@Override public CompletableFuture<Void> snapshot(String snapshotName, TableName tableName) { return CompletableFuture.supplyAsync(() -> { try { return SnapshotTableRequest.newBuilder() .setCluster(getSnapshotClusterName().toString()) .setSnapshotId(snapshotName) .setName(options.getInstanceName().toTableNameStr(tableName.getNameAsString())).build(); } catch (IOException e) { throw new CompletionException(e); } }).thenCompose( c -> bigtableTableAdminClient.snapshotTableAsync(c).thenApply(r -> null)); }
@Override public CompletableFuture<Void> cloneSnapshot(String snapshotName, TableName tableName) { return CompletableFuture.supplyAsync(() -> { try { return CreateTableFromSnapshotRequest.newBuilder() .setParent(options.getInstanceName().toString()) .setTableId(tableName.getNameAsString()) .setSourceSnapshot(getClusterName().toSnapshotName(snapshotName)).build(); } catch (IOException e) { throw new CompletionException(e); } }).thenCompose( c -> bigtableTableAdminClient.createTableFromSnapshotAsync(c).thenApply(r -> null)); }
@Override public List<SnapshotDescription> listSnapshots() throws IOException { ListSnapshotsRequest request = ListSnapshotsRequest.newBuilder() .setParent(getSnapshotClusterName().toString()) .build(); ListSnapshotsResponse snapshotList = Futures.getChecked(bigtableTableAdminClient .listSnapshotsAsync(request), IOException.class); List<SnapshotDescription> response = new ArrayList<>(); for (Snapshot snapshot : snapshotList.getSnapshotsList()) { response.add(new SnapshotDescription( snapshot.getName(), TableName.valueOf(snapshot.getSourceTable().getName()))); } return response; }
/** {@inheritDoc} */ @Override public void deleteSnapshot(String snapshotName) throws IOException { String btSnapshotName = getClusterName().toSnapshotName(snapshotName); DeleteSnapshotRequest request = DeleteSnapshotRequest.newBuilder() .setName(btSnapshotName) .build(); Futures.getUnchecked(bigtableTableAdminClient.deleteSnapshotAsync(request)); }
@Override public List<SnapshotDescription> listSnapshots() throws IOException { ListSnapshotsRequest request = ListSnapshotsRequest.newBuilder() .setParent(getSnapshotClusterName().toString()) .build(); ListSnapshotsResponse snapshotList = Futures.getChecked(bigtableTableAdminClient .listSnapshotsAsync(request), IOException.class); List<SnapshotDescription> response = new ArrayList<>(); for (Snapshot snapshot : snapshotList.getSnapshotsList()) { response.add(new SnapshotDescription( snapshot.getName(), TableName.valueOf(snapshot.getSourceTable().getName()))); } return response; }
/** {@inheritDoc} */ @Override public void deleteSnapshot(String snapshotName) throws IOException { String btSnapshotName = getClusterName().toSnapshotName(snapshotName); DeleteSnapshotRequest request = DeleteSnapshotRequest.newBuilder() .setName(btSnapshotName) .build(); Futures.getUnchecked(bigtableTableAdminClient.deleteSnapshotAsync(request)); }
/** {@inheritDoc} */ @Override public List<HBaseProtos.SnapshotDescription> listSnapshots() throws IOException { ListSnapshotsRequest request = ListSnapshotsRequest.newBuilder() .setParent(getSnapshotClusterName().toString()) .build(); ListSnapshotsResponse snapshotList = Futures.getChecked( bigtableTableAdminClient.listSnapshotsAsync(request), IOException.class ); List<HBaseProtos.SnapshotDescription> response = new ArrayList<>(); for (Snapshot snapshot : snapshotList.getSnapshotsList()) { BigtableSnapshotName snapshotName = new BigtableSnapshotName(snapshot.getName()); BigtableTableName tableName = new BigtableTableName(snapshot.getSourceTable().getName()); response.add(HBaseProtos.SnapshotDescription.newBuilder() .setName(snapshotName.getSnapshotId()) .setTable(tableName.getTableId()) .setCreationTime(TimeUnit.SECONDS.toMillis(snapshot.getCreateTime().getSeconds())) .build()); } return response; }
/** {@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); } }
/** * Creates a snapshot from an existing table. NOTE: Cloud Bigtable has a cleanup policy * * @param snapshotName * @param tableName * @return * @throws IOException */ protected Operation snapshotTable(String snapshotName, TableName tableName) throws IOException { SnapshotTableRequest.Builder requestBuilder = SnapshotTableRequest.newBuilder() .setCluster(getSnapshotClusterName().toString()) .setSnapshotId(snapshotName) .setName(options.getInstanceName().toTableNameStr(tableName.getNameAsString())); int ttlSecs = configuration.getInt(BigtableOptionsFactory.BIGTABLE_SNAPSHOT_DEFAULT_TTL_SECS_KEY, -1); if (ttlSecs > 0) { requestBuilder.setTtl( Duration.newBuilder().setSeconds(ttlSecs).build() ); } ListenableFuture<Operation> future = bigtableTableAdminClient .snapshotTableAsync(requestBuilder.build()); return Futures.getChecked(future, IOException.class); }