001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.regionserver;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertNotEquals;
022
023import java.util.HashMap;
024import java.util.Map;
025
026import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.testclassification.MetricsTests;
029import org.apache.hadoop.hbase.testclassification.SmallTests;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033
034@Category({MetricsTests.class, SmallTests.class})
035public class TestMetricsRegionSourceImpl {
036  @ClassRule
037  public static final HBaseClassTestRule CLASS_RULE =
038      HBaseClassTestRule.forClass(TestMetricsRegionSourceImpl.class);
039
040  @SuppressWarnings("SelfComparison")
041  @Test
042  public void testCompareToHashCodeEquals() {
043    MetricsRegionServerSourceFactory fact = CompatibilitySingletonFactory.getInstance(
044            MetricsRegionServerSourceFactory.class);
045
046    MetricsRegionSource one = fact.createRegion(new RegionWrapperStub("TEST"));
047    MetricsRegionSource oneClone = fact.createRegion(new RegionWrapperStub("TEST"));
048    MetricsRegionSource two = fact.createRegion(new RegionWrapperStub("TWO"));
049
050    assertEquals(0, one.compareTo(oneClone));
051    assertEquals(one.hashCode(), oneClone.hashCode());
052    assertNotEquals(one, two);
053
054    assertNotEquals(0, one.compareTo(two));
055    assertNotEquals(0, two.compareTo(one));
056    assertNotEquals(one.compareTo(two), two.compareTo(one));
057    assertEquals(0, two.compareTo(two));
058  }
059
060  @Test(expected = RuntimeException.class)
061  public void testNoGetRegionServerMetricsSourceImpl() {
062    // This should throw an exception because MetricsRegionSourceImpl should only
063    // be created by a factory.
064    CompatibilitySingletonFactory.getInstance(MetricsRegionSource.class);
065  }
066
067  static class RegionWrapperStub implements MetricsRegionWrapper {
068
069    private String regionName;
070
071    RegionWrapperStub(String regionName) {
072      this.regionName = regionName;
073    }
074
075    @Override
076    public String getTableName() {
077      return null;
078    }
079
080    @Override
081    public String getNamespace() {
082      return null;
083    }
084
085    @Override
086    public String getRegionName() {
087      return this.regionName;
088    }
089
090    @Override
091    public long getNumStores() {
092      return 0;
093    }
094
095    @Override
096    public long getNumStoreFiles() {
097      return 0;
098    }
099
100    @Override
101    public long getStoreRefCount() {
102      return 0;
103    }
104
105    @Override
106    public long getMaxCompactedStoreFileRefCount() {
107      return 0;
108    }
109
110    @Override
111    public long getMemStoreSize() {
112      return 0;
113    }
114
115    @Override
116    public long getStoreFileSize() {
117      return 0;
118    }
119
120    @Override
121    public long getReadRequestCount() {
122      return 0;
123    }
124
125    @Override
126    public long getFilteredReadRequestCount() {
127      return 0;
128    }
129
130    @Override
131    public long getMaxStoreFileAge() {
132      return 0;
133    }
134
135    @Override
136    public long getMinStoreFileAge() {
137      return 0;
138    }
139
140    @Override
141    public long getAvgStoreFileAge() {
142      return 0;
143    }
144
145    @Override
146    public long getNumReferenceFiles() {
147      return 0;
148    }
149
150    @Override
151    public long getCpRequestCount() {
152      return 0;
153    }
154
155    @Override
156    public long getWriteRequestCount() {
157      return 0;
158    }
159
160    @Override
161    public long getNumFilesCompacted() {
162      return 0;
163    }
164
165    @Override
166    public long getNumBytesCompacted() {
167      return 0;
168    }
169
170    @Override
171    public long getLastMajorCompactionAge() {
172      return 0;
173    }
174
175    @Override
176    public long getNumCompactionsCompleted() {
177      return 0;
178    }
179
180    @Override
181    public long getNumCompactionsFailed() {
182      return 0;
183    }
184
185    @Override
186    public int getRegionHashCode() {
187      return regionName.hashCode();
188    }
189
190    /**
191     * Always return 0 for testing
192     */
193    @Override
194    public int getReplicaId() {
195      return 0;
196    }
197
198    @Override
199    public long getNumCompactionsQueued() {
200      return 0;
201    }
202
203    @Override
204    public long getNumFlushesQueued() {
205      return 0;
206    }
207
208    @Override
209    public long getMaxCompactionQueueSize() {
210      return 0;
211    }
212
213    @Override
214    public long getMaxFlushQueueSize() {
215      return 0;
216    }
217
218    @Override
219    public long getTotalRequestCount() {
220      return 0;
221    }
222
223    @Override
224    public Map<String, Long> getMemstoreOnlyRowReadsCount() {
225      Map<String, Long> map = new HashMap<String, Long>();
226      map.put("info", 0L);
227      return map;
228    }
229
230    @Override
231    public Map<String, Long> getMixedRowReadsCount() {
232      Map<String, Long> map = new HashMap<String, Long>();
233      map.put("info", 0L);
234      return map;
235    }
236  }
237}