001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.util;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.HBaseConfiguration;
026import org.apache.hadoop.hbase.testclassification.MiscTests;
027import org.apache.hadoop.hbase.testclassification.SmallTests;
028import org.junit.ClassRule;
029import org.junit.Test;
030import org.junit.experimental.categories.Category;
031
032@Category({ MiscTests.class, SmallTests.class })
033public class TestLossyCounting {
034
035  @ClassRule
036  public static final HBaseClassTestRule CLASS_RULE =
037    HBaseClassTestRule.forClass(TestLossyCounting.class);
038
039  private final Configuration conf = HBaseConfiguration.create();
040
041  @Test
042  public void testBucketSize() {
043    LossyCounting<?> lossyCounting = new LossyCounting<>("testBucketSize", 0.01);
044    assertEquals(100L, lossyCounting.getBucketSize());
045    LossyCounting<?> lossyCounting2 = new LossyCounting<>("testBucketSize2", conf);
046    assertEquals(50L, lossyCounting2.getBucketSize());
047  }
048
049  @Test
050  public void testAddByOne() {
051    LossyCounting<String> lossyCounting = new LossyCounting<>("testAddByOne", 0.01);
052    for (int i = 0; i < 100; i++) {
053      String key = "" + i;
054      lossyCounting.add(key);
055    }
056    assertEquals(100L, lossyCounting.getDataSize());
057    for (int i = 0; i < 100; i++) {
058      String key = "" + i;
059      assertTrue(lossyCounting.contains(key));
060    }
061  }
062
063  @Test
064  public void testSweep1() throws Exception {
065    LossyCounting<String> lossyCounting = new LossyCounting<>("testSweep1", 0.01);
066    for (int i = 0; i < 400; i++) {
067      String key = "" + i;
068      lossyCounting.add(key);
069    }
070    assertEquals(4L, lossyCounting.getCurrentTerm());
071    waitForSweep(lossyCounting);
072
073    // Do last one sweep as some sweep will be skipped when first one was running
074    lossyCounting.sweep();
075    assertEquals(lossyCounting.getBucketSize() - 1, lossyCounting.getDataSize());
076  }
077
078  private void waitForSweep(LossyCounting<?> lossyCounting) throws InterruptedException {
079    // wait for sweep thread to complete
080    int retry = 0;
081    while (!lossyCounting.getSweepFuture().isDone() && retry < 10) {
082      Thread.sleep(100);
083      retry++;
084    }
085  }
086
087  @Test
088  public void testSweep2() throws Exception {
089    LossyCounting<String> lossyCounting = new LossyCounting<>("testSweep2", 0.1);
090    for (int i = 0; i < 10; i++) {
091      String key = "" + i;
092      lossyCounting.add(key);
093    }
094    waitForSweep(lossyCounting);
095    assertEquals(10L, lossyCounting.getDataSize());
096    for (int i = 0; i < 10; i++) {
097      String key = "1";
098      lossyCounting.add(key);
099    }
100    waitForSweep(lossyCounting);
101    assertEquals(1L, lossyCounting.getDataSize());
102  }
103}