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 */
018
019package org.apache.hadoop.hbase.chaos.actions;
020
021import org.apache.commons.lang3.RandomUtils;
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 protected Logger getLogger() {
047    return LOG;
048  }
049
050  @Override
051  public void perform() throws Exception {
052    getLogger().info("Start deleting data files");
053    FileSystem fs = CommonFSUtils.getRootDirFileSystem(getConf());
054    Path rootDir = CommonFSUtils.getRootDir(getConf());
055    Path defaultDir = rootDir.suffix("/data/default");
056    RemoteIterator<LocatedFileStatus> iterator =  fs.listFiles(defaultDir, true);
057    while (iterator.hasNext()){
058      LocatedFileStatus status = iterator.next();
059      if(!HFile.isHFileFormat(fs, status.getPath())){
060        continue;
061      }
062      if(RandomUtils.nextFloat(0, 100) > chance){
063        continue;
064      }
065      fs.delete(status.getPath(), true);
066      getLogger().info("Deleting {}", status.getPath());
067    }
068    getLogger().info("Done deleting data files");
069  }
070}