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;
025import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.testclassification.MetricsTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.junit.ClassRule;
030import org.junit.Test;
031import org.junit.experimental.categories.Category;
032
033@Category({ MetricsTests.class, SmallTests.class })
034public class TestMetricsRegionSourceImpl {
035  @ClassRule
036  public static final HBaseClassTestRule CLASS_RULE =
037    HBaseClassTestRule.forClass(TestMetricsRegionSourceImpl.class);
038
039  @SuppressWarnings("SelfComparison")
040  @Test
041  public void testCompareToHashCodeEquals() {
042    MetricsRegionServerSourceFactory fact =
043      CompatibilitySingletonFactory.getInstance(MetricsRegionServerSourceFactory.class);
044
045    MetricsRegionSource one = fact.createRegion(new RegionWrapperStub("TEST"));
046    MetricsRegionSource oneClone = fact.createRegion(new RegionWrapperStub("TEST"));
047    MetricsRegionSource two = fact.createRegion(new RegionWrapperStub("TWO"));
048
049    assertEquals(0, one.compareTo(oneClone));
050    assertEquals(one.hashCode(), oneClone.hashCode());
051    assertNotEquals(one, two);
052
053    assertNotEquals(0, one.compareTo(two));
054    assertNotEquals(0, two.compareTo(one));
055    assertNotEquals(one.compareTo(two), two.compareTo(one));
056    assertEquals(0, two.compareTo(two));
057  }
058
059  @Test(expected = RuntimeException.class)
060  public void testNoGetRegionServerMetricsSourceImpl() {
061    // This should throw an exception because MetricsRegionSourceImpl should only
062    // be created by a factory.
063    CompatibilitySingletonFactory.getInstance(MetricsRegionSource.class);
064  }
065
066  static class RegionWrapperStub implements MetricsRegionWrapper {
067
068    private String regionName;
069
070    RegionWrapperStub(String regionName) {
071      this.regionName = regionName;
072    }
073
074    @Override
075    public String getTableName() {
076      return null;
077    }
078
079    @Override
080    public String getNamespace() {
081      return null;
082    }
083
084    @Override
085    public String getRegionName() {
086      return this.regionName;
087    }
088
089    @Override
090    public long getNumStores() {
091      return 0;
092    }
093
094    @Override
095    public long getNumStoreFiles() {
096      return 0;
097    }
098
099    @Override
100    public long getStoreRefCount() {
101      return 0;
102    }
103
104    @Override
105    public long getMaxCompactedStoreFileRefCount() {
106      return 0;
107    }
108
109    @Override
110    public long getMemStoreSize() {
111      return 0;
112    }
113
114    @Override
115    public long getStoreFileSize() {
116      return 0;
117    }
118
119    @Override
120    public float getCurrentRegionCacheRatio() {
121      return 0;
122    }
123
124    @Override
125    public long getReadRequestCount() {
126      return 0;
127    }
128
129    @Override
130    public long getFilteredReadRequestCount() {
131      return 0;
132    }
133
134    @Override
135    public long getMaxStoreFileAge() {
136      return 0;
137    }
138
139    @Override
140    public long getMinStoreFileAge() {
141      return 0;
142    }
143
144    @Override
145    public long getAvgStoreFileAge() {
146      return 0;
147    }
148
149    @Override
150    public long getNumReferenceFiles() {
151      return 0;
152    }
153
154    @Override
155    public long getCpRequestCount() {
156      return 0;
157    }
158
159    @Override
160    public long getWriteRequestCount() {
161      return 0;
162    }
163
164    @Override
165    public long getNumFilesCompacted() {
166      return 0;
167    }
168
169    @Override
170    public long getNumBytesCompacted() {
171      return 0;
172    }
173
174    @Override
175    public long getLastMajorCompactionAge() {
176      return 0;
177    }
178
179    @Override
180    public long getNumCompactionsCompleted() {
181      return 0;
182    }
183
184    @Override
185    public long getNumCompactionsFailed() {
186      return 0;
187    }
188
189    @Override
190    public int getRegionHashCode() {
191      return regionName.hashCode();
192    }
193
194    /**
195     * Always return 0 for testing
196     */
197    @Override
198    public int getReplicaId() {
199      return 0;
200    }
201
202    @Override
203    public long getNumCompactionsQueued() {
204      return 0;
205    }
206
207    @Override
208    public long getNumFlushesQueued() {
209      return 0;
210    }
211
212    @Override
213    public long getMaxCompactionQueueSize() {
214      return 0;
215    }
216
217    @Override
218    public long getMaxFlushQueueSize() {
219      return 0;
220    }
221
222    @Override
223    public long getTotalRequestCount() {
224      return 0;
225    }
226
227    @Override
228    public Map<String, Long> getMemstoreOnlyRowReadsCount() {
229      Map<String, Long> map = new HashMap<String, Long>();
230      map.put("info", 0L);
231      return map;
232    }
233
234    @Override
235    public Map<String, Long> getMixedRowReadsCount() {
236      Map<String, Long> map = new HashMap<String, Long>();
237      map.put("info", 0L);
238      return map;
239    }
240  }
241}