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