Checking if already existing HostingNode and EServices belongs to old
contexts, removed from them if any
This commit is contained in:
parent
754ab7ec86
commit
d8b103b2af
|
@ -3,10 +3,13 @@ package org.gcube.smartgears.handler.resourceregistry;
|
|||
import java.util.Set;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.gcube.common.authorization.client.proxy.AuthorizationProxy;
|
||||
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
||||
import org.gcube.smartgears.provider.ProviderFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -60,6 +63,15 @@ public class ContextUtility {
|
|||
return contextFullNames;
|
||||
}
|
||||
|
||||
|
||||
public static SortedSet<UUID> getContextUUIDFromTokens(Set<String> tokens) throws ResourceRegistryException {
|
||||
SortedSet<UUID> contextsUUID = new TreeSet<>();
|
||||
ContextCache contextCache = ContextCache.getInstance();
|
||||
for(String token : tokens) {
|
||||
String contextFullName = getContextName(token);
|
||||
UUID contextUUID = contextCache.getUUIDByFullName(contextFullName);
|
||||
contextsUUID.add(contextUUID);
|
||||
}
|
||||
return contextsUUID;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,7 +8,9 @@ import static org.gcube.smartgears.lifecycle.application.ApplicationLifecycle.fa
|
|||
import static org.gcube.smartgears.lifecycle.application.ApplicationLifecycle.stop;
|
||||
import static org.gcube.smartgears.utils.Utils.rethrowUnchecked;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
@ -18,6 +20,7 @@ import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
|||
import org.gcube.common.events.Observes;
|
||||
import org.gcube.informationsystem.model.reference.entities.Resource;
|
||||
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
|
||||
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
|
||||
import org.gcube.resourcemanagement.model.reference.entities.facets.ServiceStateFacet;
|
||||
import org.gcube.resourcemanagement.model.reference.entities.resources.EService;
|
||||
import org.gcube.smartgears.context.Property;
|
||||
|
@ -73,6 +76,23 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
|
|||
logger.error("onStart failed", re);
|
||||
}
|
||||
}
|
||||
|
||||
protected void removeResourceFromOldContexts(Set<UUID> startContexts, Set<UUID> resourceContexts) {
|
||||
Set<UUID> contextsToRemove = new HashSet<>(resourceContexts);
|
||||
contextsToRemove.removeAll(startContexts);
|
||||
for(UUID contextToRemove : contextsToRemove) {
|
||||
try {
|
||||
eServiceManager.removeFromContext(contextToRemove);
|
||||
}catch (Exception e) {
|
||||
try {
|
||||
String contextFullName = ContextCache.getInstance().getContextFullNameByUUID(contextToRemove);
|
||||
logger.warn("Unable to remove {} from Context {} UUID {}", EService.NAME, contextFullName, contextsToRemove, e);
|
||||
}catch (Exception ex) {
|
||||
logger.warn("Unable to remove {} from Context with UUID {}.", EService.NAME, contextsToRemove, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void init() {
|
||||
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
|
||||
|
@ -82,8 +102,14 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
|
|||
boolean create = true;
|
||||
eServiceManager = new EServiceManager(applicationContext);
|
||||
Set<String> startTokens = applicationContext.configuration().startTokens();
|
||||
|
||||
Set<UUID> startContextsUUID = new HashSet<>();
|
||||
ContextCache contextCache = ContextCache.getInstance();
|
||||
for (String token : startTokens) {
|
||||
ContextUtility.setContextFromToken(token);
|
||||
String contextFullName = ContextUtility.getContextName(token);
|
||||
UUID contextUUID = contextCache.getUUIDByFullName(contextFullName);
|
||||
startContextsUUID.add(contextUUID);
|
||||
try {
|
||||
if (create) {
|
||||
eServiceManager.createEService();
|
||||
|
@ -98,6 +124,10 @@ public class EServiceHandler extends ApplicationLifecycleHandler {
|
|||
ContextUtility.getContextName(token), e);
|
||||
}
|
||||
}
|
||||
|
||||
Set<UUID> resourceContextsUUID = eServiceManager.getContextsUUID();
|
||||
removeResourceFromOldContexts(startContextsUUID, resourceContextsUUID);
|
||||
|
||||
} catch (Throwable e) {
|
||||
rethrowUnchecked(e);
|
||||
} finally {
|
||||
|
|
|
@ -12,7 +12,10 @@ import static org.gcube.smartgears.lifecycle.container.ContainerLifecycle.stop;
|
|||
import static org.gcube.smartgears.lifecycle.container.ContainerState.active;
|
||||
import static org.gcube.smartgears.utils.Utils.rethrowUnchecked;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
@ -24,6 +27,8 @@ import org.gcube.common.events.Observes;
|
|||
import org.gcube.informationsystem.model.reference.entities.Facet;
|
||||
import org.gcube.informationsystem.model.reference.entities.Resource;
|
||||
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
|
||||
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
||||
import org.gcube.resourcemanagement.model.reference.entities.facets.ContainerStateFacet;
|
||||
import org.gcube.resourcemanagement.model.reference.entities.resources.HostingNode;
|
||||
import org.gcube.smartgears.context.Property;
|
||||
|
@ -80,6 +85,14 @@ public class HostingNodeHandler extends ContainerHandler {
|
|||
}
|
||||
}
|
||||
|
||||
protected void removeResourceFromOldContexts(Set<UUID> startContexts, Set<UUID> resourceContexts) throws ResourceRegistryException {
|
||||
Set<UUID> contextsToRemove = new HashSet<>(resourceContexts);
|
||||
contextsToRemove.removeAll(startContexts);
|
||||
for(UUID contextToRemove : contextsToRemove) {
|
||||
hostingNodeManager.removeFromContext(contextToRemove);
|
||||
}
|
||||
}
|
||||
|
||||
private void init() {
|
||||
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
|
||||
String previousToken = SecurityTokenProvider.instance.get();
|
||||
|
@ -88,14 +101,22 @@ public class HostingNodeHandler extends ContainerHandler {
|
|||
boolean create = true;
|
||||
hostingNodeManager = new HostingNodeManager(containerContext);
|
||||
List<String> startTokens = containerContext.configuration().startTokens();
|
||||
|
||||
Set<UUID> startContextsUUID = new HashSet<>();
|
||||
ContextCache contextCache = ContextCache.getInstance();
|
||||
for (String token : startTokens) {
|
||||
ContextUtility.setContextFromToken(token);
|
||||
String contextFullName = ContextUtility.getContextName(token);
|
||||
UUID contextUUID = contextCache.getUUIDByFullName(contextFullName);
|
||||
startContextsUUID.add(contextUUID);
|
||||
try {
|
||||
if (create) {
|
||||
hostingNodeManager.createHostingNode();
|
||||
containerContext.properties()
|
||||
.add(new Property(Constants.HOSTING_NODE_MANAGER_PROPERTY, hostingNodeManager));
|
||||
create = false;
|
||||
|
||||
|
||||
} else {
|
||||
hostingNodeManager.addToContext();
|
||||
}
|
||||
|
@ -104,6 +125,10 @@ public class HostingNodeHandler extends ContainerHandler {
|
|||
ContextUtility.getContextName(token), e);
|
||||
}
|
||||
}
|
||||
|
||||
Set<UUID> resourceContextsUUID = hostingNodeManager.getContextsUUID();
|
||||
removeResourceFromOldContexts(startContextsUUID, resourceContextsUUID);
|
||||
|
||||
} catch (Throwable e) {
|
||||
rethrowUnchecked(e);
|
||||
} finally {
|
||||
|
|
|
@ -3,7 +3,6 @@ package org.gcube.smartgears.handler.resourceregistry.resourcemanager;
|
|||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
|
@ -103,6 +102,18 @@ public class EServiceManager {
|
|||
ContextUtility.getCurrentContextName());
|
||||
}
|
||||
}
|
||||
|
||||
public void removeFromContext(UUID contextUUID)
|
||||
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
|
||||
boolean removed = false;
|
||||
String contextFullName = ContextCache.getInstance().getContextFullNameByUUID(contextUUID);
|
||||
removed = resourceRegistryPublisher.removeResourceFromContext(eService, contextUUID);
|
||||
if (removed) {
|
||||
logger.info("{} successfully removed from context ({})", eService, contextFullName);
|
||||
} else {
|
||||
logger.error("Unable to remove {} from current context ({})", eService, contextFullName);
|
||||
}
|
||||
}
|
||||
|
||||
private String getBaseAddress() {
|
||||
ApplicationConfiguration configuration = applicationContext.configuration();
|
||||
|
@ -181,7 +192,6 @@ public class EServiceManager {
|
|||
public EService createEService() throws ResourceRegistryException {
|
||||
ResourceRegistryClient resourceRegistryClient = ResourceRegistryClientFactory.create();
|
||||
UUID eServiceUUID = UUID.fromString(applicationContext.id());
|
||||
Set<UUID> contexts;
|
||||
try {
|
||||
ResourceRegistryClientFactory.includeContextsInInstanceHeader(true);
|
||||
eService = resourceRegistryClient.getInstance(EService.class, eServiceUUID);
|
||||
|
@ -224,16 +234,6 @@ public class EServiceManager {
|
|||
} catch (ResourceRegistryException e) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
// TODO Remove contexts not present in start contexts
|
||||
Set<String> startTokens = applicationContext.configuration().startTokens();
|
||||
Set<String> contextFullNames = ContextUtility.getContextFullNamesFromTokens(startTokens);
|
||||
ContextCache contextCache = ContextCache.getInstance();
|
||||
|
||||
|
||||
|
||||
Map<String, UUID> contextFullNameToUUID = contextCache.getContextFullNameToUUIDAssociation();
|
||||
|
||||
return eService;
|
||||
}
|
||||
|
||||
|
@ -278,5 +278,9 @@ public class EServiceManager {
|
|||
updateServiceStateFacet();
|
||||
}
|
||||
}
|
||||
|
||||
public Set<UUID> getContextsUUID() throws Exception {
|
||||
return resourceRegistryPublisher.getResourceContexts(eService);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -33,6 +33,7 @@ import org.gcube.informationsystem.model.reference.entities.Facet;
|
|||
import org.gcube.informationsystem.model.reference.entities.Resource;
|
||||
import org.gcube.informationsystem.model.reference.properties.Header;
|
||||
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
|
||||
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
||||
|
@ -121,7 +122,19 @@ public class HostingNodeManager {
|
|||
ContextUtility.getCurrentContextName());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void removeFromContext(UUID contextUUID)
|
||||
throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException {
|
||||
boolean removed = false;
|
||||
String contextFullName = ContextCache.getInstance().getContextFullNameByUUID(contextUUID);
|
||||
removed = resourceRegistryPublisher.removeResourceFromContext(hostingNode, contextUUID);
|
||||
if (removed) {
|
||||
logger.info("{} successfully removed from context ({})", hostingNode, contextFullName);
|
||||
} else {
|
||||
logger.error("Unable to remove {} from current context ({})", hostingNode, contextFullName);
|
||||
}
|
||||
}
|
||||
|
||||
public HostingNode updateFacets() throws ResourceRegistryException {
|
||||
logger.debug("Updating {}", HostingNode.NAME);
|
||||
|
||||
|
@ -335,10 +348,6 @@ public class HostingNodeManager {
|
|||
addToContext();
|
||||
hostingNode = resourceRegistryClient.getInstance(HostingNode.class, uuid);
|
||||
}
|
||||
|
||||
// TODO Remove contexts not present in start contexts
|
||||
List<String> startTokens = containerContext.configuration().startTokens();
|
||||
|
||||
return hostingNode;
|
||||
}
|
||||
|
||||
|
@ -578,4 +587,8 @@ public class HostingNodeManager {
|
|||
return cpuFacets;
|
||||
}
|
||||
|
||||
public Set<UUID> getContextsUUID() throws Exception {
|
||||
return resourceRegistryPublisher.getResourceContexts(hostingNode);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue