Added unpublish of old ServiceEndpoint published by current ghn which are still on IS

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/vre-management/smart-executor@111939 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2015-02-12 13:40:41 +00:00
parent 7fcda68c0a
commit b096f0145c
1 changed files with 128 additions and 40 deletions

View File

@ -25,9 +25,14 @@ import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
import org.gcube.common.resources.gcore.ServiceEndpoint.Runtime; import org.gcube.common.resources.gcore.ServiceEndpoint.Runtime;
import org.gcube.common.resources.gcore.common.Platform; import org.gcube.common.resources.gcore.common.Platform;
import org.gcube.common.resources.gcore.utils.Group; import org.gcube.common.resources.gcore.utils.Group;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.informationsystem.publisher.RegistryPublisherFactory; import org.gcube.informationsystem.publisher.RegistryPublisherFactory;
import org.gcube.informationsystem.publisher.ScopedPublisher; import org.gcube.informationsystem.publisher.ScopedPublisher;
import org.gcube.informationsystem.publisher.exception.RegistryNotFoundException; 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.container.ContainerConfiguration;
import org.gcube.smartgears.context.application.ApplicationContext; import org.gcube.smartgears.context.application.ApplicationContext;
import org.gcube.smartgears.handlers.application.ApplicationLifecycleEvent; import org.gcube.smartgears.handlers.application.ApplicationLifecycleEvent;
import org.gcube.smartgears.handlers.application.ApplicationLifecycleHandler; import org.gcube.smartgears.handlers.application.ApplicationLifecycleHandler;
@ -119,7 +124,7 @@ public class SmartExecutorInitalizator extends ApplicationLifecycleHandler {
ScopedPublisher scopedPublisher = RegistryPublisherFactory.scopedPublisher(); ScopedPublisher scopedPublisher = RegistryPublisherFactory.scopedPublisher();
try { try {
logger.debug(String.format("Trying to publish\n%s", stringWriter.toString())); logger.debug("Trying to publish\n{} to {}", stringWriter.toString(), scopes);
scopedPublisher.create(resource, scopes); scopedPublisher.create(resource, scopes);
} catch (RegistryNotFoundException e) { } catch (RegistryNotFoundException e) {
logger.error("The resource was not published", e); logger.error("The resource was not published", e);
@ -139,17 +144,63 @@ public class SmartExecutorInitalizator extends ApplicationLifecycleHandler {
ScopedPublisher scopedPublisher = RegistryPublisherFactory.scopedPublisher(); ScopedPublisher scopedPublisher = RegistryPublisherFactory.scopedPublisher();
try { try {
logger.debug(String.format("Trying to unpublish\n%s", stringWriter.toString())); logger.debug("Trying to unpublish\n{} from {}", stringWriter, scopes);
scopedPublisher.remove(resource, scopes); scopedPublisher.remove(resource, scopes);
logger.debug("Unpublish request executed successfully");
} catch (RegistryNotFoundException e) { } catch (RegistryNotFoundException e) {
logger.error("The resource was not unpublished", e); logger.error("The resource was not unpublished", e);
throw e; throw e;
} }
} }
/**
* 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; 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 * Create the Service Endpoint using information related to discovered
* available plugins and their own discoverd capabilities * available plugins and their own discovered capabilities
* @return the created {@link ServiceEndpoint} * @return the created {@link ServiceEndpoint}
*/ */
protected static ServiceEndpoint createServiceEndpoint(){ protected static ServiceEndpoint createServiceEndpoint(){
@ -161,19 +212,22 @@ public class SmartExecutorInitalizator extends ApplicationLifecycleHandler {
Profile profile = serviceEndpoint.newProfile(); Profile profile = serviceEndpoint.newProfile();
profile.category(ctx.configuration().serviceClass()); profile.category(ctx.configuration().serviceClass());
profile.name(ctx.configuration().name()); profile.name(ctx.configuration().name());
profile.version(ctx.configuration().version()); String version = ctx.configuration().version();
profile.version(version);
profile.description(ctx.configuration().description()); profile.description(ctx.configuration().description());
String runningOn = getRunningOn(ctx.container().configuration());
Platform platform = profile.newPlatform(); Platform platform = profile.newPlatform();
platform.name(runningOn);
ctx.container().configuration().site(); short[] versionSlices = getVersionSlice(version, 4);
platform.version(versionSlices[0]);
platform.name(ctx.container().configuration().hostname()); platform.minorVersion(versionSlices[1]);
short version = 1; platform.buildVersion(versionSlices[2]);
platform.version(version); platform.revisionVersion(versionSlices[3]);
Runtime runtime = profile.newRuntime(); Runtime runtime = profile.newRuntime();
runtime.hostedOn(ctx.container().configuration().hostname()); runtime.hostedOn(runningOn);
runtime.status(ctx.configuration().mode().toString()); runtime.status(ctx.configuration().mode().toString());
Group<AccessPoint> accessPoints = profile.accessPoints(); Group<AccessPoint> accessPoints = profile.accessPoints();
@ -204,7 +258,7 @@ public class SmartExecutorInitalizator extends ApplicationLifecycleHandler {
StringWriter stringWriter = new StringWriter(); StringWriter stringWriter = new StringWriter();
Resources.marshal(serviceEndpoint, stringWriter); Resources.marshal(serviceEndpoint, stringWriter);
logger.debug(String.format("The created ServiceEndpoint profile is \n%s", stringWriter.toString())); logger.debug("The created ServiceEndpoint profile is\n{}", stringWriter.toString());
return serviceEndpoint; return serviceEndpoint;
} }
@ -215,9 +269,9 @@ public class SmartExecutorInitalizator extends ApplicationLifecycleHandler {
Set<String> scopes; Set<String> scopes;
if(applicationScopes==null || applicationScopes.isEmpty()){ if(applicationScopes==null || applicationScopes.isEmpty()){
scopes = containerScopes; scopes = containerScopes;
logger.debug(String.format("Application Scopes (%s). The Container Scopes (%s) will be used.", applicationScopes, scopes)); logger.debug("Application Scopes ({}). The Container Scopes ({}) will be used.", applicationScopes, scopes);
} else{ } else{
logger.debug(String.format("Container Scopes (%s). Application Scopes (%s) will be used.", containerScopes, applicationScopes)); logger.debug("Container Scopes ({}). Application Scopes ({}) will be used.", containerScopes, applicationScopes);
scopes = new HashSet<String>(applicationScopes); scopes = new HashSet<String>(applicationScopes);
} }
return new ArrayList<String>(scopes); return new ArrayList<String>(scopes);
@ -232,9 +286,10 @@ public class SmartExecutorInitalizator extends ApplicationLifecycleHandler {
*/ */
@Override @Override
public void onStart(ApplicationLifecycleEvent.Start applicationLifecycleEventStart) { public void onStart(ApplicationLifecycleEvent.Start applicationLifecycleEventStart) {
logger.debug("-------------------------------------------------------"); logger.debug(
logger.debug("Smart Executor is Starting"); "\n-------------------------------------------------------\n"
logger.debug("-------------------------------------------------------"); + "Smart Executor is Starting\n"
+ "-------------------------------------------------------");
ctx = applicationLifecycleEventStart.context(); ctx = applicationLifecycleEventStart.context();
@ -243,15 +298,48 @@ public class SmartExecutorInitalizator extends ApplicationLifecycleHandler {
serviceEndpoint = createServiceEndpoint(); serviceEndpoint = createServiceEndpoint();
pluginInstances = new HashMap<UUID, PluginThread<Plugin<? extends PluginDeclaration>>>(); pluginInstances = new HashMap<UUID, PluginThread<Plugin<? extends PluginDeclaration>>>();
// TODO Before publishing the new Resource check if on IS there is // checking if there are old unpublished ServiceEndpoint related to this ghn
// an old published ServiceEndpoint resource that where not unpublished // and try to unpublish them
// correctly List<String> scopes = getScopes(ctx);
for(String scope : scopes){
try {
ScopeProvider.instance.set(scope);
SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class);
query.addCondition(String.format("$resource/Profile/Category/text() eq '%s'", ctx.configuration().serviceClass()));
query.addCondition(String.format("$resource/Profile/Name/text() eq '%s'", ctx.configuration().name()));
query.addCondition(String.format("$resource/Profile/RunTime/HostedOn/text() eq '%s'", getRunningOn(ctx.container().configuration())));
query.setResult("$resource");
DiscoveryClient<ServiceEndpoint> client = ICFactory.clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> serviceEndpoints = client.submit(query);
for (ServiceEndpoint serviceEndpoint : serviceEndpoints) {
try {
logger.debug("Trying to unpublish the old ServiceEndpoint with ID {} from scope {}",
serviceEndpoint.id(), scope);
List<String> undeployScopes = new ArrayList<String>();
undeployScopes.add(scope);
unPublishScopedResource(serviceEndpoint, undeployScopes);
} catch(Exception e){
logger.debug("Exception tryng to unpublish the old ServiceEndpoint with ID {} from scope {}",
serviceEndpoint.id(), scope, e);
}
}
}catch(Exception e){
logger.debug("An Exception occur while checking and/or unpublishing old ServiceEndpoint", e);
}
}
// TODO set task that are still on running state on DB to have a clear // TODO set task that are still on running state on DB to have a clear
// room // room
try { try {
publishScopedResource(serviceEndpoint, getScopes(ctx)); publishScopedResource(serviceEndpoint, scopes);
} catch (RegistryNotFoundException e) { } catch (RegistryNotFoundException e) {
logger.error("Unable to Create ServiceEndpoint. the Service will be aborted", e); logger.error("Unable to Create ServiceEndpoint. the Service will be aborted", e);
return; return;
@ -267,9 +355,10 @@ public class SmartExecutorInitalizator extends ApplicationLifecycleHandler {
return; return;
} }
logger.debug("-------------------------------------------------------"); logger.debug(
logger.debug("Smart Executor Started Successfully"); "\n-------------------------------------------------------\n"
logger.debug("-------------------------------------------------------"); + "Smart Executor Started Successfully\n"
+ "-------------------------------------------------------");
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@ -280,9 +369,10 @@ public class SmartExecutorInitalizator extends ApplicationLifecycleHandler {
*/ */
@Override @Override
public void onStop(ApplicationLifecycleEvent.Stop applicationLifecycleEventStop) { public void onStop(ApplicationLifecycleEvent.Stop applicationLifecycleEventStop) {
logger.debug("-------------------------------------------------------"); logger.debug(
logger.debug("Smart Executor is Stopping"); "\n-------------------------------------------------------\n"
logger.debug("-------------------------------------------------------"); + "Smart Executor is Stopping\n"
+ "-------------------------------------------------------");
for(UUID uuid : pluginInstances.keySet()){ for(UUID uuid : pluginInstances.keySet()){
@ -291,25 +381,21 @@ public class SmartExecutorInitalizator extends ApplicationLifecycleHandler {
Plugin<? extends PluginDeclaration> pluginInstace = Plugin<? extends PluginDeclaration> pluginInstace =
pluginThread.getPlugin(); pluginThread.getPlugin();
try { try {
logger.debug(String.format("Requesting Stop to plugin instance " logger.debug("Requesting Stop to plugin instance identified by the UUID %s of Plugin named {}",
+ "identified by the UUID %s of Plugin named %s", uuid, uuid, pluginInstace.getPluginDeclaration().getName());
pluginInstace.getPluginDeclaration().getName()));
pluginInstace.stop(); pluginInstace.stop();
logger.debug(String.format("Plugin instance identified by the" logger.debug("Plugin instance identified by the UUID {} of Plugin named {} stopped coorectly itself",
+ "UUID %s of Plugin named %s stopped coorectly itself", uuid, pluginInstace.getPluginDeclaration().getName());
uuid, pluginInstace.getPluginDeclaration().getName()));
} catch (Exception e) { } catch (Exception e) {
logger.debug(String.format("Running plugin instance identified " logger.debug("Running plugin instance identified by the UUID {} of Plugin named {} failed to request of being stopped",
+ "by the UUID %s of Plugin named %s failed to request " uuid, pluginInstace.getPluginDeclaration().getName());
+ "of being stopped", uuid,
pluginInstace.getPluginDeclaration().getName()));
} finally { } finally {
pluginInstace.setState(PluginState.SUSPENDED); pluginInstace.setState(PluginState.SUSPENDED);
pluginInstances.remove(uuid); pluginInstances.remove(uuid);
} }
} }
// Forcing shutdown of all threads // Trying to force shutdown of all threads
pool.shutdown(); pool.shutdown();
try { try {
@ -326,8 +412,10 @@ public class SmartExecutorInitalizator extends ApplicationLifecycleHandler {
logger.error("Unable to close Persistence", e); logger.error("Unable to close Persistence", e);
} }
logger.debug("-------------------------------------------------------"); logger.debug(
logger.debug("Smart Executor Stopped Successfully"); "\n-------------------------------------------------------\n"
logger.debug("-------------------------------------------------------"); + "Smart Executor Stopped Successfully\n"
+ "-------------------------------------------------------");
} }
} }