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