From 0562fc1c7378be67b571bb142489272a6b1b3814 Mon Sep 17 00:00:00 2001 From: Sebastien Premont-Tendland Date: Mon, 2 Mar 2020 14:33:33 -0500 Subject: Fix kotlin cache lazy loading When USE_SCRIPT_COMPILATION_CACHE is set to false the classLoader is closed after the class is instantiated. This is causing an issue when lazy loading is happening during the script execution. Lazy loading occurs when a suspendible function returning something is called from "processNB", kotlin will create a second class that will be loaded during execution (needs to be in the classpath during execution). Issue-ID: CCSDK-2150 Signed-off-by: Sebastien Premont-Tendland Change-Id: I64780287352d762325662e0e5d5b94038dedee7f --- .../core/interfaces/BluePrintScriptsService.kt | 2 ++ .../core/scripts/BluePrintCompileService.kt | 13 +------------ .../core/scripts/BluePrintScriptsServiceImpl.kt | 7 +++++++ 3 files changed, 10 insertions(+), 12 deletions(-) (limited to 'ms/blueprintsprocessor/modules/blueprints') diff --git a/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/interfaces/BluePrintScriptsService.kt b/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/interfaces/BluePrintScriptsService.kt index aa61b0c4d..0f7cb79f2 100644 --- a/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/interfaces/BluePrintScriptsService.kt +++ b/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/interfaces/BluePrintScriptsService.kt @@ -36,4 +36,6 @@ interface BluePrintScriptsService { suspend fun scriptInstance(cacheKey: String, scriptClassName: String): T suspend fun scriptInstance(scriptClassName: String): T + + suspend fun cleanupInstance(blueprintBasePath: String) } diff --git a/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/scripts/BluePrintCompileService.kt b/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/scripts/BluePrintCompileService.kt index a8c630387..a9684a14d 100644 --- a/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/scripts/BluePrintCompileService.kt +++ b/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/scripts/BluePrintCompileService.kt @@ -25,11 +25,9 @@ import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity import org.jetbrains.kotlin.cli.common.messages.MessageCollector import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler import org.jetbrains.kotlin.config.Services -import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException import org.onap.ccsdk.cds.controllerblueprints.core.checkFileExists import org.onap.ccsdk.cds.controllerblueprints.core.logger -import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintFileUtils import java.io.File import java.net.URLClassLoader import java.util.ArrayList @@ -58,13 +56,7 @@ open class BluePrintCompileService { compile(bluePrintSourceCode) } - val classLoaderWithDependencies = if (BluePrintConstants.USE_SCRIPT_COMPILE_CACHE) { - /** Get the class loader with compiled jar from cache */ - BluePrintCompileCache.classLoader(bluePrintSourceCode.cacheKey) - } else { - /** Get the class loader with compiled jar from disk */ - BluePrintFileUtils.getURLClassLoaderFromDirectory(bluePrintSourceCode.cacheKey) - } + val classLoaderWithDependencies = BluePrintCompileCache.classLoader(bluePrintSourceCode.cacheKey) /** Create the instance from the class loader */ return instance(classLoaderWithDependencies, kClassName, args) @@ -126,9 +118,6 @@ open class BluePrintCompileService { .single().newInstance(*args.toArray()) } ?: throw BluePrintException("failed to create class($kClassName) instance for constructor argument($args).") - if (!BluePrintConstants.USE_SCRIPT_COMPILE_CACHE) { - classLoader.close() - } return instance as T } } diff --git a/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/scripts/BluePrintScriptsServiceImpl.kt b/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/scripts/BluePrintScriptsServiceImpl.kt index f3eb1a2b9..fa8ca2719 100644 --- a/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/scripts/BluePrintScriptsServiceImpl.kt +++ b/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/scripts/BluePrintScriptsServiceImpl.kt @@ -79,4 +79,11 @@ open class BluePrintScriptsServiceImpl : BluePrintScriptsService { return Thread.currentThread().contextClassLoader.loadClass(scriptClassName).constructors .single().newInstance(*args.toArray()) as T } + + override suspend fun cleanupInstance(blueprintBasePath: String) { + if (!BluePrintConstants.USE_SCRIPT_COMPILE_CACHE) { + log.info("Invalidating compile cache for blueprint ($blueprintBasePath)") + BluePrintCompileCache.cleanClassLoader(blueprintBasePath) + } + } } -- cgit 1.2.3-korg