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.apache.hadoop.hbase.client.Scan.SCAN_ATTRIBUTES_TABLE_NAME; 021 022import java.io.IOException; 023import java.util.ArrayList; 024import java.util.List; 025import java.util.Map; 026import java.util.TreeMap; 027import java.util.concurrent.ExecutorService; 028import java.util.concurrent.atomic.AtomicInteger; 029import org.apache.hadoop.conf.Configuration; 030import org.apache.hadoop.hbase.HBaseClassTestRule; 031import org.apache.hadoop.hbase.HBaseConfiguration; 032import org.apache.hadoop.hbase.HConstants; 033import org.apache.hadoop.hbase.HRegionLocation; 034import org.apache.hadoop.hbase.ServerName; 035import org.apache.hadoop.hbase.TableName; 036import org.apache.hadoop.hbase.client.Admin; 037import org.apache.hadoop.hbase.client.BufferedMutator; 038import org.apache.hadoop.hbase.client.BufferedMutatorParams; 039import org.apache.hadoop.hbase.client.ClusterConnection; 040import org.apache.hadoop.hbase.client.Connection; 041import org.apache.hadoop.hbase.client.RegionInfoBuilder; 042import org.apache.hadoop.hbase.client.RegionLocator; 043import org.apache.hadoop.hbase.client.Result; 044import org.apache.hadoop.hbase.client.Scan; 045import org.apache.hadoop.hbase.client.Table; 046import org.apache.hadoop.hbase.client.TableBuilder; 047import org.apache.hadoop.hbase.io.ImmutableBytesWritable; 048import org.apache.hadoop.hbase.security.User; 049import org.apache.hadoop.hbase.testclassification.SmallTests; 050import org.apache.hadoop.hbase.util.Bytes; 051import org.apache.hadoop.hbase.util.Pair; 052import org.apache.hadoop.mapreduce.InputSplit; 053import org.apache.hadoop.mapreduce.JobContext; 054import org.apache.hadoop.mapreduce.RecordReader; 055import org.apache.hadoop.mapreduce.TaskAttemptContext; 056import org.junit.Assert; 057import org.junit.ClassRule; 058import org.junit.Rule; 059import org.junit.Test; 060import org.junit.experimental.categories.Category; 061import org.junit.rules.TestName; 062import org.mockito.Mockito; 063import org.mockito.invocation.InvocationOnMock; 064import org.mockito.stubbing.Answer; 065 066/** 067 * Tests of MultiTableInputFormatBase. 068 */ 069@Category({ SmallTests.class }) 070public class TestMultiTableInputFormatBase { 071 072 @ClassRule 073 public static final HBaseClassTestRule CLASS_RULE = 074 HBaseClassTestRule.forClass(TestMultiTableInputFormatBase.class); 075 076 @Rule 077 public final TestName name = new TestName(); 078 079 /** 080 * Test getSplits only puts up one Connection. In past it has put up many Connections. Each 081 * Connection setup comes with a fresh new cache so we have to do fresh hit on hbase:meta. Should 082 * only do one Connection when doing getSplits even if a MultiTableInputFormat. 083 */ 084 @Test 085 public void testMRSplitsConnectionCount() throws IOException { 086 // Make instance of MTIFB. 087 MultiTableInputFormatBase mtif = new MultiTableInputFormatBase() { 088 @Override 089 public RecordReader<ImmutableBytesWritable, Result> createRecordReader(InputSplit split, 090 TaskAttemptContext context) throws IOException, InterruptedException { 091 return super.createRecordReader(split, context); 092 } 093 }; 094 // Pass it a mocked JobContext. Make the JC return our Configuration. 095 // Load the Configuration so it returns our special Connection so we can interpolate 096 // canned responses. 097 JobContext mockedJobContext = Mockito.mock(JobContext.class); 098 Configuration c = HBaseConfiguration.create(); 099 c.set(ClusterConnection.HBASE_CLIENT_CONNECTION_IMPL, MRSplitsConnection.class.getName()); 100 Mockito.when(mockedJobContext.getConfiguration()).thenReturn(c); 101 // Invent a bunch of scans. Have each Scan go against a different table so a good spread. 102 List<Scan> scans = new ArrayList<>(); 103 for (int i = 0; i < 10; i++) { 104 Scan scan = new Scan(); 105 String tableName = this.name.getMethodName() + i; 106 scan.setAttribute(SCAN_ATTRIBUTES_TABLE_NAME, Bytes.toBytes(tableName)); 107 scans.add(scan); 108 } 109 mtif.setScans(scans); 110 // Get splits. Assert that that more than one. 111 List<InputSplit> splits = mtif.getSplits(mockedJobContext); 112 Assert.assertTrue(splits.size() > 0); 113 // Assert only one Connection was made (see the static counter we have in the mocked 114 // Connection MRSplitsConnection Constructor. 115 Assert.assertEquals(1, MRSplitsConnection.creations.get()); 116 } 117 118 /** 119 * Connection to use above in Test. 120 */ 121 public static class MRSplitsConnection implements Connection { 122 private final Configuration configuration; 123 static final AtomicInteger creations = new AtomicInteger(0); 124 125 MRSplitsConnection(Configuration conf, ExecutorService pool, User user, 126 Map<String, byte[]> connectionAttributes) throws IOException { 127 this.configuration = conf; 128 creations.incrementAndGet(); 129 } 130 131 @Override 132 public void abort(String why, Throwable e) { 133 134 } 135 136 @Override 137 public boolean isAborted() { 138 return false; 139 } 140 141 @Override 142 public Configuration getConfiguration() { 143 return this.configuration; 144 } 145 146 @Override 147 public BufferedMutator getBufferedMutator(TableName tableName) throws IOException { 148 return null; 149 } 150 151 @Override 152 public BufferedMutator getBufferedMutator(BufferedMutatorParams params) throws IOException { 153 return null; 154 } 155 156 @Override 157 public RegionLocator getRegionLocator(final TableName tableName) throws IOException { 158 // Make up array of start keys. We start off w/ empty byte array. 159 final byte[][] startKeys = new byte[][] { HConstants.EMPTY_BYTE_ARRAY, Bytes.toBytes("aaaa"), 160 Bytes.toBytes("bbb"), Bytes.toBytes("ccc"), Bytes.toBytes("ddd"), Bytes.toBytes("eee"), 161 Bytes.toBytes("fff"), Bytes.toBytes("ggg"), Bytes.toBytes("hhh"), Bytes.toBytes("iii"), 162 Bytes.toBytes("lll"), Bytes.toBytes("mmm"), Bytes.toBytes("nnn"), Bytes.toBytes("ooo"), 163 Bytes.toBytes("ppp"), Bytes.toBytes("qqq"), Bytes.toBytes("rrr"), Bytes.toBytes("sss"), 164 Bytes.toBytes("ttt"), Bytes.toBytes("uuu"), Bytes.toBytes("vvv"), Bytes.toBytes("zzz") }; 165 // Make an array of end keys. We end with the empty byte array. 166 final byte[][] endKeys = 167 new byte[][] { Bytes.toBytes("aaaa"), Bytes.toBytes("bbb"), Bytes.toBytes("ccc"), 168 Bytes.toBytes("ddd"), Bytes.toBytes("eee"), Bytes.toBytes("fff"), Bytes.toBytes("ggg"), 169 Bytes.toBytes("hhh"), Bytes.toBytes("iii"), Bytes.toBytes("lll"), Bytes.toBytes("mmm"), 170 Bytes.toBytes("nnn"), Bytes.toBytes("ooo"), Bytes.toBytes("ppp"), Bytes.toBytes("qqq"), 171 Bytes.toBytes("rrr"), Bytes.toBytes("sss"), Bytes.toBytes("ttt"), Bytes.toBytes("uuu"), 172 Bytes.toBytes("vvv"), Bytes.toBytes("zzz"), HConstants.EMPTY_BYTE_ARRAY }; 173 // Now make a map of start keys to HRegionLocations. Let the server namber derive from 174 // the start key. 175 final Map<byte[], HRegionLocation> map = 176 new TreeMap<byte[], HRegionLocation>(Bytes.BYTES_COMPARATOR); 177 for (byte[] startKey : startKeys) { 178 HRegionLocation hrl = 179 new HRegionLocation(RegionInfoBuilder.newBuilder(tableName).setStartKey(startKey).build(), 180 ServerName.valueOf(Bytes.toString(startKey), 0, 0)); 181 map.put(startKey, hrl); 182 } 183 // Get a list of the locations. 184 final List<HRegionLocation> locations = new ArrayList<HRegionLocation>(map.values()); 185 // Now make a RegionLocator mock backed by the abpve map and list of locations. 186 RegionLocator mockedRegionLocator = Mockito.mock(RegionLocator.class); 187 Mockito 188 .when( 189 mockedRegionLocator.getRegionLocation(Mockito.any(byte[].class), Mockito.anyBoolean())) 190 .thenAnswer(new Answer<HRegionLocation>() { 191 @Override 192 public HRegionLocation answer(InvocationOnMock invocationOnMock) throws Throwable { 193 Object[] args = invocationOnMock.getArguments(); 194 byte[] key = (byte[]) args[0]; 195 return map.get(key); 196 } 197 }); 198 Mockito.when(mockedRegionLocator.getAllRegionLocations()).thenReturn(locations); 199 Mockito.when(mockedRegionLocator.getStartEndKeys()) 200 .thenReturn(new Pair<byte[][], byte[][]>(startKeys, endKeys)); 201 Mockito.when(mockedRegionLocator.getName()).thenReturn(tableName); 202 return mockedRegionLocator; 203 } 204 205 @Override 206 public Admin getAdmin() throws IOException { 207 Admin admin = Mockito.mock(Admin.class); 208 Mockito.when(admin.getConfiguration()).thenReturn(getConfiguration()); 209 return admin; 210 } 211 212 @Override 213 public Table getTable(TableName tableName) throws IOException { 214 Table table = Mockito.mock(Table.class); 215 Mockito.when(table.getName()).thenReturn(tableName); 216 return table; 217 } 218 219 @Override 220 public void close() throws IOException { 221 222 } 223 224 @Override 225 public boolean isClosed() { 226 return false; 227 } 228 229 @Override 230 public TableBuilder getTableBuilder(TableName tableName, ExecutorService pool) { 231 return Mockito.mock(TableBuilder.class); 232 } 233 234 @Override 235 public void clearRegionLocationCache() { 236 } 237 238 @Override 239 public String getClusterId() { 240 return null; 241 } 242 } 243}