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