You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2021/03/03 20:29:43 UTC

[GitHub] [openwhisk] bdoyle0182 commented on a change in pull request #5061: [New Scheduler] Implement InvokerHealthyManager

bdoyle0182 commented on a change in pull request #5061:
URL: https://github.com/apache/openwhisk/pull/5061#discussion_r586756142



##########
File path: core/invoker/src/main/scala/org/apache/openwhisk/core/containerpool/v2/InvokerHealthyManager.scala
##########
@@ -0,0 +1,384 @@
+/*
+ * 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.openwhisk.core.containerpool.v2
+
+import akka.actor.Status.{Failure => FailureMessage}
+import akka.actor.{Actor, ActorRef, ActorRefFactory, ActorSystem, FSM, Props, Stash}
+import akka.util.Timeout
+import org.apache.openwhisk.common._
+import org.apache.openwhisk.core.connector._
+import org.apache.openwhisk.core.database.{ArtifactStore, NoDocumentException}
+import org.apache.openwhisk.core.entitlement.Privilege
+import org.apache.openwhisk.core.entity.size._
+import org.apache.openwhisk.core.entity.types.EntityStore
+import org.apache.openwhisk.core.entity.{ActivationResponse => _, _}
+import org.apache.openwhisk.core.etcd.EtcdKV.InvokerKeys
+import org.apache.openwhisk.core.service.UpdateDataOnChange
+
+import scala.concurrent.duration._
+import scala.concurrent.{ExecutionContext, Future}
+import scala.util.{Failure, Success}
+
+class InvokerHealthManager(instanceId: InvokerInstanceId,
+                           healthContainerProxyFactory: (ActorRefFactory, ActorRef) => ActorRef,
+                           dataManagementService: ActorRef,
+                           entityStore: ArtifactStore[WhiskEntity])(implicit actorSystem: ActorSystem, logging: Logging)
+    extends FSM[InvokerState, InvokerHealthData]
+    with Stash {
+
+  implicit val requestTimeout = Timeout(5.seconds)
+  implicit val ec: ExecutionContext = actorSystem.dispatcher
+  implicit val transid: TransactionId = TransactionId.invokerHealth
+
+  private[containerpool] var healthActionProxy: Option[ActorRef] = None
+
+  startWith(
+    Offline,
+    InvokerInfo(
+      new RingBuffer[Boolean](InvokerHealthManager.bufferSize),
+      memory = MemoryInfo(instanceId.userMemory.toMB, 0, 0)))
+
+  when(Offline) {
+    case Event(GracefulShutdown, _: InvokerInfo) =>
+      logging.warn(this, "Received a graceful shutdown flag, stopping the invoker.")
+      stay
+
+    case Event(Enable, _) =>
+      InvokerHealthManager.prepare(entityStore, instanceId).map { _ =>
+        startTestAction(self)
+      }
+      goto(Unhealthy)
+  }
+
+  when(Unhealthy) {
+    case Event(ContainerRemoved, _) =>
+      healthActionProxy = None
+      startTestAction(self)
+      stay
+
+    case Event(msg: FailureMessage, _) =>
+      logging.error(this, s"invoker${instanceId}, status:${stateName} got a failure message: ${msg}")
+      stay
+
+    case Event(ContainerCreationFailed(_), _) =>
+      stay
+  }
+
+  when(Healthy) {
+    case Event(msg: FailureMessage, _) =>
+      logging.error(this, s"invoker${instanceId}, status:${stateName} got a failure message: ${msg}")
+      goto(Unhealthy)
+  }
+
+  whenUnhandled {
+    case Event(_: Initialized, _) =>
+      // Initialized messages sent by ContainerProxy for HealthManger
+      stay()
+
+    case Event(ContainerRemoved, _) =>
+      // Drop messages sent by ContainerProxy for HealthManger
+      healthActionProxy = None
+      stay()
+
+    case Event(GracefulShutdown, _) =>
+      self ! GracefulShutdown
+      goto(Offline)
+
+    case Event(healthMsg: HealthMessage, data: InvokerInfo) =>
+      if (stateName != Offline) {
+        handleHealthMessage(healthMsg.state, data.buffer)
+      } else {
+        stay
+      }
+
+    case Event(memoryInfo: MemoryInfo, data: InvokerInfo) =>
+      publishHealthStatusAndStay(stateName, data.copy(memory = memoryInfo))
+
+    // in case of StatusRuntimeException: NOT_FOUND: etcdserver: requested lease not found, we need to get the lease again.
+    case Event(t: FailureMessage, _) =>
+      logging.error(this, s"Failure happens, restart InvokerHealthManager: ${t}")
+      goto(Offline)
+  }
+
+  // It is important to note that stateName and the stateData in onTransition callback refer to the previous one.
+  // We should access to the next data with nextStateData
+  onTransition {
+    case Offline -> Unhealthy =>
+      publishHealthStatusAndStay(Unhealthy, nextStateData)
+
+    case Healthy -> Unhealthy =>
+      unstashAll()
+      transid.mark(
+        this,
+        LoggingMarkers.LOADBALANCER_INVOKER_STATUS_CHANGE(Unhealthy.asString),
+        s"invoker${instanceId.toInt} is unhealthy",
+        akka.event.Logging.WarningLevel)
+      startTestAction(self)
+      publishHealthStatusAndStay(Unhealthy, nextStateData)
+
+    case _ -> Healthy =>
+      logging.info(this, s"invoker became healthy, stop health action proxy.")
+      unstashAll()
+      stopTestAction()
+
+      publishHealthStatusAndStay(Healthy, nextStateData)
+
+    case Offline -> Offline =>

Review comment:
       nit: can't this be removed? you can just leave out state transitions that don't do anything for this partial function




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org