Skip to content

Commit cb5d6fd

Browse files
igorbernstein2pongad
authored andcommitted
---
yaml --- r: 9031 b: refs/heads/lesv-patch-1 c: 1fc5c1b h: refs/heads/master i: 9029: 7003c86 9027: d5f3dbd 9023: bf70211
1 parent 243504e commit cb5d6fd

4 files changed

Lines changed: 223 additions & 7 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ refs/tags/v0.22.0: 18b298fe4bfe8ec2f20b0e0bf7ffdcce5cc3c5fe
6666
refs/heads/vam-google-patch-1: d0c8fee3a4074d0bf7360ce8c4f7f7223d0ee7b9
6767
refs/heads/vam-google-patch-CODEOWNERS: 2ac1616e25229e51d08a984708ef1918f91a35ee
6868
refs/heads/danoscarmike-patch-1: 7342a9916bce4ed00002c7202e2a16c5d46afaea
69-
refs/heads/lesv-patch-1: 9736214ee6690db251293a209f4fb17619081024
69+
refs/heads/lesv-patch-1: 1fc5c1bbba7c0713ed9c2b31c6c673621f48b981
7070
refs/heads/ml-update-branch: 079dd6610017f5c51b9d1938c12d6d55b61513cf
7171
refs/heads/vkedia-patch-2: 7d8241388a9769a5c069334761b06c7012c878e7
7272
refs/heads/vkedia-patch-3: 4d128043acaa7db9160faf439d2ca6104e8a88cb

branches/lesv-patch-1/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStub.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818
import com.google.api.core.ApiFuture;
1919
import com.google.api.core.InternalApi;
2020
import com.google.api.gax.rpc.ApiCallContext;
21+
import com.google.api.gax.rpc.Callables;
2122
import com.google.api.gax.rpc.ClientContext;
2223
import com.google.api.gax.rpc.ResponseObserver;
2324
import com.google.api.gax.rpc.ServerStreamingCallable;
2425
import com.google.api.gax.rpc.UnaryCallable;
26+
import com.google.bigtable.v2.SampleRowKeysRequest;
27+
import com.google.bigtable.v2.SampleRowKeysResponse;
2528
import com.google.cloud.bigtable.data.v2.internal.RequestContext;
2629
import com.google.cloud.bigtable.data.v2.models.DefaultRowAdapter;
2730
import com.google.cloud.bigtable.data.v2.models.KeyOffset;
@@ -64,6 +67,14 @@ public static EnhancedBigtableStub create(EnhancedBigtableStubSettings settings)
6467
.setEndpoint(settings.getEndpoint())
6568
.setCredentialsProvider(settings.getCredentialsProvider());
6669

70+
// SampleRowKeys retries are handled in the overlay: disable retries in the base layer (but make
71+
// sure to preserve the exception callable settings.
72+
baseSettingsBuilder
73+
.sampleRowKeysSettings()
74+
.setSimpleTimeoutNoRetries(
75+
settings.sampleRowKeysSettings().getRetrySettings().getTotalTimeout())
76+
.setRetryableCodes(settings.sampleRowKeysSettings().getRetryableCodes());
77+
6778
BigtableStubSettings baseSettings = baseSettingsBuilder.build();
6879
ClientContext clientContext = ClientContext.create(baseSettings);
6980
GrpcBigtableStub stub = new GrpcBigtableStub(baseSettings, clientContext);
@@ -109,12 +120,16 @@ public void call(
109120
* </ul>
110121
*/
111122
private UnaryCallable<String, List<KeyOffset>> createSampleRowKeysCallable() {
112-
return new UnaryCallable<String, List<KeyOffset>>() {
113-
@Override
114-
public ApiFuture<List<KeyOffset>> futureCall(String request, ApiCallContext context) {
115-
throw new UnsupportedOperationException("todo");
116-
}
117-
};
123+
UnaryCallable<SampleRowKeysRequest, List<SampleRowKeysResponse>> spoolable =
124+
stub.sampleRowKeysCallable().all();
125+
126+
UnaryCallable<SampleRowKeysRequest, List<SampleRowKeysResponse>> retryable =
127+
Callables.retrying(spoolable, settings.sampleRowKeysSettings(), clientContext);
128+
129+
UnaryCallable<SampleRowKeysRequest, List<SampleRowKeysResponse>> withContext =
130+
retryable.withDefaultCallContext(clientContext.getDefaultCallContext());
131+
132+
return new SampleRowKeysCallable(withContext, requestContext);
118133
}
119134

120135
private UnaryCallable<RowMutation, Void> createMutateRowCallable() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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.data.v2.stub;
17+
18+
import com.google.api.core.ApiFunction;
19+
import com.google.api.core.ApiFuture;
20+
import com.google.api.core.ApiFutures;
21+
import com.google.api.gax.rpc.ApiCallContext;
22+
import com.google.api.gax.rpc.UnaryCallable;
23+
import com.google.bigtable.v2.SampleRowKeysRequest;
24+
import com.google.bigtable.v2.SampleRowKeysResponse;
25+
import com.google.bigtable.v2.TableName;
26+
import com.google.cloud.bigtable.data.v2.internal.RequestContext;
27+
import com.google.cloud.bigtable.data.v2.models.KeyOffset;
28+
import com.google.common.collect.ImmutableList;
29+
import java.util.List;
30+
31+
/** Simple wrapper for SampleRowKeys to wrap the request and response protobufs. */
32+
class SampleRowKeysCallable extends UnaryCallable<String, List<KeyOffset>> {
33+
private final RequestContext requestContext;
34+
private final UnaryCallable<SampleRowKeysRequest, List<SampleRowKeysResponse>> inner;
35+
36+
SampleRowKeysCallable(
37+
UnaryCallable<SampleRowKeysRequest, List<SampleRowKeysResponse>> inner,
38+
RequestContext requestContext) {
39+
40+
this.requestContext = requestContext;
41+
this.inner = inner;
42+
}
43+
44+
@Override
45+
public ApiFuture<List<KeyOffset>> futureCall(String tableId, ApiCallContext context) {
46+
TableName tableName =
47+
TableName.of(
48+
requestContext.getInstanceName().getProject(),
49+
requestContext.getInstanceName().getInstance(),
50+
tableId);
51+
52+
SampleRowKeysRequest request =
53+
SampleRowKeysRequest.newBuilder()
54+
.setTableName(tableName.toString())
55+
.setAppProfileId(requestContext.getAppProfileId())
56+
.build();
57+
58+
ApiFuture<List<SampleRowKeysResponse>> rawResponse = inner.futureCall(request, context);
59+
60+
return ApiFutures.transform(
61+
rawResponse,
62+
new ApiFunction<List<SampleRowKeysResponse>, List<KeyOffset>>() {
63+
@Override
64+
public List<KeyOffset> apply(List<SampleRowKeysResponse> rawResponse) {
65+
return convert(rawResponse);
66+
}
67+
});
68+
}
69+
70+
private static List<KeyOffset> convert(List<SampleRowKeysResponse> rawResponse) {
71+
ImmutableList.Builder<KeyOffset> results = ImmutableList.builder();
72+
73+
for (SampleRowKeysResponse element : rawResponse) {
74+
results.add(KeyOffset.create(element.getRowKey(), element.getOffsetBytes()));
75+
}
76+
77+
return results.build();
78+
}
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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.data.v2.stub;
17+
18+
import static com.google.common.truth.Truth.assertThat;
19+
20+
import com.google.api.core.ApiFuture;
21+
import com.google.api.core.SettableApiFuture;
22+
import com.google.api.gax.grpc.GrpcStatusCode;
23+
import com.google.api.gax.rpc.ApiCallContext;
24+
import com.google.api.gax.rpc.NotFoundException;
25+
import com.google.api.gax.rpc.UnaryCallable;
26+
import com.google.bigtable.admin.v2.InstanceName;
27+
import com.google.bigtable.v2.SampleRowKeysRequest;
28+
import com.google.bigtable.v2.SampleRowKeysResponse;
29+
import com.google.cloud.bigtable.data.v2.internal.RequestContext;
30+
import com.google.cloud.bigtable.data.v2.models.KeyOffset;
31+
import com.google.common.collect.ImmutableList;
32+
import com.google.protobuf.ByteString;
33+
import io.grpc.Status.Code;
34+
import java.util.List;
35+
import java.util.concurrent.ExecutionException;
36+
import java.util.concurrent.TimeUnit;
37+
import org.junit.Before;
38+
import org.junit.Test;
39+
import org.junit.runner.RunWith;
40+
import org.junit.runners.JUnit4;
41+
42+
@RunWith(JUnit4.class)
43+
public class SampleRowKeysCallableTest {
44+
private final RequestContext requestContext =
45+
RequestContext.create(InstanceName.of("my-project", "my-instance"), "my-app-profile");
46+
private FakeCallable inner;
47+
private SampleRowKeysCallable callable;
48+
49+
@Before
50+
public void setUp() {
51+
inner = new FakeCallable();
52+
callable = new SampleRowKeysCallable(inner, requestContext);
53+
}
54+
55+
@Test
56+
public void requestIsCorrect() {
57+
callable.futureCall("my-table");
58+
59+
assertThat(inner.request)
60+
.isEqualTo(
61+
SampleRowKeysRequest.newBuilder()
62+
.setTableName(requestContext.getInstanceName() + "/tables/my-table")
63+
.setAppProfileId(requestContext.getAppProfileId())
64+
.build());
65+
}
66+
67+
@Test
68+
public void responseCorrectlyTransformed() throws Exception {
69+
ApiFuture<List<KeyOffset>> result = callable.futureCall("my-table");
70+
71+
inner.response.set(
72+
ImmutableList.of(
73+
SampleRowKeysResponse.newBuilder()
74+
.setRowKey(ByteString.copyFromUtf8("key1"))
75+
.setOffsetBytes(100)
76+
.build(),
77+
SampleRowKeysResponse.newBuilder()
78+
.setRowKey(ByteString.copyFromUtf8(""))
79+
.setOffsetBytes(1000)
80+
.build()));
81+
82+
assertThat(result.get(1, TimeUnit.SECONDS))
83+
.isEqualTo(
84+
ImmutableList.of(
85+
KeyOffset.create(ByteString.copyFromUtf8("key1"), 100),
86+
KeyOffset.create(ByteString.EMPTY, 1000)));
87+
}
88+
89+
@Test
90+
public void errorIsPropagated() throws Exception {
91+
ApiFuture<List<KeyOffset>> result = callable.futureCall("my-table");
92+
93+
Throwable expectedError =
94+
new NotFoundException("fake error", null, GrpcStatusCode.of(Code.NOT_FOUND), false);
95+
inner.response.setException(expectedError);
96+
97+
Throwable actualError = null;
98+
try {
99+
result.get(1, TimeUnit.SECONDS);
100+
} catch (ExecutionException e) {
101+
actualError = e.getCause();
102+
}
103+
104+
assertThat(actualError).isEqualTo(expectedError);
105+
}
106+
107+
static class FakeCallable
108+
extends UnaryCallable<SampleRowKeysRequest, List<SampleRowKeysResponse>> {
109+
SampleRowKeysRequest request;
110+
ApiCallContext callContext;
111+
SettableApiFuture<List<SampleRowKeysResponse>> response = SettableApiFuture.create();
112+
113+
@Override
114+
public ApiFuture<List<SampleRowKeysResponse>> futureCall(
115+
SampleRowKeysRequest request, ApiCallContext context) {
116+
this.request = request;
117+
this.callContext = context;
118+
119+
return response;
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)