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  
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      assertTrue(ticks1 == 1000 || ticks1 == 999);
68      assertTrue(ticks2 == 10000 || ticks2 == 9999);
69  
70      throttler1.resetStartTick();
71      throttler2.resetStartTick();
72  
73      throttler1.addPushSize(5);
74      throttler2.addPushSize(5);
75  
76      ticks1 = throttler1.getNextSleepInterval(45);
77      ticks2 = throttler2.getNextSleepInterval(45);
78  
79      // 3. when the third push(45) arrives and throttling(45) is called, the
80      //    current cyclePushSize is 5 bytes, 50-byte makes throttler1 no
81      //    sleep, but can make throttler2 delay to next cycle
82      // note: in real case, sleep time should cover time elapses during push
83      //       operation
84      assertTrue(ticks1 == 0);
85      assertTrue(ticks2 == 100 || ticks2 == 99);
86  
87      throttler2.resetStartTick();
88  
89      throttler1.addPushSize(45);
90      throttler2.addPushSize(45);
91  
92      ticks1 = throttler1.getNextSleepInterval(60);
93      ticks2 = throttler2.getNextSleepInterval(60);
94  
95      // 4. when the fourth push(60) arrives and throttling(60) is called, throttler1
96      //    delay to next cycle since 45+60 == 105; and throttler2 should firstly sleep
97      //    ceiling(45/10)= 5 cycles = 500ms to amortize previous push
98      // note: in real case, sleep time should cover time elapses during push
99      //       operation
100     assertTrue(ticks1 == 100 || ticks1 == 99);
101     assertTrue(ticks2 == 500 || ticks2 == 499);
102   }
103 }