Skip to content

Commit a2dbe80

Browse files
authored
chore: adding BigtableVeneerApi to expose veneer implementation (googleapis#2492)
* chore: adding BigtableVeneerApi to expose veneer implementation This PR adds BigtableVeneerApi, which exposes veneer implementation of Data and Admin clients. * chore: removed null check for server in unit test
1 parent e4cd4ef commit a2dbe80

File tree

3 files changed

+210
-1
lines changed

3 files changed

+210
-1
lines changed

bigtable-client-core-parent/bigtable-hbase/src/main/java/com/google/cloud/bigtable/hbase/wrappers/BigtableApi.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.cloud.bigtable.hbase.wrappers.classic.BigtableClassicApi;
2020
import com.google.cloud.bigtable.hbase.wrappers.classic.BigtableHBaseClassicSettings;
2121
import com.google.cloud.bigtable.hbase.wrappers.veneer.BigtableHBaseVeneerSettings;
22+
import com.google.cloud.bigtable.hbase.wrappers.veneer.BigtableVeneerApi;
2223
import java.io.IOException;
2324

2425
/**
@@ -33,7 +34,7 @@ public abstract class BigtableApi implements AutoCloseable {
3334

3435
public static BigtableApi create(BigtableHBaseSettings settings) throws IOException {
3536
if (settings instanceof BigtableHBaseVeneerSettings) {
36-
throw new UnsupportedOperationException("Veneer client is not yet supported.");
37+
return new BigtableVeneerApi((BigtableHBaseVeneerSettings) settings);
3738
} else {
3839
return new BigtableClassicApi((BigtableHBaseClassicSettings) settings);
3940
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2020 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+
* http://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.hbase.wrappers.veneer;
17+
18+
import com.google.api.core.InternalApi;
19+
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
20+
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
21+
import com.google.cloud.bigtable.hbase.wrappers.AdminClientWrapper;
22+
import com.google.cloud.bigtable.hbase.wrappers.BigtableApi;
23+
import com.google.cloud.bigtable.hbase.wrappers.DataClientWrapper;
24+
import java.io.IOException;
25+
26+
/** For internal use only - public for technical reasons. */
27+
@InternalApi("For internal usage only")
28+
public class BigtableVeneerApi extends BigtableApi {
29+
30+
private final DataClientWrapper dataClientWrapper;
31+
private final AdminClientWrapper adminClientWrapper;
32+
33+
public BigtableVeneerApi(BigtableHBaseVeneerSettings settings) throws IOException {
34+
super(settings);
35+
dataClientWrapper =
36+
new DataClientVeneerApi(BigtableDataClient.create(settings.getDataSettings()));
37+
adminClientWrapper =
38+
new AdminClientVeneerApi(BigtableTableAdminClient.create(settings.getTableAdminSettings()));
39+
}
40+
41+
@Override
42+
public AdminClientWrapper getAdminClient() {
43+
return adminClientWrapper;
44+
}
45+
46+
@Override
47+
public DataClientWrapper getDataClient() {
48+
return dataClientWrapper;
49+
}
50+
51+
@Override
52+
public void close() throws IOException {
53+
dataClientWrapper.close();
54+
adminClientWrapper.close();
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*
2+
* Copyright 2020 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+
* http://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.hbase.wrappers.veneer;
17+
18+
import static com.google.cloud.bigtable.admin.v2.internal.NameUtil.extractTableIdFromTableName;
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertSame;
21+
import static org.junit.Assert.assertTrue;
22+
23+
import com.google.bigtable.admin.v2.BigtableTableAdminGrpc;
24+
import com.google.bigtable.admin.v2.DeleteTableRequest;
25+
import com.google.bigtable.v2.BigtableGrpc;
26+
import com.google.bigtable.v2.ReadRowsRequest;
27+
import com.google.bigtable.v2.ReadRowsResponse;
28+
import com.google.cloud.bigtable.data.v2.models.Query;
29+
import com.google.cloud.bigtable.hbase.BigtableOptionsFactory;
30+
import com.google.cloud.bigtable.hbase.wrappers.BigtableApi;
31+
import com.google.cloud.bigtable.hbase.wrappers.BigtableHBaseSettings;
32+
import com.google.cloud.bigtable.hbase.wrappers.classic.BigtableHBaseClassicSettings;
33+
import com.google.common.collect.Queues;
34+
import com.google.protobuf.Empty;
35+
import io.grpc.Server;
36+
import io.grpc.ServerBuilder;
37+
import io.grpc.stub.StreamObserver;
38+
import java.io.IOException;
39+
import java.net.ServerSocket;
40+
import java.util.concurrent.BlockingQueue;
41+
import java.util.concurrent.TimeUnit;
42+
import org.apache.hadoop.conf.Configuration;
43+
import org.junit.After;
44+
import org.junit.Before;
45+
import org.junit.Test;
46+
import org.junit.runner.RunWith;
47+
import org.junit.runners.JUnit4;
48+
49+
@RunWith(JUnit4.class)
50+
public class TestBigtableVeneerApi {
51+
52+
private static final String TEST_PROJECT_ID = "fake-project-id";
53+
private static final String TEST_INSTANCE_ID = "fake-instance-id";
54+
private static final String ROW_KEY = "test-row-key";
55+
private static final String TABLE_ID = "test-table-id";
56+
private Server server;
57+
private FakeDataService fakeDataService = new FakeDataService();
58+
private FakeAdminService fakeAdminService = new FakeAdminService();
59+
60+
private BigtableHBaseSettings bigtableHBaseSettings;
61+
private BigtableApi bigtableApi;
62+
63+
@Before
64+
public void setUp() throws IOException {
65+
final int port;
66+
try (ServerSocket s = new ServerSocket(0)) {
67+
port = s.getLocalPort();
68+
}
69+
server =
70+
ServerBuilder.forPort(port)
71+
.addService(fakeDataService)
72+
.addService(fakeAdminService)
73+
.build();
74+
server.start();
75+
76+
Configuration configuration = new Configuration(false);
77+
configuration.set(BigtableOptionsFactory.PROJECT_ID_KEY, TEST_PROJECT_ID);
78+
configuration.set(BigtableOptionsFactory.INSTANCE_ID_KEY, TEST_INSTANCE_ID);
79+
configuration.set(BigtableOptionsFactory.BIGTABLE_NULL_CREDENTIAL_ENABLE_KEY, "true");
80+
configuration.set(BigtableOptionsFactory.BIGTABLE_DATA_CHANNEL_COUNT_KEY, "1");
81+
configuration.set(BigtableOptionsFactory.BIGTABLE_EMULATOR_HOST_KEY, "localhost:" + port);
82+
configuration.set(BigtableOptionsFactory.BIGTABLE_USE_GCJ_CLIENT, "true");
83+
bigtableHBaseSettings = BigtableHBaseClassicSettings.create(configuration);
84+
bigtableApi = BigtableApi.create(bigtableHBaseSettings);
85+
}
86+
87+
@After
88+
public void tearDown() throws Exception {
89+
bigtableApi.close();
90+
server.shutdownNow();
91+
server.awaitTermination();
92+
}
93+
94+
@Test
95+
public void testBigtableHBaseSettings() {
96+
assertTrue(bigtableApi instanceof BigtableVeneerApi);
97+
assertSame(bigtableHBaseSettings, bigtableApi.getBigtableHBaseSettings());
98+
}
99+
100+
@Test
101+
public void testGetAdminClient() throws Exception {
102+
assertTrue(bigtableApi.getAdminClient() instanceof AdminClientVeneerApi);
103+
104+
bigtableApi.getAdminClient().deleteTableAsync(TABLE_ID).get();
105+
DeleteTableRequest deleteTableRequest = fakeAdminService.popLastRequest();
106+
assertEquals(TABLE_ID, extractTableIdFromTableName(deleteTableRequest.getName()));
107+
}
108+
109+
@Test
110+
public void testGetDataClient() throws Exception {
111+
assertTrue(bigtableApi.getDataClient() instanceof DataClientVeneerApi);
112+
113+
Query query = Query.create(TABLE_ID).rowKey(ROW_KEY);
114+
bigtableApi.getDataClient().readRowsAsync(query);
115+
ReadRowsRequest request = fakeDataService.popLastRequest();
116+
assertEquals(ROW_KEY, request.getRows().getRowKeys(0).toStringUtf8());
117+
}
118+
119+
private static class FakeDataService extends BigtableGrpc.BigtableImplBase {
120+
final BlockingQueue<Object> requests = Queues.newLinkedBlockingDeque();
121+
122+
@SuppressWarnings("unchecked")
123+
<T> T popLastRequest() throws InterruptedException {
124+
return (T) requests.poll(1, TimeUnit.SECONDS);
125+
}
126+
127+
@Override
128+
public void readRows(
129+
ReadRowsRequest request, StreamObserver<ReadRowsResponse> responseObserver) {
130+
requests.add(request);
131+
responseObserver.onNext(ReadRowsResponse.getDefaultInstance());
132+
responseObserver.onCompleted();
133+
}
134+
}
135+
136+
private static class FakeAdminService extends BigtableTableAdminGrpc.BigtableTableAdminImplBase {
137+
138+
final BlockingQueue<Object> requests = Queues.newLinkedBlockingDeque();
139+
140+
@SuppressWarnings("unchecked")
141+
<T> T popLastRequest() throws InterruptedException {
142+
return (T) requests.poll(1, TimeUnit.SECONDS);
143+
}
144+
145+
@Override
146+
public void deleteTable(DeleteTableRequest request, StreamObserver<Empty> responseObserver) {
147+
requests.add(request);
148+
responseObserver.onNext(Empty.getDefaultInstance());
149+
responseObserver.onCompleted();
150+
}
151+
}
152+
}

0 commit comments

Comments
 (0)