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

View File

@ -48,6 +48,13 @@ public class ContextUtility {
return getContextName(token); 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) { public static String getContextName(String token) {
try { try {
return authorizationProxy.get(token).getContext(); 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.informationsystem.resourceregistry.api.contexts.ContextCache;
import org.gcube.resourcemanagement.model.reference.entities.facets.StateFacet; import org.gcube.resourcemanagement.model.reference.entities.facets.StateFacet;
import org.gcube.resourcemanagement.model.reference.entities.resources.EService; 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.context.application.ApplicationContext;
import org.gcube.smartgears.handler.resourceregistry.resourcemanager.EServiceManager; import org.gcube.smartgears.handler.resourceregistry.resourcemanager.EServiceManager;
import org.gcube.smartgears.handlers.application.ApplicationLifecycleEvent.Start; import org.gcube.smartgears.handlers.application.ApplicationLifecycleEvent.Start;
@ -100,27 +99,31 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
try { try {
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader()); Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
boolean create = true; boolean create = true;
Set<String> startTokens = applicationContext.configuration().startTokens(); Set<String> startTokens = applicationContext.configuration().startTokens();
Set<UUID> startContextsUUID = new HashSet<>(); Set<UUID> startContextsUUID = new HashSet<>();
ContextCache contextCache = ContextCache.getInstance(); ContextCache contextCache = ContextCache.getInstance();
for (String token : startTokens) { for (String token : startTokens) {
try {
if (create) { if (create) {
ContextUtility.setContextFromToken(token); ContextUtility.setContextFromToken(token);
eServiceManager = new EServiceManager(applicationContext); eServiceManager = new EServiceManager(applicationContext);
eServiceManager.createEService(); eServiceManager.createEService();
applicationContext.properties() // applicationContext.properties()
.add(new Property(Constants.ESERVICE_MANAGER_PROPERTY, eServiceManager)); // .add(new Property(Constants.ESERVICE_MANAGER_PROPERTY, eServiceManager));
create = false; create = false;
} else { } else {
eServiceManager.addToContext(); try {
} UUID contextUUID = ContextUtility.getContextUUID(token);
eServiceManager.addToContext(contextUUID);
} catch (Exception e) { } catch (Exception e) {
UUID uuid = UUID.fromString(applicationContext.id()); UUID uuid = UUID.fromString(applicationContext.id());
logger.error("Unable to add {} with UUID {} to current context ({})", EService.NAME, uuid, logger.error("Unable to add {} with UUID {} to current context ({})", EService.NAME, uuid,
ContextUtility.getContextName(token), e); ContextUtility.getContextName(token), e);
} }
}
String contextFullName = ContextUtility.getContextName(token); String contextFullName = ContextUtility.getContextName(token);
UUID contextUUID = contextCache.getUUIDByFullName(contextFullName); UUID contextUUID = contextCache.getUUIDByFullName(contextFullName);
startContextsUUID.add(contextUUID); startContextsUUID.add(contextUUID);
@ -171,7 +174,7 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
try { try {
Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader()); Thread.currentThread().setContextClassLoader(EServiceHandler.class.getClassLoader());
ContextUtility.setContextFromToken(token); ContextUtility.setContextFromToken(token);
eServiceManager.addToContext(); eServiceManager.addToCurrentContext();
} catch (Exception e) { } catch (Exception e) {
logger.error("Failed to add {} to current context ({})", EService.NAME, logger.error("Failed to add {} to current context ({})", EService.NAME,
ContextUtility.getCurrentContextName(), e); 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.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.resourcemanagement.model.reference.entities.facets.StateFacet; import org.gcube.resourcemanagement.model.reference.entities.facets.StateFacet;
import org.gcube.resourcemanagement.model.reference.entities.resources.HostingNode; 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.context.container.ContainerContext;
import org.gcube.smartgears.handler.resourceregistry.resourcemanager.HostingNodeManager; import org.gcube.smartgears.handler.resourceregistry.resourcemanager.HostingNodeManager;
import org.gcube.smartgears.handlers.container.ContainerHandler; import org.gcube.smartgears.handlers.container.ContainerHandler;
@ -106,24 +105,24 @@ public class HostingNodeHandler extends ContainerHandler {
Set<UUID> startContextsUUID = new HashSet<>(); Set<UUID> startContextsUUID = new HashSet<>();
ContextCache contextCache = ContextCache.getInstance(); ContextCache contextCache = ContextCache.getInstance();
for (String token : startTokens) { for (String token : startTokens) {
try {
if (create) { if (create) {
ContextUtility.setContextFromToken(token); ContextUtility.setContextFromToken(token);
hostingNodeManager = new HostingNodeManager(containerContext); hostingNodeManager = new HostingNodeManager(containerContext);
hostingNodeManager.createHostingNode(); hostingNodeManager.createHostingNode();
containerContext.properties() // containerContext.properties()
.add(new Property(Constants.HOSTING_NODE_MANAGER_PROPERTY, hostingNodeManager)); // .add(new Property(Constants.HOSTING_NODE_MANAGER_PROPERTY, hostingNodeManager));
create = false; create = false;
} else { } else {
hostingNodeManager.addToContext(); try {
} UUID contextUUID = ContextUtility.getContextUUID(token);
hostingNodeManager.addToContext(contextUUID);
} catch (Exception e) { } catch (Exception e) {
UUID uuid = UUID.fromString(containerContext.id()); UUID uuid = UUID.fromString(containerContext.id());
logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid, logger.error("Unable to add {} with UUID {} to current context ({})", HostingNode.NAME, uuid,
ContextUtility.getContextName(token), e); ContextUtility.getContextName(token), e);
} }
}
String contextFullName = ContextUtility.getContextName(token); String contextFullName = ContextUtility.getContextName(token);
UUID contextUUID = contextCache.getUUIDByFullName(contextFullName); UUID contextUUID = contextCache.getUUIDByFullName(contextFullName);
startContextsUUID.add(contextUUID); startContextsUUID.add(contextUUID);
@ -182,7 +181,7 @@ public class HostingNodeHandler extends ContainerHandler {
} }
try { try {
Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader()); Thread.currentThread().setContextClassLoader(HostingNodeHandler.class.getClassLoader());
hostingNodeManager.addToContext(); hostingNodeManager.addToCurrentContext();
} catch (Exception e) { } catch (Exception e) {
logger.error("Failed to update Service State", e); logger.error("Failed to update Service State", e);
} finally { } 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()); UUID uuid = UUID.fromString(applicationContext.container().id());
try { try {
resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false); resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false);
@ -97,6 +97,16 @@ public class EServiceManager {
} }
} }
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() public void removeFromContext()
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
UUID uuid = UUID.fromString(applicationContext.container().id()); UUID uuid = UUID.fromString(applicationContext.container().id());
@ -216,20 +226,20 @@ public class EServiceManager {
createActivatesRelation(eService); createActivatesRelation(eService);
stateFacet = eService.getFacets(StateFacet.class).get(0); stateFacet = eService.getFacets(StateFacet.class).get(0);
} catch (AvailableInAnotherContextException e) { } catch (AvailableInAnotherContextException e) {
addToContext(); // addToContext();
try { // try {
eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID); // eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID);
} catch (AvailableInAnotherContextException ex) { // } catch (AvailableInAnotherContextException ex) {
addEServiceToCurrentContext(); // addEServiceToCurrentContext();
eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID); // eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID);
// addToContext() is executed on HostingNode. // // addToContext() is executed on HostingNode.
// If the EService is still not available we need to create activates // // If the EService is still not available we need to create activates
// relation because does not exists otherwise the EService should // // relation because does not exists otherwise the EService should
// already be in the context due to propagation constraint. // // already be in the context due to propagation constraint.
createActivatesRelation(eService); // createActivatesRelation(eService);
} // }
stateFacet = eService.getFacets(StateFacet.class).get(0); // stateFacet = eService.getFacets(StateFacet.class).get(0);
updateServiceStateFacet(); // updateServiceStateFacet();
} catch (ResourceRegistryException e) { } catch (ResourceRegistryException e) {
throw e; throw e;
} }

View File

@ -110,7 +110,7 @@ public class HostingNodeManager {
return hostingNode; return hostingNode;
} }
public void addToContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { public void addToCurrentContext() throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
UUID uuid = UUID.fromString(containerContext.id()); UUID uuid = UUID.fromString(containerContext.id());
try { try {
resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false); resourceRegistryPublisher.addResourceToCurrentContext(HostingNode.NAME, uuid, false);
@ -120,6 +120,16 @@ public class HostingNodeManager {
} }
} }
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() public void removeFromContext()
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
UUID uuid = UUID.fromString(containerContext.id()); UUID uuid = UUID.fromString(containerContext.id());
@ -200,7 +210,7 @@ public class HostingNodeManager {
consistsOfList.addAll(consistsOfToRemove); consistsOfList.addAll(consistsOfToRemove);
hostingNode = resourceRegistryPublisher.createResource(hostingNode); hostingNode = resourceRegistryPublisher.createResource(hostingNode);
} catch (AvailableInAnotherContextException e) { } catch (AvailableInAnotherContextException e) {
addToContext(); addToCurrentContext();
hostingNode = resourceRegistryPublisher.updateResource(hostingNode); hostingNode = resourceRegistryPublisher.updateResource(hostingNode);
} catch (ResourceRegistryException e) { } catch (ResourceRegistryException e) {
logger.error("error trying to publish hosting node", e); logger.error("error trying to publish hosting node", e);
@ -264,7 +274,7 @@ public class HostingNodeManager {
consistsOfList.addAll(consistsOfToRemove); consistsOfList.addAll(consistsOfToRemove);
hostingNode = resourceRegistryPublisher.createResource(hostingNode); hostingNode = resourceRegistryPublisher.createResource(hostingNode);
} catch (AvailableInAnotherContextException e) { } catch (AvailableInAnotherContextException e) {
addToContext(); addToCurrentContext();
hostingNode = resourceRegistryPublisher.updateResource(hostingNode); hostingNode = resourceRegistryPublisher.updateResource(hostingNode);
} catch (ResourceRegistryException e) { } catch (ResourceRegistryException e) {
logger.error("error trying to publish hosting node", e); logger.error("error trying to publish hosting node", e);
@ -410,8 +420,10 @@ public class HostingNodeManager {
hostingNode = instantiateHostingNode(); hostingNode = instantiateHostingNode();
hostingNode = resourceRegistryPublisher.createResource(hostingNode); hostingNode = resourceRegistryPublisher.createResource(hostingNode);
} catch (AvailableInAnotherContextException e) { } catch (AvailableInAnotherContextException e) {
addToContext(); addToCurrentContext();
hostingNode = resourceRegistryClient.getInstance(HostingNode.class, uuid); hostingNode = resourceRegistryClient.getInstance(HostingNode.class, uuid);
} catch (ResourceRegistryException e) {
logger.error("", e);
} }
return hostingNode; return hostingNode;
} }