Improving Client
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/vre-management/smart-executor-client@112041 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
3382e231b4
commit
f31360654f
|
@ -12,9 +12,11 @@ import org.gcube.common.clients.config.ProxyConfig;
|
|||
import org.gcube.common.clients.delegates.ProxyDelegate;
|
||||
import org.gcube.vremanagement.executor.api.SmartExecutor;
|
||||
import org.gcube.vremanagement.executor.client.Constants;
|
||||
import org.gcube.vremanagement.executor.client.plugins.query.SmartExecutorPluginQuery;
|
||||
import org.gcube.vremanagement.executor.client.plugins.query.filter.EndpointDiscoveryFilter;
|
||||
import org.gcube.vremanagement.executor.client.plugins.query.filter.ServiceEndpointQueryFilter;
|
||||
import org.gcube.vremanagement.executor.client.proxies.DefaultSmartExecutorProxy;
|
||||
import org.gcube.vremanagement.executor.client.proxies.SmartExecutorProxy;
|
||||
import org.gcube.vremanagement.executor.client.util.SmartExecutorPluginQuery;
|
||||
import org.gcube.vremanagement.executor.client.util.Tuple;
|
||||
|
||||
/**
|
||||
|
@ -25,6 +27,18 @@ public class ExecutorPlugin extends AbstractPlugin<SmartExecutor, SmartExecutorP
|
|||
|
||||
private static final ExecutorPlugin executorPlugin = new ExecutorPlugin();
|
||||
|
||||
public static ProxyBuilder<SmartExecutorProxy> getExecutorProxy(
|
||||
String pluginName,
|
||||
Tuple<String, String>[] tuples,
|
||||
ServiceEndpointQueryFilter serviceEndpointQueryFilter,
|
||||
EndpointDiscoveryFilter endpointDiscoveryFilter) {
|
||||
SmartExecutorPluginQuery query = new SmartExecutorPluginQuery(executorPlugin);
|
||||
query.addConditions(pluginName, tuples);
|
||||
query.setServiceEndpointQueryFilter(serviceEndpointQueryFilter);
|
||||
query.setEndpointDiscoveryFilter(endpointDiscoveryFilter);
|
||||
return new ProxyBuilderImpl<SmartExecutor, SmartExecutorProxy>(executorPlugin, query);
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public static ProxyBuilder<SmartExecutorProxy> getExecutorProxy(String pluginName, Tuple<String, String> ... tuples) {
|
||||
SmartExecutorPluginQuery query = new SmartExecutorPluginQuery(executorPlugin);
|
||||
|
|
|
@ -1,13 +1,10 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.vremanagement.executor.client.util;
|
||||
|
||||
import static java.lang.String.format;
|
||||
package org.gcube.vremanagement.executor.client.plugins.query;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import javax.xml.ws.EndpointReference;
|
||||
import javax.xml.ws.wsaddressing.W3CEndpointReferenceBuilder;
|
||||
|
@ -20,6 +17,10 @@ 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;
|
||||
import org.gcube.vremanagement.executor.client.plugins.query.filter.RandomEndpointDiscoveryFilter;
|
||||
import org.gcube.vremanagement.executor.client.plugins.query.filter.ServiceEndpointQueryFilter;
|
||||
import org.gcube.vremanagement.executor.client.util.Tuple;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
||||
|
@ -27,16 +28,20 @@ import org.gcube.resources.discovery.icclient.ICFactory;
|
|||
*/
|
||||
public class SmartExecutorPluginQuery implements Query<EndpointReference> {
|
||||
|
||||
private static String classFormat = "$resource/Profile/ServiceClass/text() eq '%1s'";
|
||||
private static String nameFormat = "$resource/Profile/ServiceName/text() eq '%1s'";
|
||||
private static String statusFormat = "$resource/Profile/DeploymentData/Status/text() eq 'ready'";
|
||||
private static String containsFormat = "contains($entry/string(),'%1s')";
|
||||
public static String classFormat = "$resource/Profile/ServiceClass/text() eq '%1s'";
|
||||
public static String nameFormat = "$resource/Profile/ServiceName/text() eq '%1s'";
|
||||
public static String statusFormat = "$resource/Profile/DeploymentData/Status/text() eq 'ready'";
|
||||
public static String containsFormat = "contains($entry/string(),'%1s')";
|
||||
|
||||
private static DiscoveryClient<ServiceEndpoint> smartExecutorDiscoveryClient = ICFactory.clientFor(ServiceEndpoint.class);
|
||||
private static DiscoveryClient<String> gCoreEndpointDiscoveryClient = ICFactory.client();
|
||||
private DiscoveryClient<ServiceEndpoint> smartExecutorDiscoveryClient = ICFactory.clientFor(ServiceEndpoint.class);
|
||||
private DiscoveryClient<String> gCoreEndpointDiscoveryClient = ICFactory.client();
|
||||
|
||||
private final SimpleQuery smartExecutorDiscoveryQuery;
|
||||
private ServiceEndpointQueryFilter serviceEndpointQueryFilter;
|
||||
|
||||
private final SimpleQuery gCoreEndpointDiscoveryQuery;
|
||||
private EndpointDiscoveryFilter endpointDiscoveryFilter = new RandomEndpointDiscoveryFilter();;
|
||||
|
||||
|
||||
public SmartExecutorPluginQuery(Plugin<?,?> plugin){
|
||||
smartExecutorDiscoveryQuery = ICFactory.queryFor(ServiceEndpoint.class)
|
||||
|
@ -59,6 +64,9 @@ public class SmartExecutorPluginQuery implements Query<EndpointReference> {
|
|||
public void addConditions(String pluginName, Tuple<String, String> ... tuples){
|
||||
smartExecutorDiscoveryQuery.addVariable("$accessPoint", "$resource/Profile/AccessPoint")
|
||||
.addCondition(String.format("$accessPoint/Interface/Endpoint/@EntryName eq '%s'", pluginName));
|
||||
if(tuples==null){
|
||||
return;
|
||||
}
|
||||
for(int i=0; i<tuples.length; i++){
|
||||
Tuple<String, String> tuple = tuples[i];
|
||||
String propertyVariableName = String.format("$property%d", i);
|
||||
|
@ -69,9 +77,21 @@ public class SmartExecutorPluginQuery implements Query<EndpointReference> {
|
|||
}
|
||||
}
|
||||
|
||||
public void setServiceEndpointQueryFilter(ServiceEndpointQueryFilter serviceEndpointQueryFilter){
|
||||
this.serviceEndpointQueryFilter = serviceEndpointQueryFilter;
|
||||
}
|
||||
|
||||
public void setEndpointDiscoveryFilter(EndpointDiscoveryFilter endpointDiscoveryFilter){
|
||||
if(endpointDiscoveryFilter!=null){
|
||||
this.endpointDiscoveryFilter = endpointDiscoveryFilter;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<EndpointReference> fire() throws DiscoveryException {
|
||||
if(serviceEndpointQueryFilter!=null){
|
||||
serviceEndpointQueryFilter.filter(smartExecutorDiscoveryQuery);
|
||||
}
|
||||
|
||||
List<ServiceEndpoint> serviceEndpoints = smartExecutorDiscoveryClient.submit(smartExecutorDiscoveryQuery);
|
||||
|
||||
|
@ -79,23 +99,7 @@ public class SmartExecutorPluginQuery implements Query<EndpointReference> {
|
|||
throw new DiscoveryException("No running SmartExecutor wich match the requested conditions");
|
||||
}
|
||||
|
||||
/*
|
||||
* Generating a random number, assuring that is positive and
|
||||
* and limiting from 0 to the number of discovered ServiceEndpoints
|
||||
* Please note that there is only one ServiceEndpoints for each running
|
||||
* ghn
|
||||
*/
|
||||
Random random = new Random();
|
||||
int number = random.nextInt();
|
||||
while(number == Integer.MIN_VALUE){
|
||||
number = random.nextInt();
|
||||
}
|
||||
number = Math.abs(number);
|
||||
int i = number % serviceEndpoints.size();
|
||||
|
||||
/* Getting random hosts using the generated random number*/
|
||||
String hostedOn = serviceEndpoints.get(i).profile().runtime().hostedOn();
|
||||
gCoreEndpointDiscoveryQuery.addCondition(format(containsFormat, hostedOn));
|
||||
endpointDiscoveryFilter.filter(gCoreEndpointDiscoveryQuery, serviceEndpoints);
|
||||
|
||||
List<EndpointReference> refs = new ArrayList<EndpointReference>();
|
||||
try {
|
|
@ -0,0 +1,23 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
||||
*
|
||||
*/
|
||||
public interface EndpointDiscoveryFilter {
|
||||
|
||||
/**
|
||||
* @param simpleQuery
|
||||
* @param serviceEndpoints
|
||||
*/
|
||||
void filter(SimpleQuery simpleQuery, List<ServiceEndpoint> serviceEndpoints);
|
||||
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.vremanagement.executor.client.plugins.query.filter;
|
||||
|
||||
import static java.lang.String.format;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
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 RandomEndpointDiscoveryFilter implements EndpointDiscoveryFilter {
|
||||
|
||||
@Override
|
||||
public void filter(SimpleQuery simpleQuery, List<ServiceEndpoint> serviceEndpoints) {
|
||||
/*
|
||||
* Generating a random number, assuring that is positive and
|
||||
* and limiting from 0 to the number of discovered ServiceEndpoints
|
||||
* Please note that there is only one ServiceEndpoints for each running
|
||||
* ghn
|
||||
*/
|
||||
Random random = new Random();
|
||||
int number = random.nextInt();
|
||||
while(number == Integer.MIN_VALUE){
|
||||
number = random.nextInt();
|
||||
}
|
||||
number = Math.abs(number);
|
||||
int i = number % serviceEndpoints.size();
|
||||
|
||||
/* Getting random hosts using the generated random number*/
|
||||
String hostedOn = serviceEndpoints.get(i).profile().runtime().hostedOn();
|
||||
simpleQuery.addCondition(format(SmartExecutorPluginQuery.containsFormat, hostedOn));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.vremanagement.executor.client.plugins.query.filter;
|
||||
|
||||
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
||||
*
|
||||
*/
|
||||
public interface ServiceEndpointFilter {
|
||||
|
||||
public void filter(SimpleQuery simpleQuery);
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.vremanagement.executor.client.plugins.query.filter;
|
||||
|
||||
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
||||
*
|
||||
*/
|
||||
public interface ServiceEndpointQueryFilter {
|
||||
|
||||
public void filter(SimpleQuery simpleQuery);
|
||||
|
||||
}
|
|
@ -7,10 +7,14 @@ import java.util.HashMap;
|
|||
import java.util.Map;
|
||||
|
||||
import org.acme.HelloWorldPluginDeclaration;
|
||||
import org.gcube.common.clients.ProxyBuilderImpl;
|
||||
import org.gcube.common.clients.exceptions.DiscoveryException;
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
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.RandomEndpointDiscoveryFilter;
|
||||
import org.gcube.vremanagement.executor.client.proxies.SmartExecutorProxy;
|
||||
import org.gcube.vremanagement.executor.client.util.Tuple;
|
||||
import org.junit.Assert;
|
||||
|
@ -76,7 +80,6 @@ public class QueriedClientTest {
|
|||
|
||||
@SuppressWarnings("unchecked")
|
||||
Tuple<String, String>[] tuples = new Tuple[map.size()+1];
|
||||
|
||||
int i = 0;
|
||||
for(String key : map.keySet()){
|
||||
tuples[i] = new Tuple<String, String>(key, map.get(key));
|
||||
|
@ -99,7 +102,7 @@ public class QueriedClientTest {
|
|||
public void testWithUnsatisfiedConditions() {
|
||||
ScopeProvider.instance.set("/gcube");
|
||||
Tuple<String, String> tuple = new Tuple<String, String>("Unsatisfied", "Condition");
|
||||
SmartExecutorProxy proxy = ExecutorPlugin.getExecutorProxy(HelloWorldPluginDeclaration.NAME, tuple).build();
|
||||
SmartExecutorProxy proxy = ExecutorPlugin.getExecutorProxy(HelloWorldPluginDeclaration.NAME, tuple).build();
|
||||
Assert.assertNotNull(proxy);
|
||||
try {
|
||||
lauchTest(proxy);
|
||||
|
@ -108,4 +111,55 @@ public class QueriedClientTest {
|
|||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testWithPersonalfilters() {
|
||||
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(null);
|
||||
|
||||
SmartExecutorProxy proxy = new ProxyBuilderImpl<SmartExecutor, SmartExecutorProxy>(executorPlugin, query).build();
|
||||
|
||||
try {
|
||||
lauchTest(proxy);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testManagedPersonalfilters() {
|
||||
ScopeProvider.instance.set("/gcube");
|
||||
HelloWorldPluginDeclaration helloWorldPluginDeclaration = new HelloWorldPluginDeclaration();
|
||||
Map<String,String> map = helloWorldPluginDeclaration.getSupportedCapabilities();
|
||||
@SuppressWarnings("unchecked")
|
||||
Tuple<String, String>[] tuples = new Tuple[map.size()+1];
|
||||
int i = 0;
|
||||
for(String key : map.keySet()){
|
||||
tuples[i] = new Tuple<String, String>(key, map.get(key));
|
||||
++i;
|
||||
}
|
||||
|
||||
SmartExecutorProxy proxy = ExecutorPlugin.getExecutorProxy(HelloWorldPluginDeclaration.NAME, tuples, null, new RandomEndpointDiscoveryFilter()).build();
|
||||
Assert.assertNotNull(proxy);
|
||||
|
||||
try {
|
||||
lauchTest(proxy);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue