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