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