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.fs.FileSystem;
025import org.apache.hadoop.fs.Path;
026import org.apache.hadoop.hbase.CoprocessorEnvironment;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseTestingUtility;
029import org.apache.hadoop.hbase.MiniHBaseCluster;
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.Test;
036import org.junit.experimental.categories.Category;
037import org.slf4j.Logger;
038import org.slf4j.LoggerFactory;
039
040/**
041 * Tests for master and regionserver coprocessor stop method
042 *
043 */
044@Category({CoprocessorTests.class, MediumTests.class})
045public class TestCoprocessorStop {
046
047  @ClassRule
048  public static final HBaseClassTestRule CLASS_RULE =
049      HBaseClassTestRule.forClass(TestCoprocessorStop.class);
050
051  private static final Logger LOG = LoggerFactory.getLogger(TestCoprocessorStop.class);
052  private static HBaseTestingUtility UTIL = new HBaseTestingUtility();
053  private static final String MASTER_FILE =
054                              "master" + System.currentTimeMillis();
055  private static final String REGIONSERVER_FILE =
056                              "regionserver" + System.currentTimeMillis();
057
058  public static class FooCoprocessor implements MasterCoprocessor, RegionServerCoprocessor {
059    @Override
060    public void start(CoprocessorEnvironment env) throws IOException {
061      String where = null;
062
063      if (env instanceof MasterCoprocessorEnvironment) {
064        // if running on HMaster
065        where = "master";
066      } else if (env instanceof RegionServerCoprocessorEnvironment) {
067        where = "regionserver";
068      } else if (env instanceof RegionCoprocessorEnvironment) {
069        LOG.error("on RegionCoprocessorEnvironment!!");
070      }
071      LOG.info("start coprocessor on " + where);
072    }
073
074    @Override
075    public void stop(CoprocessorEnvironment env) throws IOException {
076      String fileName = null;
077
078      if (env instanceof MasterCoprocessorEnvironment) {
079        // if running on HMaster
080        fileName = MASTER_FILE;
081      } else if (env instanceof RegionServerCoprocessorEnvironment) {
082        fileName = REGIONSERVER_FILE;
083      } else if (env instanceof RegionCoprocessorEnvironment) {
084        LOG.error("on RegionCoprocessorEnvironment!!");
085      }
086
087      Configuration conf = UTIL.getConfiguration();
088      Path resultFile = new Path(UTIL.getDataTestDirOnTestFS(), fileName);
089      FileSystem fs = FileSystem.get(conf);
090
091      boolean result = fs.createNewFile(resultFile);
092      LOG.info("create file " + resultFile + " return rc " + result);
093    }
094  }
095
096  @BeforeClass
097  public static void setupBeforeClass() throws Exception {
098    Configuration conf = UTIL.getConfiguration();
099
100    conf.set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
101      FooCoprocessor.class.getName());
102    conf.set(CoprocessorHost.REGIONSERVER_COPROCESSOR_CONF_KEY,
103      FooCoprocessor.class.getName());
104
105    UTIL.startMiniCluster();
106  }
107
108  @AfterClass
109  public static void tearDownAfterClass() throws Exception {
110    UTIL.shutdownMiniCluster();
111  }
112
113  @Test
114  public void testStopped() throws Exception {
115    //shutdown hbase only. then check flag file.
116    MiniHBaseCluster cluster = UTIL.getHBaseCluster();
117    LOG.info("shutdown hbase cluster...");
118    cluster.shutdown();
119    LOG.info("wait for the hbase cluster shutdown...");
120    cluster.waitUntilShutDown();
121
122    Configuration conf = UTIL.getConfiguration();
123    FileSystem fs = FileSystem.get(conf);
124
125    Path resultFile = new Path(UTIL.getDataTestDirOnTestFS(), MASTER_FILE);
126    assertTrue("Master flag file should have been created",fs.exists(resultFile));
127
128    resultFile = new Path(UTIL.getDataTestDirOnTestFS(), REGIONSERVER_FILE);
129    assertTrue("RegionServer flag file should have been created",fs.exists(resultFile));
130  }
131}