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.actions;
019
020import java.util.Random;
021import java.util.concurrent.ThreadLocalRandom;
022import org.apache.hadoop.fs.FSDataOutputStream;
023import org.apache.hadoop.fs.FileSystem;
024import org.apache.hadoop.fs.LocatedFileStatus;
025import org.apache.hadoop.fs.Path;
026import org.apache.hadoop.fs.RemoteIterator;
027import org.apache.hadoop.hbase.io.hfile.HFile;
028import org.apache.hadoop.hbase.util.CommonFSUtils;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032/**
033 * Action corrupts HFiles with a certain chance.
034 */
035public class CorruptDataFilesAction extends Action {
036  private static final Logger LOG = LoggerFactory.getLogger(CorruptDataFilesAction.class);
037  private final float chance;
038
039  /**
040   * Corrupts HFiles with a certain chance
041   * @param chance chance to corrupt any give data file (0.5 => 50%)
042   */
043  public CorruptDataFilesAction(float chance) {
044    this.chance = chance * 100;
045  }
046
047  @Override
048  protected Logger getLogger() {
049    return LOG;
050  }
051
052  @Override
053  public void perform() throws Exception {
054    getLogger().info("Start corrupting data files");
055
056    FileSystem fs = CommonFSUtils.getRootDirFileSystem(getConf());
057    Path rootDir = CommonFSUtils.getRootDir(getConf());
058    Path defaultDir = rootDir.suffix("/data/default");
059    RemoteIterator<LocatedFileStatus> iterator = fs.listFiles(defaultDir, true);
060    Random rand = ThreadLocalRandom.current();
061    while (iterator.hasNext()) {
062      LocatedFileStatus status = iterator.next();
063      if (!HFile.isHFileFormat(fs, status.getPath())) {
064        continue;
065      }
066      if ((100 * rand.nextFloat()) > chance) {
067        continue;
068      }
069      FSDataOutputStream out = fs.create(status.getPath(), true);
070      try {
071        out.write(0);
072      } finally {
073        out.close();
074      }
075      getLogger().info("Corrupting {}", status.getPath());
076    }
077    getLogger().info("Done corrupting data files");
078  }
079
080}