Improved client

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/vre-management/smart-executor-client@112114 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2015-02-16 11:20:52 +00:00
parent f31360654f
commit bae5a73ce8
6 changed files with 181 additions and 20 deletions

View File

@ -25,13 +25,12 @@ import org.gcube.vremanagement.executor.client.util.Tuple;
*/
public class ExecutorPlugin extends AbstractPlugin<SmartExecutor, SmartExecutorProxy> {
private static final ExecutorPlugin executorPlugin = new ExecutorPlugin();
public static ProxyBuilder<SmartExecutorProxy> getExecutorProxy(
String pluginName,
Tuple<String, String>[] tuples,
ServiceEndpointQueryFilter serviceEndpointQueryFilter,
EndpointDiscoveryFilter endpointDiscoveryFilter) {
ExecutorPlugin executorPlugin = new ExecutorPlugin();
SmartExecutorPluginQuery query = new SmartExecutorPluginQuery(executorPlugin);
query.addConditions(pluginName, tuples);
query.setServiceEndpointQueryFilter(serviceEndpointQueryFilter);
@ -41,12 +40,14 @@ public class ExecutorPlugin extends AbstractPlugin<SmartExecutor, SmartExecutorP
@SafeVarargs
public static ProxyBuilder<SmartExecutorProxy> getExecutorProxy(String pluginName, Tuple<String, String> ... tuples) {
ExecutorPlugin executorPlugin = new ExecutorPlugin();
SmartExecutorPluginQuery query = new SmartExecutorPluginQuery(executorPlugin);
query.addConditions(pluginName, tuples);
return new ProxyBuilderImpl<SmartExecutor, SmartExecutorProxy>(executorPlugin, query);
}
public static ProxyBuilder<SmartExecutorProxy> getExecutorProxy() {
ExecutorPlugin executorPlugin = new ExecutorPlugin();
return new ProxyBuilderImpl<SmartExecutor, SmartExecutorProxy>(executorPlugin);
}

View File

@ -14,7 +14,6 @@ import org.gcube.common.clients.exceptions.DiscoveryException;
import org.gcube.common.clients.queries.Query;
import org.gcube.common.resources.gcore.GCoreEndpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.gcube.resources.discovery.icclient.ICFactory;
import org.gcube.vremanagement.executor.client.plugins.query.filter.EndpointDiscoveryFilter;
@ -33,30 +32,30 @@ public class SmartExecutorPluginQuery implements Query<EndpointReference> {
public static String statusFormat = "$resource/Profile/DeploymentData/Status/text() eq 'ready'";
public static String containsFormat = "contains($entry/string(),'%1s')";
private DiscoveryClient<ServiceEndpoint> smartExecutorDiscoveryClient = ICFactory.clientFor(ServiceEndpoint.class);
private DiscoveryClient<String> gCoreEndpointDiscoveryClient = ICFactory.client();
private final Plugin<?,?> plugin;
private final SimpleQuery smartExecutorDiscoveryQuery;
private ServiceEndpointQueryFilter serviceEndpointQueryFilter;
private final SimpleQuery gCoreEndpointDiscoveryQuery;
private ServiceEndpointQueryFilter serviceEndpointQueryFilter;
private EndpointDiscoveryFilter endpointDiscoveryFilter = new RandomEndpointDiscoveryFilter();;
public SmartExecutorPluginQuery(Plugin<?,?> plugin){
smartExecutorDiscoveryQuery = ICFactory.queryFor(ServiceEndpoint.class)
.addCondition(String.format("$resource/Profile/Category/text() eq '%s'", plugin.serviceClass()))
.addCondition(String.format("$resource/Profile/Name/text() eq '%s'", plugin.serviceName()))
.setResult("$resource");
gCoreEndpointDiscoveryQuery = ICFactory.queryFor(GCoreEndpoint.class)
private static SimpleQuery makeEndpointDiscoveryQuery(Plugin<?,?> plugin){
return ICFactory.queryFor(GCoreEndpoint.class)
.addCondition(String.format(classFormat, plugin.serviceClass()))
.addCondition(String.format(nameFormat, plugin.serviceName()))
.addCondition(String.format(statusFormat))
.addVariable("$entry","$resource/Profile/AccessPoint/RunningInstanceInterfaces/Endpoint")
.addCondition(String.format(containsFormat,plugin.name()))
.setResult("$entry/text()");
}
public SmartExecutorPluginQuery(Plugin<?,?> plugin){
this.plugin = plugin;
smartExecutorDiscoveryQuery = ICFactory.queryFor(ServiceEndpoint.class)
.addCondition(String.format("$resource/Profile/Category/text() eq '%s'", plugin.serviceClass()))
.addCondition(String.format("$resource/Profile/Name/text() eq '%s'", plugin.serviceName()))
.setResult("$resource");
}
@ -87,29 +86,50 @@ public class SmartExecutorPluginQuery implements Query<EndpointReference> {
}
}
public List<String> discoverEndpoints(EndpointDiscoveryFilter endpointDiscoveryFilter) throws DiscoveryException {
if(serviceEndpointQueryFilter!=null){
serviceEndpointQueryFilter.filter(smartExecutorDiscoveryQuery);
}
List<ServiceEndpoint> serviceEndpoints = ICFactory.clientFor(ServiceEndpoint.class).submit(smartExecutorDiscoveryQuery);
if(serviceEndpoints.size() == 0){
throw new DiscoveryException("No running SmartExecutor wich match the requested conditions");
}
SimpleQuery preRunQuery = makeEndpointDiscoveryQuery(plugin);
endpointDiscoveryFilter.filter(preRunQuery, serviceEndpoints);
return ICFactory.client().submit(preRunQuery);
}
@Override
public List<EndpointReference> fire() throws DiscoveryException {
if(serviceEndpointQueryFilter!=null){
serviceEndpointQueryFilter.filter(smartExecutorDiscoveryQuery);
}
List<ServiceEndpoint> serviceEndpoints = smartExecutorDiscoveryClient.submit(smartExecutorDiscoveryQuery);
List<ServiceEndpoint> serviceEndpoints = ICFactory.clientFor(ServiceEndpoint.class).submit(smartExecutorDiscoveryQuery);
if(serviceEndpoints.size() == 0){
throw new DiscoveryException("No running SmartExecutor wich match the requested conditions");
}
SimpleQuery gCoreEndpointDiscoveryQuery = makeEndpointDiscoveryQuery(plugin);
endpointDiscoveryFilter.filter(gCoreEndpointDiscoveryQuery, serviceEndpoints);
List<EndpointReference> refs = new ArrayList<EndpointReference>();
try {
List<String> addresses = gCoreEndpointDiscoveryClient.submit(gCoreEndpointDiscoveryQuery);
List<String> addresses = ICFactory.client().submit(gCoreEndpointDiscoveryQuery);
for(String address : addresses)
refs.add(new W3CEndpointReferenceBuilder().address(address).build());
}
catch(org.gcube.resources.discovery.client.api.DiscoveryException ex) {
} catch(org.gcube.resources.discovery.client.api.DiscoveryException ex) {
throw new DiscoveryException(ex);
}
return refs;
}

View File

@ -0,0 +1,36 @@
/**
*
*/
package org.gcube.vremanagement.executor.client.plugins.query.filter;
import java.util.List;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.gcube.vremanagement.executor.client.plugins.query.SmartExecutorPluginQuery;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class ListEndpointDiscoveryFilter implements EndpointDiscoveryFilter {
@Override
public void filter(SimpleQuery simpleQuery, List<ServiceEndpoint> serviceEndpoints) {
String expression = "";
int size = serviceEndpoints.size();
for(int i=0; i<size; i++){
String hostedOn = serviceEndpoints.get(i).profile().runtime().hostedOn();
String condition = String.format(SmartExecutorPluginQuery.containsFormat, hostedOn);
expression = String.format("%s %s", expression, condition);
if(i<(size-1)){
expression = String.format("%s or ", expression);
}
}
simpleQuery.addCondition(expression);
}
}

View File

@ -37,6 +37,8 @@ public class RandomEndpointDiscoveryFilter implements EndpointDiscoveryFilter {
/* Getting random hosts using the generated random number*/
String hostedOn = serviceEndpoints.get(i).profile().runtime().hostedOn();
simpleQuery.addCondition(format(SmartExecutorPluginQuery.containsFormat, hostedOn));
}
}

View File

@ -0,0 +1,31 @@
/**
*
*/
package org.gcube.vremanagement.executor.client.plugins.query.filter;
import static java.lang.String.format;
import java.util.List;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.gcube.vremanagement.executor.client.plugins.query.SmartExecutorPluginQuery;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class SpecificEndpointDiscoveryFilter implements EndpointDiscoveryFilter {
private String endpointURI;
public SpecificEndpointDiscoveryFilter(String endpointURI){
this.endpointURI = endpointURI;
}
@Override
public void filter(SimpleQuery simpleQuery, List<ServiceEndpoint> serviceEndpoints) {
simpleQuery.addCondition(format(SmartExecutorPluginQuery.containsFormat, endpointURI));
}
}

View File

@ -4,6 +4,7 @@
package org.gcube.vremanagement.executor.client;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.acme.HelloWorldPluginDeclaration;
@ -14,7 +15,9 @@ import org.gcube.vremanagement.executor.api.SmartExecutor;
import org.gcube.vremanagement.executor.api.types.LaunchParameter;
import org.gcube.vremanagement.executor.client.plugins.ExecutorPlugin;
import org.gcube.vremanagement.executor.client.plugins.query.SmartExecutorPluginQuery;
import org.gcube.vremanagement.executor.client.plugins.query.filter.ListEndpointDiscoveryFilter;
import org.gcube.vremanagement.executor.client.plugins.query.filter.RandomEndpointDiscoveryFilter;
import org.gcube.vremanagement.executor.client.plugins.query.filter.SpecificEndpointDiscoveryFilter;
import org.gcube.vremanagement.executor.client.proxies.SmartExecutorProxy;
import org.gcube.vremanagement.executor.client.util.Tuple;
import org.junit.Assert;
@ -145,7 +148,7 @@ public class QueriedClientTest {
HelloWorldPluginDeclaration helloWorldPluginDeclaration = new HelloWorldPluginDeclaration();
Map<String,String> map = helloWorldPluginDeclaration.getSupportedCapabilities();
@SuppressWarnings("unchecked")
Tuple<String, String>[] tuples = new Tuple[map.size()+1];
Tuple<String, String>[] tuples = new Tuple[map.size()];
int i = 0;
for(String key : map.keySet()){
tuples[i] = new Tuple<String, String>(key, map.get(key));
@ -162,4 +165,72 @@ public class QueriedClientTest {
}
}
@SuppressWarnings("unchecked")
@Test
public void testWithListfilters() {
ScopeProvider.instance.set("/gcube");
ExecutorPlugin executorPlugin = new ExecutorPlugin();
SmartExecutorPluginQuery query = new SmartExecutorPluginQuery(executorPlugin);
HelloWorldPluginDeclaration helloWorldPluginDeclaration = new HelloWorldPluginDeclaration();
Map<String,String> map = helloWorldPluginDeclaration.getSupportedCapabilities();
Tuple<String, String> tuple = new Tuple<String, String>();
for(String key : map.keySet()){
tuple = new Tuple<String, String>(key, map.get(key));
break; // Get only the first
}
query.addConditions(HelloWorldPluginDeclaration.NAME, tuple);
query.setServiceEndpointQueryFilter(null);
query.setEndpointDiscoveryFilter(new ListEndpointDiscoveryFilter());
SmartExecutorProxy proxy = new ProxyBuilderImpl<SmartExecutor, SmartExecutorProxy>(executorPlugin, query).build();
try {
lauchTest(proxy);
} catch (Exception e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
@Test
public void testWithSpecificSelection() {
ScopeProvider.instance.set("/gcube");
ExecutorPlugin executorPlugin = new ExecutorPlugin();
SmartExecutorPluginQuery query = new SmartExecutorPluginQuery(executorPlugin);
HelloWorldPluginDeclaration helloWorldPluginDeclaration = new HelloWorldPluginDeclaration();
Map<String,String> map = helloWorldPluginDeclaration.getSupportedCapabilities();
Tuple<String, String> tuple = new Tuple<String, String>();
for(String key : map.keySet()){
tuple = new Tuple<String, String>(key, map.get(key));
break; // Get only the first
}
query.addConditions(HelloWorldPluginDeclaration.NAME, tuple);
query.setServiceEndpointQueryFilter(null);
List<String> endpoints = query.discoverEndpoints(new ListEndpointDiscoveryFilter());
for(String endpoint : endpoints){
ExecutorPlugin runExecutorPlugin = new ExecutorPlugin();
SmartExecutorPluginQuery runQuery = new SmartExecutorPluginQuery(runExecutorPlugin);
runQuery.addConditions(HelloWorldPluginDeclaration.NAME, tuple);
SpecificEndpointDiscoveryFilter sedf = new SpecificEndpointDiscoveryFilter(endpoint);
runQuery.setEndpointDiscoveryFilter(sedf);
SmartExecutorProxy proxy = new ProxyBuilderImpl<SmartExecutor, SmartExecutorProxy>(runExecutorPlugin, runQuery).build();
try {
lauchTest(proxy);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}