org.apache.flink.core.fs.RecoverableFsDataOutputStream Java Examples

The following examples show how to use org.apache.flink.core.fs.RecoverableFsDataOutputStream. 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: HadoopS3RecoverableWriterITCase.java    From Flink-CEPplus 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 #2
Source File: Bucket.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private void restoreInProgressFile(final BucketState<BucketID> state) throws IOException {
	if (!state.hasInProgressResumableFile()) {
		return;
	}

	// we try to resume the previous in-progress file
	final ResumeRecoverable resumable = state.getInProgressResumableFile();

	if (fsWriter.supportsResume()) {
		final RecoverableFsDataOutputStream stream = fsWriter.recover(resumable);
		inProgressPart = partFileFactory.resumeFrom(
				bucketId, stream, resumable, state.getInProgressFileCreationTime());
	} else {
		// if the writer does not support resume, then we close the
		// in-progress part and commit it, as done in the case of pending files.

		fsWriter.recoverForCommit(resumable).commitAfterRecovery();
	}

	if (fsWriter.requiresCleanupOfRecoverableState()) {
		fsWriter.cleanupRecoverableState(resumable);
	}
}
 
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 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 #4
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 #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
Source File: S3RecoverableFsDataOutputStreamTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public RecoverableFsDataOutputStream.Committer snapshotAndGetCommitter() throws IOException {
	lastPersistedIndex = uploadedContent.size();

	return new RecoverableFsDataOutputStream.Committer() {
		@Override
		public void commit() throws IOException {
			published = getPublishedContents();
			uploadedContent.clear();
			lastPersistedIndex = 0;
		}

		@Override
		public void commitAfterRecovery() throws IOException {
			if (published.length == 0) {
				commit();
			}
		}

		@Override
		public RecoverableWriter.CommitRecoverable getRecoverable() {
			return null;
		}
	};
}
 
Example #12
Source File: S3RecoverableFsDataOutputStreamTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public RecoverableFsDataOutputStream.Committer snapshotAndGetCommitter() throws IOException {
	lastPersistedIndex = uploadedContent.size();

	return new RecoverableFsDataOutputStream.Committer() {
		@Override
		public void commit() throws IOException {
			published = getPublishedContents();
			uploadedContent.clear();
			lastPersistedIndex = 0;
		}

		@Override
		public void commitAfterRecovery() throws IOException {
			if (published.length == 0) {
				commit();
			}
		}

		@Override
		public RecoverableWriter.CommitRecoverable getRecoverable() {
			return null;
		}
	};
}
 
Example #13
Source File: HadoopRecoverableWriterOldHadoopWithNoTruncateSupportTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testRecoveryAfterClosingForCommitWorks() throws IOException {
	final Path testPath = new Path(basePath, "test-1");
	final String expectedContent = "test_line";

	final RecoverableWriter writerUnderTest = fileSystem.createRecoverableWriter();
	final RecoverableFsDataOutputStream streamUnderTest =
			getOpenStreamToFileWithContent(writerUnderTest, testPath, expectedContent);

	final RecoverableWriter.CommitRecoverable committable =
			streamUnderTest.closeForCommit().getRecoverable();

	writerUnderTest.recoverForCommit(committable).commitAfterRecovery();

	verifyFileContent(testPath, expectedContent);
}
 
Example #14
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 #15
Source File: S3RecoverableWriter.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public RecoverableFsDataOutputStream open(Path path) throws IOException {
	final RecoverableMultiPartUpload upload = uploadFactory.getNewRecoverableUpload(path);

	return S3RecoverableFsDataOutputStream.newStream(
			upload,
			tempFileCreator,
			userDefinedMinPartSize);
}
 
Example #16
Source File: S3RecoverableFsDataOutputStreamTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void noWritesShouldResolveInAnEmptyFile() throws IOException {
	RecoverableFsDataOutputStream.Committer committer = streamUnderTest.closeForCommit();
	committer.commit();

	assertThat(multipartUploadUnderTest, hasContent(new byte[0]));
}
 
Example #17
Source File: LocalRecoverableWriter.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public RecoverableFsDataOutputStream recover(ResumeRecoverable recoverable) throws IOException {
	if (recoverable instanceof LocalRecoverable) {
		return new LocalRecoverableFsDataOutputStream((LocalRecoverable) recoverable);
	}
	else {
		throw new IllegalArgumentException(
				"LocalFileSystem cannot recover recoverable for other file system: " + recoverable);
	}
}
 
Example #18
Source File: BulkBucketWriter.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public InProgressFileWriter<IN, BucketID> resumeFrom(
		final BucketID bucketId,
		final RecoverableFsDataOutputStream stream,
		final RecoverableWriter.ResumeRecoverable resumable,
		final long creationTime) throws IOException {

	Preconditions.checkNotNull(stream);
	Preconditions.checkNotNull(resumable);

	final BulkWriter<IN> writer = writerFactory.create(stream);
	return new BulkPartWriter<>(bucketId, stream, writer, creationTime);
}
 
Example #19
Source File: LocalRecoverableWriter.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public RecoverableFsDataOutputStream open(Path filePath) throws IOException {
	final File targetFile = fs.pathToFile(filePath);
	final File tempFile = generateStagingTempFilePath(targetFile);

	// try to create the parent
	final File parent = tempFile.getParentFile();
	if (parent != null && !parent.mkdirs() && !parent.exists()) {
		throw new IOException("Failed to create the parent directory: " + parent);
	}

	return new LocalRecoverableFsDataOutputStream(targetFile, tempFile);
}
 
Example #20
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 #21
Source File: BucketStateSerializerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializationOnlyInProgress() throws IOException {
	final File testFolder = tempFolder.newFolder();
	final FileSystem fs = FileSystem.get(testFolder.toURI());

	final Path testBucket = new Path(testFolder.getPath(), "test");

	final RecoverableWriter writer = fs.createRecoverableWriter();
	final RecoverableFsDataOutputStream stream = writer.open(testBucket);
	stream.write(IN_PROGRESS_CONTENT.getBytes(Charset.forName("UTF-8")));

	final RecoverableWriter.ResumeRecoverable current = stream.persist();

	final BucketState<String> bucketState = new BucketState<>(
			"test", testBucket, Long.MAX_VALUE, current, new HashMap<>());

	final SimpleVersionedSerializer<BucketState<String>> serializer =
			new BucketStateSerializer<>(
					writer.getResumeRecoverableSerializer(),
					writer.getCommitRecoverableSerializer(),
					SimpleVersionedStringSerializer.INSTANCE
			);

	final byte[] bytes = SimpleVersionedSerialization.writeVersionAndSerialize(serializer, bucketState);

	// to simulate that everything is over for file.
	stream.close();

	final BucketState<String> recoveredState =  SimpleVersionedSerialization.readVersionAndDeSerialize(serializer, bytes);

	Assert.assertEquals(testBucket, recoveredState.getBucketPath());

	FileStatus[] statuses = fs.listStatus(testBucket.getParent());
	Assert.assertEquals(1L, statuses.length);
	Assert.assertTrue(
			statuses[0].getPath().getPath().startsWith(
					(new Path(testBucket.getParent(), ".test.inprogress")).getPath())
	);
}
 
Example #22
Source File: HadoopS3RecoverableWriterExceptionITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test(expected = IOException.class)
public void testExceptionWritingAfterCloseForCommit() throws Exception {
	final Path path = new Path(basePathForTest, "part-0");

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

	stream.closeForCommit().getRecoverable();
	stream.write(testData2.getBytes(StandardCharsets.UTF_8));
}
 
Example #23
Source File: S3RecoverableFsDataOutputStreamTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleUsage() throws IOException {
	streamUnderTest.write(bytesOf("hello world"));

	RecoverableFsDataOutputStream.Committer committer = streamUnderTest.closeForCommit();
	committer.commit();

	assertThat(multipartUploadUnderTest, hasContent(bytesOf("hello world")));
}
 
Example #24
Source File: RowWisePartWriter.java    From flink with Apache License 2.0 5 votes vote down vote up
RowWisePartWriter(
		final BucketID bucketId,
		final RecoverableFsDataOutputStream currentPartStream,
		final Encoder<IN> encoder,
		final long creationTime) {
	super(bucketId, currentPartStream, creationTime);
	this.encoder = Preconditions.checkNotNull(encoder);
}
 
Example #25
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 #26
Source File: OutputStreamBasedPartFileWriter.java    From flink with Apache License 2.0 5 votes vote down vote up
OutputStreamBasedPartFileWriter(
	final BucketID bucketID,
	final RecoverableFsDataOutputStream recoverableFsDataOutputStream,
	final long createTime) {
	super(bucketID, createTime);
	this.currentPartStream = recoverableFsDataOutputStream;
}
 
Example #27
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 #28
Source File: LocalRecoverableWriter.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public RecoverableFsDataOutputStream open(Path filePath) throws IOException {
	final File targetFile = fs.pathToFile(filePath);
	final File tempFile = generateStagingTempFilePath(targetFile);

	// try to create the parent
	final File parent = tempFile.getParentFile();
	if (parent != null && !parent.mkdirs() && !parent.exists()) {
		throw new IOException("Failed to create the parent directory: " + parent);
	}

	return new LocalRecoverableFsDataOutputStream(targetFile, tempFile);
}
 
Example #29
Source File: S3RecoverableWriter.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public RecoverableFsDataOutputStream open(Path path) throws IOException {
	final RecoverableMultiPartUpload upload = uploadFactory.getNewRecoverableUpload(path);

	return S3RecoverableFsDataOutputStream.newStream(
			upload,
			tempFileCreator,
			userDefinedMinPartSize);
}
 
Example #30
Source File: S3RecoverableFsDataOutputStreamTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleUsage() throws IOException {
	streamUnderTest.write(bytesOf("hello world"));

	RecoverableFsDataOutputStream.Committer committer = streamUnderTest.closeForCommit();
	committer.commit();

	assertThat(multipartUploadUnderTest, hasContent(bytesOf("hello world")));
}