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.Assert.assertEquals;
021import static org.junit.Assert.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.HBaseClassTestRule;
030import org.apache.hadoop.hbase.HBaseTestingUtility;
031import org.apache.hadoop.hbase.HColumnDescriptor;
032import org.apache.hadoop.hbase.HConstants;
033import org.apache.hadoop.hbase.HRegionInfo;
034import org.apache.hadoop.hbase.HTableDescriptor;
035import org.apache.hadoop.hbase.TableName;
036import org.apache.hadoop.hbase.client.Durability;
037import org.apache.hadoop.hbase.client.Increment;
038import org.apache.hadoop.hbase.client.Result;
039import org.apache.hadoop.hbase.testclassification.RegionServerTests;
040import org.apache.hadoop.hbase.testclassification.SmallTests;
041import org.apache.hadoop.hbase.util.Bytes;
042import org.junit.ClassRule;
043import org.junit.Rule;
044import org.junit.Test;
045import org.junit.experimental.categories.Category;
046import org.junit.rules.TestName;
047
048@Category({ RegionServerTests.class, SmallTests.class })
049public class TestResettingCounters {
050
051  @ClassRule
052  public static final HBaseClassTestRule CLASS_RULE =
053    HBaseClassTestRule.forClass(TestResettingCounters.class);
054
055  @Rule
056  public TestName name = new TestName();
057
058  @Test
059  public void testResettingCounters() throws Exception {
060
061    HBaseTestingUtility htu = new HBaseTestingUtility();
062    Configuration conf = htu.getConfiguration();
063    FileSystem fs = FileSystem.get(conf);
064    byte[] table = Bytes.toBytes(name.getMethodName());
065    byte[][] families =
066      new byte[][] { Bytes.toBytes("family1"), Bytes.toBytes("family2"), Bytes.toBytes("family3") };
067    int numQualifiers = 10;
068    byte[][] qualifiers = new byte[numQualifiers][];
069    for (int i = 0; i < numQualifiers; i++)
070      qualifiers[i] = Bytes.toBytes("qf" + i);
071    int numRows = 10;
072    byte[][] rows = new byte[numRows][];
073    for (int i = 0; i < numRows; i++)
074      rows[i] = Bytes.toBytes("r" + i);
075
076    HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(table));
077    for (byte[] family : families)
078      htd.addFamily(new HColumnDescriptor(family));
079
080    HRegionInfo hri = new HRegionInfo(htd.getTableName(), null, null, false);
081    String testDir = htu.getDataTestDir() + "/TestResettingCounters/";
082    Path path = new Path(testDir);
083    if (fs.exists(path)) {
084      if (!fs.delete(path, true)) {
085        throw new IOException("Failed delete of " + path);
086      }
087    }
088    HRegion region = HBaseTestingUtility.createRegionAndWAL(hri, path, conf, htd);
089    try {
090      Increment odd = new Increment(rows[0]);
091      odd.setDurability(Durability.SKIP_WAL);
092      Increment even = new Increment(rows[0]);
093      even.setDurability(Durability.SKIP_WAL);
094      Increment all = new Increment(rows[0]);
095      all.setDurability(Durability.SKIP_WAL);
096      for (int i = 0; i < numQualifiers; i++) {
097        if (i % 2 == 0) even.addColumn(families[0], qualifiers[i], 1);
098        else odd.addColumn(families[0], qualifiers[i], 1);
099        all.addColumn(families[0], qualifiers[i], 1);
100      }
101
102      // increment odd qualifiers 5 times and flush
103      for (int i = 0; i < 5; i++)
104        region.increment(odd, HConstants.NO_NONCE, HConstants.NO_NONCE);
105      region.flush(true);
106
107      // increment even qualifiers 5 times
108      for (int i = 0; i < 5; i++)
109        region.increment(even, HConstants.NO_NONCE, HConstants.NO_NONCE);
110
111      // increment all qualifiers, should have value=6 for all
112      Result result = region.increment(all, HConstants.NO_NONCE, HConstants.NO_NONCE);
113      assertEquals(numQualifiers, result.size());
114      Cell[] kvs = result.rawCells();
115      for (int i = 0; i < kvs.length; i++) {
116        System.out.println(kvs[i].toString());
117        assertTrue(CellUtil.matchingQualifier(kvs[i], qualifiers[i]));
118        assertEquals(6, Bytes.toLong(CellUtil.cloneValue(kvs[i])));
119      }
120    } finally {
121      HBaseTestingUtility.closeRegionAndWAL(region);
122    }
123    HBaseTestingUtility.closeRegionAndWAL(region);
124  }
125
126}