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