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.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertTrue; 022 023import org.apache.hadoop.conf.Configuration; 024import org.apache.hadoop.hbase.HBaseConfiguration; 025import org.apache.hadoop.hbase.testclassification.MiscTests; 026import org.apache.hadoop.hbase.testclassification.SmallTests; 027import org.junit.jupiter.api.Tag; 028import org.junit.jupiter.api.Test; 029 030@Tag(MiscTests.TAG) 031@Tag(SmallTests.TAG) 032public class TestLossyCounting { 033 034 private final Configuration conf = HBaseConfiguration.create(); 035 036 @Test 037 public void testBucketSize() { 038 LossyCounting<?> lossyCounting = new LossyCounting<>("testBucketSize", 0.01); 039 assertEquals(100L, lossyCounting.getBucketSize()); 040 LossyCounting<?> lossyCounting2 = new LossyCounting<>("testBucketSize2", conf); 041 assertEquals(50L, lossyCounting2.getBucketSize()); 042 } 043 044 @Test 045 public void testAddByOne() { 046 LossyCounting<String> lossyCounting = new LossyCounting<>("testAddByOne", 0.01); 047 for (int i = 0; i < 100; i++) { 048 String key = "" + i; 049 lossyCounting.add(key); 050 } 051 assertEquals(100L, lossyCounting.getDataSize()); 052 for (int i = 0; i < 100; i++) { 053 String key = "" + i; 054 assertTrue(lossyCounting.contains(key)); 055 } 056 } 057 058 @Test 059 public void testSweep1() throws Exception { 060 LossyCounting<String> lossyCounting = new LossyCounting<>("testSweep1", 0.01); 061 for (int i = 0; i < 400; i++) { 062 String key = "" + i; 063 lossyCounting.add(key); 064 } 065 assertEquals(4L, lossyCounting.getCurrentTerm()); 066 waitForSweep(lossyCounting); 067 068 // Do last one sweep as some sweep will be skipped when first one was running 069 lossyCounting.sweep(); 070 assertEquals(lossyCounting.getBucketSize() - 1, lossyCounting.getDataSize()); 071 } 072 073 private void waitForSweep(LossyCounting<?> lossyCounting) throws InterruptedException { 074 // wait for sweep thread to complete 075 int retry = 0; 076 while (!lossyCounting.getSweepFuture().isDone() && retry < 10) { 077 Thread.sleep(100); 078 retry++; 079 } 080 } 081 082 @Test 083 public void testSweep2() throws Exception { 084 LossyCounting<String> lossyCounting = new LossyCounting<>("testSweep2", 0.1); 085 for (int i = 0; i < 10; i++) { 086 String key = "" + i; 087 lossyCounting.add(key); 088 } 089 waitForSweep(lossyCounting); 090 assertEquals(10L, lossyCounting.getDataSize()); 091 for (int i = 0; i < 10; i++) { 092 String key = "1"; 093 lossyCounting.add(key); 094 } 095 waitForSweep(lossyCounting); 096 assertEquals(1L, lossyCounting.getDataSize()); 097 } 098}