View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import static org.junit.Assert.assertTrue;
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotEquals;
24  
25  import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
26  import org.apache.hadoop.hbase.testclassification.MetricsTests;
27  import org.apache.hadoop.hbase.testclassification.SmallTests;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  /**
32   *  Test for MetricsTableSourceImpl
33   */
34  @Category({MetricsTests.class, SmallTests.class})
35  public class TestMetricsTableSourceImpl {
36  
37    @SuppressWarnings("SelfComparison")
38    @Test
39    public void testCompareToHashCode() throws Exception {
40      MetricsRegionServerSourceFactory metricsFact =
41          CompatibilitySingletonFactory.getInstance(MetricsRegionServerSourceFactory.class);
42  
43      MetricsTableSource one = metricsFact.createTable("ONETABLE", new TableWrapperStub("ONETABLE"));
44      MetricsTableSource oneClone = metricsFact.createTable("ONETABLE", new TableWrapperStub("ONETABLE"));
45      MetricsTableSource two = metricsFact.createTable("TWOTABLE", new TableWrapperStub("TWOTABLE"));
46  
47      assertEquals(0, one.compareTo(oneClone));
48      assertEquals(one.hashCode(), oneClone.hashCode());
49      assertNotEquals(one, two);
50  
51      assertTrue(one.compareTo(two) != 0);
52      assertTrue(two.compareTo(one) != 0);
53      assertTrue(two.compareTo(one) != one.compareTo(two));
54      assertTrue(two.compareTo(two) == 0);
55    }
56  
57    @Test(expected = RuntimeException.class)
58    public void testNoGetTableMetricsSourceImpl() throws Exception {
59      // This should throw an exception because MetricsTableSourceImpl should only
60      // be created by a factory.
61      CompatibilitySingletonFactory.getInstance(MetricsTableSourceImpl.class);
62    }
63  
64    @Test
65    public void testGetTableMetrics() throws Exception{
66      MetricsTableSource oneTbl =
67          CompatibilitySingletonFactory.getInstance(MetricsRegionServerSourceFactory.class)
68          .createTable("ONETABLE", new TableWrapperStub("ONETABLE"));
69      assertEquals("ONETABLE", oneTbl.getTableName());
70    }
71  
72    static class TableWrapperStub implements MetricsTableWrapperAggregate {
73  
74      private String tableName;
75  
76      public TableWrapperStub(String tableName) {
77        this.tableName = tableName;
78      }
79  
80      @Override
81      public long getReadRequestsCount(String table) {
82        return 10;
83      }
84  
85      @Override
86      public long getWriteRequestsCount(String table) {
87        return 20;
88      }
89  
90      @Override
91      public long getTotalRequestsCount(String table) {
92        return 30;
93      }
94  
95      @Override
96      public long getMemstoresSize(String table) {
97        return 1000;
98      }
99  
100     @Override
101     public long getStoreFilesSize(String table) {
102       return 2000;
103     }
104 
105     @Override
106     public long getTableSize(String table) {
107       return 3000;
108     }
109 
110     @Override
111     public long getNumRegions(String table) {
112       return 3;
113     }
114 
115     @Override
116     public long getAvgRegionSize(String table) {
117       return 3000;
118     }
119 
120     public String getTableName() {
121       return tableName;
122     }
123   }
124 }