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