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.assertTrue;
021
022import java.io.IOException;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.CoprocessorEnvironment;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseTestingUtility;
027import org.apache.hadoop.hbase.SharedConnection;
028import org.apache.hadoop.hbase.client.Connection;
029import org.apache.hadoop.hbase.client.ServerConnectionUtils;
030import org.apache.hadoop.hbase.testclassification.CoprocessorTests;
031import org.apache.hadoop.hbase.testclassification.MediumTests;
032import org.junit.AfterClass;
033import org.junit.BeforeClass;
034import org.junit.ClassRule;
035import org.junit.Rule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038import org.junit.rules.TestName;
039
040/**
041 * Ensure Coprocessors get ShortCircuit Connections when they get a Connection from their
042 * CoprocessorEnvironment.
043 */
044@Category({ CoprocessorTests.class, MediumTests.class })
045public class TestCoprocessorShortCircuitRPC {
046
047  @ClassRule
048  public static final HBaseClassTestRule CLASS_RULE =
049    HBaseClassTestRule.forClass(TestCoprocessorShortCircuitRPC.class);
050
051  @Rule
052  public TestName name = new TestName();
053  private static final HBaseTestingUtility HTU = HBaseTestingUtility.createLocalHTU();
054
055  /**
056   * Start up a mini cluster with my three CPs loaded.
057   */
058  @BeforeClass
059  public static void beforeClass() throws Exception {
060    // Set my test Coprocessors into the Configuration before we start up the cluster.
061    Configuration conf = HTU.getConfiguration();
062    conf.setStrings(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
063      TestMasterCoprocessor.class.getName());
064    conf.setStrings(CoprocessorHost.REGIONSERVER_COPROCESSOR_CONF_KEY,
065      TestRegionServerCoprocessor.class.getName());
066    conf.setStrings(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
067      TestRegionCoprocessor.class.getName());
068    HTU.startMiniCluster();
069  }
070
071  @AfterClass
072  public static void afterClass() throws Exception {
073    HTU.shutdownMiniCluster();
074  }
075
076  // Three test coprocessors, one of each type that has a Connection in its environment
077  // (WALCoprocessor does not).
078  public static class TestMasterCoprocessor implements MasterCoprocessor {
079    public TestMasterCoprocessor() {
080    }
081
082    @Override
083    public void start(CoprocessorEnvironment env) throws IOException {
084      // At start, we get base CoprocessorEnvironment Type, not MasterCoprocessorEnvironment,
085      checkShared(((MasterCoprocessorEnvironment) env).getConnection());
086      checkShortCircuit(
087        ((MasterCoprocessorEnvironment) env).createConnection(env.getConfiguration()));
088    }
089  }
090
091  public static class TestRegionServerCoprocessor implements RegionServerCoprocessor {
092    public TestRegionServerCoprocessor() {
093    }
094
095    @Override
096    public void start(CoprocessorEnvironment env) throws IOException {
097      // At start, we get base CoprocessorEnvironment Type, not RegionServerCoprocessorEnvironment,
098      checkShared(((RegionServerCoprocessorEnvironment) env).getConnection());
099      checkShortCircuit(
100        ((RegionServerCoprocessorEnvironment) env).createConnection(env.getConfiguration()));
101    }
102  }
103
104  public static class TestRegionCoprocessor implements RegionCoprocessor {
105    public TestRegionCoprocessor() {
106    }
107
108    @Override
109    public void start(CoprocessorEnvironment env) throws IOException {
110      // At start, we get base CoprocessorEnvironment Type, not RegionCoprocessorEnvironment,
111      checkShared(((RegionCoprocessorEnvironment) env).getConnection());
112      checkShortCircuit(
113        ((RegionCoprocessorEnvironment) env).createConnection(env.getConfiguration()));
114    }
115  }
116
117  private static void checkShared(Connection connection) {
118    assertTrue(connection instanceof SharedConnection);
119  }
120
121  private static void checkShortCircuit(Connection connection) {
122    assertTrue(connection instanceof ServerConnectionUtils.ShortCircuitingClusterConnection);
123  }
124
125  @Test
126  public void test() throws IOException {
127    // Nothing to do in here. The checks are done as part of the cluster spinup when CPs get
128    // loaded. Need this here so this class looks like a test.
129  }
130}