You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by ra...@apache.org on 2017/05/26 14:00:23 UTC

[incubator-openwhisk] branch master updated: Avoid dragging memory in the load balancer. (#2294)

This is an automated email from the ASF dual-hosted git repository.

rabbah pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk.git

The following commit(s) were added to refs/heads/master by this push:
       new  09af32f   Avoid dragging memory in the load balancer. (#2294)
09af32f is described below

commit 09af32f730897f1cfede7902348d4a391395b077
Author: Nick Mitchell <st...@users.noreply.github.com>
AuthorDate: Fri May 26 10:00:21 2017 -0400

    Avoid dragging memory in the load balancer. (#2294)
    
    Use a WeakReference to the promise in the timeout handler to avoid capturing and dragging an activation.
---
 .../core/controller/actions/PrimitiveActions.scala   | 20 +++++++++++++-------
 .../core/loadBalancer/LoadBalancerService.scala      | 16 +++++++++++-----
 2 files changed, 24 insertions(+), 12 deletions(-)

diff --git a/core/controller/src/main/scala/whisk/core/controller/actions/PrimitiveActions.scala b/core/controller/src/main/scala/whisk/core/controller/actions/PrimitiveActions.scala
index 6a40b55..d7b5d7a 100644
--- a/core/controller/src/main/scala/whisk/core/controller/actions/PrimitiveActions.scala
+++ b/core/controller/src/main/scala/whisk/core/controller/actions/PrimitiveActions.scala
@@ -21,7 +21,6 @@ import scala.concurrent.Future
 import scala.concurrent.Promise
 import scala.concurrent.TimeoutException
 import scala.concurrent.duration._
-import scala.util.Failure
 
 import akka.actor.ActorSystem
 import spray.json._
@@ -35,7 +34,6 @@ import whisk.core.database.NoDocumentException
 import whisk.core.entity._
 import whisk.core.entity.types.ActivationStore
 import whisk.core.entity.types.EntityStore
-import whisk.utils.ExecutionContextFactory.FutureExtensions
 
 protected[actions] trait PrimitiveActions {
     /** The core collections require backend services to be injected in this trait. */
@@ -150,12 +148,20 @@ protected[actions] trait PrimitiveActions {
                 pollDbForResult(docid, activationId, promise)
         }
 
-        // Will either complete with activation or timeout
-        promise.future
-            .withTimeout(totalWaitTime, new BlockingInvokeTimeout(activationId))
-            .andThen {
-                case Failure(t) => promise.tryFailure(t) // Short-circuit db polling
+        // install a timeout handler; this is the handler for "the action took longer than its Limit"
+        // note the use of WeakReferences; this is to avoid the handler's closure holding on to the
+        // WhiskActivation, which in turn holds on to the full JsObject of the response
+        val promiseRef = new java.lang.ref.WeakReference(promise)
+        actorSystem.scheduler.scheduleOnce(totalWaitTime) {
+            val p = promiseRef.get
+            if (p != null) {
+                p.tryFailure(new BlockingInvokeTimeout(activationId))
             }
+        }
+
+        // return the future. note that pollDbForResult's isCompleted check will protect against unnecessary db activity
+        // that may overlap with a totalWaitTime timeout (because the promise will have already by tryFailure'd)
+        promise.future
     }
 
     /**
diff --git a/core/controller/src/main/scala/whisk/core/loadBalancer/LoadBalancerService.scala b/core/controller/src/main/scala/whisk/core/loadBalancer/LoadBalancerService.scala
index 7361ae7..b3569fe 100644
--- a/core/controller/src/main/scala/whisk/core/loadBalancer/LoadBalancerService.scala
+++ b/core/controller/src/main/scala/whisk/core/loadBalancer/LoadBalancerService.scala
@@ -142,18 +142,24 @@ class LoadBalancerService(config: WhiskConfig, entityStore: EntityStore)(implici
     private def setupActivation(activationId: ActivationId, subject: String, invokerName: String, timeout: FiniteDuration, transid: TransactionId): ActivationEntry = {
         // either create a new promise or reuse a previous one for this activation if it exists
         val entry = activationById.getOrElseUpdate(activationId, {
-            val promise = Promise[WhiskActivation]
-            // store the promise to complete on success, or fail with ActiveAckTimeout if time is up
-            // however, do not remove the entry as this is done only by processCompletion
+
+            // install a timeout handler; this is the handler for "the action took longer than ActiveAckTimeout"
+            // note the use of WeakReferences; this is to avoid the handler's closure holding on to the
+            // WhiskActivation, which in turn holds on to the full JsObject of the response
+            // NOTE: we do not remove the entry from the maps, as this is done only by processCompletion
+            val promiseRef = new java.lang.ref.WeakReference(Promise[WhiskActivation])
             actorSystem.scheduler.scheduleOnce(timeout) {
                 activationById.get(activationId).foreach { _ =>
-                    if (promise.tryFailure(new ActiveAckTimeout(activationId))) {
+                    val p = promiseRef.get
+                    if (p != null && p.tryFailure(new ActiveAckTimeout(activationId))) {
                         logging.info(this, "active response timed out")(transid)
                     }
                 }
             }
-            ActivationEntry(activationId, subject, invokerName, Instant.now(Clock.systemUTC()), promise)
+            ActivationEntry(activationId, subject, invokerName, Instant.now(Clock.systemUTC()), promiseRef.get)
         })
+
+        // add the entry to our maps, for bookkeeping
         activationByInvoker.getOrElseUpdate(invokerName, new TrieSet[ActivationEntry]).put(entry, {})
         activationBySubject.getOrElseUpdate(subject, new TrieSet[ActivationEntry]).put(entry, {})
         entry

-- 
To stop receiving notification emails like this one, please contact
['"commits@openwhisk.apache.org" <co...@openwhisk.apache.org>'].