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 = new byte [][] {
066        Bytes.toBytes("family1"),
067        Bytes.toBytes("family2"),
068        Bytes.toBytes("family3")
069    };
070    int numQualifiers = 10;
071    byte [][] qualifiers = new byte [numQualifiers][];
072    for (int i=0; i<numQualifiers; i++) qualifiers[i] = Bytes.toBytes("qf" + i);
073    int numRows = 10;
074    byte [][] rows = new byte [numRows][];
075    for (int i=0; i<numRows; i++) rows[i] = Bytes.toBytes("r" + i);
076
077    HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(table));
078    for (byte [] family : families) 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++) region.increment(odd, HConstants.NO_NONCE, HConstants.NO_NONCE);
104      region.flush(true);
105
106      // increment even qualifiers 5 times
107      for (int i=0;i<5;i++) region.increment(even, HConstants.NO_NONCE, HConstants.NO_NONCE);
108
109      // increment all qualifiers, should have value=6 for all
110      Result result = region.increment(all, HConstants.NO_NONCE, HConstants.NO_NONCE);
111      assertEquals(numQualifiers, result.size());
112      Cell[] kvs = result.rawCells();
113      for (int i=0;i<kvs.length;i++) {
114        System.out.println(kvs[i].toString());
115        assertTrue(CellUtil.matchingQualifier(kvs[i], qualifiers[i]));
116        assertEquals(6, Bytes.toLong(CellUtil.cloneValue(kvs[i])));
117      }
118    } finally {
119      HBaseTestingUtility.closeRegionAndWAL(region);
120    }
121    HBaseTestingUtility.closeRegionAndWAL(region);
122  }
123
124}
125