1   /*
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.regionserver;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.io.IOException;
26  
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.fs.FileSystem;
29  import org.apache.hadoop.fs.Path;
30  import org.apache.hadoop.hbase.*;
31  import org.apache.hadoop.hbase.client.Increment;
32  import org.apache.hadoop.hbase.client.Result;
33  import org.apache.hadoop.hbase.util.Bytes;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  
37  @Category(SmallTests.class)
38  public class TestResettingCounters {
39  
40    @Test
41    public void testResettingCounters() throws Exception {
42  
43      HBaseTestingUtility htu = new HBaseTestingUtility();
44      Configuration conf = htu.getConfiguration();
45      FileSystem fs = FileSystem.get(conf);
46      byte [] table = Bytes.toBytes("table");
47      byte [][] families = new byte [][] {
48          Bytes.toBytes("family1"),
49          Bytes.toBytes("family2"),
50          Bytes.toBytes("family3")
51      };
52      int numQualifiers = 10;
53      byte [][] qualifiers = new byte [numQualifiers][];
54      for (int i=0; i<numQualifiers; i++) qualifiers[i] = Bytes.toBytes("qf" + i);
55      int numRows = 10;
56      byte [][] rows = new byte [numRows][];
57      for (int i=0; i<numRows; i++) rows[i] = Bytes.toBytes("r" + i);
58  
59      HTableDescriptor htd = new HTableDescriptor(table);
60      for (byte [] family : families) htd.addFamily(new HColumnDescriptor(family));
61  
62      HRegionInfo hri = new HRegionInfo(htd.getName(), null, null, false);
63      String testDir = htu.getDataTestDir() + "/TestResettingCounters/";
64      Path path = new Path(testDir);
65      if (fs.exists(path)) {
66        if (!fs.delete(path, true)) {
67          throw new IOException("Failed delete of " + path);
68        }
69      }
70      HRegion region = HRegion.createHRegion(hri, path, conf, htd);
71      try {
72        Increment odd = new Increment(rows[0]);
73        Increment even = new Increment(rows[0]);
74        Increment all = new Increment(rows[0]);
75        for (int i=0;i<numQualifiers;i++) {
76          if (i % 2 == 0) even.addColumn(families[0], qualifiers[i], 1);
77          else odd.addColumn(families[0], qualifiers[i], 1);
78          all.addColumn(families[0], qualifiers[i], 1);
79        }
80  
81        // increment odd qualifiers 5 times and flush
82        for (int i=0;i<5;i++) region.increment(odd, null, false);
83        region.flushcache();
84  
85        // increment even qualifiers 5 times
86        for (int i=0;i<5;i++) region.increment(even, null, false);
87  
88        // increment all qualifiers, should have value=6 for all
89        Result result = region.increment(all, null, false);
90        assertEquals(numQualifiers, result.size());
91        KeyValue [] kvs = result.raw();
92        for (int i=0;i<kvs.length;i++) {
93          System.out.println(kvs[i].toString());
94          assertTrue(Bytes.equals(kvs[i].getQualifier(), qualifiers[i]));
95          assertEquals(6, Bytes.toLong(kvs[i].getValue()));
96        }
97      } finally {
98        HRegion.closeHRegion(region);
99      }
100     region.close();
101     region.getLog().closeAndDelete();
102   }
103 
104   @org.junit.Rule
105   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
106     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
107 }
108