Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions core/src/main/scala/org/apache/spark/BarrierCoordinator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private[spark] class BarrierCoordinator(

// Record all active stage attempts that make barrier() call(s), and the corresponding internal
// state.
private val states = new ConcurrentHashMap[ContextBarrierId, ContextBarrierState]
private[spark] val states = new ConcurrentHashMap[ContextBarrierId, ContextBarrierState]

override def onStart(): Unit = {
super.onStart()
Expand All @@ -90,14 +90,14 @@ private[spark] class BarrierCoordinator(
* @param numTasks Number of tasks of the barrier stage, all barrier() calls from the stage shall
* collect `numTasks` requests to succeed.
*/
private class ContextBarrierState(
private[spark] class ContextBarrierState(
val barrierId: ContextBarrierId,
val numTasks: Int) {

// There may be multiple barrier() calls from a barrier stage attempt, `barrierEpoch` is used
// to identify each barrier() call. It shall get increased when a barrier() call succeeds, or
// reset when a barrier() call fails due to timeout.
private var barrierEpoch: Int = 0
private[spark] var barrierEpoch: Int = 0
Copy link
Member

Choose a reason for hiding this comment

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

nit: Is it better to add a getter method? This is because to make var visible may cause unexpected update of the variable.

Copy link
Member Author

Choose a reason for hiding this comment

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

Make sense, done in ec8466a


// An array of RPCCallContexts for barrier tasks that are waiting for reply of a barrier()
// call.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* 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.scheduler

import java.util.concurrent.TimeoutException

import scala.concurrent.duration._
import scala.language.postfixOps

import org.apache.spark._
import org.apache.spark.rpc.RpcTimeout

class BarrierCoordinatorSuite extends SparkFunSuite with LocalSparkContext {

/**
* Get the current barrierEpoch from barrierCoordinator.states by ContextBarrierId
*/
def getCurrentBarrierEpoch(
stageId: Int, stageAttemptId: Int, barrierCoordinator: BarrierCoordinator): Int = {
val barrierId = ContextBarrierId(stageId, stageAttemptId)
barrierCoordinator.states.get(barrierId).barrierEpoch
}

test("normal test for single task") {
sc = new SparkContext("local", "test")
val barrierCoordinator = new BarrierCoordinator(5, sc.listenerBus, sc.env.rpcEnv)
val rpcEndpointRef = sc.env.rpcEnv.setupEndpoint("barrierCoordinator", barrierCoordinator)
val stageId = 0
val stageAttemptNumber = 0
rpcEndpointRef.askSync[Unit](
message = RequestToSync(numTasks = 1, stageId, stageAttemptNumber, taskAttemptId = 0,
barrierEpoch = 0),
timeout = new RpcTimeout(5 seconds, "rpcTimeOut"))
// sleep for waiting barrierEpoch value change
Thread.sleep(500)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do not use explicit sleep. It basically means adding 0.5 seconds to total test time and flakyness. Use conditional wait, for example: bfb7439#diff-a90010f459c27926238d7a4ce5a0aca1R107

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for guidance, done in ecf12bd. I'll also pay attention in the future work.

assert(getCurrentBarrierEpoch(stageId, stageAttemptNumber, barrierCoordinator) == 1)
}

test("normal test for multi tasks") {
sc = new SparkContext("local", "test")
val barrierCoordinator = new BarrierCoordinator(5, sc.listenerBus, sc.env.rpcEnv)
val rpcEndpointRef = sc.env.rpcEnv.setupEndpoint("barrierCoordinator", barrierCoordinator)
val numTasks = 3
val stageId = 0
val stageAttemptNumber = 0
val rpcTimeOut = new RpcTimeout(5 seconds, "rpcTimeOut")
// sync request from 3 tasks
(0 until numTasks).foreach { taskId =>
new Thread(s"task-$taskId-thread") {
setDaemon(true)
override def run(): Unit = {
rpcEndpointRef.askSync[Unit](
message = RequestToSync(numTasks, stageId, stageAttemptNumber, taskAttemptId = taskId,
barrierEpoch = 0),
timeout = rpcTimeOut)
}
}.start()
}
// sleep for waiting barrierEpoch value change
Thread.sleep(500)
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

assert(getCurrentBarrierEpoch(stageId, stageAttemptNumber, barrierCoordinator) == 1)
}

test("abnormal test for syncing with illegal barrierId") {
sc = new SparkContext("local", "test")
val barrierCoordinator = new BarrierCoordinator(5, sc.listenerBus, sc.env.rpcEnv)
val rpcEndpointRef = sc.env.rpcEnv.setupEndpoint("barrierCoordinator", barrierCoordinator)
val numTasks = 3
val stageId = 0
val stageAttemptNumber = 0
val rpcTimeOut = new RpcTimeout(5 seconds, "rpcTimeOut")
intercept[SparkException](
rpcEndpointRef.askSync[Unit](
message = RequestToSync(numTasks, stageId, stageAttemptNumber, taskAttemptId = 0,
barrierEpoch = -1), // illegal barrierId = -1
timeout = rpcTimeOut))
}

test("abnormal test for syncing with old barrierId") {
sc = new SparkContext("local", "test")
val barrierCoordinator = new BarrierCoordinator(5, sc.listenerBus, sc.env.rpcEnv)
val rpcEndpointRef = sc.env.rpcEnv.setupEndpoint("barrierCoordinator", barrierCoordinator)
val numTasks = 3
val stageId = 0
val stageAttemptNumber = 0
val rpcTimeOut = new RpcTimeout(5 seconds, "rpcTimeOut")
// sync request from 3 tasks
(0 until numTasks).foreach { taskId =>
new Thread(s"task-$taskId-thread") {
setDaemon(true)
override def run(): Unit = {
rpcEndpointRef.askSync[Unit](
message = RequestToSync(numTasks, stageId, stageAttemptNumber, taskAttemptId = taskId,
barrierEpoch = 0),
timeout = rpcTimeOut)
}
}.start()
}
// sleep for waiting barrierEpoch value change
Thread.sleep(500)
assert(getCurrentBarrierEpoch(stageId, stageAttemptNumber, barrierCoordinator) == 1)
intercept[SparkException](
rpcEndpointRef.askSync[Unit](
message = RequestToSync(numTasks, stageId, stageAttemptNumber, taskAttemptId = 0,
barrierEpoch = 0),
timeout = rpcTimeOut))
}

test("abnormal test for timeout when rpcTimeOut < barrierTimeOut") {
sc = new SparkContext("local", "test")
val barrierCoordinator = new BarrierCoordinator(2, sc.listenerBus, sc.env.rpcEnv)
val rpcEndpointRef = sc.env.rpcEnv.setupEndpoint("barrierCoordinator", barrierCoordinator)
val numTasks = 3
val stageId = 0
val stageAttemptNumber = 0
val rpcTimeOut = new RpcTimeout(1 seconds, "rpcTimeOut")
intercept[TimeoutException](
rpcEndpointRef.askSync[Unit](
message = RequestToSync(numTasks, stageId, stageAttemptNumber, taskAttemptId = 0,
barrierEpoch = 0),
timeout = rpcTimeOut))
}

test("abnormal test for timeout when rpcTimeOut > barrierTimeOut") {
sc = new SparkContext("local", "test")
val barrierCoordinator = new BarrierCoordinator(2, sc.listenerBus, sc.env.rpcEnv)
val rpcEndpointRef = sc.env.rpcEnv.setupEndpoint("barrierCoordinator", barrierCoordinator)
val numTasks = 3
val stageId = 0
val stageAttemptNumber = 0
val rpcTimeOut = new RpcTimeout(4 seconds, "rpcTimeOut")
intercept[SparkException](
rpcEndpointRef.askSync[Unit](
message = RequestToSync(numTasks, stageId, stageAttemptNumber, taskAttemptId = 0,
barrierEpoch = 0),
timeout = rpcTimeOut))
}
}