Fixing handler

This commit is contained in:
Luca Frosini 2022-06-08 17:42:47 +02:00
parent 7077f0ffb8
commit 721e69034b
6 changed files with 90 additions and 62 deletions

View File

@ -1,8 +1,5 @@
package org.gcube.smartgears.handler.resourceregistry;
import org.gcube.smartgears.handler.resourceregistry.resourcemanager.EServiceManager;
import org.gcube.smartgears.handler.resourceregistry.resourcemanager.HostingNodeManager;
/**
* Library-wide constants.
* @author Luca Frosini
@ -10,12 +7,12 @@ import org.gcube.smartgears.handler.resourceregistry.resourcemanager.HostingNode
*/
public class Constants {
public static final String HOSTING_NODE_MANAGER_PROPERTY = HostingNodeManager.class.getSimpleName();
/**
* The name of the context property that contains the EService Resource.
*/
public static final String ESERVICE_MANAGER_PROPERTY = EServiceManager.class.getSimpleName();
// public static final String HOSTING_NODE_MANAGER_PROPERT = HostingNodeManager.class.getSimpleName();
//
// /**
// * The name of the context property that contains the EService Resource.
// */
// public static final String ESERVICE_MANAGER_PROPERTY = EServiceManager.class.getSimpleName();
/**
* The configuration name of {@link EServiceHandler} and {@link HostingNodeHandler}.

View File

@ -48,6 +48,13 @@ public class ContextUtility {
return getContextName(token);
}
public static UUID getContextUUID(String token) throws ResourceRegistryException {
ContextCache contextCache = ContextCache.getInstance();
String contextFullName = getContextName(token);
UUID contextUUID = contextCache.getUUIDByFullName(contextFullName);
return contextUUID;
}
public static String getContextName(String token) {
try {
return authorizationProxy.get(token).getContext();

View File

@ -23,7 +23,6 @@ import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
import org.gcube.resourcemanagement.model.reference.entities.facets.StateFacet;
import org.gcube.resourcemanagement.model.reference.entities.resources.EService;
import org.gcube.smartgears.context.Property;
import org.gcube.smartgears.context.application.ApplicationContext;
import org.gcube.smartgears.handler.resourceregistry.resourcemanager.EServiceManager;
import org.gcube.smartgears.handlers.application.ApplicationLifecycleEvent.Start;
@ -100,27 +99,31 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
try {
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
boolean create = true;
Set<String> startTokens = applicationContext.configuration().startTokens();
Set<UUID> startContextsUUID = new HashSet<>();
ContextCache contextCache = ContextCache.getInstance();
for (String token : startTokens) {
try {
if (create) {
ContextUtility.setContextFromToken(token);
eServiceManager = new EServiceManager(applicationContext);
eServiceManager.createEService();
applicationContext.properties()
.add(new Property(Constants.ESERVICE_MANAGER_PROPERTY, eServiceManager));
create = false;
} else {
eServiceManager.addToContext();
if (create) {
ContextUtility.setContextFromToken(token);
eServiceManager = new EServiceManager(applicationContext);
eServiceManager.createEService();
// applicationContext.properties()
// .add(new Property(Constants.ESERVICE_MANAGER_PROPERTY, eServiceManager));
create = false;
} else {
try {
UUID contextUUID = ContextUtility.getContextUUID(token);
eServiceManager.addToContext(contextUUID);
} catch (Exception e) {
UUID uuid = UUID.fromString(applicationContext.id());
logger.error("Unable to add {} with UUID {} to current context ({})", EService.NAME, uuid,
ContextUtility.getContextName(token), e);
}
} catch (Exception e) {
UUID uuid = UUID.fromString(applicationContext.id());
logger.error("Unable to add {} with UUID {} to current context ({})", EService.NAME, uuid,
ContextUtility.getContextName(token), e);
}
String contextFullName = ContextUtility.getContextName(token);
UUID contextUUID = contextCache.getUUIDByFullName(contextFullName);
startContextsUUID.add(contextUUID);
@ -171,7 +174,7 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
try {
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
ContextUtility.setContextFromToken(token);
eServiceManager.addToContext();
eServiceManager.addToCurrentContext();
} catch (Exception e) {
logger.error("Failed to add {} to current context ({})", EService.NAME,
ContextUtility.getCurrentContextName(), e);

View File

@ -31,7 +31,6 @@ import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.resourcemanagement.model.reference.entities.facets.StateFacet;
import org.gcube.resourcemanagement.model.reference.entities.resources.HostingNode;
import org.gcube.smartgears.context.Property;
import org.gcube.smartgears.context.container.ContainerContext;
import org.gcube.smartgears.handler.resourceregistry.resourcemanager.HostingNodeManager;
import org.gcube.smartgears.handlers.container.ContainerHandler;
@ -106,24 +105,24 @@ public class HostingNodeHandler extends ContainerHandler {
Set<UUID> startContextsUUID = new HashSet<>();
ContextCache contextCache = ContextCache.getInstance();
for (String token : startTokens) {
try {
if (create) {
ContextUtility.setContextFromToken(token);
hostingNodeManager = new HostingNodeManager(containerContext);
hostingNodeManager.createHostingNode();
containerContext.properties()
.add(new Property(Constants.HOSTING_NODE_MANAGER_PROPERTY, hostingNodeManager));
create = false;
} else {
hostingNodeManager.addToContext();
if (create) {
ContextUtility.setContextFromToken(token);
hostingNodeManager = new HostingNodeManager(containerContext);
hostingNodeManager.createHostingNode();
// containerContext.properties()
// .add(new Property(Constants.HOSTING_NODE_MANAGER_PROPERTY, hostingNodeManager));
create = false;
} else {
try {
UUID contextUUID = ContextUtility.getContextUUID(token);
hostingNodeManager.addToContext(contextUUID);
} catch (Exception e) {
UUID uuid = UUID.fromString(containerContext.id());
logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid,
ContextUtility.getContextName(token), e);
}
} catch (Exception e) {
UUID uuid = UUID.fromString(containerContext.id());
logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid,
ContextUtility.getContextName(token), e);
}
String contextFullName = ContextUtility.getContextName(token);
UUID contextUUID = contextCache.getUUIDByFullName(contextFullName);
startContextsUUID.add(contextUUID);
@ -182,7 +181,7 @@ public class HostingNodeHandler extends ContainerHandler {
}
try {
Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader());
hostingNodeManager.addToContext();
hostingNodeManager.addToCurrentContext();
} catch (Exception e) {
logger.error("Failed to update Service State", e);
} finally {

View File

@ -87,7 +87,7 @@ public class EServiceManager {
}
}
public void addToContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
public void addToCurrentContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
UUID uuid = UUID.fromString(applicationContext.container().id());
try {
resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false);
@ -96,6 +96,16 @@ public class EServiceManager {
logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName(), e);
}
}
public void addToContext(UUID contextUUID) throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
UUID uuid = UUID.fromString(applicationContext.container().id());
try {
resourceRegistryPublisher.addToContext(HostingNode.NAME, uuid, contextUUID, false);
logger.info("{} with UUID {} successfully added to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName());
} catch (Exception e) {
logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName(), e);
}
}
public void removeFromContext()
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
@ -216,20 +226,20 @@ public class EServiceManager {
createActivatesRelation(eService);
stateFacet = eService.getFacets(StateFacet.class).get(0);
} catch (AvailableInAnotherContextException e) {
addToContext();
try {
eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID);
} catch (AvailableInAnotherContextException ex) {
addEServiceToCurrentContext();
eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID);
// addToContext() is executed on HostingNode.
// If the EService is still not available we need to create activates
// relation because does not exists otherwise the EService should
// already be in the context due to propagation constraint.
createActivatesRelation(eService);
}
stateFacet = eService.getFacets(StateFacet.class).get(0);
updateServiceStateFacet();
// addToContext();
// try {
// eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID);
// } catch (AvailableInAnotherContextException ex) {
// addEServiceToCurrentContext();
// eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID);
// // addToContext() is executed on HostingNode.
// // If the EService is still not available we need to create activates
// // relation because does not exists otherwise the EService should
// // already be in the context due to propagation constraint.
// createActivatesRelation(eService);
// }
// stateFacet = eService.getFacets(StateFacet.class).get(0);
// updateServiceStateFacet();
} catch (ResourceRegistryException e) {
throw e;
}

View File

@ -110,7 +110,7 @@ public class HostingNodeManager {
return hostingNode;
}
public void addToContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
public void addToCurrentContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
UUID uuid = UUID.fromString(containerContext.id());
try {
resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false);
@ -119,6 +119,16 @@ public class HostingNodeManager {
logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName(), e);
}
}
public void addToContext(UUID contextUUID) throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
UUID uuid = UUID.fromString(containerContext.id());
try {
resourceRegistryPublisher.addToContext(HostingNode.NAME, uuid, contextUUID, false);
logger.info("{} with UUID {} successfully added to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName());
} catch (Exception e) {
logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid, ContextUtility.getCurrentContextName(), e);
}
}
public void removeFromContext()
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
@ -200,7 +210,7 @@ public class HostingNodeManager {
consistsOfList.addAll(consistsOfToRemove);
hostingNode = resourceRegistryPublisher.createResource(hostingNode);
} catch (AvailableInAnotherContextException e) {
addToContext();
addToCurrentContext();
hostingNode = resourceRegistryPublisher.updateResource(hostingNode);
} catch (ResourceRegistryException e) {
logger.error("error trying to publish hosting node", e);
@ -264,7 +274,7 @@ public class HostingNodeManager {
consistsOfList.addAll(consistsOfToRemove);
hostingNode = resourceRegistryPublisher.createResource(hostingNode);
} catch (AvailableInAnotherContextException e) {
addToContext();
addToCurrentContext();
hostingNode = resourceRegistryPublisher.updateResource(hostingNode);
} catch (ResourceRegistryException e) {
logger.error("error trying to publish hosting node", e);
@ -410,8 +420,10 @@ public class HostingNodeManager {
hostingNode = instantiateHostingNode();
hostingNode = resourceRegistryPublisher.createResource(hostingNode);
} catch (AvailableInAnotherContextException e) {
addToContext();
addToCurrentContext();
hostingNode = resourceRegistryClient.getInstance(HostingNode.class, uuid);
} catch (ResourceRegistryException e) {
logger.error("", e);
}
return hostingNode;
}