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.mapreduce; 019 020import static org.junit.jupiter.api.Assertions.assertArrayEquals; 021import static org.junit.jupiter.api.Assertions.assertEquals; 022import static org.mockito.ArgumentMatchers.any; 023import static org.mockito.ArgumentMatchers.anyBoolean; 024import static org.mockito.Mockito.mock; 025import static org.mockito.Mockito.when; 026 027import java.io.IOException; 028import java.net.Inet6Address; 029import java.net.InetAddress; 030import java.net.UnknownHostException; 031import java.util.List; 032import java.util.Map; 033import java.util.TreeMap; 034import java.util.concurrent.ExecutorService; 035import org.apache.hadoop.conf.Configuration; 036import org.apache.hadoop.hbase.HBaseConfiguration; 037import org.apache.hadoop.hbase.HConstants; 038import org.apache.hadoop.hbase.HRegionLocation; 039import org.apache.hadoop.hbase.ServerName; 040import org.apache.hadoop.hbase.TableName; 041import org.apache.hadoop.hbase.client.Admin; 042import org.apache.hadoop.hbase.client.AsyncConnection; 043import org.apache.hadoop.hbase.client.BufferedMutator; 044import org.apache.hadoop.hbase.client.BufferedMutatorParams; 045import org.apache.hadoop.hbase.client.Connection; 046import org.apache.hadoop.hbase.client.ConnectionRegistry; 047import org.apache.hadoop.hbase.client.ConnectionUtils; 048import org.apache.hadoop.hbase.client.RegionInfo; 049import org.apache.hadoop.hbase.client.RegionInfoBuilder; 050import org.apache.hadoop.hbase.client.RegionLocator; 051import org.apache.hadoop.hbase.client.Table; 052import org.apache.hadoop.hbase.client.TableBuilder; 053import org.apache.hadoop.hbase.security.User; 054import org.apache.hadoop.hbase.testclassification.SmallTests; 055import org.apache.hadoop.hbase.util.Bytes; 056import org.apache.hadoop.hbase.util.Pair; 057import org.apache.hadoop.mapreduce.InputSplit; 058import org.apache.hadoop.mapreduce.JobContext; 059import org.junit.jupiter.api.Tag; 060import org.junit.jupiter.api.Test; 061import org.mockito.Mockito; 062import org.mockito.invocation.InvocationOnMock; 063import org.mockito.stubbing.Answer; 064 065@Tag(SmallTests.TAG) 066public class TestTableInputFormatBase { 067 068 @Test 069 public void testCreateNInputSplitsUniformPreservesOriginalBoundaries() throws IOException { 070 TableInputFormat inputFormat = new TableInputFormat(); 071 for (byte[] startRow : new byte[][] { HConstants.EMPTY_START_ROW, Bytes.toBytes("start") }) { 072 TableSplit split = 073 new TableSplit(TableName.valueOf("test"), startRow, HConstants.EMPTY_END_ROW, "localhost"); 074 075 List<InputSplit> splits = inputFormat.createNInputSplitsUniform(split, 2); 076 077 assertEquals(2, splits.size()); 078 TableSplit first = (TableSplit) splits.get(0); 079 TableSplit last = (TableSplit) splits.get(1); 080 assertArrayEquals(startRow, first.getStartRow()); 081 assertArrayEquals(first.getEndRow(), last.getStartRow()); 082 assertArrayEquals(HConstants.EMPTY_END_ROW, last.getEndRow()); 083 } 084 } 085 086 @Test 087 public void testReuseRegionSizeCalculator() throws IOException { 088 JobContext context = mock(JobContext.class); 089 Configuration conf = HBaseConfiguration.create(); 090 conf.set(ConnectionUtils.HBASE_CLIENT_CONNECTION_IMPL, 091 ConnectionForMergeTesting.class.getName()); 092 conf.set(TableInputFormat.INPUT_TABLE, "testTable"); 093 conf.setBoolean(TableInputFormatBase.MAPREDUCE_INPUT_AUTOBALANCE, true); 094 when(context.getConfiguration()).thenReturn(conf); 095 096 TableInputFormat format = Mockito.spy(new TableInputFormatForMergeTesting()); 097 format.setConf(conf); 098 // initialize so that table is set, otherwise cloneOnFinish 099 // will be true and each getSplits call will re-initialize everything 100 format.initialize(context); 101 format.getSplits(context); 102 format.getSplits(context); 103 104 // re-initialize which will cause the next getSplits call to create a new RegionSizeCalculator 105 format.initialize(context); 106 format.getSplits(context); 107 format.getSplits(context); 108 109 // should only be 2 despite calling getSplits 4 times 110 Mockito.verify(format, Mockito.times(2)).createRegionSizeCalculator(Mockito.any(), 111 Mockito.any()); 112 } 113 114 @Test 115 public void testTableInputFormatBaseReverseDNSForIPv6() throws UnknownHostException { 116 String address = "ipv6.google.com"; 117 String localhost = null; 118 InetAddress addr = null; 119 TableInputFormat inputFormat = new TableInputFormat(); 120 try { 121 localhost = InetAddress.getByName(address).getCanonicalHostName(); 122 addr = Inet6Address.getByName(address); 123 } catch (UnknownHostException e) { 124 // google.com is down, we can probably forgive this test. 125 return; 126 } 127 System.out.println("Should retrun the hostname for this host " + localhost + " addr : " + addr); 128 String actualHostName = inputFormat.reverseDNS(addr); 129 assertEquals("Should retrun the hostname for this host. Expected : " + localhost + " Actual : " 130 + actualHostName, localhost, actualHostName); 131 } 132 133 @Test 134 public void testNonSuccessiveSplitsAreNotMerged() throws IOException { 135 JobContext context = mock(JobContext.class); 136 Configuration conf = HBaseConfiguration.create(); 137 conf.set(ConnectionUtils.HBASE_CLIENT_CONNECTION_IMPL, 138 ConnectionForMergeTesting.class.getName()); 139 conf.set(TableInputFormat.INPUT_TABLE, "testTable"); 140 conf.setBoolean(TableInputFormatBase.MAPREDUCE_INPUT_AUTOBALANCE, true); 141 when(context.getConfiguration()).thenReturn(conf); 142 143 TableInputFormat tifExclude = new TableInputFormatForMergeTesting(); 144 tifExclude.setConf(conf); 145 // split["b", "c"] is excluded, split["o", "p"] and split["p", "q"] are merged, 146 // but split["a", "b"] and split["c", "d"] are not merged. 147 assertEquals(ConnectionForMergeTesting.START_KEYS.length - 1 - 1, 148 tifExclude.getSplits(context).size()); 149 } 150 151 /** 152 * Subclass of {@link TableInputFormat} to use in {@link #testNonSuccessiveSplitsAreNotMerged}. 153 * This class overrides {@link TableInputFormatBase#includeRegionInSplit} to exclude specific 154 * splits. 155 */ 156 private static class TableInputFormatForMergeTesting extends TableInputFormat { 157 private byte[] prefixStartKey = Bytes.toBytes("b"); 158 private byte[] prefixEndKey = Bytes.toBytes("c"); 159 private RegionSizeCalculator sizeCalculator; 160 161 /** 162 * Exclude regions which contain rows starting with "b". 163 */ 164 @Override 165 protected boolean includeRegionInSplit(final byte[] startKey, final byte[] endKey) { 166 if ( 167 Bytes.compareTo(startKey, prefixEndKey) < 0 && (Bytes.compareTo(prefixStartKey, endKey) < 0 168 || Bytes.equals(endKey, HConstants.EMPTY_END_ROW)) 169 ) { 170 return false; 171 } else { 172 return true; 173 } 174 } 175 176 @Override 177 protected void initializeTable(Connection connection, TableName tableName) throws IOException { 178 super.initializeTable(connection, tableName); 179 ConnectionForMergeTesting cft = (ConnectionForMergeTesting) connection; 180 sizeCalculator = cft.getRegionSizeCalculator(); 181 } 182 183 @Override 184 protected RegionSizeCalculator createRegionSizeCalculator(RegionLocator locator, Admin admin) 185 throws IOException { 186 return sizeCalculator; 187 } 188 } 189 190 /** 191 * Connection class to use in {@link #testNonSuccessiveSplitsAreNotMerged}. This class returns 192 * mocked {@link Table}, {@link RegionLocator}, {@link RegionSizeCalculator}, and {@link Admin}. 193 */ 194 private static class ConnectionForMergeTesting implements Connection { 195 public static final byte[][] SPLITS = new byte[][] { Bytes.toBytes("a"), Bytes.toBytes("b"), 196 Bytes.toBytes("c"), Bytes.toBytes("d"), Bytes.toBytes("e"), Bytes.toBytes("f"), 197 Bytes.toBytes("g"), Bytes.toBytes("h"), Bytes.toBytes("i"), Bytes.toBytes("j"), 198 Bytes.toBytes("k"), Bytes.toBytes("l"), Bytes.toBytes("m"), Bytes.toBytes("n"), 199 Bytes.toBytes("o"), Bytes.toBytes("p"), Bytes.toBytes("q"), Bytes.toBytes("r"), 200 Bytes.toBytes("s"), Bytes.toBytes("t"), Bytes.toBytes("u"), Bytes.toBytes("v"), 201 Bytes.toBytes("w"), Bytes.toBytes("x"), Bytes.toBytes("y"), Bytes.toBytes("z") }; 202 203 public static final byte[][] START_KEYS; 204 public static final byte[][] END_KEYS; 205 static { 206 START_KEYS = new byte[SPLITS.length + 1][]; 207 START_KEYS[0] = HConstants.EMPTY_BYTE_ARRAY; 208 for (int i = 0; i < SPLITS.length; i++) { 209 START_KEYS[i + 1] = SPLITS[i]; 210 } 211 212 END_KEYS = new byte[SPLITS.length + 1][]; 213 for (int i = 0; i < SPLITS.length; i++) { 214 END_KEYS[i] = SPLITS[i]; 215 } 216 END_KEYS[SPLITS.length] = HConstants.EMPTY_BYTE_ARRAY; 217 } 218 219 public static final Map<byte[], Long> SIZE_MAP = new TreeMap<>(Bytes.BYTES_COMPARATOR); 220 static { 221 for (byte[] startKey : START_KEYS) { 222 SIZE_MAP.put(startKey, 1024L * 1024L * 1024L); 223 } 224 SIZE_MAP.put(Bytes.toBytes("a"), 200L * 1024L * 1024L); 225 SIZE_MAP.put(Bytes.toBytes("b"), 200L * 1024L * 1024L); 226 SIZE_MAP.put(Bytes.toBytes("c"), 200L * 1024L * 1024L); 227 SIZE_MAP.put(Bytes.toBytes("o"), 200L * 1024L * 1024L); 228 SIZE_MAP.put(Bytes.toBytes("p"), 200L * 1024L * 1024L); 229 } 230 231 ConnectionForMergeTesting(Configuration conf, ExecutorService pool, User user, 232 ConnectionRegistry registry, Map<String, byte[]> connectionAttributes) throws IOException { 233 } 234 235 @Override 236 public void abort(String why, Throwable e) { 237 } 238 239 @Override 240 public boolean isAborted() { 241 return false; 242 } 243 244 @Override 245 public Configuration getConfiguration() { 246 throw new UnsupportedOperationException(); 247 } 248 249 @Override 250 public Table getTable(TableName tableName) throws IOException { 251 Table table = mock(Table.class); 252 when(table.getName()).thenReturn(tableName); 253 return table; 254 } 255 256 @Override 257 public Table getTable(TableName tableName, ExecutorService pool) throws IOException { 258 throw new UnsupportedOperationException(); 259 } 260 261 @Override 262 public BufferedMutator getBufferedMutator(TableName tableName) throws IOException { 263 throw new UnsupportedOperationException(); 264 } 265 266 @Override 267 public BufferedMutator getBufferedMutator(BufferedMutatorParams params) throws IOException { 268 throw new UnsupportedOperationException(); 269 } 270 271 @Override 272 public RegionLocator getRegionLocator(TableName tableName) throws IOException { 273 final Map<byte[], HRegionLocation> locationMap = new TreeMap<>(Bytes.BYTES_COMPARATOR); 274 for (byte[] startKey : START_KEYS) { 275 HRegionLocation hrl = 276 new HRegionLocation(RegionInfoBuilder.newBuilder(tableName).setStartKey(startKey).build(), 277 ServerName.valueOf("localhost", 0, 0)); 278 locationMap.put(startKey, hrl); 279 } 280 281 RegionLocator locator = mock(RegionLocator.class); 282 when(locator.getRegionLocation(any(byte[].class), anyBoolean())) 283 .thenAnswer(new Answer<HRegionLocation>() { 284 @Override 285 public HRegionLocation answer(InvocationOnMock invocationOnMock) throws Throwable { 286 Object[] args = invocationOnMock.getArguments(); 287 byte[] key = (byte[]) args[0]; 288 return locationMap.get(key); 289 } 290 }); 291 when(locator.getStartEndKeys()) 292 .thenReturn(new Pair<byte[][], byte[][]>(START_KEYS, END_KEYS)); 293 return locator; 294 } 295 296 public RegionSizeCalculator getRegionSizeCalculator() { 297 RegionSizeCalculator sizeCalculator = mock(RegionSizeCalculator.class); 298 when(sizeCalculator.getRegionSize(any(byte[].class))).thenAnswer(new Answer<Long>() { 299 @Override 300 public Long answer(InvocationOnMock invocationOnMock) throws Throwable { 301 Object[] args = invocationOnMock.getArguments(); 302 byte[] regionId = (byte[]) args[0]; 303 byte[] startKey = RegionInfo.getStartKey(regionId); 304 return SIZE_MAP.get(startKey); 305 } 306 }); 307 return sizeCalculator; 308 } 309 310 @Override 311 public Admin getAdmin() throws IOException { 312 Admin admin = mock(Admin.class); 313 // return non-null admin to pass null checks 314 return admin; 315 } 316 317 @Override 318 public void close() throws IOException { 319 } 320 321 @Override 322 public boolean isClosed() { 323 return false; 324 } 325 326 @Override 327 public TableBuilder getTableBuilder(TableName tableName, ExecutorService pool) { 328 throw new UnsupportedOperationException(); 329 } 330 331 @Override 332 public void clearRegionLocationCache() { 333 } 334 335 @Override 336 public AsyncConnection toAsyncConnection() { 337 throw new UnsupportedOperationException(); 338 } 339 340 @Override 341 public String getClusterId() { 342 return null; 343 } 344 } 345}