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;
022import java.util.Arrays;
023import java.util.List;
024import org.apache.commons.lang3.RandomUtils;
025import org.apache.hadoop.hbase.ServerName;
026import org.apache.hadoop.hbase.util.RegionMover;
027import org.apache.hadoop.util.Shell;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031/**
032 * Gracefully restarts every regionserver in a rolling fashion. At each step, it unloads,
033 * restarts the loads every rs server sleeping randomly (0-sleepTime) in between servers.
034 */
035public class GracefulRollingRestartRsAction extends RestartActionBaseAction {
036  private static final Logger LOG = LoggerFactory.getLogger(GracefulRollingRestartRsAction.class);
037
038  public GracefulRollingRestartRsAction(long sleepTime) {
039    super(sleepTime);
040  }
041
042  @Override
043  public void perform() throws Exception {
044    LOG.info("Performing action: Rolling restarting non-master region servers");
045    List<ServerName> selectedServers = selectServers();
046
047    LOG.info("Disabling balancer to make unloading possible");
048    setBalancer(false, true);
049
050    for (ServerName server : selectedServers) {
051      String rsName = server.getAddress().toString();
052      try (RegionMover rm =
053          new RegionMover.RegionMoverBuilder(rsName, getConf()).ack(true).build()) {
054        LOG.info("Unloading {}", server);
055        rm.unload();
056        LOG.info("Restarting {}", server);
057        gracefulRestartRs(server, sleepTime);
058        LOG.info("Loading {}", server);
059        rm.load();
060      } catch (Shell.ExitCodeException e) {
061        LOG.info("Problem restarting but presume successful; code={}", e.getExitCode(), e);
062      }
063      sleep(RandomUtils.nextInt(0, (int)sleepTime));
064    }
065    LOG.info("Enabling balancer");
066    setBalancer(true, true);
067  }
068
069  protected List<ServerName> selectServers() throws IOException {
070    return Arrays.asList(getCurrentServers());
071  }
072
073}