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 long getReadRequestCount() { 121 return 0; 122 } 123 124 @Override 125 public long getFilteredReadRequestCount() { 126 return 0; 127 } 128 129 @Override 130 public long getMaxStoreFileAge() { 131 return 0; 132 } 133 134 @Override 135 public long getMinStoreFileAge() { 136 return 0; 137 } 138 139 @Override 140 public long getAvgStoreFileAge() { 141 return 0; 142 } 143 144 @Override 145 public long getNumReferenceFiles() { 146 return 0; 147 } 148 149 @Override 150 public long getWriteRequestCount() { 151 return 0; 152 } 153 154 @Override 155 public long getNumFilesCompacted() { 156 return 0; 157 } 158 159 @Override 160 public long getNumBytesCompacted() { 161 return 0; 162 } 163 164 @Override 165 public long getLastMajorCompactionAge() { 166 return 0; 167 } 168 169 @Override 170 public long getNumCompactionsCompleted() { 171 return 0; 172 } 173 174 @Override 175 public long getNumCompactionsFailed() { 176 return 0; 177 } 178 179 @Override 180 public int getRegionHashCode() { 181 return regionName.hashCode(); 182 } 183 184 /** 185 * Always return 0 for testing 186 */ 187 @Override 188 public int getReplicaId() { 189 return 0; 190 } 191 192 @Override 193 public long getNumCompactionsQueued() { 194 return 0; 195 } 196 197 @Override 198 public long getNumFlushesQueued() { 199 return 0; 200 } 201 202 @Override 203 public long getMaxCompactionQueueSize() { 204 return 0; 205 } 206 207 @Override 208 public long getMaxFlushQueueSize() { 209 return 0; 210 } 211 212 @Override 213 public long getTotalRequestCount() { 214 return 0; 215 } 216 217 @Override 218 public Map<String, Long> getMemstoreOnlyRowReadsCount() { 219 Map<String, Long> map = new HashMap<String, Long>(); 220 map.put("info", 0L); 221 return map; 222 } 223 224 @Override 225 public Map<String, Long> getMixedRowReadsCount() { 226 Map<String, Long> map = new HashMap<String, Long>(); 227 map.put("info", 0L); 228 return map; 229 } 230 } 231}