-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[New Scheduler]Add CreationJobManager #5116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ningyougang
merged 6 commits into
apache:master
from
jiangpengcheng:add_creation_job_manager
Jun 7, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
29487c7
Add CreationJobManager
jiangpengcheng ca88e74
Remove unused import
jiangpengcheng 140b5e3
Use finite duration string for a config
jiangpengcheng 219c069
Fix tests
jiangpengcheng 65ce158
Resolve rebase conflicts
jiangpengcheng 58d6fb9
Fix tests
jiangpengcheng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
231 changes: 231 additions & 0 deletions
231
...ler/src/main/scala/org/apache/openwhisk/core/scheduler/container/CreationJobManager.scala
This file contains hidden or 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,231 @@ | ||
/* | ||
* 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.scheduler.container | ||
|
||
import java.nio.charset.StandardCharsets | ||
import java.util.concurrent.TimeUnit | ||
import akka.actor.{Actor, ActorRef, ActorRefFactory, ActorSystem, Cancellable, Props} | ||
import org.apache.openwhisk.common.{GracefulShutdown, Logging} | ||
import org.apache.openwhisk.core.connector._ | ||
import org.apache.openwhisk.core.entity._ | ||
import org.apache.openwhisk.core.etcd.EtcdKV.ContainerKeys.inProgressContainer | ||
import org.apache.openwhisk.core.service.{RegisterData, UnregisterData} | ||
import org.apache.openwhisk.core.ConfigKeys | ||
import org.apache.openwhisk.core.scheduler.message.{ | ||
CreationJobState, | ||
FailedCreationJob, | ||
FinishCreationJob, | ||
RegisterCreationJob, | ||
ReschedulingCreationJob, | ||
SuccessfulCreationJob | ||
} | ||
import org.apache.openwhisk.core.scheduler.queue.{MemoryQueueKey, QueuePool} | ||
import pureconfig.loadConfigOrThrow | ||
|
||
import scala.collection.concurrent.TrieMap | ||
import scala.concurrent.duration._ | ||
import scala.concurrent.{ExecutionContext, Future} | ||
|
||
case object GetPoolStatus | ||
|
||
case class JobEntry(action: FullyQualifiedEntityName, timer: Cancellable) | ||
|
||
class CreationJobManager(feedFactory: (ActorRefFactory, String, Int, Array[Byte] => Future[Unit]) => ActorRef, | ||
schedulerInstanceId: SchedulerInstanceId, | ||
dataManagementService: ActorRef)(implicit actorSystem: ActorSystem, logging: Logging) | ||
extends Actor { | ||
private implicit val ec: ExecutionContext = actorSystem.dispatcher | ||
private val baseTimeout = loadConfigOrThrow[FiniteDuration](ConfigKeys.schedulerInProgressJobRetentionSecond) | ||
private val retryLimit = 5 | ||
private val retryDelayTime = 100.milliseconds | ||
|
||
/** | ||
* Store a JobEntry in local to get an alarm for key timeout | ||
* It does not matter whether the information stored in Local is redundant or null. | ||
* When a new JobEntry is created, it is overwritten if it is duplicated. | ||
* If there is no corresponding JobEntry at the time of deletion, nothing is done. | ||
*/ | ||
protected val creationJobPool = TrieMap[CreationId, JobEntry]() | ||
|
||
override def receive: Receive = { | ||
case RegisterCreationJob( | ||
ContainerCreationMessage(_, invocationNamespace, action, revision, actionMetaData, _, _, _, _, creationId)) => | ||
val isBlackboxInvocation = actionMetaData.toExecutableWhiskAction.exists(a => a.exec.pull) | ||
registerJob(invocationNamespace, action, revision, creationId, isBlackboxInvocation) | ||
|
||
case FinishCreationJob( | ||
ContainerCreationAckMessage( | ||
tid, | ||
creationId, | ||
invocationNamespace, | ||
action, | ||
revision, | ||
actionMetaData, | ||
_, | ||
schedulerHost, | ||
rpcPort, | ||
retryCount, | ||
error, | ||
reason)) => | ||
if (error.isEmpty) { | ||
logging.info(this, s"[$creationId] create container successfully") | ||
deleteJob( | ||
invocationNamespace, | ||
action, | ||
revision, | ||
creationId, | ||
SuccessfulCreationJob(creationId, invocationNamespace, action, revision)) | ||
|
||
} else { | ||
val cause = reason.getOrElse("unknown reason") | ||
// if exceed the retry limit or meet errors which we don't need to reschedule, make it a failure | ||
if (retryCount >= retryLimit || !error.exists(ContainerCreationError.whiskErrors.contains)) { | ||
logging.error( | ||
this, | ||
s"[$creationId] Failed to create container $retryCount/$retryLimit times for $cause. Finished creation") | ||
// Delete from pool after all retries are failed | ||
deleteJob( | ||
invocationNamespace, | ||
action, | ||
revision, | ||
creationId, | ||
FailedCreationJob(creationId, invocationNamespace, action, revision, error.get, cause)) | ||
} else { | ||
// Reschedule | ||
logging.error( | ||
this, | ||
s"[$creationId] Failed to create container $retryCount/$retryLimit times for $cause. Started rescheduling") | ||
// Add some time interval during retry create container, because etcd put operation needs some time if data inconsistant happens | ||
actorSystem.scheduler.scheduleOnce(retryDelayTime) { | ||
context.parent ! ReschedulingCreationJob( | ||
tid, | ||
creationId, | ||
invocationNamespace, | ||
action, | ||
revision, | ||
actionMetaData, | ||
schedulerHost, | ||
rpcPort, | ||
|
||
} | ||
} | ||
} | ||
|
||
case GracefulShutdown => | ||
ackFeed ! GracefulShutdown | ||
} | ||
|
||
private def registerJob(invocationNamespace: String, | ||
action: FullyQualifiedEntityName, | ||
revision: DocRevision, | ||
creationId: CreationId, | ||
isBlackboxInvocation: Boolean) = { | ||
creationJobPool getOrElseUpdate (creationId, { | ||
val key = inProgressContainer(invocationNamespace, action, revision, schedulerInstanceId, creationId) | ||
dataManagementService ! RegisterData(key, "", failoverEnabled = false) | ||
JobEntry(action, createTimer(invocationNamespace, action, revision, creationId, isBlackboxInvocation)) | ||
}) | ||
} | ||
|
||
private def deleteJob(invocationNamespace: String, | ||
action: FullyQualifiedEntityName, | ||
revision: DocRevision, | ||
creationId: CreationId, | ||
state: CreationJobState) = { | ||
val key = inProgressContainer(invocationNamespace, action, revision, schedulerInstanceId, creationId) | ||
|
||
// If there is a JobEntry, delete it. | ||
creationJobPool | ||
.remove(creationId) | ||
.foreach(entry => { | ||
sendState(state) | ||
entry.timer.cancel() | ||
}) | ||
|
||
dataManagementService ! UnregisterData(key) | ||
Future.successful({}) | ||
} | ||
|
||
private def sendState(state: CreationJobState): Unit = { | ||
context.parent ! state // send state to ContainerManager | ||
QueuePool.get(MemoryQueueKey(state.invocationNamespace, state.action.toDocId.asDocInfo(state.revision))) match { | ||
case Some(memoryQueueValue) if memoryQueueValue.isLeader => | ||
memoryQueueValue.queue ! state | ||
case _ => | ||
logging.error(this, s"get a $state for a nonexistent memory queue or a follower") | ||
} | ||
} | ||
|
||
protected def createTimer(invocationNamespace: String, | ||
action: FullyQualifiedEntityName, | ||
revision: DocRevision, | ||
creationId: CreationId, | ||
isBlackbox: Boolean): Cancellable = { | ||
val timeout = if (isBlackbox) FiniteDuration(baseTimeout.toSeconds * 3, TimeUnit.SECONDS) else baseTimeout | ||
actorSystem.scheduler.scheduleOnce(timeout) { | ||
logging.warn( | ||
this, | ||
s"Failed to create a container for $action(blackbox: $isBlackbox), error: $creationId timed out after $timeout") | ||
creationJobPool | ||
.remove(creationId) | ||
.foreach( | ||
_ => | ||
sendState( | ||
FailedCreationJob( | ||
creationId, | ||
invocationNamespace, | ||
action, | ||
revision, | ||
ContainerCreationError.TimeoutError, | ||
s"timeout waiting for the ack of $creationId after $timeout"))) | ||
dataManagementService ! UnregisterData( | ||
inProgressContainer(invocationNamespace, action, revision, schedulerInstanceId, creationId)) | ||
} | ||
} | ||
|
||
private val topicPrefix = loadConfigOrThrow[String](ConfigKeys.kafkaTopicsPrefix) | ||
private val topic = s"${topicPrefix}creationAck${schedulerInstanceId.asString}" | ||
private val maxActiveAcksPerPoll = 128 | ||
private val ackFeed = feedFactory(actorSystem, topic, maxActiveAcksPerPoll, processAcknowledgement) | ||
|
||
def processAcknowledgement(bytes: Array[Byte]): Future[Unit] = { | ||
Future(ContainerCreationAckMessage.parse(new String(bytes, StandardCharsets.UTF_8))) | ||
.flatMap(Future.fromTry) | ||
.flatMap { msg => | ||
// forward msg to job manager | ||
self ! FinishCreationJob(msg) | ||
ackFeed ! MessageFeed.Processed | ||
Future.successful(()) | ||
} | ||
.recoverWith { | ||
case t => | ||
// Iff everything above failed, we have a terminal error at hand. Either the message failed | ||
// to deserialize, or something threw an error where it is not expected to throw. | ||
ackFeed ! MessageFeed.Processed | ||
logging.error(this, s"terminal failure while processing container creation ack message: $t") | ||
Future.successful(()) | ||
} | ||
} | ||
} | ||
|
||
object CreationJobManager { | ||
def props(feedFactory: (ActorRefFactory, String, Int, Array[Byte] => Future[Unit]) => ActorRef, | ||
schedulerInstanceId: SchedulerInstanceId, | ||
dataManagementService: ActorRef)(implicit actorSystem: ActorSystem, logging: Logging) = | ||
Props(new CreationJobManager(feedFactory, schedulerInstanceId, dataManagementService)) | ||
} |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Scheduler.scala, this value is configurable.
Can we make it configurable as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it seems other places are also hardcoded