-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-25331][SS] Make FileStreamSink ignore partitions of batches that have already been written to file system #22331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...core/src/main/scala/org/apache/spark/sql/execution/streaming/ManifestCommitProtocol.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.execution.streaming | ||
|
|
||
| trait ManifestCommitProtocol { | ||
| @transient protected var fileLog: MetadataLog[Array[SinkFileStatus]] = _ | ||
| protected var batchId: Long = _ | ||
| /** | ||
| * Sets up the manifest log output and the batch id for this job. | ||
| * Must be called before any other function. | ||
| */ | ||
| def setupManifestOptions(fileLog: MetadataLog[Array[SinkFileStatus]], batchId: Long): Unit = { | ||
| this.fileLog = fileLog | ||
| this.batchId = batchId | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
...e/src/main/scala/org/apache/spark/sql/execution/streaming/StagingFileCommitProtocol.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.execution.streaming | ||
|
|
||
| import org.apache.hadoop.fs.{FileAlreadyExistsException, FileContext, Path} | ||
| import org.apache.hadoop.mapreduce.{JobContext, TaskAttemptContext} | ||
|
|
||
| import org.apache.spark.internal.Logging | ||
| import org.apache.spark.internal.io.FileCommitProtocol | ||
| import org.apache.spark.internal.io.FileCommitProtocol.TaskCommitMessage | ||
|
|
||
| class StagingFileCommitProtocol(jobId: String, path: String) | ||
| extends FileCommitProtocol with Serializable with Logging | ||
| with ManifestCommitProtocol { | ||
| private var stagingDir: Option[Path] = None | ||
|
|
||
|
|
||
| def jobStagingDir: Path = { | ||
| new Path(new Path(path, "staging"), s"job-$jobId") | ||
| } | ||
|
|
||
| override def setupJob(jobContext: JobContext): Unit = { | ||
| jobStagingDir.getFileSystem(jobContext.getConfiguration).delete(jobStagingDir, true) | ||
| logInfo(s"Job $jobId set up") | ||
| } | ||
|
|
||
|
|
||
| override def setupTask(taskContext: TaskAttemptContext): Unit = { | ||
| stagingDir = Some(new Path(jobStagingDir, s"partition-${partition(taskContext)}")) | ||
| stagingDir.get.getFileSystem(taskContext.getConfiguration).delete(stagingDir.get, true) | ||
| logInfo(s"Task set up to handle partition ${partition(taskContext)} in job $jobId") | ||
|
|
||
| } | ||
|
|
||
| private def partition(taskContext: TaskAttemptContext) = { | ||
| taskContext.getConfiguration.getInt("mapreduce.task.partition", -1) | ||
| } | ||
|
|
||
|
|
||
| override def commitJob(jobContext: JobContext, taskCommits: Seq[TaskCommitMessage]): Unit = { | ||
| val fs = jobStagingDir.getFileSystem(jobContext.getConfiguration) | ||
| val fileCtx = FileContext.getFileContext | ||
|
|
||
| def moveIfPossible(next: Path, target: Path) = { | ||
| try { | ||
| fileCtx.rename(next, target) | ||
| } catch { | ||
| case _: FileAlreadyExistsException => | ||
| val status = fileCtx.getFileStatus(target) | ||
| logWarning(s"File ${target.toUri.toASCIIString} has already been generated " + | ||
| s"earlier (${status.toString}), deleting instead of moving " + | ||
| s"recently generated file: ${fileCtx.getFileStatus(target).toString}") | ||
| fileCtx.delete(next, false) | ||
| } | ||
| } | ||
|
|
||
| def moveEach(from: Path, to: String) = { | ||
| val files = fs.listFiles(from, true) | ||
| val statuses = Array.newBuilder[SinkFileStatus] | ||
| while (files.hasNext) { | ||
| val next = files.next().getPath | ||
| val target = if (next.getParent.getName.startsWith(outputPartitionPrefix)) { | ||
| val subdir = next.getParent.getName.substring(outputPartitionPrefix.length) | ||
| .replaceAll(subdirEscapeSequence, "/") | ||
| val outputPartition = new Path(to, subdir) | ||
| fs.mkdirs(outputPartition) | ||
| new Path(outputPartition, next.getName) | ||
| } else { | ||
| new Path(to, next.getName) | ||
| } | ||
| moveIfPossible(next, target) | ||
| statuses += SinkFileStatus(fs.getFileStatus(target)) | ||
| } | ||
| if (fileLog.add(batchId, statuses.result)) { | ||
| logInfo(s"Job $jobId committed") | ||
| } else { | ||
| throw new IllegalStateException(s"Race while writing batch $batchId") | ||
| } | ||
| } | ||
|
|
||
| moveEach(jobStagingDir, path) | ||
|
|
||
| Seq() | ||
| } | ||
|
|
||
| override def abortJob(jobContext: JobContext): Unit = {} | ||
|
|
||
|
|
||
| private var fileCounter: Int = -1 | ||
|
|
||
| private def nextCounter: Int = { | ||
| fileCounter += 1 | ||
| fileCounter | ||
| } | ||
|
|
||
| private val outputPartitionPrefix = "part_prefix_" | ||
|
|
||
| private val subdirEscapeSequence = "___per___" | ||
|
|
||
| override def newTaskTempFile( | ||
| taskContext: TaskAttemptContext, dir: Option[String], ext: String): String = { | ||
| val staging = stagingDir.getOrElse( | ||
| throw new IllegalStateException("Staging dir needs to be initilized in setupTask()")) | ||
| val targetDir = dir.map(d => new Path(staging, stagingReplacementDir(d))).getOrElse(staging) | ||
| val res = new Path(targetDir, s"part-j$jobId-p${partition(taskContext)}-c$nextCounter$ext") | ||
| .toString | ||
| logInfo(s"New file generated $res") | ||
| res | ||
| } | ||
|
|
||
| private def stagingReplacementDir(d: String) = { | ||
| outputPartitionPrefix + d.replaceAll("/", subdirEscapeSequence) | ||
| } | ||
|
|
||
| override def newTaskTempFileAbsPath( | ||
| taskContext: TaskAttemptContext, absoluteDir: String, ext: String): String = { | ||
| throw new UnsupportedOperationException( | ||
| s"$this does not support adding files with an absolute path") | ||
| } | ||
|
|
||
| override def commitTask(taskContext: TaskAttemptContext): TaskCommitMessage = { | ||
| new TaskCommitMessage(None) | ||
| } | ||
|
|
||
| override def abortTask(taskContext: TaskAttemptContext): Unit = {} | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.