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.io.IOException;
22  import java.util.ArrayList;
23  import java.util.LinkedList;
24  import java.util.List;
25  import java.util.Queue;
26  
27  import org.apache.commons.lang.math.RandomUtils;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.hadoop.hbase.ServerName;
31  import org.apache.hadoop.hbase.chaos.monkies.PolicyBasedChaosMonkey;
32  
33  /**
34   * Restarts a ratio of the regionservers in a rolling fashion. At each step, either kills a
35   * server, or starts one, sleeping randomly (0-sleepTime) in between steps.
36   * The parameter maxDeadServers limits the maximum number of servers that
37   * can be down at the same time during rolling restarts.
38   */
39  public class RollingBatchRestartRsAction extends BatchRestartRsAction {
40    private static final Log LOG = LogFactory.getLog(RollingBatchRestartRsAction.class);
41    protected int maxDeadServers; // number of maximum dead servers at any given time. Defaults to 5
42  
43    public RollingBatchRestartRsAction(long sleepTime, float ratio) {
44      this(sleepTime, ratio, 5);
45    }
46  
47    public RollingBatchRestartRsAction(long sleepTime, float ratio, int maxDeadServers) {
48      super(sleepTime, ratio);
49      this.maxDeadServers = maxDeadServers;
50    }
51  
52    enum KillOrStart {
53      KILL,
54      START
55    }
56  
57    @Override
58    public void perform() throws Exception {
59      LOG.info(String.format("Performing action: Rolling batch restarting %d%% of region servers",
60          (int)(ratio * 100)));
61      List<ServerName> selectedServers = selectServers();
62  
63      Queue<ServerName> serversToBeKilled = new LinkedList<ServerName>(selectedServers);
64      Queue<ServerName> deadServers = new LinkedList<ServerName>();
65  
66      // loop while there are servers to be killed or dead servers to be restarted
67      while ((!serversToBeKilled.isEmpty() || !deadServers.isEmpty())  && !context.isStopping()) {
68        KillOrStart action = KillOrStart.KILL;
69  
70        if (serversToBeKilled.isEmpty()) { // no more servers to kill
71          action = KillOrStart.START;
72        } else if (deadServers.isEmpty()) {
73          action = KillOrStart.KILL; // no more servers to start
74        } else if (deadServers.size() >= maxDeadServers) {
75          // we have too many dead servers. Don't kill any more
76          action = KillOrStart.START;
77        } else {
78          // do a coin toss
79          action = RandomUtils.nextBoolean() ? KillOrStart.KILL : KillOrStart.START;
80        }
81  
82        ServerName server;
83  
84        switch (action) {
85          case KILL:
86            server = serversToBeKilled.remove();
87            try {
88              killRs(server);
89            } catch (org.apache.hadoop.util.Shell.ExitCodeException e) {
90              // We've seen this in test runs where we timeout but the kill went through. HBASE-9743
91              // So, add to deadServers even if exception so the start gets called.
92              LOG.info("Problem killing but presume successful; code=" + e.getExitCode(), e);
93            }
94            deadServers.add(server);
95            break;
96          case START:
97            try {
98              server = deadServers.remove();
99              startRs(server);
100           } catch (org.apache.hadoop.util.Shell.ExitCodeException e) {
101             // The start may fail but better to just keep going though we may lose server.
102             //
103             LOG.info("Problem starting, will retry; code=" + e.getExitCode(), e);
104           }
105           break;
106       }
107 
108       sleep(RandomUtils.nextInt((int)sleepTime));
109     }
110   }
111 
112   protected List<ServerName> selectServers() throws IOException {
113     return PolicyBasedChaosMonkey.selectRandomItems(getCurrentServers(), ratio);
114   }
115 
116   /**
117    * Small test to ensure the class basically works.
118    * @param args
119    * @throws Exception
120    */
121   public static void main(final String[] args) throws Exception {
122     RollingBatchRestartRsAction action = new RollingBatchRestartRsAction(1, 1.0f) {
123       private int invocations = 0;
124       @Override
125       protected ServerName[] getCurrentServers() throws IOException {
126         final int count = 4;
127         List<ServerName> serverNames = new ArrayList<ServerName>(count);
128         for (int i = 0; i < 4; i++) {
129           serverNames.add(ServerName.valueOf(i + ".example.org", i, i));
130         }
131         return serverNames.toArray(new ServerName[serverNames.size()]);
132       }
133 
134       @Override
135       protected void killRs(ServerName server) throws IOException {
136         LOG.info("Killed " + server);
137         if (this.invocations++ % 3 == 0) {
138           throw new org.apache.hadoop.util.Shell.ExitCodeException(-1, "Failed");
139         }
140       }
141 
142       @Override
143       protected void startRs(ServerName server) throws IOException {
144         LOG.info("Started " + server);
145         if (this.invocations++ % 3 == 0) {
146           throw new org.apache.hadoop.util.Shell.ExitCodeException(-1, "Failed");
147         }
148       }
149     };
150 
151     action.perform();
152   }
153 }