From 1387e68a48a00758ae2634de6eb81944d565aec4 Mon Sep 17 00:00:00 2001 From: Soufiane Jounaid Date: Sun, 9 May 2021 15:45:32 +0200 Subject: serverless: Add support for custom termination policies This change adds support for custom termination policies for function instances. This allows the user to build different strategies for downscaling function instances after they become idle. --- .../opendc/serverless/simulator/SimFunctionDeployer.kt | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'opendc-serverless/opendc-serverless-simulator/src/main') diff --git a/opendc-serverless/opendc-serverless-simulator/src/main/kotlin/org/opendc/serverless/simulator/SimFunctionDeployer.kt b/opendc-serverless/opendc-serverless-simulator/src/main/kotlin/org/opendc/serverless/simulator/SimFunctionDeployer.kt index 2945a279..0605eaac 100644 --- a/opendc-serverless/opendc-serverless-simulator/src/main/kotlin/org/opendc/serverless/simulator/SimFunctionDeployer.kt +++ b/opendc-serverless/opendc-serverless-simulator/src/main/kotlin/org/opendc/serverless/simulator/SimFunctionDeployer.kt @@ -27,6 +27,7 @@ import kotlinx.coroutines.channels.Channel import org.opendc.serverless.service.FunctionObject import org.opendc.serverless.service.deployer.FunctionDeployer import org.opendc.serverless.service.deployer.FunctionInstance +import org.opendc.serverless.service.deployer.FunctionInstanceListener import org.opendc.serverless.service.deployer.FunctionInstanceState import org.opendc.serverless.simulator.delay.DelayInjector import org.opendc.serverless.simulator.workload.SimServerlessWorkloadMapper @@ -53,8 +54,8 @@ public class SimFunctionDeployer( private val mapper: SimServerlessWorkloadMapper ) : FunctionDeployer { - override fun deploy(function: FunctionObject): Instance { - val instance = Instance(function) + override fun deploy(function: FunctionObject, listener: FunctionInstanceListener): Instance { + val instance = Instance(function, listener) instance.start() return instance } @@ -62,7 +63,7 @@ public class SimFunctionDeployer( /** * A simulated [FunctionInstance]. */ - public inner class Instance(override val function: FunctionObject) : FunctionInstance { + public inner class Instance(override val function: FunctionObject, private val listener: FunctionInstanceListener) : FunctionInstance { /** * The workload associated with this instance. */ @@ -89,6 +90,13 @@ public class SimFunctionDeployer( private val chan = Channel(Channel.RENDEZVOUS) override var state: FunctionInstanceState = FunctionInstanceState.Provisioning + set(value) { + if (field != value) { + listener.onStateChanged(this, value) + } + + field = value + } override suspend fun invoke() { check(state != FunctionInstanceState.Deleted) { "Function instance has been released" } @@ -119,7 +127,7 @@ public class SimFunctionDeployer( try { machine.run(workload) } finally { - state = FunctionInstanceState.Terminated + state = FunctionInstanceState.Deleted } } -- cgit v1.2.3