Examples of AtomicFileOutputStream


Examples of org.apache.hadoop.hdfs.util.AtomicFileOutputStream

   */
  private void persistPaxosData(long segmentTxId,
      PersistedRecoveryPaxosData newData) throws IOException {
    File f = journalStorage.getPaxosFile(segmentTxId);
    boolean success = false;
    AtomicFileOutputStream fos = new AtomicFileOutputStream(f);
    try {
      newData.writeDelimitedTo(fos);
      fos.write('\n');
      // Write human-readable data after the protobuf. This is only
      // to assist in debugging -- it's not parsed at all.
      OutputStreamWriter writer = new OutputStreamWriter(fos, Charsets.UTF_8);
     
      writer.write(String.valueOf(newData));
      writer.write('\n');
      writer.flush();
     
      fos.flush();
      success = true;
    } finally {
      if (success) {
        IOUtils.closeStream(fos);
      } else {
        fos.abort();
      }
    }
  }
View Full Code Here

Examples of org.apache.zookeeper.common.AtomicFileOutputStream

   * @param value the long value to write to the named file
   * @throws IOException if the file cannot be written atomically
   */
    private void writeLongToFile(String name, long value) throws IOException {
        File file = new File(logFactory.getSnapDir(), name);
        AtomicFileOutputStream out = new AtomicFileOutputStream(file);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
        boolean aborted = false;
        try {
            bw.write(Long.toString(value));
            bw.flush();
           
            out.flush();
        } catch (IOException e) {
            LOG.error("Failed to write new file " + file, e);
            // worst case here the tmp file/resources(fd) are not cleaned up
            //   and the caller will be notified (IOException)
            aborted = true;
            out.abort();
            throw e;
        } finally {
            if (!aborted) {
                // if the close operation (rename) fails we'll get notified.
                // worst case the tmp file may still exist
                out.close();
            }
        }
    }
View Full Code Here

Examples of org.apache.zookeeper.common.AtomicFileOutputStream

            br.close();
        }
    }

    static void writeLongToFile(File file, long value) throws IOException {
        AtomicFileOutputStream out = new AtomicFileOutputStream(file);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
        try {
            bw.write(Long.toString(value));
            bw.flush();
            out.flush();
            out.close();
        } catch (IOException e) {
            LOG.error("Failed to write new file " + file, e);
            out.abort();
            throw e;
        }
    }
View Full Code Here

Examples of org.apache.zookeeper.common.AtomicFileOutputStream

    /**
     * Test case where there is no existing file
     */
    @Test
    public void testWriteNewFile() throws IOException {
        OutputStream fos = new AtomicFileOutputStream(dstFile);
        assertFalse(dstFile.exists());
        fos.write(TEST_STRING.getBytes());
        fos.flush();
        assertFalse(dstFile.exists());
        fos.close();
        assertTrue(dstFile.exists());

        String readBackData = ClientBase.readFile(dstFile);
        assertEquals(TEST_STRING, readBackData);
    }
View Full Code Here

Examples of org.apache.zookeeper.common.AtomicFileOutputStream

     */
    @Test
    public void testOverwriteFile() throws IOException {
        assertTrue("Creating empty dst file", dstFile.createNewFile());

        OutputStream fos = new AtomicFileOutputStream(dstFile);

        assertTrue("Empty file still exists", dstFile.exists());
        fos.write(TEST_STRING.getBytes());
        fos.flush();

        // Original contents still in place
        assertEquals("", ClientBase.readFile(dstFile));

        fos.close();

        // New contents replace original file
        String readBackData = ClientBase.readFile(dstFile);
        assertEquals(TEST_STRING, readBackData);
    }
View Full Code Here

Examples of org.apache.zookeeper.common.AtomicFileOutputStream

    /**
     * Create a stream that fails to flush at close time
     */
    private OutputStream createFailingStream() throws FileNotFoundException {
        return new AtomicFileOutputStream(dstFile) {
            @Override
            public void flush() throws IOException {
                throw new IOException("injected failure");
            }
        };
View Full Code Here

Examples of org.apache.zookeeper.common.AtomicFileOutputStream

     * Ensure the tmp file is cleaned up and dstFile is not created when
     * aborting a new file.
     */
    @Test
    public void testAbortNewFile() throws IOException {
        AtomicFileOutputStream fos = new AtomicFileOutputStream(dstFile);

        fos.abort();

        assertEquals(0, testDir.list().length);
    }
View Full Code Here

Examples of org.apache.zookeeper.common.AtomicFileOutputStream

     * Ensure the tmp file is cleaned up and dstFile is not created when
     * aborting a new file.
     */
    @Test
    public void testAbortNewFileAfterFlush() throws IOException {
        AtomicFileOutputStream fos = new AtomicFileOutputStream(dstFile);
        fos.write(TEST_STRING.getBytes());
        fos.flush();

        fos.abort();

        assertEquals(0, testDir.list().length);
    }
View Full Code Here

Examples of org.apache.zookeeper.common.AtomicFileOutputStream

    public void testAbortExistingFile() throws IOException {
        FileOutputStream fos1 = new FileOutputStream(dstFile);
        fos1.write(TEST_STRING.getBytes());
        fos1.close();

        AtomicFileOutputStream fos2 = new AtomicFileOutputStream(dstFile);

        fos2.abort();

        // Should not have touched original file
        assertEquals(TEST_STRING, ClientBase.readFile(dstFile));
        assertEquals(1, testDir.list().length);
    }
View Full Code Here

Examples of org.apache.zookeeper.common.AtomicFileOutputStream

    public void testAbortExistingFileAfterFlush() throws IOException {
        FileOutputStream fos1 = new FileOutputStream(dstFile);
        fos1.write(TEST_STRING.getBytes());
        fos1.close();

        AtomicFileOutputStream fos2 = new AtomicFileOutputStream(dstFile);
        fos2.write(TEST_STRING_2.getBytes());
        fos2.flush();

        fos2.abort();

        // Should not have touched original file
        assertEquals(TEST_STRING, ClientBase.readFile(dstFile));
        assertEquals(1, testDir.list().length);
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.