View Javadoc

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