View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.chaos.actions;
20  
21  import java.util.HashSet;
22  import java.util.List;
23  import java.util.Set;
24  
25  import org.apache.hadoop.hbase.ServerName;
26  import org.apache.hadoop.hbase.chaos.monkies.PolicyBasedChaosMonkey;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  /**
31   * Restarts a ratio of the running regionservers at the same time
32   */
33  public class BatchRestartRsAction extends RestartActionBaseAction {
34    float ratio; //ratio of regionservers to restart
35    private static final Logger LOG =
36        LoggerFactory.getLogger(BatchRestartRsAction.class);
37  
38    public BatchRestartRsAction(long sleepTime, float ratio) {
39      super(sleepTime);
40      this.ratio = ratio;
41    }
42  
43    @Override
44    public void perform() throws Exception {
45      LOG.info(String.format("Performing action: Batch restarting %d%% of region servers",
46          (int)(ratio * 100)));
47      List<ServerName> selectedServers = PolicyBasedChaosMonkey.selectRandomItems(getCurrentServers(),
48          ratio);
49  
50      Set<ServerName> killedServers = new HashSet<ServerName>();
51  
52      for (ServerName server : selectedServers) {
53        // Don't keep killing servers if we're
54        // trying to stop the monkey.
55        if (context.isStopping()) {
56          break;
57        }
58        LOG.info("Killing region server:" + server);
59        cluster.killRegionServer(server);
60        killedServers.add(server);
61      }
62  
63      for (ServerName server : killedServers) {
64        cluster.waitForRegionServerToStop(server, PolicyBasedChaosMonkey.TIMEOUT);
65      }
66  
67      LOG.info("Killed " + killedServers.size() + " region servers. Reported num of rs:"
68          + cluster.getClusterStatus().getServersSize());
69  
70      sleep(sleepTime);
71  
72      for (ServerName server : killedServers) {
73        LOG.info("Starting region server:" + server.getHostname());
74        cluster.startRegionServer(server.getHostname(), server.getPort());
75  
76      }
77      for (ServerName server : killedServers) {
78        cluster.waitForRegionServerToStart(server.getHostname(),
79            server.getPort(),
80            PolicyBasedChaosMonkey.TIMEOUT);
81      }
82      LOG.info("Started " + killedServers.size() +" region servers. Reported num of rs:"
83          + cluster.getClusterStatus().getServersSize());
84    }
85  }