Search in sources :

Example 1 with EditLogValidation

use of org.apache.hadoop.hdfs.server.namenode.FSEditLogLoader.EditLogValidation in project hadoop by apache.

the class TestFSEditLogLoader method testValidateEditLogWithCorruptHeader.

@Test
public void testValidateEditLogWithCorruptHeader() throws IOException {
    File testDir = new File(TEST_DIR, "testValidateEditLogWithCorruptHeader");
    SortedMap<Long, Long> offsetToTxId = Maps.newTreeMap();
    File logFile = prepareUnfinalizedTestEditLog(testDir, 2, offsetToTxId);
    RandomAccessFile rwf = new RandomAccessFile(logFile, "rw");
    try {
        rwf.seek(0);
        // corrupt header
        rwf.writeLong(42);
    } finally {
        rwf.close();
    }
    EditLogValidation validation = EditLogFileInputStream.scanEditLog(logFile, Long.MAX_VALUE, true);
    assertTrue(validation.hasCorruptHeader());
}
Also used : RandomAccessFile(java.io.RandomAccessFile) EditLogValidation(org.apache.hadoop.hdfs.server.namenode.FSEditLogLoader.EditLogValidation) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Test(org.junit.Test)

Example 2 with EditLogValidation

use of org.apache.hadoop.hdfs.server.namenode.FSEditLogLoader.EditLogValidation in project hadoop by apache.

the class TestFSEditLogLoader method testValidateEmptyEditLog.

@Test
public void testValidateEmptyEditLog() throws IOException {
    File testDir = new File(TEST_DIR, "testValidateEmptyEditLog");
    SortedMap<Long, Long> offsetToTxId = Maps.newTreeMap();
    File logFile = prepareUnfinalizedTestEditLog(testDir, 0, offsetToTxId);
    // Truncate the file so that there is nothing except the header and
    // layout flags section.
    truncateFile(logFile, 8);
    EditLogValidation validation = EditLogFileInputStream.scanEditLog(logFile, Long.MAX_VALUE, true);
    assertTrue(!validation.hasCorruptHeader());
    assertEquals(HdfsServerConstants.INVALID_TXID, validation.getEndTxId());
}
Also used : EditLogValidation(org.apache.hadoop.hdfs.server.namenode.FSEditLogLoader.EditLogValidation) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Test(org.junit.Test)

Example 3 with EditLogValidation

use of org.apache.hadoop.hdfs.server.namenode.FSEditLogLoader.EditLogValidation in project hadoop by apache.

the class TestFSEditLogLoader method testValidateEditLogWithCorruptBody.

@Test
public void testValidateEditLogWithCorruptBody() throws IOException {
    File testDir = new File(TEST_DIR, "testValidateEditLogWithCorruptBody");
    SortedMap<Long, Long> offsetToTxId = Maps.newTreeMap();
    final int NUM_TXNS = 20;
    File logFile = prepareUnfinalizedTestEditLog(testDir, NUM_TXNS, offsetToTxId);
    // Back up the uncorrupted log
    File logFileBak = new File(testDir, logFile.getName() + ".bak");
    Files.copy(logFile, logFileBak);
    EditLogValidation validation = EditLogFileInputStream.scanEditLog(logFile, Long.MAX_VALUE, true);
    assertTrue(!validation.hasCorruptHeader());
    // We expect that there will be an OP_START_LOG_SEGMENT, followed by
    // NUM_TXNS opcodes, followed by an OP_END_LOG_SEGMENT.
    assertEquals(NUM_TXNS + 1, validation.getEndTxId());
    // Corrupt each edit and verify that validation continues to work
    for (Map.Entry<Long, Long> entry : offsetToTxId.entrySet()) {
        long txOffset = entry.getKey();
        long txId = entry.getValue();
        // Restore backup, corrupt the txn opcode
        Files.copy(logFileBak, logFile);
        corruptByteInFile(logFile, txOffset);
        validation = EditLogFileInputStream.scanEditLog(logFile, Long.MAX_VALUE, true);
        long expectedEndTxId = (txId == (NUM_TXNS + 1)) ? NUM_TXNS : (NUM_TXNS + 1);
        assertEquals("Failed when corrupting txn opcode at " + txOffset, expectedEndTxId, validation.getEndTxId());
        assertTrue(!validation.hasCorruptHeader());
    }
    // to work
    for (Map.Entry<Long, Long> entry : offsetToTxId.entrySet()) {
        long txOffset = entry.getKey();
        long txId = entry.getValue();
        // Restore backup, corrupt the txn opcode
        Files.copy(logFileBak, logFile);
        truncateFile(logFile, txOffset);
        validation = EditLogFileInputStream.scanEditLog(logFile, Long.MAX_VALUE, true);
        long expectedEndTxId = (txId == 0) ? HdfsServerConstants.INVALID_TXID : (txId - 1);
        assertEquals("Failed when corrupting txid " + txId + " txn opcode " + "at " + txOffset, expectedEndTxId, validation.getEndTxId());
        assertTrue(!validation.hasCorruptHeader());
    }
}
Also used : EditLogValidation(org.apache.hadoop.hdfs.server.namenode.FSEditLogLoader.EditLogValidation) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) SortedMap(java.util.SortedMap) Test(org.junit.Test)

Aggregations

File (java.io.File)3 RandomAccessFile (java.io.RandomAccessFile)3 EditLogValidation (org.apache.hadoop.hdfs.server.namenode.FSEditLogLoader.EditLogValidation)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)1 Map (java.util.Map)1 SortedMap (java.util.SortedMap)1