View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.util;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertTrue;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.HBaseConfiguration;
26  import org.apache.hadoop.hbase.testclassification.MiscTests;
27  import org.apache.hadoop.hbase.testclassification.SmallTests;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  @Category({MiscTests.class, SmallTests.class})
32  public class TestLossyCounting {
33  
34    private final Configuration conf = HBaseConfiguration.create();
35  
36    @Test
37    public void testBucketSize() {
38      LossyCounting<?> lossyCounting = new LossyCounting<>("testBucketSize", 0.01);
39      assertEquals(100L, lossyCounting.getBucketSize());
40      LossyCounting<?> lossyCounting2 = new LossyCounting<>("testBucketSize2", conf);
41      assertEquals(50L, lossyCounting2.getBucketSize());
42    }
43  
44    @Test
45    public void testAddByOne() {
46      LossyCounting<String> lossyCounting = new LossyCounting<>("testAddByOne", 0.01);
47      for (int i = 0; i < 100; i++) {
48        String key = "" + i;
49        lossyCounting.add(key);
50      }
51      assertEquals(100L, lossyCounting.getDataSize());
52      for (int i = 0; i < 100; i++) {
53        String key = "" + i;
54        assertTrue(lossyCounting.contains(key));
55      }
56    }
57  
58    @Test
59    public void testSweep1() throws Exception {
60      LossyCounting<String> lossyCounting = new LossyCounting<>("testSweep1", 0.01);
61      for(int i = 0; i < 400; i++){
62        String key = "" + i;
63        lossyCounting.add(key);
64      }
65      assertEquals(4L, lossyCounting.getCurrentTerm());
66      waitForSweep(lossyCounting);
67  
68      //Do last one sweep as some sweep will be skipped when first one was running
69      lossyCounting.sweep();
70      assertEquals(lossyCounting.getBucketSize() - 1, lossyCounting.getDataSize());
71    }
72  
73    private void waitForSweep(LossyCounting<?> lossyCounting) throws InterruptedException {
74      //wait for sweep thread to complete
75      int retry = 0;
76      while (!lossyCounting.getSweepFuture().isDone() && retry < 10) {
77        Thread.sleep(100);
78        retry++;
79      }
80    }
81  
82    @Test
83    public void testSweep2() throws Exception {
84      LossyCounting<String> lossyCounting = new LossyCounting<>("testSweep2", 0.1);
85      for (int i = 0; i < 10; i++) {
86        String key = "" + i;
87        lossyCounting.add(key);
88      }
89      waitForSweep(lossyCounting);
90      assertEquals(10L, lossyCounting.getDataSize());
91      for(int i = 0; i < 10; i++){
92        String key = "1";
93        lossyCounting.add(key);
94      }
95      waitForSweep(lossyCounting);
96      assertEquals(1L, lossyCounting.getDataSize());
97    }
98  }