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 */
018
019package org.apache.hadoop.hbase.chaos.actions;
020
021import java.io.IOException;
022
023import org.apache.hadoop.hbase.ServerName;
024import org.apache.hadoop.hbase.util.Threads;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028/**
029* Base class for restarting HBaseServer's
030*/
031public class RestartActionBaseAction extends Action {
032  private static final Logger LOG =
033      LoggerFactory.getLogger(RestartActionBaseAction.class);
034  long sleepTime; // how long should we sleep
035
036  public RestartActionBaseAction(long sleepTime) {
037    this.sleepTime = sleepTime;
038  }
039
040  void sleep(long sleepTime) {
041    LOG.info("Sleeping for:" + sleepTime);
042    Threads.sleep(sleepTime);
043  }
044
045  void restartMaster(ServerName server, long sleepTime) throws IOException {
046    sleepTime = Math.max(sleepTime, 1000);
047    // Don't try the kill if we're stopping
048    if (context.isStopping()) {
049      return;
050    }
051
052    LOG.info("Killing master: {}", server);
053    killMaster(server);
054    sleep(sleepTime);
055    LOG.info("Starting master: {}", server);
056    startMaster(server);
057  }
058
059  /**
060   * Stop and then restart the region server instead of killing it.
061   * @param server hostname to restart the regionserver on
062   * @param sleepTime number of milliseconds between stop and restart
063   * @throws IOException if something goes wrong
064   */
065  void gracefulRestartRs(ServerName server, long sleepTime) throws IOException {
066    sleepTime = Math.max(sleepTime, 1000);
067    // Don't try the stop if we're stopping already
068    if (context.isStopping()) {
069      return;
070    }
071    LOG.info("Stopping region server: {}", server);
072    stopRs(server);
073    sleep(sleepTime);
074    LOG.info("Starting region server: {}", server);
075    startRs(server);
076  }
077
078  void restartRs(ServerName server, long sleepTime) throws IOException {
079    sleepTime = Math.max(sleepTime, 1000);
080    // Don't try the kill if we're stopping
081    if (context.isStopping()) {
082      return;
083    }
084    LOG.info("Killing region server: {}", server);
085    killRs(server);
086    sleep(sleepTime);
087    LOG.info("Starting region server: {}", server);
088    startRs(server);
089  }
090
091  void restartZKNode(ServerName server, long sleepTime) throws IOException {
092    sleepTime = Math.max(sleepTime, 1000);
093    // Don't try the kill if we're stopping
094    if (context.isStopping()) {
095      return;
096    }
097    LOG.info("Killing zookeeper node: {}", server);
098    killZKNode(server);
099    sleep(sleepTime);
100    LOG.info("Starting zookeeper node: {}", server);
101    startZKNode(server);
102  }
103
104  void restartDataNode(ServerName server, long sleepTime) throws IOException {
105    sleepTime = Math.max(sleepTime, 1000);
106    // Don't try the kill if we're stopping
107    if (context.isStopping()) {
108      return;
109    }
110    LOG.info("Killing data node: {}", server);
111    killDataNode(server);
112    sleep(sleepTime);
113    LOG.info("Starting data node: {}", server);
114    startDataNode(server);
115  }
116
117  void restartNameNode(ServerName server, long sleepTime) throws IOException {
118    sleepTime = Math.max(sleepTime, 1000);
119    // Don't try the kill if we're stopping
120    if (context.isStopping()) {
121      return;
122    }
123    LOG.info("Killing name node: {}", server);
124    killNameNode(server);
125    sleep(sleepTime);
126    LOG.info("Starting name node: {}", server);
127    startNameNode(server);
128  }
129
130}