Skip to content

Commit 10ae878

Browse files
igorbernstein2pongad
authored andcommitted
Bigtable: Decouple TableAdminClient from BigtableTableAdminSettings. (googleapis#3512)
This is in preparation for renaming the auto generated GAPIC clients & settings to be prefixed with 'Base'. This will make it easier to understand the layout of the code: the GAPIC generated client/settings will be called BaseBigtableTableAdmin{Client,Settings}, while the handwritten overlay will be called BigtableTableAdmin{Client,Settings}.
1 parent c1fb390 commit 10ae878

File tree

3 files changed

+216
-60
lines changed

3 files changed

+216
-60
lines changed

google-cloud-clients/google-cloud-bigtable-admin/src/main/java/com/google/cloud/bigtable/admin/v2/TableAdminClient.java

Lines changed: 28 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.google.api.core.ApiFunction;
2222
import com.google.api.core.ApiFuture;
2323
import com.google.api.core.ApiFutures;
24-
import com.google.api.core.BetaApi;
2524
import com.google.bigtable.admin.v2.CheckConsistencyResponse;
2625
import com.google.bigtable.admin.v2.DeleteTableRequest;
2726
import com.google.bigtable.admin.v2.DropRowRangeRequest;
@@ -40,11 +39,11 @@
4039
import com.google.cloud.bigtable.admin.v2.models.TableAdminResponses.ConsistencyToken;
4140
import com.google.cloud.bigtable.admin.v2.models.TableAdminResponses.Table;
4241
import com.google.cloud.bigtable.admin.v2.stub.BigtableTableAdminStub;
43-
import com.google.cloud.bigtable.admin.v2.stub.BigtableTableAdminStubSettings;
4442
import com.google.common.annotations.VisibleForTesting;
4543
import com.google.common.base.Preconditions;
4644
import com.google.protobuf.ByteString;
4745
import com.google.protobuf.Empty;
46+
import javax.annotation.Nonnull;
4847

4948
/**
5049
* Client for creating, configuring, and deleting Cloud Bigtable tables
@@ -64,95 +63,64 @@
6463
* .addSplit(ByteString.copyFromUtf8("b"))
6564
* .addSplit(ByteString.copyFromUtf8("q"));
6665
* client.createTable(createTableReq);
67-
* }
68-
* }</pre>
66+
* }
67+
* }</pre>
6968
*
70-
* <p>Note: close() needs to be called on the bigtableTableAdminClient object to clean up resources
71-
* such as threads. In the example above, try-with-resources is used, which automatically calls
72-
* close().
69+
* <p>Note: close() needs to be called on the client object to clean up resources such as threads.
70+
* In the example above, try-with-resources is used, which automatically calls close().
7371
*
74-
* <p>This class can be customized by passing in a custom instance of BigtableTableAdminSettings to
72+
* <p>This class can be customized by passing in a custom instance of TableAdminSettings to
7573
* create(). For example:
7674
*
7775
* <p>To customize credentials:
7876
*
7977
* <pre>{@code
80-
* BigtableTableAdminSettings bigtableTableAdminSettings =
81-
* BigtableTableAdminSettings.newBuilder()
78+
* TableAdminSettings tableAdminSettings = TableAdminSettings.newBuilder()
79+
* .setInstanceName(InstanceName.of("[PROJECT]", "[INSTANCE]"))
8280
* .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
8381
* .build();
82+
*
8483
* TableAdminClient client =
85-
* TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"), bigtableTableAdminSettings);
84+
* TableAdminClient.create(tableAdminSettings);
8685
* }</pre>
8786
*
8887
* To customize the endpoint:
8988
*
9089
* <pre>{@code
91-
* BigtableTableAdminSettings bigtableTableAdminSettings =
92-
* BigtableTableAdminSettings.newBuilder().setEndpoint(myEndpoint).build();
93-
* TableAdminClient client =
94-
* TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"), bigtableTableAdminSettings);
90+
* TableAdminSettings tableAdminSettings = TableAdminSettings.newBuilder()
91+
* .setInstanceName(InstanceName.of("[PROJECT]", "[INSTANCE]"))
92+
* .setEndpoint(myEndpoint).build();
93+
*
94+
* TableAdminClient client = TableAdminClient.create(tableAdminSettings);
9595
* }</pre>
9696
*/
97-
@BetaApi
9897
public class TableAdminClient implements AutoCloseable {
9998
private final BigtableTableAdminStub stub;
10099
private final InstanceName instanceName;
101100

102-
/**
103-
* Constructs an instance of TableAdminClient with the given instanceName
104-
*
105-
* @param instanceName
106-
* @throws IOException
107-
*/
108-
public static TableAdminClient create(InstanceName instanceName) throws IOException {
109-
return new TableAdminClient(instanceName, BigtableTableAdminSettings.newBuilder().build());
101+
/** Constructs an instance of TableAdminClient with the given instanceName. */
102+
public static TableAdminClient create(@Nonnull InstanceName instanceName) throws IOException {
103+
return create(TableAdminSettings.newBuilder().setInstanceName(instanceName).build());
110104
}
111105

112-
/**
113-
* Constructs an instance of TableAdminClient with the given instanceName and
114-
* bigtableTableAdminSettings
115-
*
116-
* @param instanceName
117-
* @param adminSettings
118-
* @throws IOException
119-
*/
120-
public static TableAdminClient create(
121-
InstanceName instanceName, BigtableTableAdminSettings adminSettings) throws IOException {
122-
return new TableAdminClient(instanceName, adminSettings);
106+
/** Constructs an instance of TableAdminClient with the given settings. */
107+
public static TableAdminClient create(@Nonnull TableAdminSettings settings) throws IOException {
108+
return create(settings.getInstanceName(), settings.getStubSettings().createStub());
123109
}
124110

125-
/**
126-
* Constructs an instance of TableAdminClient with the given instanceName and
127-
* bigtableTableAdminStub
128-
*
129-
* @param instanceName
130-
* @param stub
131-
* @throws IOException
132-
*/
133-
public static TableAdminClient create(InstanceName instanceName, BigtableTableAdminStub stub)
134-
throws IOException {
111+
/** Constructs an instance of TableAdminClient with the given instanceName and stub. */
112+
public static TableAdminClient create(@Nonnull InstanceName instanceName, @Nonnull BigtableTableAdminStub stub) {
135113
return new TableAdminClient(instanceName, stub);
136114
}
137115

138-
private TableAdminClient(InstanceName instanceName, BigtableTableAdminSettings adminSettings)
139-
throws IOException {
140-
this(
141-
instanceName,
142-
((BigtableTableAdminStubSettings) adminSettings.getStubSettings()).createStub());
143-
}
144-
145-
private TableAdminClient(InstanceName instanceName, BigtableTableAdminStub stub)
146-
throws IOException {
116+
private TableAdminClient(@Nonnull InstanceName instanceName, @Nonnull BigtableTableAdminStub stub) {
147117
Preconditions.checkNotNull(instanceName);
148118
Preconditions.checkNotNull(stub);
149119
this.instanceName = instanceName;
150120
this.stub = stub;
151121
}
152122

153-
/**
154-
* Gets the instanceName this client is associated to
155-
*/
123+
/** Gets the instanceName this client is associated with. */
156124
public InstanceName getInstanceName() {
157125
return instanceName;
158126
}
@@ -188,9 +156,9 @@ public Table createTable(CreateTable createTable) {
188156

189157
/**
190158
* Creates a new table with the specified configuration asynchronously
191-
*
159+
*
192160
* <p>Sample code:
193-
*
161+
*
194162
* <pre>{@code
195163
* try(TableAdminClient client = TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
196164
* CreateTable createTableReq =
@@ -343,7 +311,7 @@ public Table getTable(String tableId) {
343311
* Gets the Table by tableId
344312
*
345313
* <p>Sample code:
346-
*
314+
*
347315
* <pre>{@code
348316
* try(TableAdminClient client = TableAdminClient.create(InstanceName.of("[PROJECT]", "[INSTANCE]"))) {
349317
* client.getTableAsync("tableId");
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.cloud.bigtable.admin.v2;
17+
18+
import com.google.bigtable.admin.v2.InstanceName;
19+
import com.google.cloud.bigtable.admin.v2.stub.BigtableTableAdminStubSettings;
20+
import com.google.common.base.Preconditions;
21+
import com.google.common.base.Verify;
22+
import java.io.IOException;
23+
import javax.annotation.Nonnull;
24+
import javax.annotation.Nullable;
25+
26+
/**
27+
* Settings class to configure an instance of {@link TableAdminClient}.
28+
*
29+
* <p>It must be configured with an {@link InstanceName} and be used to change default RPC settings.
30+
*
31+
* <p>Example usage:
32+
*
33+
* <pre>{@code
34+
* TableAdminSettings.Builder tableAdminSettingsBuilder = TableAdminSettings.newBuilder()
35+
* .setInstanceName(InstanceName.of("my-project", "my-instance");
36+
*
37+
* tableAdminSettingsBuilder.stubSettings().createTableSettings()
38+
* .setRetrySettings(
39+
* RetrySettings.newBuilder()
40+
* .setTotalTimeout(Duration.ofMinutes(15))
41+
* .build());
42+
*
43+
* BigtableTableAdminSettings tableAdminSettings = tableAdminSettingsBuilder.build();
44+
* }</pre>
45+
*/
46+
public final class TableAdminSettings {
47+
private final InstanceName instanceName;
48+
private final BigtableTableAdminStubSettings stubSettings;
49+
50+
private TableAdminSettings(Builder builder) throws IOException {
51+
this.instanceName = Preconditions.checkNotNull(builder.instanceName, "InstanceName must be set");
52+
this.stubSettings = Verify.verifyNotNull(builder.stubSettings, "stubSettings should never be null").build();
53+
}
54+
55+
/** Gets the name of instance whose tables the client will manage. */
56+
@Nonnull
57+
public InstanceName getInstanceName() {
58+
return instanceName;
59+
}
60+
61+
/** Gets the underlying RPC settings. */
62+
public BigtableTableAdminStubSettings getStubSettings() {
63+
return stubSettings;
64+
}
65+
66+
/** Returns a builder containing all the values of this settings class. */
67+
public Builder toBuilder() {
68+
return new Builder(this);
69+
}
70+
71+
/** Returns a new builder for this class. */
72+
public static Builder newBuilder() {
73+
return new Builder();
74+
}
75+
76+
/** Builder for TableAdminSettings. */
77+
public static final class Builder {
78+
@Nullable
79+
private InstanceName instanceName;
80+
private final BigtableTableAdminStubSettings.Builder stubSettings;
81+
82+
private Builder() {
83+
stubSettings = BigtableTableAdminStubSettings.newBuilder();
84+
}
85+
86+
private Builder(TableAdminSettings settings) {
87+
this.instanceName = settings.instanceName;
88+
this.stubSettings = settings.stubSettings.toBuilder();
89+
}
90+
91+
/** Sets the name of instance whose tables the client will manage. */
92+
public Builder setInstanceName(@Nonnull InstanceName instanceName) {
93+
Preconditions.checkNotNull(instanceName);
94+
this.instanceName = instanceName;
95+
return this;
96+
}
97+
98+
/** Gets the name of instance whose tables the client will manage. */
99+
@Nullable
100+
public InstanceName getInstanceName() {
101+
return instanceName;
102+
}
103+
104+
/**
105+
* Returns the builder for the settings used for all RPCs.
106+
*
107+
* <p>This is meant for advanced usage. The default RPC settings are set to their recommended
108+
* values.
109+
*/
110+
public BigtableTableAdminStubSettings.Builder stubSettings() {
111+
return stubSettings;
112+
}
113+
114+
/** Builds an instance of the settings. */
115+
public TableAdminSettings build() throws IOException {
116+
return new TableAdminSettings(this);
117+
}
118+
}
119+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.cloud.bigtable.admin.v2;
17+
18+
import static com.google.common.truth.Truth.assertThat;
19+
20+
import com.google.api.gax.rpc.StatusCode.Code;
21+
import com.google.bigtable.admin.v2.InstanceName;
22+
import java.io.IOException;
23+
import org.junit.Test;
24+
25+
public class TableAdminSettingsTest {
26+
27+
@Test
28+
public void testInstanceName() throws IOException {
29+
InstanceName instanceName = InstanceName.of("my-project", "my-instance");
30+
31+
TableAdminSettings.Builder builder = TableAdminSettings.newBuilder()
32+
.setInstanceName(instanceName);
33+
34+
assertThat(builder.getInstanceName()).isEqualTo(instanceName);
35+
assertThat(builder.build().getInstanceName()).isEqualTo(instanceName);
36+
assertThat(builder.build().toBuilder().getInstanceName()).isEqualTo(instanceName);
37+
}
38+
39+
@Test
40+
public void testMissingInstanceName() {
41+
Exception actualException = null;
42+
43+
try {
44+
TableAdminSettings.newBuilder().build();
45+
} catch (Exception e) {
46+
actualException = e;
47+
}
48+
49+
assertThat(actualException).isInstanceOf(NullPointerException.class);
50+
}
51+
52+
@Test
53+
public void testStubSettings() throws IOException {
54+
InstanceName instanceName = InstanceName.of("my-project", "my-instance");
55+
56+
TableAdminSettings.Builder builder = TableAdminSettings.newBuilder()
57+
.setInstanceName(instanceName);
58+
59+
builder.stubSettings().createTableSettings()
60+
.setRetryableCodes(Code.INVALID_ARGUMENT);
61+
62+
assertThat(builder.build().getStubSettings().createTableSettings().getRetryableCodes())
63+
.containsExactly(Code.INVALID_ARGUMENT);
64+
65+
assertThat(builder.build().toBuilder().build().getStubSettings().createTableSettings()
66+
.getRetryableCodes())
67+
.containsExactly(Code.INVALID_ARGUMENT);
68+
}
69+
}

0 commit comments

Comments
 (0)