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.HBaseTestingUtility;
31  import org.apache.hadoop.hbase.HColumnDescriptor;
32  import org.apache.hadoop.hbase.HRegionInfo;
33  import org.apache.hadoop.hbase.HTableDescriptor;
34  import org.apache.hadoop.hbase.KeyValue;
35  import org.apache.hadoop.hbase.client.Increment;
36  import org.apache.hadoop.hbase.client.Result;
37  import org.apache.hadoop.hbase.util.Bytes;
38  import org.junit.Test;
39  
40  public class TestResettingCounters {
41  
42    @Test
43    public void testResettingCounters() throws Exception {
44  
45      HBaseTestingUtility htu = new HBaseTestingUtility();
46      Configuration conf = htu.getConfiguration();
47      FileSystem fs = FileSystem.get(conf);
48      byte [] table = Bytes.toBytes("table");
49      byte [][] families = new byte [][] {
50          Bytes.toBytes("family1"),
51          Bytes.toBytes("family2"),
52          Bytes.toBytes("family3")
53      };
54      int numQualifiers = 10;
55      byte [][] qualifiers = new byte [numQualifiers][];
56      for (int i=0; i<numQualifiers; i++) qualifiers[i] = Bytes.toBytes("qf" + i);
57      int numRows = 10;
58      byte [][] rows = new byte [numRows][];
59      for (int i=0; i<numRows; i++) rows[i] = Bytes.toBytes("r" + i);
60  
61      HTableDescriptor htd = new HTableDescriptor(table);
62      for (byte [] family : families) htd.addFamily(new HColumnDescriptor(family));
63  
64      HRegionInfo hri = new HRegionInfo(htd, null, null, false);
65      String testDir = HBaseTestingUtility.getTestDir() + "/TestResettingCounters/";
66      Path path = new Path(testDir);
67      if (fs.exists(path)) {
68        if (!fs.delete(path, true)) {
69          throw new IOException("Failed delete of " + path);
70        }
71      }
72      HRegion region = HRegion.createHRegion(hri, path, conf);
73  
74      Increment odd = new Increment(rows[0]);
75      Increment even = new Increment(rows[0]);
76      Increment all = new Increment(rows[0]);
77      for (int i=0;i<numQualifiers;i++) {
78        if (i % 2 == 0) even.addColumn(families[0], qualifiers[i], 1);
79        else odd.addColumn(families[0], qualifiers[i], 1);
80        all.addColumn(families[0], qualifiers[i], 1);
81      }
82  
83      // increment odd qualifiers 5 times and flush
84      for (int i=0;i<5;i++) region.increment(odd, null, false);
85      region.flushcache();
86  
87      // increment even qualifiers 5 times
88      for (int i=0;i<5;i++) region.increment(even, null, false);
89  
90      // increment all qualifiers, should have value=6 for all
91      Result result = region.increment(all, null, false);
92      assertEquals(numQualifiers, result.size());
93      KeyValue [] kvs = result.raw();
94      for (int i=0;i<kvs.length;i++) {
95        System.out.println(kvs[i].toString());
96        assertTrue(Bytes.equals(kvs[i].getQualifier(), qualifiers[i]));
97        assertEquals(6, Bytes.toLong(kvs[i].getValue()));
98      }
99    }
100 }