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.List;
22  
23  import org.apache.hadoop.hbase.ServerName;
24  import org.apache.hadoop.hbase.chaos.monkies.PolicyBasedChaosMonkey;
25  
26  /**
27   * Restarts a ratio of the running regionservers at the same time
28   */
29  public class BatchRestartRsAction extends RestartActionBaseAction {
30    float ratio; //ratio of regionservers to restart
31  
32    public BatchRestartRsAction(long sleepTime, float ratio) {
33      super(sleepTime);
34      this.ratio = ratio;
35    }
36  
37    @Override
38    public void perform() throws Exception {
39      LOG.info(String.format("Performing action: Batch restarting %d%% of region servers",
40          (int)(ratio * 100)));
41      List<ServerName> selectedServers = PolicyBasedChaosMonkey.selectRandomItems(getCurrentServers(),
42          ratio);
43  
44      for (ServerName server : selectedServers) {
45        LOG.info("Killing region server:" + server);
46        cluster.killRegionServer(server);
47      }
48  
49      for (ServerName server : selectedServers) {
50        cluster.waitForRegionServerToStop(server, PolicyBasedChaosMonkey.TIMEOUT);
51      }
52  
53      LOG.info("Killed " + selectedServers.size() + " region servers. Reported num of rs:"
54          + cluster.getClusterStatus().getServersSize());
55  
56      sleep(sleepTime);
57  
58      for (ServerName server : selectedServers) {
59        LOG.info("Starting region server:" + server.getHostname());
60        cluster.startRegionServer(server.getHostname());
61  
62      }
63      for (ServerName server : selectedServers) {
64        cluster.waitForRegionServerToStart(server.getHostname(), PolicyBasedChaosMonkey.TIMEOUT);
65      }
66      LOG.info("Started " + selectedServers.size() +" region servers. Reported num of rs:"
67          + cluster.getClusterStatus().getServersSize());
68    }
69  }