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.*;
022import org.apache.hadoop.hbase.chaos.monkies.ChaosMonkey;
023import org.apache.hadoop.hbase.chaos.monkies.PolicyBasedChaosMonkey;
024import org.apache.hadoop.hbase.chaos.policies.CompositeSequentialPolicy;
025import org.apache.hadoop.hbase.chaos.policies.DoActionsOncePolicy;
026import org.apache.hadoop.hbase.chaos.policies.PeriodicRandomActionPolicy;
027
028public class SlowDeterministicMonkeyFactory extends MonkeyFactory {
029
030  private long action1Period;
031  private long action2Period;
032  private long action3Period;
033  private long action4Period;
034  private long moveRegionsMaxTime;
035  private long moveRegionsSleepTime;
036  private long moveRandomRegionSleepTime;
037  private long restartRandomRSSleepTime;
038  private long batchRestartRSSleepTime;
039  private float batchRestartRSRatio;
040  private long restartActiveMasterSleepTime;
041  private long rollingBatchRestartRSSleepTime;
042  private float rollingBatchRestartRSRatio;
043  private long restartRsHoldingMetaSleepTime;
044  private float compactTableRatio;
045  private float compactRandomRegionRatio;
046  private long decreaseHFileSizeSleepTime;
047
048  @Override
049  public ChaosMonkey build() {
050
051    loadProperties();
052    // Actions such as compact/flush a table/region,
053    // move one region around. They are not so destructive,
054    // can be executed more frequently.
055    Action[] actions1 = new Action[] {
056        new CompactTableAction(tableName, compactTableRatio),
057        new CompactRandomRegionOfTableAction(tableName, compactRandomRegionRatio),
058        new FlushTableAction(tableName),
059        new FlushRandomRegionOfTableAction(tableName),
060        new MoveRandomRegionOfTableAction(tableName)
061    };
062
063    // Actions such as split/merge/snapshot.
064    // They should not cause data loss, or unreliability
065    // such as region stuck in transition.
066    Action[] actions2 = new Action[] {
067        new SplitRandomRegionOfTableAction(tableName),
068        new MergeRandomAdjacentRegionsOfTableAction(tableName),
069        new SnapshotTableAction(tableName),
070        new AddColumnAction(tableName),
071        new RemoveColumnAction(tableName, columnFamilies),
072        new ChangeEncodingAction(tableName),
073        new ChangeCompressionAction(tableName),
074        new ChangeBloomFilterAction(tableName),
075        new ChangeVersionsAction(tableName),
076        new ChangeSplitPolicyAction(tableName),
077    };
078
079    // Destructive actions to mess things around.
080    Action[] actions3 = new Action[] {
081        new MoveRegionsOfTableAction(moveRegionsSleepTime, moveRegionsMaxTime,
082            tableName),
083        new MoveRandomRegionOfTableAction(moveRandomRegionSleepTime, tableName),
084        new RestartRandomRsAction(restartRandomRSSleepTime),
085        new BatchRestartRsAction(batchRestartRSSleepTime, batchRestartRSRatio),
086        new RestartActiveMasterAction(restartActiveMasterSleepTime),
087        new RollingBatchRestartRsAction(rollingBatchRestartRSSleepTime,
088            rollingBatchRestartRSRatio),
089        new RestartRsHoldingMetaAction(restartRsHoldingMetaSleepTime),
090        new DecreaseMaxHFileSizeAction(decreaseHFileSizeSleepTime, tableName),
091        new SplitAllRegionOfTableAction(tableName),
092    };
093
094    // Action to log more info for debugging
095    Action[] actions4 = new Action[] {
096        new DumpClusterStatusAction()
097    };
098
099    return new PolicyBasedChaosMonkey(util,
100        new PeriodicRandomActionPolicy(action1Period, actions1),
101        new PeriodicRandomActionPolicy(action2Period, actions2),
102        new CompositeSequentialPolicy(
103            new DoActionsOncePolicy(action3Period, actions3),
104            new PeriodicRandomActionPolicy(action3Period, actions3)),
105        new PeriodicRandomActionPolicy(action4Period, actions4));
106  }
107
108  private void loadProperties() {
109
110      action1Period = Long.parseLong(this.properties.getProperty(
111        MonkeyConstants.PERIODIC_ACTION1_PERIOD,
112        MonkeyConstants.DEFAULT_PERIODIC_ACTION1_PERIOD + ""));
113      action2Period = Long.parseLong(this.properties.getProperty(
114        MonkeyConstants.PERIODIC_ACTION2_PERIOD,
115        MonkeyConstants.DEFAULT_PERIODIC_ACTION2_PERIOD + ""));
116      action3Period = Long.parseLong(this.properties.getProperty(
117        MonkeyConstants.COMPOSITE_ACTION3_PERIOD,
118        MonkeyConstants.DEFAULT_COMPOSITE_ACTION3_PERIOD + ""));
119      action4Period = Long.parseLong(this.properties.getProperty(
120        MonkeyConstants.PERIODIC_ACTION4_PERIOD,
121        MonkeyConstants.DEFAULT_PERIODIC_ACTION4_PERIOD + ""));
122      moveRegionsMaxTime = Long.parseLong(this.properties.getProperty(
123        MonkeyConstants.MOVE_REGIONS_MAX_TIME,
124        MonkeyConstants.DEFAULT_MOVE_REGIONS_MAX_TIME + ""));
125      moveRegionsSleepTime = Long.parseLong(this.properties.getProperty(
126        MonkeyConstants.MOVE_REGIONS_SLEEP_TIME,
127        MonkeyConstants.DEFAULT_MOVE_REGIONS_SLEEP_TIME + ""));
128      moveRandomRegionSleepTime = Long.parseLong(this.properties.getProperty(
129        MonkeyConstants.MOVE_RANDOM_REGION_SLEEP_TIME,
130        MonkeyConstants.DEFAULT_MOVE_RANDOM_REGION_SLEEP_TIME + ""));
131      restartRandomRSSleepTime = Long.parseLong(this.properties.getProperty(
132        MonkeyConstants.RESTART_RANDOM_RS_SLEEP_TIME,
133        MonkeyConstants.DEFAULT_RESTART_RANDOM_RS_SLEEP_TIME + ""));
134      batchRestartRSSleepTime = Long.parseLong(this.properties.getProperty(
135        MonkeyConstants.BATCH_RESTART_RS_SLEEP_TIME,
136        MonkeyConstants.DEFAULT_BATCH_RESTART_RS_SLEEP_TIME + ""));
137      batchRestartRSRatio = Float.parseFloat(this.properties.getProperty(
138        MonkeyConstants.BATCH_RESTART_RS_RATIO,
139        MonkeyConstants.DEFAULT_BATCH_RESTART_RS_RATIO + ""));
140      restartActiveMasterSleepTime = Long.parseLong(this.properties.getProperty(
141        MonkeyConstants.RESTART_ACTIVE_MASTER_SLEEP_TIME,
142        MonkeyConstants.DEFAULT_RESTART_ACTIVE_MASTER_SLEEP_TIME + ""));
143      rollingBatchRestartRSSleepTime = Long.parseLong(this.properties.getProperty(
144        MonkeyConstants.ROLLING_BATCH_RESTART_RS_SLEEP_TIME,
145        MonkeyConstants.DEFAULT_ROLLING_BATCH_RESTART_RS_SLEEP_TIME + ""));
146      rollingBatchRestartRSRatio = Float.parseFloat(this.properties.getProperty(
147        MonkeyConstants.ROLLING_BATCH_RESTART_RS_RATIO,
148        MonkeyConstants.DEFAULT_ROLLING_BATCH_RESTART_RS_RATIO + ""));
149      restartRsHoldingMetaSleepTime = Long.parseLong(this.properties.getProperty(
150        MonkeyConstants.RESTART_RS_HOLDING_META_SLEEP_TIME,
151        MonkeyConstants.DEFAULT_RESTART_RS_HOLDING_META_SLEEP_TIME + ""));
152      compactTableRatio = Float.parseFloat(this.properties.getProperty(
153        MonkeyConstants.COMPACT_TABLE_ACTION_RATIO,
154        MonkeyConstants.DEFAULT_COMPACT_TABLE_ACTION_RATIO + ""));
155      compactRandomRegionRatio = Float.parseFloat(this.properties.getProperty(
156        MonkeyConstants.COMPACT_RANDOM_REGION_RATIO,
157        MonkeyConstants.DEFAULT_COMPACT_RANDOM_REGION_RATIO + ""));
158    decreaseHFileSizeSleepTime = Long.parseLong(this.properties.getProperty(
159        MonkeyConstants.DECREASE_HFILE_SIZE_SLEEP_TIME,
160        MonkeyConstants.DEFAULT_DECREASE_HFILE_SIZE_SLEEP_TIME + ""));
161  }
162}