1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
45 assertEquals(0, backoff.getBackoffTime(server, null, null));
46 assertEquals(0, backoff.getBackoffTime(server, regionname, null));
47
48
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
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
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
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
84
85
86 @Test
87 public void testResultOrdering() {
88 Configuration conf = new Configuration(false);
89
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 }