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  
23  import org.apache.commons.logging.Log;
24  import org.apache.hadoop.classification.InterfaceAudience;
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.protobuf.generated.AdminProtos.AdminService;
29  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ClientService;
30  
31  /**
32   * Utility used by client connections.
33   */
34  @InterfaceAudience.Private
35  public class ConnectionUtils {
36  
37    private static final Random RANDOM = new Random();
38    /**
39     * Calculate pause time.
40     * Built on {@link HConstants#RETRY_BACKOFF}.
41     * @param pause
42     * @param tries
43     * @return How long to wait after <code>tries</code> retries
44     */
45    public static long getPauseTime(final long pause, final int tries) {
46      int ntries = tries;
47      if (ntries >= HConstants.RETRY_BACKOFF.length) {
48        ntries = HConstants.RETRY_BACKOFF.length - 1;
49      }
50  
51      long normalPause = pause * HConstants.RETRY_BACKOFF[ntries];
52      long jitter =  (long)(normalPause * RANDOM.nextFloat() * 0.01f); // 1% possible jitter
53      return normalPause + jitter;
54    }
55  
56  
57    /**
58     * Adds / subs a 10% jitter to a pause time. Minimum is 1.
59     * @param pause the expected pause.
60     * @param jitter the jitter ratio, between 0 and 1, exclusive.
61     */
62    public static long addJitter(final long pause, final float jitter) {
63      float lag = pause * (RANDOM.nextFloat() - 0.5f) * jitter;
64      long newPause = pause + (long) lag;
65      if (newPause <= 0) {
66        return 1;
67      }
68      return newPause;
69    }
70    
71    /**
72     * @param conn The connection for which to replace the generator.
73     * @param cnm Replaces the nonce generator used, for testing.
74     * @return old nonce generator.
75     */
76    public static NonceGenerator injectNonceGeneratorForTesting(
77        HConnection conn, NonceGenerator cnm) {
78      return ConnectionManager.injectNonceGeneratorForTesting(conn, cnm);
79    }
80  
81    /**
82     * Changes the configuration to set the number of retries needed when using HConnection
83     * internally, e.g. for  updating catalog tables, etc.
84     * Call this method before we create any Connections.
85     * @param c The Configuration instance to set the retries into.
86     * @param log Used to log what we set in here.
87     */
88    public static void setServerSideHConnectionRetriesConfig(
89        final Configuration c, final String sn, final Log log) {
90      int hcRetries = c.getInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER,
91        HConstants.DEFAULT_HBASE_CLIENT_RETRIES_NUMBER);
92      // Go big.  Multiply by 10.  If we can't get to meta after this many retries
93      // then something seriously wrong.
94      int serversideMultiplier = c.getInt("hbase.client.serverside.retries.multiplier", 10);
95      int retries = hcRetries * serversideMultiplier;
96      c.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, retries);
97      log.info(sn + " server-side HConnection retries=" + retries);
98    }
99  
100   /**
101    * Adapt a HConnection so that it can bypass the RPC layer (serialization,
102    * deserialization, networking, etc..) when it talks to a local server.
103    * @param conn the connection to adapt
104    * @param serverName the local server name
105    * @param admin the admin interface of the local server
106    * @param client the client interface of the local server
107    * @return an adapted/decorated HConnection
108    */
109   public static HConnection createShortCircuitHConnection(final HConnection conn,
110       final ServerName serverName, final AdminService.BlockingInterface admin,
111       final ClientService.BlockingInterface client) {
112     return new ConnectionAdapter(conn) {
113       @Override
114       public AdminService.BlockingInterface getAdmin(
115           ServerName sn, boolean getMaster) throws IOException {
116         return serverName.equals(sn) ? admin : super.getAdmin(sn, getMaster);
117       }
118 
119       @Override
120       public ClientService.BlockingInterface getClient(
121           ServerName sn) throws IOException {
122         return serverName.equals(sn) ? client : super.getClient(sn);
123       }
124     };
125   }
126 }