Search in sources :

Example 21 with RandomAccessFile

use of java.io.RandomAccessFile in project hadoop by apache.

the class TestFSEditLogLoader method testDisplayRecentEditLogOpCodes.

@Test
public void testDisplayRecentEditLogOpCodes() throws IOException {
    // start a cluster
    Configuration conf = getConf();
    MiniDFSCluster cluster = null;
    FileSystem fileSys = null;
    cluster = new MiniDFSCluster.Builder(conf).numDataNodes(NUM_DATA_NODES).enableManagedDfsDirsRedundancy(false).build();
    cluster.waitActive();
    fileSys = cluster.getFileSystem();
    final FSNamesystem namesystem = cluster.getNamesystem();
    FSImage fsimage = namesystem.getFSImage();
    for (int i = 0; i < 20; i++) {
        fileSys.mkdirs(new Path("/tmp/tmp" + i));
    }
    StorageDirectory sd = fsimage.getStorage().dirIterator(NameNodeDirType.EDITS).next();
    cluster.shutdown();
    File editFile = FSImageTestUtil.findLatestEditsLog(sd).getFile();
    assertTrue("Should exist: " + editFile, editFile.exists());
    // Corrupt the edits file.
    long fileLen = editFile.length();
    RandomAccessFile rwf = new RandomAccessFile(editFile, "rw");
    rwf.seek(fileLen - 40);
    for (int i = 0; i < 20; i++) {
        rwf.write(FSEditLogOpCodes.OP_DELETE.getOpCode());
    }
    rwf.close();
    StringBuilder bld = new StringBuilder();
    bld.append("^Error replaying edit log at offset \\d+.  ");
    bld.append("Expected transaction ID was \\d+\n");
    bld.append("Recent opcode offsets: (\\d+\\s*){4}$");
    try {
        cluster = new MiniDFSCluster.Builder(conf).numDataNodes(NUM_DATA_NODES).enableManagedDfsDirsRedundancy(false).format(false).build();
        fail("should not be able to start");
    } catch (IOException e) {
        assertTrue("error message contains opcodes message", e.getMessage().matches(bld.toString()));
    }
}
Also used : Path(org.apache.hadoop.fs.Path) MiniDFSCluster(org.apache.hadoop.hdfs.MiniDFSCluster) Configuration(org.apache.hadoop.conf.Configuration) HdfsConfiguration(org.apache.hadoop.hdfs.HdfsConfiguration) StorageDirectory(org.apache.hadoop.hdfs.server.common.Storage.StorageDirectory) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) FileSystem(org.apache.hadoop.fs.FileSystem) DistributedFileSystem(org.apache.hadoop.hdfs.DistributedFileSystem) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Test(org.junit.Test)

Example 22 with RandomAccessFile

use of java.io.RandomAccessFile in project hadoop by apache.

the class TestFSEditLogLoader method corruptByteInFile.

/**
   * Corrupt the byte at the given offset in the given file,
   * by subtracting 1 from it.
   */
private void corruptByteInFile(File file, long offset) throws IOException {
    RandomAccessFile raf = new RandomAccessFile(file, "rw");
    try {
        raf.seek(offset);
        int origByte = raf.read();
        raf.seek(offset);
        raf.writeByte(origByte - 1);
    } finally {
        IOUtils.closeStream(raf);
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile)

Example 23 with RandomAccessFile

use of java.io.RandomAccessFile 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 24 with RandomAccessFile

use of java.io.RandomAccessFile in project hadoop by apache.

the class TestFileJournalManager method corruptAfterStartSegment.

/** 
   * Corrupt an edit log file after the start segment transaction
   */
private void corruptAfterStartSegment(File f) throws IOException {
    RandomAccessFile raf = new RandomAccessFile(f, "rw");
    // skip version and first tranaction and a bit of next transaction
    raf.seek(0x20);
    for (int i = 0; i < 1000; i++) {
        raf.writeInt(0xdeadbeef);
    }
    raf.close();
}
Also used : RandomAccessFile(java.io.RandomAccessFile)

Example 25 with RandomAccessFile

use of java.io.RandomAccessFile in project hadoop by apache.

the class TestSnapshot method testOfflineImageViewer.

/**
   * Test if the OfflineImageViewerPB can correctly parse a fsimage containing
   * snapshots
   */
@Test
public void testOfflineImageViewer() throws Exception {
    runTestSnapshot(1);
    // retrieve the fsimage. Note that we already save namespace to fsimage at
    // the end of each iteration of runTestSnapshot.
    File originalFsimage = FSImageTestUtil.findLatestImageFile(FSImageTestUtil.getFSImage(cluster.getNameNode()).getStorage().getStorageDir(0));
    assertNotNull("Didn't generate or can't find fsimage", originalFsimage);
    PrintStream o = new PrintStream(NullOutputStream.NULL_OUTPUT_STREAM);
    PBImageXmlWriter v = new PBImageXmlWriter(new Configuration(), o);
    v.visit(new RandomAccessFile(originalFsimage, "r"));
}
Also used : PBImageXmlWriter(org.apache.hadoop.hdfs.tools.offlineImageViewer.PBImageXmlWriter) PrintStream(java.io.PrintStream) Configuration(org.apache.hadoop.conf.Configuration) RandomAccessFile(java.io.RandomAccessFile) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Test(org.junit.Test)

Aggregations

RandomAccessFile (java.io.RandomAccessFile)866 IOException (java.io.IOException)425 File (java.io.File)349 FileChannel (java.nio.channels.FileChannel)133 FileNotFoundException (java.io.FileNotFoundException)84 ByteBuffer (java.nio.ByteBuffer)78 Test (org.junit.Test)78 FileLock (java.nio.channels.FileLock)64 EOFException (java.io.EOFException)50 FileOutputStream (java.io.FileOutputStream)47 FileInputStream (java.io.FileInputStream)40 InputStream (java.io.InputStream)36 MappedByteBuffer (java.nio.MappedByteBuffer)33 Random (java.util.Random)26 ByteArrayInputStream (java.io.ByteArrayInputStream)24 BufferedInputStream (java.io.BufferedInputStream)21 DataInputStream (java.io.DataInputStream)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)17 Configuration (org.apache.hadoop.conf.Configuration)16 AtomicFile (android.util.AtomicFile)12