forked from datastax/spark-cassandra-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request datastax#976 from datastax/SPARKC-377-master
SPARKC-377 shutdown cluster and client connection sequentially
- Loading branch information
Showing
6 changed files
with
72 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...ndra-connector/src/main/scala/com/datastax/spark/connector/util/SerialShutdownHooks.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.datastax.spark.connector.util | ||
|
||
import scala.collection.mutable | ||
|
||
import org.apache.spark.Logging | ||
|
||
private[connector] object SerialShutdownHooks extends Logging { | ||
|
||
private val hooks = mutable.Map[String, () => Unit]() | ||
@volatile private var isShuttingDown = false | ||
|
||
def add(name: String)(body: () => Unit): Unit = SerialShutdownHooks.synchronized { | ||
if (isShuttingDown) { | ||
logError(s"Adding shutdown hook ($name) during shutting down is not allowed.") | ||
} else { | ||
hooks.put(name, body) | ||
} | ||
} | ||
|
||
Runtime.getRuntime.addShutdownHook(new Thread("Serial shutdown hooks thread") { | ||
override def run(): Unit = { | ||
SerialShutdownHooks.synchronized { | ||
isShuttingDown = true | ||
} | ||
for ((name, task) <- hooks) { | ||
try { | ||
logDebug(s"Running shutdown hook: $name") | ||
task() | ||
logInfo(s"Successfully executed shutdown hook: $name") | ||
} catch { | ||
case exc: Throwable => | ||
logError(s"Shutdown hook ($name) failed", exc) | ||
} | ||
} | ||
} | ||
}) | ||
} |