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