Merged from branch of release 4.5.0

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/vre-management/smart-executor@149072 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-05-25 12:38:46 +00:00
parent 5ca3f3d881
commit b00cd5e9d9
10 changed files with 405 additions and 416 deletions

54
pom.xml
View File

@ -58,6 +58,7 @@
</dependencyManagement> </dependencyManagement>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.gcube.resources.discovery</groupId> <groupId>org.gcube.resources.discovery</groupId>
<artifactId>ic-client</artifactId> <artifactId>ic-client</artifactId>
@ -68,20 +69,14 @@
<artifactId>discovery-client</artifactId> <artifactId>discovery-client</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.gcube.core</groupId> <groupId>org.gcube.core</groupId>
<artifactId>common-generic-clients</artifactId> <artifactId>common-scope</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.gcube.core</groupId> <groupId>org.gcube.core</groupId>
<artifactId>common-gcube-calls</artifactId> <artifactId>common-scope-maps</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-jaxws-calls</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
@ -95,10 +90,10 @@
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.gcube.core</groupId> <groupId>org.gcube.resources</groupId>
<artifactId>common-smartgears-app</artifactId> <artifactId>common-gcore-resources</artifactId>
<scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.gcube.common</groupId> <groupId>org.gcube.common</groupId>
<artifactId>authorization-client</artifactId> <artifactId>authorization-client</artifactId>
@ -114,40 +109,57 @@
<artifactId>smart-executor-client</artifactId> <artifactId>smart-executor-client</artifactId>
<version>[1.4.0-SNAPSHOT,2.0.0-SNAPSHOT]</version> <version>[1.4.0-SNAPSHOT,2.0.0-SNAPSHOT]</version>
</dependency> </dependency>
<dependency>
<!-- <groupId>com.fasterxml.jackson.core</groupId>
<dependency> <artifactId>jackson-databind</artifactId>
<groupId>org.gcube.information-system</groupId>
<artifactId>resource-registry-publisher</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.gcube.information-system</groupId> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>information-system-model</artifactId> <artifactId>jackson-annotations</artifactId>
<scope>provided</scope>
</dependency> </dependency>
-->
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-generic-clients</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-smartgears-app</artifactId>
</dependency>
<dependency>
<groupId>com.orientechnologies</groupId> <groupId>com.orientechnologies</groupId>
<artifactId>orientdb-client</artifactId> <artifactId>orientdb-client</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.gcube.vremanagement</groupId> <groupId>org.gcube.vremanagement</groupId>
<artifactId>smart-executor-api</artifactId> <artifactId>smart-executor-api</artifactId>
<version>[1.4.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version> <version>[1.4.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
</dependency> </dependency>
<!--
<dependency> <dependency>
<groupId>javax.servlet</groupId> <groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId> <artifactId>javax.servlet-api</artifactId>
<version>[3.0.1, 4.0.0)</version> <version>[3.0.1, 4.0.0)</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
-->
<dependency> <dependency>
<groupId>com.sun.xml.ws</groupId> <groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId> <artifactId>jaxws-rt</artifactId>
<version>2.1.7</version> <version>2.1.7</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>

View File

@ -19,6 +19,7 @@ import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceFact
import org.gcube.vremanagement.executor.plugin.PluginState; import org.gcube.vremanagement.executor.plugin.PluginState;
import org.gcube.vremanagement.executor.plugin.PluginStateEvolution; import org.gcube.vremanagement.executor.plugin.PluginStateEvolution;
import org.gcube.vremanagement.executor.scheduler.SmartExecutorScheduler; import org.gcube.vremanagement.executor.scheduler.SmartExecutorScheduler;
import org.gcube.vremanagement.executor.scheduler.SmartExecutorSchedulerFactory;
import org.quartz.SchedulerException; import org.quartz.SchedulerException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -47,7 +48,12 @@ public class SmartExecutorImpl implements SmartExecutor {
logger.info("Launch requested {}", parameter); logger.info("Launch requested {}", parameter);
SmartExecutorScheduler smartExecutorScheduler = SmartExecutorScheduler.getInstance(); SmartExecutorScheduler smartExecutorScheduler;
try {
smartExecutorScheduler = SmartExecutorSchedulerFactory.getSmartExecutorScheduler();
} catch (SchedulerException e) {
throw new ExecutorException(e);
}
UUID uuid = smartExecutorScheduler.schedule(parameter, null); UUID uuid = smartExecutorScheduler.schedule(parameter, null);
logger.info( logger.info(
String.format( String.format(
@ -61,7 +67,7 @@ public class SmartExecutorImpl implements SmartExecutor {
@Override @Override
public boolean stop(String executionIdentifier) throws ExecutorException { public boolean stop(String executionIdentifier) throws ExecutorException {
logger.info("Stop requested for {}", executionIdentifier); logger.info("Stop requested for {}", executionIdentifier);
boolean ret = unSchedule(executionIdentifier, true, false); boolean ret = unSchedule(executionIdentifier, false);
logger.info("{} was{} stopped succesfully", executionIdentifier, ret? "" : " not"); logger.info("{} was{} stopped succesfully", executionIdentifier, ret? "" : " not");
return ret; return ret;
} }
@ -72,18 +78,18 @@ public class SmartExecutorImpl implements SmartExecutor {
throws ExecutorException { throws ExecutorException {
logger.info("UnSchedule requested for {} globally : {}", logger.info("UnSchedule requested for {} globally : {}",
executionIdentifier, globally); executionIdentifier, globally);
boolean ret = unSchedule(executionIdentifier, false, globally); boolean ret = unSchedule(executionIdentifier, globally);
logger.info("{} was{} unscheduled {} succesfully", executionIdentifier, ret? "" : " not", globally? "globally": "locally"); logger.info("{} was{} unscheduled {} succesfully", executionIdentifier, ret? "" : " not", globally? "globally": "locally");
return ret; return ret;
} }
// TODO Manage better exception to to advise the caller // TODO Manage better exception to to advise the caller
protected boolean unSchedule(String executionIdentifier, boolean stopOnly, boolean globally) throws ExecutorException { protected boolean reallyUnSchedule(String executionIdentifier, boolean globally) throws ExecutorException {
boolean currentStopped = true; boolean currentStopped = true;
try { try {
SmartExecutorScheduler smartExecutorScheduler = SmartExecutorScheduler.getInstance(); SmartExecutorScheduler smartExecutorScheduler = SmartExecutorSchedulerFactory.getSmartExecutorScheduler();
UUID uuid = UUID.fromString(executionIdentifier); UUID uuid = UUID.fromString(executionIdentifier);
smartExecutorScheduler.stop(uuid, stopOnly, globally); smartExecutorScheduler.stop(uuid, globally);
} catch (SchedulerNotFoundException e) { } catch (SchedulerNotFoundException e) {
// currentStopped = true; // currentStopped = true;
logger.error("Error unscheduling task {}", executionIdentifier, e); logger.error("Error unscheduling task {}", executionIdentifier, e);

View File

@ -38,6 +38,8 @@ import org.gcube.vremanagement.executor.plugin.PluginDeclaration;
import org.gcube.vremanagement.executor.pluginmanager.PluginManager; import org.gcube.vremanagement.executor.pluginmanager.PluginManager;
import org.gcube.vremanagement.executor.scheduledtask.ScheduledTask; import org.gcube.vremanagement.executor.scheduledtask.ScheduledTask;
import org.gcube.vremanagement.executor.scheduler.SmartExecutorScheduler; import org.gcube.vremanagement.executor.scheduler.SmartExecutorScheduler;
import org.gcube.vremanagement.executor.scheduler.SmartExecutorSchedulerFactory;
import org.quartz.SchedulerException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -297,6 +299,7 @@ public class SmartExecutorInitializator implements ApplicationManager {
@Override @Override
public void onInit() { public void onInit() {
String scope = getCurrentScope(); String scope = getCurrentScope();
logger.trace( logger.trace(
"\n-------------------------------------------------------\n" "\n-------------------------------------------------------\n"
+ "Smart Executor is Starting on scope {}\n" + "Smart Executor is Starting on scope {}\n"
@ -330,54 +333,60 @@ public class SmartExecutorInitializator implements ApplicationManager {
// Persistence to clean previous situation of a failure of HostingNode // Persistence to clean previous situation of a failure of HostingNode
try { try {
logger.debug("Going to get Orphan Scheduled Tasks"); logger.debug("Going to get Orphan Scheduled Tasks in scope {}", scope);
List<ScheduledTask> scheduledTasks = smartExecutorPersistenceConnector.getOrphanScheduledTasks(availablePlugins.values()); List<ScheduledTask> scheduledTasks = smartExecutorPersistenceConnector.getOrphanScheduledTasks(availablePlugins.values());
if(scheduledTasks.size()==0){
logger.debug("No Orphan Scheduled Tasks this instance can take in charge in scope {}", scope);
}
for(final ScheduledTask scheduledTask : scheduledTasks){ for(final ScheduledTask scheduledTask : scheduledTasks){
final ObjectMapper mapper = ObjectMapperManager.getObjectMapper();
String taskAsString = mapper.writeValueAsString(scheduledTask);
try { try {
// Reserving the task. // Reserving the task.
smartExecutorPersistenceConnector.reserveScheduledTask(scheduledTask); smartExecutorPersistenceConnector.reserveScheduledTask(scheduledTask);
}catch (Exception e) { }catch (Exception e) {
logger.debug("someone else is going to take in charge the scheduled task. Skipping."); logger.debug("({}) Someone else is going to take in charge the scheduled task {}. Skipping.", scope, taskAsString);
continue; continue;
} }
final ObjectMapper mapper = ObjectMapperManager.getObjectMapper();
Thread thread = new Thread(){ Thread thread = new Thread(){
@Override @Override
public void run(){ public void run(){
LaunchParameter launchParameter = scheduledTask.getLaunchParameter(); LaunchParameter launchParameter = scheduledTask.getLaunchParameter();
try { try {
logger.info("Going to schedule an already scheduled task with the following parameters {}", mapper.writeValueAsString(launchParameter)); logger.info("({}) Going to schedule an already scheduled task with the following parameters {}", scope, mapper.writeValueAsString(launchParameter));
} catch (Exception e1) { } catch (Exception e1) {
} }
SmartExecutorScheduler smartExecutorScheduler = SmartExecutorScheduler.getInstance();
String scheduledTasktoken = scheduledTask.getToken(); String scheduledTasktoken = scheduledTask.getToken();
try { try {
setContext(scheduledTasktoken); setContext(scheduledTasktoken);
SmartExecutorScheduler smartExecutorScheduler = SmartExecutorSchedulerFactory.getSmartExecutorScheduler();
// A new Scheduled Task will be persisted due to launch. Removing it // A new Scheduled Task will be persisted due to launch. Removing it
smartExecutorPersistenceConnector.removeScheduledTask(scheduledTask); smartExecutorPersistenceConnector.removeScheduledTask(scheduledTask);
smartExecutorScheduler.schedule(launchParameter, scheduledTask.getUUID()); smartExecutorScheduler.schedule(launchParameter, scheduledTask.getUUID());
} catch (Exception e) { } catch (Exception e) {
logger.error("Error while trying to relaunch scheduled task.", e); logger.error("({}) Error while trying to relaunch scheduled task.", scope, e);
try { try {
smartExecutorPersistenceConnector.addScheduledTask(scheduledTask); smartExecutorPersistenceConnector.addScheduledTask(scheduledTask);
} catch (Exception ex) { } catch (Exception ex) {
logger.error("Unable to "); logger.error("({}) Unable to add back scheduled task {}", scope, taskAsString);
} }
} }
} }
}; };
thread.start(); thread.start();
} }
} catch (Exception e) { } catch (Exception e) {
@ -409,14 +418,21 @@ public class SmartExecutorInitializator implements ApplicationManager {
+ "-------------------------------------------------------", + "-------------------------------------------------------",
getCurrentScope()); getCurrentScope());
// TODO release scheduled tasks
SmartExecutorScheduler.getInstance().stopAll(); SmartExecutorScheduler scheduler;
try {
scheduler = SmartExecutorSchedulerFactory.getSmartExecutorScheduler();
scheduler.stopAll();
SmartExecutorSchedulerFactory.remove();
} catch (SchedulerException e) {
logger.error("", e);
}
cleanServiceEndpoints(); cleanServiceEndpoints();
try { try {
SmartExecutorPersistenceFactory.getPersistenceConnector().close(); SmartExecutorPersistenceFactory.closePersistenceConnector();
} catch (Exception e) { } catch (Throwable e) {
logger.error("Unable to correctly close {} for scope {}", logger.error("Unable to correctly close {} for scope {}",
SmartExecutorPersistenceConnector.class.getSimpleName(), SmartExecutorPersistenceConnector.class.getSimpleName(),
getCurrentScope(), e); getCurrentScope(), e);
@ -427,6 +443,5 @@ public class SmartExecutorInitializator implements ApplicationManager {
+ "Smart Executor Stopped Successfully on scope {}\n" + "Smart Executor Stopped Successfully on scope {}\n"
+ "-------------------------------------------------------", + "-------------------------------------------------------",
getCurrentScope()); getCurrentScope());
} }
} }

View File

@ -6,11 +6,13 @@ package org.gcube.vremanagement.executor.persistence;
import java.util.HashMap; import java.util.HashMap;
import java.util.UUID; import java.util.UUID;
import org.gcube.common.clients.exceptions.DiscoveryException;
import org.gcube.common.resources.gcore.HostingNode; import org.gcube.common.resources.gcore.HostingNode;
import org.gcube.smartgears.ContextProvider; import org.gcube.smartgears.ContextProvider;
import org.gcube.vremanagement.executor.client.plugins.ExecutorPlugin; import org.gcube.vremanagement.executor.client.plugins.ExecutorPlugin;
import org.gcube.vremanagement.executor.client.plugins.query.filter.SpecificEndpointDiscoveryFilter; import org.gcube.vremanagement.executor.client.plugins.query.filter.SpecificEndpointDiscoveryFilter;
import org.gcube.vremanagement.executor.client.proxies.SmartExecutorProxy; import org.gcube.vremanagement.executor.client.proxies.SmartExecutorProxy;
import org.gcube.vremanagement.executor.exception.ExecutorException;
import org.gcube.vremanagement.executor.json.ObjectMapperManager; import org.gcube.vremanagement.executor.json.ObjectMapperManager;
import org.gcube.vremanagement.executor.plugin.Plugin; import org.gcube.vremanagement.executor.plugin.Plugin;
import org.gcube.vremanagement.executor.plugin.PluginState; import org.gcube.vremanagement.executor.plugin.PluginState;
@ -79,7 +81,6 @@ public abstract class SmartExecutorPersistenceConnector extends PluginStateNotif
} }
}catch (Exception e) { }catch (Exception e) {
logger.error("Unable to check if current hosting node is the same of the one in ScheduledTask", 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 address = runOn.getEService().getAddress();
@ -95,18 +96,26 @@ public abstract class SmartExecutorPersistenceConnector extends PluginStateNotif
.getExecutorProxy(pluginName, null, null, .getExecutorProxy(pluginName, null, null,
specificEndpointDiscoveryFilter).build(); specificEndpointDiscoveryFilter).build();
proxy.getStateEvolution(uuid.toString()); proxy.getStateEvolution(uuid.toString());
} catch (Exception e) { logger.trace("{} is not orphan.", ObjectMapperManager
.getObjectMapper().writeValueAsString(scheduledTask));
return false;
} catch (DiscoveryException | ExecutorException e) {
// The instance was not found or the request failed. // The instance was not found or the request failed.
// The scheduledTask is considered orphan // The scheduledTask is considered orphan
logger.trace("{} is considered orphan.", ObjectMapperManager logger.trace("{} is considered orphan.", ObjectMapperManager
.getObjectMapper().writeValueAsString(scheduledTask)); .getObjectMapper().writeValueAsString(scheduledTask), e);
return true; return true;
} catch (Throwable e) {
// The scheduledTask is NOT considered orphan
logger.trace("{} is NOT considered orphan.", ObjectMapperManager
.getObjectMapper().writeValueAsString(scheduledTask), e);
return false;
} }
} catch (Exception e) { } catch (Exception e) {
String string = ObjectMapperManager.getObjectMapper() String string = ObjectMapperManager.getObjectMapper()
.writeValueAsString(scheduledTask); .writeValueAsString(scheduledTask);
logger.error("Error while checking orphanity of " + string logger.error("Error while checking orphanity of " + string
+ ". Considering as not orphan."); + ". Considering as not orphan.", e);
} }
return false; return false;

View File

@ -25,7 +25,7 @@ public abstract class SmartExecutorPersistenceFactory {
persistenceConnectors = new HashMap<String, SmartExecutorPersistenceConnector>(); persistenceConnectors = new HashMap<String, SmartExecutorPersistenceConnector>();
} }
public static SmartExecutorPersistenceConnector getPersistenceConnector(String scope){ private static SmartExecutorPersistenceConnector getPersistenceConnector(String scope) throws Exception {
if(scope==null){ if(scope==null){
String error = "No Scope available."; String error = "No Scope available.";
logger.error(error); logger.error(error);
@ -35,16 +35,7 @@ public abstract class SmartExecutorPersistenceFactory {
logger.trace("Retrieving {} for scope {}", logger.trace("Retrieving {} for scope {}",
SmartExecutorPersistenceConnector.class.getSimpleName(), scope); SmartExecutorPersistenceConnector.class.getSimpleName(), scope);
return persistenceConnectors.get(scope); SmartExecutorPersistenceConnector persistence = persistenceConnectors.get(scope);
}
/**
* @return the persistenceConnector
*/
public static synchronized SmartExecutorPersistenceConnector getPersistenceConnector() throws Exception {
String scope = SmartExecutorInitializator.getCurrentScope();
SmartExecutorPersistenceConnector persistence =
getPersistenceConnector(scope);
if(persistence==null){ if(persistence==null){
logger.trace("Retrieving {} for scope {} not found on internal {}. Intializing it.", logger.trace("Retrieving {} for scope {} not found on internal {}. Intializing it.",
@ -55,7 +46,7 @@ public abstract class SmartExecutorPersistenceFactory {
SmartExecutorPersistenceConfiguration configuration = SmartExecutorPersistenceConfiguration configuration =
new SmartExecutorPersistenceConfiguration(className); new SmartExecutorPersistenceConfiguration(className);
persistence = new OrientDBPersistenceConnector(scope, configuration); persistence = new OrientDBPersistenceConnector(configuration);
persistenceConnectors.put(SmartExecutorInitializator.getCurrentScope(), persistenceConnectors.put(SmartExecutorInitializator.getCurrentScope(),
persistence); persistence);
} }
@ -63,6 +54,14 @@ public abstract class SmartExecutorPersistenceFactory {
return persistence; return persistence;
} }
/**
* @return the persistenceConnector
*/
public static synchronized SmartExecutorPersistenceConnector getPersistenceConnector() throws Exception {
String scope = SmartExecutorInitializator.getCurrentScope();
return getPersistenceConnector(scope);
}
public static synchronized void closePersistenceConnector() throws Exception { public static synchronized void closePersistenceConnector() throws Exception {
String scope = SmartExecutorInitializator.getCurrentScope(); String scope = SmartExecutorInitializator.getCurrentScope();
SmartExecutorPersistenceConnector persistence = SmartExecutorPersistenceConnector persistence =

View File

@ -42,8 +42,6 @@ public class OrientDBPersistenceConnector extends
protected final static int LAST = -1; protected final static int LAST = -1;
protected final String scope;
protected final String SCOPE = "scope"; protected final String SCOPE = "scope";
protected final String UUID = "uuid"; protected final String UUID = "uuid";
protected final String ITERATION = "iteration"; protected final String ITERATION = "iteration";
@ -54,21 +52,13 @@ public class OrientDBPersistenceConnector extends
protected OPartitionedDatabasePool oPartitionedDatabasePool; protected OPartitionedDatabasePool oPartitionedDatabasePool;
protected ObjectMapper mapper; protected ObjectMapper mapper;
public OrientDBPersistenceConnector(String scope, public OrientDBPersistenceConnector(SmartExecutorPersistenceConfiguration configuration)
SmartExecutorPersistenceConfiguration configuration)
throws Exception { throws Exception {
super(); super();
this.scope = scope;
prepareConnection(configuration); prepareConnection(configuration);
this.mapper = ObjectMapperManager.getObjectMapper(); this.mapper = ObjectMapperManager.getObjectMapper();
} }
public OrientDBPersistenceConnector(
SmartExecutorPersistenceConfiguration configuration)
throws Exception {
this(SmartExecutorInitializator.getCurrentScope(), configuration);
}
protected void prepareConnection( protected void prepareConnection(
SmartExecutorPersistenceConfiguration configuration) SmartExecutorPersistenceConfiguration configuration)
throws Exception { throws Exception {
@ -99,7 +89,7 @@ public class OrientDBPersistenceConnector extends
String type = PluginStateEvolution.class.getSimpleName(); String type = PluginStateEvolution.class.getSimpleName();
Map<String, Object> params = new HashMap<String, Object>(); Map<String, Object> params = new HashMap<String, Object>();
params.put(UUID, uuid.toString()); params.put(UUID, uuid.toString());
params.put(SCOPE, scope); params.put(SCOPE, SmartExecutorInitializator.getCurrentScope());
OSQLSynchQuery<ODocument> query = null; OSQLSynchQuery<ODocument> query = null;
if (iterationNumber != LAST) { if (iterationNumber != LAST) {
@ -169,7 +159,7 @@ public class OrientDBPersistenceConnector extends
PluginStateEvolution.class.getSimpleName()); PluginStateEvolution.class.getSimpleName());
String json = mapper.writeValueAsString(pluginStateEvolution); String json = mapper.writeValueAsString(pluginStateEvolution);
doc.fromJSON(json); doc.fromJSON(json);
doc.field(SCOPE, scope); doc.field(SCOPE, SmartExecutorInitializator.getCurrentScope());
doc.save(); doc.save();
db.commit(); db.commit();
@ -263,7 +253,7 @@ public class OrientDBPersistenceConnector extends
} catch (Exception e) { } catch (Exception e) {
logger.error( logger.error(
"An Exception occurred while evaluating if {} is orphan", "An Exception occurred while evaluating if {} is orphan",
json); json, e);
} }
} }

View File

@ -5,9 +5,10 @@ package org.gcube.vremanagement.executor.scheduler;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
@ -16,9 +17,7 @@ import org.gcube.vremanagement.executor.api.types.Scheduling;
import org.gcube.vremanagement.executor.exception.InputsNullException; import org.gcube.vremanagement.executor.exception.InputsNullException;
import org.gcube.vremanagement.executor.exception.LaunchException; import org.gcube.vremanagement.executor.exception.LaunchException;
import org.gcube.vremanagement.executor.exception.PluginNotFoundException; import org.gcube.vremanagement.executor.exception.PluginNotFoundException;
import org.gcube.vremanagement.executor.exception.SchedulePersistenceException;
import org.gcube.vremanagement.executor.exception.SchedulerNotFoundException; import org.gcube.vremanagement.executor.exception.SchedulerNotFoundException;
import org.gcube.vremanagement.executor.exception.SchedulerRemoveException;
import org.gcube.vremanagement.executor.exception.UnableToInterruptTaskException; import org.gcube.vremanagement.executor.exception.UnableToInterruptTaskException;
import org.gcube.vremanagement.executor.json.ObjectMapperManager; import org.gcube.vremanagement.executor.json.ObjectMapperManager;
import org.gcube.vremanagement.executor.pluginmanager.PluginManager; import org.gcube.vremanagement.executor.pluginmanager.PluginManager;
@ -34,11 +33,9 @@ import org.quartz.JobKey;
import org.quartz.ScheduleBuilder; import org.quartz.ScheduleBuilder;
import org.quartz.Scheduler; import org.quartz.Scheduler;
import org.quartz.SchedulerException; import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder; import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger; import org.quartz.Trigger;
import org.quartz.TriggerBuilder; import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -50,26 +47,15 @@ public class SmartExecutorScheduler {
private static Logger logger = LoggerFactory private static Logger logger = LoggerFactory
.getLogger(SmartExecutorScheduler.class); .getLogger(SmartExecutorScheduler.class);
/**
* Contains running plugin instances. The key is the associated random UUID.
* This is needed to correctly stop the running plugin execution if the
* container is stopped in the proper way
*/
protected Map<UUID, Scheduler> activeSchedulers;
private static SmartExecutorScheduler smartExecutorScheduler;
public synchronized static SmartExecutorScheduler getInstance() {
if (smartExecutorScheduler == null) {
smartExecutorScheduler = new SmartExecutorScheduler();
}
return smartExecutorScheduler;
}
private SmartExecutorScheduler() {
activeSchedulers = new HashMap<UUID, Scheduler>();
}
protected Set<UUID> scheduledJobs;
protected final Scheduler shedul;
SmartExecutorScheduler(Scheduler scheduler) throws SchedulerException {
this.shedul = scheduler;
this.shedul.start();
this.scheduledJobs = new HashSet<>();
}
protected TriggerBuilder<? extends Trigger> createTriggerBuilder(UUID uuid, ScheduleBuilder<? extends Trigger> sb){ protected TriggerBuilder<? extends Trigger> createTriggerBuilder(UUID uuid, ScheduleBuilder<? extends Trigger> sb){
return TriggerBuilder.newTrigger().withIdentity(uuid.toString()) return TriggerBuilder.newTrigger().withIdentity(uuid.toString())
@ -115,9 +101,7 @@ public class SmartExecutorScheduler {
* @throws SchedulerException if the scheduler cannot be created by the * @throws SchedulerException if the scheduler cannot be created by the
* scheduler factory * scheduler factory
*/ */
protected Scheduler reallySchedule(final UUID uuid, LaunchParameter parameter) throws LaunchException, SchedulerException { protected void reallySchedule(final UUID uuid, LaunchParameter parameter) throws LaunchException, SchedulerException {
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
JobKey jobKey = new JobKey(uuid.toString()); JobKey jobKey = new JobKey(uuid.toString());
JobDetail jobDetail = JobBuilder.newJob(SmartExecutorTask.class). JobDetail jobDetail = JobBuilder.newJob(SmartExecutorTask.class).
@ -183,13 +167,11 @@ public class SmartExecutorScheduler {
try { try {
SmartExecutorTaskListener sejl = new SmartExecutorTaskListener(); SmartExecutorTaskListener sejl = new SmartExecutorTaskListener();
scheduler.getListenerManager().addJobListener(sejl); shedul.getListenerManager().addJobListener(sejl);
scheduler.scheduleJob(jobDetail, triggerBuilder.build()); shedul.scheduleJob(jobDetail, triggerBuilder.build());
} catch (SchedulerException e) { } catch (SchedulerException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
return scheduler;
} }
/** /**
@ -220,75 +202,39 @@ public class SmartExecutorScheduler {
} }
try { try {
Scheduler scheduler = reallySchedule(uuid, parameter); reallySchedule(uuid, parameter);
activeSchedulers.put(uuid, scheduler); scheduledJobs.add(uuid);
scheduler.start();
} catch (SchedulerException e) { } catch (SchedulerException e) {
throw new LaunchException(e); throw new LaunchException(e);
} }
return uuid; return uuid;
} }
public Scheduler getScheduler(UUID key){
return activeSchedulers.get(key);
}
protected void stopLastcurrentExecution(Scheduler scheduler, UUID uuid) protected void stopTask(UUID uuid)
throws UnableToInterruptTaskException{ throws UnableToInterruptTaskException{
JobKey jobKey = new JobKey(uuid.toString()); JobKey jobKey = new JobKey(uuid.toString());
try { try {
logger.debug("Going to stop current SmartExecutor Task {} execution if any", uuid); logger.debug("Going to stop current SmartExecutor Task {} execution if any", uuid);
if(!scheduler.checkExists(jobKey)){ if(!shedul.checkExists(jobKey)){
logger.debug("No SmartExecutor Task {} was found. That's all folk.", uuid); logger.trace("SmartExecutor Task {} does not have any instaces associated. Cleaning the environment. That's all folk.", uuid);
scheduledJobs.remove(uuid);
throw new SchedulerNotFoundException("Scheduler Not Found"); throw new SchedulerNotFoundException("Scheduler Not Found");
} }
boolean interrupted = scheduler.interrupt(jobKey); boolean interrupted = shedul.interrupt(jobKey);
shedul.deleteJob(jobKey);
if (interrupted) { if (interrupted) {
logger.debug("SmartExecutor Task {} interrupted successfully.", uuid); logger.debug("SmartExecutor Task {} interrupted successfully.", uuid);
} else { } else {
List<JobExecutionContext> list = getCurrentlyExecutingJobs(scheduler); logger.debug("SmartExecutor Task {} was not interrupted.", uuid);
if(list!=null && list.size()>0){
logger.debug("SmartExecutor Task {} was not interrupted.", uuid);
throw new UnableToInterruptTaskException(uuid);
}
} }
} catch (UnableToInterruptTaskException e) {
throw e;
} catch(Exception e){ } catch(Exception e){
throw new UnableToInterruptTaskException(uuid, e); throw new UnableToInterruptTaskException(uuid, e);
} }
}
protected void deleteScheduler(Scheduler scheduler, UUID uuid) throws SchedulerRemoveException {
JobKey jobKey = new JobKey(uuid.toString());
try {
logger.debug("Going to delete SmartExecutor Scheduled Task {}", uuid);
boolean deleted = scheduler.deleteJob(jobKey);
if (deleted) {
logger.debug("SmartExecutor Task {} deleted successfully", uuid);
} else {
logger.debug("SmartExecutor Task {} was not deleted", uuid);
throw new SchedulerRemoveException(uuid);
}
} catch(SchedulerRemoveException e){
throw e;
} catch(Exception e1){
throw new SchedulerRemoveException(uuid, e1);
} finally {
activeSchedulers.remove(uuid);
try {
scheduler.clear();
} catch(SchedulerException e){
throw new SchedulerRemoveException(uuid, e);
}
}
} }
protected List<JobExecutionContext> getCurrentlyExecutingJobs(Scheduler scheduler) throws SchedulerException{ protected List<JobExecutionContext> getCurrentlyExecutingJobs(Scheduler scheduler) throws SchedulerException{
@ -298,35 +244,13 @@ public class SmartExecutorScheduler {
return cej; return cej;
} }
public LaunchParameter getLaunchParameter(Scheduler scheduler, JobKey jobKey) throws SchedulerException{ public LaunchParameter getLaunchParameter(JobKey jobKey) throws SchedulerException{
JobDetail jobDetail = scheduler.getJobDetail(jobKey); JobDetail jobDetail = shedul.getJobDetail(jobKey);
JobDataMap jobDataMap = jobDetail.getJobDataMap(); JobDataMap jobDataMap = jobDetail.getJobDataMap();
return (LaunchParameter) jobDataMap.get(SmartExecutorTask.LAUNCH_PARAMETER); return (LaunchParameter) jobDataMap.get(SmartExecutorTask.LAUNCH_PARAMETER);
} }
protected void removeFromPersistence(boolean global, UUID uuid, boolean remove) throws SchedulePersistenceException{
try {
ScheduledTaskPersistence stc = ScheduledTaskPersistenceFactory.getScheduledTaskPersistence();
if(remove){
logger.debug("Going to remove the SmartExecutor Scheduled Task {} from global scheduling", uuid);
stc.removeScheduledTask(uuid);
}else{
if(global){
logger.debug("Going to release the SmartExecutor Scheduled Task {}. The Task can be take in charge from another SmartExecutor instance", uuid);
stc.releaseScheduledTask(uuid);
}else{
logger.debug("Going to remove the SmartExecutor Scheduled Task {} from local scheduling", uuid);
stc.removeScheduledTask(uuid);
}
}
}catch(Exception e){
throw new SchedulePersistenceException(
String.format("Unable to Remove Scheduled Task %s from global scheduling",
uuid.toString()), e);
}
}
/** /**
* Stop the execution of the Task identified by UUID * Stop the execution of the Task identified by UUID
* @param uuid which identify the Task * @param uuid which identify the Task
@ -334,59 +258,40 @@ public class SmartExecutorScheduler {
* @param remove : when the Task is a Scheduled one indicate if the Task * @param remove : when the Task is a Scheduled one indicate if the Task
* has to be released or to be removed (the argument is set to true when * has to be released or to be removed (the argument is set to true when
* an explicit request arrive to remove the scheduled task) * an explicit request arrive to remove the scheduled task)
* @throws UnableToInterruptTaskException * @throws Exception
* @throws SchedulerRemoveException
* @throws SchedulePersistenceException
* @throws SchedulerNotFoundException * @throws SchedulerNotFoundException
* @throws SchedulerException
*/ */
public synchronized void stop(UUID uuid, boolean stopOnly, boolean remove) public synchronized void stop(UUID uuid, boolean remove)
throws UnableToInterruptTaskException, SchedulerRemoveException, throws Exception {
SchedulePersistenceException, SchedulerException {
Scheduler scheduler = activeSchedulers.get(uuid);
if(scheduler==null){
logger.debug("No SmartExecutor Task {} was found. That's all folk.", uuid);
removeFromPersistence(true, uuid, remove);
return;
}
JobKey jobKey = new JobKey(uuid.toString()); JobKey jobKey = new JobKey(uuid.toString());
boolean exist = scheduler.checkExists(jobKey); LaunchParameter launchParameter = getLaunchParameter(jobKey);
if(!exist){
logger.trace("SmartExecutor Task {} does not have any instaces associated. Cleaning the environment. That's all folk.", uuid);
activeSchedulers.remove(uuid);
return;
}else{
logger.trace("SmartExecutor Task {} to stop exist", uuid);
}
LaunchParameter launchParameter = getLaunchParameter(scheduler, jobKey);
Scheduling scheduling = launchParameter.getScheduling(); Scheduling scheduling = launchParameter.getScheduling();
boolean scheduled = launchParameter.getScheduling() != null ? true : false; boolean scheduled = scheduling != null ? true : false;
stopLastcurrentExecution(scheduler, uuid); stopTask(uuid);
try { ScheduledTaskPersistence stc = ScheduledTaskPersistenceFactory.getScheduledTaskPersistence();
if(stopOnly ^ scheduled){
deleteScheduler(scheduler, uuid); if(scheduled){
} if(remove){
}catch(Exception e){ logger.debug("Going to remove the SmartExecutor Scheduled Task {} from global scheduling", uuid);
throw e; stc.removeScheduledTask(uuid);
} finally { }else{
if(!stopOnly && scheduled){ if(scheduling.getGlobal()){
/* Removing scheduling from persistence */ logger.debug("Going to release the SmartExecutor Scheduled Task {}. The Task can be take in charge from another SmartExecutor instance", uuid);
removeFromPersistence(scheduling.getGlobal(), uuid, remove); stc.releaseScheduledTask(uuid);
}
} }
} }
} }
public void stopAll() { public void stopAll() {
List<UUID> set = new ArrayList<UUID>(activeSchedulers.keySet()); List<UUID> set = new ArrayList<UUID>(scheduledJobs);
for (UUID uuid : set) { for (UUID uuid : set) {
try { try {
stop(uuid, true, false); stop(uuid, false);
} catch (Exception e) { } catch (Exception e) {
logger.error("Error stopping plugin instace with UUID {}", logger.error("Error stopping plugin instace with UUID {}",
uuid, e); uuid, e);

View File

@ -0,0 +1,70 @@
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.persistence.SmartExecutorPersistenceConnector;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SmartExecutorSchedulerFactory {
private static Logger logger = LoggerFactory.getLogger(SmartExecutorScheduler.class);
private static Map<String, SmartExecutorScheduler> smartExecutorSchedulers = new HashMap<>();
protected static SchedulerFactory schedulerFactory;
static {
schedulerFactory = new StdSchedulerFactory();
smartExecutorSchedulers = new HashMap<>();
}
private static SmartExecutorScheduler getSmartExecutorScheduler(String scope) throws SchedulerException {
if(scope==null){
String error = "No Scope available.";
logger.error(error);
throw new RuntimeException(error);
}
logger.trace("Retrieving {} for scope {}",
SmartExecutorPersistenceConnector.class.getSimpleName(), scope);
SmartExecutorScheduler smartExecutorScheduler = smartExecutorSchedulers.get(scope);
if(smartExecutorScheduler==null){
logger.trace("Retrieving {} for scope {} not found on internal {}. Intializing it.",
SmartExecutorScheduler.class.getSimpleName(),
scope, Map.class.getSimpleName());
Scheduler scheduler = schedulerFactory.getScheduler();
smartExecutorScheduler = new SmartExecutorScheduler(scheduler);
smartExecutorSchedulers.put(SmartExecutorInitializator.getCurrentScope(),
smartExecutorScheduler);
}
return smartExecutorScheduler;
}
/**
* @return the persistenceConnector
* @throws SchedulerException
*/
public static synchronized SmartExecutorScheduler getSmartExecutorScheduler() throws SchedulerException {
String scope = SmartExecutorInitializator.getCurrentScope();
return getSmartExecutorScheduler(scope);
}
public static void remove(){
String scope = SmartExecutorInitializator.getCurrentScope();
smartExecutorSchedulers.remove(scope);
}
}

View File

@ -16,9 +16,6 @@ import org.gcube.vremanagement.executor.exception.AlreadyInFinalStateException;
import org.gcube.vremanagement.executor.exception.InputsNullException; import org.gcube.vremanagement.executor.exception.InputsNullException;
import org.gcube.vremanagement.executor.exception.InvalidPluginStateEvolutionException; import org.gcube.vremanagement.executor.exception.InvalidPluginStateEvolutionException;
import org.gcube.vremanagement.executor.exception.PluginNotFoundException; import org.gcube.vremanagement.executor.exception.PluginNotFoundException;
import org.gcube.vremanagement.executor.exception.SchedulePersistenceException;
import org.gcube.vremanagement.executor.exception.SchedulerRemoveException;
import org.gcube.vremanagement.executor.exception.UnableToInterruptTaskException;
import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceConnector; import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceConnector;
import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceFactory; import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceFactory;
import org.gcube.vremanagement.executor.plugin.Plugin; import org.gcube.vremanagement.executor.plugin.Plugin;
@ -31,7 +28,6 @@ import org.quartz.InterruptableJob;
import org.quartz.JobDataMap; import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext; import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException; import org.quartz.JobExecutionException;
import org.quartz.SchedulerException;
import org.quartz.UnableToInterruptJobException; import org.quartz.UnableToInterruptJobException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -300,9 +296,8 @@ public class SmartExecutorTask implements InterruptableJob {
} }
protected void deschedule(boolean globally) protected void deschedule(boolean globally)
throws UnableToInterruptTaskException, SchedulerRemoveException, throws Exception {
SchedulePersistenceException, SchedulerException { SmartExecutorSchedulerFactory.getSmartExecutorScheduler().stop(uuid, globally);
SmartExecutorScheduler.getInstance().stop(uuid, false, globally);
} }
@Override @Override

View File

@ -20,6 +20,7 @@ import org.gcube.vremanagement.executor.persistence.SmartExecutorPersistenceFact
import org.gcube.vremanagement.executor.plugin.PluginState; import org.gcube.vremanagement.executor.plugin.PluginState;
import org.gcube.vremanagement.executor.plugin.PluginStateEvolution; import org.gcube.vremanagement.executor.plugin.PluginStateEvolution;
import org.gcube.vremanagement.executor.scheduler.SmartExecutorScheduler; import org.gcube.vremanagement.executor.scheduler.SmartExecutorScheduler;
import org.gcube.vremanagement.executor.scheduler.SmartExecutorSchedulerFactory;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.quartz.CronExpression; import org.quartz.CronExpression;
@ -32,470 +33,457 @@ import org.slf4j.LoggerFactory;
public class SmartExecutorSchedulerTest extends ScopedTest { public class SmartExecutorSchedulerTest extends ScopedTest {
private static Logger logger = LoggerFactory.getLogger(SmartExecutorSchedulerTest.class); private static Logger logger = LoggerFactory.getLogger(SmartExecutorSchedulerTest.class);
public static final String START = "START"; public static final String START = "START";
public static final String END = "END"; public static final String END = "END";
public static SmartExecutorPersistenceConnector pc; public static SmartExecutorPersistenceConnector pc;
public UUID scheduleTest(Scheduling scheduling, Long sleepTime) throws Exception { public UUID scheduleTest(Scheduling scheduling, Long sleepTime) throws Exception {
Map<String, Object> inputs = new HashMap<String, Object>(); Map<String, Object> inputs = new HashMap<String, Object>();
if(sleepTime==null){ if (sleepTime == null) {
sleepTime = new Long(10*1000); // 10 sec = 10 * 1000 millisec sleepTime = new Long(10 * 1000); // 10 sec = 10 * 1000 millisec
} }
inputs.put(HelloWorldPlugin.SLEEP_TIME, sleepTime); inputs.put(HelloWorldPlugin.SLEEP_TIME, sleepTime);
inputs.put("Test UUID", UUID.randomUUID()); inputs.put("Test UUID", UUID.randomUUID());
logger.debug("Inputs : {}", inputs); logger.debug("Inputs : {}", inputs);
LaunchParameter parameter = new LaunchParameter(HelloWorldPluginDeclaration.NAME, inputs); LaunchParameter parameter = new LaunchParameter(HelloWorldPluginDeclaration.NAME, inputs);
parameter.setScheduling(scheduling); parameter.setScheduling(scheduling);
SmartExecutorScheduler smartExecutorScheduler = SmartExecutorScheduler.getInstance(); SmartExecutorScheduler smartExecutorScheduler = SmartExecutorSchedulerFactory.getSmartExecutorScheduler();
UUID uuid = smartExecutorScheduler.schedule(parameter, null); UUID uuid = smartExecutorScheduler.schedule(parameter, null);
logger.debug("Scheduled Job with ID {}", uuid); logger.debug("Scheduled Job with ID {}", uuid);
pc = SmartExecutorPersistenceFactory.getPersistenceConnector(); pc = SmartExecutorPersistenceFactory.getPersistenceConnector();
return uuid; return uuid;
} }
/* DeprecatedHelloWorldPlugin dependency needed /*
@Test * DeprecatedHelloWorldPlugin dependency needed
public void deprecatedConstructorTest() throws Exception { *
Map<String, Object> inputs = new HashMap<String, Object>(); * @Test public void deprecatedConstructorTest() throws Exception {
Long sleepTime = new Long(10*1000); // 10 sec = 10 * 1000 millisec * Map<String, Object> inputs = new HashMap<String, Object>(); Long
inputs.put(DeprecatedHelloWorldPlugin.SLEEP_TIME, sleepTime); * sleepTime = new Long(10*1000); // 10 sec = 10 * 1000 millisec
inputs.put("Test UUID", UUID.randomUUID()); * inputs.put(DeprecatedHelloWorldPlugin.SLEEP_TIME, sleepTime);
logger.debug("Inputs : {}", inputs); * inputs.put("Test UUID", UUID.randomUUID()); logger.debug("Inputs : {}",
* inputs);
LaunchParameter parameter = new LaunchParameter(DeprecatedHelloWorldPluginDeclaration.class.newInstance(), inputs); *
parameter.setScheduling(null); * LaunchParameter parameter = new
* LaunchParameter(DeprecatedHelloWorldPluginDeclaration.class.newInstance()
SmartExecutorScheduler smartExecutorScheduler = SmartExecutorScheduler.getInstance(); * , inputs); parameter.setScheduling(null);
UUID uuid = smartExecutorScheduler.schedule(parameter); *
logger.debug("Scheduled Job with ID {}", uuid); * SmartExecutorScheduler smartExecutorScheduler =
* SmartExecutorScheduler.getInstance(); UUID uuid =
long startTime = Calendar.getInstance().getTimeInMillis(); * smartExecutorScheduler.schedule(parameter);
long endTime = startTime; * logger.debug("Scheduled Job with ID {}", uuid);
while(endTime <= (startTime + 12000)){ *
endTime = Calendar.getInstance().getTimeInMillis(); * long startTime = Calendar.getInstance().getTimeInMillis(); long endTime =
} * startTime; while(endTime <= (startTime + 12000)){ endTime =
PluginState pluginState = pc.getLastPluginInstanceState(uuid); * Calendar.getInstance().getTimeInMillis(); } PluginState pluginState =
Assert.assertEquals(PluginState.DONE, pluginState); * pc.getLastPluginInstanceState(uuid);
} * Assert.assertEquals(PluginState.DONE, pluginState); }
*/ */
@Test @Test
public void schedulingTest() throws Exception { public void schedulingTest() throws Exception {
UUID uuid = scheduleTest(null, null); UUID uuid = scheduleTest(null, null);
long startTime = Calendar.getInstance().getTimeInMillis(); long startTime = Calendar.getInstance().getTimeInMillis();
long endTime = startTime; long endTime = startTime;
while(endTime <= (startTime + 12000)){ while (endTime <= (startTime + 12000)) {
endTime = Calendar.getInstance().getTimeInMillis(); endTime = Calendar.getInstance().getTimeInMillis();
} }
PluginStateEvolution pluginStateEvolution = pc.getLastPluginInstanceState(uuid); PluginStateEvolution pluginStateEvolution = pc.getLastPluginInstanceState(uuid);
Assert.assertEquals(PluginState.DONE, pluginStateEvolution.getPluginState()); Assert.assertEquals(PluginState.DONE, pluginStateEvolution.getPluginState());
} }
@Test @Test
public void earlyStopTest() throws Exception { public void earlyStopTest() throws Exception {
SmartExecutorScheduler smartExecutorScheduler = SmartExecutorScheduler.getInstance(); SmartExecutorScheduler smartExecutorScheduler = SmartExecutorSchedulerFactory.getSmartExecutorScheduler();
UUID uuid = scheduleTest(null, null); UUID uuid = scheduleTest(null, null);
try { try {
smartExecutorScheduler.stop(uuid, true, false); smartExecutorScheduler.stop(uuid, false);
} catch(UnableToInterruptTaskException e){ } catch (UnableToInterruptTaskException e) {
logger.error("UnableToInterruptTaskException this is the normal behaviour.", e); logger.error("UnableToInterruptTaskException this is the normal behaviour.", e);
return; return;
} }
long startTime = Calendar.getInstance().getTimeInMillis(); long startTime = Calendar.getInstance().getTimeInMillis();
long endTime = startTime; long endTime = startTime;
while(endTime <= (startTime + 12000)){ while (endTime <= (startTime + 12000)) {
endTime = Calendar.getInstance().getTimeInMillis(); endTime = Calendar.getInstance().getTimeInMillis();
} }
try{ try {
PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, 5); PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, 5);
Assert.assertEquals(PluginState.STOPPED, pluginStateEvolution.getPluginState()); Assert.assertEquals(PluginState.STOPPED, pluginStateEvolution.getPluginState());
}catch(PluginStateNotRetrievedException e){ } catch (PluginStateNotRetrievedException e) {
// OK // OK
logger.error("PluginStateNotRetrievedException this can be acceptable in some tests", e); logger.error("PluginStateNotRetrievedException this can be acceptable in some tests", e);
} }
} }
@Test @Test
public void middleStopTest() throws Exception { public void middleStopTest() throws Exception {
UUID uuid = scheduleTest(null, null); UUID uuid = scheduleTest(null, null);
SmartExecutorScheduler smartExecutorScheduler = SmartExecutorScheduler.getInstance(); SmartExecutorScheduler smartExecutorScheduler = SmartExecutorSchedulerFactory.getSmartExecutorScheduler();
long startTime = Calendar.getInstance().getTimeInMillis(); long startTime = Calendar.getInstance().getTimeInMillis();
long endTime = startTime; long endTime = startTime;
while(endTime <= (startTime + 2000)){ while (endTime <= (startTime + 2000)) {
endTime = Calendar.getInstance().getTimeInMillis(); endTime = Calendar.getInstance().getTimeInMillis();
} }
smartExecutorScheduler.stop(uuid, true, false); smartExecutorScheduler.stop(uuid, false);
startTime = Calendar.getInstance().getTimeInMillis(); startTime = Calendar.getInstance().getTimeInMillis();
endTime = startTime; endTime = startTime;
while(endTime <= (startTime + 10000)){ while (endTime <= (startTime + 10000)) {
endTime = Calendar.getInstance().getTimeInMillis(); endTime = Calendar.getInstance().getTimeInMillis();
} }
PluginStateEvolution pluginStateEvolution = pc.getLastPluginInstanceState(uuid); PluginStateEvolution pluginStateEvolution = pc.getLastPluginInstanceState(uuid);
Assert.assertEquals(PluginState.STOPPED, pluginStateEvolution.getPluginState()); Assert.assertEquals(PluginState.STOPPED, pluginStateEvolution.getPluginState());
} }
@Test @Test
public void lateStopTest() throws Exception { public void lateStopTest() throws Exception {
UUID uuid = scheduleTest(null, null); UUID uuid = scheduleTest(null, null);
SmartExecutorScheduler smartExecutorScheduler = SmartExecutorScheduler.getInstance(); SmartExecutorScheduler smartExecutorScheduler = SmartExecutorSchedulerFactory.getSmartExecutorScheduler();
long startTime = Calendar.getInstance().getTimeInMillis(); long startTime = Calendar.getInstance().getTimeInMillis();
long endTime = startTime; long endTime = startTime;
while(endTime <= (startTime + 12000)){ while (endTime <= (startTime + 12000)) {
endTime = Calendar.getInstance().getTimeInMillis(); endTime = Calendar.getInstance().getTimeInMillis();
} }
smartExecutorScheduler.stop(uuid, true, false); smartExecutorScheduler.stop(uuid, false);
PluginStateEvolution pluginStateEvolution = pc.getLastPluginInstanceState(uuid); PluginStateEvolution pluginStateEvolution = pc.getLastPluginInstanceState(uuid);
Assert.assertEquals(PluginState.DONE, pluginStateEvolution.getPluginState()); Assert.assertEquals(PluginState.DONE, pluginStateEvolution.getPluginState());
} }
@Test @Test
public void doubleLaunchfirstStoppedSchedulingTest() throws Exception { public void doubleLaunchfirstStoppedSchedulingTest() throws Exception {
UUID first = scheduleTest(null, null); UUID first = scheduleTest(null, null);
logger.debug("First scheduled id {}", first); logger.debug("First scheduled id {}", first);
UUID second = scheduleTest(null, null); UUID second = scheduleTest(null, null);
logger.debug("Second scheduled id {}", second); logger.debug("Second scheduled id {}", second);
SmartExecutorScheduler smartExecutorScheduler = SmartExecutorScheduler.getInstance(); SmartExecutorScheduler smartExecutorScheduler = SmartExecutorSchedulerFactory.getSmartExecutorScheduler();
long startTime = Calendar.getInstance().getTimeInMillis(); long startTime = Calendar.getInstance().getTimeInMillis();
long endTime = startTime; long endTime = startTime;
while(endTime <= (startTime + 2000)){ while (endTime <= (startTime + 2000)) {
endTime = Calendar.getInstance().getTimeInMillis(); endTime = Calendar.getInstance().getTimeInMillis();
} }
smartExecutorScheduler.stop(first, true, false); smartExecutorScheduler.stop(first, false);
startTime = Calendar.getInstance().getTimeInMillis(); startTime = Calendar.getInstance().getTimeInMillis();
endTime = startTime; endTime = startTime;
while(endTime <= (startTime + 12000)){ while (endTime <= (startTime + 12000)) {
endTime = Calendar.getInstance().getTimeInMillis(); endTime = Calendar.getInstance().getTimeInMillis();
} }
PluginStateEvolution pluginStateEvolution = pc.getLastPluginInstanceState(first); PluginStateEvolution pluginStateEvolution = pc.getLastPluginInstanceState(first);
Assert.assertEquals(PluginState.STOPPED, pluginStateEvolution.getPluginState()); Assert.assertEquals(PluginState.STOPPED, pluginStateEvolution.getPluginState());
pluginStateEvolution = pc.getLastPluginInstanceState(second); pluginStateEvolution = pc.getLastPluginInstanceState(second);
Assert.assertEquals(PluginState.DONE, pluginStateEvolution.getPluginState()); Assert.assertEquals(PluginState.DONE, pluginStateEvolution.getPluginState());
} }
@Test @Test
public void delayed() throws Exception { public void delayed() throws Exception {
Scheduling scheduling = new Scheduling(20); Scheduling scheduling = new Scheduling(20);
UUID uuid = scheduleTest(scheduling, new Long(10 * 1000)); UUID uuid = scheduleTest(scheduling, new Long(10 * 1000));
SmartExecutorScheduler smartExecutorScheduler = SmartExecutorScheduler.getInstance(); SmartExecutorScheduler smartExecutorScheduler = SmartExecutorSchedulerFactory.getSmartExecutorScheduler();
long startTime = Calendar.getInstance().getTimeInMillis(); long startTime = Calendar.getInstance().getTimeInMillis();
long endTime = startTime; long endTime = startTime;
while(endTime <= (startTime + 83 * 1000)){ while (endTime <= (startTime + 83 * 1000)) {
endTime = Calendar.getInstance().getTimeInMillis(); endTime = Calendar.getInstance().getTimeInMillis();
} }
smartExecutorScheduler.stop(uuid, true, false); smartExecutorScheduler.stop(uuid, false);
startTime = Calendar.getInstance().getTimeInMillis(); startTime = Calendar.getInstance().getTimeInMillis();
endTime = startTime; endTime = startTime;
while(endTime <= (startTime + 30 * 1000)){ while (endTime <= (startTime + 30 * 1000)) {
endTime = Calendar.getInstance().getTimeInMillis(); endTime = Calendar.getInstance().getTimeInMillis();
} }
for(int i=1; i<5; i++){ for (int i = 1; i < 5; i++) {
PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, i); PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, i);
Assert.assertEquals(PluginState.DONE, pluginStateEvolution.getPluginState()); Assert.assertEquals(PluginState.DONE, pluginStateEvolution.getPluginState());
} }
PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, 5); PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, 5);
Assert.assertEquals(PluginState.STOPPED, pluginStateEvolution.getPluginState()); Assert.assertEquals(PluginState.STOPPED, pluginStateEvolution.getPluginState());
} }
@Test @Test
public void delayedPreviousMustBeTerminated() throws Exception { public void delayedPreviousMustBeTerminated() throws Exception {
Scheduling scheduling = new Scheduling(20, true); Scheduling scheduling = new Scheduling(20, true);
UUID uuid = scheduleTest(scheduling, new Long(22 * 1000)); UUID uuid = scheduleTest(scheduling, new Long(22 * 1000));
SmartExecutorScheduler smartExecutorScheduler = SmartExecutorScheduler.getInstance(); SmartExecutorScheduler smartExecutorScheduler = SmartExecutorSchedulerFactory.getSmartExecutorScheduler();
long startTime = Calendar.getInstance().getTimeInMillis(); long startTime = Calendar.getInstance().getTimeInMillis();
long endTime = startTime; long endTime = startTime;
while(endTime <= (startTime + 80 * 1000)){ while (endTime <= (startTime + 80 * 1000)) {
endTime = Calendar.getInstance().getTimeInMillis(); endTime = Calendar.getInstance().getTimeInMillis();
} }
smartExecutorScheduler.stop(uuid, true, false); smartExecutorScheduler.stop(uuid, false);
startTime = Calendar.getInstance().getTimeInMillis(); startTime = Calendar.getInstance().getTimeInMillis();
endTime = startTime; endTime = startTime;
while(endTime <= (startTime + 30 * 1000)){ while (endTime <= (startTime + 30 * 1000)) {
endTime = Calendar.getInstance().getTimeInMillis(); endTime = Calendar.getInstance().getTimeInMillis();
} }
for(int i=1; i<5; i++){ for (int i = 1; i < 5; i++) {
PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, i); PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, i);
if(i%2!=0){ if (i % 2 != 0) {
Assert.assertEquals(PluginState.DONE, pluginStateEvolution.getPluginState()); Assert.assertEquals(PluginState.DONE, pluginStateEvolution.getPluginState());
}else{ } else {
Assert.assertEquals(PluginState.DISCARDED, pluginStateEvolution.getPluginState()); Assert.assertEquals(PluginState.DISCARDED, pluginStateEvolution.getPluginState());
} }
} }
PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, 5); PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, 5);
Assert.assertEquals(PluginState.STOPPED, pluginStateEvolution.getPluginState()); Assert.assertEquals(PluginState.STOPPED, pluginStateEvolution.getPluginState());
} }
@Test @Test
public void delayedAllPreviousMustBeTerminated() throws Exception { public void delayedAllPreviousMustBeTerminated() throws Exception {
Scheduling scheduling = new Scheduling(20, true); Scheduling scheduling = new Scheduling(20, true);
UUID uuid = scheduleTest(scheduling, new Long(45 * 1000)); UUID uuid = scheduleTest(scheduling, new Long(45 * 1000));
SmartExecutorScheduler smartExecutorScheduler = SmartExecutorScheduler.getInstance(); SmartExecutorScheduler smartExecutorScheduler = SmartExecutorSchedulerFactory.getSmartExecutorScheduler();
long startTime = Calendar.getInstance().getTimeInMillis(); long startTime = Calendar.getInstance().getTimeInMillis();
long endTime = startTime; long endTime = startTime;
while(endTime <= (startTime + 1.5 * 60 * 1000)){ while (endTime <= (startTime + 1.5 * 60 * 1000)) {
endTime = Calendar.getInstance().getTimeInMillis(); endTime = Calendar.getInstance().getTimeInMillis();
} }
smartExecutorScheduler.stop(uuid, true, false); smartExecutorScheduler.stop(uuid, false);
startTime = Calendar.getInstance().getTimeInMillis(); startTime = Calendar.getInstance().getTimeInMillis();
endTime = startTime; endTime = startTime;
while(endTime <= (startTime + 30 * 1000)){ while (endTime <= (startTime + 30 * 1000)) {
endTime = Calendar.getInstance().getTimeInMillis(); endTime = Calendar.getInstance().getTimeInMillis();
} }
PluginState[] expectedStates = new PluginState[]{ PluginState[] expectedStates = new PluginState[] { PluginState.DONE, PluginState.DISCARDED,
PluginState.DONE, PluginState.DISCARDED, PluginState.STOPPED };
PluginState.DISCARDED,
PluginState.DISCARDED, for (int i = 0; i < expectedStates.length; i++) {
PluginState.STOPPED PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, i + 1);
};
for(int i=0; i<expectedStates.length; i++){
PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, i+1);
Assert.assertEquals(expectedStates[i], pluginStateEvolution.getPluginState()); Assert.assertEquals(expectedStates[i], pluginStateEvolution.getPluginState());
} }
PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, 5); PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, 5);
Assert.assertEquals(PluginState.DISCARDED, pluginStateEvolution.getPluginState()); Assert.assertEquals(PluginState.DISCARDED, pluginStateEvolution.getPluginState());
} }
@Test(expected=PluginStateNotRetrievedException.class) @Test(expected = PluginStateNotRetrievedException.class)
public void delayedExpMaxtimes() throws Exception { public void delayedExpMaxtimes() throws Exception {
Scheduling scheduling = new Scheduling(20, 3); Scheduling scheduling = new Scheduling(20, 3);
UUID uuid = scheduleTest(scheduling, new Long(10 * 1000)); UUID uuid = scheduleTest(scheduling, new Long(10 * 1000));
long startTime = Calendar.getInstance().getTimeInMillis(); long startTime = Calendar.getInstance().getTimeInMillis();
long endTime = startTime; long endTime = startTime;
while(endTime <= (startTime + 1.5 * 60 * 1000)){ while (endTime <= (startTime + 1.5 * 60 * 1000)) {
endTime = Calendar.getInstance().getTimeInMillis(); endTime = Calendar.getInstance().getTimeInMillis();
} }
for(int i=0; i<3; i++){ for (int i = 0; i < 3; i++) {
PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, i+1); PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, i + 1);
Assert.assertEquals(PluginState.DONE, pluginStateEvolution.getPluginState()); Assert.assertEquals(PluginState.DONE, pluginStateEvolution.getPluginState());
} }
pc.getPluginInstanceState(uuid, 4); pc.getPluginInstanceState(uuid, 4);
} }
@Test(expected=PluginStateNotRetrievedException.class) @Test(expected = PluginStateNotRetrievedException.class)
public void delayedExpTimeLimits() throws Exception { public void delayedExpTimeLimits() throws Exception {
Calendar firstStartTime = Calendar.getInstance(); Calendar firstStartTime = Calendar.getInstance();
Calendar stopTime = Calendar.getInstance(); Calendar stopTime = Calendar.getInstance();
stopTime.setTimeInMillis(firstStartTime.getTimeInMillis()); stopTime.setTimeInMillis(firstStartTime.getTimeInMillis());
stopTime.add(Calendar.SECOND, 10); stopTime.add(Calendar.SECOND, 10);
firstStartTime.add(Calendar.SECOND, 5); firstStartTime.add(Calendar.SECOND, 5);
Scheduling scheduling = new Scheduling(20, 0, firstStartTime, stopTime); Scheduling scheduling = new Scheduling(20, 0, firstStartTime, stopTime);
logger.debug("Scheduling : {}", scheduling); logger.debug("Scheduling : {}", scheduling);
UUID uuid = scheduleTest(scheduling, new Long(10 * 1000)); UUID uuid = scheduleTest(scheduling, new Long(10 * 1000));
long startTime = Calendar.getInstance().getTimeInMillis(); long startTime = Calendar.getInstance().getTimeInMillis();
long endTime = startTime; long endTime = startTime;
while(endTime <= (startTime + 30 * 1000)){ while (endTime <= (startTime + 30 * 1000)) {
endTime = Calendar.getInstance().getTimeInMillis(); endTime = Calendar.getInstance().getTimeInMillis();
} }
PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, 1); PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, 1);
Assert.assertEquals(PluginState.DONE, pluginStateEvolution.getPluginState()); Assert.assertEquals(PluginState.DONE, pluginStateEvolution.getPluginState());
pc.getPluginInstanceState(uuid, 2); pc.getPluginInstanceState(uuid, 2);
} }
@Test @Test
public void cronExp() throws Exception { public void cronExp() throws Exception {
CronExpression cronExpression = new CronExpression("0/20 * * ? * *"); CronExpression cronExpression = new CronExpression("0/20 * * ? * *");
Scheduling scheduling = new Scheduling(cronExpression); Scheduling scheduling = new Scheduling(cronExpression);
UUID uuid = scheduleTest(scheduling, new Long(10 * 1000)); UUID uuid = scheduleTest(scheduling, new Long(10 * 1000));
SmartExecutorScheduler smartExecutorScheduler = SmartExecutorScheduler.getInstance(); SmartExecutorScheduler smartExecutorScheduler = SmartExecutorSchedulerFactory.getSmartExecutorScheduler();
long startTime = Calendar.getInstance().getTimeInMillis(); long startTime = Calendar.getInstance().getTimeInMillis();
long endTime = startTime; long endTime = startTime;
while(endTime <= (startTime + 1.5 * 60 * 1000)){ while (endTime <= (startTime + 1.5 * 60 * 1000)) {
endTime = Calendar.getInstance().getTimeInMillis(); endTime = Calendar.getInstance().getTimeInMillis();
} }
smartExecutorScheduler.stop(uuid, true, false); smartExecutorScheduler.stop(uuid, false);
startTime = Calendar.getInstance().getTimeInMillis(); startTime = Calendar.getInstance().getTimeInMillis();
endTime = startTime; endTime = startTime;
while(endTime <= (startTime + 30 * 1000)){ while (endTime <= (startTime + 30 * 1000)) {
endTime = Calendar.getInstance().getTimeInMillis(); endTime = Calendar.getInstance().getTimeInMillis();
} }
for(int i=1; i<5; i++){ for (int i = 1; i < 5; i++) {
PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, i); PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, i);
Assert.assertEquals(PluginState.DONE, pluginStateEvolution.getPluginState()); Assert.assertEquals(PluginState.DONE, pluginStateEvolution.getPluginState());
} }
try{ try {
PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, 5); PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, 5);
Assert.assertEquals(PluginState.STOPPED, pluginStateEvolution.getPluginState()); Assert.assertEquals(PluginState.STOPPED, pluginStateEvolution.getPluginState());
}catch(PluginStateNotRetrievedException e){ } catch (PluginStateNotRetrievedException e) {
// OK // OK
} }
} }
@Test @Test
public void cronExpPreviousMustBeTerminated() throws Exception { public void cronExpPreviousMustBeTerminated() throws Exception {
CronExpression cronExpression = new CronExpression("0/20 * * ? * *"); CronExpression cronExpression = new CronExpression("0/20 * * ? * *");
Scheduling scheduling = new Scheduling(cronExpression, true); Scheduling scheduling = new Scheduling(cronExpression, true);
UUID uuid = scheduleTest(scheduling, new Long(30 * 1000)); UUID uuid = scheduleTest(scheduling, new Long(30 * 1000));
SmartExecutorScheduler smartExecutorScheduler = SmartExecutorScheduler.getInstance(); SmartExecutorScheduler smartExecutorScheduler = SmartExecutorSchedulerFactory.getSmartExecutorScheduler();
long startTime = Calendar.getInstance().getTimeInMillis(); long startTime = Calendar.getInstance().getTimeInMillis();
long endTime = startTime; long endTime = startTime;
while(endTime <= (startTime + 1.5 * 60 * 1000)){ while (endTime <= (startTime + 1.5 * 60 * 1000)) {
endTime = Calendar.getInstance().getTimeInMillis(); endTime = Calendar.getInstance().getTimeInMillis();
} }
smartExecutorScheduler.stop(uuid, true, false); smartExecutorScheduler.stop(uuid, false);
startTime = Calendar.getInstance().getTimeInMillis(); startTime = Calendar.getInstance().getTimeInMillis();
endTime = startTime; endTime = startTime;
while(endTime <= (startTime + 30 * 1000)){ while (endTime <= (startTime + 30 * 1000)) {
endTime = Calendar.getInstance().getTimeInMillis(); endTime = Calendar.getInstance().getTimeInMillis();
} }
for(int i=1; i<5; i++){ for (int i = 1; i < 5; i++) {
PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, i); PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, i);
if(i%2!=0){ if (i % 2 != 0) {
Assert.assertEquals(PluginState.DONE, pluginStateEvolution.getPluginState()); Assert.assertEquals(PluginState.DONE, pluginStateEvolution.getPluginState());
}else{ } else {
Assert.assertEquals(PluginState.DISCARDED, pluginStateEvolution.getPluginState()); Assert.assertEquals(PluginState.DISCARDED, pluginStateEvolution.getPluginState());
} }
} }
try{ try {
PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, 5); PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, 5);
Assert.assertEquals(PluginState.STOPPED, pluginStateEvolution.getPluginState()); Assert.assertEquals(PluginState.STOPPED, pluginStateEvolution.getPluginState());
}catch(PluginStateNotRetrievedException e){ } catch (PluginStateNotRetrievedException e) {
// OK // OK
} }
} }
@Test @Test
public void cronExpAllPreviousTerminated() throws Exception { public void cronExpAllPreviousTerminated() throws Exception {
CronExpression cronExpression = new CronExpression("0/20 * * ? * *"); CronExpression cronExpression = new CronExpression("0/20 * * ? * *");
Scheduling scheduling = new Scheduling(cronExpression, true); Scheduling scheduling = new Scheduling(cronExpression, true);
UUID uuid = scheduleTest(scheduling, new Long(45 * 1000)); UUID uuid = scheduleTest(scheduling, new Long(45 * 1000));
SmartExecutorScheduler smartExecutorScheduler = SmartExecutorScheduler.getInstance(); SmartExecutorScheduler smartExecutorScheduler = SmartExecutorSchedulerFactory.getSmartExecutorScheduler();
long startTime = Calendar.getInstance().getTimeInMillis(); long startTime = Calendar.getInstance().getTimeInMillis();
long endTime = startTime; long endTime = startTime;
while(endTime <= (startTime + 1.5 * 60 * 1000)){ while (endTime <= (startTime + 1.5 * 60 * 1000)) {
endTime = Calendar.getInstance().getTimeInMillis(); endTime = Calendar.getInstance().getTimeInMillis();
} }
smartExecutorScheduler.stop(uuid, true, false); smartExecutorScheduler.stop(uuid, false);
startTime = Calendar.getInstance().getTimeInMillis(); startTime = Calendar.getInstance().getTimeInMillis();
endTime = startTime; endTime = startTime;
while(endTime <= (startTime + 20 * 1000)){ while (endTime <= (startTime + 20 * 1000)) {
endTime = Calendar.getInstance().getTimeInMillis(); endTime = Calendar.getInstance().getTimeInMillis();
} }
PluginState[] expectedStates = new PluginState[]{ PluginState[] expectedStates = new PluginState[] { PluginState.DONE, PluginState.DISCARDED,
PluginState.DONE, PluginState.DISCARDED, PluginState.STOPPED };
PluginState.DISCARDED,
PluginState.DISCARDED,
PluginState.STOPPED
};
for (int i = 0; i < expectedStates.length; i++) {
for(int i=0; i<expectedStates.length; i++){ PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, i + 1);
PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, i+1);
Assert.assertEquals(expectedStates[i], pluginStateEvolution.getPluginState()); Assert.assertEquals(expectedStates[i], pluginStateEvolution.getPluginState());
} }
try{ try {
PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, 5); PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, 5);
Assert.assertEquals(PluginState.DISCARDED, pluginStateEvolution.getPluginState()); Assert.assertEquals(PluginState.DISCARDED, pluginStateEvolution.getPluginState());
}catch(PluginStateNotRetrievedException e){ } catch (PluginStateNotRetrievedException e) {
// OK // OK
} }
} }
@Test(expected=PluginStateNotRetrievedException.class) @Test(expected = PluginStateNotRetrievedException.class)
public void cronExpMaxtimes() throws Exception { public void cronExpMaxtimes() throws Exception {
CronExpression cronExpression = new CronExpression("0/20 * * ? * *"); CronExpression cronExpression = new CronExpression("0/20 * * ? * *");
Scheduling scheduling = new Scheduling(cronExpression, 3); Scheduling scheduling = new Scheduling(cronExpression, 3);
UUID uuid = scheduleTest(scheduling, new Long(10 * 1000)); UUID uuid = scheduleTest(scheduling, new Long(10 * 1000));
long startTime = Calendar.getInstance().getTimeInMillis(); long startTime = Calendar.getInstance().getTimeInMillis();
long endTime = startTime; long endTime = startTime;
while(endTime <= (startTime + 1.5 * 60 * 1000)){ while (endTime <= (startTime + 1.5 * 60 * 1000)) {
endTime = Calendar.getInstance().getTimeInMillis(); endTime = Calendar.getInstance().getTimeInMillis();
} }
for(int i=0; i<3; i++){ for (int i = 0; i < 3; i++) {
PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, i+1); PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, i + 1);
Assert.assertEquals(PluginState.DONE, pluginStateEvolution.getPluginState()); Assert.assertEquals(PluginState.DONE, pluginStateEvolution.getPluginState());
} }
pc.getPluginInstanceState(uuid, 4); pc.getPluginInstanceState(uuid, 4);
} }
@Test(expected=PluginStateNotRetrievedException.class) @Test(expected = PluginStateNotRetrievedException.class)
public void cronExpTimeLimits() throws Exception { public void cronExpTimeLimits() throws Exception {
CronExpression cronExpression = new CronExpression("0/20 * * ? * *"); CronExpression cronExpression = new CronExpression("0/20 * * ? * *");
Calendar firstStartTime = Calendar.getInstance(); Calendar firstStartTime = Calendar.getInstance();
Calendar stopTime = Calendar.getInstance(); Calendar stopTime = Calendar.getInstance();
stopTime.setTimeInMillis(firstStartTime.getTimeInMillis()); stopTime.setTimeInMillis(firstStartTime.getTimeInMillis());
stopTime.add(Calendar.SECOND, 20); stopTime.add(Calendar.SECOND, 20);
Scheduling scheduling = new Scheduling(cronExpression, 0, firstStartTime, stopTime); Scheduling scheduling = new Scheduling(cronExpression, 0, firstStartTime, stopTime);
logger.debug("Scheduling : {}", scheduling); logger.debug("Scheduling : {}", scheduling);
UUID uuid = scheduleTest(scheduling, new Long(10 * 1000)); UUID uuid = scheduleTest(scheduling, new Long(10 * 1000));
long startTime = Calendar.getInstance().getTimeInMillis(); long startTime = Calendar.getInstance().getTimeInMillis();
long endTime = startTime; long endTime = startTime;
while(endTime <= (startTime + 30 * 1000)){ while (endTime <= (startTime + 30 * 1000)) {
endTime = Calendar.getInstance().getTimeInMillis(); endTime = Calendar.getInstance().getTimeInMillis();
} }
PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, 1); PluginStateEvolution pluginStateEvolution = pc.getPluginInstanceState(uuid, 1);
Assert.assertEquals(PluginState.DONE, pluginStateEvolution.getPluginState()); Assert.assertEquals(PluginState.DONE, pluginStateEvolution.getPluginState());
pc.getPluginInstanceState(uuid, 2); pc.getPluginInstanceState(uuid, 2);
} }
} }