Search in sources :

Example 6 with EnhancedBigtableStubSettings

use of com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings in project java-bigtable by googleapis.

the class EnhancedBigtableStubTest method testCallContextPropagatedInMutationBatcher.

@Test
public void testCallContextPropagatedInMutationBatcher() throws IOException, InterruptedException, ExecutionException {
    EnhancedBigtableStubSettings settings = defaultSettings.toBuilder().setRefreshingChannel(true).setPrimedTableIds("table1", "table2").build();
    try (EnhancedBigtableStub stub = EnhancedBigtableStub.create(settings)) {
        // clear the previous contexts
        contextInterceptor.contexts.clear();
        // Override the timeout
        GrpcCallContext clientCtx = GrpcCallContext.createDefault().withTimeout(Duration.ofMinutes(10));
        // Send a batch
        try (Batcher<RowMutationEntry, Void> batcher = stub.newMutateRowsBatcher("table1", clientCtx)) {
            batcher.add(RowMutationEntry.create("key").deleteRow()).get();
        }
        // Ensure that the server got the overriden deadline
        Context serverCtx = contextInterceptor.contexts.poll();
        assertThat(serverCtx).isNotNull();
        assertThat(serverCtx.getDeadline()).isAtLeast(Deadline.after(8, TimeUnit.MINUTES));
    }
}
Also used : GrpcCallContext(com.google.api.gax.grpc.GrpcCallContext) Context(io.grpc.Context) RequestContext(com.google.cloud.bigtable.data.v2.internal.RequestContext) GrpcCallContext(com.google.api.gax.grpc.GrpcCallContext) RowMutationEntry(com.google.cloud.bigtable.data.v2.models.RowMutationEntry) Test(org.junit.Test)

Example 7 with EnhancedBigtableStubSettings

use of com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings in project java-bigtable by googleapis.

the class EnhancedBigtableStubTest method testCallContextPropagatedInReadBatcher.

@Test
public void testCallContextPropagatedInReadBatcher() throws IOException, InterruptedException, ExecutionException {
    EnhancedBigtableStubSettings settings = defaultSettings.toBuilder().setRefreshingChannel(true).setPrimedTableIds("table1", "table2").build();
    try (EnhancedBigtableStub stub = EnhancedBigtableStub.create(settings)) {
        // clear the previous contexts
        contextInterceptor.contexts.clear();
        // Override the timeout
        GrpcCallContext clientCtx = GrpcCallContext.createDefault().withTimeout(Duration.ofMinutes(10));
        // Send a batch
        try (Batcher<ByteString, Row> batcher = stub.newBulkReadRowsBatcher(Query.create("table1"), clientCtx)) {
            batcher.add(ByteString.copyFromUtf8("key")).get();
        }
        // Ensure that the server got the overriden deadline
        Context serverCtx = contextInterceptor.contexts.poll();
        assertThat(serverCtx).isNotNull();
        assertThat(serverCtx.getDeadline()).isAtLeast(Deadline.after(8, TimeUnit.MINUTES));
    }
}
Also used : GrpcCallContext(com.google.api.gax.grpc.GrpcCallContext) Context(io.grpc.Context) RequestContext(com.google.cloud.bigtable.data.v2.internal.RequestContext) GrpcCallContext(com.google.api.gax.grpc.GrpcCallContext) ByteString(com.google.protobuf.ByteString) Row(com.google.cloud.bigtable.data.v2.models.Row) Test(org.junit.Test)

Example 8 with EnhancedBigtableStubSettings

use of com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings in project java-bigtable by googleapis.

the class BigtableDataSettingsTest method testToString.

@Test
public void testToString() {
    BigtableDataSettings settings = BigtableDataSettings.newBuilder().setProjectId("our-project-2-12").setInstanceId("our-instance-85").setAppProfileId("our-appProfile-06").enableBatchMutationLatencyBasedThrottling(10).build();
    EnhancedBigtableStubSettings stubSettings = settings.getStubSettings();
    assertThat(settings.toString()).isEqualTo("BigtableDataSettings{stubSettings=" + stubSettings.toString() + "}");
}
Also used : EnhancedBigtableStubSettings(com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings) Test(org.junit.Test)

Example 9 with EnhancedBigtableStubSettings

use of com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings in project java-bigtable by googleapis.

the class BigtableChannelPrimer method sendPrimeRequests.

private void sendPrimeRequests(ManagedChannel managedChannel) throws IOException {
    // Wrap the channel in a temporary stub
    EnhancedBigtableStubSettings primingSettings = settingsTemplate.toBuilder().setTransportChannelProvider(FixedTransportChannelProvider.create(GrpcTransportChannel.create(managedChannel))).build();
    try (EnhancedBigtableStub stub = EnhancedBigtableStub.create(primingSettings)) {
        Map<String, ApiFuture<?>> primeFutures = new HashMap<>();
        // Prime all of the table ids in parallel
        for (String tableId : tableIds) {
            ApiFuture<Row> f = stub.createReadRowsRawCallable(new DefaultRowAdapter()).first().futureCall(ReadRowsRequest.newBuilder().setTableName(TableName.format(primingSettings.getProjectId(), primingSettings.getInstanceId(), tableId)).setAppProfileId(primingSettings.getAppProfileId()).setRows(RowSet.newBuilder().addRowKeys(PRIMING_ROW_KEY).build()).setFilter(RowFilter.newBuilder().setBlockAllFilter(true).build()).setRowsLimit(1).build());
            primeFutures.put(tableId, f);
        }
        // Wait for all of the prime requests to complete.
        for (Map.Entry<String, ApiFuture<?>> entry : primeFutures.entrySet()) {
            try {
                entry.getValue().get();
            } catch (Throwable e) {
                if (e instanceof ExecutionException) {
                    e = e.getCause();
                }
                LOG.warning(String.format("Failed to prime channel for table: %s: %s", entry.getKey(), e.getMessage()));
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString) DefaultRowAdapter(com.google.cloud.bigtable.data.v2.models.DefaultRowAdapter) ApiFuture(com.google.api.core.ApiFuture) Row(com.google.cloud.bigtable.data.v2.models.Row) ExecutionException(java.util.concurrent.ExecutionException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with EnhancedBigtableStubSettings

use of com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings in project java-bigtable by googleapis.

the class EnhancedBigtableStub method finalizeSettings.

public static EnhancedBigtableStubSettings finalizeSettings(EnhancedBigtableStubSettings settings, Tagger tagger, StatsRecorder stats) throws IOException {
    EnhancedBigtableStubSettings.Builder builder = settings.toBuilder();
    // TODO: this implementation is on the cusp of unwieldy, if we end up adding more features
    // consider splitting it up by feature.
    // workaround JWT audience issues
    patchCredentials(builder);
    // Inject channel priming
    if (settings.isRefreshingChannel()) {
        // Fix the credentials so that they can be shared
        Credentials credentials = null;
        if (settings.getCredentialsProvider() != null) {
            credentials = settings.getCredentialsProvider().getCredentials();
        }
        builder.setCredentialsProvider(FixedCredentialsProvider.create(credentials));
        // Inject the primer
        InstantiatingGrpcChannelProvider transportProvider = (InstantiatingGrpcChannelProvider) settings.getTransportChannelProvider();
        builder.setTransportChannelProvider(transportProvider.toBuilder().setChannelPrimer(BigtableChannelPrimer.create(credentials, settings.getProjectId(), settings.getInstanceId(), settings.getAppProfileId(), settings.getPrimedTableIds())).build());
    }
    ImmutableMap<TagKey, TagValue> attributes = ImmutableMap.<TagKey, TagValue>builder().put(RpcMeasureConstants.BIGTABLE_PROJECT_ID, TagValue.create(settings.getProjectId())).put(RpcMeasureConstants.BIGTABLE_INSTANCE_ID, TagValue.create(settings.getInstanceId())).put(RpcMeasureConstants.BIGTABLE_APP_PROFILE_ID, TagValue.create(settings.getAppProfileId())).build();
    // Inject Opencensus instrumentation
    builder.setTracerFactory(new CompositeTracerFactory(ImmutableList.of(// Add OpenCensus Tracing
    new OpencensusTracerFactory(ImmutableMap.<String, String>builder().put(RpcMeasureConstants.BIGTABLE_PROJECT_ID.getName(), settings.getProjectId()).put(RpcMeasureConstants.BIGTABLE_INSTANCE_ID.getName(), settings.getInstanceId()).put(RpcMeasureConstants.BIGTABLE_APP_PROFILE_ID.getName(), settings.getAppProfileId()).put("gax", GaxGrpcProperties.getGaxGrpcVersion()).put("grpc", GaxGrpcProperties.getGrpcVersion()).put("gapic", Version.VERSION).build()), // Add OpenCensus Metrics
    MetricsTracerFactory.create(tagger, stats, attributes), // Add user configured tracer
    settings.getTracerFactory())));
    return builder.build();
}
Also used : InstantiatingGrpcChannelProvider(com.google.api.gax.grpc.InstantiatingGrpcChannelProvider) CompositeTracerFactory(com.google.cloud.bigtable.data.v2.stub.metrics.CompositeTracerFactory) TagKey(io.opencensus.tags.TagKey) TagValue(io.opencensus.tags.TagValue) OpencensusTracerFactory(com.google.api.gax.tracing.OpencensusTracerFactory) ServiceAccountJwtAccessCredentials(com.google.auth.oauth2.ServiceAccountJwtAccessCredentials) Credentials(com.google.auth.Credentials)

Aggregations

EnhancedBigtableStubSettings (com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings)5 FakeServiceHelper (com.google.cloud.bigtable.data.v2.FakeServiceHelper)4 Before (org.junit.Before)4 Test (org.junit.Test)3 GrpcCallContext (com.google.api.gax.grpc.GrpcCallContext)2 BigtableDataSettings (com.google.cloud.bigtable.data.v2.BigtableDataSettings)2 RequestContext (com.google.cloud.bigtable.data.v2.internal.RequestContext)2 Row (com.google.cloud.bigtable.data.v2.models.Row)2 EnhancedBigtableStub (com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStub)2 ByteString (com.google.protobuf.ByteString)2 Context (io.grpc.Context)2 ApiFuture (com.google.api.core.ApiFuture)1 InstantiatingGrpcChannelProvider (com.google.api.gax.grpc.InstantiatingGrpcChannelProvider)1 ClientContext (com.google.api.gax.rpc.ClientContext)1 OpencensusTracerFactory (com.google.api.gax.tracing.OpencensusTracerFactory)1 Credentials (com.google.auth.Credentials)1 ServiceAccountJwtAccessCredentials (com.google.auth.oauth2.ServiceAccountJwtAccessCredentials)1 Builder (com.google.cloud.bigtable.data.v2.BigtableDataSettings.Builder)1 DefaultRowAdapter (com.google.cloud.bigtable.data.v2.models.DefaultRowAdapter)1 RowMutationEntry (com.google.cloud.bigtable.data.v2.models.RowMutationEntry)1