Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -70,7 +70,7 @@ case class ExecuteEventsManager(executeHolder: ExecuteHolder, clock: Clock) {

private def sessionStatus = sessionHolder.eventManager.status

private var _status: ExecuteStatus = ExecuteStatus.Pending
@volatile private var _status: ExecuteStatus = ExecuteStatus.Pending

private var error = Option.empty[Boolean]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ private[connect] class ExecuteHolder(
* true if it was not interrupted before, false if it was already interrupted.
*/
def interrupt(): Boolean = {
if (eventsManager.status == ExecuteStatus.Pending) {
return false
Copy link
Member

Choose a reason for hiding this comment

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

  1. According to the function description, false is already occupied for the status where it was already interrupted.

  2. For the code change, according to the state transition, can we change the status to ExecuteStatus.Canceled status directly from the current ExecuteStatus.Pending because it's not started yet? In this case, we can return true.

object ExecuteStatus {
case object Pending extends ExecuteStatus(0)
case object Started extends ExecuteStatus(1)
case object Analyzed extends ExecuteStatus(2)
case object ReadyForExecution extends ExecuteStatus(3)
case object Finished extends ExecuteStatus(4)
case object Failed extends ExecuteStatus(5)
case object Canceled extends ExecuteStatus(6)
case object Closed extends ExecuteStatus(7)
}

Copy link
Member Author

Choose a reason for hiding this comment

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

For the code change, according to the state transition, can we change the status to ExecuteStatus.Canceled status directly from the current ExecuteStatus.Pending because it's not started yet? In this case, we can return true.

Actually, that was my first idea to solve this issue. But as I mentioned in the description, I found that didn't work because transitioning from Pending to Canceled causes another issue.

Copy link
Member Author

Choose a reason for hiding this comment

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

According to the function description, false is already occupied for the status where it was already interrupted.

Hmm, if it's OK to ignore interruption to a pending state operation and we need exactly tell already interrupted from interruption failed, how about returning the exact interruption result rather than boolean?

Copy link
Member

Choose a reason for hiding this comment

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

Got it. Thank you. As long as the code and description are consistent, I'm okay for both. (1) Updating the description by changing the meaning of false and (2) changing the return types.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for your suggestion. I'll simply update the description.

}
runner.interrupt()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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.connect.service

import java.util.UUID

import org.scalatestplus.mockito.MockitoSugar

import org.apache.spark.SparkFunSuite
import org.apache.spark.connect.proto
import org.apache.spark.connect.proto.{ExecutePlanRequest, Plan, UserContext}
import org.apache.spark.sql.test.SharedSparkSession
import org.apache.spark.util.ManualClock

class SparkConnectExecuteHolderSuite
extends SparkFunSuite
with MockitoSugar
with SharedSparkSession {

val DEFAULT_CLOCK = new ManualClock()
val DEFAULT_USER_ID = "1"
val DEFAULT_USER_NAME = "userName"
val DEFAULT_SESSION_ID = UUID.randomUUID.toString
val DEFAULT_QUERY_ID = UUID.randomUUID.toString
val DEFAULT_CLIENT_TYPE = "clientType"

test("SPARK-53339: ExecuteHolder should ignore interruption when execute status is Pending") {
val executeHolder = setupExecuteHolder()
executeHolder.sessionHolder.eventManager.postStarted()

assert(executeHolder.eventsManager.status == ExecuteStatus.Pending)
assert(!executeHolder.interrupt())

executeHolder.eventsManager.postStarted()
assert(executeHolder.eventsManager.status == ExecuteStatus.Started)
assert(executeHolder.interrupt())
}

def setupExecuteHolder(): ExecuteHolder = {
val relation = proto.Relation.newBuilder
.setLimit(proto.Limit.newBuilder.setLimit(10))
.build()

val executePlanRequest = ExecutePlanRequest
.newBuilder()
.setPlan(Plan.newBuilder().setRoot(relation))
.setUserContext(
UserContext
.newBuilder()
.setUserId(DEFAULT_USER_ID)
.setUserName(DEFAULT_USER_NAME))
.setSessionId(DEFAULT_SESSION_ID)
.setOperationId(DEFAULT_QUERY_ID)
.setClientType(DEFAULT_CLIENT_TYPE)
.build()

val sessionHolder = SessionHolder(DEFAULT_USER_ID, DEFAULT_SESSION_ID, spark)
val executeKey = ExecuteKey(executePlanRequest, sessionHolder)
new ExecuteHolder(executeKey, executePlanRequest, sessionHolder)
}
}