doTestFixCorruptEvents(true);
}
public void doTestFixCorruptEvents(boolean withCheckpoint) throws Exception {
Set<String> corruptFiles = new HashSet<String>();
File[] files = dataDir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if(name.contains("lock") || name.contains("meta")) {
return false;
}
return true;
}
});
Random random = new Random();
int corrupted = 0;
for (File dataFile : files) {
LogFile.SequentialReader reader =
new LogFileV3.SequentialReader(dataFile, null, true);
RandomAccessFile handle = new RandomAccessFile(dataFile, "rw");
long eventPosition1 = reader.getPosition();
LogRecord rec = reader.next();
//No point corrupting commits, so ignore them
if(rec == null ||
rec.getEvent().getClass().getName().
equals("org.apache.flume.channel.file.Commit")) {
handle.close();
reader.close();
continue;
}
long eventPosition2 = reader.getPosition();
rec = reader.next();
handle.seek(eventPosition1 + 100);
handle.writeInt(random.nextInt());
corrupted++;
corruptFiles.add(dataFile.getName());
if (rec == null ||
rec.getEvent().getClass().getName().
equals("org.apache.flume.channel.file.Commit")) {
handle.close();
reader.close();
continue;
}
handle.seek(eventPosition2 + 100);
handle.writeInt(random.nextInt());
corrupted++;
handle.close();
reader.close();
}
FileChannelIntegrityTool tool = new FileChannelIntegrityTool();
tool.run(new String[] {"-l", dataDir.toString()});
FileChannel channel = new FileChannel();
channel.setName("channel");
String cp;
if(withCheckpoint) {
cp = origCheckpointDir.toString();
} else {
FileUtils.deleteDirectory(checkpointDir);
Assert.assertTrue(checkpointDir.mkdirs());
cp = checkpointDir.toString();
}
ctx.put(FileChannelConfiguration.CHECKPOINT_DIR,cp);
ctx.put(FileChannelConfiguration.DATA_DIRS, dataDir.toString());
channel.configure(ctx);
channel.start();
Transaction tx = channel.getTransaction();
tx.begin();
int i = 0;
while(channel.take() != null) {
i++;
}
tx.commit();
tx.close();
channel.stop();
Assert.assertEquals(25 - corrupted, i);
files = dataDir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if(name.contains(".bak")) {
return true;
}