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