Github user vanzin commented on a diff in the pull request: https://github.com/apache/spark/pull/9214#discussion_r43666862 --- Diff: core/src/main/scala/org/apache/spark/shuffle/ShuffleOutputCoordinator.scala --- @@ -0,0 +1,105 @@ +/* + * 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.shuffle + +import java.io.{FileOutputStream, FileInputStream, File} + +import com.google.common.annotations.VisibleForTesting + +import org.apache.spark.storage.ShuffleMapStatusBlockId +import org.apache.spark.{SparkEnv, Logging} +import org.apache.spark.scheduler.MapStatus +import org.apache.spark.serializer.SerializerInstance + +/** + * Ensures that on each executor, there are no conflicting writes to the same shuffle files. It + * implements "first write wins", by atomically moving all shuffle files into their final location, + * only if the files did not already exist. See SPARK-8029 + */ +private[spark] object ShuffleOutputCoordinator extends Logging { + + /** + * If any of the destination files do not exist, then move all of the temporary files to their + * destinations, and return (true, the given MapStatus). If all destination files exist, then + * delete all temporary files, and return (false, the MapStatus from previously committed shuffle + * output). + * + * Note that this will write to all destination files. If the tmp file is missing, then a + * zero-length destination file will be created. This is so the ShuffleOutputCoordinator can work + * even when there is a non-determinstic data, where the output exists in one attempt, but is + * empty in another attempt. + * + * @param tmpToDest Seq of (temporary, destination) file pairs + * @param mapStatus the [[MapStatus]] for the output already written to the the temporary files + * @return pair of: (1) true iff the set of temporary files was moved to the destination and (2) + * the MapStatus of the committed attempt. + * + */ + def commitOutputs( + shuffleId: Int, + partitionId: Int, + tmpToDest: Seq[(File, File)], + mapStatus: MapStatus, + sparkEnv: SparkEnv): (Boolean, MapStatus) = synchronized { + val mapStatusFile = sparkEnv.blockManager.diskBlockManager.getFile( + ShuffleMapStatusBlockId(shuffleId, partitionId)) + val ser = sparkEnv.serializer.newInstance() + commitOutputs(shuffleId, partitionId, tmpToDest, mapStatus, mapStatusFile, ser) + } + + @VisibleForTesting + def commitOutputs( + shuffleId: Int, + partitionId: Int, + tmpToDest: Seq[(File, File)], + mapStatus: MapStatus, + mapStatusFile: File, + serializer: SerializerInstance): (Boolean, MapStatus) = synchronized { + val destAlreadyExists = tmpToDest.forall{_._2.exists()} && mapStatusFile.exists() + if (!destAlreadyExists) { + tmpToDest.foreach { case (tmp, dest) => + // If *some* of the destination files exist, but not all of them, then its not clear + // what to do. There could be a task already reading from this dest file when we delete --- End diff -- So, this justification feels a little weird to me. Let's say you have t1 and t2. * if t1 finishes and writes "n" files, and t2 finishes and writes "n + 1" files, then you'll get the output of t2 * if instead t2 also creates "n" files, you'll get the output of t1 instead. Or am I misunderstanding something? Also, the "if a task is already reading from the file" case will probably make this whole block of code fail on Windows; I believe `File.delete()` will fail if the file is open, unlike on POSIX fses.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org