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
19 package org.apache.hadoop.hbase.replication.regionserver;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import org.apache.hadoop.hbase.testclassification.SmallTests;
28 import org.junit.Test;
29 import org.junit.experimental.categories.Category;
30
31 @Category(SmallTests.class)
32 public class TestReplicationThrottler {
33
34 private static final Log LOG = LogFactory.getLog(TestReplicationThrottler.class);
35
36 /**
37 * unit test for throttling
38 */
39 @Test(timeout=10000)
40 public void testThrottling() {
41 LOG.info("testThrottling");
42
43 // throttle bandwidth is 100 and 10 bytes/cycle respectively
44 ReplicationThrottler throttler1 = new ReplicationThrottler(100);
45 ReplicationThrottler throttler2 = new ReplicationThrottler(10);
46
47 long ticks1 = throttler1.getNextSleepInterval(1000);
48 long ticks2 = throttler2.getNextSleepInterval(1000);
49
50 // 1. the first push size is 1000, though 1000 bytes exceeds 100/10
51 // bandwidthes, but no sleep since it's the first push of current
52 // cycle, amortizing occurs when next push arrives
53 assertEquals(0, ticks1);
54 assertEquals(0, ticks2);
55
56 throttler1.addPushSize(1000);
57 throttler2.addPushSize(1000);
58
59 ticks1 = throttler1.getNextSleepInterval(5);
60 ticks2 = throttler2.getNextSleepInterval(5);
61
62 // 2. when the second push(5) arrives and throttling(5) is called, the
63 // current cyclePushSize is 1000 bytes, this should make throttler1
64 // sleep 1000/100 = 10 cycles = 1s and make throttler2 sleep 1000/10
65 // = 100 cycles = 10s before the second push occurs -- amortize case
66 // after amortizing, both cycleStartTick and cyclePushSize are reset
67 //
68 // Note: in a slow machine, the sleep interval might be less than ideal ticks.
69 // If it is 75% of expected value, its is still acceptable.
70 if (ticks1 != 1000 && ticks1 != 999) {
71 assertTrue(ticks1 >= 750 && ticks1 <=1000);
72 }
73 if (ticks2 != 10000 && ticks2 != 9999) {
74 assertTrue(ticks2 >= 7500 && ticks2 <=10000);
75 }
76
77 throttler1.resetStartTick();
78 throttler2.resetStartTick();
79
80 throttler1.addPushSize(5);
81 throttler2.addPushSize(5);
82
83 ticks1 = throttler1.getNextSleepInterval(45);
84 ticks2 = throttler2.getNextSleepInterval(45);
85
86 // 3. when the third push(45) arrives and throttling(45) is called, the
87 // current cyclePushSize is 5 bytes, 50-byte makes throttler1 no
88 // sleep, but can make throttler2 delay to next cycle
89 // note: in real case, sleep time should cover time elapses during push
90 // operation
91 assertTrue(ticks1 == 0);
92 if (ticks2 != 100 && ticks2 != 99) {
93 assertTrue(ticks1 >= 75 && ticks1 <=100);
94 }
95
96 throttler2.resetStartTick();
97
98 throttler1.addPushSize(45);
99 throttler2.addPushSize(45);
100
101 ticks1 = throttler1.getNextSleepInterval(60);
102 ticks2 = throttler2.getNextSleepInterval(60);
103
104 // 4. when the fourth push(60) arrives and throttling(60) is called, throttler1
105 // delay to next cycle since 45+60 == 105; and throttler2 should firstly sleep
106 // ceiling(45/10)= 5 cycles = 500ms to amortize previous push
107 //
108 // Note: in real case, sleep time should cover time elapses during push operation
109 if (ticks1 != 100 && ticks1 != 99) {
110 assertTrue(ticks1 >= 75 && ticks1 <=100);
111 }
112 if (ticks2 != 500 && ticks2 != 499) {
113 assertTrue(ticks1 >= 375 && ticks1 <=500);
114 }
115 }
116 }