|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.hbase.client; |
| 19 | + |
| 20 | +import java.io.Closeable; |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Collection; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Set; |
| 27 | +import java.util.concurrent.CompletableFuture; |
| 28 | +import org.apache.hadoop.conf.Configuration; |
| 29 | +import org.apache.hadoop.hbase.HConstants; |
| 30 | +import org.apache.hadoop.hbase.ServerName; |
| 31 | +import org.apache.hadoop.hbase.ipc.BadAuthException; |
| 32 | +import org.apache.hadoop.hbase.ipc.HBaseRpcController; |
| 33 | +import org.apache.hadoop.hbase.ipc.RpcClient; |
| 34 | +import org.apache.hadoop.hbase.ipc.RpcClientFactory; |
| 35 | +import org.apache.hadoop.hbase.ipc.RpcControllerFactory; |
| 36 | +import org.apache.hadoop.hbase.security.User; |
| 37 | +import org.apache.hadoop.hbase.util.IOExceptionSupplier; |
| 38 | +import org.apache.hadoop.ipc.RemoteException; |
| 39 | +import org.apache.yetus.audience.InterfaceAudience; |
| 40 | +import org.slf4j.Logger; |
| 41 | +import org.slf4j.LoggerFactory; |
| 42 | + |
| 43 | +import org.apache.hbase.thirdparty.com.google.common.base.Preconditions; |
| 44 | +import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableMap; |
| 45 | +import org.apache.hbase.thirdparty.com.google.protobuf.RpcCallback; |
| 46 | +import org.apache.hbase.thirdparty.com.google.protobuf.RpcChannel; |
| 47 | + |
| 48 | +import org.apache.hadoop.hbase.shaded.protobuf.generated.RegistryProtos.ClientMetaService; |
| 49 | +import org.apache.hadoop.hbase.shaded.protobuf.generated.RegistryProtos.ConnectionRegistryService; |
| 50 | +import org.apache.hadoop.hbase.shaded.protobuf.generated.RegistryProtos.GetConnectionRegistryRequest; |
| 51 | +import org.apache.hadoop.hbase.shaded.protobuf.generated.RegistryProtos.GetConnectionRegistryResponse; |
| 52 | + |
| 53 | +/** |
| 54 | + * A class for creating {@link RpcClient} and related stubs used by |
| 55 | + * {@link AbstractRpcBasedConnectionRegistry}. We need to connect to bootstrap nodes to get the |
| 56 | + * cluster id first, before creating the final {@link RpcClient} and related stubs. |
| 57 | + * <p> |
| 58 | + * See HBASE-25051 for more details. |
| 59 | + */ |
| 60 | +@InterfaceAudience.Private |
| 61 | +class ConnectionRegistryRpcStubHolder implements Closeable { |
| 62 | + |
| 63 | + private static final Logger LOG = LoggerFactory.getLogger(ConnectionRegistryRpcStubHolder.class); |
| 64 | + |
| 65 | + private final Configuration conf; |
| 66 | + |
| 67 | + // used for getting cluster id |
| 68 | + private final Configuration noAuthConf; |
| 69 | + |
| 70 | + private final User user; |
| 71 | + |
| 72 | + private final RpcControllerFactory rpcControllerFactory; |
| 73 | + |
| 74 | + private final Set<ServerName> bootstrapNodes; |
| 75 | + |
| 76 | + private final int rpcTimeoutMs; |
| 77 | + |
| 78 | + private volatile ImmutableMap<ServerName, ClientMetaService.Interface> addr2Stub; |
| 79 | + |
| 80 | + private volatile RpcClient rpcClient; |
| 81 | + |
| 82 | + private CompletableFuture<ImmutableMap<ServerName, ClientMetaService.Interface>> addr2StubFuture; |
| 83 | + |
| 84 | + ConnectionRegistryRpcStubHolder(Configuration conf, User user, |
| 85 | + RpcControllerFactory rpcControllerFactory, Set<ServerName> bootstrapNodes) { |
| 86 | + this.conf = conf; |
| 87 | + if (User.isHBaseSecurityEnabled(conf)) { |
| 88 | + this.noAuthConf = new Configuration(conf); |
| 89 | + this.noAuthConf.set(User.HBASE_SECURITY_CONF_KEY, "simple"); |
| 90 | + } else { |
| 91 | + this.noAuthConf = conf; |
| 92 | + } |
| 93 | + this.user = user; |
| 94 | + this.rpcControllerFactory = rpcControllerFactory; |
| 95 | + this.bootstrapNodes = Collections.unmodifiableSet(bootstrapNodes); |
| 96 | + this.rpcTimeoutMs = (int) Math.min(Integer.MAX_VALUE, |
| 97 | + conf.getLong(HConstants.HBASE_RPC_TIMEOUT_KEY, HConstants.DEFAULT_HBASE_RPC_TIMEOUT)); |
| 98 | + } |
| 99 | + |
| 100 | + private ImmutableMap<ServerName, ClientMetaService.Interface> createStubs(RpcClient rpcClient, |
| 101 | + Collection<ServerName> addrs) { |
| 102 | + LOG.debug("Going to use new servers to create stubs: {}", addrs); |
| 103 | + Preconditions.checkNotNull(addrs); |
| 104 | + ImmutableMap.Builder<ServerName, ClientMetaService.Interface> builder = |
| 105 | + ImmutableMap.builderWithExpectedSize(addrs.size()); |
| 106 | + for (ServerName masterAddr : addrs) { |
| 107 | + builder.put(masterAddr, |
| 108 | + ClientMetaService.newStub(rpcClient.createRpcChannel(masterAddr, user, rpcTimeoutMs))); |
| 109 | + } |
| 110 | + return builder.build(); |
| 111 | + } |
| 112 | + |
| 113 | + private boolean isBadAuthException(IOException e) { |
| 114 | + return e instanceof RemoteException |
| 115 | + && BadAuthException.class.getName().equals(((RemoteException) e).getClassName()); |
| 116 | + } |
| 117 | + |
| 118 | + private void createStubsAndComplete(String clusterId, |
| 119 | + CompletableFuture<ImmutableMap<ServerName, ClientMetaService.Interface>> future) { |
| 120 | + RpcClient c = RpcClientFactory.createClient(conf, clusterId); |
| 121 | + ImmutableMap<ServerName, ClientMetaService.Interface> m = createStubs(c, bootstrapNodes); |
| 122 | + rpcClient = c; |
| 123 | + addr2Stub = m; |
| 124 | + future.complete(m); |
| 125 | + } |
| 126 | + |
| 127 | + private void createStubs(List<ServerName> bootstrapServers, int index, |
| 128 | + CompletableFuture<ImmutableMap<ServerName, ClientMetaService.Interface>> future) { |
| 129 | + // use null cluster id here as we do not know the cluster id yet, we will fetch it through this |
| 130 | + // rpc client and use it to create the final rpc client |
| 131 | + RpcClient getConnectionRegistryRpcClient = RpcClientFactory.createClient(noAuthConf, null); |
| 132 | + // user and rpcTimeout are both not important here, as we will not actually send any rpc calls |
| 133 | + // out, only a preamble connection header, but if we pass null as user, there will be NPE in |
| 134 | + // some code paths... |
| 135 | + RpcChannel channel = |
| 136 | + getConnectionRegistryRpcClient.createRpcChannel(bootstrapServers.get(index), user, 0); |
| 137 | + ConnectionRegistryService.Interface stub = ConnectionRegistryService.newStub(channel); |
| 138 | + HBaseRpcController controller = rpcControllerFactory.newController(); |
| 139 | + stub.getConnectionRegistry(controller, GetConnectionRegistryRequest.getDefaultInstance(), |
| 140 | + new RpcCallback<GetConnectionRegistryResponse>() { |
| 141 | + |
| 142 | + @Override |
| 143 | + public void run(GetConnectionRegistryResponse resp) { |
| 144 | + synchronized (ConnectionRegistryRpcStubHolder.this) { |
| 145 | + addr2StubFuture = null; |
| 146 | + if (controller.failed()) { |
| 147 | + if (isBadAuthException(controller.getFailed())) { |
| 148 | + // this means we have connected to an old server where it does not support passing |
| 149 | + // cluster id through preamble connnection header, so we fallback to use null |
| 150 | + // cluster id, which is the old behavior |
| 151 | + LOG.debug("Failed to get connection registry info, should be an old server," |
| 152 | + + " fallback to use null cluster id", controller.getFailed()); |
| 153 | + createStubsAndComplete(null, future); |
| 154 | + } else { |
| 155 | + LOG.debug("Failed to get connection registry info", controller.getFailed()); |
| 156 | + if (index == bootstrapServers.size() - 1) { |
| 157 | + future.completeExceptionally(controller.getFailed()); |
| 158 | + } else { |
| 159 | + // try next bootstrap server |
| 160 | + createStubs(bootstrapServers, index + 1, future); |
| 161 | + } |
| 162 | + } |
| 163 | + } else { |
| 164 | + LOG.debug("Got connection registry info: {}", resp); |
| 165 | + String clusterId = resp.getClusterId(); |
| 166 | + createStubsAndComplete(clusterId, future); |
| 167 | + } |
| 168 | + } |
| 169 | + getConnectionRegistryRpcClient.close(); |
| 170 | + } |
| 171 | + }); |
| 172 | + } |
| 173 | + |
| 174 | + private CompletableFuture<ImmutableMap<ServerName, ClientMetaService.Interface>> createStubs() { |
| 175 | + CompletableFuture<ImmutableMap<ServerName, ClientMetaService.Interface>> future = |
| 176 | + new CompletableFuture<>(); |
| 177 | + List<ServerName> bootstrapServers = new ArrayList<ServerName>(bootstrapNodes); |
| 178 | + Collections.shuffle(bootstrapServers); |
| 179 | + createStubs(bootstrapServers, 0, future); |
| 180 | + return future; |
| 181 | + } |
| 182 | + |
| 183 | + CompletableFuture<ImmutableMap<ServerName, ClientMetaService.Interface>> getStubs() { |
| 184 | + ImmutableMap<ServerName, ClientMetaService.Interface> s = this.addr2Stub; |
| 185 | + if (s != null) { |
| 186 | + return CompletableFuture.completedFuture(s); |
| 187 | + } |
| 188 | + synchronized (this) { |
| 189 | + s = this.addr2Stub; |
| 190 | + if (s != null) { |
| 191 | + return CompletableFuture.completedFuture(s); |
| 192 | + } |
| 193 | + if (addr2StubFuture != null) { |
| 194 | + return addr2StubFuture; |
| 195 | + } |
| 196 | + addr2StubFuture = createStubs(); |
| 197 | + return addr2StubFuture; |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + void refreshStubs(IOExceptionSupplier<Collection<ServerName>> fetchEndpoints) throws IOException { |
| 202 | + // There is no actual call yet so we have not initialize the rpc client and related stubs yet, |
| 203 | + // give up refreshing |
| 204 | + if (addr2Stub == null) { |
| 205 | + LOG.debug("Skip refreshing stubs as we have not initialized rpc client yet"); |
| 206 | + return; |
| 207 | + } |
| 208 | + LOG.debug("Going to refresh stubs"); |
| 209 | + assert rpcClient != null; |
| 210 | + addr2Stub = createStubs(rpcClient, fetchEndpoints.get()); |
| 211 | + } |
| 212 | + |
| 213 | + @Override |
| 214 | + public void close() { |
| 215 | + if (rpcClient != null) { |
| 216 | + rpcClient.close(); |
| 217 | + } |
| 218 | + } |
| 219 | +} |
0 commit comments