Explicit scope set

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/vre-management/smart-executor@148854 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-05-18 15:33:07 +00:00
parent e6ec979bb3
commit 54ccaeed55
5 changed files with 44 additions and 8 deletions

View File

@ -6,6 +6,7 @@ import java.util.List;
import java.util.Map;
import org.gcube.common.authorization.client.Constants;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.ClientType;
import org.gcube.common.authorization.library.provider.ClientInfo;
@ -66,6 +67,19 @@ public class SmartExecutorInitializator implements ApplicationManager {
return authorizationEntry.getContext();
}
public static String getCurrentScope(String token) throws ObjectNotFound, Exception{
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
String context = authorizationEntry.getContext();
logger.info("Context of token {} is {}", token, context);
return context;
}
public static void setContext(String token) throws ObjectNotFound, Exception{
SecurityTokenProvider.instance.set(token);
ScopeProvider.instance.set(getCurrentScope(token));
}
public static ClientInfo getClientInfo() {
String token = SecurityTokenProvider.instance.get();
AuthorizationEntry authorizationEntry;
@ -344,8 +358,8 @@ public class SmartExecutorInitializator implements ApplicationManager {
SmartExecutorScheduler smartExecutorScheduler = SmartExecutorScheduler.getInstance();
String scheduledTasktoken = scheduledTask.getToken();
SecurityTokenProvider.instance.set(scheduledTasktoken);
try {
setContext(scheduledTasktoken);
// A new Scheduled Task will be persisted due to launch. Removing it
smartExecutorPersistenceConnector.removeScheduledTask(scheduledTask);
smartExecutorScheduler.schedule(launchParameter, scheduledTask.getUUID());

View File

@ -14,6 +14,7 @@ import org.gcube.accounting.datamodel.UsageRecord.OperationResult;
import org.gcube.accounting.datamodel.usagerecords.TaskUsageRecord;
import org.gcube.accounting.persistence.AccountingPersistence;
import org.gcube.accounting.persistence.AccountingPersistenceFactory;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.documentstore.exception.InvalidValueException;
import org.gcube.vremanagement.executor.SmartExecutorInitializator;
import org.gcube.vremanagement.executor.exception.AlreadyInFinalStateException;
@ -55,35 +56,46 @@ public class RunnablePlugin<T extends Plugin<? extends PluginDeclaration>> imple
protected final int iterationNumber;
protected final List<PluginStateNotification> pluginStateNotifications;
protected final String token;
protected PluginStateEvolution actualStateEvolution;
public RunnablePlugin(T plugin, Map<String, Object> inputs,
UUID uuid, int iterationNumber, List<PluginStateNotification> pluginStateNotifications){
UUID uuid, int iterationNumber, List<PluginStateNotification> pluginStateNotifications, String token){
this.plugin = plugin;
this.plugin.setPercentageSetter(new PercentageSetterImpl<T>(this));
this.inputs = inputs;
this.uuid = uuid;
this.iterationNumber = iterationNumber;
this.pluginStateNotifications = pluginStateNotifications;
this.token = token;
try {
SecurityTokenProvider.instance.set(token);
}catch (Exception e) {
throw new RuntimeException(e);
}
try {
setState(PluginState.CREATED);
} catch (AlreadyInFinalStateException | InvalidPluginStateEvolutionException e) {
logger.error(" --- You should not be here. Seem that the {} is suspended before the istance is created. This is really STRANGE.",
logger.error(" --- You should not be here. Seem that the {} is suspended before the instance is created. This is really STRANGE.",
uuid);
throw new RuntimeException(e);
}
}
@Override
public void run(){
//String previousToken = SecurityTokenProvider.instance.get();
logger.info("{} : {} is going to be launched (UUID={}, iterationNumber={}) with the following inputs {}",
plugin.getPluginDeclaration().getName(), plugin.getPluginDeclaration().getVersion(),
uuid, iterationNumber, inputs);
TaskUsageRecord taskUsageRecord = new TaskUsageRecord();
try {
SmartExecutorInitializator.setContext(token);
setState(PluginState.RUNNING);
Calendar taskStartTime = Calendar.getInstance();
@ -151,6 +163,8 @@ public class RunnablePlugin<T extends Plugin<? extends PluginDeclaration>> imple
} catch (InvalidValueException e) {
logger.error("Unable to account {}", taskUsageRecord, e);
}
// SmartExecutorInitializator.setContext(previousToken);
}
}

View File

@ -10,6 +10,7 @@ import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.vremanagement.executor.api.types.LaunchParameter;
import org.gcube.vremanagement.executor.api.types.Scheduling;
import org.gcube.vremanagement.executor.exception.InputsNullException;
@ -125,6 +126,9 @@ public class SmartExecutorScheduler {
jobDataMap.put(SmartExecutorTask.UUID, uuid);
jobDataMap.put(SmartExecutorTask.LAUNCH_PARAMETER, parameter);
String token = SecurityTokenProvider.instance.get();
jobDataMap.put(SmartExecutorTask.TOKEN, token);
@SuppressWarnings("rawtypes")
TriggerBuilder triggerBuilder = TriggerBuilder.newTrigger()
.withIdentity(uuid.toString());

View File

@ -49,6 +49,7 @@ public class SmartExecutorTask implements InterruptableJob {
public static final String UUID = "UUID";
public static final String LAUNCH_PARAMETER = "LAUNCH_PARAMETER";
public static final String TOKEN = "TOKEN";
protected static Map<UUID, Integer> executionsCount;
@ -72,6 +73,7 @@ public class SmartExecutorTask implements InterruptableJob {
protected UUID uuid;
protected LaunchParameter launchParameter;
protected String token;
/* Derived from launchParameter*/
protected int executionCount;
@ -87,6 +89,7 @@ public class SmartExecutorTask implements InterruptableJob {
protected void init(JobDataMap jobDataMap) throws JobExecutionException{
uuid = (UUID) jobDataMap.get(UUID);
launchParameter = (LaunchParameter) jobDataMap.get(LAUNCH_PARAMETER);
token = (String) jobDataMap.get(TOKEN);
pluginName = launchParameter.getPluginName();
@ -218,7 +221,7 @@ public class SmartExecutorTask implements InterruptableJob {
}
runnablePlugin = new RunnablePlugin<Plugin<? extends PluginDeclaration>>(
plugin, inputs, uuid, executionCount, pluginStateNotifications);
plugin, inputs, uuid, executionCount, pluginStateNotifications, token);
logger.debug("Going to run Job with ID {} (iteration {})", uuid, executionCount);

View File

@ -9,6 +9,7 @@ import java.util.UUID;
import org.acme.HelloWorldPlugin;
import org.acme.HelloWorldPluginDeclaration;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.vremanagement.executor.ScopedTest;
import org.gcube.vremanagement.executor.exception.InputsNullException;
import org.gcube.vremanagement.executor.exception.InvalidInputsException;
@ -39,7 +40,7 @@ public class RunnablePluginTest extends ScopedTest {
pluginStateNotifications.add(persistenceConnector);
HelloWorldPlugin helloWorldPlugin = new HelloWorldPlugin(hwpd);
try {
RunnablePlugin<HelloWorldPlugin> runnablePlugin = new RunnablePlugin<HelloWorldPlugin>(helloWorldPlugin, null, uuid, 1, pluginStateNotifications);
RunnablePlugin<HelloWorldPlugin> runnablePlugin = new RunnablePlugin<HelloWorldPlugin>(helloWorldPlugin, null, uuid, 1, pluginStateNotifications, SecurityTokenProvider.instance.get());
runnablePlugin.run();
} catch(Exception e){
Assert.assertEquals(InputsNullException.class, e.getCause().getClass());
@ -57,7 +58,7 @@ public class RunnablePluginTest extends ScopedTest {
pluginStateNotifications.add(persistenceConnector);
HelloWorldPlugin helloWorldPlugin = new HelloWorldPlugin(hwpd);
RunnablePlugin<HelloWorldPlugin> pt = new RunnablePlugin<HelloWorldPlugin>(helloWorldPlugin, inputs, uuid, 1, pluginStateNotifications);
RunnablePlugin<HelloWorldPlugin> pt = new RunnablePlugin<HelloWorldPlugin>(helloWorldPlugin, inputs, uuid, 1, pluginStateNotifications,SecurityTokenProvider.instance.get());
try {
pt.run();
} catch(RuntimeException e) {
@ -80,7 +81,7 @@ public class RunnablePluginTest extends ScopedTest {
List<PluginStateNotification> pluginStateNotifications = new ArrayList<PluginStateNotification>();
pluginStateNotifications.add(persistenceConnector);
HelloWorldPlugin helloWorldPlugin = new HelloWorldPlugin(hwpd);
RunnablePlugin<HelloWorldPlugin> rp = new RunnablePlugin<HelloWorldPlugin>(helloWorldPlugin, inputs, uuid, 1, pluginStateNotifications);
RunnablePlugin<HelloWorldPlugin> rp = new RunnablePlugin<HelloWorldPlugin>(helloWorldPlugin, inputs, uuid, 1, pluginStateNotifications,SecurityTokenProvider.instance.get());
long startTime = Calendar.getInstance().getTimeInMillis();
long endTime = startTime;
while(endTime <= (startTime + 1000)){