|
| 1 | +/* |
| 2 | + * Copyright 2019 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.misc_utilities; |
| 17 | + |
| 18 | +import com.google.bigtable.v2.BigtableGrpc; |
| 19 | +import com.google.bigtable.v2.ReadRowsRequest; |
| 20 | +import com.google.bigtable.v2.ReadRowsResponse; |
| 21 | +import com.google.common.collect.ImmutableList; |
| 22 | +import com.google.common.collect.ImmutableMap; |
| 23 | +import com.google.common.collect.Lists; |
| 24 | +import io.grpc.ManagedChannel; |
| 25 | +import io.grpc.alts.ComputeEngineChannelBuilder; |
| 26 | +import java.util.Iterator; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.logging.ConsoleHandler; |
| 29 | +import java.util.logging.Level; |
| 30 | +import java.util.logging.Logger; |
| 31 | + |
| 32 | +/** |
| 33 | + * Simple utility to debug connection issues. |
| 34 | + * |
| 35 | + * <p>It enables verbose gRPC logging and does a simple read using a GCE channel builder. |
| 36 | + * |
| 37 | + * <pre>{@code |
| 38 | + * mvn install |
| 39 | + * |
| 40 | + * mvn test-compile exec:java \ |
| 41 | + * -pl google-cloud-bigtable \ |
| 42 | + * -Dexec.mainClass=com.google.cloud.bigtable.misc_utilities.SimpleGceRawRead \ |
| 43 | + * -Dexec.classpathScope=test \ |
| 44 | + * -Dbigtable.project=gcloud-devel \ |
| 45 | + * -Dbigtable.instance=google-cloud-bigtable \ |
| 46 | + * -Dbigtable.table=integration-tests \ |
| 47 | + * -Dbigtable.data-endpoint="bigtable.googleapis.com" |
| 48 | + * }</pre> |
| 49 | + */ |
| 50 | +public class SimpleGceRawRead { |
| 51 | + |
| 52 | + public static void main(String[] args) { |
| 53 | + configureLogging(); |
| 54 | + |
| 55 | + String projectId = System.getProperty("bigtable.project", "gcloud-devel"); |
| 56 | + String instanceId = System.getProperty("bigtable.instance", "google-cloud-bigtable"); |
| 57 | + String tableId = System.getProperty("bigtable.table", "integration-tests"); |
| 58 | + |
| 59 | + String endpoint = System.getProperty("bigtable.data-endpoint", "bigtable.googleapis.com"); |
| 60 | + String tableName = |
| 61 | + String.format("projects/%s/instances/%s/tables/%s", projectId, instanceId, tableId); |
| 62 | + |
| 63 | + System.out.printf(">>>>>>>>> Trying to connect to: %s, to read %s%n%n%n", endpoint, tableName); |
| 64 | + |
| 65 | + ManagedChannel channel = |
| 66 | + ComputeEngineChannelBuilder.forAddress(endpoint, 443) |
| 67 | + .disableServiceConfigLookUp() |
| 68 | + .defaultServiceConfig(newServiceConfig()) |
| 69 | + .build(); |
| 70 | + try { |
| 71 | + BigtableGrpc.BigtableBlockingStub stub = BigtableGrpc.newBlockingStub(channel); |
| 72 | + |
| 73 | + Iterator<ReadRowsResponse> iter = |
| 74 | + stub.readRows( |
| 75 | + ReadRowsRequest.newBuilder().setTableName(tableName).setRowsLimit(1).build()); |
| 76 | + System.out.printf("%n%n>>>>>>>>> Success Rows Read: %d%n%n", Lists.newArrayList(iter).size()); |
| 77 | + } finally { |
| 78 | + channel.shutdown(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** Enable verbose gRPC logging */ |
| 83 | + private static void configureLogging() { |
| 84 | + Logger.getLogger("io.grpc").setLevel(Level.ALL); |
| 85 | + Logger.getLogger("io.grpc.netty.shaded").setLevel(Level.ALL); |
| 86 | + |
| 87 | + ConsoleHandler stderr = new ConsoleHandler(); |
| 88 | + stderr.setLevel(Level.ALL); |
| 89 | + |
| 90 | + Logger.getLogger("").addHandler(stderr); |
| 91 | + } |
| 92 | + |
| 93 | + private static Map<String, Object> newServiceConfig() { |
| 94 | + ImmutableMap<String, Object> pickFirstStrategy = |
| 95 | + ImmutableMap.<String, Object>of("pick_first", ImmutableMap.of()); |
| 96 | + ImmutableMap<String, Object> childPolicy = |
| 97 | + ImmutableMap.<String, Object>of("childPolicy", ImmutableList.of(pickFirstStrategy)); |
| 98 | + ImmutableMap<String, Object> grpcLbPolicy = |
| 99 | + ImmutableMap.<String, Object>of("grpclb", childPolicy); |
| 100 | + return ImmutableMap.<String, Object>of("loadBalancingConfig", ImmutableList.of(grpcLbPolicy)); |
| 101 | + } |
| 102 | +} |
0 commit comments