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.coprocessor; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertNotNull; 022import static org.junit.Assert.assertNull; 023import static org.junit.Assert.assertTrue; 024import static org.junit.Assert.fail; 025 026import com.google.protobuf.RpcController; 027import com.google.protobuf.ServiceException; 028import java.io.IOException; 029import java.util.Collections; 030import java.util.List; 031import java.util.Map; 032import java.util.TreeMap; 033import org.apache.hadoop.conf.Configuration; 034import org.apache.hadoop.hbase.HBaseClassTestRule; 035import org.apache.hadoop.hbase.HBaseTestingUtility; 036import org.apache.hadoop.hbase.HColumnDescriptor; 037import org.apache.hadoop.hbase.HConstants; 038import org.apache.hadoop.hbase.HRegionInfo; 039import org.apache.hadoop.hbase.HRegionLocation; 040import org.apache.hadoop.hbase.HTableDescriptor; 041import org.apache.hadoop.hbase.TableName; 042import org.apache.hadoop.hbase.client.Admin; 043import org.apache.hadoop.hbase.client.Put; 044import org.apache.hadoop.hbase.client.RegionLocator; 045import org.apache.hadoop.hbase.client.Table; 046import org.apache.hadoop.hbase.client.coprocessor.Batch; 047import org.apache.hadoop.hbase.coprocessor.protobuf.generated.ColumnAggregationProtos; 048import org.apache.hadoop.hbase.ipc.CoprocessorRpcChannel; 049import org.apache.hadoop.hbase.ipc.CoprocessorRpcUtils; 050import org.apache.hadoop.hbase.ipc.ServerRpcController; 051import org.apache.hadoop.hbase.ipc.protobuf.generated.TestProtos; 052import org.apache.hadoop.hbase.ipc.protobuf.generated.TestRpcServiceProtos; 053import org.apache.hadoop.hbase.testclassification.CoprocessorTests; 054import org.apache.hadoop.hbase.testclassification.MediumTests; 055import org.apache.hadoop.hbase.util.ByteStringer; 056import org.apache.hadoop.hbase.util.Bytes; 057import org.junit.AfterClass; 058import org.junit.BeforeClass; 059import org.junit.ClassRule; 060import org.junit.Test; 061import org.junit.experimental.categories.Category; 062import org.slf4j.Logger; 063import org.slf4j.LoggerFactory; 064 065/** 066 * TestEndpoint: test cases to verify coprocessor Endpoint 067 */ 068@Category({CoprocessorTests.class, MediumTests.class}) 069public class TestCoprocessorEndpoint { 070 071 @ClassRule 072 public static final HBaseClassTestRule CLASS_RULE = 073 HBaseClassTestRule.forClass(TestCoprocessorEndpoint.class); 074 075 private static final Logger LOG = LoggerFactory.getLogger(TestCoprocessorEndpoint.class); 076 077 private static final TableName TEST_TABLE = 078 TableName.valueOf("TestCoprocessorEndpoint"); 079 private static final byte[] TEST_FAMILY = Bytes.toBytes("TestFamily"); 080 private static final byte[] TEST_QUALIFIER = Bytes.toBytes("TestQualifier"); 081 private static byte[] ROW = Bytes.toBytes("testRow"); 082 083 private static final int ROWSIZE = 20; 084 private static final int rowSeperator1 = 5; 085 private static final int rowSeperator2 = 12; 086 private static byte[][] ROWS = makeN(ROW, ROWSIZE); 087 088 private static HBaseTestingUtility util = new HBaseTestingUtility(); 089 090 @BeforeClass 091 public static void setupBeforeClass() throws Exception { 092 // set configure to indicate which cp should be loaded 093 Configuration conf = util.getConfiguration(); 094 conf.setInt(HConstants.HBASE_CLIENT_OPERATION_TIMEOUT, 5000); 095 conf.setStrings(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY, 096 org.apache.hadoop.hbase.coprocessor.ColumnAggregationEndpoint.class.getName(), 097 ProtobufCoprocessorService.class.getName()); 098 conf.setStrings(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY, 099 ProtobufCoprocessorService.class.getName()); 100 util.startMiniCluster(2); 101 Admin admin = util.getAdmin(); 102 HTableDescriptor desc = new HTableDescriptor(TEST_TABLE); 103 desc.addFamily(new HColumnDescriptor(TEST_FAMILY)); 104 admin.createTable(desc, new byte[][]{ROWS[rowSeperator1], ROWS[rowSeperator2]}); 105 util.waitUntilAllRegionsAssigned(TEST_TABLE); 106 107 Table table = util.getConnection().getTable(TEST_TABLE); 108 for (int i = 0; i < ROWSIZE; i++) { 109 Put put = new Put(ROWS[i]); 110 put.addColumn(TEST_FAMILY, TEST_QUALIFIER, Bytes.toBytes(i)); 111 table.put(put); 112 } 113 table.close(); 114 } 115 116 @AfterClass 117 public static void tearDownAfterClass() throws Exception { 118 util.shutdownMiniCluster(); 119 } 120 121 private Map<byte [], Long> sum(final Table table, final byte [] family, 122 final byte [] qualifier, final byte [] start, final byte [] end) 123 throws ServiceException, Throwable { 124 return table.coprocessorService(ColumnAggregationProtos.ColumnAggregationService.class, 125 start, end, 126 new Batch.Call<ColumnAggregationProtos.ColumnAggregationService, Long>() { 127 @Override 128 public Long call(ColumnAggregationProtos.ColumnAggregationService instance) 129 throws IOException { 130 CoprocessorRpcUtils.BlockingRpcCallback<ColumnAggregationProtos.SumResponse> rpcCallback = 131 new CoprocessorRpcUtils.BlockingRpcCallback<>(); 132 ColumnAggregationProtos.SumRequest.Builder builder = 133 ColumnAggregationProtos.SumRequest.newBuilder(); 134 builder.setFamily(ByteStringer.wrap(family)); 135 if (qualifier != null && qualifier.length > 0) { 136 builder.setQualifier(ByteStringer.wrap(qualifier)); 137 } 138 instance.sum(null, builder.build(), rpcCallback); 139 return rpcCallback.get().getSum(); 140 } 141 }); 142 } 143 144 @Test 145 public void testAggregation() throws Throwable { 146 Table table = util.getConnection().getTable(TEST_TABLE); 147 Map<byte[], Long> results = sum(table, TEST_FAMILY, TEST_QUALIFIER, 148 ROWS[0], ROWS[ROWS.length-1]); 149 int sumResult = 0; 150 int expectedResult = 0; 151 for (Map.Entry<byte[], Long> e : results.entrySet()) { 152 LOG.info("Got value "+e.getValue()+" for region "+Bytes.toStringBinary(e.getKey())); 153 sumResult += e.getValue(); 154 } 155 for (int i = 0; i < ROWSIZE; i++) { 156 expectedResult += i; 157 } 158 assertEquals("Invalid result", expectedResult, sumResult); 159 160 results.clear(); 161 162 // scan: for region 2 and region 3 163 results = sum(table, TEST_FAMILY, TEST_QUALIFIER, 164 ROWS[rowSeperator1], ROWS[ROWS.length-1]); 165 sumResult = 0; 166 expectedResult = 0; 167 for (Map.Entry<byte[], Long> e : results.entrySet()) { 168 LOG.info("Got value "+e.getValue()+" for region "+Bytes.toStringBinary(e.getKey())); 169 sumResult += e.getValue(); 170 } 171 for (int i = rowSeperator1; i < ROWSIZE; i++) { 172 expectedResult += i; 173 } 174 assertEquals("Invalid result", expectedResult, sumResult); 175 table.close(); 176 } 177 178 @Test 179 public void testCoprocessorService() throws Throwable { 180 Table table = util.getConnection().getTable(TEST_TABLE); 181 182 List<HRegionLocation> regions; 183 try(RegionLocator rl = util.getConnection().getRegionLocator(TEST_TABLE)) { 184 regions = rl.getAllRegionLocations(); 185 } 186 final TestProtos.EchoRequestProto request = 187 TestProtos.EchoRequestProto.newBuilder().setMessage("hello").build(); 188 final Map<byte[], String> results = Collections.synchronizedMap( 189 new TreeMap<byte[], String>(Bytes.BYTES_COMPARATOR)); 190 try { 191 // scan: for all regions 192 final RpcController controller = new ServerRpcController(); 193 table.coprocessorService(TestRpcServiceProtos.TestProtobufRpcProto.class, 194 ROWS[0], ROWS[ROWS.length - 1], 195 new Batch.Call<TestRpcServiceProtos.TestProtobufRpcProto, TestProtos.EchoResponseProto>() { 196 public TestProtos.EchoResponseProto call(TestRpcServiceProtos.TestProtobufRpcProto instance) 197 throws IOException { 198 LOG.debug("Default response is " + TestProtos.EchoRequestProto.getDefaultInstance()); 199 CoprocessorRpcUtils.BlockingRpcCallback<TestProtos.EchoResponseProto> callback = 200 new CoprocessorRpcUtils.BlockingRpcCallback<>(); 201 instance.echo(controller, request, callback); 202 TestProtos.EchoResponseProto response = callback.get(); 203 LOG.debug("Batch.Call returning result " + response); 204 return response; 205 } 206 }, 207 new Batch.Callback<TestProtos.EchoResponseProto>() { 208 public void update(byte[] region, byte[] row, TestProtos.EchoResponseProto result) { 209 assertNotNull(result); 210 assertEquals("hello", result.getMessage()); 211 results.put(region, result.getMessage()); 212 } 213 } 214 ); 215 for (Map.Entry<byte[], String> e : results.entrySet()) { 216 LOG.info("Got value "+e.getValue()+" for region "+Bytes.toStringBinary(e.getKey())); 217 } 218 assertEquals(3, results.size()); 219 for (HRegionLocation info : regions) { 220 LOG.info("Region info is "+info.getRegionInfo().getRegionNameAsString()); 221 assertTrue(results.containsKey(info.getRegionInfo().getRegionName())); 222 } 223 results.clear(); 224 225 // scan: for region 2 and region 3 226 table.coprocessorService(TestRpcServiceProtos.TestProtobufRpcProto.class, 227 ROWS[rowSeperator1], ROWS[ROWS.length - 1], 228 new Batch.Call<TestRpcServiceProtos.TestProtobufRpcProto, TestProtos.EchoResponseProto>() { 229 public TestProtos.EchoResponseProto call(TestRpcServiceProtos.TestProtobufRpcProto instance) 230 throws IOException { 231 LOG.debug("Default response is " + TestProtos.EchoRequestProto.getDefaultInstance()); 232 CoprocessorRpcUtils.BlockingRpcCallback<TestProtos.EchoResponseProto> callback = 233 new CoprocessorRpcUtils.BlockingRpcCallback<>(); 234 instance.echo(controller, request, callback); 235 TestProtos.EchoResponseProto response = callback.get(); 236 LOG.debug("Batch.Call returning result " + response); 237 return response; 238 } 239 }, 240 new Batch.Callback<TestProtos.EchoResponseProto>() { 241 public void update(byte[] region, byte[] row, TestProtos.EchoResponseProto result) { 242 assertNotNull(result); 243 assertEquals("hello", result.getMessage()); 244 results.put(region, result.getMessage()); 245 } 246 } 247 ); 248 for (Map.Entry<byte[], String> e : results.entrySet()) { 249 LOG.info("Got value "+e.getValue()+" for region "+Bytes.toStringBinary(e.getKey())); 250 } 251 assertEquals(2, results.size()); 252 } finally { 253 table.close(); 254 } 255 } 256 257 @Test 258 public void testCoprocessorServiceNullResponse() throws Throwable { 259 Table table = util.getConnection().getTable(TEST_TABLE); 260 List<HRegionLocation> regions; 261 try(RegionLocator rl = util.getConnection().getRegionLocator(TEST_TABLE)) { 262 regions = rl.getAllRegionLocations(); 263 } 264 265 final TestProtos.EchoRequestProto request = 266 TestProtos.EchoRequestProto.newBuilder().setMessage("hello").build(); 267 try { 268 // scan: for all regions 269 final RpcController controller = new ServerRpcController(); 270 // test that null results are supported 271 Map<byte[], String> results = 272 table.coprocessorService(TestRpcServiceProtos.TestProtobufRpcProto.class, 273 ROWS[0], ROWS[ROWS.length - 1], 274 new Batch.Call<TestRpcServiceProtos.TestProtobufRpcProto, String>() { 275 public String call(TestRpcServiceProtos.TestProtobufRpcProto instance) 276 throws IOException { 277 CoprocessorRpcUtils.BlockingRpcCallback<TestProtos.EchoResponseProto> callback = 278 new CoprocessorRpcUtils.BlockingRpcCallback<>(); 279 instance.echo(controller, request, callback); 280 TestProtos.EchoResponseProto response = callback.get(); 281 LOG.debug("Batch.Call got result " + response); 282 return null; 283 } 284 } 285 ); 286 for (Map.Entry<byte[], String> e : results.entrySet()) { 287 LOG.info("Got value "+e.getValue()+" for region "+Bytes.toStringBinary(e.getKey())); 288 } 289 assertEquals(3, results.size()); 290 for (HRegionLocation region : regions) { 291 HRegionInfo info = region.getRegionInfo(); 292 LOG.info("Region info is "+info.getRegionNameAsString()); 293 assertTrue(results.containsKey(info.getRegionName())); 294 assertNull(results.get(info.getRegionName())); 295 } 296 } finally { 297 table.close(); 298 } 299 } 300 301 @Test 302 public void testMasterCoprocessorService() throws Throwable { 303 Admin admin = util.getAdmin(); 304 final TestProtos.EchoRequestProto request = 305 TestProtos.EchoRequestProto.newBuilder().setMessage("hello").build(); 306 TestRpcServiceProtos.TestProtobufRpcProto.BlockingInterface service = 307 TestRpcServiceProtos.TestProtobufRpcProto.newBlockingStub(admin.coprocessorService()); 308 assertEquals("hello", service.echo(null, request).getMessage()); 309 } 310 311 @Test 312 public void testCoprocessorError() throws Exception { 313 Configuration configuration = new Configuration(util.getConfiguration()); 314 // Make it not retry forever 315 configuration.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 1); 316 Table table = util.getConnection().getTable(TEST_TABLE); 317 318 try { 319 CoprocessorRpcChannel protocol = table.coprocessorService(ROWS[0]); 320 321 TestRpcServiceProtos.TestProtobufRpcProto.BlockingInterface service = 322 TestRpcServiceProtos.TestProtobufRpcProto.newBlockingStub(protocol); 323 324 service.error(null, TestProtos.EmptyRequestProto.getDefaultInstance()); 325 fail("Should have thrown an exception"); 326 } catch (ServiceException e) { 327 } finally { 328 table.close(); 329 } 330 } 331 332 @Test 333 public void testMasterCoprocessorError() throws Throwable { 334 Admin admin = util.getAdmin(); 335 TestRpcServiceProtos.TestProtobufRpcProto.BlockingInterface service = 336 TestRpcServiceProtos.TestProtobufRpcProto.newBlockingStub(admin.coprocessorService()); 337 try { 338 service.error(null, TestProtos.EmptyRequestProto.getDefaultInstance()); 339 fail("Should have thrown an exception"); 340 } catch (ServiceException e) { 341 } 342 } 343 344 private static byte[][] makeN(byte[] base, int n) { 345 byte[][] ret = new byte[n][]; 346 for (int i = 0; i < n; i++) { 347 ret[i] = Bytes.add(base, Bytes.toBytes(String.format("%02d", i))); 348 } 349 return ret; 350 } 351 352} 353