forked from apache/incubator-livy
-
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.
LIVY-165. Added session heartbeat to help notebooks to prevent leakag…
…e. (apache#247) Notebook applications like Jupyter might crash while livy is running. No one will clean up its corresponding Livy session and will be leaked. Heartbeat is added to address this. To keep a session alive, the notebook application must continously make GET requests to the interactive session. If no GET request is made within the heartbeat interval, livy-server will delete the session regardless to its state (busy, idle). Heartbeat is per session and is controlled by session property "heartbeatTimeoutInSecond". Its default is 0 and it means heartbeat is disabled. To enable heartbeat, please create the session with non-zero "heartbeatTimeoutInSecond" in the create request.
- Loading branch information
1 parent
678839c
commit 69ac11e
Showing
11 changed files
with
253 additions
and
11 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
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
114 changes: 114 additions & 0 deletions
114
server/src/main/scala/com/cloudera/livy/server/interactive/SessionHeartbeat.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,114 @@ | ||
/* | ||
* Licensed to Cloudera, Inc. under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. Cloudera, Inc. 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 com.cloudera.livy.server.interactive | ||
|
||
import java.util.Date | ||
|
||
import scala.concurrent.duration.{Deadline, Duration, FiniteDuration} | ||
|
||
import com.cloudera.livy.sessions.Session.RecoveryMetadata | ||
import com.cloudera.livy.LivyConf | ||
import com.cloudera.livy.server.SessionServlet | ||
import com.cloudera.livy.sessions.{Session, SessionManager} | ||
|
||
/** | ||
* A session trait to provide heartbeat expiration check. | ||
* Note: Session will not expire if heartbeat() was never called. | ||
*/ | ||
trait SessionHeartbeat { | ||
protected val heartbeatTimeout: FiniteDuration | ||
|
||
private var _lastHeartbeat: Date = _ // For reporting purpose | ||
private var heartbeatDeadline: Option[Deadline] = None | ||
|
||
def heartbeat(): Unit = synchronized { | ||
if (heartbeatTimeout > Duration.Zero) { | ||
heartbeatDeadline = Some(heartbeatTimeout.fromNow) | ||
} | ||
|
||
_lastHeartbeat = new Date() | ||
} | ||
|
||
def lastHeartbeat: Date = synchronized { _lastHeartbeat } | ||
|
||
def heartbeatExpired: Boolean = synchronized { heartbeatDeadline.exists(_.isOverdue()) } | ||
} | ||
|
||
/** | ||
* Servlet can mixin this trait to update session's heartbeat | ||
* whenever a /sessions/:id REST call is made. e.g. GET /sessions/:id | ||
* Note: GET /sessions doesn't update heartbeats. | ||
*/ | ||
trait SessionHeartbeatNotifier[S <: Session with SessionHeartbeat, R <: RecoveryMetadata] | ||
extends SessionServlet[S, R] { | ||
|
||
abstract override protected def withUnprotectedSession(fn: (S => Any)): Any = { | ||
super.withUnprotectedSession { s => | ||
s.heartbeat() | ||
fn(s) | ||
} | ||
} | ||
|
||
abstract override protected def withSession(fn: (S => Any)): Any = { | ||
super.withSession { s => | ||
s.heartbeat() | ||
fn(s) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* A SessionManager trait. | ||
* It will create a thread that periodically deletes sessions with expired heartbeat. | ||
*/ | ||
trait SessionHeartbeatWatchdog[S <: Session with SessionHeartbeat, R <: RecoveryMetadata] { | ||
self: SessionManager[S, R] => | ||
|
||
private val watchdogThread = new Thread(s"HeartbeatWatchdog-${self.getClass.getName}") { | ||
override def run(): Unit = { | ||
val interval = livyConf.getTimeAsMs(LivyConf.HEARTBEAT_WATCHDOG_INTERVAL) | ||
info("Heartbeat watchdog thread started.") | ||
while (true) { | ||
deleteExpiredSessions() | ||
Thread.sleep(interval) | ||
} | ||
} | ||
} | ||
|
||
protected def start(): Unit = { | ||
assert(!watchdogThread.isAlive()) | ||
|
||
watchdogThread.setDaemon(true) | ||
watchdogThread.start() | ||
} | ||
|
||
private[interactive] def deleteExpiredSessions(): Unit = { | ||
// Delete takes time. If we use .filter().foreach() here, the time difference between we check | ||
// expiration and the time we delete the session might be huge. To avoid that, check expiration | ||
// inside the foreach block. | ||
sessions.values.foreach { s => | ||
if (s.heartbeatExpired) { | ||
info(s"Session ${s.id} expired. Last heartbeat is at ${s.lastHeartbeat}.") | ||
try { delete(s) } catch { | ||
case t: Throwable => | ||
warn(s"Exception was thrown when deleting expired session ${s.id}", t) | ||
} | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.