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