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