001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.ipc; 019 020import java.io.IOException; 021import java.net.InetAddress; 022import java.net.UnknownHostException; 023import java.util.concurrent.TimeUnit; 024 025import org.apache.hadoop.conf.Configuration; 026import org.apache.hadoop.hbase.HConstants; 027import org.apache.hadoop.hbase.codec.Codec; 028import org.apache.hadoop.hbase.security.SecurityInfo; 029import org.apache.hadoop.hbase.security.User; 030import org.apache.hadoop.hbase.security.provider.SaslClientAuthenticationProvider; 031import org.apache.hadoop.hbase.security.provider.SaslClientAuthenticationProviders; 032import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 033import org.apache.hadoop.hbase.util.Pair; 034import org.apache.hadoop.io.compress.CompressionCodec; 035import org.apache.hadoop.security.token.Token; 036import org.apache.hadoop.security.token.TokenIdentifier; 037import org.apache.yetus.audience.InterfaceAudience; 038import org.slf4j.Logger; 039import org.slf4j.LoggerFactory; 040 041import org.apache.hbase.thirdparty.io.netty.util.HashedWheelTimer; 042import org.apache.hbase.thirdparty.io.netty.util.Timeout; 043import org.apache.hbase.thirdparty.io.netty.util.TimerTask; 044import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; 045import org.apache.hadoop.hbase.shaded.protobuf.generated.RPCProtos.ConnectionHeader; 046import org.apache.hadoop.hbase.shaded.protobuf.generated.RPCProtos.UserInformation; 047 048/** 049 * Base class for ipc connection. 050 */ 051@InterfaceAudience.Private 052abstract class RpcConnection { 053 054 private static final Logger LOG = LoggerFactory.getLogger(RpcConnection.class); 055 056 protected final ConnectionId remoteId; 057 058 protected final boolean useSasl; 059 060 protected final Token<? extends TokenIdentifier> token; 061 062 protected final InetAddress serverAddress; 063 064 protected final SecurityInfo securityInfo; 065 066 protected final int reloginMaxBackoff; // max pause before relogin on sasl failure 067 068 protected final Codec codec; 069 070 protected final CompressionCodec compressor; 071 072 protected final HashedWheelTimer timeoutTimer; 073 074 protected final Configuration conf; 075 076 protected static String CRYPTO_AES_ENABLED_KEY = "hbase.rpc.crypto.encryption.aes.enabled"; 077 078 protected static boolean CRYPTO_AES_ENABLED_DEFAULT = false; 079 080 // the last time we were picked up from connection pool. 081 protected long lastTouched; 082 083 protected SaslClientAuthenticationProvider provider; 084 085 protected RpcConnection(Configuration conf, HashedWheelTimer timeoutTimer, ConnectionId remoteId, 086 String clusterId, boolean isSecurityEnabled, Codec codec, CompressionCodec compressor) 087 throws IOException { 088 if (remoteId.getAddress().isUnresolved()) { 089 throw new UnknownHostException("unknown host: " + remoteId.getAddress().getHostName()); 090 } 091 this.serverAddress = remoteId.getAddress().getAddress(); 092 this.timeoutTimer = timeoutTimer; 093 this.codec = codec; 094 this.compressor = compressor; 095 this.conf = conf; 096 097 User ticket = remoteId.getTicket(); 098 this.securityInfo = SecurityInfo.getInfo(remoteId.getServiceName()); 099 this.useSasl = isSecurityEnabled; 100 101 // Choose the correct Token and AuthenticationProvider for this client to use 102 SaslClientAuthenticationProviders providers = 103 SaslClientAuthenticationProviders.getInstance(conf); 104 Pair<SaslClientAuthenticationProvider, Token<? extends TokenIdentifier>> pair; 105 if (useSasl && securityInfo != null) { 106 pair = providers.selectProvider(clusterId, ticket); 107 if (pair == null) { 108 if (LOG.isTraceEnabled()) { 109 LOG.trace("Found no valid authentication method from providers={} with tokens={}", 110 providers.toString(), ticket.getTokens()); 111 } 112 throw new RuntimeException("Found no valid authentication method from options"); 113 } 114 } else if (!useSasl) { 115 // Hack, while SIMPLE doesn't go via SASL. 116 pair = providers.getSimpleProvider(); 117 } else { 118 throw new RuntimeException("Could not compute valid client authentication provider"); 119 } 120 121 this.provider = pair.getFirst(); 122 this.token = pair.getSecond(); 123 124 LOG.debug("Using {} authentication for service={}, sasl={}", 125 provider.getSaslAuthMethod().getName(), remoteId.serviceName, useSasl); 126 reloginMaxBackoff = conf.getInt("hbase.security.relogin.maxbackoff", 5000); 127 this.remoteId = remoteId; 128 } 129 130 protected void scheduleTimeoutTask(final Call call) { 131 if (call.timeout > 0) { 132 call.timeoutTask = timeoutTimer.newTimeout(new TimerTask() { 133 134 @Override 135 public void run(Timeout timeout) throws Exception { 136 call.setTimeout(new CallTimeoutException(call.toShortString() + ", waitTime=" 137 + (EnvironmentEdgeManager.currentTime() - call.getStartTime()) + ", rpcTimeout=" 138 + call.timeout)); 139 callTimeout(call); 140 } 141 }, call.timeout, TimeUnit.MILLISECONDS); 142 } 143 } 144 145 protected byte[] getConnectionHeaderPreamble() { 146 // Assemble the preamble up in a buffer first and then send it. Writing individual elements, 147 // they are getting sent across piecemeal according to wireshark and then server is messing 148 // up the reading on occasion (the passed in stream is not buffered yet). 149 150 // Preamble is six bytes -- 'HBas' + VERSION + AUTH_CODE 151 int rpcHeaderLen = HConstants.RPC_HEADER.length; 152 byte[] preamble = new byte[rpcHeaderLen + 2]; 153 System.arraycopy(HConstants.RPC_HEADER, 0, preamble, 0, rpcHeaderLen); 154 preamble[rpcHeaderLen] = HConstants.RPC_CURRENT_VERSION; 155 synchronized (this) { 156 preamble[rpcHeaderLen + 1] = provider.getSaslAuthMethod().getCode(); 157 } 158 return preamble; 159 } 160 161 protected ConnectionHeader getConnectionHeader() { 162 final ConnectionHeader.Builder builder = ConnectionHeader.newBuilder(); 163 builder.setServiceName(remoteId.getServiceName()); 164 final UserInformation userInfoPB = provider.getUserInfo(remoteId.ticket); 165 if (userInfoPB != null) { 166 builder.setUserInfo(userInfoPB); 167 } 168 if (this.codec != null) { 169 builder.setCellBlockCodecClass(this.codec.getClass().getCanonicalName()); 170 } 171 if (this.compressor != null) { 172 builder.setCellBlockCompressorClass(this.compressor.getClass().getCanonicalName()); 173 } 174 builder.setVersionInfo(ProtobufUtil.getVersionInfo()); 175 boolean isCryptoAESEnable = conf.getBoolean(CRYPTO_AES_ENABLED_KEY, CRYPTO_AES_ENABLED_DEFAULT); 176 // if Crypto AES enable, setup Cipher transformation 177 if (isCryptoAESEnable) { 178 builder.setRpcCryptoCipherTransformation( 179 conf.get("hbase.rpc.crypto.encryption.aes.cipher.transform", "AES/CTR/NoPadding")); 180 } 181 return builder.build(); 182 } 183 184 protected abstract void callTimeout(Call call); 185 186 public ConnectionId remoteId() { 187 return remoteId; 188 } 189 190 public long getLastTouched() { 191 return lastTouched; 192 } 193 194 public void setLastTouched(long lastTouched) { 195 this.lastTouched = lastTouched; 196 } 197 198 /** 199 * Tell the idle connection sweeper whether we could be swept. 200 */ 201 public abstract boolean isActive(); 202 203 /** 204 * Just close connection. Do not need to remove from connection pool. 205 */ 206 public abstract void shutdown(); 207 208 public abstract void sendRequest(Call call, HBaseRpcController hrc) throws IOException; 209 210 /** 211 * Does the clean up work after the connection is removed from the connection pool 212 */ 213 public abstract void cleanupConnection(); 214}