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