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.factories;
020
021import org.apache.hadoop.hbase.chaos.actions.UnbalanceKillAndRebalanceAction;
022import org.apache.hadoop.hbase.chaos.monkies.ChaosMonkey;
023import org.apache.hadoop.hbase.chaos.monkies.PolicyBasedChaosMonkey;
024import org.apache.hadoop.hbase.chaos.policies.PeriodicRandomActionPolicy;
025import org.apache.hadoop.hbase.chaos.policies.Policy;
026
027public class UnbalanceMonkeyFactory extends MonkeyFactory {
028  /** How often to introduce the chaos. If too frequent, sequence of kills on minicluster
029   * can cause test to fail when Put runs out of retries. */
030  private long chaosEveryMilliSec;
031  private long waitForUnbalanceMilliSec;
032  private long waitForKillMilliSec;
033  private long waitAfterBalanceMilliSec;
034  private boolean killMetaRs;
035
036  @Override
037  public ChaosMonkey build() {
038    loadProperties();
039    Policy chaosPolicy = new PeriodicRandomActionPolicy(chaosEveryMilliSec,
040        new UnbalanceKillAndRebalanceAction(waitForUnbalanceMilliSec, waitForKillMilliSec,
041            waitAfterBalanceMilliSec, killMetaRs));
042
043    return new PolicyBasedChaosMonkey(properties, util, chaosPolicy);
044  }
045
046  private void loadProperties() {
047    chaosEveryMilliSec = Long.parseLong(this.properties.getProperty(
048      MonkeyConstants.UNBALANCE_CHAOS_EVERY_MS,
049      MonkeyConstants.DEFAULT_UNBALANCE_CHAOS_EVERY_MS + ""));
050    waitForUnbalanceMilliSec = Long.parseLong(this.properties.getProperty(
051      MonkeyConstants.UNBALANCE_WAIT_FOR_UNBALANCE_MS,
052      MonkeyConstants.DEFAULT_UNBALANCE_WAIT_FOR_UNBALANCE_MS + ""));
053    waitForKillMilliSec = Long.parseLong(this.properties.getProperty(
054      MonkeyConstants.UNBALANCE_WAIT_FOR_KILLS_MS,
055      MonkeyConstants.DEFAULT_UNBALANCE_WAIT_FOR_KILLS_MS + ""));
056    waitAfterBalanceMilliSec = Long.parseLong(this.properties.getProperty(
057      MonkeyConstants.UNBALANCE_WAIT_AFTER_BALANCE_MS,
058      MonkeyConstants.DEFAULT_UNBALANCE_WAIT_AFTER_BALANCE_MS + ""));
059    killMetaRs = Boolean.parseBoolean(this.properties.getProperty(
060      MonkeyConstants.UNBALANCE_KILL_META_RS,
061      MonkeyConstants.DEFAULT_UNBALANCE_KILL_META_RS + ""));
062  }
063}