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