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.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 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 public void perform() throws Exception { 049 LOG.info("Start corrupting data files"); 050 051 FileSystem fs = CommonFSUtils.getRootDirFileSystem(getConf()); 052 Path rootDir = CommonFSUtils.getRootDir(getConf()); 053 Path defaultDir = rootDir.suffix("/data/default"); 054 RemoteIterator<LocatedFileStatus> iterator = fs.listFiles(defaultDir, true); 055 while (iterator.hasNext()){ 056 LocatedFileStatus status = iterator.next(); 057 if(!HFile.isHFileFormat(fs, status.getPath())){ 058 continue; 059 } 060 if(RandomUtils.nextFloat(0, 100) > chance){ 061 continue; 062 } 063 064 FSDataOutputStream out = fs.create(status.getPath(), true); 065 try { 066 out.write(0); 067 } finally { 068 out.close(); 069 } 070 LOG.info("Corrupting {}", status.getPath()); 071 } 072 LOG.info("Done corrupting data files"); 073 } 074 075}