Skip to content
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

CASL-592 Extension Cleanup Capability #374

Merged
merged 43 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
4b77d3f
Initial Changes.
rlakde Aug 19, 2024
ba85d9d
Fix Unit Tests
rlakde Aug 19, 2024
1befc37
Review Fixes
rlakde Sep 18, 2024
fec7f97
Merge branch 'v2' into CASL-321_Extn_Sub_Environments
rlakde Sep 18, 2024
58995d3
Update openapi.yaml
rlakde Oct 25, 2024
7ec0f86
Update gradle.properties
rlakde Oct 25, 2024
acc5770
Update gradle.properties
rlakde Oct 25, 2024
b07480f
Rebase CASL-321 (#371)
rlakde Oct 25, 2024
3e55ff9
Update reusable-build-and-publish.yml
rlakde Oct 28, 2024
dbd6e79
Update gradle.properties
rlakde Oct 28, 2024
9ea31d5
Update openapi.yaml
rlakde Oct 28, 2024
c802b92
Merge branch 'v2' into CASL-321_Extn_Sub_Environments
rlakde Oct 28, 2024
0b204a5
Add Interface IExtensionInit
rlakde Oct 29, 2024
528c950
Add instanceCache logic.
rlakde Oct 29, 2024
7325ec8
Create ValueTuple.java
rlakde Nov 4, 2024
9443100
loaderCache Changes
rlakde Nov 4, 2024
32c1b9e
Correct logger messages
rlakde Nov 4, 2024
3bd4148
Update IExtensionInit description
rlakde Nov 4, 2024
d718694
Fix bug
rlakde Nov 4, 2024
f9314f6
Correct logger in Hub.
rlakde Nov 4, 2024
a8cfeaa
Update IExtensionInit Description.
rlakde Nov 5, 2024
f05cd53
Correct removeExtensionFromCache.
rlakde Nov 5, 2024
5fda375
Review Fixes for ExtensionCache
rlakde Nov 5, 2024
6acf521
Update ExtensionCache.java
rlakde Dec 12, 2024
ec3f3fc
Update NakshaHub.java
rlakde Dec 12, 2024
5311364
Extension Sub Environment Changes (#330) (#392)
rlakde Dec 12, 2024
a80f6a4
Update ExtensionCache.java
rlakde Dec 12, 2024
ee35c84
Update NakshaHub.java
rlakde Dec 12, 2024
e421d65
Merge branch 'v2' into v2_CASL_592-Extension_Cleanup_Capability
rlakde Dec 12, 2024
1d73a38
Update gradle.properties
rlakde Dec 20, 2024
9834cc1
Update openapi.yaml
rlakde Dec 20, 2024
807706c
Update NakshaVersion.java
rlakde Dec 20, 2024
26ae812
Update gradle.properties
rlakde Dec 20, 2024
d364a21
Update gradle.properties
rlakde Jan 3, 2025
6c461d3
Update NakshaVersion.java
rlakde Jan 3, 2025
896c98c
Update NakshaHub.java
rlakde Jan 3, 2025
44aab83
Support multiple JWT public keys (#395) (#396)
rlakde Jan 3, 2025
7a70459
Update ExtensionCache.java
rlakde Jan 6, 2025
7c5dbb0
Update gradle.properties
rlakde Jan 14, 2025
698fc56
Update openapi.yaml
rlakde Jan 14, 2025
cbc078d
Update NakshaVersion.java
rlakde Jan 14, 2025
765d423
Merge branch 'v2' into v2_CASL_592-Extension_Cleanup_Capability
rlakde Jan 14, 2025
b0943f1
Update logger as per review comment.
rlakde Jan 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ mavenPassword=YourPassword
# When updating the version, please as well consider:
# - here-naksha-lib-core/src/main/com/here/naksha/lib/core/NakshaVersion (static property: latest)
# - here-naksha-app-service/src/main/resources/swagger/openapi.yaml (info.version property)
version=2.2.1
version=2.2.2

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ servers:
info:
title: "Naksha Hub-API"
description: "Naksha Hub-API is a REST API to provide simple access to geo data."
version: "2.2.1"
version: "2.2.2"

security:
- AccessToken: [ ]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (C) 2017-2024 HERE Europe B.V.
*
* Licensed 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.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/
package com.here.naksha.lib.core;

import com.here.naksha.lib.core.models.features.Extension;

/**
* Naksha Extension Interface for all extensions providing initClassName.
*/
public interface IExtensionInit {

/**
* Initializes the extension with the specified hub and extension parameters.
* This method should be called to set up any necessary resources or configurations
* required by the extension to operate correctly.
* @param hub The hub instance to be used by the extension.
* @param extension Extension configuration supplied as part of deployment pipeline for respective Extension and sub-env.
*/
void init(INaksha hub, Extension extension);

/**
* Closes the extension. This method should be called to ensure proper
* cleanup when the extension is no longer needed.
*/
void close();
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ public class NakshaVersion implements Comparable<NakshaVersion> {
public static final String v2_1_1 = "2.1.1";
public static final String v2_2_0 = "2.2.0";
public static final String v2_2_1 = "2.2.1";
public static final String v2_2_2 = "2.2.2";

/**
* The latest version of the naksha-extension stored in the resources.
*/
@AvailableSince(v2_0_5)
public static final NakshaVersion latest = of(v2_2_1);
public static final NakshaVersion latest = of(v2_2_2);

private final int major;
private final int minor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package com.here.naksha.lib.extmanager;

import com.here.naksha.lib.core.IExtensionInit;
import com.here.naksha.lib.core.INaksha;
import com.here.naksha.lib.core.SimpleTask;
import com.here.naksha.lib.core.models.ExtensionConfig;
Expand All @@ -29,7 +30,6 @@
import com.here.naksha.lib.extmanager.models.KVPair;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -46,10 +46,10 @@
*/
public class ExtensionCache {
private static final @NotNull Logger logger = LoggerFactory.getLogger(ExtensionCache.class);
private static final ConcurrentHashMap<String, KVPair<Extension, ClassLoader>> loaderCache =
new ConcurrentHashMap<>();
private static final ConcurrentHashMap<String, ValueTuple> loaderCache = new ConcurrentHashMap<>();
private static final Map<String, FileClient> jarClientMap = new HashMap<>();
private final @NotNull INaksha naksha;
private static final String WHITE_LIST_CLASSES = "whitelistClasses";

static {
jarClientMap.put(JarClientType.S3.getType(), new AmazonS3Helper());
Expand Down Expand Up @@ -90,10 +90,8 @@ protected void buildExtensionCache(ExtensionConfig extensionConfig) {

for (String key : loaderCache.keySet()) {
if (!extIds.contains(key)) {
loaderCache.remove(key);
PluginCache.removeExtensionCache(key);
logger.info("Extension {} removed from cache.", key);
}
removeExtensionFromCache(key);
}
}
logger.info("Extension cache size " + loaderCache.size());
}
Expand All @@ -103,23 +101,36 @@ private void publishIntoCache(KVPair<Extension, File> result, ExtensionConfig ex
final Extension extension = result.getKey();
final String extensionIdWthEnv = extension.getEnv() + ":" + extension.getId();
rlakde marked this conversation as resolved.
Show resolved Hide resolved
final File jarFile = result.getValue();
IExtensionInit initObj = null;
ClassLoader loader;
try {
loader = ClassLoaderHelper.getClassLoader(jarFile, extensionConfig.getWhilelistDelegateClass());
@SuppressWarnings("unchecked")
List<String> whitelistClasses = (List<String>) extension
.getProperties()
.getOrDefault(WHITE_LIST_CLASSES, extensionConfig.getWhilelistDelegateClass());
logger.info("Whitelist classes in use for extension {} are {}", extensionIdWthEnv, whitelistClasses);
loader = ClassLoaderHelper.getClassLoader(jarFile, whitelistClasses);
rlakde marked this conversation as resolved.
Show resolved Hide resolved
} catch (Exception e) {
logger.error("Failed to load extension jar " + extension.getId(), e);
logger.error("Failed to load extension jar " + extensionIdWthEnv, e);
return;
}

if (!isNullOrEmpty(extension.getInitClassName())) {
try {
Class<?> clz = loader.loadClass(extension.getInitClassName());
clz.getConstructor(INaksha.class, Extension.class).newInstance(naksha, extension);
} catch (ClassNotFoundException
| InvocationTargetException
| InstantiationException
| NoSuchMethodException
| IllegalAccessException e) {
Object obj = clz.getConstructor().newInstance();
if (obj instanceof IExtensionInit initInstance) {
initInstance.init(naksha, extension);
initObj = initInstance;
logger.info(
"Extension {} initialization using initClassName {} done successfully.",
extensionIdWthEnv,
extension.getInitClassName());
} else {
logger.error("InitClassName {} does not implement IExtensionInit for Extension {}", extension.getInitClassName(), extensionIdWthEnv);
return;
}
} catch (Exception e) {
logger.error(
"Failed to instantiate class {} for extension {} ",
extension.getInitClassName(),
Expand All @@ -128,28 +139,48 @@ private void publishIntoCache(KVPair<Extension, File> result, ExtensionConfig ex
return;
}
}
if (!isNullOrEmpty(extension.getInitClassName()))
logger.info(
"Extension {} initialization using initClassName {} done successfully.",
extensionIdWthEnv,
extension.getInitClassName());
loaderCache.put(extensionIdWthEnv, new KVPair<Extension, ClassLoader>(extension, loader));
PluginCache.removeExtensionCache(extensionIdWthEnv);

ValueTuple previousValue = loaderCache.put(extensionIdWthEnv, new ValueTuple(extension, loader, initObj));
if (previousValue != null) {
IExtensionInit previousInitObj = previousValue.getInstance();
closeExtensionInstance(extensionIdWthEnv, previousInitObj);
}

logger.info(
"Extension id={}, version={} is successfully loaded into the cache, using Jar at {} for env={}.",
"Extension id={}, version={} is successfully loaded into the cache, using Jar at {}.",
extensionIdWthEnv,
extension.getVersion(),
extension.getUrl().substring(extension.getUrl().lastIndexOf("/") + 1),
extension.getEnv());
extension.getUrl().substring(extension.getUrl().lastIndexOf("/") + 1));
}
}

private void removeExtensionFromCache(String extensionId) {
ValueTuple valueTuple = loaderCache.remove(extensionId);
PluginCache.removeExtensionCache(extensionId);
logger.info("Extension {} removed from cache.", extensionId);
if (valueTuple != null) {
IExtensionInit initObj = valueTuple.getInstance();
closeExtensionInstance(extensionId, initObj);
}
}

private void closeExtensionInstance(String extensionId, IExtensionInit initObj) {
if (initObj != null) {
try {
initObj.close();
logger.info("Extension {} closed successfully.", extensionId);
} catch (Exception e) {
logger.error("Failed to close extension {}", extensionId, e);
}
}
}

private boolean isLoaderMappingExist(Extension extension) {
final String extensionIdWthEnv = extension.getEnv() + ":" + extension.getId();
KVPair<Extension, ClassLoader> existingMapping = loaderCache.get(extensionIdWthEnv);
ValueTuple existingMapping = loaderCache.get(extensionIdWthEnv);
if (existingMapping == null) return false;

final Extension exExtension = existingMapping.getKey();
final Extension exExtension = existingMapping.getExtension();
final String exInitClassName =
isNullOrEmpty(exExtension.getInitClassName()) ? "" : exExtension.getInitClassName();
final String initClassName = isNullOrEmpty(extension.getInitClassName()) ? "" : extension.getInitClassName();
Expand All @@ -160,7 +191,7 @@ private boolean isLoaderMappingExist(Extension extension) {
}

/**
* Lamda function which will initiate the downloading for extension jar
* Lambda function which will initiate the downloading for extension jar
*/
private KVPair<Extension, File> downloadJar(Extension extension) {
logger.info("Downloading jar {} with version {} ", extension.getId(), extension.getVersion());
Expand All @@ -184,16 +215,16 @@ protected FileClient getJarClient(String url) {
}

protected ClassLoader getClassLoaderById(@NotNull String extensionId) {
KVPair<Extension, ClassLoader> mappedLoader = loaderCache.get(extensionId);
return mappedLoader == null ? null : mappedLoader.getValue();
ValueTuple mappedLoader = loaderCache.get(extensionId);
return mappedLoader == null ? null : mappedLoader.getClassLoader();
}

public int getCacheLength() {
return loaderCache.size();
}

public List<Extension> getCachedExtensions() {
return loaderCache.values().stream().map(KVPair::getKey).toList();
return loaderCache.values().stream().map(ValueTuple::getExtension).toList();
}

private boolean isNullOrEmpty(String value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2017-2024 HERE Europe B.V.
*
* Licensed 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.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/
package com.here.naksha.lib.extmanager;

import com.here.naksha.lib.core.IExtensionInit;
import com.here.naksha.lib.core.models.features.Extension;

public class ValueTuple {
private final Extension extension;
private final ClassLoader classLoader;
private final IExtensionInit instance;

public ValueTuple(Extension extension, ClassLoader classLoader, IExtensionInit instance) {
this.extension = extension;
this.classLoader = classLoader;
this.instance = instance;
}

public Extension getExtension() {
return extension;
}

public ClassLoader getClassLoader() {
return classLoader;
}

public IExtensionInit getInstance() {
return instance;
}
}
Loading