refs #2032: Create IS Sweeper as SmartExecutor Plugin

https://support.d4science.org/issues/2032

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/is-sweeper-se-plugin@122685 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-02-01 15:47:39 +00:00
parent d383924669
commit aa1acd0e82
6 changed files with 208 additions and 6 deletions

25
pom.xml
View File

@ -54,6 +54,26 @@
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.portlets.admin</groupId>
<artifactId>rmp-common-library</artifactId>
<version>[2.6.0-SNAPSHOT,3.0.0-SNAPSHOT)</version>
<exclusions>
<exclusion>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-user</artifactId>
</exclusion>
<exclusion>
<groupId>com.sencha.gxt</groupId>
<artifactId>gxt2.2.5-gwt2.X</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-scope</artifactId>
<scope>provided</scope>
</dependency>
<!-- Test dependencies -->
<dependency>
@ -61,11 +81,6 @@
<artifactId>common-encryption</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-scope</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.gcube.vremanagement</groupId>
<artifactId>smart-executor-client</artifactId>

View File

@ -1,7 +1,15 @@
package org.gcube.informationsystem.sweeper;
import java.util.List;
import java.util.Map;
import org.gcube.common.resources.gcore.HostingNode;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.resourcemanagement.support.server.managers.resources.GHNManager;
import org.gcube.resourcemanagement.support.server.managers.resources.RunningInstanceManager;
import org.gcube.resourcemanagement.support.server.sweeper.Sweeper;
import org.gcube.resourcemanagement.support.shared.util.SweeperActions;
import org.gcube.vremanagement.executor.plugin.Plugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -21,11 +29,70 @@ public class ISSweeperPlugin extends Plugin<ISSweeperPluginDeclaration> {
logger.debug("contructor");
}
public static String getTagValue(String xml, String tagName){
return xml.split("<"+tagName+">")[1].split("</"+tagName+">")[0];
}
@SuppressWarnings("deprecation")
public void applySweep(ScopeBean scopeBean, List<String> elements) {
for (String element : elements) {
try {
String elementID = getTagValue(element, "ID");
SweeperActions action = SweeperActions.valueOf(getTagValue(element, "Actions"));
logger.info("Cleaning up {} {}", elementID, action);
switch(action) {
case APPLY_GHN_DELETE:
GHNManager manager = new GHNManager(elementID);
manager.forceDelete(scopeBean);
break;
case APPLY_GHN_MOVE_TO_UNREACHABLE:
GHNManager ghnManager = new GHNManager(elementID);
HostingNode res = (HostingNode) ghnManager.getResource(scopeBean);
logger.trace("Setting {} (ID : {}) status to unreachable",
HostingNode.class.getSimpleName(), elementID);
res.profile().description().status("unreachable");
HostingNode hn = ghnManager.getRegistryPublisher().update(res);
logger.trace("{} (ID : {}) get after update {}",
HostingNode.class.getSimpleName(), elementID, hn);
break;
case APPLY_RI_DELETE:
RunningInstanceManager riManager = new RunningInstanceManager(elementID);
logger.trace("RunningInstance with ID {} will be deleted");
riManager.forceDelete(scopeBean);
break;
default:
}
} catch (Exception e) {
logger.error("Error cleaning {}", element, e);
}
}
}
/**{@inheritDoc}*/
@Override
public void launch(Map<String, Object> inputs) throws Exception {
logger.debug("Launching {} execution", ISSweeperPluginDeclaration.NAME);
// No inputs needed
String scope = ScopeProvider.instance.get();
ScopeBean scopeBean = new ScopeBean(scope);
Sweeper sweeper = new Sweeper();
List<String> expiredGHNs = sweeper.getExpiredGHNs(scopeBean);
logger.trace("Expired GHNs : {}", expiredGHNs);
applySweep(scopeBean, expiredGHNs);
List<String> deadGHNs = sweeper.getDeadGHNs(scopeBean);
logger.trace("Dead GHNs : {}", deadGHNs);
applySweep(scopeBean, deadGHNs);
List<String> orphanRIs = sweeper.getOrphanRI(scopeBean);
logger.trace("Orphan Running Instances : {}", orphanRIs);
applySweep(scopeBean, orphanRIs);
logger.debug("{} execution finished", ISSweeperPluginDeclaration.NAME);
}

View File

@ -1 +1 @@
org.gcube.vremanagement.smartexecutor.sweeper.ScheduledTaskSweeperPluginDeclaration
org.gcube.informationsystem.sweeper.ISSweeperPluginDeclaration

View File

@ -0,0 +1,36 @@
package org.gcube.informationsystem.sweeper;
import java.util.HashMap;
import java.util.Map;
import org.gcube.common.scope.api.ScopeProvider;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*/
public class ISSweeperPluginTest {
/**
* Logger
*/
private static Logger logger = LoggerFactory.getLogger(ISSweeperPluginTest.class);
@Before
public void beforeTest(){
ScopeProvider.instance.set("/gcube/devsec");
}
@Test
public void testLaunch() throws Exception {
logger.debug("Starting to test launch");
Map<String, Object> inputs = new HashMap<String, Object>();
ISSweeperPlugin couchDBQueryPlugin = new ISSweeperPlugin(null);
couchDBQueryPlugin.launch(inputs);
logger.debug("-------------- launch test finished");
}
}

View File

@ -0,0 +1,68 @@
/**
*
*/
package org.gcube.informationsystem.sweeper;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.vremanagement.executor.api.types.LaunchParameter;
import org.gcube.vremanagement.executor.api.types.Scheduling;
import org.gcube.vremanagement.executor.client.plugins.ExecutorPlugin;
import org.gcube.vremanagement.executor.client.proxies.SmartExecutorProxy;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.quartz.CronExpression;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*/
public class SmartExecutorSchedulerTest {
private static Logger logger = LoggerFactory.getLogger(SmartExecutorSchedulerTest.class);
public static final String START = "START";
public static final String END = "END";
public static final String SCOPE = "";
private SmartExecutorProxy proxy;
//@Before
public void before() throws Exception{
ScopeProvider.instance.set(SCOPE);
proxy = ExecutorPlugin.getExecutorProxy(ISSweeperPluginDeclaration.NAME).build();
Assert.assertNotNull(proxy);
}
public UUID scheduleTest(Scheduling scheduling, Long sleepTime) throws Exception {
Map<String, Object> inputs = new HashMap<String, Object>();
logger.debug("Inputs : {}", inputs);
LaunchParameter parameter = new LaunchParameter(ISSweeperPluginDeclaration.NAME, inputs);
parameter.setScheduling(scheduling);
String uuidString = proxy.launch(parameter);
return UUID.fromString(uuidString);
}
//@Test
public void cronExpPreviousMustBeTerminated() throws Exception {
CronExpression cronExpression = new CronExpression("0 */10 * * * ?"); // every 10 minutes starting from now
Scheduling scheduling = new Scheduling(cronExpression, true);
scheduling.setGlobal(true);
UUID uuid = scheduleTest(scheduling, new Long(1000*60)); // 1 min
logger.debug("Launched with UUID : {}", uuid);
}
//@Test
public void unSchedule() throws Exception {
proxy.unSchedule(null, true);
}
}

View File

@ -0,0 +1,16 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{0}: %msg%n</pattern>
</encoder>
</appender>
<logger name="org.gcube" level="TRACE" />
<root level="WARN">
<appender-ref ref="STDOUT" />
</root>
</configuration>