From 46f38e74c9b3ff08d4eca1eb414980a675306841 Mon Sep 17 00:00:00 2001 From: Lucio Lelii Date: Wed, 14 Jan 2009 14:02:30 +0000 Subject: [PATCH] git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/information-system/gCubeIS/Registry@8374 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../registry/impl/RegistryFactory.java | 42 +++++++++++++++++-- .../registry/impl/util/Couple.java | 26 ++++++++++++ .../impl/util/EliminatePoolingThread.java | 22 +++++----- 3 files changed, 75 insertions(+), 15 deletions(-) create mode 100644 src/org/gcube/informationsystem/registry/impl/util/Couple.java diff --git a/src/org/gcube/informationsystem/registry/impl/RegistryFactory.java b/src/org/gcube/informationsystem/registry/impl/RegistryFactory.java index a18a636..44c90df 100644 --- a/src/org/gcube/informationsystem/registry/impl/RegistryFactory.java +++ b/src/org/gcube/informationsystem/registry/impl/RegistryFactory.java @@ -13,7 +13,11 @@ import java.util.Collections; import java.util.List; import org.apache.axis.components.uuid.UUIDGen; import org.apache.axis.components.uuid.UUIDGenFactory; +import org.gcube.common.core.contexts.GHNContext; import org.gcube.common.core.faults.GCUBEFault; +import org.gcube.common.core.informationsystem.client.ISClient; +import org.gcube.common.core.informationsystem.client.XMLResult; +import org.gcube.common.core.informationsystem.client.queries.GCUBEGenericQuery; import org.gcube.common.core.resources.GCUBEHostingNode; import org.gcube.common.core.resources.GCUBEResource; import org.gcube.common.core.scope.GCUBEScope; @@ -25,6 +29,7 @@ import org.gcube.informationsystem.registry.impl.contexts.ProfileContext; import org.gcube.informationsystem.registry.impl.contexts.ServiceContext; import org.gcube.informationsystem.registry.impl.state.ProfileResource; import org.gcube.informationsystem.registry.impl.state.RegistryFactoryResource; +import org.gcube.informationsystem.registry.impl.util.Couple; import org.gcube.informationsystem.registry.impl.util.ProfileManager; import org.gcube.informationsystem.registry.impl.util.RegistryUtil; import org.gcube.informationsystem.registry.stubs.CreateResourceMessage; @@ -168,13 +173,27 @@ public class RegistryFactory{ //try to create resource try { logger.debug(resource.getID()+" Creating the stateful resource for " + resource.getID()); + //finding the timestamp + ISClient client = GHNContext.getImplementation(ISClient.class); + GCUBEGenericQuery query = client.getQuery(GCUBEGenericQuery.class); + query.setExpression("declare namespace is = 'http://gcube-system.org/namespaces/informationsystem/registry';" + + "for $result in collection(\"/db/Profiles\")//Document where ($result/Data/is:Profile/Resource/ID/string() eq '"+resource.getID()+"') return $result/LastUpdateMs"); + List list= client.execute(query, ServiceContext.getContext().getScope()); + long timestamp=0; + if (list.size()>0) + timestamp=Long.parseLong(list.get(0).evaluate("/LastUpdateMs/text()").get(0)); + ProfileResource presource = (ProfileResource) ProfileContext.getContext().getWSHome().create(ProfileContext.getContext().makeKey(resource.getID()),resource); presource.getPersistenceDelegate().store(presource); if (resource.getType().compareTo(GCUBEHostingNode.TYPE)!=0){ GCUBEScope tmpScope= ServiceContext.getContext().getScope().getType() == GCUBEScope.Type.VRE ? ServiceContext.getContext().getScope().getEnclosingScope() : ServiceContext.getContext().getScope(); - if(!ServiceContext.threadTable.get(tmpScope.getName()).getStack().contains(presource)){ - ServiceContext.threadTable.get(tmpScope.getName()).getStack().add(presource); + Couple c= new Couple(timestamp, presource); + if(!ServiceContext.threadTable.get(tmpScope.getName()).getStack().contains(c)){ + ServiceContext.threadTable.get(tmpScope.getName()).getStack().add(c); logger.trace("Adding resource to EliminatePoolingThread "+presource.getGCubeResource().getID()); + }else{ + int index= ServiceContext.threadTable.get(tmpScope.getName()).getStack().indexOf(c); + ServiceContext.threadTable.get(tmpScope.getName()).getStack().get(index).timestamp=timestamp; } } } @@ -245,13 +264,28 @@ public class RegistryFactory{ try { resource = ResourceType.valueOf(mess.getType()).getResourceClass(); resource.load(new StringReader(xmlProfile)); + + //finding the timestamp + ISClient client = GHNContext.getImplementation(ISClient.class); + GCUBEGenericQuery query = client.getQuery(GCUBEGenericQuery.class); + query.setExpression("declare namespace is = 'http://gcube-system.org/namespaces/informationsystem/registry';" + + "for $result in collection(\"/db/Profiles\")//Document where ($result/Data/is:Profile/Resource/ID/string() eq '"+resource.getID()+"') return $result/LastUpdateMs"); + List list= client.execute(query, ServiceContext.getContext().getScope()); + long timestamp=0; + if (list.size()>0) + timestamp=Long.parseLong(list.get(0).evaluate("/LastUpdateMs/text()").get(0)); + ProfileResource pr= getProfileResource(ID); pr.updateResource(resource); if (resource.getType().compareTo(GCUBEHostingNode.TYPE)!=0){ GCUBEScope tmpScope= ServiceContext.getContext().getScope().getType() == GCUBEScope.Type.VRE ? ServiceContext.getContext().getScope().getEnclosingScope() : ServiceContext.getContext().getScope(); - if(!ServiceContext.threadTable.get(tmpScope.getName()).getStack().contains(pr)){ - ServiceContext.threadTable.get(tmpScope.getName()).getStack().add(pr); + Couple c= new Couple(timestamp, pr); + if(!ServiceContext.threadTable.get(tmpScope.getName()).getStack().contains(c)){ + ServiceContext.threadTable.get(tmpScope.getName()).getStack().add(c); logger.trace("Adding resource to EliminatePoolingThread "+pr.getGCubeResource().getID()); + }else{ + int index= ServiceContext.threadTable.get(tmpScope.getName()).getStack().indexOf(c); + ServiceContext.threadTable.get(tmpScope.getName()).getStack().get(index).timestamp=timestamp; } } } diff --git a/src/org/gcube/informationsystem/registry/impl/util/Couple.java b/src/org/gcube/informationsystem/registry/impl/util/Couple.java new file mode 100644 index 0000000..e918348 --- /dev/null +++ b/src/org/gcube/informationsystem/registry/impl/util/Couple.java @@ -0,0 +1,26 @@ +package org.gcube.informationsystem.registry.impl.util; + +import org.gcube.informationsystem.registry.impl.state.ProfileResource; + +/** + * couple + * @author lucio + * + */ +public class Couple{ + + public long timestamp; + public ProfileResource resource; + + public Couple(long timestamp, ProfileResource resource){ + this.timestamp=timestamp; + this.resource= resource; + } + + public boolean equals(Object o){ + Couple objectCouple= (Couple) o; + return objectCouple.resource.equals(this.resource); + + } + +} \ No newline at end of file diff --git a/src/org/gcube/informationsystem/registry/impl/util/EliminatePoolingThread.java b/src/org/gcube/informationsystem/registry/impl/util/EliminatePoolingThread.java index 7f4968d..9a7476e 100644 --- a/src/org/gcube/informationsystem/registry/impl/util/EliminatePoolingThread.java +++ b/src/org/gcube/informationsystem/registry/impl/util/EliminatePoolingThread.java @@ -17,7 +17,7 @@ import org.gcube.informationsystem.registry.impl.state.ProfileResource; public class EliminatePoolingThread extends Thread { - private List stack = Collections.synchronizedList(new LinkedList()); + private List stack = Collections.synchronizedList(new LinkedList()); private static GCUBELog logger = new GCUBELog(RegistryFactory.class.getName()); @@ -25,10 +25,10 @@ public class EliminatePoolingThread extends Thread { boolean noErrors= true; while(noErrors){ try { - this.sleep(30000); + this.sleep(120000); } catch (InterruptedException e) {} - LinkedList tmpStack = new LinkedList(); + LinkedList tmpStack = new LinkedList(); ISClient client; GCUBEGenericQuery query; @@ -36,20 +36,20 @@ public class EliminatePoolingThread extends Thread { client = GHNContext.getImplementation(ISClient.class); query = client.getQuery(GCUBEGenericQuery.class); - ProfileResource p; + synchronized (stack) { int numRes= stack.size(); while (stack.size()>0){ - p=stack.remove(stack.size()-1); + Couple c=stack.remove(stack.size()-1); query.setExpression("declare namespace is = 'http://gcube-system.org/namespaces/informationsystem/registry';" + - "for $result in collection(\"/db/Profiles\")//Document/Data/is:Profile/Resource where ($result/ID/string() eq '"+p.getGCubeResource().getID()+"') return 1"); + "for $result in collection(\"/db/Profiles\")//Document where ($result/Data/is:Profile/Resource/ID/string() eq '"+c.resource.getGCubeResource().getID()+"') return $result/LastUpdateMs"); List list= client.execute(query, ServiceContext.getContext().getScope()); - if (list.size()>0) try { - ProfileContext.getContext().getWSHome().remove(p.getID()); - }catch(Exception e){e.printStackTrace();tmpStack.offer(p);} - else tmpStack.offer(p); + if ( (list.size()>0) && (Long.parseLong(list.get(0).evaluate("/LastUpdateMs/text()").get(0))>c.timestamp)) try { + ProfileContext.getContext().getWSHome().remove(c.resource.getID()); + }catch(Exception e){e.printStackTrace();tmpStack.offer(c);} + else tmpStack.offer(c); } logger.debug("cannot destroy "+tmpStack.size()+" resources retrying in 30 seconds"); @@ -68,7 +68,7 @@ public class EliminatePoolingThread extends Thread { } - public synchronized List getStack(){ + public synchronized List getStack(){ return this.stack; }