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; 021 022import com.google.protobuf.RpcCallback; 023import com.google.protobuf.RpcController; 024import com.google.protobuf.Service; 025import org.apache.hadoop.conf.Configuration; 026import org.apache.hadoop.hbase.HBaseClassTestRule; 027import org.apache.hadoop.hbase.HBaseTestingUtility; 028import org.apache.hadoop.hbase.TableName; 029import org.apache.hadoop.hbase.coprocessor.protobuf.generated.DummyRegionServerEndpointProtos.*; 030import org.apache.hadoop.hbase.testclassification.SmallTests; 031import org.apache.hadoop.hbase.util.Bytes; 032import org.junit.AfterClass; 033import org.junit.BeforeClass; 034import org.junit.ClassRule; 035import org.junit.Test; 036import org.junit.experimental.categories.Category; 037 038/** 039 * Tests to ensure that 2.0 is backward compatible in loading CoprocessorService. 040 */ 041@Category({SmallTests.class}) 042public class TestCoprocessorServiceBackwardCompatibility { 043 044 @ClassRule 045 public static final HBaseClassTestRule CLASS_RULE = 046 HBaseClassTestRule.forClass(TestCoprocessorServiceBackwardCompatibility.class); 047 048 private static HBaseTestingUtility TEST_UTIL = null; 049 private static Configuration CONF = null; 050 051 private static final long MASTER = 1; 052 private static final long REGIONSERVER = 2; 053 private static final long REGION = 3; 054 055 public static class DummyCoprocessorService extends DummyService 056 implements CoprocessorService, SingletonCoprocessorService { 057 // depending on the value passed thru DummyRequest, the following fields would be incremented 058 // value == MASTER 059 static int numMaster = 0; 060 // value == REGIONSERVER 061 static int numRegionServer = 0; 062 // value == REGION 063 static int numRegion = 0; 064 065 @Override 066 public Service getService() { 067 return this; 068 } 069 070 @Override 071 public void dummyCall(RpcController controller, DummyRequest request, 072 RpcCallback<DummyResponse> callback) { 073 callback.run(DummyResponse.newBuilder().setValue("").build()); 074 if (request.getValue() == MASTER) { 075 numMaster += request.getValue(); 076 } else if (request.getValue() == REGIONSERVER) { 077 numRegionServer += request.getValue(); 078 } else if (request.getValue() == REGION) { 079 numRegion += request.getValue(); 080 } 081 } 082 083 @Override 084 public void dummyThrow(RpcController controller, DummyRequest request, 085 RpcCallback<DummyResponse> callback) { 086 } 087 } 088 089 @BeforeClass 090 public static void setupBeforeClass() throws Exception { 091 TEST_UTIL = new HBaseTestingUtility(); 092 CONF = TEST_UTIL.getConfiguration(); 093 CONF.setStrings(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY, 094 DummyCoprocessorService.class.getName()); 095 CONF.setStrings(CoprocessorHost.REGIONSERVER_COPROCESSOR_CONF_KEY, 096 DummyCoprocessorService.class.getName()); 097 CONF.setStrings(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY, 098 DummyCoprocessorService.class.getName()); 099 TEST_UTIL.startMiniCluster(); 100 } 101 102 @AfterClass 103 public static void tearDownAfter() throws Exception { 104 TEST_UTIL.shutdownMiniCluster(); 105 } 106 107 @Test 108 public void testCoprocessorServiceLoadedByMaster() throws Throwable { 109 TEST_UTIL.getAdmin().coprocessorService().callBlockingMethod( 110 DummyCoprocessorService.getDescriptor().findMethodByName("dummyCall"), null, 111 DummyRequest.newBuilder().setValue(MASTER).build(), DummyResponse.getDefaultInstance()); 112 assertEquals(MASTER, DummyCoprocessorService.numMaster); 113 114 TEST_UTIL.getAdmin().coprocessorService( 115 TEST_UTIL.getHBaseCluster().getRegionServer(0).getServerName()).callBlockingMethod( 116 DummyCoprocessorService.getDescriptor().findMethodByName("dummyCall"), null, 117 DummyRequest.newBuilder().setValue(REGIONSERVER).build(), 118 DummyResponse.getDefaultInstance()); 119 assertEquals(REGIONSERVER, DummyCoprocessorService.numRegionServer); 120 121 TEST_UTIL.getConnection().getTable(TableName.valueOf("hbase:meta")).batchCoprocessorService( 122 DummyCoprocessorService.getDescriptor().findMethodByName("dummyCall"), 123 DummyRequest.newBuilder().setValue(REGION).build(), Bytes.toBytes(""), Bytes.toBytes(""), 124 DummyResponse.getDefaultInstance()); 125 assertEquals(REGION, DummyCoprocessorService.numRegion); 126 } 127}