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.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(TableName.valueOf(table));
60      for (byte [] family : families) htd.addFamily(new HColumnDescriptor(family));
61  
62      HRegionInfo hri = new HRegionInfo(htd.getTableName(), 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        odd.setDurability(Durability.SKIP_WAL);
74        Increment even = new Increment(rows[0]);
75        even.setDurability(Durability.SKIP_WAL);
76        Increment all = new Increment(rows[0]);
77        all.setDurability(Durability.SKIP_WAL);
78        for (int i=0;i<numQualifiers;i++) {
79          if (i % 2 == 0) even.addColumn(families[0], qualifiers[i], 1);
80          else odd.addColumn(families[0], qualifiers[i], 1);
81          all.addColumn(families[0], qualifiers[i], 1);
82        }
83  
84        // increment odd qualifiers 5 times and flush
85        for (int i=0;i<5;i++) region.increment(odd);
86        region.flushcache();
87  
88        // increment even qualifiers 5 times
89        for (int i=0;i<5;i++) region.increment(even);
90  
91        // increment all qualifiers, should have value=6 for all
92        Result result = region.increment(all);
93        assertEquals(numQualifiers, result.size());
94        Cell [] kvs = result.rawCells();
95        for (int i=0;i<kvs.length;i++) {
96          System.out.println(kvs[i].toString());
97          assertTrue(CellUtil.matchingQualifier(kvs[i], qualifiers[i]));
98          assertEquals(6, Bytes.toLong(CellUtil.cloneValue(kvs[i])));
99        }
100     } finally {
101       HRegion.closeHRegion(region);
102     }
103     HRegion.closeHRegion(region);
104   }
105 
106 }
107