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