This commit is contained in:
Nunzio Andrea Galante 2017-03-29 14:06:46 +00:00
parent 45fb751fc2
commit da8f4bbd66
5 changed files with 438 additions and 262 deletions

View File

@ -24,6 +24,12 @@
</properties> </properties>
<dependencies> <dependencies>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-smartgears</artifactId>
<!-- <version>[1.0.0-SNAPSHOT,1.2.7)</version>-->
<scope>provided</scope>
</dependency>
<dependency> <dependency>
<groupId>commons-io</groupId> <groupId>commons-io</groupId>
<artifactId>commons-io</artifactId> <artifactId>commons-io</artifactId>

View File

@ -0,0 +1,22 @@
package org.gcube.dataanalysis.dataminer.poolmanager.datamodel;
public class AlgoResource ***REMOVED***
protected String id;
public AlgoResource() ***REMOVED***
***REMOVED***
public AlgoResource(String id) ***REMOVED***
this.id = id;
***REMOVED***
public String getId() ***REMOVED***
return this.id;
***REMOVED***
public void setId(String id) ***REMOVED***
this.id = id;
***REMOVED***
***REMOVED***

View File

@ -3,7 +3,34 @@ package org.gcube.dataanalysis.dataminer.poolmanager.datamodel;
import java.util.Collection; import java.util.Collection;
import java.util.Vector; import java.util.Vector;
public class Algorithm ***REMOVED***
public class Algorithm extends AlgoResource ***REMOVED***
***REMOVED***
public int hashCode() ***REMOVED***
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
***REMOVED***
***REMOVED***
public boolean equals(Object obj) ***REMOVED***
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Algorithm other = (Algorithm) obj;
if (id == null) ***REMOVED***
if (other.id != null)
return false;
***REMOVED*** else if (!id.equals(other.id))
return false;
return true;
***REMOVED***
private String name; private String name;

View File

@ -27,6 +27,7 @@
***REMOVED*** ***REMOVED***
***REMOVED***
***REMOVED*** ***REMOVED***
***REMOVED*** ***REMOVED***
***REMOVED*** ***REMOVED***
@ -49,4 +50,10 @@ public interface PoolManager ***REMOVED***
URL getURLfromWorkerLog(String logUrl) throws MalformedURLException, UnknownHostException; URL getURLfromWorkerLog(String logUrl) throws MalformedURLException, UnknownHostException;
void addAlgToIs(Algorithm algo);
Set<Algorithm> getAlgoFromIs();
Algorithm getAlgoById(String id);
***REMOVED*** ***REMOVED***

View File

@ -262,16 +262,29 @@
***REMOVED******REMOVED*** ***REMOVED******REMOVED***
package org.gcube.dataanalysis.dataminer.poolmanager.service; package org.gcube.dataanalysis.dataminer.poolmanager.service;
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
***REMOVED*** ***REMOVED***
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.StringWriter;
***REMOVED*** ***REMOVED***
***REMOVED*** ***REMOVED***
import java.net.URLConnection; import java.net.URLConnection;
***REMOVED*** ***REMOVED***
import java.util.Arrays;
import java.util.HashSet;
***REMOVED***
***REMOVED***
import java.util.UUID; import java.util.UUID;
import org.gcube.common.resources.gcore.GenericResource;
import org.gcube.common.resources.gcore.HostingNode;
import org.gcube.common.resources.gcore.Resources;
import org.gcube.common.resources.gcore.Software.Profile.Dependency;
***REMOVED***
import org.gcube.dataanalysis.dataminer.poolmanager.ansiblebridge.AnsibleBridge; import org.gcube.dataanalysis.dataminer.poolmanager.ansiblebridge.AnsibleBridge;
import org.gcube.dataanalysis.dataminer.poolmanager.clients.ISClient; import org.gcube.dataanalysis.dataminer.poolmanager.clients.ISClient;
***REMOVED*** ***REMOVED***
@ -280,15 +293,20 @@ import org.gcube.dataanalysis.dataminer.poolmanager.datamodel.Cluster;
import org.gcube.dataanalysis.dataminer.poolmanager.datamodel.Host; import org.gcube.dataanalysis.dataminer.poolmanager.datamodel.Host;
import org.gcube.dataanalysis.dataminer.poolmanager.process.AlgorithmPackageParser; import org.gcube.dataanalysis.dataminer.poolmanager.process.AlgorithmPackageParser;
import org.gcube.dataanalysis.dataminer.poolmanager.rest.PoolManager; import org.gcube.dataanalysis.dataminer.poolmanager.rest.PoolManager;
import org.gcube.informationsystem.publisher.AdvancedScopedPublisher;
import org.gcube.informationsystem.publisher.RegistryPublisherFactory;
import org.gcube.informationsystem.publisher.ScopedPublisher;
import org.gcube.informationsystem.publisher.exception.RegistryNotFoundException;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
***REMOVED*** ***REMOVED***
***REMOVED*** ***REMOVED***
public class DataminerPoolManager implements PoolManager ***REMOVED*** public class DataminerPoolManager implements PoolManager ***REMOVED***
private static final Logger LOGGER = LoggerFactory.getLogger(DataminerPoolManager.class); private static final Logger LOGGER = LoggerFactory.getLogger(DataminerPoolManager.class);
***REMOVED*** static Collection<Algorithm> algorithms; ***REMOVED*** static Collection<Algorithm> algorithms;
***REMOVED*** ***REMOVED***
***REMOVED*** static Collection<AlgorithmSet> sets; ***REMOVED*** static Collection<AlgorithmSet> sets;
@ -301,7 +319,8 @@ public class DataminerPoolManager implements PoolManager ***REMOVED***
***REMOVED*** ***REMOVED*** ***REMOVED*** ***REMOVED***
***REMOVED*** ***REMOVED***
***REMOVED*** /** ***REMOVED*** /**
***REMOVED*** * Add a new algorithm to the set of known ones. No further action is expected ***REMOVED*** * Add a new algorithm to the set of known ones. No further action is
***REMOVED*** expected
***REMOVED*** * on the pool. ***REMOVED*** * on the pool.
***REMOVED*** */ ***REMOVED*** */
***REMOVED*** public void publishAlgorithm(Algorithm algorithm) ***REMOVED*** ***REMOVED*** public void publishAlgorithm(Algorithm algorithm) ***REMOVED***
@ -314,7 +333,8 @@ public class DataminerPoolManager implements PoolManager ***REMOVED***
***REMOVED*** * @param algorithm ***REMOVED*** * @param algorithm
***REMOVED*** */ ***REMOVED*** */
***REMOVED*** /* ***REMOVED*** /*
***REMOVED*** * public void updateAlgorithm(Algorithm algorithm) ***REMOVED*** ***REMOVED*** TODO implement this ***REMOVED*** ***REMOVED*** * public void updateAlgorithm(Algorithm algorithm) ***REMOVED*** ***REMOVED*** TODO implement
***REMOVED*** this ***REMOVED***
***REMOVED*** */ ***REMOVED*** */
***REMOVED*** ***REMOVED***
***REMOVED*** /** ***REMOVED*** /**
@ -338,7 +358,8 @@ public class DataminerPoolManager implements PoolManager ***REMOVED***
***REMOVED*** * @param setId ***REMOVED*** * @param setId
***REMOVED*** * @param clusterId ***REMOVED*** * @param clusterId
***REMOVED*** */ ***REMOVED*** */
***REMOVED*** public void applyAlgorithmSetToCluster(String setName, String clusterName) ***REMOVED*** ***REMOVED*** public void applyAlgorithmSetToCluster(String setName, String
***REMOVED*** clusterName) ***REMOVED***
***REMOVED*** AlgorithmSet set = this.getAlgorithmSet(setName); ***REMOVED*** AlgorithmSet set = this.getAlgorithmSet(setName);
***REMOVED*** Cluster cluster = new ISClient().getCluster(clusterName); ***REMOVED*** Cluster cluster = new ISClient().getCluster(clusterName);
***REMOVED*** if (set != null && cluster != null) ***REMOVED*** ***REMOVED*** if (set != null && cluster != null) ***REMOVED***
@ -365,9 +386,6 @@ public class DataminerPoolManager implements PoolManager ***REMOVED***
***REMOVED*** return null; ***REMOVED*** return null;
***REMOVED*** ***REMOVED*** ***REMOVED*** ***REMOVED***
***REMOVED*** ***REMOVED***
***REMOVED*** public void getLogId(final Algorithm algorithm, final String vre) ***REMOVED*** ***REMOVED*** public void getLogId(final Algorithm algorithm, final String vre) ***REMOVED***
***REMOVED*** new Thread() ***REMOVED*** ***REMOVED*** new Thread() ***REMOVED***
@ -399,13 +417,11 @@ public class DataminerPoolManager implements PoolManager ***REMOVED***
***REMOVED*** System.out.println("Log stored to to " + n.getAbsolutePath()); ***REMOVED*** System.out.println("Log stored to to " + n.getAbsolutePath());
***REMOVED*** ***REMOVED*** ***REMOVED*** ***REMOVED***
***REMOVED*** public String getLogById(String id) throws IOException ***REMOVED*** ***REMOVED*** public String getLogById(String id) throws IOException ***REMOVED***
***REMOVED*** String strLine = null; ***REMOVED*** String strLine = null;
***REMOVED*** try***REMOVED*** ***REMOVED*** try***REMOVED***
***REMOVED*** FileInputStream fstream = new FileInputStream("/tmp/dataminer-pool-manager/work/"+id+"/logs/"+id); ***REMOVED*** FileInputStream fstream = new
***REMOVED*** FileInputStream("/tmp/dataminer-pool-manager/work/"+id+"/logs/"+id);
***REMOVED*** BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); ***REMOVED*** BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
***REMOVED*** /* read log line by line */ ***REMOVED*** /* read log line by line */
***REMOVED*** while ((strLine = br.readLine()) != null) ***REMOVED*** ***REMOVED*** while ((strLine = br.readLine()) != null) ***REMOVED***
@ -419,14 +435,12 @@ public class DataminerPoolManager implements PoolManager ***REMOVED***
***REMOVED*** return strLine; ***REMOVED*** return strLine;
***REMOVED*** ***REMOVED*** ***REMOVED*** ***REMOVED***
public String getScriptFromURL(URL url) throws IOException ***REMOVED*** public String getScriptFromURL(URL url) throws IOException ***REMOVED***
if (url == null) ***REMOVED*** if (url == null) ***REMOVED***
return null; ***REMOVED***
***REMOVED*** ***REMOVED***
URLConnection yc = url.openConnection(); URLConnection yc = url.openConnection();
BufferedReader input = new BufferedReader(new InputStreamReader( BufferedReader input = new BufferedReader(new InputStreamReader(yc.getInputStream()));
yc.getInputStream()));
String line; String line;
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();
while ((line = input.readLine()) != null) ***REMOVED*** while ((line = input.readLine()) != null) ***REMOVED***
@ -437,19 +451,14 @@ public class DataminerPoolManager implements PoolManager ***REMOVED***
return bufferScript; return bufferScript;
***REMOVED*** ***REMOVED***
***REMOVED****
/**
* Publish the given algorithm in the given VRE * Publish the given algorithm in the given VRE
* *
* @param algorithmName * @param algorithmName
* @param vre * @param vre
* *
*/ ***REMOVED***
public String addAlgorithmToVRE(Algorithm algorithm, final String vre) public String addAlgorithmToVRE(Algorithm algorithm, final String vre) throws IOException ***REMOVED***
throws IOException ***REMOVED***
***REMOVED*** create a fake algorithm set ***REMOVED*** create a fake algorithm set
final AlgorithmSet algoSet = new AlgorithmSet(); final AlgorithmSet algoSet = new AlgorithmSet();
algoSet.setName("fake"); algoSet.setName("fake");
@ -457,9 +466,9 @@ public class DataminerPoolManager implements PoolManager ***REMOVED***
final String uuid = UUID.randomUUID().toString(); final String uuid = UUID.randomUUID().toString();
new Thread(new Runnable() ***REMOVED*** new Thread(new Runnable() ***REMOVED***
@Override ***REMOVED***
public void run() ***REMOVED*** public void run() ***REMOVED***
***REMOVED*** TODO Auto-generated method stub ***REMOVED***
try ***REMOVED*** try ***REMOVED***
addAlgorithmsToVRE(algoSet, vre, uuid); addAlgorithmsToVRE(algoSet, vre, uuid);
***REMOVED*** catch (IOException e) ***REMOVED*** ***REMOVED*** catch (IOException e) ***REMOVED***
@ -468,13 +477,13 @@ public class DataminerPoolManager implements PoolManager ***REMOVED***
***REMOVED*** ***REMOVED***
***REMOVED*** ***REMOVED***
***REMOVED***).start(); ***REMOVED***).start();
***REMOVED*** this line will execute immediately, not waiting for your task to complete ***REMOVED*** this line will execute immediately, not waiting for your task to
***REMOVED*** complete
System.out.println(uuid); System.out.println(uuid);
return uuid; return uuid;
***REMOVED*** ***REMOVED***
public String addAlgorithmToHost(Algorithm algorithm, final String hostname) public String addAlgorithmToHost(Algorithm algorithm, final String hostname) throws IOException ***REMOVED***
throws IOException ***REMOVED***
***REMOVED*** create a fake algorithm set ***REMOVED*** create a fake algorithm set
final AlgorithmSet algoSet = new AlgorithmSet(); final AlgorithmSet algoSet = new AlgorithmSet();
algoSet.setName("fake"); algoSet.setName("fake");
@ -482,9 +491,9 @@ public class DataminerPoolManager implements PoolManager ***REMOVED***
final String uuid = UUID.randomUUID().toString(); final String uuid = UUID.randomUUID().toString();
new Thread(new Runnable() ***REMOVED*** new Thread(new Runnable() ***REMOVED***
@Override ***REMOVED***
public void run() ***REMOVED*** public void run() ***REMOVED***
***REMOVED*** TODO Auto-generated method stub ***REMOVED***
try ***REMOVED*** try ***REMOVED***
addAlgorithmsToHost(algoSet, hostname, uuid); addAlgorithmsToHost(algoSet, hostname, uuid);
***REMOVED*** catch (IOException e) ***REMOVED*** ***REMOVED*** catch (IOException e) ***REMOVED***
@ -493,15 +502,16 @@ public class DataminerPoolManager implements PoolManager ***REMOVED***
***REMOVED*** ***REMOVED***
***REMOVED*** ***REMOVED***
***REMOVED***).start(); ***REMOVED***).start();
***REMOVED*** this line will execute immediately, not waiting for your task to complete ***REMOVED*** this line will execute immediately, not waiting for your task to
***REMOVED*** complete
System.out.println(uuid); System.out.println(uuid);
return uuid; return uuid;
***REMOVED*** ***REMOVED***
public URL getURLfromWorkerLog(String a) throws MalformedURLException, UnknownHostException ***REMOVED*** public URL getURLfromWorkerLog(String a) throws MalformedURLException, UnknownHostException ***REMOVED***
File path = new File(System.getProperty("user.home")+File.separator+"/gcube/dataminer-pool-manager/work/"+a+File.separator+"logs"); File path = new File(System.getProperty("user.home") + File.separator + "/gcube/dataminer-pool-manager/work/"
+ a + File.separator + "logs");
path.mkdirs(); path.mkdirs();
File n = new File(path + File.separator + a); File n = new File(path + File.separator + a);
***REMOVED*** String addr = InetAddress.getLocalHost().getHostAddress(); ***REMOVED*** String addr = InetAddress.getLocalHost().getHostAddress();
@ -509,8 +519,6 @@ public class DataminerPoolManager implements PoolManager ***REMOVED***
return new File(n.getPath()).toURI().toURL(); return new File(n.getPath()).toURI().toURL();
***REMOVED*** ***REMOVED***
public String addAlgorithmsToVRE(AlgorithmSet algorithms, String vre, String uuid) throws IOException ***REMOVED*** public String addAlgorithmsToVRE(AlgorithmSet algorithms, String vre, String uuid) throws IOException ***REMOVED***
***REMOVED*** create the cluster (dataminers in the vre) ***REMOVED*** create the cluster (dataminers in the vre)
@ -540,29 +548,135 @@ public class DataminerPoolManager implements PoolManager ***REMOVED***
***REMOVED*** ***REMOVED***
public Algorithm extractAlgorithm(String url) throws IOException ***REMOVED*** public Algorithm extractAlgorithm(String url) throws IOException ***REMOVED***
return new AlgorithmPackageParser().parsePackage(url); return new AlgorithmPackageParser().parsePackage(url);
***REMOVED*** ***REMOVED***
***REMOVED***
@Override
public void getLogId(Algorithm algo, String vre) ***REMOVED*** public void getLogId(Algorithm algo, String vre) ***REMOVED***
***REMOVED*** TODO Auto-generated method stub ***REMOVED***
***REMOVED*** ***REMOVED***
***REMOVED***
@Override
public String getLogById(String logId) throws IOException ***REMOVED*** public String getLogById(String logId) throws IOException ***REMOVED***
***REMOVED*** TODO Auto-generated method stub ***REMOVED***
return null; ***REMOVED***
***REMOVED*** ***REMOVED***
***REMOVED*** 2017 March 29
public void unPublishScopedResource(GenericResource resource) throws RegistryNotFoundException, Exception ***REMOVED***
ScopedPublisher scopedPublisher = RegistryPublisherFactory.scopedPublisher();
AdvancedScopedPublisher advancedScopedPublisher = new AdvancedScopedPublisher(scopedPublisher);
String id = resource.id();
LOGGER.debug("Trying to remove ***REMOVED******REMOVED*** with ID ***REMOVED******REMOVED*** from ***REMOVED******REMOVED***", resource.getClass().getSimpleName(), id,
ScopeProvider.instance.get());
***REMOVED*** scopedPublisher.remove(resource, scopes);
advancedScopedPublisher.forceRemove(resource);
LOGGER.debug("***REMOVED******REMOVED*** with ID ***REMOVED******REMOVED*** removed successfully", resource.getClass().getSimpleName(), id);
***REMOVED*** ***REMOVED***
public void publishScopedResource(GenericResource a, List<String> scopes)
throws RegistryNotFoundException, Exception ***REMOVED***
StringWriter stringWriter = new StringWriter();
Resources.marshal(a, stringWriter);
ScopedPublisher scopedPublisher = RegistryPublisherFactory.scopedPublisher();
try ***REMOVED***
System.out.println(scopes);
System.out.println(stringWriter);
scopedPublisher.create(a, scopes);
***REMOVED*** catch (RegistryNotFoundException e) ***REMOVED***
System.out.println(e);
throw e;
***REMOVED***
***REMOVED***
***REMOVED***
public void addAlgToIs(Algorithm algo) ***REMOVED***
GenericResource a = new GenericResource();
a.newProfile().name(algo.getName()).type("DataMinerAlgorithms").description(algo.getDescription());
a.profile().newBody(this.getAlgoBody(algo));
try ***REMOVED***
publishScopedResource(a, Arrays.asList(new String[] ***REMOVED*** ScopeProvider.instance.get() ***REMOVED***));
***REMOVED*** catch (Exception e) ***REMOVED***
e.printStackTrace();
***REMOVED***
***REMOVED***
private String getAlgoBody(Algorithm algo) ***REMOVED***
return "<id>" + algo.getId() + "</id>" + "\n" + "<category>" + algo.getCategory() + "</category>" + "\n"
+ "<clazz>" + algo.getClazz() + "</clazz>" + "\n" + "<algorithmType>" + algo.getAlgorithmType()
+ "</algorithmType>" + "\n" + "<skipJava>" + algo.getSkipJava() + "</skipJava>" + "\n" + "<packageURL>"
+ algo.getPackageURL() + "</packageURL>" + "\n" + "<dependencies>"
+ algo.getDependencies().iterator().next().getName() + "</dependencies>";
***REMOVED***
public void updateAlg(Algorithm algo) ***REMOVED***
ScopedPublisher scopedPublisher = RegistryPublisherFactory.scopedPublisher();
SimpleQuery query = queryFor(GenericResource.class);
query.addCondition("$resource/Profile/Name/text() eq '" + algo.getName() + "'").setResult("$resource");
DiscoveryClient<GenericResource> client = clientFor(GenericResource.class);
List<GenericResource> ds = client.submit(query);
if (ds.isEmpty()) ***REMOVED***
return;
***REMOVED***
GenericResource a = ds.get(0);
a.profile().newBody(this.getAlgoBody(algo));
try ***REMOVED***
scopedPublisher.update(a);
***REMOVED*** catch (RegistryNotFoundException e) ***REMOVED***
e.printStackTrace();
***REMOVED***
***REMOVED***
private Algorithm convertAlgo(GenericResource a) ***REMOVED***
Algorithm out = new Algorithm();
out.setId(a.profile().body().getElementsByTagName("id").item(0).getTextContent());
out.setAlgorithmType(a.profile().body().getElementsByTagName("algorithmType").item(0).getTextContent());
out.setCategory(a.profile().body().getElementsByTagName("category").item(0).getTextContent());
out.setClazz(a.profile().body().getElementsByTagName("clazz").item(0).getTextContent());
out.setName(a.profile().name());
out.setPackageURL(a.profile().body().getElementsByTagName("packageURL").item(0).getTextContent());
out.setSkipJava(a.profile().body().getElementsByTagName("skipJava").item(0).getTextContent());
out.setDescription(a.profile().description());
Set<org.gcube.dataanalysis.dataminer.poolmanager.datamodel.Dependency> deps = new HashSet<org.gcube.dataanalysis.dataminer.poolmanager.datamodel.Dependency>();
for (int i = 0; i < a.profile().body().getElementsByTagName("dependencies").getLength(); i++) ***REMOVED***
org.gcube.dataanalysis.dataminer.poolmanager.datamodel.Dependency d1 = new org.gcube.dataanalysis.dataminer.poolmanager.datamodel.Dependency();
d1.setName(a.profile().body().getElementsByTagName("dependencies").item(i).getTextContent());
deps.add(d1);
***REMOVED***
out.setDependencies(deps);
return out;
***REMOVED***
public Algorithm getAlgoById(String id) ***REMOVED***
for (Algorithm aa : this.getAlgoFromIs()) ***REMOVED***
if (aa.getId().equals(id)) ***REMOVED***
return aa;
***REMOVED***
***REMOVED***
***REMOVED***
***REMOVED***
***REMOVED***
public Set<Algorithm> getAlgoFromIs() ***REMOVED***
***REMOVED***
Set<Algorithm> out = new HashSet<Algorithm>();
SimpleQuery query = queryFor(GenericResource.class);
query.addCondition("$resource/Profile/SecondaryType/text() eq 'DataMinerAlgorithms'").setResult("$resource");
DiscoveryClient<GenericResource> client = clientFor(GenericResource.class);
List<GenericResource> ds = client.submit(query);
for (GenericResource a : ds) ***REMOVED***
out.add(this.convertAlgo(a));
***REMOVED***
return out;
***REMOVED***
***REMOVED***