Search in sources :

Example 1 with PartialListClustersException

use of com.google.cloud.bigtable.admin.v2.models.PartialListClustersException in project java-bigtable by googleapis.

the class BigtableInstanceAdminClientTests method testListClustersFailedZone.

@Test
public void testListClustersFailedZone() {
    // Setup
    Mockito.when(mockStub.listClustersCallable()).thenReturn(mockListClustersCallable);
    com.google.bigtable.admin.v2.ListClustersRequest expectedRequest = com.google.bigtable.admin.v2.ListClustersRequest.newBuilder().setParent(INSTANCE_NAME).build();
    com.google.bigtable.admin.v2.ListClustersResponse expectedResponse = com.google.bigtable.admin.v2.ListClustersResponse.newBuilder().addClusters(com.google.bigtable.admin.v2.Cluster.newBuilder().setName(CLUSTER_NAME)).addFailedLocations(NameUtil.formatLocationName(PROJECT_ID, "us-east1-c")).build();
    Mockito.when(mockListClustersCallable.futureCall(expectedRequest)).thenReturn(ApiFutures.immediateFuture(expectedResponse));
    // Execute
    Exception actualError = null;
    try {
        adminClient.listClusters(INSTANCE_ID);
    } catch (Exception e) {
        actualError = e;
    }
    // Verify
    assertThat(actualError).isInstanceOf(PartialListClustersException.class);
    assert actualError != null;
    PartialListClustersException partialListError = (PartialListClustersException) actualError;
    assertThat(partialListError.getClusters()).containsExactly(Cluster.fromProto(expectedResponse.getClusters(0)));
    assertThat(partialListError.getUnavailableZones()).containsExactly("us-east1-c");
}
Also used : PartialListClustersException(com.google.cloud.bigtable.admin.v2.models.PartialListClustersException) NotFoundException(com.google.api.gax.rpc.NotFoundException) PartialListClustersException(com.google.cloud.bigtable.admin.v2.models.PartialListClustersException) PartialListInstancesException(com.google.cloud.bigtable.admin.v2.models.PartialListInstancesException) Test(org.junit.Test)

Example 2 with PartialListClustersException

use of com.google.cloud.bigtable.admin.v2.models.PartialListClustersException in project java-bigtable by googleapis.

the class BigtableInstanceAdminClient method listClustersAsync.

/**
 * Asynchronously lists all clusters in the specified instance.
 *
 * <p>This method will throw a {@link PartialListClustersException} when any zone is unavailable.
 * If a partial list is OK, the exception can be caught and inspected.
 *
 * <p>Sample code:
 *
 * <pre>{@code
 * ApiFuture<Cluster> clustersFuture = client.listClustersAsync("my-instance");
 *
 * ApiFutures.addCallback(clustersFuture, new ApiFutureCallback<List<Cluster>>() {
 *   public void onFailure(Throwable t) {
 *     if (t instanceof PartialListClustersException) {
 *       PartialListClustersException partialError = (PartialListClustersException)t;
 *       System.out.println("The following zones are unavailable: " + partialError.getUnavailableZones());
 *       System.out.println("But the following clusters are reachable: " + partialError.getClusters());
 *     } else {
 *       t.printStackTrace();
 *     }
 *   }
 *
 *   public void onSuccess(List<Cluster> result) {
 *     System.out.println("Found a complete set of instances: " + result);
 *   }
 * }, MoreExecutors.directExecutor());
 * }</pre>
 */
@SuppressWarnings("WeakerAccess")
public ApiFuture<List<Cluster>> listClustersAsync(String instanceId) {
    String name = NameUtil.formatInstanceName(projectId, instanceId);
    com.google.bigtable.admin.v2.ListClustersRequest request = com.google.bigtable.admin.v2.ListClustersRequest.newBuilder().setParent(name).build();
    return ApiFutures.transform(stub.listClustersCallable().futureCall(request), new ApiFunction<com.google.bigtable.admin.v2.ListClustersResponse, List<Cluster>>() {

        @Override
        public List<Cluster> apply(com.google.bigtable.admin.v2.ListClustersResponse proto) {
            // NOTE: Server-side pagination is not and will not be implemented, so remaining pages
            // are not fetched. However, if that assumption turns out to be wrong, fail fast to
            // avoid returning partial data.
            Verify.verify(proto.getNextPageToken().isEmpty(), "Server returned an unexpected paginated response");
            ImmutableList.Builder<Cluster> clusters = ImmutableList.builder();
            for (com.google.bigtable.admin.v2.Cluster cluster : proto.getClustersList()) {
                clusters.add(Cluster.fromProto(cluster));
            }
            ImmutableList.Builder<String> failedZones = ImmutableList.builder();
            for (String locationStr : proto.getFailedLocationsList()) {
                failedZones.add(NameUtil.extractZoneIdFromLocationName(locationStr));
            }
            if (!failedZones.build().isEmpty()) {
                throw new PartialListClustersException(failedZones.build(), clusters.build());
            }
            return clusters.build();
        }
    }, MoreExecutors.directExecutor());
}
Also used : Cluster(com.google.cloud.bigtable.admin.v2.models.Cluster) PartialListClustersException(com.google.cloud.bigtable.admin.v2.models.PartialListClustersException) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 3 with PartialListClustersException

use of com.google.cloud.bigtable.admin.v2.models.PartialListClustersException in project java-bigtable by googleapis.

the class BigtableInstanceAdminClientTest method testListClustersFailedZone.

@Test
public void testListClustersFailedZone() {
    // Setup
    Mockito.when(mockStub.listClustersCallable()).thenReturn(mockListClustersCallable);
    com.google.bigtable.admin.v2.ListClustersRequest expectedRequest = com.google.bigtable.admin.v2.ListClustersRequest.newBuilder().setParent(INSTANCE_NAME).build();
    com.google.bigtable.admin.v2.ListClustersResponse expectedResponse = com.google.bigtable.admin.v2.ListClustersResponse.newBuilder().addClusters(com.google.bigtable.admin.v2.Cluster.newBuilder().setName(CLUSTER_NAME)).addFailedLocations(NameUtil.formatLocationName(PROJECT_ID, "us-east1-c")).build();
    Mockito.when(mockListClustersCallable.futureCall(expectedRequest)).thenReturn(ApiFutures.immediateFuture(expectedResponse));
    // Execute
    Exception actualError = null;
    try {
        adminClient.listClusters(INSTANCE_ID);
    } catch (Exception e) {
        actualError = e;
    }
    // Verify
    assertThat(actualError).isInstanceOf(PartialListClustersException.class);
    assert actualError != null;
    PartialListClustersException partialListError = (PartialListClustersException) actualError;
    assertThat(partialListError.getClusters()).containsExactly(Cluster.fromProto(expectedResponse.getClusters(0)));
    assertThat(partialListError.getUnavailableZones()).containsExactly("us-east1-c");
}
Also used : PartialListClustersException(com.google.cloud.bigtable.admin.v2.models.PartialListClustersException) NotFoundException(com.google.api.gax.rpc.NotFoundException) PartialListClustersException(com.google.cloud.bigtable.admin.v2.models.PartialListClustersException) PartialListInstancesException(com.google.cloud.bigtable.admin.v2.models.PartialListInstancesException) Test(org.junit.Test)

Aggregations

PartialListClustersException (com.google.cloud.bigtable.admin.v2.models.PartialListClustersException)3 NotFoundException (com.google.api.gax.rpc.NotFoundException)2 PartialListInstancesException (com.google.cloud.bigtable.admin.v2.models.PartialListInstancesException)2 Test (org.junit.Test)2 Cluster (com.google.cloud.bigtable.admin.v2.models.Cluster)1 ImmutableList (com.google.common.collect.ImmutableList)1 List (java.util.List)1