Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2021 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
*
* http://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.example.bigtable;

// [START bigtable_configure_connection_pool]

import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
import com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings;
import java.io.IOException;

public class ConfigureConnectionPool {

public static void configureConnectionPool(String projectId, String instanceId) {
// String projectId = "my-project-id";
// String instanceId = "my-instance-id";

BigtableDataSettings.Builder settingsBuilder = BigtableDataSettings.newBuilder()
.setProjectId(projectId)
.setInstanceId(instanceId);

settingsBuilder.stubSettings()
.setTransportChannelProvider(
EnhancedBigtableStubSettings.defaultGrpcTransportProviderBuilder()
.setPoolSize(250)
.build());

BigtableDataSettings settings = settingsBuilder.build();
try (BigtableDataClient dataClient = BigtableDataClient.create(settings)) {
InstantiatingGrpcChannelProvider provider =
(InstantiatingGrpcChannelProvider) settings.getStubSettings()
.getTransportChannelProvider();

int poolSize = provider.toBuilder().getPoolSize();

System.out.println(String.format("Connected with pool size of %d", poolSize));
} catch (IOException e) {
System.out.println("Error during ConfigureConnectionPool: \n" + e.toString());
}
}
}

// [END bigtable_configure_connection_pool]
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2021 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
*
* http://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.example.bigtable;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.hamcrest.CoreMatchers;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ConfigureConnectionPoolTest {

private static String projectId;
private static String instanceId;
private ByteArrayOutputStream bout;

private static String requireEnv(String varName) {
String value = System.getenv(varName);
assertNotNull(
String.format("Environment variable '%s' is required to perform these tests.", varName),
value);
return value;
}

@BeforeClass
public static void beforeClass() {
projectId = requireEnv("GOOGLE_CLOUD_PROJECT");
instanceId = requireEnv("BIGTABLE_TESTING_INSTANCE");
}

@Before
public void setupStream() {
bout = new ByteArrayOutputStream();
System.setOut(new PrintStream(bout));
}

@Test
public void testConfigureConnectionPool() {
ConfigureConnectionPool.configureConnectionPool(projectId, instanceId);

String output = bout.toString();
assertThat(output, CoreMatchers.containsString("Connected with pool size of 250"));
}
}