View Javadoc

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.IOException;
21  import java.util.Random;
22  import java.util.concurrent.ExecutorService;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.HConstants;
27  import org.apache.hadoop.hbase.RegionLocations;
28  import org.apache.hadoop.hbase.ServerName;
29  import org.apache.hadoop.hbase.TableName;
30  import org.apache.hadoop.hbase.classification.InterfaceAudience;
31  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.AdminService;
32  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ClientService;
33  import org.apache.hadoop.hbase.security.User;
34  import org.apache.hadoop.hbase.security.UserProvider;
35  
36  import com.google.common.annotations.VisibleForTesting;
37  
38  /**
39   * Utility used by client connections.
40   */
41  @InterfaceAudience.Private
42  public class ConnectionUtils {
43  
44    private static final Random RANDOM = new Random();
45    /**
46     * Calculate pause time.
47     * Built on {@link HConstants#RETRY_BACKOFF}.
48     * @param pause
49     * @param tries
50     * @return How long to wait after <code>tries</code> retries
51     */
52    public static long getPauseTime(final long pause, final int tries) {
53      int ntries = tries;
54      if (ntries >= HConstants.RETRY_BACKOFF.length) {
55        ntries = HConstants.RETRY_BACKOFF.length - 1;
56      }
57      if (ntries < 0) {
58        ntries = 0;
59      }
60  
61      long normalPause = pause * HConstants.RETRY_BACKOFF[ntries];
62      long jitter =  (long)(normalPause * RANDOM.nextFloat() * 0.01f); // 1% possible jitter
63      return normalPause + jitter;
64    }
65  
66  
67    /**
68     * @param conn The connection for which to replace the generator.
69     * @param cnm Replaces the nonce generator used, for testing.
70     * @return old nonce generator.
71     */
72    public static NonceGenerator injectNonceGeneratorForTesting(
73        ClusterConnection conn, NonceGenerator cnm) {
74      return ConnectionManager.injectNonceGeneratorForTesting(conn, cnm);
75    }
76  
77    /**
78     * Changes the configuration to set the number of retries needed when using HConnection
79     * internally, e.g. for  updating catalog tables, etc.
80     * Call this method before we create any Connections.
81     * @param c The Configuration instance to set the retries into.
82     * @param log Used to log what we set in here.
83     */
84    public static void setServerSideHConnectionRetriesConfig(
85        final Configuration c, final String sn, final Log log) {
86      // TODO: Fix this. Not all connections from server side should have 10 times the retries.
87      int hcRetries = c.getInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER,
88        HConstants.DEFAULT_HBASE_CLIENT_RETRIES_NUMBER);
89      // Go big.  Multiply by 10.  If we can't get to meta after this many retries
90      // then something seriously wrong.
91      int serversideMultiplier = c.getInt("hbase.client.serverside.retries.multiplier", 10);
92      int retries = hcRetries * serversideMultiplier;
93      c.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, retries);
94      log.info(sn + " server-side HConnection retries=" + retries);
95    }
96  
97    /**
98     * Adapt a HConnection so that it can bypass the RPC layer (serialization,
99     * deserialization, networking, etc..) -- i.e. short-circuit -- when talking to a local server.
100    * @param conn the connection to adapt
101    * @param serverName the local server name
102    * @param admin the admin interface of the local server
103    * @param client the client interface of the local server
104    * @return an adapted/decorated HConnection
105    */
106   @Deprecated
107   public static ClusterConnection createShortCircuitHConnection(final Connection conn,
108       final ServerName serverName, final AdminService.BlockingInterface admin,
109       final ClientService.BlockingInterface client) {
110     return new ConnectionAdapter(conn) {
111       @Override
112       public AdminService.BlockingInterface getAdmin(
113           ServerName sn, boolean getMaster) throws IOException {
114         return serverName.equals(sn) ? admin : super.getAdmin(sn, getMaster);
115       }
116 
117       @Override
118       public ClientService.BlockingInterface getClient(
119           ServerName sn) throws IOException {
120         return serverName.equals(sn) ? client : super.getClient(sn);
121       }
122     };
123   }
124 
125   /**
126    * Creates a short-circuit connection that can bypass the RPC layer (serialization,
127    * deserialization, networking, etc..) when talking to a local server.
128    * @param conf the current configuration
129    * @param pool the thread pool to use for batch operations
130    * @param user the user the connection is for
131    * @param serverName the local server name
132    * @param admin the admin interface of the local server
133    * @param client the client interface of the local server
134    * @return a short-circuit connection.
135    * @throws IOException
136    */
137   public static ClusterConnection createShortCircuitConnection(final Configuration conf,
138     ExecutorService pool, User user, final ServerName serverName,
139     final AdminService.BlockingInterface admin, final ClientService.BlockingInterface client)
140     throws IOException {
141     if (user == null) {
142       user = UserProvider.instantiate(conf).getCurrent();
143     }
144     return new ConnectionManager.HConnectionImplementation(conf, false, pool, user) {
145       @Override
146       public AdminService.BlockingInterface getAdmin(ServerName sn, boolean getMaster)
147         throws IOException {
148         return serverName.equals(sn) ? admin : super.getAdmin(sn, getMaster);
149       }
150 
151       @Override
152       public ClientService.BlockingInterface getClient(ServerName sn) throws IOException {
153         return serverName.equals(sn) ? client : super.getClient(sn);
154       }
155     };
156   }
157 
158   /**
159    * Setup the connection class, so that it will not depend on master being online. Used for testing
160    * @param conf configuration to set
161    */
162   @VisibleForTesting
163   public static void setupMasterlessConnection(Configuration conf) {
164     conf.set(HConnection.HBASE_CLIENT_CONNECTION_IMPL,
165       MasterlessConnection.class.getName());
166   }
167 
168   /**
169    * Some tests shut down the master. But table availability is a master RPC which is performed on
170    * region re-lookups.
171    */
172   static class MasterlessConnection extends ConnectionManager.HConnectionImplementation {
173     MasterlessConnection(Configuration conf, boolean managed,
174       ExecutorService pool, User user) throws IOException {
175       super(conf, managed, pool, user);
176     }
177 
178     @Override
179     public boolean isTableDisabled(TableName tableName) throws IOException {
180       // treat all tables as enabled
181       return false;
182     }
183   }
184 }