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.*;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.client.Durability;
030import org.apache.hadoop.hbase.client.Increment;
031import org.apache.hadoop.hbase.client.Result;
032import org.apache.hadoop.hbase.testclassification.RegionServerTests;
033import org.apache.hadoop.hbase.testclassification.SmallTests;
034import org.apache.hadoop.hbase.util.Bytes;
035import org.junit.ClassRule;
036import org.junit.Rule;
037import org.junit.Test;
038import org.junit.experimental.categories.Category;
039import org.junit.rules.TestName;
040
041@Category({RegionServerTests.class, SmallTests.class})
042public class TestResettingCounters {
043
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE =
046      HBaseClassTestRule.forClass(TestResettingCounters.class);
047
048  @Rule
049  public TestName name = new TestName();
050
051  @Test
052  public void testResettingCounters() throws Exception {
053
054    HBaseTestingUtility htu = new HBaseTestingUtility();
055    Configuration conf = htu.getConfiguration();
056    FileSystem fs = FileSystem.get(conf);
057    byte [] table = Bytes.toBytes(name.getMethodName());
058    byte [][] families = new byte [][] {
059        Bytes.toBytes("family1"),
060        Bytes.toBytes("family2"),
061        Bytes.toBytes("family3")
062    };
063    int numQualifiers = 10;
064    byte [][] qualifiers = new byte [numQualifiers][];
065    for (int i=0; i<numQualifiers; i++) qualifiers[i] = Bytes.toBytes("qf" + i);
066    int numRows = 10;
067    byte [][] rows = new byte [numRows][];
068    for (int i=0; i<numRows; i++) rows[i] = Bytes.toBytes("r" + i);
069
070    HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(table));
071    for (byte [] family : families) htd.addFamily(new HColumnDescriptor(family));
072
073    HRegionInfo hri = new HRegionInfo(htd.getTableName(), null, null, false);
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 = HBaseTestingUtility.createRegionAndWAL(hri, path, conf, htd);
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++) region.increment(odd, HConstants.NO_NONCE, HConstants.NO_NONCE);
097      region.flush(true);
098
099      // increment even qualifiers 5 times
100      for (int i=0;i<5;i++) region.increment(even, HConstants.NO_NONCE, HConstants.NO_NONCE);
101
102      // increment all qualifiers, should have value=6 for all
103      Result result = region.increment(all, HConstants.NO_NONCE, HConstants.NO_NONCE);
104      assertEquals(numQualifiers, result.size());
105      Cell [] kvs = result.rawCells();
106      for (int i=0;i<kvs.length;i++) {
107        System.out.println(kvs[i].toString());
108        assertTrue(CellUtil.matchingQualifier(kvs[i], qualifiers[i]));
109        assertEquals(6, Bytes.toLong(CellUtil.cloneValue(kvs[i])));
110      }
111    } finally {
112      HBaseTestingUtility.closeRegionAndWAL(region);
113    }
114    HBaseTestingUtility.closeRegionAndWAL(region);
115  }
116
117}
118