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