Java Code Examples for org.apache.flink.core.fs.RecoverableWriter#open()

The following examples show how to use org.apache.flink.core.fs.RecoverableWriter#open() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HadoopS3RecoverableWriterExceptionITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test(expected = IOException.class)
public void testResumeWithWrongOffset() throws Exception {
	// this is a rather unrealistic scenario, but it is to trigger
	// truncation of the file and try to resume with missing data.

	final RecoverableWriter writer = getFileSystem().createRecoverableWriter();
	final Path path = new Path(basePathForTest, "part-0");

	final RecoverableFsDataOutputStream stream = writer.open(path);
	stream.write(testData1.getBytes(StandardCharsets.UTF_8));

	final RecoverableWriter.ResumeRecoverable recoverable1 = stream.persist();
	stream.write(testData2.getBytes(StandardCharsets.UTF_8));

	final RecoverableWriter.ResumeRecoverable recoverable2 = stream.persist();
	stream.write(testData3.getBytes(StandardCharsets.UTF_8));

	final RecoverableFsDataOutputStream recoveredStream = writer.recover(recoverable1);
	recoveredStream.closeForCommit().commit();

	// this should throw an exception
	final RecoverableFsDataOutputStream newRecoveredStream = writer.recover(recoverable2);
	newRecoveredStream.closeForCommit().commit();
}
 
Example 2
Source File: HadoopS3RecoverableWriterITCase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test(expected = FileNotFoundException.class)
public void testCleanupRecoverableState() throws Exception {
	final RecoverableWriter writer = getRecoverableWriter();
	final Path path = new Path(basePathForTest, "part-0");

	final RecoverableFsDataOutputStream stream = writer.open(path);
	stream.write(bytesOf(testData1));
	S3Recoverable recoverable = (S3Recoverable) stream.persist();

	stream.closeForCommit().commit();

	// still the data is there as we have not deleted them from the tmp object
	final String content = getContentsOfFile(new Path('/' + recoverable.incompleteObjectName()));
	Assert.assertEquals(testData1, content);

	boolean successfullyDeletedState = writer.cleanupRecoverableState(recoverable);
	Assert.assertTrue(successfullyDeletedState);

	// this should throw the exception as we deleted the file.
	getContentsOfFile(new Path('/' + recoverable.incompleteObjectName()));
}
 
Example 3
Source File: HadoopS3RecoverableWriterExceptionITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test(expected = IOException.class)
public void testResumeAfterCommit() throws Exception {
	final RecoverableWriter writer = getFileSystem().createRecoverableWriter();
	final Path path = new Path(basePathForTest, "part-0");

	final RecoverableFsDataOutputStream stream = writer.open(path);
	stream.write(testData1.getBytes(StandardCharsets.UTF_8));

	final RecoverableWriter.ResumeRecoverable recoverable = stream.persist();
	stream.write(testData2.getBytes(StandardCharsets.UTF_8));

	stream.closeForCommit().commit();

	final RecoverableFsDataOutputStream recoveredStream = writer.recover(recoverable);
	recoveredStream.closeForCommit().commit();
}
 
Example 4
Source File: HadoopS3RecoverableWriterExceptionITCase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test(expected = IOException.class)
public void testResumeAfterCommit() throws Exception {
	final RecoverableWriter writer = getFileSystem().createRecoverableWriter();
	final Path path = new Path(basePathForTest, "part-0");

	final RecoverableFsDataOutputStream stream = writer.open(path);
	stream.write(testData1.getBytes(StandardCharsets.UTF_8));

	final RecoverableWriter.ResumeRecoverable recoverable = stream.persist();
	stream.write(testData2.getBytes(StandardCharsets.UTF_8));

	stream.closeForCommit().commit();

	final RecoverableFsDataOutputStream recoveredStream = writer.recover(recoverable);
	recoveredStream.closeForCommit().commit();
}
 
Example 5
Source File: HadoopS3RecoverableWriterExceptionITCase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test(expected = IOException.class)
public void testResumeWithWrongOffset() throws Exception {
	// this is a rather unrealistic scenario, but it is to trigger
	// truncation of the file and try to resume with missing data.

	final RecoverableWriter writer = getFileSystem().createRecoverableWriter();
	final Path path = new Path(basePathForTest, "part-0");

	final RecoverableFsDataOutputStream stream = writer.open(path);
	stream.write(testData1.getBytes(StandardCharsets.UTF_8));

	final RecoverableWriter.ResumeRecoverable recoverable1 = stream.persist();
	stream.write(testData2.getBytes(StandardCharsets.UTF_8));

	final RecoverableWriter.ResumeRecoverable recoverable2 = stream.persist();
	stream.write(testData3.getBytes(StandardCharsets.UTF_8));

	final RecoverableFsDataOutputStream recoveredStream = writer.recover(recoverable1);
	recoveredStream.closeForCommit().commit();

	// this should throw an exception
	final RecoverableFsDataOutputStream newRecoveredStream = writer.recover(recoverable2);
	newRecoveredStream.closeForCommit().commit();
}
 
Example 6
Source File: HadoopS3RecoverableWriterITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testCallingDeleteObjectTwiceDoesNotThroughException() throws Exception {
	final RecoverableWriter writer = getRecoverableWriter();
	final Path path = new Path(basePathForTest, "part-0");

	final RecoverableFsDataOutputStream stream = writer.open(path);
	stream.write(bytesOf(testData1));
	S3Recoverable recoverable = (S3Recoverable) stream.persist();

	stream.closeForCommit().commit();

	// still the data is there as we have not deleted them from the tmp object
	final String content = getContentsOfFile(new Path('/' + recoverable.incompleteObjectName()));
	Assert.assertEquals(testData1, content);

	boolean successfullyDeletedState = writer.cleanupRecoverableState(recoverable);
	Assert.assertTrue(successfullyDeletedState);

	boolean unsuccessfulDeletion = writer.cleanupRecoverableState(recoverable);
	Assert.assertFalse(unsuccessfulDeletion);
}
 
Example 7
Source File: HadoopS3RecoverableWriterExceptionITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test(expected = IOException.class)
public void testResumeAfterCommit() throws Exception {
	final RecoverableWriter writer = getFileSystem().createRecoverableWriter();
	final Path path = new Path(basePathForTest, "part-0");

	final RecoverableFsDataOutputStream stream = writer.open(path);
	stream.write(testData1.getBytes(StandardCharsets.UTF_8));

	final RecoverableWriter.ResumeRecoverable recoverable = stream.persist();
	stream.write(testData2.getBytes(StandardCharsets.UTF_8));

	stream.closeForCommit().commit();

	final RecoverableFsDataOutputStream recoveredStream = writer.recover(recoverable);
	recoveredStream.closeForCommit().commit();
}
 
Example 8
Source File: HadoopS3RecoverableWriterITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test(expected = FileNotFoundException.class)
public void testCleanupRecoverableState() throws Exception {
	final RecoverableWriter writer = getRecoverableWriter();
	final Path path = new Path(basePathForTest, "part-0");

	final RecoverableFsDataOutputStream stream = writer.open(path);
	stream.write(bytesOf(testData1));
	S3Recoverable recoverable = (S3Recoverable) stream.persist();

	stream.closeForCommit().commit();

	// still the data is there as we have not deleted them from the tmp object
	final String content = getContentsOfFile(new Path('/' + recoverable.incompleteObjectName()));
	Assert.assertEquals(testData1, content);

	boolean successfullyDeletedState = writer.cleanupRecoverableState(recoverable);
	Assert.assertTrue(successfullyDeletedState);

	// this should throw the exception as we deleted the file.
	getContentsOfFile(new Path('/' + recoverable.incompleteObjectName()));
}
 
Example 9
Source File: HadoopRecoverableWriterOldHadoopWithNoTruncateSupportTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testExceptionThrownWhenRecoveringWithInProgressFile() throws IOException {
	final RecoverableWriter writerUnderTest = fileSystem.createRecoverableWriter();
	final RecoverableFsDataOutputStream stream = writerUnderTest.open(new Path(basePath, "test-2"));
	final RecoverableWriter.ResumeRecoverable recoverable = stream.persist();
	assertNotNull(recoverable);

	try {
		writerUnderTest.recover(recoverable);
	} catch (IOException e) {
		// this is the expected exception and we check also if the root cause is the hadoop < 2.7 version
		assertTrue(e.getCause() instanceof IllegalStateException);
	}
}
 
Example 10
Source File: HadoopS3RecoverableWriterITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testCommitAfterPersist() throws Exception {
	final RecoverableWriter writer = getRecoverableWriter();
	final Path path = new Path(basePathForTest, "part-0");

	final RecoverableFsDataOutputStream stream = writer.open(path);
	stream.write(bytesOf(testData1));
	stream.persist();

	stream.write(bytesOf(testData2));
	stream.closeForCommit().commit();

	Assert.assertEquals(testData1 + testData2, getContentsOfFile(path));
}
 
Example 11
Source File: HadoopS3RecoverableWriterITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
private void testResumeAfterMultiplePersist(
		final String persistName,
		final String expectedFinalContents,
		final String firstItemToWrite,
		final String secondItemToWrite,
		final String thirdItemToWrite) throws Exception {

	final Path path = new Path(basePathForTest, "part-0");
	final RecoverableWriter initWriter = getRecoverableWriter();

	final Map<String, RecoverableWriter.ResumeRecoverable> recoverables = new HashMap<>(4);
	try (final RecoverableFsDataOutputStream stream = initWriter.open(path)) {
		recoverables.put(INIT_EMPTY_PERSIST, stream.persist());

		stream.write(bytesOf(firstItemToWrite));

		recoverables.put(INTERM_WITH_STATE_PERSIST, stream.persist());
		recoverables.put(INTERM_WITH_NO_ADDITIONAL_STATE_PERSIST, stream.persist());

		// and write some more data
		stream.write(bytesOf(secondItemToWrite));

		recoverables.put(FINAL_WITH_EXTRA_STATE, stream.persist());
	}

	final SimpleVersionedSerializer<RecoverableWriter.ResumeRecoverable> serializer = initWriter.getResumeRecoverableSerializer();
	final byte[] serializedRecoverable = serializer.serialize(recoverables.get(persistName));

	// get a new serializer from a new writer to make sure that no pre-initialized state leaks in.
	final RecoverableWriter newWriter = getRecoverableWriter();
	final SimpleVersionedSerializer<RecoverableWriter.ResumeRecoverable> deserializer = newWriter.getResumeRecoverableSerializer();
	final RecoverableWriter.ResumeRecoverable recoveredRecoverable =
			deserializer.deserialize(serializer.getVersion(), serializedRecoverable);

	final RecoverableFsDataOutputStream recoveredStream = newWriter.recover(recoveredRecoverable);
	recoveredStream.write(bytesOf(thirdItemToWrite));
	recoveredStream.closeForCommit().commit();

	Assert.assertEquals(expectedFinalContents, getContentsOfFile(path));
}
 
Example 12
Source File: HadoopS3RecoverableWriterITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testCommitAfterNormalClose() throws Exception {
	final RecoverableWriter writer = getRecoverableWriter();
	final Path path = new Path(basePathForTest, "part-0");

	final RecoverableFsDataOutputStream stream = writer.open(path);
	stream.write(bytesOf(testData1));
	stream.closeForCommit().commit();

	Assert.assertEquals(testData1, getContentsOfFile(path));
}
 
Example 13
Source File: HadoopS3RecoverableWriterITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
private void testResumeAfterMultiplePersist(
		final String persistName,
		final String expectedFinalContents,
		final String firstItemToWrite,
		final String secondItemToWrite,
		final String thirdItemToWrite) throws Exception {

	final Path path = new Path(basePathForTest, "part-0");
	final RecoverableWriter initWriter = getRecoverableWriter();

	final Map<String, RecoverableWriter.ResumeRecoverable> recoverables = new HashMap<>(4);
	try (final RecoverableFsDataOutputStream stream = initWriter.open(path)) {
		recoverables.put(INIT_EMPTY_PERSIST, stream.persist());

		stream.write(bytesOf(firstItemToWrite));

		recoverables.put(INTERM_WITH_STATE_PERSIST, stream.persist());
		recoverables.put(INTERM_WITH_NO_ADDITIONAL_STATE_PERSIST, stream.persist());

		// and write some more data
		stream.write(bytesOf(secondItemToWrite));

		recoverables.put(FINAL_WITH_EXTRA_STATE, stream.persist());
	}

	final SimpleVersionedSerializer<RecoverableWriter.ResumeRecoverable> serializer = initWriter.getResumeRecoverableSerializer();
	final byte[] serializedRecoverable = serializer.serialize(recoverables.get(persistName));

	// get a new serializer from a new writer to make sure that no pre-initialized state leaks in.
	final RecoverableWriter newWriter = getRecoverableWriter();
	final SimpleVersionedSerializer<RecoverableWriter.ResumeRecoverable> deserializer = newWriter.getResumeRecoverableSerializer();
	final RecoverableWriter.ResumeRecoverable recoveredRecoverable =
			deserializer.deserialize(serializer.getVersion(), serializedRecoverable);

	final RecoverableFsDataOutputStream recoveredStream = newWriter.recover(recoveredRecoverable);
	recoveredStream.write(bytesOf(thirdItemToWrite));
	recoveredStream.closeForCommit().commit();

	Assert.assertEquals(expectedFinalContents, getContentsOfFile(path));
}
 
Example 14
Source File: HadoopS3RecoverableWriterITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testCommitAfterNormalClose() throws Exception {
	final RecoverableWriter writer = getRecoverableWriter();
	final Path path = new Path(basePathForTest, "part-0");

	final RecoverableFsDataOutputStream stream = writer.open(path);
	stream.write(bytesOf(testData1));
	stream.closeForCommit().commit();

	Assert.assertEquals(testData1, getContentsOfFile(path));
}
 
Example 15
Source File: HadoopS3RecoverableWriterITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testCloseWithNoData() throws Exception {
	final RecoverableWriter writer = getRecoverableWriter();
	final Path path = new Path(basePathForTest, "part-0");

	final RecoverableFsDataOutputStream stream = writer.open(path);

	stream.closeForCommit().commit();
}
 
Example 16
Source File: HadoopS3RecoverableWriterITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void testResumeAfterMultiplePersist(
		final String persistName,
		final String expectedFinalContents,
		final String firstItemToWrite,
		final String secondItemToWrite,
		final String thirdItemToWrite) throws Exception {

	final Path path = new Path(basePathForTest, "part-0");
	final RecoverableWriter initWriter = getRecoverableWriter();

	final Map<String, RecoverableWriter.ResumeRecoverable> recoverables = new HashMap<>(4);
	try (final RecoverableFsDataOutputStream stream = initWriter.open(path)) {
		recoverables.put(INIT_EMPTY_PERSIST, stream.persist());

		stream.write(bytesOf(firstItemToWrite));

		recoverables.put(INTERM_WITH_STATE_PERSIST, stream.persist());
		recoverables.put(INTERM_WITH_NO_ADDITIONAL_STATE_PERSIST, stream.persist());

		// and write some more data
		stream.write(bytesOf(secondItemToWrite));

		recoverables.put(FINAL_WITH_EXTRA_STATE, stream.persist());
	}

	final SimpleVersionedSerializer<RecoverableWriter.ResumeRecoverable> serializer = initWriter.getResumeRecoverableSerializer();
	final byte[] serializedRecoverable = serializer.serialize(recoverables.get(persistName));

	// get a new serializer from a new writer to make sure that no pre-initialized state leaks in.
	final RecoverableWriter newWriter = getRecoverableWriter();
	final SimpleVersionedSerializer<RecoverableWriter.ResumeRecoverable> deserializer = newWriter.getResumeRecoverableSerializer();
	final RecoverableWriter.ResumeRecoverable recoveredRecoverable =
			deserializer.deserialize(serializer.getVersion(), serializedRecoverable);

	final RecoverableFsDataOutputStream recoveredStream = newWriter.recover(recoveredRecoverable);
	recoveredStream.write(bytesOf(thirdItemToWrite));
	recoveredStream.closeForCommit().commit();

	Assert.assertEquals(expectedFinalContents, getContentsOfFile(path));
}
 
Example 17
Source File: HadoopS3RecoverableWriterITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testCommitAfterPersist() throws Exception {
	final RecoverableWriter writer = getRecoverableWriter();
	final Path path = new Path(basePathForTest, "part-0");

	final RecoverableFsDataOutputStream stream = writer.open(path);
	stream.write(bytesOf(testData1));
	stream.persist();

	stream.write(bytesOf(testData2));
	stream.closeForCommit().commit();

	Assert.assertEquals(testData1 + testData2, getContentsOfFile(path));
}
 
Example 18
Source File: HadoopS3RecoverableWriterITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testCommitAfterNormalClose() throws Exception {
	final RecoverableWriter writer = getRecoverableWriter();
	final Path path = new Path(basePathForTest, "part-0");

	final RecoverableFsDataOutputStream stream = writer.open(path);
	stream.write(bytesOf(testData1));
	stream.closeForCommit().commit();

	Assert.assertEquals(testData1, getContentsOfFile(path));
}
 
Example 19
Source File: HadoopS3RecoverableWriterITCase.java    From Flink-CEPplus with Apache License 2.0 3 votes vote down vote up
@Test
public void testCommitAfterRecovery() throws Exception {
	final Path path = new Path(basePathForTest, "part-0");

	final RecoverableWriter initWriter = getRecoverableWriter();

	final RecoverableFsDataOutputStream stream = initWriter.open(path);
	stream.write(bytesOf(testData1));

	stream.persist();
	stream.persist();

	// and write some more data
	stream.write(bytesOf(testData2));

	final RecoverableWriter.CommitRecoverable recoverable = stream.closeForCommit().getRecoverable();

	final byte[] serializedRecoverable = initWriter.getCommitRecoverableSerializer().serialize(recoverable);

	// get a new serializer from a new writer to make sure that no pre-initialized state leaks in.
	final RecoverableWriter newWriter = getRecoverableWriter();

	final SimpleVersionedSerializer<RecoverableWriter.CommitRecoverable> deserializer = newWriter.getCommitRecoverableSerializer();
	final RecoverableWriter.CommitRecoverable recoveredRecoverable = deserializer.deserialize(deserializer.getVersion(), serializedRecoverable);

	final RecoverableFsDataOutputStream.Committer committer = newWriter.recoverForCommit(recoveredRecoverable);
	committer.commitAfterRecovery();

	Assert.assertEquals(testData1 + testData2, getContentsOfFile(path));
}
 
Example 20
Source File: HadoopS3RecoverableWriterITCase.java    From flink with Apache License 2.0 3 votes vote down vote up
@Test
public void testCommitAfterRecovery() throws Exception {
	final Path path = new Path(basePathForTest, "part-0");

	final RecoverableWriter initWriter = getRecoverableWriter();

	final RecoverableFsDataOutputStream stream = initWriter.open(path);
	stream.write(bytesOf(testData1));

	stream.persist();
	stream.persist();

	// and write some more data
	stream.write(bytesOf(testData2));

	final RecoverableWriter.CommitRecoverable recoverable = stream.closeForCommit().getRecoverable();

	final byte[] serializedRecoverable = initWriter.getCommitRecoverableSerializer().serialize(recoverable);

	// get a new serializer from a new writer to make sure that no pre-initialized state leaks in.
	final RecoverableWriter newWriter = getRecoverableWriter();

	final SimpleVersionedSerializer<RecoverableWriter.CommitRecoverable> deserializer = newWriter.getCommitRecoverableSerializer();
	final RecoverableWriter.CommitRecoverable recoveredRecoverable = deserializer.deserialize(deserializer.getVersion(), serializedRecoverable);

	final RecoverableFsDataOutputStream.Committer committer = newWriter.recoverForCommit(recoveredRecoverable);
	committer.commitAfterRecovery();

	Assert.assertEquals(testData1 + testData2, getContentsOfFile(path));
}