Skip to content

Commit

Permalink
feat: add implementation of ChannelPrimer to establish connection to …
Browse files Browse the repository at this point in the history
…GFE and integrate into bigtable client
  • Loading branch information
tonytanger committed Dec 17, 2019
1 parent 4e77ace commit 8651577
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
Expand Up @@ -19,8 +19,10 @@
import com.google.api.core.BetaApi;
import com.google.api.gax.core.CredentialsProvider;
import com.google.api.gax.core.NoCredentialsProvider;
import com.google.api.gax.grpc.ChannelPrimer;
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
import com.google.api.gax.rpc.UnaryCallSettings;
import com.google.cloud.bigtable.data.v2.internal.RefreshChannel;
import com.google.cloud.bigtable.data.v2.models.Query;
import com.google.cloud.bigtable.data.v2.models.Row;
import com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings;
Expand Down Expand Up @@ -275,6 +277,21 @@ public CredentialsProvider getCredentialsProvider() {
return stubSettings.getCredentialsProvider();
}

/** Set to true to enable channels to periodically reconnect to GFE, false to disable */
@BetaApi("This API depends on experimental gRPC APIs")
public Builder setRefreshChannel(boolean isRefreshing) {
ChannelPrimer channelPrimer = null;
if (isRefreshing) {
channelPrimer = new RefreshChannel();
}
InstantiatingGrpcChannelProvider.Builder channelBuilder =
((InstantiatingGrpcChannelProvider) stubSettings().getTransportChannelProvider())
.toBuilder()
.setChannelPrimer(channelPrimer);
stubSettings().setTransportChannelProvider(channelBuilder.build());
return this;
}

/**
* Returns the underlying settings for making RPC calls. The settings should be changed with
* care.
Expand Down
@@ -0,0 +1,48 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigtable.data.v2.internal;

import com.google.api.core.InternalApi;
import com.google.api.gax.grpc.ChannelPrimer;
import io.grpc.ConnectivityState;
import io.grpc.ExperimentalApi;
import io.grpc.ManagedChannel;
import java.util.concurrent.TimeUnit;

/**
* Establish a connection to the GFE on managedChannel
*
* <p>This class is considered an internal implementation detail and not meant to be used by
* applications.
*/
@ExperimentalApi("This API depends on gRPC experimental API")
@InternalApi
public class RefreshChannel implements ChannelPrimer {
@Override
public void primeChannel(ManagedChannel managedChannel) {
for (int i = 0; i < 10; i++) {
ConnectivityState connectivityState = managedChannel.getState(true);
if (connectivityState == ConnectivityState.READY) {
break;
}
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
break;
}
}
}
}

0 comments on commit 8651577

Please sign in to comment.