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 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  public void perform() throws Exception {
048    LOG.info("Start deleting data files");
049    FileSystem fs = CommonFSUtils.getRootDirFileSystem(getConf());
050    Path rootDir = CommonFSUtils.getRootDir(getConf());
051    Path defaultDir = rootDir.suffix("/data/default");
052    RemoteIterator<LocatedFileStatus> iterator =  fs.listFiles(defaultDir, true);
053    while (iterator.hasNext()){
054      LocatedFileStatus status = iterator.next();
055      if(!HFile.isHFileFormat(fs, status.getPath())){
056        continue;
057      }
058      if(RandomUtils.nextFloat(0, 100) > chance){
059        continue;
060      }
061      fs.delete(status.getPath());
062      LOG.info("Deleting {}", status.getPath());
063    }
064    LOG.info("Done deleting data files");
065  }
066}