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.policies;
20  
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.List;
24  
25  import org.apache.hadoop.hbase.chaos.actions.Action;
26  import org.apache.hadoop.util.StringUtils;
27  
28  /** A policy which performs a sequence of actions deterministically. */
29  public class DoActionsOncePolicy extends PeriodicPolicy {
30    private List<Action> actions;
31  
32    public DoActionsOncePolicy(long periodMs, List<Action> actions) {
33      super(periodMs);
34      this.actions = new ArrayList<Action>(actions);
35    }
36  
37    public DoActionsOncePolicy(long periodMs, Action... actions) {
38      this(periodMs, Arrays.asList(actions));
39    }
40  
41    @Override
42    protected void runOneIteration() {
43      if (actions.isEmpty()) {
44        this.stop("done");
45        return;
46      }
47      Action action = actions.remove(0);
48  
49      try {
50        action.perform();
51      } catch (Exception ex) {
52        LOG.warn("Exception occured during performing action: "
53            + StringUtils.stringifyException(ex));
54      }
55    }
56  
57    @Override
58    public void init(PolicyContext context) throws Exception {
59      super.init(context);
60      for (Action action : actions) {
61        action.init(this.context);
62      }
63    }
64  }