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