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.util.Random;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.hadoop.classification.InterfaceAudience;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.HConstants;
26  
27  /**
28   * Utility used by client connections.
29   */
30  @InterfaceAudience.Private
31  public class ConnectionUtils {
32  
33    private static final Random RANDOM = new Random();
34    /**
35     * Calculate pause time.
36     * Built on {@link HConstants#RETRY_BACKOFF}.
37     * @param pause
38     * @param tries
39     * @return How long to wait after <code>tries</code> retries
40     */
41    public static long getPauseTime(final long pause, final int tries) {
42      int ntries = tries;
43      if (ntries >= HConstants.RETRY_BACKOFF.length) {
44        ntries = HConstants.RETRY_BACKOFF.length - 1;
45      }
46  
47      long normalPause = pause * HConstants.RETRY_BACKOFF[ntries];
48      long jitter =  (long)(normalPause * RANDOM.nextFloat() * 0.01f); // 1% possible jitter
49      return normalPause + jitter;
50    }
51  
52  
53    /**
54     * Adds / subs a 10% jitter to a pause time. Minimum is 1.
55     * @param pause the expected pause.
56     * @param jitter the jitter ratio, between 0 and 1, exclusive.
57     */
58    public static long addJitter(final long pause, final float jitter) {
59      float lag = pause * (RANDOM.nextFloat() - 0.5f) * jitter;
60      long newPause = pause + (long) lag;
61      if (newPause <= 0) {
62        return 1;
63      }
64      return newPause;
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        HConnection 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      int hcRetries = c.getInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER,
87        HConstants.DEFAULT_HBASE_CLIENT_RETRIES_NUMBER);
88      // Go big.  Multiply by 10.  If we can't get to meta after this many retries
89      // then something seriously wrong.
90      int serversideMultiplier = c.getInt("hbase.client.serverside.retries.multiplier", 10);
91      int retries = hcRetries * serversideMultiplier;
92      c.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, retries);
93      log.debug(sn + " HConnection server-to-server retries=" + retries);
94    }
95  }