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 */
018
019package org.apache.hadoop.hbase.chaos.policies;
020
021import java.util.Properties;
022
023import org.apache.hadoop.hbase.IntegrationTestingUtility;
024import org.apache.hadoop.hbase.chaos.actions.Action;
025import org.apache.hadoop.hbase.util.StoppableImplementation;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029/**
030 * A policy to introduce chaos to the cluster
031 */
032public abstract class Policy extends StoppableImplementation implements Runnable {
033
034  protected static final Logger LOG = LoggerFactory.getLogger(Policy.class);
035
036  protected PolicyContext context;
037
038  public void init(PolicyContext context) throws Exception {
039    this.context = context;
040
041    // Used to wire up stopping.
042    context.setPolicy(this);
043  }
044
045  /**
046   * A context for a Policy
047   */
048  public static class PolicyContext extends Action.ActionContext {
049
050    Policy policy = null;
051
052    public PolicyContext(Properties monkeyProps, IntegrationTestingUtility util) {
053      super(monkeyProps, util);
054    }
055
056    @Override
057    public boolean isStopping() {
058      return policy.isStopped();
059    }
060
061    public void setPolicy(Policy policy) {
062      this.policy = policy;
063    }
064  }
065}