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