View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.coprocessor;
21  
22  import java.io.IOException;
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.*;
27  import org.apache.hadoop.fs.Path;
28  import org.apache.hadoop.fs.FileSystem;
29  
30  import org.apache.hadoop.hbase.testclassification.MediumTests;
31  import org.junit.AfterClass;
32  import org.junit.BeforeClass;
33  import org.junit.Test;
34  import org.junit.experimental.categories.Category;
35  import static org.junit.Assert.assertTrue;
36  
37  
38  /**
39   * Tests for master and regionserver coprocessor stop method
40   *
41   */
42  @Category(MediumTests.class)
43  public class TestCoprocessorStop {
44    private static final Log LOG = LogFactory.getLog(TestCoprocessorStop.class);
45    private static HBaseTestingUtility UTIL = new HBaseTestingUtility();
46    private static final String MASTER_FILE =
47                                "master" + System.currentTimeMillis();
48    private static final String REGIONSERVER_FILE =
49                                "regionserver" + System.currentTimeMillis();
50  
51    public static class FooCoprocessor implements Coprocessor {
52      @Override
53      public void start(CoprocessorEnvironment env) throws IOException {
54        String where = null;
55  
56        if (env instanceof MasterCoprocessorEnvironment) {
57          // if running on HMaster
58          where = "master";
59        } else if (env instanceof RegionServerCoprocessorEnvironment) {
60          where = "regionserver";
61        } else if (env instanceof RegionCoprocessorEnvironment) {
62          LOG.error("on RegionCoprocessorEnvironment!!");
63        }
64        LOG.info("start coprocessor on " + where);
65      }
66  
67      @Override
68      public void stop(CoprocessorEnvironment env) throws IOException {
69        String fileName = null;
70  
71        if (env instanceof MasterCoprocessorEnvironment) {
72          // if running on HMaster
73          fileName = MASTER_FILE;
74        } else if (env instanceof RegionServerCoprocessorEnvironment) {
75          fileName = REGIONSERVER_FILE;
76        } else if (env instanceof RegionCoprocessorEnvironment) {
77          LOG.error("on RegionCoprocessorEnvironment!!");
78        }
79  
80        Configuration conf = UTIL.getConfiguration();
81        Path resultFile = new Path(UTIL.getDataTestDirOnTestFS(), fileName);
82        FileSystem fs = FileSystem.get(conf);
83  
84        boolean result = fs.createNewFile(resultFile);
85        LOG.info("create file " + resultFile + " return rc " + result);
86      }
87    }
88  
89    @BeforeClass
90    public static void setupBeforeClass() throws Exception {
91      Configuration conf = UTIL.getConfiguration();
92  
93      conf.set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
94        FooCoprocessor.class.getName());
95      conf.set(CoprocessorHost.REGIONSERVER_COPROCESSOR_CONF_KEY,
96        FooCoprocessor.class.getName());
97  
98      UTIL.startMiniCluster();
99    }
100 
101   @AfterClass
102   public static void tearDownAfterClass() throws Exception {
103     UTIL.shutdownMiniCluster();
104   }
105 
106   @Test
107   public void testStopped() throws Exception {
108     //shutdown hbase only. then check flag file.
109     MiniHBaseCluster cluster = UTIL.getHBaseCluster();
110     LOG.info("shutdown hbase cluster...");
111     cluster.shutdown();
112     LOG.info("wait for the hbase cluster shutdown...");
113     cluster.waitUntilShutDown();
114 
115     Configuration conf = UTIL.getConfiguration();
116     FileSystem fs = FileSystem.get(conf);
117 
118     Path resultFile = new Path(UTIL.getDataTestDirOnTestFS(), MASTER_FILE);
119     assertTrue("Master flag file should have been created",fs.exists(resultFile));
120 
121     resultFile = new Path(UTIL.getDataTestDirOnTestFS(), REGIONSERVER_FILE);
122     assertTrue("RegionServer flag file should have been created",fs.exists(resultFile));
123   }
124 }