From f3d685ebcaa8a92a88a8c48a68a4110d85b1241e Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Fri, 10 Jul 2020 18:16:41 +0200 Subject: [PATCH 01/33] Switching to gcube-jackson --- .settings/.gitignore | 2 ++ pom.xml | 17 ++++++----------- .../executor/json/ExtendedSEMapper.java | 3 +-- .../executor/rest/RestSmartExecutor.java | 3 +-- .../executor/scheduledtask/ScheduledTask.java | 3 +-- .../vremanagement/executor/ContextTest.java | 2 ++ .../executor/SerializationTest.java | 7 +++---- .../SmartExecutorPersistenceConnectorTest.java | 2 ++ 8 files changed, 18 insertions(+), 21 deletions(-) create mode 100644 .settings/.gitignore diff --git a/.settings/.gitignore b/.settings/.gitignore new file mode 100644 index 0000000..6cb3d6d --- /dev/null +++ b/.settings/.gitignore @@ -0,0 +1,2 @@ +/org.eclipse.core.resources.prefs +/org.eclipse.jdt.core.prefs diff --git a/pom.xml b/pom.xml index 68ef96a..6d6de27 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ org.gcube.vremanagement smart-executor - 2.0.0 + 3.0.0-SNAPSHOT SmartExecutor Smart Executor Service allows to launch recurrent tasks such as task for infrastructure management, healthy monitoring etc. war @@ -20,12 +20,7 @@ UTF-8 ${project.basedir}${file.separator}src${file.separator}main${file.separator}webapp${file.separator}WEB-INF VREManagement - 3.0.15 - - 2.6.0 - 2.6.0 + 3.1.0 @@ -39,7 +34,7 @@ org.gcube.distribution gcube-smartgears-bom - 1.1.0 + 2.0.0-SNAPSHOT pom import @@ -78,12 +73,12 @@ org.gcube.vremanagement smart-executor-api - [2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT) + [3.0.0-SNAPSHOT, 4.0.0-SNAPSHOT) org.gcube.vremanagement smart-executor-client - [2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT) + [3.0.0-SNAPSHOT, 4.0.0-SNAPSHOT) @@ -122,7 +117,7 @@ org.gcube.vremanagement hello-world-se-plugin - [1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT) + [2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT) test diff --git a/src/main/java/org/gcube/vremanagement/executor/json/ExtendedSEMapper.java b/src/main/java/org/gcube/vremanagement/executor/json/ExtendedSEMapper.java index 8ab8f10..8f7d20e 100644 --- a/src/main/java/org/gcube/vremanagement/executor/json/ExtendedSEMapper.java +++ b/src/main/java/org/gcube/vremanagement/executor/json/ExtendedSEMapper.java @@ -3,6 +3,7 @@ */ package org.gcube.vremanagement.executor.json; +import org.gcube.com.fasterxml.jackson.annotation.JsonTypeInfo; import org.gcube.common.authorization.library.provider.ClientInfo; import org.gcube.common.authorization.library.provider.ContainerInfo; import org.gcube.common.authorization.library.provider.ExternalServiceInfo; @@ -10,8 +11,6 @@ import org.gcube.common.authorization.library.provider.ServiceInfo; import org.gcube.common.authorization.library.provider.UserInfo; import org.gcube.vremanagement.executor.scheduledtask.ScheduledTask; -import com.fasterxml.jackson.annotation.JsonTypeInfo; - /** * @author Luca Frosini (ISTI - CNR) * diff --git a/src/main/java/org/gcube/vremanagement/executor/rest/RestSmartExecutor.java b/src/main/java/org/gcube/vremanagement/executor/rest/RestSmartExecutor.java index 2d9531a..01dd41c 100644 --- a/src/main/java/org/gcube/vremanagement/executor/rest/RestSmartExecutor.java +++ b/src/main/java/org/gcube/vremanagement/executor/rest/RestSmartExecutor.java @@ -20,6 +20,7 @@ import javax.ws.rs.core.Response.ResponseBuilder; import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.UriInfo; +import org.gcube.com.fasterxml.jackson.core.JsonProcessingException; import org.gcube.common.authorization.library.provider.CalledMethodProvider; import org.gcube.vremanagement.executor.ResourceInitializer; import org.gcube.vremanagement.executor.annotation.PURGE; @@ -44,8 +45,6 @@ import org.quartz.SchedulerException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.core.JsonProcessingException; - @Path(RestConstants.PLUGINS_PATH_PART) public class RestSmartExecutor { diff --git a/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTask.java b/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTask.java index e266eea..cd1099e 100644 --- a/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTask.java +++ b/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTask.java @@ -5,6 +5,7 @@ package org.gcube.vremanagement.executor.scheduledtask; import java.util.UUID; +import org.gcube.com.fasterxml.jackson.annotation.JsonTypeInfo; import org.gcube.common.authorization.library.provider.ClientInfo; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.resources.gcore.GCoreEndpoint; @@ -20,8 +21,6 @@ import org.gcube.vremanagement.executor.json.SEMapper; import org.gcube.vremanagement.executor.plugin.Ref; import org.gcube.vremanagement.executor.plugin.RunOn; -import com.fasterxml.jackson.annotation.JsonTypeInfo; - /** * @author Luca Frosini (ISTI - CNR) */ diff --git a/src/test/java/org/gcube/vremanagement/executor/ContextTest.java b/src/test/java/org/gcube/vremanagement/executor/ContextTest.java index ee6073e..35110fb 100644 --- a/src/test/java/org/gcube/vremanagement/executor/ContextTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/ContextTest.java @@ -31,6 +31,8 @@ public class ContextTest { protected static Properties properties; protected static final String PROPERTIES_FILENAME = "token.properties"; + public static final String ROOT = "/d4science.research-infrastructures.eu"; + public static final String DEFAULT_TEST_SCOPE_NAME; static { diff --git a/src/test/java/org/gcube/vremanagement/executor/SerializationTest.java b/src/test/java/org/gcube/vremanagement/executor/SerializationTest.java index 846a37a..1fcb202 100644 --- a/src/test/java/org/gcube/vremanagement/executor/SerializationTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/SerializationTest.java @@ -11,6 +11,9 @@ import java.util.List; import java.util.Map; import java.util.UUID; +import org.gcube.com.fasterxml.jackson.core.JsonGenerationException; +import org.gcube.com.fasterxml.jackson.databind.JsonMappingException; +import org.gcube.com.fasterxml.jackson.databind.ObjectMapper; import org.gcube.vremanagement.executor.api.types.LaunchParameter; import org.gcube.vremanagement.executor.api.types.Scheduling; import org.gcube.vremanagement.executor.exception.InvalidPluginStateEvolutionException; @@ -28,10 +31,6 @@ import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.core.JsonGenerationException; -import com.fasterxml.jackson.databind.JsonMappingException; -import com.fasterxml.jackson.databind.ObjectMapper; - /** * @author Luca Frosini (ISTI - CNR) * diff --git a/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java b/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java index 58ebe8d..3424fc4 100644 --- a/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java @@ -31,6 +31,7 @@ public class SmartExecutorPersistenceConnectorTest extends ContextTest { @Test public void getConnectionTest() throws Exception { + // ContextTest.setContextByName(ROOT); SmartExecutorPersistenceConnector persistenceConnector = SmartExecutorPersistenceFactory.getPersistenceConnector(); Assert.assertNotNull(persistenceConnector); Assert.assertEquals(OrientDBPersistenceConnector.class, persistenceConnector.getClass()); @@ -69,6 +70,7 @@ public class SmartExecutorPersistenceConnectorTest extends ContextTest { @Test public void getAvailableScheduledTasksTest() throws Exception { + ContextTest.setContextByName(ROOT); ScheduledTaskPersistence stc = ScheduledTaskPersistenceFactory.getScheduledTaskPersistence(); Assert.assertNotNull(stc); Assert.assertEquals(OrientDBPersistenceConnector.class, stc.getClass()); From 946f3315656722cbb5b083e903e6ab40c57eba66 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Tue, 4 Aug 2020 16:04:05 +0200 Subject: [PATCH 02/33] Switched changelog to the new format --- CHANGELOG.md | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ changelog.xml | 49 --------------------------------- 2 files changed, 75 insertions(+), 49 deletions(-) create mode 100644 CHANGELOG.md delete mode 100644 changelog.xml diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..161f17c --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,75 @@ +This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +# Changelog for Smart Executor Service + +## [v3.0.0-SNAPSHOT] [r5.0.0] - + +- Switched smart-executor JSON management to gcube-jackson [#19647] +- Migrated Code from OrientDB 2.2.X APIs OrientDB 3.0.X APIs [#16123] +- Redesigned HTTP APIs to comply with RESTful architectural style [#12997] +- Added API to retrieve scheduled tasks [#10780] + +## [v2.0.0] [r4.13.0] - 2018-11-20 + +- Removed SOAP APIs + + +## [v1.9.0] [r4.10.0] - 2018-02-15 + +- Added REST interface to Smart Executor [#5109] + + +## [v1.8.0] [r4.8.0] - 2017-11-29 + +- Fixed exception on stop() method [#10064] + + +## [v1.7.0] [r4.7.0] - 2017-10-09 + +- Changed JobUsageRecord use due to changes in model [#9646] +- Removed TaskUsageRecord use due to changes in model [#9647] + + +## [v1.6.0] [r4.6.0] - 2017-07-25 + +- Setting explicitly the context before running the plugin to avoid wrong context made from quartz thread reuse + + +## [v1.5.0] [r4.3.0] - 2017-03-16 + +- Provided access to UUID and iteration number for a plugin [#6733] +- Added support to implements Reminiscence for a Scheduled Task [#772] +- Removed configuration file support added in release 1.2.0 now available via Reminiscence [#772] +- Migrated from CouchDB® to OrientDB® [#6565] +- Added accounting by using TaskUsageRecord [#6116] + + +## [v1.4.0] [r4.1.0] - 2016-11-07 + +- SmartExecutor has been migrated to Authorization 2.0 [#4944] [#2112] +- Provided to plugins the possibility to specify progress percentage [#440] +- Provided to plugins the possibility to define a custom notifier [#5089] + + +## [v1.3.0] [r3.10.0] - 2016-02-08 + +- Using CouchDB® to save Scheduled Task configuration [#579] +- Added Unscheduling feature for repetitive task [#521] + + +## [v1.2.0] [r3.9.1] - 2015-12-31 + +- Removed Support for configuration File to run task at service startup + + +## [v1.1.0] [r3.9.0] - 2015-12-09 + +- Added Support for configuration File to run task at service startup [#508] +- Added Recurrent and Scheduled Task support [#111] +- Saving Task Evolution on Persistence CouchDB® [#89] + + +## [v1.0.0] - 2015-02-05 + +- First Release + diff --git a/changelog.xml b/changelog.xml deleted file mode 100644 index 763cb8b..0000000 --- a/changelog.xml +++ /dev/null @@ -1,49 +0,0 @@ - - - - - Migrated Code from OrientDB 2.2.X APIs OrientDB 3.0.X APIs #16123 - Removed SOAP APIs - Redesigned HTTP APIs to comply with RESTful architectural style #12997 - Added API to retrieve scheduled tasks #10780 - - - Added REST interface to Smart Executor #5109 - - - Fixed exception on stop() method #10064 - - - Changed TaskUsageRecord use due to changes in model #9646 #9647 - - - Setting explictly the context before running the plugin to avoid wrong context made from quartz thread reuse - - - Provided access to UUID and iteration number fro a plugin (refs #6733) - Added support to implements Reminiscence for a Scheduled Task (refs #772) - Removed config file support added in release 1.2.0 now available via Reminiscence (refs #772) - Migrated from CouchDB to OrientDB (refs #6565) - Added accountign by using TaskUsageRecord (refs #6116) - - - SmartExecutor has been passed to Authorization 2.0 (refs #4944 #2112) - Provided to plugins the possibility to specify progress percentage (refs #440) - >Provided to plugins the possibility to define a custom notifier (refs #5089) - - - Using Persistence (CouchDB) to save Scheduled Task configuration (refs #579) - Added Unscheduling feature for repetitive task (refs #521) - - - Removed Support for Config File to run task at service startup - - - Added Support for Config File to run task at service startup (refs #508) - Added Recurrent and Scheduled Task support (refs #111) - Saving Task Evolution on Persistence (CouchDB) (refs #89) - - - First Release - - \ No newline at end of file From 230043e29db3574350b793bfe2ebb4e3857810c0 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Tue, 4 Aug 2020 17:16:27 +0200 Subject: [PATCH 03/33] Fixed changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 161f17c..98861e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Redesigned HTTP APIs to comply with RESTful architectural style [#12997] - Added API to retrieve scheduled tasks [#10780] -## [v2.0.0] [r4.13.0] - 2018-11-20 +## [v2.0.0] - Removed SOAP APIs From e85f28ae7020755f53be4b75a8b4f96ce8a62c9e Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 6 Aug 2020 16:51:52 +0200 Subject: [PATCH 04/33] Removed servicearchive creation which is deprecated --- pom.xml | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/pom.xml b/pom.xml index 6d6de27..6ecd519 100644 --- a/pom.xml +++ b/pom.xml @@ -80,8 +80,6 @@ smart-executor-client [3.0.0-SNAPSHOT, 4.0.0-SNAPSHOT) - - @@ -104,8 +102,6 @@ org.slf4j slf4j-api - - junit @@ -113,14 +109,12 @@ 4.11 test - org.gcube.vremanagement hello-world-se-plugin [2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT) test - @@ -144,10 +138,10 @@ src + CHANGELOG.md LICENSE.md README.md gcube-app.xml - changelog.xml @@ -155,16 +149,6 @@ - - org.apache.maven.plugins - maven-assembly-plugin - - - make-servicearchive - package - - - - + \ No newline at end of file From 9e9694c7deffc71a5a0241aa7c49aa8a11c2ffcd Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 6 Aug 2020 17:34:08 +0200 Subject: [PATCH 05/33] gcube-app.xml has been moved to gcube/extra-resources/WEB-INF thanks to the new feature available in maven-parent. The section in the pom which did such a job has been removed because it is not needed anymore. --- gcube-app.xml | 10 ------ gcube/extra-resources/WEB-INF/gcube-app.xml | 9 ++++++ pom.xml | 35 --------------------- 3 files changed, 9 insertions(+), 45 deletions(-) delete mode 100644 gcube-app.xml create mode 100644 gcube/extra-resources/WEB-INF/gcube-app.xml diff --git a/gcube-app.xml b/gcube-app.xml deleted file mode 100644 index 28f89c7..0000000 --- a/gcube-app.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - ${serviceClass} - ${name} - ${version} - ${description} - - - diff --git a/gcube/extra-resources/WEB-INF/gcube-app.xml b/gcube/extra-resources/WEB-INF/gcube-app.xml new file mode 100644 index 0000000..19503bc --- /dev/null +++ b/gcube/extra-resources/WEB-INF/gcube-app.xml @@ -0,0 +1,9 @@ + + + + ${project.artifactId} + ${serviceClass} + ${project.version} + ${project.description} + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 6ecd519..36ef5fa 100644 --- a/pom.xml +++ b/pom.xml @@ -116,39 +116,4 @@ test - - - - org.apache.maven.plugins - maven-resources-plugin - - - copy-profile - - copy-resources - - process-resources - - ${webappDirectory} - - - ${project.basedir} - true - - src - - - CHANGELOG.md - LICENSE.md - README.md - gcube-app.xml - - - - - - - - - \ No newline at end of file From 99d9653a87566bb536418abc24ebb5c0ff585ae0 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 23 Sep 2020 15:55:06 +0200 Subject: [PATCH 06/33] Fixed orientdb version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 36ef5fa..cc5e8a4 100644 --- a/pom.xml +++ b/pom.xml @@ -20,7 +20,7 @@ UTF-8 ${project.basedir}${file.separator}src${file.separator}main${file.separator}webapp${file.separator}WEB-INF VREManagement - 3.1.0 + 3.0.34 From 68a2904371d1ea35dd31a974233796c7340155c5 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Mon, 28 Sep 2020 10:13:36 +0200 Subject: [PATCH 07/33] Removed wrong tag from gcube-app.xml --- gcube/extra-resources/WEB-INF/gcube-app.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/gcube/extra-resources/WEB-INF/gcube-app.xml b/gcube/extra-resources/WEB-INF/gcube-app.xml index 19503bc..44b06dc 100644 --- a/gcube/extra-resources/WEB-INF/gcube-app.xml +++ b/gcube/extra-resources/WEB-INF/gcube-app.xml @@ -5,5 +5,4 @@ ${serviceClass} ${project.version} ${project.description} - \ No newline at end of file From 376cd8e5acdc88bc2351f2d8b411e47a8ac9e512 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Tue, 29 Sep 2020 11:10:35 +0200 Subject: [PATCH 08/33] Switching pubblication to the new IS --- pom.xml | 24 ++ .../executor/ContextUtility.java | 82 ++++++ .../executor/GCoreISPublisher.java | 243 ++++++++++++++++++ .../vremanagement/executor/ISPublisher.java | 20 ++ .../executor/RestISPublisher.java | 88 +++++++ .../executor/pluginmanager/PluginManager.java | 17 +- 6 files changed, 458 insertions(+), 16 deletions(-) create mode 100644 src/main/java/org/gcube/vremanagement/executor/ContextUtility.java create mode 100644 src/main/java/org/gcube/vremanagement/executor/GCoreISPublisher.java create mode 100644 src/main/java/org/gcube/vremanagement/executor/ISPublisher.java create mode 100644 src/main/java/org/gcube/vremanagement/executor/RestISPublisher.java diff --git a/pom.xml b/pom.xml index cc5e8a4..4d053f4 100644 --- a/pom.xml +++ b/pom.xml @@ -70,17 +70,41 @@ ${orientdb.version} + + org.gcube.information-system + information-system-model + provided + + + org.gcube.resource-management + gcube-model + provided + + + org.gcube.information-system + resource-registry-client + provided + + + org.gcube.information-system + resource-registry-publisher + provided + + org.gcube.vremanagement smart-executor-api [3.0.0-SNAPSHOT, 4.0.0-SNAPSHOT) + provided org.gcube.vremanagement smart-executor-client [3.0.0-SNAPSHOT, 4.0.0-SNAPSHOT) + provided + javax.ws.rs diff --git a/src/main/java/org/gcube/vremanagement/executor/ContextUtility.java b/src/main/java/org/gcube/vremanagement/executor/ContextUtility.java new file mode 100644 index 0000000..73e79bb --- /dev/null +++ b/src/main/java/org/gcube/vremanagement/executor/ContextUtility.java @@ -0,0 +1,82 @@ +package org.gcube.vremanagement.executor; + +import java.util.ArrayList; +import java.util.List; + +import org.gcube.common.authorization.client.Constants; +import org.gcube.common.authorization.client.exceptions.ObjectNotFound; +import org.gcube.common.authorization.library.AuthorizationEntry; +import org.gcube.common.authorization.library.ClientType; +import org.gcube.common.authorization.library.provider.ClientInfo; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.scope.api.ScopeProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public class ContextUtility { + + private static Logger logger = LoggerFactory.getLogger(ContextUtility.class); + + public static String getCurrentScope() { + String token = SecurityTokenProvider.instance.get(); + AuthorizationEntry authorizationEntry; + try { + authorizationEntry = Constants.authorizationService().get(token); + } catch(Exception e) { + logger.trace("Context was not retrieved from token. Going to get it from {}", + ScopeProvider.class.getSimpleName()); + return ScopeProvider.instance.get(); + } + String context = authorizationEntry.getContext(); + logger.trace("Context retrieved from token is {}. Context in {} is {}", context, + ScopeProvider.class.getSimpleName(), ScopeProvider.instance.get()); + return context; + } + + public static String getCurrentScope(String token) throws ObjectNotFound, Exception { + AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token); + String context = authorizationEntry.getContext(); + logger.info("Context of token {} is {}", token, context); + return context; + } + + public static void setContext(String token) throws ObjectNotFound, Exception { + SecurityTokenProvider.instance.set(token); + ScopeProvider.instance.set(getCurrentScope(token)); + } + + public static ClientInfo getClientInfo() { + String token = SecurityTokenProvider.instance.get(); + AuthorizationEntry authorizationEntry; + try { + authorizationEntry = Constants.authorizationService().get(token); + } catch(Exception e) { + return new ClientInfo() { + + /** + * Generated Serial Version UID + */ + private static final long serialVersionUID = 8311873203596762883L; + + @Override + public ClientType getType() { + return ClientType.USER; + } + + @Override + public List getRoles() { + return new ArrayList<>(); + } + + @Override + public String getId() { + return "UNKNOWN"; + } + }; + } + return authorizationEntry.getClientInfo(); + } +} diff --git a/src/main/java/org/gcube/vremanagement/executor/GCoreISPublisher.java b/src/main/java/org/gcube/vremanagement/executor/GCoreISPublisher.java new file mode 100644 index 0000000..56bb39f --- /dev/null +++ b/src/main/java/org/gcube/vremanagement/executor/GCoreISPublisher.java @@ -0,0 +1,243 @@ +package org.gcube.vremanagement.executor; + +import java.io.StringWriter; +import java.util.List; +import java.util.Map; + +import org.gcube.common.resources.gcore.Resource; +import org.gcube.common.resources.gcore.Resources; +import org.gcube.common.resources.gcore.ServiceEndpoint; +import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; +import org.gcube.common.resources.gcore.ServiceEndpoint.Profile; +import org.gcube.common.resources.gcore.ServiceEndpoint.Property; +import org.gcube.common.resources.gcore.ServiceEndpoint.Runtime; +import org.gcube.common.resources.gcore.common.Platform; +import org.gcube.common.resources.gcore.utils.Group; +import org.gcube.informationsystem.publisher.RegistryPublisher; +import org.gcube.informationsystem.publisher.RegistryPublisherFactory; +import org.gcube.informationsystem.publisher.exception.RegistryNotFoundException; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; +import org.gcube.resources.discovery.icclient.ICFactory; +import org.gcube.smartgears.configuration.application.ApplicationConfiguration; +import org.gcube.smartgears.configuration.container.ContainerConfiguration; +import org.gcube.smartgears.context.application.ApplicationContext; +import org.gcube.smartgears.context.container.ContainerContext; +import org.gcube.vremanagement.executor.plugin.PluginDeclaration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class GCoreISPublisher extends ISPublisher { + + private static Logger logger = LoggerFactory.getLogger(GCoreISPublisher.class); + + public GCoreISPublisher(ApplicationContext applicationContext) { + super(applicationContext); + } + + /** + * Publish the provided resource on all Service Scopes retrieved from + * Context + * @param resource to be published + * @throws RegistryNotFoundException if the Registry is not found so the + * resource has not be published + */ + private void publishResource(Resource resource) throws Exception { + StringWriter stringWriter = new StringWriter(); + Resources.marshal(resource, stringWriter); + + RegistryPublisher registryPublisher = RegistryPublisherFactory.create(); + + try { + logger.debug("Trying to publish to {}:\n{}", ContextUtility.getCurrentScope(), stringWriter); + registryPublisher.create(resource); + } catch(Exception e) { + logger.error("The resource was not published", e); + throw e; + } + } + + /** + * Remove the resource from IS + * @param resource to be unpublished + * @throws RegistryNotFoundException if the Registry is not found so the + * resource has not be published + */ + private void unPublishResource(Resource resource) throws Exception { + //StringWriter stringWriter = new StringWriter(); + //Resources.marshal(resource, stringWriter); + + RegistryPublisher registryPublisher = RegistryPublisherFactory.create(); + + String id = resource.id(); + logger.debug("Trying to remove {} with ID {} from {}", resource.getClass().getSimpleName(), id, + ContextUtility.getCurrentScope()); + + registryPublisher.remove(resource); + + logger.debug("{} with ID {} removed successfully", resource.getClass().getSimpleName(), id); + } + + /** + * Return the parsed version string as array of short. + * @param version the version as String + * @param wantedLenght if the length is equals to dot (.) separated + * number in the string. Otherwise the version is padded or truncated to + * the required version + * @return the parsed version as array of short. If on slicing some of the + * version cannot be parsed as short 1 is used for the first number, 0 is + * used instead or for padding + */ + private short[] getVersionSlice(String version, int wantedLenght) { + logger.trace("Trying to parse {}", version); + + short[] versionSlices = new short[wantedLenght]; + for(int j = 0; j < wantedLenght; j++) { + versionSlices[j] = (short) (j == 0 ? 1 : 0); + } + + try { + String[] stringSlices = version.split("[.-]"); + for(int i = 0; i < stringSlices.length; i++) { + logger.trace("Parsing version slice n. {} wich is '{}'", i, stringSlices[i]); + if(i >= wantedLenght) { + break; + } + try { + short n = Short.parseShort(stringSlices[i]); + versionSlices[i] = n; + logger.trace("Version slice n. {} wich is '{}' parsed as short {}", i, stringSlices[i], n); + } catch(NumberFormatException nfe) { + logger.trace("Version slice n. {} wich is '{}' failed to parse. The default value {} will be used", + i, stringSlices[i], versionSlices[i]); + } + } + } catch(Exception e) { + logger.trace("Error parsing the supplied version the default will be used", versionSlices); + } + + logger.trace("Version {} parsed as {}", version, versionSlices); + return versionSlices; + } + + private static String getRunningOn(ContainerConfiguration containerConfiguration) { + return String.format("%s:%s", containerConfiguration.hostname(), containerConfiguration.port()); + } + + /** + * Create the Service Endpoint using information related to discovered + * available plugins and their own discovered capabilities + * @return the created {@link ServiceEndpoint} + */ + protected ServiceEndpoint createServiceEndpoint(Map availablePlugins) { + logger.debug( + "Creating ServiceEndpoint to publish on IS available plugins and their own supported capabilities"); + + ApplicationConfiguration applicationConfiguration = applicationContext.configuration(); + + ServiceEndpoint serviceEndpoint = new ServiceEndpoint(); + Profile profile = serviceEndpoint.newProfile(); + profile.category(applicationConfiguration.serviceClass()); + profile.name(applicationConfiguration.name()); + String version = applicationConfiguration.version(); + profile.version(version); + profile.description(applicationConfiguration.description()); + + String runningOn = getRunningOn(applicationContext.container().configuration()); + Platform platform = profile.newPlatform(); + platform.name(runningOn); + + short[] versionSlices = getVersionSlice(version, 4); + platform.version(versionSlices[0]); + platform.minorVersion(versionSlices[1]); + platform.buildVersion(versionSlices[2]); + platform.revisionVersion(versionSlices[3]); + + Runtime runtime = profile.newRuntime(); + runtime.hostedOn(runningOn); + runtime.status(applicationConfiguration.mode().toString()); + + Group accessPoints = profile.accessPoints(); + + for(String pluginName : availablePlugins.keySet()) { + AccessPoint accessPointElement = new AccessPoint(); + accessPointElement.name(pluginName); + + PluginDeclaration pluginDeclaration = availablePlugins.get(pluginName); + + accessPointElement.description(pluginDeclaration.getDescription()); + + Group properties = accessPointElement.properties(); + Property propertyVersionElement = new Property(); + propertyVersionElement.nameAndValue("Version", pluginDeclaration.getVersion()); + properties.add(propertyVersionElement); + + Map pluginCapabilities = pluginDeclaration.getSupportedCapabilities(); + for(String capabilityName : pluginCapabilities.keySet()) { + Property propertyElement = new Property(); + propertyElement.nameAndValue(capabilityName, pluginCapabilities.get(capabilityName)); + properties.add(propertyElement); + } + accessPoints.add(accessPointElement); + } + + StringWriter stringWriter = new StringWriter(); + Resources.marshal(serviceEndpoint, stringWriter); + logger.debug("The created ServiceEndpoint profile is\n{}", stringWriter.toString()); + + return serviceEndpoint; + } + + protected void cleanServiceEndpoints() { + try { + ApplicationConfiguration applicationConfiguration = applicationContext.configuration(); + ContainerContext containerContext = applicationContext.container(); + + SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class) + .addCondition(String.format("$resource/Profile/Category/text() eq '%s'", + applicationConfiguration.serviceClass())) + .addCondition(String.format("$resource/Profile/Name/text() eq '%s'", + applicationConfiguration.name())) + .addCondition(String.format("$resource/Profile/RunTime/HostedOn/text() eq '%s'", + getRunningOn(containerContext.configuration()))) + .setResult("$resource"); + + DiscoveryClient client = ICFactory.clientFor(ServiceEndpoint.class); + List serviceEndpoints = client.submit(query); + + for(ServiceEndpoint serviceEndpoint : serviceEndpoints) { + try { + logger.debug("Trying to unpublish the old ServiceEndpoint with ID {} from scope {}", + serviceEndpoint.id(), ContextUtility.getCurrentScope()); + unPublishResource(serviceEndpoint); + } catch(Exception e) { + logger.debug("Exception tryng to unpublish the old ServiceEndpoint with ID {} from scope {}", + serviceEndpoint.id(), ContextUtility.getCurrentScope(), e); + } + } + } catch(Exception e) { + logger.debug("An Exception occur while checking and/or unpublishing old ServiceEndpoint", e); + } + } + + @Override + public void publishPlugins(Map availablePlugins) { + ServiceEndpoint serviceEndpoint = createServiceEndpoint(availablePlugins); + + cleanServiceEndpoints(); + + try { + publishResource(serviceEndpoint); + } catch(Exception e) { + logger.error("Unable to Create ServiceEndpoint for scope {}. The Service will be aborted", + ContextUtility.getCurrentScope(), e); + throw new RuntimeException(e); + } + } + + @Override + public void unpublishPlugins() { + cleanServiceEndpoints(); + } + +} diff --git a/src/main/java/org/gcube/vremanagement/executor/ISPublisher.java b/src/main/java/org/gcube/vremanagement/executor/ISPublisher.java new file mode 100644 index 0000000..80bc28e --- /dev/null +++ b/src/main/java/org/gcube/vremanagement/executor/ISPublisher.java @@ -0,0 +1,20 @@ +package org.gcube.vremanagement.executor; + +import java.util.Map; + +import org.gcube.smartgears.context.application.ApplicationContext; +import org.gcube.vremanagement.executor.plugin.PluginDeclaration; + +public abstract class ISPublisher { + + protected ApplicationContext applicationContext; + + public ISPublisher(ApplicationContext applicationContext) { + this.applicationContext = applicationContext; + } + + public abstract void publishPlugins(Map availablePlugins) throws Exception; + + public abstract void unpublishPlugins(); + +} diff --git a/src/main/java/org/gcube/vremanagement/executor/RestISPublisher.java b/src/main/java/org/gcube/vremanagement/executor/RestISPublisher.java new file mode 100644 index 0000000..fa1a5d8 --- /dev/null +++ b/src/main/java/org/gcube/vremanagement/executor/RestISPublisher.java @@ -0,0 +1,88 @@ +package org.gcube.vremanagement.executor; + +import java.util.Map; +import java.util.UUID; + +import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl; +import org.gcube.informationsystem.model.reference.entities.Resource; +import org.gcube.informationsystem.model.reference.properties.PropagationConstraint; +import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.AddConstraint; +import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.RemoveConstraint; +import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; +import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient; +import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory; +import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisher; +import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisherFactory; +import org.gcube.resourcemanagement.model.impl.entities.facets.SimplePropertyFacetImpl; +import org.gcube.resourcemanagement.model.impl.entities.facets.SoftwareFacetImpl; +import org.gcube.resourcemanagement.model.impl.entities.resources.RunningPluginImpl; +import org.gcube.resourcemanagement.model.impl.relations.isrelatedto.UsesImpl; +import org.gcube.resourcemanagement.model.reference.entities.facets.SimplePropertyFacet; +import org.gcube.resourcemanagement.model.reference.entities.facets.SoftwareFacet; +import org.gcube.resourcemanagement.model.reference.entities.resources.EService; +import org.gcube.resourcemanagement.model.reference.entities.resources.RunningPlugin; +import org.gcube.resourcemanagement.model.reference.relations.isrelatedto.Uses; +import org.gcube.smartgears.context.application.ApplicationContext; +import org.gcube.vremanagement.executor.plugin.PluginDeclaration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RestISPublisher extends ISPublisher { + + private static Logger logger = LoggerFactory.getLogger(RestISPublisher.class); + + protected final UUID eServiceUUID; + + public RestISPublisher(ApplicationContext applicationContext) { + super(applicationContext); + this.eServiceUUID = UUID.fromString(applicationContext.id()); + } + + @Override + public void publishPlugins(Map availablePlugins) throws Exception { + ResourceRegistryClient resourceRegistryClient = ResourceRegistryClientFactory.create(); + ResourceRegistryPublisher resourceRegistryPublisher = ResourceRegistryPublisherFactory.create(); + + EService smartExecutorEService = resourceRegistryClient.getInstance(EService.class, eServiceUUID); + + for(String pluginName : availablePlugins.keySet()) { + PluginDeclaration pluginDeclaration = availablePlugins.get(pluginName); + + RunningPlugin runningPlugin = new RunningPluginImpl(); + + SoftwareFacet softwareFacet = new SoftwareFacetImpl(); + softwareFacet.setGroup("SmartExecutorPlugin"); + softwareFacet.setName(pluginName); + softwareFacet.setVersion(pluginDeclaration.getVersion()); + softwareFacet.setDescription(pluginDeclaration.getDescription()); + runningPlugin.addFacet(softwareFacet); + + Map pluginCapabilities = pluginDeclaration.getSupportedCapabilities(); + for(String capabilityName : pluginCapabilities.keySet()) { + SimplePropertyFacet simplePropertyFacet = new SimplePropertyFacetImpl(); + simplePropertyFacet.setName(capabilityName); + simplePropertyFacet.setValue(pluginCapabilities.get(capabilityName)); + runningPlugin.addFacet(simplePropertyFacet); + } + + PropagationConstraint propagationConstraint = new PropagationConstraintImpl(); + propagationConstraint.setAddConstraint(AddConstraint.propagate); + propagationConstraint.setRemoveConstraint(RemoveConstraint.cascade); + + Uses uses = new UsesImpl(smartExecutorEService, runningPlugin, propagationConstraint); + try { + resourceRegistryPublisher.createIsRelatedTo(uses); + } catch (ResourceRegistryException e) { + logger.error("Unable to publish %s instace %s for plugin %s. I'm going to stop the service.", Resource.NAME, RunningPlugin.NAME, pluginName); + throw e; + } + + } + } + + @Override + public void unpublishPlugins() { + logger.info("The Plugin will be removed when the Eservice will be removed thanks to propagation contraints. Nothing to do"); + } + +} diff --git a/src/main/java/org/gcube/vremanagement/executor/pluginmanager/PluginManager.java b/src/main/java/org/gcube/vremanagement/executor/pluginmanager/PluginManager.java index 372d78a..8bb08ad 100644 --- a/src/main/java/org/gcube/vremanagement/executor/pluginmanager/PluginManager.java +++ b/src/main/java/org/gcube/vremanagement/executor/pluginmanager/PluginManager.java @@ -7,7 +7,6 @@ import java.util.ServiceLoader; import org.gcube.vremanagement.executor.exception.InputsNullException; import org.gcube.vremanagement.executor.exception.PluginNotFoundException; -import org.gcube.vremanagement.executor.persistence.Persistence; import org.gcube.vremanagement.executor.plugin.Plugin; import org.gcube.vremanagement.executor.plugin.PluginDeclaration; import org.slf4j.Logger; @@ -19,7 +18,6 @@ import org.slf4j.LoggerFactory; * The plugin implementation class can be retrieved using its name. * @author Luca Frosini (ISTI - CNR) */ -@SuppressWarnings("deprecation") public class PluginManager { /** @@ -83,20 +81,7 @@ public class PluginManager { try { executorPluginConstructor = plugin.getDeclaredConstructor(argTypes); } catch (Exception e) { - - /* Maintaining backward compatibility */ - argTypes = new Class[2]; - argTypes[0] = pluginDeclaration.getClass(); - argTypes[1] = Persistence.class; - - try { - executorPluginConstructor = plugin.getDeclaredConstructor(argTypes); - arguments = new Object[2]; - arguments[0] = pluginDeclaration; - arguments[1] = null; - } catch (Exception e1) { - throw new PluginNotFoundException(); - } + throw new PluginNotFoundException(); } // Instancing the plugin From 8c3c2b9b413ce1f8b0992f06ecc153bdf6be52ef Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Tue, 29 Sep 2020 11:10:46 +0200 Subject: [PATCH 09/33] Ignored Eclipse project settings --- .settings/.gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.settings/.gitignore b/.settings/.gitignore index 6cb3d6d..1de83a6 100644 --- a/.settings/.gitignore +++ b/.settings/.gitignore @@ -1,2 +1,3 @@ /org.eclipse.core.resources.prefs /org.eclipse.jdt.core.prefs +/org.eclipse.m2e.core.prefs From 43328f5cfe5533447cf57c80ac733cdc3e2b33b8 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 30 Sep 2020 11:19:49 +0200 Subject: [PATCH 10/33] Switched the service to the use of new APi which does not require the PluginDeclaration class --- .../executor/SmartExecutorInitializator.java | 298 ++---------------- .../{ => ispublisher}/GCoreISPublisher.java | 23 +- .../{ => ispublisher}/ISPublisher.java | 6 +- .../{ => ispublisher}/RestISPublisher.java | 15 +- ...SmartExecutorPersistenceConfiguration.java | 4 +- .../SmartExecutorPersistenceFactory.java | 8 +- .../OrientDBPersistenceConnector.java | 19 +- .../pluginmanager/PercentageSetterImpl.java | 3 +- .../executor/pluginmanager/PluginManager.java | 104 ++---- .../pluginmanager/RunnablePlugin.java | 17 +- .../executor/rest/RestSmartExecutor.java | 32 +- .../executor/scheduledtask/ScheduledTask.java | 6 +- .../ScheduledTaskPersistence.java | 6 +- .../scheduler/SmartExecutorScheduler.java | 10 +- .../SmartExecutorSchedulerFactory.java | 9 +- .../executor/scheduler/SmartExecutorTask.java | 13 +- .../executor/SerializationTest.java | 20 +- .../executor/SmartExecutorImplTest.java | 16 +- .../SmartExecutorInizializatorTest.java | 4 +- ...SmartExecutorPersistenceConnectorTest.java | 4 +- .../pluginmanager/PluginManagerTest.java | 22 +- .../pluginmanager/RunnablePluginTest.java | 11 +- .../SmartExecutorSchedulerTest.java | 3 +- 23 files changed, 165 insertions(+), 488 deletions(-) rename src/main/java/org/gcube/vremanagement/executor/{ => ispublisher}/GCoreISPublisher.java (91%) rename src/main/java/org/gcube/vremanagement/executor/{ => ispublisher}/ISPublisher.java (61%) rename src/main/java/org/gcube/vremanagement/executor/{ => ispublisher}/RestISPublisher.java (88%) diff --git a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java index bb368d5..3dcc16e 100644 --- a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java +++ b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java @@ -1,40 +1,18 @@ package org.gcube.vremanagement.executor; -import java.io.StringWriter; -import java.util.ArrayList; import java.util.List; import java.util.Map; -import org.gcube.common.authorization.client.Constants; -import org.gcube.common.authorization.client.exceptions.ObjectNotFound; -import org.gcube.common.authorization.library.AuthorizationEntry; -import org.gcube.common.authorization.library.ClientType; -import org.gcube.common.authorization.library.provider.ClientInfo; -import org.gcube.common.authorization.library.provider.SecurityTokenProvider; -import org.gcube.common.resources.gcore.Resource; -import org.gcube.common.resources.gcore.Resources; -import org.gcube.common.resources.gcore.ServiceEndpoint; -import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; -import org.gcube.common.resources.gcore.ServiceEndpoint.Profile; -import org.gcube.common.resources.gcore.ServiceEndpoint.Property; -import org.gcube.common.resources.gcore.ServiceEndpoint.Runtime; -import org.gcube.common.resources.gcore.common.Platform; -import org.gcube.common.resources.gcore.utils.Group; -import org.gcube.common.scope.api.ScopeProvider; -import org.gcube.informationsystem.publisher.RegistryPublisher; -import org.gcube.informationsystem.publisher.RegistryPublisherFactory; -import org.gcube.informationsystem.publisher.exception.RegistryNotFoundException; -import org.gcube.resources.discovery.client.api.DiscoveryClient; -import org.gcube.resources.discovery.client.queries.api.SimpleQuery; -import org.gcube.resources.discovery.icclient.ICFactory; import org.gcube.smartgears.ApplicationManager; import org.gcube.smartgears.ContextProvider; -import org.gcube.smartgears.configuration.container.ContainerConfiguration; +import org.gcube.smartgears.context.application.ApplicationContext; import org.gcube.vremanagement.executor.api.types.LaunchParameter; +import org.gcube.vremanagement.executor.ispublisher.GCoreISPublisher; +import org.gcube.vremanagement.executor.ispublisher.RestISPublisher; import org.gcube.vremanagement.executor.json.ExtendedSEMapper; import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceConnector; import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceFactory; -import org.gcube.vremanagement.executor.plugin.PluginDeclaration; +import org.gcube.vremanagement.executor.plugin.Plugin; import org.gcube.vremanagement.executor.pluginmanager.PluginManager; import org.gcube.vremanagement.executor.scheduledtask.ScheduledTask; import org.gcube.vremanagement.executor.scheduler.SmartExecutorScheduler; @@ -55,241 +33,6 @@ public class SmartExecutorInitializator implements ApplicationManager { public static final long JOIN_TIMEOUT = 1000; - public static String getCurrentScope(){ - String token = SecurityTokenProvider.instance.get(); - AuthorizationEntry authorizationEntry; - try { - authorizationEntry = Constants.authorizationService().get(token); - } catch (Exception e) { - logger.trace("Context was not retrieved from token. Going to get it from {}", ScopeProvider.class.getSimpleName()); - return ScopeProvider.instance.get(); - } - String context = authorizationEntry.getContext(); - logger.trace("Context retrieved from token is {}. Context in {} is {}", - context, ScopeProvider.class.getSimpleName(), ScopeProvider.instance.get()); - return context; - } - - public static String getCurrentScope(String token) throws ObjectNotFound, Exception{ - AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token); - String context = authorizationEntry.getContext(); - logger.info("Context of token {} is {}", token, context); - return context; - } - - - public static void setContext(String token) throws ObjectNotFound, Exception{ - SecurityTokenProvider.instance.set(token); - ScopeProvider.instance.set(getCurrentScope(token)); - } - - public static ClientInfo getClientInfo() { - String token = SecurityTokenProvider.instance.get(); - AuthorizationEntry authorizationEntry; - try { - authorizationEntry = Constants.authorizationService().get(token); - } catch (Exception e) { - return new ClientInfo() { - - /** - * Generated Serial Version UID - */ - private static final long serialVersionUID = 8311873203596762883L; - - @Override - public ClientType getType() { - return ClientType.USER; - } - - @Override - public List getRoles() { - return new ArrayList<>(); - } - - @Override - public String getId() { - return "UNKNOWN"; - } - }; - } - return authorizationEntry.getClientInfo(); - } - - - /** - * Publish the provided resource on all Service Scopes retrieved from - * Context - * @param resource to be published - * @throws RegistryNotFoundException if the Registry is not found so the - * resource has not be published - */ - private static void publishResource(Resource resource) throws Exception { - StringWriter stringWriter = new StringWriter(); - Resources.marshal(resource, stringWriter); - - RegistryPublisher registryPublisher = RegistryPublisherFactory.create(); - - try { - logger.debug("Trying to publish to {}:\n{}", getCurrentScope(), stringWriter); - registryPublisher.create(resource); - } catch (Exception e) { - logger.error("The resource was not published", e); - throw e; - } - } - - /** - * Remove the resource from IS - * @param resource to be unpublished - * @throws RegistryNotFoundException if the Registry is not found so the - * resource has not be published - */ - private static void unPublishResource(Resource resource) throws Exception { - //StringWriter stringWriter = new StringWriter(); - //Resources.marshal(resource, stringWriter); - - RegistryPublisher registryPublisher = RegistryPublisherFactory.create(); - - String id = resource.id(); - logger.debug("Trying to remove {} with ID {} from {}", resource.getClass().getSimpleName(), id, getCurrentScope()); - - registryPublisher.remove(resource); - - logger.debug("{} with ID {} removed successfully", resource.getClass().getSimpleName(), id); - } - - /** - * Return the parsed version string as array of short. - * @param version the version as String - * @param wantedLenght if the length is equals to dot (.) separated - * number in the string. Otherwise the version is padded or truncated to - * the required version - * @return the parsed version as array of short. If on slicing some of the - * version cannot be parsed as short 1 is used for the first number, 0 is - * used instead or for padding - */ - private static short[] getVersionSlice(String version, int wantedLenght){ - logger.trace("Trying to parse {}", version); - - short[] versionSlices = new short[wantedLenght]; - for(int j=0; j=wantedLenght){ - break; - } - try { - short n = Short.parseShort(stringSlices[i]); - versionSlices[i] = n; - logger.trace("Version slice n. {} wich is '{}' parsed as short {}", i, stringSlices[i], n); - } catch(NumberFormatException nfe){ - logger.trace("Version slice n. {} wich is '{}' failed to parse. The default value {} will be used", i, stringSlices[i], versionSlices[i]); - } - } - } catch(Exception e){ - logger.trace("Error parsing the supplied version the default will be used", versionSlices); - } - - logger.trace("Version {} parsed as {}", version, versionSlices); - return versionSlices; - } - - private static String getRunningOn(ContainerConfiguration containerConfiguration){ - return String.format("%s:%s", containerConfiguration.hostname(), containerConfiguration.port()); - } - - /** - * Create the Service Endpoint using information related to discovered - * available plugins and their own discovered capabilities - * @return the created {@link ServiceEndpoint} - */ - protected static ServiceEndpoint createServiceEndpoint(Map availablePlugins){ - logger.debug("Creating ServiceEndpoint to publish on IS available plugins and their own supported capabilities"); - ServiceEndpoint serviceEndpoint = new ServiceEndpoint(); - Profile profile = serviceEndpoint.newProfile(); - profile.category(ContextProvider.get().configuration().serviceClass()); - profile.name(ContextProvider.get().configuration().name()); - String version = ContextProvider.get().configuration().version(); - profile.version(version); - profile.description(ContextProvider.get().configuration().description()); - - String runningOn = getRunningOn(ContextProvider.get().container().configuration()); - Platform platform = profile.newPlatform(); - platform.name(runningOn); - - short[] versionSlices = getVersionSlice(version, 4); - platform.version(versionSlices[0]); - platform.minorVersion(versionSlices[1]); - platform.buildVersion(versionSlices[2]); - platform.revisionVersion(versionSlices[3]); - - Runtime runtime = profile.newRuntime(); - runtime.hostedOn(runningOn); - runtime.status(ContextProvider.get().configuration().mode().toString()); - - Group accessPoints = profile.accessPoints(); - - for(String pluginName : availablePlugins.keySet()){ - AccessPoint accessPointElement = new AccessPoint(); - accessPointElement.name(pluginName); - - PluginDeclaration pluginDeclaration = availablePlugins.get(pluginName); - - accessPointElement.description(pluginDeclaration.getDescription()); - - Group properties = accessPointElement.properties(); - Property propertyVersionElement = new Property(); - propertyVersionElement.nameAndValue("Version", pluginDeclaration.getVersion()); - properties.add(propertyVersionElement); - - - Map pluginCapabilities = pluginDeclaration.getSupportedCapabilities(); - for(String capabilityName : pluginCapabilities.keySet()){ - Property propertyElement = new Property(); - propertyElement.nameAndValue(capabilityName, pluginCapabilities.get(capabilityName)); - properties.add(propertyElement); - } - accessPoints.add(accessPointElement); - } - - StringWriter stringWriter = new StringWriter(); - Resources.marshal(serviceEndpoint, stringWriter); - logger.debug("The created ServiceEndpoint profile is\n{}", stringWriter.toString()); - - return serviceEndpoint; - } - - private void cleanServiceEndpoints(){ - try { - SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class) - .addCondition(String.format("$resource/Profile/Category/text() eq '%s'", ContextProvider.get().configuration().serviceClass())) - .addCondition(String.format("$resource/Profile/Name/text() eq '%s'", ContextProvider.get().configuration().name())) - .addCondition(String.format("$resource/Profile/RunTime/HostedOn/text() eq '%s'", getRunningOn(ContextProvider.get().container().configuration()))) - .setResult("$resource"); - - DiscoveryClient client = ICFactory.clientFor(ServiceEndpoint.class); - List serviceEndpoints = client.submit(query); - - for (ServiceEndpoint serviceEndpoint : serviceEndpoints) { - try { - logger.debug("Trying to unpublish the old ServiceEndpoint with ID {} from scope {}", - serviceEndpoint.id(), getCurrentScope()); - unPublishResource(serviceEndpoint); - } catch(Exception e){ - logger.debug("Exception tryng to unpublish the old ServiceEndpoint with ID {} from scope {}", - serviceEndpoint.id(), getCurrentScope(), e); - } - } - }catch(Exception e){ - logger.debug("An Exception occur while checking and/or unpublishing old ServiceEndpoint", e); - } - } - /** * {@inheritDoc} * The method discover the plugins available on classpath and their own @@ -299,7 +42,7 @@ public class SmartExecutorInitializator implements ApplicationManager { */ @Override public void onInit() { - String scope = getCurrentScope(); + String scope = ContextUtility.getCurrentScope(); logger.trace( "\n-------------------------------------------------------\n" @@ -308,14 +51,16 @@ public class SmartExecutorInitializator implements ApplicationManager { scope); logger.debug("Getting Available Plugins and their own supported capabilities"); - PluginManager pluginManager = PluginManager.getInstance(); - Map availablePlugins = pluginManager.getAvailablePlugins(); - ServiceEndpoint serviceEndpoint = createServiceEndpoint(availablePlugins); - cleanServiceEndpoints(); + Map> availablePlugins = PluginManager.getAvailablePlugins(); + ApplicationContext applicationContext = ContextProvider.get(); try { - publishResource(serviceEndpoint); + GCoreISPublisher gCoreISPublisher = new GCoreISPublisher(applicationContext); + gCoreISPublisher.publishPlugins(availablePlugins); + + RestISPublisher restISPublisher = new RestISPublisher(applicationContext); + restISPublisher.publishPlugins(availablePlugins); } catch (Exception e) { logger.error("Unable to Create ServiceEndpoint for scope {}. The Service will be aborted", scope, e); throw new RuntimeException(e); @@ -335,8 +80,8 @@ public class SmartExecutorInitializator implements ApplicationManager { try { logger.debug("Going to get Orphan Scheduled Tasks in scope {}", scope); - - List scheduledTasks = smartExecutorPersistenceConnector.getOrphanScheduledTasks(availablePlugins.values()); + + List scheduledTasks = smartExecutorPersistenceConnector.getOrphanScheduledTasks(PluginManager.getAvailablePlugins().keySet()); if(scheduledTasks.size()==0){ logger.debug("No Orphan Scheduled Tasks this instance can take in charge in scope {}", scope); } @@ -368,7 +113,7 @@ public class SmartExecutorInitializator implements ApplicationManager { String scheduledTasktoken = scheduledTask.getToken(); try { - setContext(scheduledTasktoken); + ContextUtility.setContext(scheduledTasktoken); SmartExecutorScheduler smartExecutorScheduler = SmartExecutorSchedulerFactory.getSmartExecutorScheduler(); // A new Scheduled Task will be persisted due to launch. Removing it @@ -417,7 +162,7 @@ public class SmartExecutorInitializator implements ApplicationManager { "\n-------------------------------------------------------\n" + "Smart Executor is Stopping on scope {}\n" + "-------------------------------------------------------", - getCurrentScope()); + ContextUtility.getCurrentScope()); SmartExecutorScheduler scheduler; @@ -429,20 +174,25 @@ public class SmartExecutorInitializator implements ApplicationManager { logger.error("", e); } - cleanServiceEndpoints(); + ApplicationContext applicationContext = ContextProvider.get(); + GCoreISPublisher gCoreISPublisher = new GCoreISPublisher(applicationContext); + gCoreISPublisher.unpublishPlugins(); + + RestISPublisher restISPublisher = new RestISPublisher(applicationContext); + restISPublisher.unpublishPlugins(); try { SmartExecutorPersistenceFactory.closePersistenceConnector(); } catch (Throwable e) { logger.error("Unable to correctly close {} for scope {}", SmartExecutorPersistenceConnector.class.getSimpleName(), - getCurrentScope(), e); + ContextUtility.getCurrentScope(), e); } logger.trace( "\n-------------------------------------------------------\n" + "Smart Executor Stopped Successfully on scope {}\n" + "-------------------------------------------------------", - getCurrentScope()); + ContextUtility.getCurrentScope()); } } diff --git a/src/main/java/org/gcube/vremanagement/executor/GCoreISPublisher.java b/src/main/java/org/gcube/vremanagement/executor/ispublisher/GCoreISPublisher.java similarity index 91% rename from src/main/java/org/gcube/vremanagement/executor/GCoreISPublisher.java rename to src/main/java/org/gcube/vremanagement/executor/ispublisher/GCoreISPublisher.java index 56bb39f..df5e9c0 100644 --- a/src/main/java/org/gcube/vremanagement/executor/GCoreISPublisher.java +++ b/src/main/java/org/gcube/vremanagement/executor/ispublisher/GCoreISPublisher.java @@ -1,4 +1,4 @@ -package org.gcube.vremanagement.executor; +package org.gcube.vremanagement.executor.ispublisher; import java.io.StringWriter; import java.util.List; @@ -23,7 +23,11 @@ import org.gcube.smartgears.configuration.application.ApplicationConfiguration; import org.gcube.smartgears.configuration.container.ContainerConfiguration; import org.gcube.smartgears.context.application.ApplicationContext; import org.gcube.smartgears.context.container.ContainerContext; -import org.gcube.vremanagement.executor.plugin.PluginDeclaration; +import org.gcube.vremanagement.executor.ContextUtility; +import org.gcube.vremanagement.executor.exception.ExecutorException; +import org.gcube.vremanagement.executor.exception.PluginNotFoundException; +import org.gcube.vremanagement.executor.plugin.Plugin; +import org.gcube.vremanagement.executor.pluginmanager.PluginManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -128,8 +132,10 @@ public class GCoreISPublisher extends ISPublisher { * Create the Service Endpoint using information related to discovered * available plugins and their own discovered capabilities * @return the created {@link ServiceEndpoint} + * @throws ExecutorException + * @throws PluginNotFoundException */ - protected ServiceEndpoint createServiceEndpoint(Map availablePlugins) { + protected ServiceEndpoint createServiceEndpoint(Map> availablePlugins) throws PluginNotFoundException, ExecutorException { logger.debug( "Creating ServiceEndpoint to publish on IS available plugins and their own supported capabilities"); @@ -159,11 +165,12 @@ public class GCoreISPublisher extends ISPublisher { Group accessPoints = profile.accessPoints(); + for(String pluginName : availablePlugins.keySet()) { AccessPoint accessPointElement = new AccessPoint(); accessPointElement.name(pluginName); - PluginDeclaration pluginDeclaration = availablePlugins.get(pluginName); + Plugin pluginDeclaration = PluginManager.getPlugin(pluginName); accessPointElement.description(pluginDeclaration.getDescription()); @@ -221,12 +228,10 @@ public class GCoreISPublisher extends ISPublisher { } @Override - public void publishPlugins(Map availablePlugins) { - ServiceEndpoint serviceEndpoint = createServiceEndpoint(availablePlugins); - - cleanServiceEndpoints(); - + public void publishPlugins(Map> availablePlugins) { try { + ServiceEndpoint serviceEndpoint = createServiceEndpoint(availablePlugins); + cleanServiceEndpoints(); publishResource(serviceEndpoint); } catch(Exception e) { logger.error("Unable to Create ServiceEndpoint for scope {}. The Service will be aborted", diff --git a/src/main/java/org/gcube/vremanagement/executor/ISPublisher.java b/src/main/java/org/gcube/vremanagement/executor/ispublisher/ISPublisher.java similarity index 61% rename from src/main/java/org/gcube/vremanagement/executor/ISPublisher.java rename to src/main/java/org/gcube/vremanagement/executor/ispublisher/ISPublisher.java index 80bc28e..c51e389 100644 --- a/src/main/java/org/gcube/vremanagement/executor/ISPublisher.java +++ b/src/main/java/org/gcube/vremanagement/executor/ispublisher/ISPublisher.java @@ -1,9 +1,9 @@ -package org.gcube.vremanagement.executor; +package org.gcube.vremanagement.executor.ispublisher; import java.util.Map; import org.gcube.smartgears.context.application.ApplicationContext; -import org.gcube.vremanagement.executor.plugin.PluginDeclaration; +import org.gcube.vremanagement.executor.plugin.Plugin; public abstract class ISPublisher { @@ -13,7 +13,7 @@ public abstract class ISPublisher { this.applicationContext = applicationContext; } - public abstract void publishPlugins(Map availablePlugins) throws Exception; + public abstract void publishPlugins(Map> availablePlugins) throws Exception; public abstract void unpublishPlugins(); diff --git a/src/main/java/org/gcube/vremanagement/executor/RestISPublisher.java b/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java similarity index 88% rename from src/main/java/org/gcube/vremanagement/executor/RestISPublisher.java rename to src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java index fa1a5d8..e2464f1 100644 --- a/src/main/java/org/gcube/vremanagement/executor/RestISPublisher.java +++ b/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java @@ -1,4 +1,4 @@ -package org.gcube.vremanagement.executor; +package org.gcube.vremanagement.executor.ispublisher; import java.util.Map; import java.util.UUID; @@ -23,7 +23,8 @@ import org.gcube.resourcemanagement.model.reference.entities.resources.EService; import org.gcube.resourcemanagement.model.reference.entities.resources.RunningPlugin; import org.gcube.resourcemanagement.model.reference.relations.isrelatedto.Uses; import org.gcube.smartgears.context.application.ApplicationContext; -import org.gcube.vremanagement.executor.plugin.PluginDeclaration; +import org.gcube.vremanagement.executor.plugin.Plugin; +import org.gcube.vremanagement.executor.pluginmanager.PluginManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,25 +40,25 @@ public class RestISPublisher extends ISPublisher { } @Override - public void publishPlugins(Map availablePlugins) throws Exception { + public void publishPlugins(Map> availablePlugins) throws Exception { ResourceRegistryClient resourceRegistryClient = ResourceRegistryClientFactory.create(); ResourceRegistryPublisher resourceRegistryPublisher = ResourceRegistryPublisherFactory.create(); EService smartExecutorEService = resourceRegistryClient.getInstance(EService.class, eServiceUUID); for(String pluginName : availablePlugins.keySet()) { - PluginDeclaration pluginDeclaration = availablePlugins.get(pluginName); + Plugin plugin = PluginManager.getPlugin(pluginName); RunningPlugin runningPlugin = new RunningPluginImpl(); SoftwareFacet softwareFacet = new SoftwareFacetImpl(); softwareFacet.setGroup("SmartExecutorPlugin"); softwareFacet.setName(pluginName); - softwareFacet.setVersion(pluginDeclaration.getVersion()); - softwareFacet.setDescription(pluginDeclaration.getDescription()); + softwareFacet.setVersion(plugin.getVersion()); + softwareFacet.setDescription(plugin.getDescription()); runningPlugin.addFacet(softwareFacet); - Map pluginCapabilities = pluginDeclaration.getSupportedCapabilities(); + Map pluginCapabilities = plugin.getSupportedCapabilities(); for(String capabilityName : pluginCapabilities.keySet()) { SimplePropertyFacet simplePropertyFacet = new SimplePropertyFacetImpl(); simplePropertyFacet.setName(capabilityName); diff --git a/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConfiguration.java b/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConfiguration.java index ccc0ec1..7da12bd 100644 --- a/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConfiguration.java +++ b/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConfiguration.java @@ -16,7 +16,7 @@ import org.gcube.common.resources.gcore.utils.Group; import org.gcube.resources.discovery.client.api.DiscoveryClient; import org.gcube.resources.discovery.client.queries.api.SimpleQuery; import org.gcube.resources.discovery.icclient.ICFactory; -import org.gcube.vremanagement.executor.SmartExecutorInitializator; +import org.gcube.vremanagement.executor.ContextUtility; /** * @author Luca Frosini (ISTI - CNR) @@ -131,7 +131,7 @@ public class SmartExecutorPersistenceConfiguration { List serviceEndpoints = client.submit(query); if(serviceEndpoints.size()>1){ query.addCondition(String.format("$resource/Profile/AccessPoint/Properties/Property/Name/text() eq '%s'", TARGET_SCOPE)); - query.addCondition(String.format("$resource/Profile/AccessPoint/Properties/Property/Value/text() eq '%s'", SmartExecutorInitializator.getCurrentScope())); + query.addCondition(String.format("$resource/Profile/AccessPoint/Properties/Property/Value/text() eq '%s'", ContextUtility.getCurrentScope())); serviceEndpoints = client.submit(query); } return serviceEndpoints.get(0); diff --git a/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceFactory.java b/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceFactory.java index 433eaf0..b95f03e 100644 --- a/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceFactory.java +++ b/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceFactory.java @@ -6,7 +6,7 @@ package org.gcube.vremanagement.executor.persistence; import java.util.HashMap; import java.util.Map; -import org.gcube.vremanagement.executor.SmartExecutorInitializator; +import org.gcube.vremanagement.executor.ContextUtility; import org.gcube.vremanagement.executor.persistence.orientdb.OrientDBPersistenceConnector; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -47,7 +47,7 @@ public abstract class SmartExecutorPersistenceFactory { new SmartExecutorPersistenceConfiguration(className); persistence = new OrientDBPersistenceConnector(configuration); - persistenceConnectors.put(SmartExecutorInitializator.getCurrentScope(), + persistenceConnectors.put(ContextUtility.getCurrentScope(), persistence); } @@ -58,12 +58,12 @@ public abstract class SmartExecutorPersistenceFactory { * @return the persistenceConnector */ public static synchronized SmartExecutorPersistenceConnector getPersistenceConnector() throws Exception { - String scope = SmartExecutorInitializator.getCurrentScope(); + String scope = ContextUtility.getCurrentScope(); return getPersistenceConnector(scope); } public static synchronized void closePersistenceConnector() throws Exception { - String scope = SmartExecutorInitializator.getCurrentScope(); + String scope = ContextUtility.getCurrentScope(); SmartExecutorPersistenceConnector persistence = getPersistenceConnector(scope); if(persistence!=null){ diff --git a/src/main/java/org/gcube/vremanagement/executor/persistence/orientdb/OrientDBPersistenceConnector.java b/src/main/java/org/gcube/vremanagement/executor/persistence/orientdb/OrientDBPersistenceConnector.java index bec3da8..8f76ec0 100644 --- a/src/main/java/org/gcube/vremanagement/executor/persistence/orientdb/OrientDBPersistenceConnector.java +++ b/src/main/java/org/gcube/vremanagement/executor/persistence/orientdb/OrientDBPersistenceConnector.java @@ -11,7 +11,7 @@ import java.util.List; import java.util.Map; import java.util.UUID; -import org.gcube.vremanagement.executor.SmartExecutorInitializator; +import org.gcube.vremanagement.executor.ContextUtility; import org.gcube.vremanagement.executor.api.types.LaunchParameter; import org.gcube.vremanagement.executor.exception.ExecutorException; import org.gcube.vremanagement.executor.exception.PluginInstanceNotFoundException; @@ -19,7 +19,6 @@ import org.gcube.vremanagement.executor.exception.SchedulePersistenceException; import org.gcube.vremanagement.executor.json.ExtendedSEMapper; import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceConfiguration; import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceConnector; -import org.gcube.vremanagement.executor.plugin.PluginDeclaration; import org.gcube.vremanagement.executor.plugin.PluginStateEvolution; import org.gcube.vremanagement.executor.scheduledtask.ScheduledTask; import org.slf4j.Logger; @@ -75,7 +74,7 @@ public class OrientDBPersistenceConnector extends SmartExecutorPersistenceConnec String type = PluginStateEvolution.class.getSimpleName(); Map params = new HashMap(); params.put(UUID, uuid.toString()); - params.put(SCOPE, SmartExecutorInitializator.getCurrentScope()); + params.put(SCOPE, ContextUtility.getCurrentScope()); OSQLSynchQuery query = null; if(iterationNumber != null && iterationNumber > 0) { @@ -138,7 +137,7 @@ public class OrientDBPersistenceConnector extends SmartExecutorPersistenceConnec ODocument doc = new ODocument(PluginStateEvolution.class.getSimpleName()); String json = ExtendedSEMapper.getInstance().marshal(pluginStateEvolution); doc.fromJSON(json); - doc.field(SCOPE, SmartExecutorInitializator.getCurrentScope()); + doc.field(SCOPE, ContextUtility.getCurrentScope()); doc.save(); oDatabaseSession.commit(); @@ -184,7 +183,7 @@ public class OrientDBPersistenceConnector extends SmartExecutorPersistenceConnec } @Override - public List getOrphanScheduledTasks(Collection pluginDeclarations) + public List getOrphanScheduledTasks(Collection plugins) throws SchedulePersistenceException { ODatabaseSession oDatabaseSession = null; try { @@ -192,19 +191,19 @@ public class OrientDBPersistenceConnector extends SmartExecutorPersistenceConnec String type = ScheduledTask.class.getSimpleName(); String queryString = String.format("SELECT * FROM %s WHERE %s = '%s'", type, "scope", - SmartExecutorInitializator.getCurrentScope()); - if(pluginDeclarations != null && pluginDeclarations.size() != 0) { + ContextUtility.getCurrentScope()); + if(plugins != null && plugins.size() != 0) { boolean first = true; - for(PluginDeclaration pluginDeclaration : pluginDeclarations) { + for(String pluginName : plugins) { if(first) { first = false; queryString = String.format("%s AND ( (%s = '%s') ", queryString, ScheduledTask.LAUNCH_PARAMETER + "." + LaunchParameter.PLUGIN_NAME, - pluginDeclaration.getName()); + pluginName); } else { queryString = String.format("%s OR (%s = '%s') ", queryString, ScheduledTask.LAUNCH_PARAMETER + "." + LaunchParameter.PLUGIN_NAME, - pluginDeclaration.getName()); + pluginName); } } queryString = queryString + ")"; diff --git a/src/main/java/org/gcube/vremanagement/executor/pluginmanager/PercentageSetterImpl.java b/src/main/java/org/gcube/vremanagement/executor/pluginmanager/PercentageSetterImpl.java index ecb8789..4652c67 100644 --- a/src/main/java/org/gcube/vremanagement/executor/pluginmanager/PercentageSetterImpl.java +++ b/src/main/java/org/gcube/vremanagement/executor/pluginmanager/PercentageSetterImpl.java @@ -6,13 +6,12 @@ package org.gcube.vremanagement.executor.pluginmanager; import org.gcube.vremanagement.executor.exception.InvalidPluginStateEvolutionException; import org.gcube.vremanagement.executor.plugin.PercentageSetter; import org.gcube.vremanagement.executor.plugin.Plugin; -import org.gcube.vremanagement.executor.plugin.PluginDeclaration; /** * @author Luca Frosini (ISTI - CNR) * */ -public class PercentageSetterImpl> implements PercentageSetter { +public class PercentageSetterImpl implements PercentageSetter { private final RunnablePlugin runnablePlugin; diff --git a/src/main/java/org/gcube/vremanagement/executor/pluginmanager/PluginManager.java b/src/main/java/org/gcube/vremanagement/executor/pluginmanager/PluginManager.java index 8bb08ad..91a7839 100644 --- a/src/main/java/org/gcube/vremanagement/executor/pluginmanager/PluginManager.java +++ b/src/main/java/org/gcube/vremanagement/executor/pluginmanager/PluginManager.java @@ -1,14 +1,12 @@ package org.gcube.vremanagement.executor.pluginmanager; -import java.lang.reflect.Constructor; import java.util.HashMap; import java.util.Map; import java.util.ServiceLoader; -import org.gcube.vremanagement.executor.exception.InputsNullException; +import org.gcube.vremanagement.executor.exception.ExecutorException; import org.gcube.vremanagement.executor.exception.PluginNotFoundException; import org.gcube.vremanagement.executor.plugin.Plugin; -import org.gcube.vremanagement.executor.plugin.PluginDeclaration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,69 +32,7 @@ public class PluginManager { * Contains mapping between plugin name and the instance of its declaration * class */ - private Map availablePlugins; - - /** - * Retrieve the PluginDeclaration class representing the plugin which - * have the name provided as input - * @param pluginName the name of the plugin - * @return the PluginDeclaration - * @throws PluginNotFoundException if the plugin is not available - */ - public static PluginDeclaration getPluginDeclaration(String pluginName) throws PluginNotFoundException { - logger.debug(String.format("Trying to instantiate a Plugin named %s", - pluginName)); - PluginDeclaration pluginDeclaration = PluginManager.getInstance() - .getPlugin(pluginName); - if (pluginDeclaration == null) { - throw new PluginNotFoundException(); - } - return pluginDeclaration; - } - - public static Plugin instantiatePlugin( - String pluginName) throws InputsNullException, - PluginNotFoundException { - - PluginDeclaration pluginDeclaration = getPluginDeclaration(pluginName); - - // Retrieving the plugin instance class to be run from PluginDeclaration - Class> plugin = pluginDeclaration - .getPluginImplementation(); - logger.debug(String.format( - "The class which will run the execution will be %s", - plugin.getName())); - - // Retrieve the Constructor of Plugin to instantiate it - @SuppressWarnings("rawtypes") - Class[] argTypes = { pluginDeclaration.getClass()}; - - // Creating the Argument to pass to constructor - Object[] arguments = { pluginDeclaration}; - - - // logger.debug(String.format("Plugin named %s once instantiated will be identified by the UUID %s", - // name, executionIdentifier)); - Constructor> executorPluginConstructor; - try { - executorPluginConstructor = plugin.getDeclaredConstructor(argTypes); - } catch (Exception e) { - throw new PluginNotFoundException(); - } - - // Instancing the plugin - Plugin instantiatedPlugin; - try { - instantiatedPlugin = executorPluginConstructor - .newInstance(arguments); - } catch (Exception e) { - throw new PluginNotFoundException(); - } - logger.debug(String - .format("Plugin named %s has been instantiated", pluginName)); - - return instantiatedPlugin; - } + private Map> availablePlugins; /** * Get the singleton instance of {@link #PluginManager}. @@ -104,7 +40,7 @@ public class PluginManager { * so it is created. Otherwise the already created instance is returned * @return singleton instance of {@link #PluginManager} */ - public static PluginManager getInstance(){ + private static PluginManager getInstance(){ if(pluginManager== null){ pluginManager = new PluginManager(); } @@ -117,33 +53,41 @@ public class PluginManager { */ protected PluginManager(){ logger.debug("Loading plugins available on classpath"); - this.availablePlugins = new HashMap(); - ServiceLoader serviceLoader = ServiceLoader.load(PluginDeclaration.class); - for (PluginDeclaration pluginDeclaration : serviceLoader) { + this.availablePlugins = new HashMap>(); + ServiceLoader serviceLoader = ServiceLoader.load(Plugin.class); + for (Plugin plugin : serviceLoader) { try { - logger.debug(String.format("%s plugin found", pluginDeclaration.getName())); - pluginDeclaration.init(); - String name = pluginDeclaration.getName(); - this.availablePlugins.put(name, pluginDeclaration); + logger.debug(String.format("%s plugin found", plugin.getName())); + String name = plugin.getName(); + this.availablePlugins.put(name, plugin.getClass()); } catch (Exception e) { - logger.debug(String.format("%s not initialized correctly. It will not be used", pluginDeclaration.getName())); + logger.debug(String.format("%s not initialized correctly. It will not be used", plugin.getName())); } } } /** * - * @param name The name of the plugin + * @param pluginName The name of the plugin * @return The plugin declaration if available, null otherwise + * @throws PluginNotFoundException */ - public PluginDeclaration getPlugin(String name){ - return this.availablePlugins.get(name); + public static Plugin getPlugin(String pluginName) throws PluginNotFoundException, ExecutorException { + Class pluginClass = getAvailablePlugins().get(pluginName); + if (pluginClass== null) { + throw new PluginNotFoundException("Plugin " + pluginName + " not available in this smart-executor instance"); + } + try { + return pluginClass.getDeclaredConstructor().newInstance(); + }catch (Exception e) { + throw new ExecutorException("Unable to instatiate plugin " + pluginName, e); + } } /** * @return the availablePlugins */ - public Map getAvailablePlugins() { - return availablePlugins; + public static Map> getAvailablePlugins() { + return PluginManager.getInstance().availablePlugins; } } diff --git a/src/main/java/org/gcube/vremanagement/executor/pluginmanager/RunnablePlugin.java b/src/main/java/org/gcube/vremanagement/executor/pluginmanager/RunnablePlugin.java index 29b9438..c34b142 100644 --- a/src/main/java/org/gcube/vremanagement/executor/pluginmanager/RunnablePlugin.java +++ b/src/main/java/org/gcube/vremanagement/executor/pluginmanager/RunnablePlugin.java @@ -18,11 +18,10 @@ import org.gcube.common.authorization.library.provider.ClientInfo; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.documentstore.exception.InvalidValueException; import org.gcube.smartgears.ContextProvider; -import org.gcube.vremanagement.executor.SmartExecutorInitializator; +import org.gcube.vremanagement.executor.ContextUtility; import org.gcube.vremanagement.executor.exception.AlreadyInFinalStateException; import org.gcube.vremanagement.executor.exception.InvalidPluginStateEvolutionException; import org.gcube.vremanagement.executor.plugin.Plugin; -import org.gcube.vremanagement.executor.plugin.PluginDeclaration; import org.gcube.vremanagement.executor.plugin.PluginState; import org.gcube.vremanagement.executor.plugin.PluginStateEvolution; import org.gcube.vremanagement.executor.plugin.PluginStateNotification; @@ -36,7 +35,7 @@ import org.slf4j.LoggerFactory; * @author Luca Frosini (ISTI - CNR) * */ -public class RunnablePlugin> implements Runnable { +public class RunnablePlugin implements Runnable { /** * Logger @@ -81,10 +80,10 @@ public class RunnablePlugin> imple @Override public void run(){ - String pluginName = plugin.getPluginDeclaration().getName(); + String pluginName = plugin.getName(); logger.info("{} : {} is going to be launched (UUID={}, iterationNumber={}) with the following inputs {}", - pluginName, plugin.getPluginDeclaration().getVersion(), + pluginName, plugin.getVersion(), uuid, iterationNumber, inputs); JobUsageRecord jobUsageRecord = new JobUsageRecord(); @@ -92,7 +91,7 @@ public class RunnablePlugin> imple long startTime = actualStateEvolution.getTimestamp(); try { - SmartExecutorInitializator.setContext(token); + ContextUtility.setContext(token); setState(PluginState.RUNNING); @@ -103,7 +102,7 @@ public class RunnablePlugin> imple jobUsageRecord.setHost(hnRef.getAddress()); - ClientInfo clientInfo = SmartExecutorInitializator.getClientInfo(); + ClientInfo clientInfo = ContextUtility.getClientInfo(); String consumerId = clientInfo.getId(); jobUsageRecord.setConsumerId(consumerId); @@ -222,7 +221,7 @@ public class RunnablePlugin> imple throw new AlreadyInFinalStateException(); } - PluginStateEvolution pluginStateEvolution = new PluginStateEvolution(uuid, iterationNumber, timestamp, plugin.getPluginDeclaration(), pluginState, percentage); + PluginStateEvolution pluginStateEvolution = new PluginStateEvolution(uuid, iterationNumber, timestamp, plugin, pluginState, percentage); for(PluginStateNotification pluginStateNotification : pluginStateNotifications){ String pluginStateNotificationName = pluginStateNotification.getClass().getSimpleName(); @@ -241,7 +240,7 @@ public class RunnablePlugin> imple public String toString(){ return String.format("UUID : %s, Iteration : %d, Plugin : %s", uuid.toString(), iterationNumber, - plugin.getPluginDeclaration().getName()); + plugin.getName()); } /** diff --git a/src/main/java/org/gcube/vremanagement/executor/rest/RestSmartExecutor.java b/src/main/java/org/gcube/vremanagement/executor/rest/RestSmartExecutor.java index 01dd41c..dc048e0 100644 --- a/src/main/java/org/gcube/vremanagement/executor/rest/RestSmartExecutor.java +++ b/src/main/java/org/gcube/vremanagement/executor/rest/RestSmartExecutor.java @@ -35,7 +35,8 @@ import org.gcube.vremanagement.executor.json.ExtendedSEMapper; import org.gcube.vremanagement.executor.json.SEMapper; import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceConnector; import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceFactory; -import org.gcube.vremanagement.executor.plugin.PluginDeclaration; +import org.gcube.vremanagement.executor.plugin.Plugin; +import org.gcube.vremanagement.executor.plugin.PluginDefinition; import org.gcube.vremanagement.executor.plugin.PluginStateEvolution; import org.gcube.vremanagement.executor.pluginmanager.PluginManager; import org.gcube.vremanagement.executor.scheduledtask.ScheduledTask; @@ -76,10 +77,12 @@ public class RestSmartExecutor { //setCalledMethod(HttpMethod.GET + " /" + RestConstants.PLUGINS_PATH_PART); setCalledMethod("getAvailablePlugins"); try { - PluginManager pluginManager = PluginManager.getInstance(); - Map availablePlugins = pluginManager.getAvailablePlugins(); - List plugins = new ArrayList(availablePlugins.values()); - return ExtendedSEMapper.getInstance().marshal(PluginDeclaration.class, plugins); + Map> availablePlugins = PluginManager.getAvailablePlugins(); + List plugins = new ArrayList<>(); + for(String pluginName : availablePlugins.keySet()) { + plugins.add(PluginManager.getPlugin(pluginName)); + } + return ExtendedSEMapper.getInstance().marshal(PluginDefinition.class, plugins); }catch (Exception e) { throw new ExecutorException(e); } @@ -95,24 +98,15 @@ public class RestSmartExecutor { SmartExecutorPersistenceConnector persistenceConnector = SmartExecutorPersistenceFactory .getPersistenceConnector(); - List pluginDeclarations = new ArrayList<>(); + List plugins = new ArrayList<>(); if(pluginName.compareTo(RestConstants.ORPHAN_PATH_PARAM)!=0) { - PluginManager pluginManager = PluginManager.getInstance(); - Map availablePlugins = pluginManager.getAvailablePlugins(); - PluginDeclaration pluginDeclaration = availablePlugins.get(pluginName); - if(pluginDeclaration==null) { - String error = String.format("This SmartExecutor instace does not manage any plugin with name %s", pluginName); - logger.error(error); - throw new ExecutorException(error); - }else { - pluginDeclarations.add(pluginDeclaration); - } + plugins.addAll(PluginManager.getAvailablePlugins().keySet()); }else { // TODO check role } - List scheduledTasks = persistenceConnector.getOrphanScheduledTasks(pluginDeclarations); + List scheduledTasks = persistenceConnector.getOrphanScheduledTasks(plugins); /* @@ -202,10 +196,10 @@ public class RestSmartExecutor { throw new ExecutorException(e); } - if(pluginName.compareTo(pluginStateEvolution.getPluginDeclaration().getName()) != 0) { + if(pluginName.compareTo(pluginStateEvolution.getPluginDefinition().getName()) != 0) { String error = String.format( "Plugin Name provided in the URL (%s) does not match with the one got from %s (%s)", pluginName, - PluginStateEvolution.class.getSimpleName(), pluginStateEvolution.getPluginDeclaration().getName()); + PluginStateEvolution.class.getSimpleName(), pluginStateEvolution.getPluginDefinition().getName()); throw new InvalidInputsException(error); } diff --git a/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTask.java b/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTask.java index cd1099e..fefadf6 100644 --- a/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTask.java +++ b/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTask.java @@ -14,7 +14,7 @@ import org.gcube.common.resources.gcore.HostingNode; import org.gcube.common.resources.gcore.utils.Group; import org.gcube.smartgears.Constants; import org.gcube.smartgears.ContextProvider; -import org.gcube.vremanagement.executor.SmartExecutorInitializator; +import org.gcube.vremanagement.executor.ContextUtility; import org.gcube.vremanagement.executor.api.types.LaunchParameter; import org.gcube.vremanagement.executor.json.ExtendedSEMapper; import org.gcube.vremanagement.executor.json.SEMapper; @@ -42,8 +42,8 @@ public class ScheduledTask extends org.gcube.vremanagement.executor.plugin.Sched this.uuid = uuid; this.launchParameter = launchParameter; this.token = SecurityTokenProvider.instance.get(); - this.scope = SmartExecutorInitializator.getCurrentScope(); - this.clientInfo = SmartExecutorInitializator.getClientInfo(); + this.scope = ContextUtility.getCurrentScope(); + this.clientInfo = ContextUtility.getClientInfo(); this.runOn = runOn; } diff --git a/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTaskPersistence.java b/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTaskPersistence.java index 25e6fa0..2dd7cff 100644 --- a/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTaskPersistence.java +++ b/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTaskPersistence.java @@ -8,7 +8,6 @@ import java.util.List; import java.util.UUID; import org.gcube.vremanagement.executor.exception.SchedulePersistenceException; -import org.gcube.vremanagement.executor.plugin.PluginDeclaration; /** * @author Luca Frosini (ISTI - CNR) @@ -18,13 +17,12 @@ public interface ScheduledTaskPersistence { /** * Retrieve from the #SmartExecutorPersistenceConnector the orphaned * Scheduled tasks - * @param pluginDeclarations + * @param plugins * @return the list of orphaned Scheduled * @throws SchedulePersistenceException * if fails */ - public List getOrphanScheduledTasks( - Collection pluginDeclarations) + public List getOrphanScheduledTasks(Collection plugins) throws SchedulePersistenceException; /** diff --git a/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorScheduler.java b/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorScheduler.java index f35e66d..f9308cb 100644 --- a/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorScheduler.java +++ b/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorScheduler.java @@ -14,6 +14,7 @@ import java.util.UUID; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.vremanagement.executor.api.types.LaunchParameter; import org.gcube.vremanagement.executor.api.types.Scheduling; +import org.gcube.vremanagement.executor.exception.ExecutorException; import org.gcube.vremanagement.executor.exception.InputsNullException; import org.gcube.vremanagement.executor.exception.LaunchException; import org.gcube.vremanagement.executor.exception.PluginInstanceNotFoundException; @@ -192,7 +193,14 @@ public class SmartExecutorScheduler { * Checking if the requested plugin is available on this smart executor * instance */ - PluginManager.getPluginDeclaration(parameter.getPluginName()); + try { + PluginManager.getPlugin(parameter.getPluginName()); + } catch (PluginNotFoundException e) { + throw e; + } catch (ExecutorException e) { + throw new LaunchException(e); + } + if(uuid == null) { uuid = UUID.randomUUID(); diff --git a/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorSchedulerFactory.java b/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorSchedulerFactory.java index 53dad23..7628526 100644 --- a/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorSchedulerFactory.java +++ b/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorSchedulerFactory.java @@ -3,7 +3,7 @@ package org.gcube.vremanagement.executor.scheduler; import java.util.HashMap; import java.util.Map; -import org.gcube.vremanagement.executor.SmartExecutorInitializator; +import org.gcube.vremanagement.executor.ContextUtility; import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceConnector; import org.quartz.Scheduler; import org.quartz.SchedulerException; @@ -45,7 +45,7 @@ public class SmartExecutorSchedulerFactory { Scheduler scheduler = schedulerFactory.getScheduler(); smartExecutorScheduler = new SmartExecutorScheduler(scheduler); - smartExecutorSchedulers.put(SmartExecutorInitializator.getCurrentScope(), + smartExecutorSchedulers.put(ContextUtility.getCurrentScope(), smartExecutorScheduler); } @@ -57,13 +57,12 @@ public class SmartExecutorSchedulerFactory { * @throws SchedulerException */ public static synchronized SmartExecutorScheduler getSmartExecutorScheduler() throws SchedulerException { - String scope = SmartExecutorInitializator.getCurrentScope(); + String scope = ContextUtility.getCurrentScope(); return getSmartExecutorScheduler(scope); } - public static void remove(){ - String scope = SmartExecutorInitializator.getCurrentScope(); + String scope = ContextUtility.getCurrentScope(); smartExecutorSchedulers.remove(scope); } diff --git a/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorTask.java b/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorTask.java index ff48b81..2c56a0c 100644 --- a/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorTask.java +++ b/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorTask.java @@ -13,13 +13,10 @@ import java.util.UUID; import org.gcube.vremanagement.executor.api.types.LaunchParameter; import org.gcube.vremanagement.executor.api.types.Scheduling; import org.gcube.vremanagement.executor.exception.AlreadyInFinalStateException; -import org.gcube.vremanagement.executor.exception.InputsNullException; import org.gcube.vremanagement.executor.exception.InvalidPluginStateEvolutionException; -import org.gcube.vremanagement.executor.exception.PluginNotFoundException; import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceConnector; import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceFactory; import org.gcube.vremanagement.executor.plugin.Plugin; -import org.gcube.vremanagement.executor.plugin.PluginDeclaration; import org.gcube.vremanagement.executor.plugin.PluginState; import org.gcube.vremanagement.executor.plugin.PluginStateNotification; import org.gcube.vremanagement.executor.pluginmanager.PluginManager; @@ -74,9 +71,9 @@ public class SmartExecutorTask implements InterruptableJob { /* Derived from launchParameter*/ protected int executionCount; protected String pluginName; - protected Plugin plugin; + protected Plugin plugin; protected Map inputs; - protected RunnablePlugin> runnablePlugin; + protected RunnablePlugin runnablePlugin; protected boolean mustPreviousExecutionsCompleted; protected int maxExecutionNumber; /**/ @@ -90,8 +87,8 @@ public class SmartExecutorTask implements InterruptableJob { pluginName = launchParameter.getPluginName(); try { - plugin = PluginManager.instantiatePlugin(pluginName); - } catch (InputsNullException | PluginNotFoundException e) { + plugin = PluginManager.getPlugin(pluginName); + } catch (Exception e) { throw new JobExecutionException(e); } @@ -216,7 +213,7 @@ public class SmartExecutorTask implements InterruptableJob { return; } - runnablePlugin = new RunnablePlugin>( + runnablePlugin = new RunnablePlugin( plugin, inputs, uuid, executionCount, pluginStateNotifications, token); logger.debug("Going to run Job with ID {} (iteration {})", uuid, executionCount); diff --git a/src/test/java/org/gcube/vremanagement/executor/SerializationTest.java b/src/test/java/org/gcube/vremanagement/executor/SerializationTest.java index 1fcb202..e0c023c 100644 --- a/src/test/java/org/gcube/vremanagement/executor/SerializationTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/SerializationTest.java @@ -19,14 +19,15 @@ import org.gcube.vremanagement.executor.api.types.Scheduling; import org.gcube.vremanagement.executor.exception.InvalidPluginStateEvolutionException; import org.gcube.vremanagement.executor.json.ExtendedSEMapper; import org.gcube.vremanagement.executor.json.SEMapper; -import org.gcube.vremanagement.executor.plugin.PluginDeclaration; +import org.gcube.vremanagement.executor.plugin.Plugin; +import org.gcube.vremanagement.executor.plugin.PluginDefinition; import org.gcube.vremanagement.executor.plugin.PluginState; import org.gcube.vremanagement.executor.plugin.PluginStateEvolution; import org.gcube.vremanagement.executor.plugin.Ref; import org.gcube.vremanagement.executor.plugin.RunOn; import org.gcube.vremanagement.executor.pluginmanager.PluginManager; import org.gcube.vremanagement.executor.scheduledtask.ScheduledTask; -import org.gcube.vremanagement.helloworld.HelloWorldPluginDeclaration; +import org.gcube.vremanagement.helloworld.HelloWorldPlugin; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -93,7 +94,7 @@ public class SerializationTest extends ContextTest { throws JsonGenerationException, JsonMappingException, IOException, InvalidPluginStateEvolutionException { PluginStateEvolution pes = new PluginStateEvolution(UUID.randomUUID(), 1, - Calendar.getInstance().getTimeInMillis(), new HelloWorldPluginDeclaration(), PluginState.RUNNING, 10); + Calendar.getInstance().getTimeInMillis(), new HelloWorldPlugin(), PluginState.RUNNING, 10); logger.debug("{} to be Marshalled : {}", pes.getClass().getSimpleName(), pes); ObjectMapper objectMapper = new ObjectMapper(); @@ -107,13 +108,12 @@ public class SerializationTest extends ContextTest { @Test public void testAvailablePluginMarshalling() throws Exception { - HelloWorldPluginDeclaration helloWorldPluginDeclaration = new HelloWorldPluginDeclaration(); - logger.debug("{}", ExtendedSEMapper.getInstance().marshal(helloWorldPluginDeclaration)); - - PluginManager pluginManager = PluginManager.getInstance(); - Map availablePlugins = pluginManager.getAvailablePlugins(); - List plugins = new ArrayList(availablePlugins.values()); - String list = ExtendedSEMapper.getInstance().marshal(PluginDeclaration.class, plugins); + Map> availablePlugins = PluginManager.getAvailablePlugins(); + List plugins = new ArrayList<>(); + for(String pluginName : availablePlugins.keySet()) { + plugins.add(PluginManager.getPlugin(pluginName)); + } + String list = ExtendedSEMapper.getInstance().marshal(PluginDefinition.class, plugins); logger.debug("Plugins are :\n{}", list); } diff --git a/src/test/java/org/gcube/vremanagement/executor/SmartExecutorImplTest.java b/src/test/java/org/gcube/vremanagement/executor/SmartExecutorImplTest.java index 25d8312..501fbe4 100644 --- a/src/test/java/org/gcube/vremanagement/executor/SmartExecutorImplTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/SmartExecutorImplTest.java @@ -8,10 +8,8 @@ import java.util.HashMap; import java.util.Map; import org.gcube.vremanagement.executor.plugin.Plugin; -import org.gcube.vremanagement.executor.plugin.PluginDeclaration; import org.gcube.vremanagement.executor.pluginmanager.PluginManager; import org.gcube.vremanagement.helloworld.HelloWorldPlugin; -import org.gcube.vremanagement.helloworld.HelloWorldPluginDeclaration; import org.junit.Test; /** @@ -25,17 +23,9 @@ public class SmartExecutorImplTest { Map inputs = new HashMap(); long sleepTime = 10000; inputs.put(HelloWorldPlugin.SLEEP_TIME, sleepTime); - Plugin runnablePlugin = PluginManager.instantiatePlugin(HelloWorldPluginDeclaration.NAME); - runnablePlugin.launch(inputs); - } - - @Test - public void helloWorldFullTest() throws Exception{ - Map inputs = new HashMap(); - long sleepTime = 10000; - inputs.put(HelloWorldPlugin.SLEEP_TIME, sleepTime); - Plugin runnablePlugin = PluginManager.instantiatePlugin(HelloWorldPluginDeclaration.NAME); - runnablePlugin.launch(inputs); + String name = (new HelloWorldPlugin()).getName(); + Plugin plugin = PluginManager.getPlugin(name); + plugin.launch(inputs); } } diff --git a/src/test/java/org/gcube/vremanagement/executor/SmartExecutorInizializatorTest.java b/src/test/java/org/gcube/vremanagement/executor/SmartExecutorInizializatorTest.java index 4c1514d..a7cfc0d 100644 --- a/src/test/java/org/gcube/vremanagement/executor/SmartExecutorInizializatorTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/SmartExecutorInizializatorTest.java @@ -88,11 +88,11 @@ public class SmartExecutorInizializatorTest { for (ServiceEndpoint serviceEndpoint : serviceEndpoints) { try { logger.debug("Trying to unpublish the old ServiceEndpoint with ID {} ({}) from scope {}", - serviceEndpoint.id(), serviceEndpoint.profile().runtime().hostedOn(), SmartExecutorInitializator.getCurrentScope()); + serviceEndpoint.id(), serviceEndpoint.profile().runtime().hostedOn(), ContextUtility.getCurrentScope()); // unPublishResource(serviceEndpoint); } catch(Exception e){ logger.debug("Exception tryng to unpublish the old ServiceEndpoint with ID {} ({}) from scope {}", - serviceEndpoint.id(), serviceEndpoint.profile().runtime().hostedOn(), SmartExecutorInitializator.getCurrentScope(), e); + serviceEndpoint.id(), serviceEndpoint.profile().runtime().hostedOn(), ContextUtility.getCurrentScope(), e); } } }catch(Exception e){ diff --git a/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java b/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java index 3424fc4..eb5269e 100644 --- a/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java @@ -15,7 +15,7 @@ import org.gcube.vremanagement.executor.plugin.PluginStateEvolution; import org.gcube.vremanagement.executor.scheduledtask.ScheduledTask; import org.gcube.vremanagement.executor.scheduledtask.ScheduledTaskPersistence; import org.gcube.vremanagement.executor.scheduledtask.ScheduledTaskPersistenceFactory; -import org.gcube.vremanagement.helloworld.HelloWorldPluginDeclaration; +import org.gcube.vremanagement.helloworld.HelloWorldPlugin; import org.junit.Assert; import org.junit.Test; import org.slf4j.Logger; @@ -47,7 +47,7 @@ public class SmartExecutorPersistenceConnectorTest extends ContextTest { for(int i=0; i pluginStateNotifications = new ArrayList(); pluginStateNotifications.add(persistenceConnector); - HelloWorldPlugin helloWorldPlugin = new HelloWorldPlugin(hwpd); + HelloWorldPlugin helloWorldPlugin = new HelloWorldPlugin(); try { RunnablePlugin runnablePlugin = new RunnablePlugin(helloWorldPlugin, null, uuid, 1, pluginStateNotifications, SecurityTokenProvider.instance.get()); runnablePlugin.run(); @@ -52,11 +51,10 @@ public class RunnablePluginTest extends ContextTest { logger.debug("Testing Empty inputs"); Map inputs = new HashMap(); UUID uuid = UUID.randomUUID(); - HelloWorldPluginDeclaration hwpd = new HelloWorldPluginDeclaration(); SmartExecutorPersistenceConnector persistenceConnector = SmartExecutorPersistenceFactory.getPersistenceConnector(); List pluginStateNotifications = new ArrayList(); pluginStateNotifications.add(persistenceConnector); - HelloWorldPlugin helloWorldPlugin = new HelloWorldPlugin(hwpd); + HelloWorldPlugin helloWorldPlugin = new HelloWorldPlugin(); RunnablePlugin pt = new RunnablePlugin(helloWorldPlugin, inputs, uuid, 1, pluginStateNotifications,SecurityTokenProvider.instance.get()); try { @@ -76,11 +74,10 @@ public class RunnablePluginTest extends ContextTest { inputs.put(HelloWorldPlugin.SLEEP_TIME, sleepTime); UUID uuid = UUID.randomUUID(); - HelloWorldPluginDeclaration hwpd = new HelloWorldPluginDeclaration(); SmartExecutorPersistenceConnector persistenceConnector = SmartExecutorPersistenceFactory.getPersistenceConnector(); List pluginStateNotifications = new ArrayList(); pluginStateNotifications.add(persistenceConnector); - HelloWorldPlugin helloWorldPlugin = new HelloWorldPlugin(hwpd); + HelloWorldPlugin helloWorldPlugin = new HelloWorldPlugin(); RunnablePlugin rp = new RunnablePlugin(helloWorldPlugin, inputs, uuid, 1, pluginStateNotifications,SecurityTokenProvider.instance.get()); long startTime = Calendar.getInstance().getTimeInMillis(); long endTime = startTime; diff --git a/src/test/java/org/gcube/vremanagement/executor/pluginmanager/SmartExecutorSchedulerTest.java b/src/test/java/org/gcube/vremanagement/executor/pluginmanager/SmartExecutorSchedulerTest.java index a698a65..0e470be 100644 --- a/src/test/java/org/gcube/vremanagement/executor/pluginmanager/SmartExecutorSchedulerTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/pluginmanager/SmartExecutorSchedulerTest.java @@ -20,7 +20,6 @@ import org.gcube.vremanagement.executor.plugin.PluginStateEvolution; import org.gcube.vremanagement.executor.scheduler.SmartExecutorScheduler; import org.gcube.vremanagement.executor.scheduler.SmartExecutorSchedulerFactory; import org.gcube.vremanagement.helloworld.HelloWorldPlugin; -import org.gcube.vremanagement.helloworld.HelloWorldPluginDeclaration; import org.junit.Assert; import org.junit.Test; import org.quartz.CronExpression; @@ -48,7 +47,7 @@ public class SmartExecutorSchedulerTest extends ContextTest { inputs.put("Test UUID", UUID.randomUUID()); logger.debug("Inputs : {}", inputs); - LaunchParameter parameter = new LaunchParameter(HelloWorldPluginDeclaration.NAME, inputs); + LaunchParameter parameter = new LaunchParameter(new HelloWorldPlugin().getName(), inputs); parameter.setScheduling(scheduling); SmartExecutorScheduler smartExecutorScheduler = SmartExecutorSchedulerFactory.getSmartExecutorScheduler(); From 9a282c4a7f46671e1fd3c04a76827b1ee55c1610 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 30 Sep 2020 13:57:09 +0200 Subject: [PATCH 11/33] Fixed properties retrieving --- .../vremanagement/executor/ContextTest.java | 19 ++++++++- .../SmartExecutorInizializatorTest.java | 42 +++++-------------- 2 files changed, 27 insertions(+), 34 deletions(-) diff --git a/src/test/java/org/gcube/vremanagement/executor/ContextTest.java b/src/test/java/org/gcube/vremanagement/executor/ContextTest.java index 35110fb..8d2198e 100644 --- a/src/test/java/org/gcube/vremanagement/executor/ContextTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/ContextTest.java @@ -50,7 +50,22 @@ public class ContextTest { DEFAULT_TEST_SCOPE_NAME = "/gcube/devNext/NextNext"; } - public static String getCurrentScope(String token) throws ObjectNotFound, Exception { + public static String getCurrentContext(){ + String token = SecurityTokenProvider.instance.get(); + AuthorizationEntry authorizationEntry; + try { + authorizationEntry = Constants.authorizationService().get(token); + } catch (Exception e) { + logger.trace("Context was not retrieved from token. Going to get it from {}", ScopeProvider.class.getSimpleName()); + return ScopeProvider.instance.get(); + } + String context = authorizationEntry.getContext(); + logger.trace("Context retrieved from token is {}. Context in {} is {}", + context, ScopeProvider.class.getSimpleName(), ScopeProvider.instance.get()); + return context; + } + + public static String getContext(String token) throws ObjectNotFound, Exception { AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token); String context = authorizationEntry.getContext(); logger.info("Context of token {} is {}", token, context); @@ -70,7 +85,7 @@ public class ContextTest { String qualifier = authorizationEntry.getQualifier(); Caller caller = new Caller(clientInfo, qualifier); AuthorizationProvider.instance.set(caller); - ScopeProvider.instance.set(getCurrentScope(token)); + ScopeProvider.instance.set(getContext(token)); } @BeforeClass diff --git a/src/test/java/org/gcube/vremanagement/executor/SmartExecutorInizializatorTest.java b/src/test/java/org/gcube/vremanagement/executor/SmartExecutorInizializatorTest.java index a7cfc0d..a92aa9c 100644 --- a/src/test/java/org/gcube/vremanagement/executor/SmartExecutorInizializatorTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/SmartExecutorInizializatorTest.java @@ -5,19 +5,18 @@ package org.gcube.vremanagement.executor; import java.util.List; +import java.util.Map; -import org.gcube.common.authorization.client.Constants; -import org.gcube.common.authorization.client.exceptions.ObjectNotFound; -import org.gcube.common.authorization.library.AuthorizationEntry; -import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.resources.gcore.Resource; import org.gcube.common.resources.gcore.ServiceEndpoint; -import org.gcube.common.scope.api.ScopeProvider; import org.gcube.informationsystem.publisher.RegistryPublisher; import org.gcube.informationsystem.publisher.RegistryPublisherFactory; import org.gcube.resources.discovery.client.api.DiscoveryClient; import org.gcube.resources.discovery.client.queries.api.SimpleQuery; import org.gcube.resources.discovery.icclient.ICFactory; +import org.gcube.vremanagement.executor.plugin.Plugin; +import org.gcube.vremanagement.executor.pluginmanager.PluginManager; +import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -29,33 +28,12 @@ public class SmartExecutorInizializatorTest { private static Logger logger = LoggerFactory.getLogger(SmartExecutorInizializatorTest.class); - public static String getCurrentScope(String token) throws ObjectNotFound, Exception{ - AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token); - String context = authorizationEntry.getContext(); - logger.info("Context of token {} is {}", token, context); - return context; - } - - - public static void setContext(String token) throws ObjectNotFound, Exception{ - SecurityTokenProvider.instance.set(token); - ScopeProvider.instance.set(getCurrentScope(token)); - } - - - public static String getCurrentScope(){ - String token = SecurityTokenProvider.instance.get(); - AuthorizationEntry authorizationEntry; - try { - authorizationEntry = Constants.authorizationService().get(token); - } catch (Exception e) { - logger.trace("Context was not retrieved from token. Going to get it from {}", ScopeProvider.class.getSimpleName()); - return ScopeProvider.instance.get(); + @Test + public void getAvailablePlugin() { + Map> availablePlugins = PluginManager.getAvailablePlugins(); + for(String pluginName : availablePlugins.keySet()) { + logger.debug("Plugin {}, Class {}", pluginName, availablePlugins.get(pluginName).getSimpleName()); } - String context = authorizationEntry.getContext(); - logger.trace("Context retrieved from token is {}. Context in {} is {}", - context, ScopeProvider.class.getSimpleName(), ScopeProvider.instance.get()); - return context; } protected static void unPublishResource(Resource resource) throws Exception { @@ -65,7 +43,7 @@ public class SmartExecutorInizializatorTest { RegistryPublisher registryPublisher = RegistryPublisherFactory.create(); String id = resource.id(); - logger.debug("Trying to remove {} with ID {} from {}", resource.getClass().getSimpleName(), id, getCurrentScope()); + logger.debug("Trying to remove {} with ID {} from {}", resource.getClass().getSimpleName(), id, ContextTest.getCurrentContext()); registryPublisher.remove(resource); From 90512acf7bd1a96d83f1110edf0102f73dce7fa6 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 30 Sep 2020 15:47:49 +0200 Subject: [PATCH 12/33] Fixing pubblication --- .../executor/ispublisher/RestISPublisher.java | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java b/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java index e2464f1..579a16f 100644 --- a/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java +++ b/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java @@ -16,11 +16,15 @@ import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPu import org.gcube.resourcemanagement.model.impl.entities.facets.SimplePropertyFacetImpl; import org.gcube.resourcemanagement.model.impl.entities.facets.SoftwareFacetImpl; import org.gcube.resourcemanagement.model.impl.entities.resources.RunningPluginImpl; +import org.gcube.resourcemanagement.model.impl.relations.isrelatedto.EnablesImpl; import org.gcube.resourcemanagement.model.impl.relations.isrelatedto.UsesImpl; import org.gcube.resourcemanagement.model.reference.entities.facets.SimplePropertyFacet; import org.gcube.resourcemanagement.model.reference.entities.facets.SoftwareFacet; import org.gcube.resourcemanagement.model.reference.entities.resources.EService; import org.gcube.resourcemanagement.model.reference.entities.resources.RunningPlugin; +import org.gcube.resourcemanagement.model.reference.entities.resources.Service; +import org.gcube.resourcemanagement.model.reference.entities.resources.Software; +import org.gcube.resourcemanagement.model.reference.relations.isrelatedto.Enables; import org.gcube.resourcemanagement.model.reference.relations.isrelatedto.Uses; import org.gcube.smartgears.context.application.ApplicationContext; import org.gcube.vremanagement.executor.plugin.Plugin; @@ -44,15 +48,13 @@ public class RestISPublisher extends ISPublisher { ResourceRegistryClient resourceRegistryClient = ResourceRegistryClientFactory.create(); ResourceRegistryPublisher resourceRegistryPublisher = ResourceRegistryPublisherFactory.create(); - EService smartExecutorEService = resourceRegistryClient.getInstance(EService.class, eServiceUUID); - for(String pluginName : availablePlugins.keySet()) { Plugin plugin = PluginManager.getPlugin(pluginName); RunningPlugin runningPlugin = new RunningPluginImpl(); SoftwareFacet softwareFacet = new SoftwareFacetImpl(); - softwareFacet.setGroup("SmartExecutorPlugin"); + softwareFacet.setGroup(plugin.getGroup()); softwareFacet.setName(pluginName); softwareFacet.setVersion(plugin.getVersion()); softwareFacet.setDescription(plugin.getDescription()); @@ -66,11 +68,13 @@ public class RestISPublisher extends ISPublisher { runningPlugin.addFacet(simplePropertyFacet); } - PropagationConstraint propagationConstraint = new PropagationConstraintImpl(); - propagationConstraint.setAddConstraint(AddConstraint.propagate); - propagationConstraint.setRemoveConstraint(RemoveConstraint.cascade); - Uses uses = new UsesImpl(smartExecutorEService, runningPlugin, propagationConstraint); + EService smartExecutorEService = resourceRegistryClient.getInstance(EService.class, eServiceUUID); + PropagationConstraint usesPropagationConstraint = new PropagationConstraintImpl(); + usesPropagationConstraint.setAddConstraint(AddConstraint.propagate); + usesPropagationConstraint.setRemoveConstraint(RemoveConstraint.cascade); + + Uses uses = new UsesImpl(smartExecutorEService, runningPlugin, usesPropagationConstraint); try { resourceRegistryPublisher.createIsRelatedTo(uses); } catch (ResourceRegistryException e) { @@ -78,6 +82,20 @@ public class RestISPublisher extends ISPublisher { throw e; } + org.gcube.resourcemanagement.model.reference.entities.resources.Plugin pluginResource = null; + if(pluginResource!=null) { // The if allows not commenting the following code in the meanwhile the pluginResource retrieving is properly coded + PropagationConstraint enablesPropagationConstraint = new PropagationConstraintImpl(); + enablesPropagationConstraint.setAddConstraint(AddConstraint.propagate); + enablesPropagationConstraint.setRemoveConstraint(RemoveConstraint.keep); + Enables enables = new EnablesImpl(runningPlugin, pluginResource, enablesPropagationConstraint); + try { + resourceRegistryPublisher.createIsRelatedTo(enables); + } catch (ResourceRegistryException e) { + logger.error("Unable to publish %s instace %s for plugin %s. I'm going to stop the service.", Resource.NAME, RunningPlugin.NAME, pluginName); + throw e; + } + } + } } From 36a4e6684da392d2e1f7925e3d128f1b43f27ccc Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 30 Sep 2020 16:18:46 +0200 Subject: [PATCH 13/33] Implementing new IS publisher --- .../executor/SmartExecutorInitializator.java | 3 + .../executor/ispublisher/RestISPublisher.java | 116 +++++++++++------- .../executor/pluginmanager/PluginManager.java | 14 ++- 3 files changed, 88 insertions(+), 45 deletions(-) diff --git a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java index 3dcc16e..245e848 100644 --- a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java +++ b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java @@ -33,6 +33,9 @@ public class SmartExecutorInitializator implements ApplicationManager { public static final long JOIN_TIMEOUT = 1000; + protected static GCoreISPublisher gCoreISPublisher; + protected static RestISPublisher restISPublisher; + /** * {@inheritDoc} * The method discover the plugins available on classpath and their own diff --git a/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java b/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java index 579a16f..2a81fc1 100644 --- a/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java +++ b/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java @@ -3,11 +3,14 @@ package org.gcube.vremanagement.executor.ispublisher; import java.util.Map; import java.util.UUID; +import org.gcube.informationsystem.model.impl.properties.HeaderImpl; import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl; import org.gcube.informationsystem.model.reference.entities.Resource; import org.gcube.informationsystem.model.reference.properties.PropagationConstraint; import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.AddConstraint; import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.RemoveConstraint; +import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient; import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory; @@ -37,63 +40,88 @@ public class RestISPublisher extends ISPublisher { private static Logger logger = LoggerFactory.getLogger(RestISPublisher.class); protected final UUID eServiceUUID; + protected ResourceRegistryClient resourceRegistryClient; + protected ResourceRegistryPublisher resourceRegistryPublisher; public RestISPublisher(ApplicationContext applicationContext) { super(applicationContext); this.eServiceUUID = UUID.fromString(applicationContext.id()); + this.resourceRegistryClient = ResourceRegistryClientFactory.create(); + this.resourceRegistryPublisher = ResourceRegistryPublisherFactory.create(); } - @Override - public void publishPlugins(Map> availablePlugins) throws Exception { - ResourceRegistryClient resourceRegistryClient = ResourceRegistryClientFactory.create(); - ResourceRegistryPublisher resourceRegistryPublisher = ResourceRegistryPublisherFactory.create(); + @SuppressWarnings("unused") + protected RunningPlugin publishRunningPluginWithRelations(String pluginName) throws Exception { + Plugin plugin = PluginManager.getPlugin(pluginName); - for(String pluginName : availablePlugins.keySet()) { - Plugin plugin = PluginManager.getPlugin(pluginName); - - RunningPlugin runningPlugin = new RunningPluginImpl(); - - SoftwareFacet softwareFacet = new SoftwareFacetImpl(); - softwareFacet.setGroup(plugin.getGroup()); - softwareFacet.setName(pluginName); - softwareFacet.setVersion(plugin.getVersion()); - softwareFacet.setDescription(plugin.getDescription()); - runningPlugin.addFacet(softwareFacet); - - Map pluginCapabilities = plugin.getSupportedCapabilities(); - for(String capabilityName : pluginCapabilities.keySet()) { - SimplePropertyFacet simplePropertyFacet = new SimplePropertyFacetImpl(); - simplePropertyFacet.setName(capabilityName); - simplePropertyFacet.setValue(pluginCapabilities.get(capabilityName)); - runningPlugin.addFacet(simplePropertyFacet); - } - - - EService smartExecutorEService = resourceRegistryClient.getInstance(EService.class, eServiceUUID); - PropagationConstraint usesPropagationConstraint = new PropagationConstraintImpl(); - usesPropagationConstraint.setAddConstraint(AddConstraint.propagate); - usesPropagationConstraint.setRemoveConstraint(RemoveConstraint.cascade); - - Uses uses = new UsesImpl(smartExecutorEService, runningPlugin, usesPropagationConstraint); + RunningPlugin runningPlugin = new RunningPluginImpl(); + + SoftwareFacet softwareFacet = new SoftwareFacetImpl(); + softwareFacet.setGroup(plugin.getGroup()); + softwareFacet.setName(pluginName); + softwareFacet.setVersion(plugin.getVersion()); + softwareFacet.setDescription(plugin.getDescription()); + runningPlugin.addFacet(softwareFacet); + + Map pluginCapabilities = plugin.getSupportedCapabilities(); + for(String capabilityName : pluginCapabilities.keySet()) { + SimplePropertyFacet simplePropertyFacet = new SimplePropertyFacetImpl(); + simplePropertyFacet.setName(capabilityName); + simplePropertyFacet.setValue(pluginCapabilities.get(capabilityName)); + runningPlugin.addFacet(simplePropertyFacet); + } + + EService smartExecutorEService = resourceRegistryClient.getInstance(EService.class, eServiceUUID); + PropagationConstraint usesPropagationConstraint = new PropagationConstraintImpl(); + usesPropagationConstraint.setAddConstraint(AddConstraint.propagate); + usesPropagationConstraint.setRemoveConstraint(RemoveConstraint.cascade); + + Uses uses = new UsesImpl(smartExecutorEService, runningPlugin, usesPropagationConstraint); + try { + resourceRegistryPublisher.createIsRelatedTo(uses); + } catch (ResourceRegistryException e) { + logger.error("Unable to publish %s instace %s for plugin %s. I'm going to stop the service.", Resource.NAME, RunningPlugin.NAME, pluginName); + throw e; + } + + org.gcube.resourcemanagement.model.reference.entities.resources.Plugin pluginResource = null; + if(pluginResource!=null) { // The if allows not commenting the following code in the meanwhile the pluginResource retrieving is properly coded + PropagationConstraint enablesPropagationConstraint = new PropagationConstraintImpl(); + enablesPropagationConstraint.setAddConstraint(AddConstraint.propagate); + enablesPropagationConstraint.setRemoveConstraint(RemoveConstraint.keep); + Enables enables = new EnablesImpl(runningPlugin, pluginResource, enablesPropagationConstraint); try { - resourceRegistryPublisher.createIsRelatedTo(uses); + resourceRegistryPublisher.createIsRelatedTo(enables); } catch (ResourceRegistryException e) { logger.error("Unable to publish %s instace %s for plugin %s. I'm going to stop the service.", Resource.NAME, RunningPlugin.NAME, pluginName); throw e; } + } + + return runningPlugin; + } + + @Override + public void publishPlugins(Map> availablePlugins) throws Exception { + + UUID contextUUID = resourceRegistryClient.getCurrentContext().getHeader().getUUID(); + + for(String pluginName : availablePlugins.keySet()) { - org.gcube.resourcemanagement.model.reference.entities.resources.Plugin pluginResource = null; - if(pluginResource!=null) { // The if allows not commenting the following code in the meanwhile the pluginResource retrieving is properly coded - PropagationConstraint enablesPropagationConstraint = new PropagationConstraintImpl(); - enablesPropagationConstraint.setAddConstraint(AddConstraint.propagate); - enablesPropagationConstraint.setRemoveConstraint(RemoveConstraint.keep); - Enables enables = new EnablesImpl(runningPlugin, pluginResource, enablesPropagationConstraint); - try { - resourceRegistryPublisher.createIsRelatedTo(enables); - } catch (ResourceRegistryException e) { - logger.error("Unable to publish %s instace %s for plugin %s. I'm going to stop the service.", Resource.NAME, RunningPlugin.NAME, pluginName); - throw e; - } + UUID uuid = PluginManager.getPluginUUID(pluginName); + + RunningPlugin runningPlugin; + + try { + runningPlugin = resourceRegistryClient.getInstance(RunningPlugin.class, uuid); + } catch (NotFoundException e) { + runningPlugin = publishRunningPluginWithRelations(pluginName); + } catch (AvailableInAnotherContextException e) { + runningPlugin = new RunningPluginImpl(); + runningPlugin.setHeader(new HeaderImpl(uuid)); + resourceRegistryPublisher.addToContext(contextUUID, runningPlugin); + } catch (ResourceRegistryException e) { + throw e; } } diff --git a/src/main/java/org/gcube/vremanagement/executor/pluginmanager/PluginManager.java b/src/main/java/org/gcube/vremanagement/executor/pluginmanager/PluginManager.java index 91a7839..f109c97 100644 --- a/src/main/java/org/gcube/vremanagement/executor/pluginmanager/PluginManager.java +++ b/src/main/java/org/gcube/vremanagement/executor/pluginmanager/PluginManager.java @@ -3,6 +3,7 @@ package org.gcube.vremanagement.executor.pluginmanager; import java.util.HashMap; import java.util.Map; import java.util.ServiceLoader; +import java.util.UUID; import org.gcube.vremanagement.executor.exception.ExecutorException; import org.gcube.vremanagement.executor.exception.PluginNotFoundException; @@ -34,6 +35,8 @@ public class PluginManager { */ private Map> availablePlugins; + private Map uuids; + /** * Get the singleton instance of {@link #PluginManager}. * The first time this function is invoked the instance is null @@ -53,13 +56,15 @@ public class PluginManager { */ protected PluginManager(){ logger.debug("Loading plugins available on classpath"); - this.availablePlugins = new HashMap>(); + this.availablePlugins = new HashMap<>(); + this.uuids = new HashMap<>(); ServiceLoader serviceLoader = ServiceLoader.load(Plugin.class); for (Plugin plugin : serviceLoader) { try { logger.debug(String.format("%s plugin found", plugin.getName())); String name = plugin.getName(); this.availablePlugins.put(name, plugin.getClass()); + this.uuids.put(name, UUID.randomUUID()); } catch (Exception e) { logger.debug(String.format("%s not initialized correctly. It will not be used", plugin.getName())); } @@ -84,10 +89,17 @@ public class PluginManager { } } + + public static UUID getPluginUUID(String pluginName) { + return PluginManager.getInstance().uuids.get(pluginName); + } + /** * @return the availablePlugins */ public static Map> getAvailablePlugins() { return PluginManager.getInstance().availablePlugins; } + + } From 2e04364edc0b8f40fb40c36b7c9731563896636d Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 1 Oct 2020 12:51:57 +0200 Subject: [PATCH 14/33] Implementing new IS publisher --- .../executor/SmartExecutorInitializator.java | 33 +++++++++-------- .../ispublisher/GCoreISPublisher.java | 4 +- .../executor/ispublisher/ISPublisher.java | 15 +++++++- .../executor/ispublisher/RestISPublisher.java | 37 +++++++++++++------ 4 files changed, 59 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java index 245e848..c62a8ab 100644 --- a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java +++ b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java @@ -7,8 +7,7 @@ import org.gcube.smartgears.ApplicationManager; import org.gcube.smartgears.ContextProvider; import org.gcube.smartgears.context.application.ApplicationContext; import org.gcube.vremanagement.executor.api.types.LaunchParameter; -import org.gcube.vremanagement.executor.ispublisher.GCoreISPublisher; -import org.gcube.vremanagement.executor.ispublisher.RestISPublisher; +import org.gcube.vremanagement.executor.ispublisher.ISPublisher; import org.gcube.vremanagement.executor.json.ExtendedSEMapper; import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceConnector; import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceFactory; @@ -33,9 +32,6 @@ public class SmartExecutorInitializator implements ApplicationManager { public static final long JOIN_TIMEOUT = 1000; - protected static GCoreISPublisher gCoreISPublisher; - protected static RestISPublisher restISPublisher; - /** * {@inheritDoc} * The method discover the plugins available on classpath and their own @@ -59,11 +55,15 @@ public class SmartExecutorInitializator implements ApplicationManager { ApplicationContext applicationContext = ContextProvider.get(); try { - GCoreISPublisher gCoreISPublisher = new GCoreISPublisher(applicationContext); - gCoreISPublisher.publishPlugins(availablePlugins); - - RestISPublisher restISPublisher = new RestISPublisher(applicationContext); - restISPublisher.publishPlugins(availablePlugins); + List isPublishers = ISPublisher.getISPublishers(applicationContext); + for(ISPublisher isPublisher : isPublishers) { + try { + isPublisher.unpublishPlugins(true); + }catch (Exception e) { + logger.error("unable to unpublish plugind from IS using {}", isPublisher.getClass().getName()); + } + isPublisher.publishPlugins(availablePlugins); + } } catch (Exception e) { logger.error("Unable to Create ServiceEndpoint for scope {}. The Service will be aborted", scope, e); throw new RuntimeException(e); @@ -178,11 +178,14 @@ public class SmartExecutorInitializator implements ApplicationManager { } ApplicationContext applicationContext = ContextProvider.get(); - GCoreISPublisher gCoreISPublisher = new GCoreISPublisher(applicationContext); - gCoreISPublisher.unpublishPlugins(); - - RestISPublisher restISPublisher = new RestISPublisher(applicationContext); - restISPublisher.unpublishPlugins(); + List isPublishers = ISPublisher.getISPublishers(applicationContext); + for(ISPublisher isPublisher : isPublishers) { + try { + isPublisher.unpublishPlugins(false); + }catch (Exception e) { + logger.error("unable to unpublish plugind from IS using {}", isPublisher.getClass().getName()); + } + } try { SmartExecutorPersistenceFactory.closePersistenceConnector(); diff --git a/src/main/java/org/gcube/vremanagement/executor/ispublisher/GCoreISPublisher.java b/src/main/java/org/gcube/vremanagement/executor/ispublisher/GCoreISPublisher.java index df5e9c0..63899f5 100644 --- a/src/main/java/org/gcube/vremanagement/executor/ispublisher/GCoreISPublisher.java +++ b/src/main/java/org/gcube/vremanagement/executor/ispublisher/GCoreISPublisher.java @@ -31,7 +31,7 @@ import org.gcube.vremanagement.executor.pluginmanager.PluginManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class GCoreISPublisher extends ISPublisher { +class GCoreISPublisher extends ISPublisher { private static Logger logger = LoggerFactory.getLogger(GCoreISPublisher.class); @@ -241,7 +241,7 @@ public class GCoreISPublisher extends ISPublisher { } @Override - public void unpublishPlugins() { + public void unpublishPlugins(boolean force) throws Exception { cleanServiceEndpoints(); } diff --git a/src/main/java/org/gcube/vremanagement/executor/ispublisher/ISPublisher.java b/src/main/java/org/gcube/vremanagement/executor/ispublisher/ISPublisher.java index c51e389..8553398 100644 --- a/src/main/java/org/gcube/vremanagement/executor/ispublisher/ISPublisher.java +++ b/src/main/java/org/gcube/vremanagement/executor/ispublisher/ISPublisher.java @@ -1,5 +1,7 @@ package org.gcube.vremanagement.executor.ispublisher; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import org.gcube.smartgears.context.application.ApplicationContext; @@ -7,6 +9,17 @@ import org.gcube.vremanagement.executor.plugin.Plugin; public abstract class ISPublisher { + protected static List isPublishers; + + public static List getISPublishers(ApplicationContext applicationContext){ + if(isPublishers==null) { + List isPublishers = new ArrayList<>(); + isPublishers.add(new GCoreISPublisher(applicationContext)); + isPublishers.add(new RestISPublisher(applicationContext)); + } + return isPublishers; + } + protected ApplicationContext applicationContext; public ISPublisher(ApplicationContext applicationContext) { @@ -15,6 +28,6 @@ public abstract class ISPublisher { public abstract void publishPlugins(Map> availablePlugins) throws Exception; - public abstract void unpublishPlugins(); + public abstract void unpublishPlugins(boolean force) throws Exception ; } diff --git a/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java b/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java index 2a81fc1..a8913c4 100644 --- a/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java +++ b/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java @@ -1,5 +1,6 @@ package org.gcube.vremanagement.executor.ispublisher; +import java.util.List; import java.util.Map; import java.util.UUID; @@ -12,6 +13,7 @@ import org.gcube.informationsystem.model.reference.properties.PropagationConstra import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; +import org.gcube.informationsystem.resourceregistry.client.Direction; import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient; import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory; import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisher; @@ -35,7 +37,7 @@ import org.gcube.vremanagement.executor.pluginmanager.PluginManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class RestISPublisher extends ISPublisher { +class RestISPublisher extends ISPublisher { private static Logger logger = LoggerFactory.getLogger(RestISPublisher.class); @@ -43,6 +45,8 @@ public class RestISPublisher extends ISPublisher { protected ResourceRegistryClient resourceRegistryClient; protected ResourceRegistryPublisher resourceRegistryPublisher; + + public RestISPublisher(ApplicationContext applicationContext) { super(applicationContext); this.eServiceUUID = UUID.fromString(applicationContext.id()); @@ -51,14 +55,14 @@ public class RestISPublisher extends ISPublisher { } @SuppressWarnings("unused") - protected RunningPlugin publishRunningPluginWithRelations(String pluginName) throws Exception { - Plugin plugin = PluginManager.getPlugin(pluginName); + protected RunningPlugin publishRunningPluginWithRelations(Plugin plugin, UUID pluginUUID) throws Exception { RunningPlugin runningPlugin = new RunningPluginImpl(); + runningPlugin.setHeader(new HeaderImpl(pluginUUID)); SoftwareFacet softwareFacet = new SoftwareFacetImpl(); softwareFacet.setGroup(plugin.getGroup()); - softwareFacet.setName(pluginName); + softwareFacet.setName(plugin.getName()); softwareFacet.setVersion(plugin.getVersion()); softwareFacet.setDescription(plugin.getDescription()); runningPlugin.addFacet(softwareFacet); @@ -80,7 +84,7 @@ public class RestISPublisher extends ISPublisher { try { resourceRegistryPublisher.createIsRelatedTo(uses); } catch (ResourceRegistryException e) { - logger.error("Unable to publish %s instace %s for plugin %s. I'm going to stop the service.", Resource.NAME, RunningPlugin.NAME, pluginName); + logger.error("Unable to publish %s instace %s for plugin %s. I'm going to stop the service.", Resource.NAME, RunningPlugin.NAME, plugin.getName()); throw e; } @@ -93,7 +97,7 @@ public class RestISPublisher extends ISPublisher { try { resourceRegistryPublisher.createIsRelatedTo(enables); } catch (ResourceRegistryException e) { - logger.error("Unable to publish %s instace %s for plugin %s. I'm going to stop the service.", Resource.NAME, RunningPlugin.NAME, pluginName); + logger.error("Unable to publish %s instace %s for plugin %s. I'm going to stop the service.", Resource.NAME, RunningPlugin.NAME, plugin.getName()); throw e; } } @@ -108,17 +112,18 @@ public class RestISPublisher extends ISPublisher { for(String pluginName : availablePlugins.keySet()) { - UUID uuid = PluginManager.getPluginUUID(pluginName); + Plugin plugin = PluginManager.getPlugin(pluginName); + UUID pluginUUID = PluginManager.getPluginUUID(pluginName); RunningPlugin runningPlugin; try { - runningPlugin = resourceRegistryClient.getInstance(RunningPlugin.class, uuid); + runningPlugin = resourceRegistryClient.getInstance(RunningPlugin.class, pluginUUID); } catch (NotFoundException e) { - runningPlugin = publishRunningPluginWithRelations(pluginName); + runningPlugin = publishRunningPluginWithRelations(plugin, pluginUUID); } catch (AvailableInAnotherContextException e) { runningPlugin = new RunningPluginImpl(); - runningPlugin.setHeader(new HeaderImpl(uuid)); + runningPlugin.setHeader(new HeaderImpl(pluginUUID)); resourceRegistryPublisher.addToContext(contextUUID, runningPlugin); } catch (ResourceRegistryException e) { throw e; @@ -128,8 +133,16 @@ public class RestISPublisher extends ISPublisher { } @Override - public void unpublishPlugins() { - logger.info("The Plugin will be removed when the Eservice will be removed thanks to propagation contraints. Nothing to do"); + public void unpublishPlugins(boolean force) throws Exception { + if(force) { + List runningPlugins = resourceRegistryClient.getRelatedResourcesFromReferenceResource(RunningPlugin.class, Uses.class, EService.class, this.eServiceUUID, Direction.IN, true); + for(RunningPlugin runningPlugin : runningPlugins) { + resourceRegistryPublisher.delete(runningPlugin); + } + }else { + logger.info("The Plugin will be removed when the Eservice will be removed thanks to propagation contraints. Nothing to do"); + } + } } From cadd9ec02dac044c8056bbd462edfe90d3a64694 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 1 Oct 2020 15:38:48 +0200 Subject: [PATCH 15/33] Fixing add to context --- .../vremanagement/executor/ispublisher/RestISPublisher.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java b/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java index a8913c4..1117aea 100644 --- a/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java +++ b/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java @@ -108,8 +108,6 @@ class RestISPublisher extends ISPublisher { @Override public void publishPlugins(Map> availablePlugins) throws Exception { - UUID contextUUID = resourceRegistryClient.getCurrentContext().getHeader().getUUID(); - for(String pluginName : availablePlugins.keySet()) { Plugin plugin = PluginManager.getPlugin(pluginName); @@ -124,7 +122,7 @@ class RestISPublisher extends ISPublisher { } catch (AvailableInAnotherContextException e) { runningPlugin = new RunningPluginImpl(); runningPlugin.setHeader(new HeaderImpl(pluginUUID)); - resourceRegistryPublisher.addToContext(contextUUID, runningPlugin); + resourceRegistryPublisher.addToCurrentContext(runningPlugin); } catch (ResourceRegistryException e) { throw e; } From 65a307eea8a975b82e8c176d703c0a83eb9a1370 Mon Sep 17 00:00:00 2001 From: lucafrosini Date: Wed, 7 Oct 2020 14:08:31 +0200 Subject: [PATCH 16/33] added test dependency --- pom.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pom.xml b/pom.xml index 4d053f4..6c3e923 100644 --- a/pom.xml +++ b/pom.xml @@ -133,6 +133,11 @@ 4.11 test + + ch.qos.logback + logback-classic + test + org.gcube.vremanagement hello-world-se-plugin From b638aef34d54a1a2fca455aeaa9fe0551b100edb Mon Sep 17 00:00:00 2001 From: lucafrosini Date: Wed, 7 Oct 2020 14:50:31 +0200 Subject: [PATCH 17/33] Removed unneeded test dependency --- pom.xml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pom.xml b/pom.xml index 6c3e923..4d053f4 100644 --- a/pom.xml +++ b/pom.xml @@ -133,11 +133,6 @@ 4.11 test - - ch.qos.logback - logback-classic - test - org.gcube.vremanagement hello-world-se-plugin From 5d66a3ea09b36707d77f8249b66b78bf007126c6 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Tue, 13 Oct 2020 15:33:26 +0200 Subject: [PATCH 18/33] Fixing service start --- pom.xml | 2 -- .../executor/SmartExecutorInitializator.java | 30 ++++++++++--------- .../ispublisher/GCoreISPublisher.java | 10 ++++--- .../executor/ispublisher/ISPublisher.java | 4 +-- .../executor/ispublisher/RestISPublisher.java | 12 ++++---- 5 files changed, 31 insertions(+), 27 deletions(-) diff --git a/pom.xml b/pom.xml index 4d053f4..7e06f27 100644 --- a/pom.xml +++ b/pom.xml @@ -95,13 +95,11 @@ org.gcube.vremanagement smart-executor-api [3.0.0-SNAPSHOT, 4.0.0-SNAPSHOT) - provided org.gcube.vremanagement smart-executor-client [3.0.0-SNAPSHOT, 4.0.0-SNAPSHOT) - provided diff --git a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java index c62a8ab..a509b27 100644 --- a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java +++ b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java @@ -40,7 +40,7 @@ public class SmartExecutorInitializator implements ApplicationManager { * Furthermore create/connect to DB */ @Override - public void onInit() { + public synchronized void onInit() { String scope = ContextUtility.getCurrentScope(); logger.trace( @@ -54,20 +54,22 @@ public class SmartExecutorInitializator implements ApplicationManager { Map> availablePlugins = PluginManager.getAvailablePlugins(); ApplicationContext applicationContext = ContextProvider.get(); - try { - List isPublishers = ISPublisher.getISPublishers(applicationContext); - for(ISPublisher isPublisher : isPublishers) { - try { - isPublisher.unpublishPlugins(true); - }catch (Exception e) { - logger.error("unable to unpublish plugind from IS using {}", isPublisher.getClass().getName()); - } - isPublisher.publishPlugins(availablePlugins); + List isPublishers = ISPublisher.getISPublishers(applicationContext); + for(ISPublisher isPublisher : isPublishers) { + try { + isPublisher.unpublishPlugins(true); + }catch (Exception e) { + logger.error("unable to unpublish plugind from IS using {}. Trying to continue.", isPublisher.getClass().getName()); } - } catch (Exception e) { - logger.error("Unable to Create ServiceEndpoint for scope {}. The Service will be aborted", scope, e); - throw new RuntimeException(e); + try { + isPublisher.publishPlugins(availablePlugins); + }catch (Exception e) { + logger.error("Unable to Create ServiceEndpoint for scope {}. The Service will be aborted", scope, e); + throw new RuntimeException(e); + } + } + final SmartExecutorPersistenceConnector smartExecutorPersistenceConnector; try { @@ -158,7 +160,7 @@ public class SmartExecutorInitializator implements ApplicationManager { * Furthermore close the connection to DB. */ @Override - public void onShutdown(){ + public synchronized void onShutdown(){ logger.trace( diff --git a/src/main/java/org/gcube/vremanagement/executor/ispublisher/GCoreISPublisher.java b/src/main/java/org/gcube/vremanagement/executor/ispublisher/GCoreISPublisher.java index 63899f5..9c76055 100644 --- a/src/main/java/org/gcube/vremanagement/executor/ispublisher/GCoreISPublisher.java +++ b/src/main/java/org/gcube/vremanagement/executor/ispublisher/GCoreISPublisher.java @@ -180,10 +180,12 @@ class GCoreISPublisher extends ISPublisher { properties.add(propertyVersionElement); Map pluginCapabilities = pluginDeclaration.getSupportedCapabilities(); - for(String capabilityName : pluginCapabilities.keySet()) { - Property propertyElement = new Property(); - propertyElement.nameAndValue(capabilityName, pluginCapabilities.get(capabilityName)); - properties.add(propertyElement); + if(pluginCapabilities!=null) { + for(String capabilityName : pluginCapabilities.keySet()) { + Property propertyElement = new Property(); + propertyElement.nameAndValue(capabilityName, pluginCapabilities.get(capabilityName)); + properties.add(propertyElement); + } } accessPoints.add(accessPointElement); } diff --git a/src/main/java/org/gcube/vremanagement/executor/ispublisher/ISPublisher.java b/src/main/java/org/gcube/vremanagement/executor/ispublisher/ISPublisher.java index 8553398..5478312 100644 --- a/src/main/java/org/gcube/vremanagement/executor/ispublisher/ISPublisher.java +++ b/src/main/java/org/gcube/vremanagement/executor/ispublisher/ISPublisher.java @@ -11,9 +11,9 @@ public abstract class ISPublisher { protected static List isPublishers; - public static List getISPublishers(ApplicationContext applicationContext){ + public synchronized static List getISPublishers(ApplicationContext applicationContext){ if(isPublishers==null) { - List isPublishers = new ArrayList<>(); + isPublishers = new ArrayList<>(); isPublishers.add(new GCoreISPublisher(applicationContext)); isPublishers.add(new RestISPublisher(applicationContext)); } diff --git a/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java b/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java index 1117aea..7456053 100644 --- a/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java +++ b/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java @@ -68,11 +68,13 @@ class RestISPublisher extends ISPublisher { runningPlugin.addFacet(softwareFacet); Map pluginCapabilities = plugin.getSupportedCapabilities(); - for(String capabilityName : pluginCapabilities.keySet()) { - SimplePropertyFacet simplePropertyFacet = new SimplePropertyFacetImpl(); - simplePropertyFacet.setName(capabilityName); - simplePropertyFacet.setValue(pluginCapabilities.get(capabilityName)); - runningPlugin.addFacet(simplePropertyFacet); + if(pluginCapabilities!=null) { + for(String capabilityName : pluginCapabilities.keySet()) { + SimplePropertyFacet simplePropertyFacet = new SimplePropertyFacetImpl(); + simplePropertyFacet.setName(capabilityName); + simplePropertyFacet.setValue(pluginCapabilities.get(capabilityName)); + runningPlugin.addFacet(simplePropertyFacet); + } } EService smartExecutorEService = resourceRegistryClient.getInstance(EService.class, eServiceUUID); From fd30b3dd0d517fef79a7876dd0c2d6f6479d8b31 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 14 Oct 2020 11:25:52 +0200 Subject: [PATCH 19/33] Plugin manager is now used as singleton --- .../executor/SmartExecutorInitializator.java | 10 ++++++---- .../executor/ispublisher/GCoreISPublisher.java | 3 ++- .../executor/ispublisher/RestISPublisher.java | 5 +++-- .../executor/pluginmanager/PluginManager.java | 12 ++++++------ .../executor/rest/RestSmartExecutor.java | 8 +++++--- .../executor/scheduler/SmartExecutorScheduler.java | 3 ++- .../executor/scheduler/SmartExecutorTask.java | 3 ++- .../vremanagement/executor/SerializationTest.java | 5 +++-- .../executor/SmartExecutorImplTest.java | 3 ++- .../executor/SmartExecutorInizializatorTest.java | 3 ++- .../executor/pluginmanager/PluginManagerTest.java | 5 +++-- 11 files changed, 36 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java index a509b27..40169bf 100644 --- a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java +++ b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java @@ -40,7 +40,7 @@ public class SmartExecutorInitializator implements ApplicationManager { * Furthermore create/connect to DB */ @Override - public synchronized void onInit() { + public void onInit() { String scope = ContextUtility.getCurrentScope(); logger.trace( @@ -51,7 +51,9 @@ public class SmartExecutorInitializator implements ApplicationManager { logger.debug("Getting Available Plugins and their own supported capabilities"); - Map> availablePlugins = PluginManager.getAvailablePlugins(); + PluginManager pluginManager = PluginManager.getInstance(); + + Map> availablePlugins = pluginManager.getAvailablePlugins(); ApplicationContext applicationContext = ContextProvider.get(); List isPublishers = ISPublisher.getISPublishers(applicationContext); @@ -86,7 +88,7 @@ public class SmartExecutorInitializator implements ApplicationManager { try { logger.debug("Going to get Orphan Scheduled Tasks in scope {}", scope); - List scheduledTasks = smartExecutorPersistenceConnector.getOrphanScheduledTasks(PluginManager.getAvailablePlugins().keySet()); + List scheduledTasks = smartExecutorPersistenceConnector.getOrphanScheduledTasks(pluginManager.getAvailablePlugins().keySet()); if(scheduledTasks.size()==0){ logger.debug("No Orphan Scheduled Tasks this instance can take in charge in scope {}", scope); } @@ -160,7 +162,7 @@ public class SmartExecutorInitializator implements ApplicationManager { * Furthermore close the connection to DB. */ @Override - public synchronized void onShutdown(){ + public void onShutdown(){ logger.trace( diff --git a/src/main/java/org/gcube/vremanagement/executor/ispublisher/GCoreISPublisher.java b/src/main/java/org/gcube/vremanagement/executor/ispublisher/GCoreISPublisher.java index 9c76055..7491077 100644 --- a/src/main/java/org/gcube/vremanagement/executor/ispublisher/GCoreISPublisher.java +++ b/src/main/java/org/gcube/vremanagement/executor/ispublisher/GCoreISPublisher.java @@ -165,12 +165,13 @@ class GCoreISPublisher extends ISPublisher { Group accessPoints = profile.accessPoints(); + PluginManager pluginManager = PluginManager.getInstance(); for(String pluginName : availablePlugins.keySet()) { AccessPoint accessPointElement = new AccessPoint(); accessPointElement.name(pluginName); - Plugin pluginDeclaration = PluginManager.getPlugin(pluginName); + Plugin pluginDeclaration = pluginManager.getPlugin(pluginName); accessPointElement.description(pluginDeclaration.getDescription()); diff --git a/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java b/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java index 7456053..1806424 100644 --- a/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java +++ b/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java @@ -109,11 +109,12 @@ class RestISPublisher extends ISPublisher { @Override public void publishPlugins(Map> availablePlugins) throws Exception { + PluginManager pluginManager = PluginManager.getInstance(); for(String pluginName : availablePlugins.keySet()) { - Plugin plugin = PluginManager.getPlugin(pluginName); - UUID pluginUUID = PluginManager.getPluginUUID(pluginName); + Plugin plugin = pluginManager.getPlugin(pluginName); + UUID pluginUUID = pluginManager.getPluginUUID(pluginName); RunningPlugin runningPlugin; diff --git a/src/main/java/org/gcube/vremanagement/executor/pluginmanager/PluginManager.java b/src/main/java/org/gcube/vremanagement/executor/pluginmanager/PluginManager.java index f109c97..8c957ed 100644 --- a/src/main/java/org/gcube/vremanagement/executor/pluginmanager/PluginManager.java +++ b/src/main/java/org/gcube/vremanagement/executor/pluginmanager/PluginManager.java @@ -43,7 +43,7 @@ public class PluginManager { * so it is created. Otherwise the already created instance is returned * @return singleton instance of {@link #PluginManager} */ - private static PluginManager getInstance(){ + public synchronized static PluginManager getInstance(){ if(pluginManager== null){ pluginManager = new PluginManager(); } @@ -77,7 +77,7 @@ public class PluginManager { * @return The plugin declaration if available, null otherwise * @throws PluginNotFoundException */ - public static Plugin getPlugin(String pluginName) throws PluginNotFoundException, ExecutorException { + public Plugin getPlugin(String pluginName) throws PluginNotFoundException, ExecutorException { Class pluginClass = getAvailablePlugins().get(pluginName); if (pluginClass== null) { throw new PluginNotFoundException("Plugin " + pluginName + " not available in this smart-executor instance"); @@ -90,15 +90,15 @@ public class PluginManager { } - public static UUID getPluginUUID(String pluginName) { - return PluginManager.getInstance().uuids.get(pluginName); + public UUID getPluginUUID(String pluginName) { + return uuids.get(pluginName); } /** * @return the availablePlugins */ - public static Map> getAvailablePlugins() { - return PluginManager.getInstance().availablePlugins; + public Map> getAvailablePlugins() { + return availablePlugins; } diff --git a/src/main/java/org/gcube/vremanagement/executor/rest/RestSmartExecutor.java b/src/main/java/org/gcube/vremanagement/executor/rest/RestSmartExecutor.java index dc048e0..0a5dcd9 100644 --- a/src/main/java/org/gcube/vremanagement/executor/rest/RestSmartExecutor.java +++ b/src/main/java/org/gcube/vremanagement/executor/rest/RestSmartExecutor.java @@ -77,10 +77,11 @@ public class RestSmartExecutor { //setCalledMethod(HttpMethod.GET + " /" + RestConstants.PLUGINS_PATH_PART); setCalledMethod("getAvailablePlugins"); try { - Map> availablePlugins = PluginManager.getAvailablePlugins(); + PluginManager pluginManager = PluginManager.getInstance(); + Map> availablePlugins = pluginManager.getAvailablePlugins(); List plugins = new ArrayList<>(); for(String pluginName : availablePlugins.keySet()) { - plugins.add(PluginManager.getPlugin(pluginName)); + plugins.add(pluginManager.getPlugin(pluginName)); } return ExtendedSEMapper.getInstance().marshal(PluginDefinition.class, plugins); }catch (Exception e) { @@ -97,11 +98,12 @@ public class RestSmartExecutor { try { SmartExecutorPersistenceConnector persistenceConnector = SmartExecutorPersistenceFactory .getPersistenceConnector(); + PluginManager pluginManager = PluginManager.getInstance(); List plugins = new ArrayList<>(); if(pluginName.compareTo(RestConstants.ORPHAN_PATH_PARAM)!=0) { - plugins.addAll(PluginManager.getAvailablePlugins().keySet()); + plugins.addAll(pluginManager.getAvailablePlugins().keySet()); }else { // TODO check role } diff --git a/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorScheduler.java b/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorScheduler.java index f9308cb..a15753b 100644 --- a/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorScheduler.java +++ b/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorScheduler.java @@ -194,7 +194,8 @@ public class SmartExecutorScheduler { * instance */ try { - PluginManager.getPlugin(parameter.getPluginName()); + PluginManager pluginManager = PluginManager.getInstance(); + pluginManager.getPlugin(parameter.getPluginName()); } catch (PluginNotFoundException e) { throw e; } catch (ExecutorException e) { diff --git a/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorTask.java b/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorTask.java index 2c56a0c..480632d 100644 --- a/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorTask.java +++ b/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorTask.java @@ -86,8 +86,9 @@ public class SmartExecutorTask implements InterruptableJob { pluginName = launchParameter.getPluginName(); + PluginManager pluginManager = PluginManager.getInstance(); try { - plugin = PluginManager.getPlugin(pluginName); + plugin = pluginManager.getPlugin(pluginName); } catch (Exception e) { throw new JobExecutionException(e); } diff --git a/src/test/java/org/gcube/vremanagement/executor/SerializationTest.java b/src/test/java/org/gcube/vremanagement/executor/SerializationTest.java index e0c023c..3d7e9d2 100644 --- a/src/test/java/org/gcube/vremanagement/executor/SerializationTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/SerializationTest.java @@ -108,10 +108,11 @@ public class SerializationTest extends ContextTest { @Test public void testAvailablePluginMarshalling() throws Exception { - Map> availablePlugins = PluginManager.getAvailablePlugins(); + PluginManager pluginManager = PluginManager.getInstance(); + Map> availablePlugins = pluginManager.getAvailablePlugins(); List plugins = new ArrayList<>(); for(String pluginName : availablePlugins.keySet()) { - plugins.add(PluginManager.getPlugin(pluginName)); + plugins.add(pluginManager.getPlugin(pluginName)); } String list = ExtendedSEMapper.getInstance().marshal(PluginDefinition.class, plugins); logger.debug("Plugins are :\n{}", list); diff --git a/src/test/java/org/gcube/vremanagement/executor/SmartExecutorImplTest.java b/src/test/java/org/gcube/vremanagement/executor/SmartExecutorImplTest.java index 501fbe4..40c2595 100644 --- a/src/test/java/org/gcube/vremanagement/executor/SmartExecutorImplTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/SmartExecutorImplTest.java @@ -24,7 +24,8 @@ public class SmartExecutorImplTest { long sleepTime = 10000; inputs.put(HelloWorldPlugin.SLEEP_TIME, sleepTime); String name = (new HelloWorldPlugin()).getName(); - Plugin plugin = PluginManager.getPlugin(name); + PluginManager pluginManager = PluginManager.getInstance(); + Plugin plugin = pluginManager.getPlugin(name); plugin.launch(inputs); } diff --git a/src/test/java/org/gcube/vremanagement/executor/SmartExecutorInizializatorTest.java b/src/test/java/org/gcube/vremanagement/executor/SmartExecutorInizializatorTest.java index a92aa9c..8d34e7f 100644 --- a/src/test/java/org/gcube/vremanagement/executor/SmartExecutorInizializatorTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/SmartExecutorInizializatorTest.java @@ -30,7 +30,8 @@ public class SmartExecutorInizializatorTest { @Test public void getAvailablePlugin() { - Map> availablePlugins = PluginManager.getAvailablePlugins(); + PluginManager pluginManager = PluginManager.getInstance(); + Map> availablePlugins = pluginManager.getAvailablePlugins(); for(String pluginName : availablePlugins.keySet()) { logger.debug("Plugin {}, Class {}", pluginName, availablePlugins.get(pluginName).getSimpleName()); } diff --git a/src/test/java/org/gcube/vremanagement/executor/pluginmanager/PluginManagerTest.java b/src/test/java/org/gcube/vremanagement/executor/pluginmanager/PluginManagerTest.java index 7310dc9..b32c13d 100644 --- a/src/test/java/org/gcube/vremanagement/executor/pluginmanager/PluginManagerTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/pluginmanager/PluginManagerTest.java @@ -16,9 +16,10 @@ public class PluginManagerTest extends ContextTest { @Test public void getHelloWorldPlugin() throws ExecutorException { - Assert.assertEquals(HelloWorldPlugin.class, PluginManager.getPlugin(new HelloWorldPlugin().getName()).getClass()); + PluginManager pluginManager = PluginManager.getInstance(); + Assert.assertEquals(HelloWorldPlugin.class, pluginManager.getPlugin(new HelloWorldPlugin().getName()).getClass()); try { - PluginManager.getPlugin(UUID.randomUUID().toString()); + pluginManager.getPlugin(UUID.randomUUID().toString()); }catch (PluginNotFoundException e) { // Ok. This is the expected behaviour } From cf094b088ed58f64cfcc7edc5b9371f053f9c1f7 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 14 Oct 2020 14:57:33 +0200 Subject: [PATCH 20/33] Do not stop the service if publishing o new IS fails --- .../vremanagement/executor/SmartExecutorInitializator.java | 7 +++++-- .../executor/ispublisher/GCoreISPublisher.java | 2 +- .../executor/ispublisher/RestISPublisher.java | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java index 40169bf..506a054 100644 --- a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java +++ b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java @@ -8,6 +8,7 @@ import org.gcube.smartgears.ContextProvider; import org.gcube.smartgears.context.application.ApplicationContext; import org.gcube.vremanagement.executor.api.types.LaunchParameter; import org.gcube.vremanagement.executor.ispublisher.ISPublisher; +import org.gcube.vremanagement.executor.ispublisher.RestISPublisher; import org.gcube.vremanagement.executor.json.ExtendedSEMapper; import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceConnector; import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceFactory; @@ -66,8 +67,10 @@ public class SmartExecutorInitializator implements ApplicationManager { try { isPublisher.publishPlugins(availablePlugins); }catch (Exception e) { - logger.error("Unable to Create ServiceEndpoint for scope {}. The Service will be aborted", scope, e); - throw new RuntimeException(e); + logger.error("Unable to create plugins is resources in context {}. The Service will be aborted", scope, e); + if(!(isPublisher instanceof RestISPublisher)) { + throw new RuntimeException(e); + } } } diff --git a/src/main/java/org/gcube/vremanagement/executor/ispublisher/GCoreISPublisher.java b/src/main/java/org/gcube/vremanagement/executor/ispublisher/GCoreISPublisher.java index 7491077..90fb0cf 100644 --- a/src/main/java/org/gcube/vremanagement/executor/ispublisher/GCoreISPublisher.java +++ b/src/main/java/org/gcube/vremanagement/executor/ispublisher/GCoreISPublisher.java @@ -31,7 +31,7 @@ import org.gcube.vremanagement.executor.pluginmanager.PluginManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -class GCoreISPublisher extends ISPublisher { +public class GCoreISPublisher extends ISPublisher { private static Logger logger = LoggerFactory.getLogger(GCoreISPublisher.class); diff --git a/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java b/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java index 1806424..14d0101 100644 --- a/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java +++ b/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java @@ -37,7 +37,7 @@ import org.gcube.vremanagement.executor.pluginmanager.PluginManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -class RestISPublisher extends ISPublisher { +public class RestISPublisher extends ISPublisher { private static Logger logger = LoggerFactory.getLogger(RestISPublisher.class); From 16d41f40d0221be46dcdb45667325dd7401e96ec Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 14 Oct 2020 16:25:46 +0200 Subject: [PATCH 21/33] RunningPlugin has now Software Facet related with IsIdentifiedBy --- .../executor/ispublisher/RestISPublisher.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java b/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java index 14d0101..0fb0196 100644 --- a/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java +++ b/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java @@ -6,6 +6,7 @@ import java.util.UUID; import org.gcube.informationsystem.model.impl.properties.HeaderImpl; import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl; +import org.gcube.informationsystem.model.reference.entities.Facet; import org.gcube.informationsystem.model.reference.entities.Resource; import org.gcube.informationsystem.model.reference.properties.PropagationConstraint; import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.AddConstraint; @@ -21,6 +22,7 @@ import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPu import org.gcube.resourcemanagement.model.impl.entities.facets.SimplePropertyFacetImpl; import org.gcube.resourcemanagement.model.impl.entities.facets.SoftwareFacetImpl; import org.gcube.resourcemanagement.model.impl.entities.resources.RunningPluginImpl; +import org.gcube.resourcemanagement.model.impl.relations.consistsof.IsIdentifiedByImpl; import org.gcube.resourcemanagement.model.impl.relations.isrelatedto.EnablesImpl; import org.gcube.resourcemanagement.model.impl.relations.isrelatedto.UsesImpl; import org.gcube.resourcemanagement.model.reference.entities.facets.SimplePropertyFacet; @@ -29,6 +31,7 @@ import org.gcube.resourcemanagement.model.reference.entities.resources.EService; import org.gcube.resourcemanagement.model.reference.entities.resources.RunningPlugin; import org.gcube.resourcemanagement.model.reference.entities.resources.Service; import org.gcube.resourcemanagement.model.reference.entities.resources.Software; +import org.gcube.resourcemanagement.model.reference.relations.consistsof.IsIdentifiedBy; import org.gcube.resourcemanagement.model.reference.relations.isrelatedto.Enables; import org.gcube.resourcemanagement.model.reference.relations.isrelatedto.Uses; import org.gcube.smartgears.context.application.ApplicationContext; @@ -65,7 +68,9 @@ public class RestISPublisher extends ISPublisher { softwareFacet.setName(plugin.getName()); softwareFacet.setVersion(plugin.getVersion()); softwareFacet.setDescription(plugin.getDescription()); - runningPlugin.addFacet(softwareFacet); + + IsIdentifiedBy identifiedBy = new IsIdentifiedByImpl(runningPlugin, softwareFacet); + runningPlugin.addFacet(identifiedBy); Map pluginCapabilities = plugin.getSupportedCapabilities(); if(pluginCapabilities!=null) { From 7ea388aec13d5504ec9f1f8d960fb4c235d84802 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 14 Oct 2020 18:16:49 +0200 Subject: [PATCH 22/33] improving code --- .../vremanagement/executor/ispublisher/RestISPublisher.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java b/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java index 0fb0196..a6ddd5f 100644 --- a/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java +++ b/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java @@ -69,8 +69,8 @@ public class RestISPublisher extends ISPublisher { softwareFacet.setVersion(plugin.getVersion()); softwareFacet.setDescription(plugin.getDescription()); - IsIdentifiedBy identifiedBy = new IsIdentifiedByImpl(runningPlugin, softwareFacet); - runningPlugin.addFacet(identifiedBy); + IsIdentifiedBy isIdentifiedBy = new IsIdentifiedByImpl(runningPlugin, softwareFacet); + runningPlugin.addFacet(isIdentifiedBy); Map pluginCapabilities = plugin.getSupportedCapabilities(); if(pluginCapabilities!=null) { From 48eeb529310153bd7f9d1363a533e3be688636dc Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 15 Oct 2020 14:03:24 +0200 Subject: [PATCH 23/33] Improving startup code --- .../vremanagement/executor/SmartExecutorInitializator.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java index 506a054..3fdd6e4 100644 --- a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java +++ b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java @@ -67,8 +67,10 @@ public class SmartExecutorInitializator implements ApplicationManager { try { isPublisher.publishPlugins(availablePlugins); }catch (Exception e) { - logger.error("Unable to create plugins is resources in context {}. The Service will be aborted", scope, e); - if(!(isPublisher instanceof RestISPublisher)) { + if(isPublisher instanceof RestISPublisher) { + logger.warn("Unable to create RunningPlugin in context {}. {}", scope, e.getMessage()); + } else { + logger.error("Unable to create ServiceEndpoint in context {}. The Service will be aborted", scope, e); throw new RuntimeException(e); } } From 44b6e6e237fdd1eb4b810dedfd8b4d6150544b1b Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 22 Oct 2020 09:34:42 +0200 Subject: [PATCH 24/33] serviceClass has been set to the same value of groupId --- gcube/extra-resources/WEB-INF/gcube-app.xml | 2 +- pom.xml | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/gcube/extra-resources/WEB-INF/gcube-app.xml b/gcube/extra-resources/WEB-INF/gcube-app.xml index 44b06dc..f77abc3 100644 --- a/gcube/extra-resources/WEB-INF/gcube-app.xml +++ b/gcube/extra-resources/WEB-INF/gcube-app.xml @@ -2,7 +2,7 @@ ${project.artifactId} - ${serviceClass} + ${project.groupId} ${project.version} ${project.description} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 7e06f27..2942803 100644 --- a/pom.xml +++ b/pom.xml @@ -19,7 +19,6 @@ UTF-8 ${project.basedir}${file.separator}src${file.separator}main${file.separator}webapp${file.separator}WEB-INF - VREManagement 3.0.34 From 89b7c3013f1454eed17ed8021e55502c1964706c Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 2 Dec 2020 11:22:48 +0100 Subject: [PATCH 25/33] Switched to gCube Jackson and Moving to new resource-registry --- pom.xml | 2 +- .../executor/ContextUtility.java | 6 +- .../executor/SmartExecutorInitializator.java | 48 +++++------ .../ispublisher/GCoreISPublisher.java | 10 +-- ...SmartExecutorPersistenceConfiguration.java | 2 +- .../SmartExecutorPersistenceFactory.java | 85 +++++++++++-------- .../OrientDBPersistenceConnector.java | 64 +++++++------- .../executor/scheduledtask/ScheduledTask.java | 10 +-- .../scheduler/SmartExecutorScheduler.java | 3 +- .../SmartExecutorSchedulerFactory.java | 24 +++--- .../SmartExecutorInizializatorTest.java | 4 +- ...SmartExecutorPersistenceConnectorTest.java | 4 +- 12 files changed, 141 insertions(+), 121 deletions(-) diff --git a/pom.xml b/pom.xml index 2942803..4857da2 100644 --- a/pom.xml +++ b/pom.xml @@ -33,7 +33,7 @@ org.gcube.distribution gcube-smartgears-bom - 2.0.0-SNAPSHOT + 2.1.0 pom import diff --git a/src/main/java/org/gcube/vremanagement/executor/ContextUtility.java b/src/main/java/org/gcube/vremanagement/executor/ContextUtility.java index 73e79bb..f09b547 100644 --- a/src/main/java/org/gcube/vremanagement/executor/ContextUtility.java +++ b/src/main/java/org/gcube/vremanagement/executor/ContextUtility.java @@ -20,7 +20,7 @@ public class ContextUtility { private static Logger logger = LoggerFactory.getLogger(ContextUtility.class); - public static String getCurrentScope() { + public static String getCurrentContext() { String token = SecurityTokenProvider.instance.get(); AuthorizationEntry authorizationEntry; try { @@ -36,7 +36,7 @@ public class ContextUtility { return context; } - public static String getCurrentScope(String token) throws ObjectNotFound, Exception { + public static String getCurrentContext(String token) throws ObjectNotFound, Exception { AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token); String context = authorizationEntry.getContext(); logger.info("Context of token {} is {}", token, context); @@ -45,7 +45,7 @@ public class ContextUtility { public static void setContext(String token) throws ObjectNotFound, Exception { SecurityTokenProvider.instance.set(token); - ScopeProvider.instance.set(getCurrentScope(token)); + ScopeProvider.instance.set(getCurrentContext(token)); } public static ClientInfo getClientInfo() { diff --git a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java index 3fdd6e4..fee3fcd 100644 --- a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java +++ b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java @@ -42,13 +42,13 @@ public class SmartExecutorInitializator implements ApplicationManager { */ @Override public void onInit() { - String scope = ContextUtility.getCurrentScope(); + String context = ContextUtility.getCurrentContext(); logger.trace( "\n-------------------------------------------------------\n" - + "Smart Executor is Starting on scope {}\n" + + "Smart Executor is Starting on context {}\n" + "-------------------------------------------------------", - scope); + context); logger.debug("Getting Available Plugins and their own supported capabilities"); @@ -68,9 +68,9 @@ public class SmartExecutorInitializator implements ApplicationManager { isPublisher.publishPlugins(availablePlugins); }catch (Exception e) { if(isPublisher instanceof RestISPublisher) { - logger.warn("Unable to create RunningPlugin in context {}. {}", scope, e.getMessage()); + logger.warn("Unable to create RunningPlugin in context {}. {}", context, e.getMessage()); } else { - logger.error("Unable to create ServiceEndpoint in context {}. The Service will be aborted", scope, e); + logger.error("Unable to create ServiceEndpoint in context {}. The Service will be aborted", context, e); throw new RuntimeException(e); } } @@ -83,7 +83,7 @@ public class SmartExecutorInitializator implements ApplicationManager { smartExecutorPersistenceConnector = SmartExecutorPersistenceFactory.getPersistenceConnector(); } catch (Exception e) { logger.error("Unable to instantiate {} for scope {}. The Service will be aborted", - SmartExecutorPersistenceConnector.class.getSimpleName(), scope, e); + SmartExecutorPersistenceConnector.class.getSimpleName(), context, e); throw new RuntimeException(e); } @@ -91,11 +91,11 @@ public class SmartExecutorInitializator implements ApplicationManager { // Persistence to clean previous situation of a failure of HostingNode try { - logger.debug("Going to get Orphan Scheduled Tasks in scope {}", scope); + logger.debug("Going to get Orphan Scheduled Tasks in scope {}", context); List scheduledTasks = smartExecutorPersistenceConnector.getOrphanScheduledTasks(pluginManager.getAvailablePlugins().keySet()); if(scheduledTasks.size()==0){ - logger.debug("No Orphan Scheduled Tasks this instance can take in charge in scope {}", scope); + logger.debug("No Orphan Scheduled Tasks this instance can take in charge in scope {}", context); } for(final ScheduledTask scheduledTask : scheduledTasks){ @@ -106,7 +106,7 @@ public class SmartExecutorInitializator implements ApplicationManager { // Reserving the task. smartExecutorPersistenceConnector.reserveScheduledTask(scheduledTask); }catch (Exception e) { - logger.debug("({}) Someone else is going to take in charge the scheduled task {}. Skipping.", scope, taskAsString); + logger.debug("({}) Someone else is going to take in charge the scheduled task {}. Skipping.", context, taskAsString); continue; } @@ -117,7 +117,7 @@ public class SmartExecutorInitializator implements ApplicationManager { LaunchParameter launchParameter = scheduledTask.getLaunchParameter(); try { - logger.info("({}) Going to schedule an already scheduled task with the following parameters {}", scope, + logger.info("({}) Going to schedule an already scheduled task with the following parameters {}", context, ExtendedSEMapper.getInstance().marshal(launchParameter)); } catch (Exception e1) { @@ -132,11 +132,11 @@ public class SmartExecutorInitializator implements ApplicationManager { smartExecutorPersistenceConnector.removeScheduledTask(scheduledTask); smartExecutorScheduler.schedule(launchParameter, scheduledTask.getUUID()); } catch (Exception e) { - logger.error("({}) Error while trying to relaunch scheduled task.", scope, e); + logger.error("({}) Error while trying to relaunch scheduled task.", context, e); try { smartExecutorPersistenceConnector.addScheduledTask(scheduledTask); } catch (Exception ex) { - logger.error("({}) Unable to add back scheduled task {}", scope, taskAsString); + logger.error("({}) Unable to add back scheduled task {}", context, taskAsString); } } @@ -148,14 +148,14 @@ public class SmartExecutorInitializator implements ApplicationManager { } } catch (Exception e) { - logger.error("Unable to get Orphan Scheduled Tasksfor scope {}.", scope, e); + logger.error("Unable to get Orphan Scheduled Tasksfor scope {}.", context, e); return; } logger.trace( "\n-------------------------------------------------------\n" - + "Smart Executor Started Successfully on scope {}\n" - + "-------------------------------------------------------", scope); + + "Smart Executor Started Successfully on context {}\n" + + "-------------------------------------------------------", context); } @@ -172,16 +172,16 @@ public class SmartExecutorInitializator implements ApplicationManager { logger.trace( "\n-------------------------------------------------------\n" - + "Smart Executor is Stopping on scope {}\n" + + "Smart Executor is Stopping on context {}\n" + "-------------------------------------------------------", - ContextUtility.getCurrentScope()); + ContextUtility.getCurrentContext()); SmartExecutorScheduler scheduler; try { scheduler = SmartExecutorSchedulerFactory.getSmartExecutorScheduler(); scheduler.stopAll(); - SmartExecutorSchedulerFactory.remove(); + SmartExecutorSchedulerFactory.removeCurrentSmartExecutorScheduler(); } catch (SchedulerException e) { logger.error("", e); } @@ -192,22 +192,22 @@ public class SmartExecutorInitializator implements ApplicationManager { try { isPublisher.unpublishPlugins(false); }catch (Exception e) { - logger.error("unable to unpublish plugind from IS using {}", isPublisher.getClass().getName()); + logger.error("unable to unpublish plugins from IS using {}", isPublisher.getClass().getName()); } } try { - SmartExecutorPersistenceFactory.closePersistenceConnector(); + SmartExecutorPersistenceFactory.closeCurrentPersistenceConnector(); } catch (Throwable e) { - logger.error("Unable to correctly close {} for scope {}", + logger.error("Unable to correctly close {} for context {}", SmartExecutorPersistenceConnector.class.getSimpleName(), - ContextUtility.getCurrentScope(), e); + ContextUtility.getCurrentContext(), e); } logger.trace( "\n-------------------------------------------------------\n" - + "Smart Executor Stopped Successfully on scope {}\n" + + "Smart Executor Stopped Successfully on context {}\n" + "-------------------------------------------------------", - ContextUtility.getCurrentScope()); + ContextUtility.getCurrentContext()); } } diff --git a/src/main/java/org/gcube/vremanagement/executor/ispublisher/GCoreISPublisher.java b/src/main/java/org/gcube/vremanagement/executor/ispublisher/GCoreISPublisher.java index 90fb0cf..9ef9865 100644 --- a/src/main/java/org/gcube/vremanagement/executor/ispublisher/GCoreISPublisher.java +++ b/src/main/java/org/gcube/vremanagement/executor/ispublisher/GCoreISPublisher.java @@ -53,7 +53,7 @@ public class GCoreISPublisher extends ISPublisher { RegistryPublisher registryPublisher = RegistryPublisherFactory.create(); try { - logger.debug("Trying to publish to {}:\n{}", ContextUtility.getCurrentScope(), stringWriter); + logger.debug("Trying to publish to {}:\n{}", ContextUtility.getCurrentContext(), stringWriter); registryPublisher.create(resource); } catch(Exception e) { logger.error("The resource was not published", e); @@ -75,7 +75,7 @@ public class GCoreISPublisher extends ISPublisher { String id = resource.id(); logger.debug("Trying to remove {} with ID {} from {}", resource.getClass().getSimpleName(), id, - ContextUtility.getCurrentScope()); + ContextUtility.getCurrentContext()); registryPublisher.remove(resource); @@ -218,11 +218,11 @@ public class GCoreISPublisher extends ISPublisher { for(ServiceEndpoint serviceEndpoint : serviceEndpoints) { try { logger.debug("Trying to unpublish the old ServiceEndpoint with ID {} from scope {}", - serviceEndpoint.id(), ContextUtility.getCurrentScope()); + serviceEndpoint.id(), ContextUtility.getCurrentContext()); unPublishResource(serviceEndpoint); } catch(Exception e) { logger.debug("Exception tryng to unpublish the old ServiceEndpoint with ID {} from scope {}", - serviceEndpoint.id(), ContextUtility.getCurrentScope(), e); + serviceEndpoint.id(), ContextUtility.getCurrentContext(), e); } } } catch(Exception e) { @@ -238,7 +238,7 @@ public class GCoreISPublisher extends ISPublisher { publishResource(serviceEndpoint); } catch(Exception e) { logger.error("Unable to Create ServiceEndpoint for scope {}. The Service will be aborted", - ContextUtility.getCurrentScope(), e); + ContextUtility.getCurrentContext(), e); throw new RuntimeException(e); } } diff --git a/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConfiguration.java b/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConfiguration.java index 7da12bd..a63ce1c 100644 --- a/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConfiguration.java +++ b/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConfiguration.java @@ -131,7 +131,7 @@ public class SmartExecutorPersistenceConfiguration { List serviceEndpoints = client.submit(query); if(serviceEndpoints.size()>1){ query.addCondition(String.format("$resource/Profile/AccessPoint/Properties/Property/Name/text() eq '%s'", TARGET_SCOPE)); - query.addCondition(String.format("$resource/Profile/AccessPoint/Properties/Property/Value/text() eq '%s'", ContextUtility.getCurrentScope())); + query.addCondition(String.format("$resource/Profile/AccessPoint/Properties/Property/Value/text() eq '%s'", ContextUtility.getCurrentContext())); serviceEndpoints = client.submit(query); } return serviceEndpoints.get(0); diff --git a/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceFactory.java b/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceFactory.java index b95f03e..82a3a99 100644 --- a/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceFactory.java +++ b/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceFactory.java @@ -18,58 +18,75 @@ import org.slf4j.LoggerFactory; public abstract class SmartExecutorPersistenceFactory { private static final Logger logger = LoggerFactory.getLogger(SmartExecutorPersistenceFactory.class); - + private static Map persistenceConnectors; - + static { persistenceConnectors = new HashMap(); } - - private static SmartExecutorPersistenceConnector getPersistenceConnector(String scope) throws Exception { - if(scope==null){ - String error = "No Scope available."; + + private static synchronized SmartExecutorPersistenceConnector getPersistenceConnector(String context) throws Exception { + if (context == null) { + String error = "No Context available."; logger.error(error); - throw new RuntimeException(error); + throw new RuntimeException(error); } - logger.trace("Retrieving {} for scope {}", - SmartExecutorPersistenceConnector.class.getSimpleName(), scope); - - SmartExecutorPersistenceConnector persistence = persistenceConnectors.get(scope); - - if(persistence==null){ - logger.trace("Retrieving {} for scope {} not found on internal {}. Intializing it.", - SmartExecutorPersistenceConnector.class.getSimpleName(), - scope, Map.class.getSimpleName()); - + logger.trace("Retrieving {} for context {}", SmartExecutorPersistenceConnector.class.getSimpleName(), context); + + SmartExecutorPersistenceConnector persistence = persistenceConnectors.get(context); + + if (persistence == null) { + logger.trace("Retrieving {} for context {} not found on internal {}. Intializing it.", + SmartExecutorPersistenceConnector.class.getSimpleName(), context, Map.class.getSimpleName()); + String className = OrientDBPersistenceConnector.class.getSimpleName(); - SmartExecutorPersistenceConfiguration configuration = - new SmartExecutorPersistenceConfiguration(className); - + SmartExecutorPersistenceConfiguration configuration = new SmartExecutorPersistenceConfiguration(className); + persistence = new OrientDBPersistenceConnector(configuration); - persistenceConnectors.put(ContextUtility.getCurrentScope(), - persistence); + persistenceConnectors.put(ContextUtility.getCurrentContext(), persistence); } - + return persistence; } - + /** * @return the persistenceConnector */ - public static synchronized SmartExecutorPersistenceConnector getPersistenceConnector() throws Exception { - String scope = ContextUtility.getCurrentScope(); - return getPersistenceConnector(scope); + public static SmartExecutorPersistenceConnector getPersistenceConnector() throws Exception { + String context = ContextUtility.getCurrentContext(); + return getPersistenceConnector(context); } - - public static synchronized void closePersistenceConnector() throws Exception { - String scope = ContextUtility.getCurrentScope(); - SmartExecutorPersistenceConnector persistence = - getPersistenceConnector(scope); - if(persistence!=null){ + + public static void closeCurrentPersistenceConnector() throws Exception { + String context = ContextUtility.getCurrentContext(); + closePersistenceConnector(context); + } + + private static synchronized void closePersistenceConnector(String context) throws Exception { + SmartExecutorPersistenceConnector persistence = getPersistenceConnector(context); + if (persistence != null) { persistence.close(); - persistenceConnectors.remove(scope); + persistenceConnectors.remove(context); + if(persistenceConnectors.isEmpty()) { + + } } } + public static void closeAll() { + for (String context : persistenceConnectors.keySet()) { + try { + closePersistenceConnector(context); + } catch (Exception e) { + logger.error("Unable to close {} for context {}", + SmartExecutorPersistenceConnector.class.getSimpleName(), context); + } + } + } + + public static void shutdown() { + OrientDBPersistenceConnector.shutdown(); + } + } diff --git a/src/main/java/org/gcube/vremanagement/executor/persistence/orientdb/OrientDBPersistenceConnector.java b/src/main/java/org/gcube/vremanagement/executor/persistence/orientdb/OrientDBPersistenceConnector.java index 8f76ec0..42d4474 100644 --- a/src/main/java/org/gcube/vremanagement/executor/persistence/orientdb/OrientDBPersistenceConnector.java +++ b/src/main/java/org/gcube/vremanagement/executor/persistence/orientdb/OrientDBPersistenceConnector.java @@ -24,6 +24,7 @@ import org.gcube.vremanagement.executor.scheduledtask.ScheduledTask; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.orientechnologies.orient.core.Orient; import com.orientechnologies.orient.core.db.ODatabasePool; import com.orientechnologies.orient.core.db.ODatabaseSession; import com.orientechnologies.orient.core.record.impl.ODocument; @@ -37,10 +38,10 @@ public class OrientDBPersistenceConnector extends SmartExecutorPersistenceConnec private static final Logger logger = LoggerFactory.getLogger(OrientDBPersistenceConnector.class); - protected final String SCOPE = "scope"; - protected final String UUID = "uuid"; - protected final String ITERATION = "iteration"; - protected final String TIMESTAMP = "timestamp"; + protected final static String CONTEXT = "context"; + protected final static String UUID = "uuid"; + protected final static String ITERATION = "iteration"; + protected final static String TIMESTAMP = "timestamp"; protected final String RUN_ON = "runOn"; @@ -52,6 +53,8 @@ public class OrientDBPersistenceConnector extends SmartExecutorPersistenceConnec } protected void prepareConnection(SmartExecutorPersistenceConfiguration configuration) throws Exception { + Orient.instance().removeShutdownHook(); + logger.debug("Preparing Connection for {}", this.getClass().getSimpleName()); String url = configuration.getURL(); String username = configuration.getUsername(); @@ -63,6 +66,7 @@ public class OrientDBPersistenceConnector extends SmartExecutorPersistenceConnec @Override public void close() throws Exception { oDatabasePool.close(); + oDatabasePool = null; } @Override @@ -74,13 +78,13 @@ public class OrientDBPersistenceConnector extends SmartExecutorPersistenceConnec String type = PluginStateEvolution.class.getSimpleName(); Map params = new HashMap(); params.put(UUID, uuid.toString()); - params.put(SCOPE, ContextUtility.getCurrentScope()); + params.put(CONTEXT, ContextUtility.getCurrentContext()); OSQLSynchQuery query = null; if(iterationNumber != null && iterationNumber > 0) { query = new OSQLSynchQuery(String.format( - "SELECT FROM %s WHERE %s = :%s AND %s = :%s AND %s = :%s ORDER BY %s DESC LIMIT 1", type, SCOPE, - SCOPE, UUID, UUID, ITERATION, ITERATION, TIMESTAMP)); + "SELECT FROM %s WHERE %s = :%s AND %s = :%s AND %s = :%s ORDER BY %s DESC LIMIT 1", type, CONTEXT, + CONTEXT, UUID, UUID, ITERATION, ITERATION, TIMESTAMP)); params.put(ITERATION, iterationNumber); } else { /* @@ -91,7 +95,7 @@ public class OrientDBPersistenceConnector extends SmartExecutorPersistenceConnec */ query = new OSQLSynchQuery( String.format("SELECT FROM %s WHERE %s = :%s AND %s = :%s ORDER BY %s DESC, %s DESC LIMIT 1", - type, SCOPE, SCOPE, UUID, UUID, ITERATION, TIMESTAMP)); + type, CONTEXT, CONTEXT, UUID, UUID, ITERATION, TIMESTAMP)); } List result = query.execute(params); @@ -123,9 +127,7 @@ public class OrientDBPersistenceConnector extends SmartExecutorPersistenceConnec } catch(Exception e) { throw new PluginInstanceNotFoundException(uuid); } finally { - if(oDatabaseSession != null) { - oDatabaseSession.close(); - } + closeDatabaseSession(oDatabaseSession); } } @@ -137,7 +139,7 @@ public class OrientDBPersistenceConnector extends SmartExecutorPersistenceConnec ODocument doc = new ODocument(PluginStateEvolution.class.getSimpleName()); String json = ExtendedSEMapper.getInstance().marshal(pluginStateEvolution); doc.fromJSON(json); - doc.field(SCOPE, ContextUtility.getCurrentScope()); + doc.field(CONTEXT, ContextUtility.getCurrentContext()); doc.save(); oDatabaseSession.commit(); @@ -147,9 +149,7 @@ public class OrientDBPersistenceConnector extends SmartExecutorPersistenceConnec } throw e; } finally { - if(oDatabaseSession != null) { - oDatabaseSession.close(); - } + closeDatabaseSession(oDatabaseSession); } } @@ -175,9 +175,7 @@ public class OrientDBPersistenceConnector extends SmartExecutorPersistenceConnec } throw new SchedulePersistenceException(e); } finally { - if(oDatabaseSession != null) { - oDatabaseSession.close(); - } + closeDatabaseSession(oDatabaseSession); } } @@ -191,7 +189,7 @@ public class OrientDBPersistenceConnector extends SmartExecutorPersistenceConnec String type = ScheduledTask.class.getSimpleName(); String queryString = String.format("SELECT * FROM %s WHERE %s = '%s'", type, "scope", - ContextUtility.getCurrentScope()); + ContextUtility.getCurrentContext()); if(plugins != null && plugins.size() != 0) { boolean first = true; for(String pluginName : plugins) { @@ -233,9 +231,7 @@ public class OrientDBPersistenceConnector extends SmartExecutorPersistenceConnec } catch(Exception e) { throw new SchedulePersistenceException(e); } finally { - if(oDatabaseSession != null) { - oDatabaseSession.close(); - } + closeDatabaseSession(oDatabaseSession); } } @@ -278,9 +274,7 @@ public class OrientDBPersistenceConnector extends SmartExecutorPersistenceConnec } catch(Exception e) { throw new SchedulePersistenceException(e); } finally { - if(oDatabaseSession != null) { - oDatabaseSession.close(); - } + closeDatabaseSession(oDatabaseSession); } } @@ -303,9 +297,7 @@ public class OrientDBPersistenceConnector extends SmartExecutorPersistenceConnec } throw new SchedulePersistenceException(e); } finally { - if(oDatabaseSession != null) { - oDatabaseSession.close(); - } + closeDatabaseSession(oDatabaseSession); } } @@ -328,9 +320,14 @@ public class OrientDBPersistenceConnector extends SmartExecutorPersistenceConnec } throw new SchedulePersistenceException(e); } finally { - if(oDatabaseSession != null) { - oDatabaseSession.close(); - } + closeDatabaseSession(oDatabaseSession); + } + } + + private void closeDatabaseSession(ODatabaseSession oDatabaseSession) { + if(oDatabaseSession != null) { + oDatabaseSession.close(); + oDatabaseSession = null; } } @@ -338,4 +335,9 @@ public class OrientDBPersistenceConnector extends SmartExecutorPersistenceConnec public void releaseScheduledTask(ScheduledTask scheduledTask) throws SchedulePersistenceException { releaseScheduledTask(scheduledTask.getUUID()); } + + public static void shutdown(){ + //Orient.OShutdownOrientDBInstancesHandler. + Orient.instance().shutdown(); + } } diff --git a/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTask.java b/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTask.java index fefadf6..ca28ea9 100644 --- a/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTask.java +++ b/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTask.java @@ -28,7 +28,7 @@ import org.gcube.vremanagement.executor.plugin.RunOn; public class ScheduledTask extends org.gcube.vremanagement.executor.plugin.ScheduledTask { protected String token; - protected String scope; + protected String context; protected ClientInfo clientInfo; protected ScheduledTask() { @@ -42,7 +42,7 @@ public class ScheduledTask extends org.gcube.vremanagement.executor.plugin.Sched this.uuid = uuid; this.launchParameter = launchParameter; this.token = SecurityTokenProvider.instance.get(); - this.scope = ContextUtility.getCurrentScope(); + this.context = ContextUtility.getCurrentContext(); this.clientInfo = ContextUtility.getClientInfo(); this.runOn = runOn; } @@ -57,8 +57,8 @@ public class ScheduledTask extends org.gcube.vremanagement.executor.plugin.Sched /** * @return the scope */ - public String getScope() { - return scope; + public String getContext() { + return context; } /** @@ -109,7 +109,7 @@ public class ScheduledTask extends org.gcube.vremanagement.executor.plugin.Sched try { return ExtendedSEMapper.getInstance().marshal(this); } catch(Exception e) { - return "ScheduledTask [token=" + token + ", scope=" + scope + ", clientInfo=" + clientInfo + ", uuid=" + return "ScheduledTask [token=" + token + ", context=" + context + ", clientInfo=" + clientInfo + ", uuid=" + uuid + ", launchParameter=" + launchParameter + ", runOn=" + runOn + "]"; } } diff --git a/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorScheduler.java b/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorScheduler.java index a15753b..9a5e213 100644 --- a/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorScheduler.java +++ b/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorScheduler.java @@ -49,7 +49,7 @@ public class SmartExecutorScheduler { private static Logger logger = LoggerFactory.getLogger(SmartExecutorScheduler.class); protected Set scheduledJobs; - protected final Scheduler scheduler; + protected Scheduler scheduler; SmartExecutorScheduler(Scheduler scheduler) throws SchedulerException { this.scheduler = scheduler; @@ -319,6 +319,7 @@ public class SmartExecutorScheduler { } scheduler.clear(); scheduler.shutdown(); + scheduler = null; } } diff --git a/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorSchedulerFactory.java b/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorSchedulerFactory.java index 7628526..914b829 100644 --- a/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorSchedulerFactory.java +++ b/src/main/java/org/gcube/vremanagement/executor/scheduler/SmartExecutorSchedulerFactory.java @@ -25,27 +25,27 @@ public class SmartExecutorSchedulerFactory { smartExecutorSchedulers = new HashMap<>(); } - private static SmartExecutorScheduler getSmartExecutorScheduler(String scope) throws SchedulerException { - if(scope==null){ - String error = "No Scope available."; + private static SmartExecutorScheduler getSmartExecutorScheduler(String context) throws SchedulerException { + if(context==null){ + String error = "No context available."; logger.error(error); throw new RuntimeException(error); } logger.trace("Retrieving {} for scope {}", - SmartExecutorPersistenceConnector.class.getSimpleName(), scope); + SmartExecutorPersistenceConnector.class.getSimpleName(), context); - SmartExecutorScheduler smartExecutorScheduler = smartExecutorSchedulers.get(scope); + SmartExecutorScheduler smartExecutorScheduler = smartExecutorSchedulers.get(context); if(smartExecutorScheduler==null){ logger.trace("Retrieving {} for scope {} not found on internal {}. Intializing it.", SmartExecutorScheduler.class.getSimpleName(), - scope, Map.class.getSimpleName()); + context, Map.class.getSimpleName()); Scheduler scheduler = schedulerFactory.getScheduler(); smartExecutorScheduler = new SmartExecutorScheduler(scheduler); - smartExecutorSchedulers.put(ContextUtility.getCurrentScope(), + smartExecutorSchedulers.put(ContextUtility.getCurrentContext(), smartExecutorScheduler); } @@ -57,13 +57,13 @@ public class SmartExecutorSchedulerFactory { * @throws SchedulerException */ public static synchronized SmartExecutorScheduler getSmartExecutorScheduler() throws SchedulerException { - String scope = ContextUtility.getCurrentScope(); - return getSmartExecutorScheduler(scope); + String context = ContextUtility.getCurrentContext(); + return getSmartExecutorScheduler(context); } - public static void remove(){ - String scope = ContextUtility.getCurrentScope(); - smartExecutorSchedulers.remove(scope); + public static void removeCurrentSmartExecutorScheduler(){ + String context = ContextUtility.getCurrentContext(); + smartExecutorSchedulers.remove(context); } } diff --git a/src/test/java/org/gcube/vremanagement/executor/SmartExecutorInizializatorTest.java b/src/test/java/org/gcube/vremanagement/executor/SmartExecutorInizializatorTest.java index 8d34e7f..7357142 100644 --- a/src/test/java/org/gcube/vremanagement/executor/SmartExecutorInizializatorTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/SmartExecutorInizializatorTest.java @@ -67,11 +67,11 @@ public class SmartExecutorInizializatorTest { for (ServiceEndpoint serviceEndpoint : serviceEndpoints) { try { logger.debug("Trying to unpublish the old ServiceEndpoint with ID {} ({}) from scope {}", - serviceEndpoint.id(), serviceEndpoint.profile().runtime().hostedOn(), ContextUtility.getCurrentScope()); + serviceEndpoint.id(), serviceEndpoint.profile().runtime().hostedOn(), ContextUtility.getCurrentContext()); // unPublishResource(serviceEndpoint); } catch(Exception e){ logger.debug("Exception tryng to unpublish the old ServiceEndpoint with ID {} ({}) from scope {}", - serviceEndpoint.id(), serviceEndpoint.profile().runtime().hostedOn(), ContextUtility.getCurrentScope(), e); + serviceEndpoint.id(), serviceEndpoint.profile().runtime().hostedOn(), ContextUtility.getCurrentContext(), e); } } }catch(Exception e){ diff --git a/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java b/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java index eb5269e..171299b 100644 --- a/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java @@ -35,7 +35,7 @@ public class SmartExecutorPersistenceConnectorTest extends ContextTest { SmartExecutorPersistenceConnector persistenceConnector = SmartExecutorPersistenceFactory.getPersistenceConnector(); Assert.assertNotNull(persistenceConnector); Assert.assertEquals(OrientDBPersistenceConnector.class, persistenceConnector.getClass()); - SmartExecutorPersistenceFactory.closePersistenceConnector(); + SmartExecutorPersistenceFactory.closeCurrentPersistenceConnector(); } @Test @@ -65,7 +65,7 @@ public class SmartExecutorPersistenceConnectorTest extends ContextTest { PluginState ps = pse.getPluginState(); Assert.assertEquals(states[states.length-1], ps); - SmartExecutorPersistenceFactory.closePersistenceConnector(); + SmartExecutorPersistenceFactory.closeCurrentPersistenceConnector(); } @Test From cb545c0cf03cfc3bc6fca59af20a59aea2c093d7 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 2 Dec 2020 18:11:44 +0100 Subject: [PATCH 26/33] Using Activates IsRelatedTo in place of Uses which was an error --- .../executor/ispublisher/RestISPublisher.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java b/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java index a6ddd5f..8b47fc6 100644 --- a/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java +++ b/src/main/java/org/gcube/vremanagement/executor/ispublisher/RestISPublisher.java @@ -23,8 +23,8 @@ import org.gcube.resourcemanagement.model.impl.entities.facets.SimplePropertyFac import org.gcube.resourcemanagement.model.impl.entities.facets.SoftwareFacetImpl; import org.gcube.resourcemanagement.model.impl.entities.resources.RunningPluginImpl; import org.gcube.resourcemanagement.model.impl.relations.consistsof.IsIdentifiedByImpl; +import org.gcube.resourcemanagement.model.impl.relations.isrelatedto.ActivatesImpl; import org.gcube.resourcemanagement.model.impl.relations.isrelatedto.EnablesImpl; -import org.gcube.resourcemanagement.model.impl.relations.isrelatedto.UsesImpl; import org.gcube.resourcemanagement.model.reference.entities.facets.SimplePropertyFacet; import org.gcube.resourcemanagement.model.reference.entities.facets.SoftwareFacet; import org.gcube.resourcemanagement.model.reference.entities.resources.EService; @@ -32,8 +32,8 @@ import org.gcube.resourcemanagement.model.reference.entities.resources.RunningPl import org.gcube.resourcemanagement.model.reference.entities.resources.Service; import org.gcube.resourcemanagement.model.reference.entities.resources.Software; import org.gcube.resourcemanagement.model.reference.relations.consistsof.IsIdentifiedBy; +import org.gcube.resourcemanagement.model.reference.relations.isrelatedto.Activates; import org.gcube.resourcemanagement.model.reference.relations.isrelatedto.Enables; -import org.gcube.resourcemanagement.model.reference.relations.isrelatedto.Uses; import org.gcube.smartgears.context.application.ApplicationContext; import org.gcube.vremanagement.executor.plugin.Plugin; import org.gcube.vremanagement.executor.pluginmanager.PluginManager; @@ -87,9 +87,9 @@ public class RestISPublisher extends ISPublisher { usesPropagationConstraint.setAddConstraint(AddConstraint.propagate); usesPropagationConstraint.setRemoveConstraint(RemoveConstraint.cascade); - Uses uses = new UsesImpl(smartExecutorEService, runningPlugin, usesPropagationConstraint); + Activates activates = new ActivatesImpl(smartExecutorEService, runningPlugin, usesPropagationConstraint); try { - resourceRegistryPublisher.createIsRelatedTo(uses); + resourceRegistryPublisher.createIsRelatedTo(activates); } catch (ResourceRegistryException e) { logger.error("Unable to publish %s instace %s for plugin %s. I'm going to stop the service.", Resource.NAME, RunningPlugin.NAME, plugin.getName()); throw e; @@ -141,7 +141,7 @@ public class RestISPublisher extends ISPublisher { @Override public void unpublishPlugins(boolean force) throws Exception { if(force) { - List runningPlugins = resourceRegistryClient.getRelatedResourcesFromReferenceResource(RunningPlugin.class, Uses.class, EService.class, this.eServiceUUID, Direction.IN, true); + List runningPlugins = resourceRegistryClient.getRelatedResourcesFromReferenceResource(RunningPlugin.class, Activates.class, EService.class, this.eServiceUUID, Direction.IN, true); for(RunningPlugin runningPlugin : runningPlugins) { resourceRegistryPublisher.delete(runningPlugin); } From 651f42ab80c4a693fa43ae104945188bc2ad68f5 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 9 Dec 2020 11:59:37 +0100 Subject: [PATCH 27/33] Fixed log typos --- .../vremanagement/executor/SmartExecutorInitializator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java index fee3fcd..be6857f 100644 --- a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java +++ b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java @@ -62,7 +62,7 @@ public class SmartExecutorInitializator implements ApplicationManager { try { isPublisher.unpublishPlugins(true); }catch (Exception e) { - logger.error("unable to unpublish plugind from IS using {}. Trying to continue.", isPublisher.getClass().getName()); + logger.error("Unable to unpublish plugins from IS using {}. Trying to continue.", isPublisher.getClass().getName()); } try { isPublisher.publishPlugins(availablePlugins); From 0d0eb85644497b1706c863587ff6ef347b46d840 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 26 May 2021 12:47:29 +0200 Subject: [PATCH 28/33] Fixing bugs --- .../executor/SmartExecutorInitializator.java | 2 +- .../orientdb/OrientDBPersistenceConnector.java | 10 +++++++--- .../vremanagement/executor/rest/RestSmartExecutor.java | 10 ++++++---- .../scheduledtask/ScheduledTaskPersistence.java | 2 +- .../SmartExecutorPersistenceConnectorTest.java | 7 +++++-- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java index be6857f..5306fec 100644 --- a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java +++ b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java @@ -93,7 +93,7 @@ public class SmartExecutorInitializator implements ApplicationManager { try { logger.debug("Going to get Orphan Scheduled Tasks in scope {}", context); - List scheduledTasks = smartExecutorPersistenceConnector.getOrphanScheduledTasks(pluginManager.getAvailablePlugins().keySet()); + List scheduledTasks = smartExecutorPersistenceConnector.getScheduledTasks(pluginManager.getAvailablePlugins().keySet(), true); if(scheduledTasks.size()==0){ logger.debug("No Orphan Scheduled Tasks this instance can take in charge in scope {}", context); } diff --git a/src/main/java/org/gcube/vremanagement/executor/persistence/orientdb/OrientDBPersistenceConnector.java b/src/main/java/org/gcube/vremanagement/executor/persistence/orientdb/OrientDBPersistenceConnector.java index 42d4474..a5cf87c 100644 --- a/src/main/java/org/gcube/vremanagement/executor/persistence/orientdb/OrientDBPersistenceConnector.java +++ b/src/main/java/org/gcube/vremanagement/executor/persistence/orientdb/OrientDBPersistenceConnector.java @@ -181,14 +181,14 @@ public class OrientDBPersistenceConnector extends SmartExecutorPersistenceConnec } @Override - public List getOrphanScheduledTasks(Collection plugins) + public List getScheduledTasks(Collection plugins, boolean onlyOrphan) throws SchedulePersistenceException { ODatabaseSession oDatabaseSession = null; try { oDatabaseSession = oDatabasePool.acquire(); String type = ScheduledTask.class.getSimpleName(); - String queryString = String.format("SELECT * FROM %s WHERE %s = '%s'", type, "scope", + String queryString = String.format("SELECT * FROM %s WHERE %s = '%s'", type, "context", ContextUtility.getCurrentContext()); if(plugins != null && plugins.size() != 0) { boolean first = true; @@ -218,7 +218,11 @@ public class OrientDBPersistenceConnector extends SmartExecutorPersistenceConnec ScheduledTask scheduledTask = ExtendedSEMapper.getInstance().unmarshal(ScheduledTask.class, json); try { - if(isOrphan(scheduledTask)) { + if(onlyOrphan) { + if(isOrphan(scheduledTask)) { + scheduledTasks.add(scheduledTask); + } + }else { scheduledTasks.add(scheduledTask); } } catch(Exception e) { diff --git a/src/main/java/org/gcube/vremanagement/executor/rest/RestSmartExecutor.java b/src/main/java/org/gcube/vremanagement/executor/rest/RestSmartExecutor.java index 0a5dcd9..610e757 100644 --- a/src/main/java/org/gcube/vremanagement/executor/rest/RestSmartExecutor.java +++ b/src/main/java/org/gcube/vremanagement/executor/rest/RestSmartExecutor.java @@ -102,14 +102,16 @@ public class RestSmartExecutor { List plugins = new ArrayList<>(); + boolean orphan = false; if(pluginName.compareTo(RestConstants.ORPHAN_PATH_PARAM)!=0) { - plugins.addAll(pluginManager.getAvailablePlugins().keySet()); + plugins.add(pluginName); + }else { - // TODO check role + plugins.addAll(pluginManager.getAvailablePlugins().keySet()); + orphan = true; } - List scheduledTasks = persistenceConnector.getOrphanScheduledTasks(plugins); - + List scheduledTasks = persistenceConnector.getScheduledTasks(plugins, orphan); /* * Using SEMapper because the server must not return sensitive information like token diff --git a/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTaskPersistence.java b/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTaskPersistence.java index 2dd7cff..74a132c 100644 --- a/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTaskPersistence.java +++ b/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTaskPersistence.java @@ -22,7 +22,7 @@ public interface ScheduledTaskPersistence { * @throws SchedulePersistenceException * if fails */ - public List getOrphanScheduledTasks(Collection plugins) + public List getScheduledTasks(Collection plugins, boolean onlyOrphan) throws SchedulePersistenceException; /** diff --git a/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java b/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java index 171299b..2e9fdaf 100644 --- a/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java @@ -3,6 +3,7 @@ */ package org.gcube.vremanagement.executor.persistence; +import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; @@ -70,12 +71,14 @@ public class SmartExecutorPersistenceConnectorTest extends ContextTest { @Test public void getAvailableScheduledTasksTest() throws Exception { - ContextTest.setContextByName(ROOT); + ContextTest.setContextByName("/gcube/devsec"); ScheduledTaskPersistence stc = ScheduledTaskPersistenceFactory.getScheduledTaskPersistence(); Assert.assertNotNull(stc); Assert.assertEquals(OrientDBPersistenceConnector.class, stc.getClass()); - List lc = stc.getOrphanScheduledTasks(null); + List plugins = new ArrayList<>(); + plugins.add("hello-world-se-plugin"); + List lc = stc.getScheduledTasks(plugins, false); logger.debug("Available Scheduled Tasks : {}", lc); } From b315c845e28577e28332790535fd0eafd18fc3c9 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 26 May 2021 12:53:29 +0200 Subject: [PATCH 29/33] fixed test --- .../persistence/SmartExecutorPersistenceConnectorTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java b/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java index 2e9fdaf..ccbb5eb 100644 --- a/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java @@ -78,7 +78,7 @@ public class SmartExecutorPersistenceConnectorTest extends ContextTest { List plugins = new ArrayList<>(); plugins.add("hello-world-se-plugin"); - List lc = stc.getScheduledTasks(plugins, false); + List lc = stc.getScheduledTasks(plugins, true); logger.debug("Available Scheduled Tasks : {}", lc); } From 4a109b94594b4bbaf798f0f0d9b3eb02638ea989 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 27 May 2021 09:28:36 +0200 Subject: [PATCH 30/33] Fixing behaviour --- .../executor/SmartExecutorInitializator.java | 10 +++++++++- .../SmartExecutorPersistenceConnector.java | 17 ++++++++-------- .../OrientDBPersistenceConnector.java | 16 ++------------- .../executor/rest/RestSmartExecutor.java | 20 +++++++++++++++---- .../ScheduledTaskPersistence.java | 2 +- ...SmartExecutorPersistenceConnectorTest.java | 2 +- 6 files changed, 38 insertions(+), 29 deletions(-) diff --git a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java index 5306fec..396d0a0 100644 --- a/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java +++ b/src/main/java/org/gcube/vremanagement/executor/SmartExecutorInitializator.java @@ -1,5 +1,6 @@ package org.gcube.vremanagement.executor; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -93,7 +94,14 @@ public class SmartExecutorInitializator implements ApplicationManager { try { logger.debug("Going to get Orphan Scheduled Tasks in scope {}", context); - List scheduledTasks = smartExecutorPersistenceConnector.getScheduledTasks(pluginManager.getAvailablePlugins().keySet(), true); + List gotScheduledTasks = smartExecutorPersistenceConnector.getScheduledTasks(pluginManager.getAvailablePlugins().keySet()); + List scheduledTasks = new ArrayList<>(); + for(ScheduledTask scheduledTask : gotScheduledTasks) { + if(smartExecutorPersistenceConnector.isOrphan(scheduledTask, true)) { + scheduledTasks.add(scheduledTask); + } + } + if(scheduledTasks.size()==0){ logger.debug("No Orphan Scheduled Tasks this instance can take in charge in scope {}", context); } diff --git a/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnector.java b/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnector.java index 53cdbb2..f464a49 100644 --- a/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnector.java +++ b/src/main/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnector.java @@ -54,7 +54,7 @@ public abstract class SmartExecutorPersistenceConnector extends PluginStateNotif */ public abstract PluginStateEvolution getPluginInstanceState(UUID uuid, Integer iterationNumber) throws PluginInstanceNotFoundException, ExecutorException; - protected boolean isOrphan(ScheduledTask scheduledTask) throws ExecutorException { + public boolean isOrphan(ScheduledTask scheduledTask, boolean sameHost) throws ExecutorException { try { UUID uuid = scheduledTask.getUUID(); @@ -64,19 +64,20 @@ public abstract class SmartExecutorPersistenceConnector extends PluginStateNotif } try { - HostingNode hostingNode = ContextProvider.get().container().profile(HostingNode.class); - String hnAddress = hostingNode.profile().description().name(); - - if(runOn.getHostingNode().getAddress().compareTo(hnAddress)==0){ - return true; + if(sameHost) { + HostingNode hostingNode = ContextProvider.get().container().profile(HostingNode.class); + String hnAddress = hostingNode.profile().description().name(); + + if(runOn.getHostingNode().getAddress().compareTo(hnAddress)==0){ + return true; + } } }catch (Exception e) { logger.error("Unable to check if current hosting node is the same of the one in ScheduledTask", e); } String address = runOn.getEService().getAddress(); - String pluginName = scheduledTask.getLaunchParameter() - .getPluginName(); + String pluginName = scheduledTask.getLaunchParameter().getPluginName(); try { SmartExecutorClientImpl smartExecutorClient = new SmartExecutorClientImpl(); diff --git a/src/main/java/org/gcube/vremanagement/executor/persistence/orientdb/OrientDBPersistenceConnector.java b/src/main/java/org/gcube/vremanagement/executor/persistence/orientdb/OrientDBPersistenceConnector.java index a5cf87c..7452858 100644 --- a/src/main/java/org/gcube/vremanagement/executor/persistence/orientdb/OrientDBPersistenceConnector.java +++ b/src/main/java/org/gcube/vremanagement/executor/persistence/orientdb/OrientDBPersistenceConnector.java @@ -181,7 +181,7 @@ public class OrientDBPersistenceConnector extends SmartExecutorPersistenceConnec } @Override - public List getScheduledTasks(Collection plugins, boolean onlyOrphan) + public List getScheduledTasks(Collection plugins) throws SchedulePersistenceException { ODatabaseSession oDatabaseSession = null; try { @@ -215,20 +215,8 @@ public class OrientDBPersistenceConnector extends SmartExecutorPersistenceConnec for(ODocument doc : result) { String json = doc.toJSON("class"); - ScheduledTask scheduledTask = ExtendedSEMapper.getInstance().unmarshal(ScheduledTask.class, json); - try { - if(onlyOrphan) { - if(isOrphan(scheduledTask)) { - scheduledTasks.add(scheduledTask); - } - }else { - scheduledTasks.add(scheduledTask); - } - } catch(Exception e) { - logger.error("An Exception occurred while evaluating if {} is orphan", json, e); - } - + scheduledTasks.add(scheduledTask); } return scheduledTasks; diff --git a/src/main/java/org/gcube/vremanagement/executor/rest/RestSmartExecutor.java b/src/main/java/org/gcube/vremanagement/executor/rest/RestSmartExecutor.java index 610e757..7018306 100644 --- a/src/main/java/org/gcube/vremanagement/executor/rest/RestSmartExecutor.java +++ b/src/main/java/org/gcube/vremanagement/executor/rest/RestSmartExecutor.java @@ -101,17 +101,29 @@ public class RestSmartExecutor { PluginManager pluginManager = PluginManager.getInstance(); List plugins = new ArrayList<>(); - boolean orphan = false; + if(pluginName.compareTo(RestConstants.ORPHAN_PATH_PARAM)!=0) { plugins.add(pluginName); - }else { - plugins.addAll(pluginManager.getAvailablePlugins().keySet()); + // plugins.addAll(pluginManager.getAvailablePlugins().keySet()); orphan = true; } - List scheduledTasks = persistenceConnector.getScheduledTasks(plugins, orphan); + List gotScheduledTasks = persistenceConnector.getScheduledTasks(plugins); + + List scheduledTasks; + + if(orphan) { + scheduledTasks = new ArrayList<>(); + for(ScheduledTask scheduledTask : gotScheduledTasks) { + if(persistenceConnector.isOrphan(scheduledTask, false)) { + scheduledTasks.add(scheduledTask); + } + } + }else { + scheduledTasks = gotScheduledTasks; + } /* * Using SEMapper because the server must not return sensitive information like token diff --git a/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTaskPersistence.java b/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTaskPersistence.java index 74a132c..2b9f70a 100644 --- a/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTaskPersistence.java +++ b/src/main/java/org/gcube/vremanagement/executor/scheduledtask/ScheduledTaskPersistence.java @@ -22,7 +22,7 @@ public interface ScheduledTaskPersistence { * @throws SchedulePersistenceException * if fails */ - public List getScheduledTasks(Collection plugins, boolean onlyOrphan) + public List getScheduledTasks(Collection plugins) throws SchedulePersistenceException; /** diff --git a/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java b/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java index ccbb5eb..a84db16 100644 --- a/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java +++ b/src/test/java/org/gcube/vremanagement/executor/persistence/SmartExecutorPersistenceConnectorTest.java @@ -78,7 +78,7 @@ public class SmartExecutorPersistenceConnectorTest extends ContextTest { List plugins = new ArrayList<>(); plugins.add("hello-world-se-plugin"); - List lc = stc.getScheduledTasks(plugins, true); + List lc = stc.getScheduledTasks(plugins); logger.debug("Available Scheduled Tasks : {}", lc); } From bfa4b0a629c7168d89d697511bb89663e82267dc Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 3 Jun 2021 23:05:57 +0200 Subject: [PATCH 31/33] Switching to orientdb 3.2.0 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 4857da2..1921f12 100644 --- a/pom.xml +++ b/pom.xml @@ -19,7 +19,7 @@ UTF-8 ${project.basedir}${file.separator}src${file.separator}main${file.separator}webapp${file.separator}WEB-INF - 3.0.34 + 3.2.0 From 636eb9e37b71707f261e2cc97d4f7b422bca7eb9 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Fri, 4 Jun 2021 12:03:46 +0200 Subject: [PATCH 32/33] Removed unneeded assignement --- .../gcube/vremanagement/executor/rest/RestSmartExecutor.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/gcube/vremanagement/executor/rest/RestSmartExecutor.java b/src/main/java/org/gcube/vremanagement/executor/rest/RestSmartExecutor.java index 7018306..a70a7c2 100644 --- a/src/main/java/org/gcube/vremanagement/executor/rest/RestSmartExecutor.java +++ b/src/main/java/org/gcube/vremanagement/executor/rest/RestSmartExecutor.java @@ -98,7 +98,8 @@ public class RestSmartExecutor { try { SmartExecutorPersistenceConnector persistenceConnector = SmartExecutorPersistenceFactory .getPersistenceConnector(); - PluginManager pluginManager = PluginManager.getInstance(); + + PluginManager.getInstance(); List plugins = new ArrayList<>(); boolean orphan = false; From a27004d960bf275b4df51c4a43c3dead9395d260 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Tue, 8 Jun 2021 12:09:46 +0200 Subject: [PATCH 33/33] Switched OrientDB to version 3.1.12 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 1921f12..00e2dc5 100644 --- a/pom.xml +++ b/pom.xml @@ -19,7 +19,7 @@ UTF-8 ${project.basedir}${file.separator}src${file.separator}main${file.separator}webapp${file.separator}WEB-INF - 3.2.0 + 3.1.12