This commit is contained in:
Lucio Lelii 2009-01-14 14:02:30 +00:00
parent 3e6b8468c7
commit 46f38e74c9
3 changed files with 75 additions and 15 deletions

View File

@ -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<XMLResult> 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<XMLResult> 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;
}
}
}

View File

@ -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);
}
}

View File

@ -17,7 +17,7 @@ import org.gcube.informationsystem.registry.impl.state.ProfileResource;
public class EliminatePoolingThread extends Thread {
private List<ProfileResource> stack = Collections.synchronizedList(new LinkedList<ProfileResource>());
private List<Couple> stack = Collections.synchronizedList(new LinkedList<Couple>());
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<ProfileResource> tmpStack = new LinkedList<ProfileResource>();
LinkedList<Couple> tmpStack = new LinkedList<Couple>();
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<XMLResult> 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<ProfileResource> getStack(){
public synchronized List<Couple> getStack(){
return this.stack;
}