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 org.apache.hadoop.conf.Configuration;
21  import org.apache.hadoop.hbase.ServerName;
22  import org.apache.hadoop.hbase.client.backoff.ExponentialClientBackoffPolicy;
23  import org.apache.hadoop.hbase.client.backoff.ServerStatistics;
24  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
25  import org.apache.hadoop.hbase.util.Bytes;
26  import org.junit.Test;
27  import org.mockito.Mockito;
28  
29  import static org.junit.Assert.assertEquals;
30  import static org.junit.Assert.assertTrue;
31  
32  public class TestClientExponentialBackoff {
33  
34    ServerName server = Mockito.mock(ServerName.class);
35    byte[] regionname = Bytes.toBytes("region");
36  
37    @Test
38    public void testNulls() {
39      Configuration conf = new Configuration(false);
40      ExponentialClientBackoffPolicy backoff = new ExponentialClientBackoffPolicy(conf);
41      assertEquals(0, backoff.getBackoffTime(null, null, null));
42  
43      // server name doesn't matter to calculation, but check it now anyways
44      assertEquals(0, backoff.getBackoffTime(server, null, null));
45      assertEquals(0, backoff.getBackoffTime(server, regionname, null));
46  
47      // check when no stats for the region yet
48      ServerStatistics stats = new ServerStatistics();
49      assertEquals(0, backoff.getBackoffTime(server, regionname, stats));
50    }
51  
52    @Test
53    public void testMaxLoad() {
54      Configuration conf = new Configuration(false);
55      ExponentialClientBackoffPolicy backoff = new ExponentialClientBackoffPolicy(conf);
56  
57      ServerStatistics stats = new ServerStatistics();
58      update(stats, 100);
59      assertEquals(ExponentialClientBackoffPolicy.DEFAULT_MAX_BACKOFF, backoff.getBackoffTime(server,
60          regionname, stats));
61  
62      // another policy with a different max timeout
63      long max = 100;
64      conf.setLong(ExponentialClientBackoffPolicy.MAX_BACKOFF_KEY, max);
65      ExponentialClientBackoffPolicy backoffShortTimeout = new ExponentialClientBackoffPolicy(conf);
66      assertEquals(max, backoffShortTimeout.getBackoffTime(server, regionname, stats));
67  
68      // test beyond 100 still doesn't exceed the max
69      update(stats, 101);
70      assertEquals(ExponentialClientBackoffPolicy.DEFAULT_MAX_BACKOFF, backoff.getBackoffTime(server,
71          regionname, stats));
72      assertEquals(max, backoffShortTimeout.getBackoffTime(server, regionname, stats));
73  
74      // and that when we are below 100, its less than the max timeout
75      update(stats, 99);
76      assertTrue(backoff.getBackoffTime(server,
77          regionname, stats) < ExponentialClientBackoffPolicy.DEFAULT_MAX_BACKOFF);
78      assertTrue(backoffShortTimeout.getBackoffTime(server, regionname, stats) < max);
79    }
80  
81    /**
82     * Make sure that we get results in the order that we expect - backoff for a load of 1 should
83     * less than backoff for 10, which should be less than that for 50.
84     */
85    @Test
86    public void testResultOrdering() {
87      Configuration conf = new Configuration(false);
88      // make the max timeout really high so we get differentiation between load factors
89      conf.setLong(ExponentialClientBackoffPolicy.MAX_BACKOFF_KEY, Integer.MAX_VALUE);
90      ExponentialClientBackoffPolicy backoff = new ExponentialClientBackoffPolicy(conf);
91  
92      ServerStatistics stats = new ServerStatistics();
93      long previous = backoff.getBackoffTime(server, regionname, stats);
94      for (int i = 1; i <= 100; i++) {
95        update(stats, i);
96        long next = backoff.getBackoffTime(server, regionname, stats);
97        assertTrue(
98            "Previous backoff time" + previous + " >= " + next + ", the next backoff time for " +
99                "load " + i, previous < next);
100       previous = next;
101     }
102   }
103 
104   @Test
105   public void testHeapOccupancyPolicy() {
106     Configuration conf = new Configuration(false);
107     ExponentialClientBackoffPolicy backoff = new ExponentialClientBackoffPolicy(conf);
108 
109     ServerStatistics stats = new ServerStatistics();
110     long backoffTime;
111 
112     update(stats, 0, 95);
113     backoffTime = backoff.getBackoffTime(server, regionname, stats);
114     assertTrue("Heap occupancy at low watermark had no effect", backoffTime > 0);
115 
116     long previous = backoffTime;
117     update(stats, 0, 96);
118     backoffTime = backoff.getBackoffTime(server, regionname, stats);
119     assertTrue("Increase above low watermark should have increased backoff",
120       backoffTime > previous);
121 
122     update(stats, 0, 98);
123     backoffTime = backoff.getBackoffTime(server, regionname, stats);
124     assertEquals("We should be using max backoff when at high watermark", backoffTime,
125       ExponentialClientBackoffPolicy.DEFAULT_MAX_BACKOFF);
126   }
127 
128   private void update(ServerStatistics stats, int load) {
129     ClientProtos.RegionLoadStats stat = ClientProtos.RegionLoadStats.newBuilder()
130         .setMemstoreLoad
131             (load).build();
132     stats.update(regionname, stat);
133   }
134 
135   private void update(ServerStatistics stats, int memstoreLoad, int heapOccupancy) {
136     ClientProtos.RegionLoadStats stat = ClientProtos.RegionLoadStats.newBuilder()
137         .setMemstoreLoad(memstoreLoad)
138         .setHeapOccupancy(heapOccupancy)
139             .build();
140     stats.update(regionname, stat);
141   }
142 }