ningyougang commented on a change in pull request #5061: URL: https://github.com/apache/openwhisk/pull/5061#discussion_r572519129
########## File path: core/invoker/src/main/scala/org/apache/openwhisk/core/containerpool/v2/InvokerHealthyManager.scala ########## @@ -0,0 +1,396 @@ +/* + * 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.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 => + // this is an initial transition due to startWith, do nothing + + case _ -> newState => + publishHealthStatusAndStay(newState, nextStateData) + + unstashAll() + + } + + private def publishHealthStatusAndStay(state: InvokerState, stateData: InvokerHealthData) = { + stateData match { + case data: InvokerInfo => + val invokerResourceMessage = InvokerResourceMessage( + state.asString, + data.memory.freeMemory, + data.memory.busyMemory, + data.memory.inProgressMemory, + instanceId.tags, + instanceId.dedicatedNamespaces) + InvokerHealthManager.useMemory = invokerResourceMessage.busyMemory + invokerResourceMessage.inProgressMemory + dataManagementService ! UpdateDataOnChange(InvokerKeys.health(instanceId), invokerResourceMessage.serialize) + + stay using data.copy(currentInvokerResource = Some(invokerResourceMessage)) + + case data => + logging.error(this, s"unexpected data is found: $data") + + stay + } + } + + initialize() + + private def startTestAction(manager: ActorRef): Unit = { + val namespace = InvokerHealthManager.healthActionIdentity.namespace.name.asString + val docId = InvokerHealthManager.healthAction(instanceId).get.docid + + WhiskAction.get(entityStore, docId).onComplete { + case Success(action) => + val initialize = Initialize(namespace, action.toExecutableWhiskAction.get, "", 0, transid) + startHealthAction(initialize, manager) + case Failure(t) => logging.error(this, s"get health action error: ${t.getMessage}") + } + } + + private def startHealthAction(initialize: Initialize, manager: ActorRef): Unit = { + healthActionProxy match { + case Some(proxy) => + // make healthContainerProxy's status is Running, then healthContainerProxy can fetch the activation using ActivationServiceClient + proxy ! initialize + case None => + val proxy = healthContainerProxyFactory(context, manager) + proxy ! initialize + healthActionProxy = Some(proxy) + } + } + + def stopTestAction(): Unit = { + healthActionProxy.foreach { + healthActionProxy = None + _ ! GracefulShutdown + } + } + + /** + * This method is to handle health message from ContainerProxy.pub + * It can induce status change. + * + * @param state activation result state + * @param buffer RingBuffer to track status + * @return + */ + def handleHealthMessage(state: Boolean, buffer: RingBuffer[Boolean]): State = { Review comment: Currently, i used a simple method to check invoker's health status. did't check for system error. Maybe after this pr merged, we can submit another pr to optimize it. (I din't have much more good idea for it.) ########## File path: core/invoker/src/main/scala/org/apache/openwhisk/core/containerpool/v2/InvokerHealthyManager.scala ########## @@ -0,0 +1,396 @@ +/* + * 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.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 => + // this is an initial transition due to startWith, do nothing + + case _ -> newState => + publishHealthStatusAndStay(newState, nextStateData) + + unstashAll() + + } + + private def publishHealthStatusAndStay(state: InvokerState, stateData: InvokerHealthData) = { + stateData match { + case data: InvokerInfo => + val invokerResourceMessage = InvokerResourceMessage( + state.asString, + data.memory.freeMemory, + data.memory.busyMemory, + data.memory.inProgressMemory, + instanceId.tags, + instanceId.dedicatedNamespaces) + InvokerHealthManager.useMemory = invokerResourceMessage.busyMemory + invokerResourceMessage.inProgressMemory + dataManagementService ! UpdateDataOnChange(InvokerKeys.health(instanceId), invokerResourceMessage.serialize) + + stay using data.copy(currentInvokerResource = Some(invokerResourceMessage)) + + case data => + logging.error(this, s"unexpected data is found: $data") + + stay + } + } + + initialize() + + private def startTestAction(manager: ActorRef): Unit = { + val namespace = InvokerHealthManager.healthActionIdentity.namespace.name.asString + val docId = InvokerHealthManager.healthAction(instanceId).get.docid + + WhiskAction.get(entityStore, docId).onComplete { + case Success(action) => + val initialize = Initialize(namespace, action.toExecutableWhiskAction.get, "", 0, transid) + startHealthAction(initialize, manager) + case Failure(t) => logging.error(this, s"get health action error: ${t.getMessage}") + } + } + + private def startHealthAction(initialize: Initialize, manager: ActorRef): Unit = { + healthActionProxy match { + case Some(proxy) => + // make healthContainerProxy's status is Running, then healthContainerProxy can fetch the activation using ActivationServiceClient + proxy ! initialize + case None => + val proxy = healthContainerProxyFactory(context, manager) + proxy ! initialize + healthActionProxy = Some(proxy) + } + } + + def stopTestAction(): Unit = { + healthActionProxy.foreach { + healthActionProxy = None + _ ! GracefulShutdown + } + } + + /** + * This method is to handle health message from ContainerProxy.pub + * It can induce status change. + * + * @param state activation result state + * @param buffer RingBuffer to track status + * @return + */ + def handleHealthMessage(state: Boolean, buffer: RingBuffer[Boolean]): State = { Review comment: Currently, i used a simple method to check invoker's health status. did't check for system error. Maybe after this pr merged, we can submit another pr to optimize it. (I didn't have much more good idea for it.) ---------------------------------------------------------------- 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: [email protected]
