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.regionserver; 019 020import static org.junit.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertTrue; 022 023import java.io.IOException; 024import org.apache.hadoop.conf.Configuration; 025import org.apache.hadoop.fs.FileSystem; 026import org.apache.hadoop.fs.Path; 027import org.apache.hadoop.hbase.Cell; 028import org.apache.hadoop.hbase.CellUtil; 029import org.apache.hadoop.hbase.HBaseTestingUtil; 030import org.apache.hadoop.hbase.HConstants; 031import org.apache.hadoop.hbase.TableName; 032import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 033import org.apache.hadoop.hbase.client.Durability; 034import org.apache.hadoop.hbase.client.Increment; 035import org.apache.hadoop.hbase.client.RegionInfo; 036import org.apache.hadoop.hbase.client.RegionInfoBuilder; 037import org.apache.hadoop.hbase.client.Result; 038import org.apache.hadoop.hbase.client.TableDescriptor; 039import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 040import org.apache.hadoop.hbase.testclassification.RegionServerTests; 041import org.apache.hadoop.hbase.testclassification.SmallTests; 042import org.apache.hadoop.hbase.util.Bytes; 043import org.junit.jupiter.api.Tag; 044import org.junit.jupiter.api.Test; 045import org.junit.jupiter.api.TestInfo; 046 047@Tag(RegionServerTests.TAG) 048@Tag(SmallTests.TAG) 049public class TestResettingCounters { 050 051 @Test 052 public void testResettingCounters(TestInfo testInfo) throws Exception { 053 HBaseTestingUtil htu = new HBaseTestingUtil(); 054 Configuration conf = htu.getConfiguration(); 055 FileSystem fs = FileSystem.get(conf); 056 byte[] table = Bytes.toBytes(testInfo.getTestMethod().get().getName()); 057 byte[][] families = 058 new byte[][] { Bytes.toBytes("family1"), Bytes.toBytes("family2"), Bytes.toBytes("family3") }; 059 int numQualifiers = 10; 060 byte[][] qualifiers = new byte[numQualifiers][]; 061 for (int i = 0; i < numQualifiers; i++) 062 qualifiers[i] = Bytes.toBytes("qf" + i); 063 int numRows = 10; 064 byte[][] rows = new byte[numRows][]; 065 for (int i = 0; i < numRows; i++) 066 rows[i] = Bytes.toBytes("r" + i); 067 068 TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(TableName.valueOf(table)); 069 for (byte[] family : families) { 070 builder.setColumnFamily(ColumnFamilyDescriptorBuilder.of(family)); 071 } 072 TableDescriptor tableDescriptor = builder.build(); 073 RegionInfo hri = RegionInfoBuilder.newBuilder(tableDescriptor.getTableName()).build(); 074 String testDir = htu.getDataTestDir() + "/TestResettingCounters/"; 075 Path path = new Path(testDir); 076 if (fs.exists(path)) { 077 if (!fs.delete(path, true)) { 078 throw new IOException("Failed delete of " + path); 079 } 080 } 081 HRegion region = HBaseTestingUtil.createRegionAndWAL(hri, path, conf, tableDescriptor); 082 try { 083 Increment odd = new Increment(rows[0]); 084 odd.setDurability(Durability.SKIP_WAL); 085 Increment even = new Increment(rows[0]); 086 even.setDurability(Durability.SKIP_WAL); 087 Increment all = new Increment(rows[0]); 088 all.setDurability(Durability.SKIP_WAL); 089 for (int i = 0; i < numQualifiers; i++) { 090 if (i % 2 == 0) even.addColumn(families[0], qualifiers[i], 1); 091 else odd.addColumn(families[0], qualifiers[i], 1); 092 all.addColumn(families[0], qualifiers[i], 1); 093 } 094 095 // increment odd qualifiers 5 times and flush 096 for (int i = 0; i < 5; i++) 097 region.increment(odd, HConstants.NO_NONCE, HConstants.NO_NONCE); 098 region.flush(true); 099 100 // increment even qualifiers 5 times 101 for (int i = 0; i < 5; i++) 102 region.increment(even, HConstants.NO_NONCE, HConstants.NO_NONCE); 103 104 // increment all qualifiers, should have value=6 for all 105 Result result = region.increment(all, HConstants.NO_NONCE, HConstants.NO_NONCE); 106 assertEquals(numQualifiers, result.size()); 107 Cell[] kvs = result.rawCells(); 108 for (int i = 0; i < kvs.length; i++) { 109 System.out.println(kvs[i].toString()); 110 assertTrue(CellUtil.matchingQualifier(kvs[i], qualifiers[i])); 111 assertEquals(6, Bytes.toLong(CellUtil.cloneValue(kvs[i]))); 112 } 113 } finally { 114 HBaseTestingUtil.closeRegionAndWAL(region); 115 } 116 HBaseTestingUtil.closeRegionAndWAL(region); 117 } 118 119}