Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ case class FileWriterFactory (
@transient private lazy val jobId = SparkHadoopWriterUtils.createJobID(jobTrackerID, 0)

override def createWriter(partitionId: Int, realTaskId: Long): DataWriter[InternalRow] = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to this pr, why not naming it as taskAttemptId ? does realTaskId can be something else ?

val taskAttemptContext = createTaskAttemptContext(partitionId)
val taskAttemptContext = createTaskAttemptContext(partitionId, realTaskId.toInt & Int.MaxValue)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it the same as math.abs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it same as Math.abs(realTaskId.toInt)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

val sparkAttemptNumber = TaskContext.get().taskAttemptId().toInt & Int.MaxValue

sparkAttemptNumber = taskContext.taskAttemptId().toInt & Integer.MAX_VALUE,

There are at least two other similar cases here, should we unify them as math.abs? Of course, this should be another PR. @cloud-fan

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think perf matters here, and math.abs is definitely more readable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, we can unify the above three cases to math.abs in a follow-up.

Copy link
Contributor

@LuciferYang LuciferYang Jun 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cloud-fan If overflow, realTaskId.toInt & Int.MaxValue and math.abs are not equal:

scala> val realTaskId = Long.MaxValue
val realTaskId: Long = 9223372036854775807

scala> val a = realTaskId.toInt
val a: Int = -1

scala> val b = realTaskId.toInt & Int.MaxValue
val b: Int = 2147483647

scala> val c= math.abs(realTaskId.toInt)
val c: Int = 1
scala> val realTaskId = Int.MaxValue.toLong + 1
val realTaskId: Long = 2147483648

scala> val a = realTaskId.toInt
val a: Int = -2147483648

scala> val b = realTaskId.toInt & Int.MaxValue
val b: Int = 0

scala> val c= math.abs(realTaskId.toInt)
val c: Int = -2147483648

Meanwhile, when an overflow occurs, math.abs may still return a negative value, so I suggest we continue using & Int.MaxValue

committer.setupTask(taskAttemptContext)
if (description.partitionColumns.isEmpty) {
new SingleDirectoryDataWriter(description, taskAttemptContext, committer)
Expand All @@ -47,9 +47,11 @@ case class FileWriterFactory (
}
}

private def createTaskAttemptContext(partitionId: Int): TaskAttemptContextImpl = {
private def createTaskAttemptContext(
partitionId: Int,
realTaskId: Int): TaskAttemptContextImpl = {
val taskId = new TaskID(jobId, TaskType.MAP, partitionId)
val taskAttemptId = new TaskAttemptID(taskId, 0)
val taskAttemptId = new TaskAttemptID(taskId, realTaskId)
// Set up the configuration object
val hadoopConf = description.serializableHadoopConf.value
hadoopConf.set("mapreduce.job.id", jobId.toString)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.datasources.v2

import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.mapreduce.task.TaskAttemptContextImpl
import org.mockito.Mockito._
import org.scalatest.PrivateMethodTester

import org.apache.spark.SparkFunSuite
import org.apache.spark.internal.io.FileCommitProtocol
import org.apache.spark.sql.execution.datasources.WriteJobDescription
import org.apache.spark.util.SerializableConfiguration

class FileWriterFactorySuite extends SparkFunSuite with PrivateMethodTester {

test("SPARK-48484: V2Write uses different TaskAttemptIds for different task attempts") {
val jobDescription = mock(classOf[WriteJobDescription])
when(jobDescription.serializableHadoopConf).thenReturn(
new SerializableConfiguration(new Configuration(false)))
val committer = mock(classOf[FileCommitProtocol])

val writerFactory = FileWriterFactory(jobDescription, committer)
val createTaskAttemptContext =
PrivateMethod[TaskAttemptContextImpl](Symbol("createTaskAttemptContext"))

val attemptContext =
writerFactory.invokePrivate(createTaskAttemptContext(0, 1))
val attemptContext1 =
writerFactory.invokePrivate(createTaskAttemptContext(0, 2))
assert(attemptContext.getTaskAttemptID.getTaskID == attemptContext1.getTaskAttemptID.getTaskID)
assert(attemptContext.getTaskAttemptID.getId != attemptContext1.getTaskAttemptID.getId)
}
}