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.hbase.master.HMaster;
28  import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
29  import org.apache.hadoop.fs.Path;
30  import org.apache.hadoop.fs.FileSystem;
31  
32  import org.junit.AfterClass;
33  import org.junit.BeforeClass;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  import static org.junit.Assert.assertTrue;
37  
38  
39  /**
40   * Tests for master and regionserver coprocessor stop method
41   *
42   */
43  @Category(MediumTests.class)
44  public class TestCoprocessorStop {
45    private static final Log LOG = LogFactory.getLog(TestCoprocessorStop.class);
46    private static HBaseTestingUtility UTIL = new HBaseTestingUtility();
47    private static final String MASTER_FILE =
48                                "master" + System.currentTimeMillis();
49    private static final String REGIONSERVER_FILE =
50                                "regionserver" + System.currentTimeMillis();
51  
52    public static class FooCoprocessor implements Coprocessor {
53      @Override
54      public void start(CoprocessorEnvironment env) throws IOException {
55        String where = null;
56  
57        if (env instanceof MasterCoprocessorEnvironment) {
58          // if running on HMaster
59          where = "master";
60        } else if (env instanceof RegionServerCoprocessorEnvironment) {
61          where = "regionserver";
62        } else if (env instanceof RegionCoprocessorEnvironment) {
63          LOG.error("on RegionCoprocessorEnvironment!!");
64        }
65        LOG.info("start coprocessor on " + where);
66      }
67  
68      @Override
69      public void stop(CoprocessorEnvironment env) throws IOException {
70        String fileName = null;
71  
72        if (env instanceof MasterCoprocessorEnvironment) {
73          // if running on HMaster
74          fileName = MASTER_FILE;
75        } else if (env instanceof RegionServerCoprocessorEnvironment) {
76          fileName = REGIONSERVER_FILE;
77        } else if (env instanceof RegionCoprocessorEnvironment) {
78          LOG.error("on RegionCoprocessorEnvironment!!");
79        }
80  
81        Configuration conf = UTIL.getConfiguration();
82        Path resultFile = new Path(UTIL.getDataTestDirOnTestFS(), fileName);
83        FileSystem fs = FileSystem.get(conf);
84  
85        boolean result = fs.createNewFile(resultFile);
86        LOG.info("create file " + resultFile + " return rc " + result);
87      }
88    }
89  
90    @BeforeClass
91    public static void setupBeforeClass() throws Exception {
92      Configuration conf = UTIL.getConfiguration();
93  
94      conf.set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
95        FooCoprocessor.class.getName());
96      conf.set(CoprocessorHost.REGIONSERVER_COPROCESSOR_CONF_KEY,
97        FooCoprocessor.class.getName());
98  
99      UTIL.startMiniCluster();
100   }
101 
102   @AfterClass
103   public static void tearDownAfterClass() throws Exception {
104     UTIL.shutdownMiniCluster();
105   }
106 
107   @Test
108   public void testStopped() throws Exception {
109     //shutdown hbase only. then check flag file.
110     MiniHBaseCluster cluster = UTIL.getHBaseCluster();
111     LOG.info("shutdown hbase cluster...");
112     cluster.shutdown();
113     LOG.info("wait for the hbase cluster shutdown...");
114     cluster.waitUntilShutDown();
115 
116     Configuration conf = UTIL.getConfiguration();
117     FileSystem fs = FileSystem.get(conf);
118 
119     Path resultFile = new Path(UTIL.getDataTestDirOnTestFS(), MASTER_FILE);
120     assertTrue("Master flag file should have been created",fs.exists(resultFile));
121 
122     resultFile = new Path(UTIL.getDataTestDirOnTestFS(), REGIONSERVER_FILE);
123     assertTrue("RegionServer flag file should have been created",fs.exists(resultFile));
124   }
125 }