Skip to content

Commit 0edfab9

Browse files
authored
Submission timeout refactoring (#427)
426 - Submission timeout refactoring
1 parent dd1aa5e commit 0edfab9

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/main/scala/za/co/absa/hyperdrive/trigger/scheduler/executors/spark/SparkExecutor.scala

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ import za.co.absa.hyperdrive.trigger.models.{JobInstance, JobParameters}
1919

2020
import scala.concurrent.{ExecutionContext, Future}
2121
import java.util.UUID.randomUUID
22+
import java.util.concurrent.{CountDownLatch, TimeUnit}
2223

2324
import akka.actor.ActorSystem
2425
import akka.stream.ActorMaterializer
2526

2627
import scala.collection.JavaConverters._
27-
import org.apache.spark.launcher.SparkLauncher
28+
import org.apache.spark.launcher.{SparkAppHandle, SparkLauncher}
2829
import play.api.libs.json.{JsValue, Json}
2930
import play.api.libs.ws.ahc.StandaloneAhcWSClient
3031
import za.co.absa.hyperdrive.trigger.models.enums.JobStatuses._
@@ -51,9 +52,19 @@ object SparkExecutor extends Executor {
5152
val id = randomUUID().toString
5253
val ji = jobInstance.copy(executorJobId = Some(id), jobStatus = Submitting)
5354
updateJob(ji).map { _ =>
54-
val running = getSparkLauncher(id, ji.jobName, ji.jobParameters).launch()
55-
Thread.sleep(SparkExecutorConfig.getSubmitTimeOut)
56-
running.destroyForcibly()
55+
val submitTimeOut = SparkExecutorConfig.getSubmitTimeOut
56+
val latch = new CountDownLatch(1)
57+
val sparkAppHandle = getSparkLauncher(id, ji.jobName, ji.jobParameters).startApplication(new SparkAppHandle.Listener {
58+
override def stateChanged(handle: SparkAppHandle): Unit =
59+
if (handle.getState == SparkAppHandle.State.SUBMITTED) {
60+
latch.countDown()
61+
}
62+
override def infoChanged(handle: SparkAppHandle): Unit = {
63+
// do nothing
64+
}
65+
})
66+
latch.await(submitTimeOut, TimeUnit.MILLISECONDS)
67+
sparkAppHandle.kill()
5768
}
5869
}
5970

0 commit comments

Comments
 (0)