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