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 */ 018package org.apache.hadoop.hbase.chaos.policies; 019 020import java.util.ArrayList; 021import java.util.Arrays; 022import java.util.List; 023import org.apache.hadoop.hbase.chaos.actions.Action; 024import org.apache.hadoop.util.StringUtils; 025 026/** A policy which performs a sequence of actions deterministically. */ 027public class DoActionsOncePolicy extends PeriodicPolicy { 028 private List<Action> actions; 029 030 public DoActionsOncePolicy(long periodMs, List<Action> actions) { 031 super(periodMs); 032 this.actions = new ArrayList<>(actions); 033 } 034 035 public DoActionsOncePolicy(long periodMs, Action... actions) { 036 this(periodMs, Arrays.asList(actions)); 037 } 038 039 @Override 040 protected void runOneIteration() { 041 if (actions.isEmpty()) { 042 this.stop("done"); 043 return; 044 } 045 Action action = actions.remove(0); 046 047 try { 048 action.perform(); 049 } catch (Exception ex) { 050 LOG 051 .warn("Exception occurred during performing action: " + StringUtils.stringifyException(ex)); 052 } 053 } 054 055 @Override 056 public void init(PolicyContext context) throws Exception { 057 super.init(context); 058 for (Action action : actions) { 059 action.init(this.context); 060 } 061 } 062}