fixes #6116: Add Accounting to SmartExecutor

https://support.d4science.org/issues/6116

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/vre-management/smart-executor@142008 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-02-01 16:58:52 +00:00
parent 59dcdec5f6
commit ce406ee802
1 changed files with 67 additions and 0 deletions

View File

@ -3,11 +3,19 @@
*/
package org.gcube.vremanagement.executor.pluginmanager;
import java.io.Serializable;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
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.documentstore.exception.InvalidValueException;
import org.gcube.vremanagement.executor.SmartExecutorInitializator;
import org.gcube.vremanagement.executor.exception.AlreadyInFinalStateException;
import org.gcube.vremanagement.executor.exception.InvalidPluginStateEvolutionException;
import org.gcube.vremanagement.executor.plugin.Plugin;
@ -15,6 +23,9 @@ import org.gcube.vremanagement.executor.plugin.PluginDeclaration;
import org.gcube.vremanagement.executor.plugin.PluginState;
import org.gcube.vremanagement.executor.plugin.PluginStateEvolution;
import org.gcube.vremanagement.executor.plugin.PluginStateNotification;
import org.gcube.vremanagement.executor.plugin.Ref;
import org.gcube.vremanagement.executor.plugin.RunOn;
import org.gcube.vremanagement.executor.scheduledtask.ScheduledTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -32,6 +43,9 @@ public class RunnablePlugin<T extends Plugin<? extends PluginDeclaration>> imple
public static final String MAX_LAUNCH_TIMES = "___max_launch_times___";
protected static final String SEPARATOR = "---";
private static final String ITERATION_NUMBER = "iterationNumber";
private static final String FINAL_STATE = "finalState";
protected final T plugin;
@ -64,8 +78,36 @@ public class RunnablePlugin<T extends Plugin<? extends PluginDeclaration>> imple
@Override
public void run(){
TaskUsageRecord taskUsageRecord = new TaskUsageRecord();
try {
setState(PluginState.RUNNING);
Calendar taskStartTime = Calendar.getInstance();
taskStartTime.setTimeInMillis(actualStateEvolution.getTimestamp());
taskUsageRecord.setTaskStartTime(taskStartTime);
taskUsageRecord.setTaskId(uuid.toString());
taskUsageRecord.setResourceProperty(ITERATION_NUMBER, iterationNumber);
taskUsageRecord.setConsumerId(SmartExecutorInitializator.getClientInfo().getId());
RunOn runOn = ScheduledTask.generateRunOn();
Ref hnRef = runOn.getHostingNode();
taskUsageRecord.setRefHostingNodeId(hnRef.getId());
taskUsageRecord.setHost(hnRef.getAddress());
HashMap<String, Serializable> map =
new HashMap<String, Serializable>();
for(String key : inputs.keySet()){
if(inputs.get(key) instanceof Serializable){
map.put(key, (Serializable) inputs.get(key));
}
}
taskUsageRecord.setInputParameters(map);
this.plugin.setUUID(uuid);
this.plugin.setIterationNumber(iterationNumber);
this.plugin.launch(inputs);
@ -80,6 +122,31 @@ public class RunnablePlugin<T extends Plugin<? extends PluginDeclaration>> imple
return;
}
throw new RuntimeException(e);
}finally {
AccountingPersistence accountingPersistence =
AccountingPersistenceFactory.getPersistence();
try {
Calendar taskEndTime = Calendar.getInstance();
taskEndTime.setTimeInMillis(actualStateEvolution.getTimestamp());
taskUsageRecord.setTaskStartTime(taskEndTime);
PluginState pluginState = actualStateEvolution.getPluginState();
switch (pluginState) {
case DONE:
taskUsageRecord.setOperationResult(OperationResult.SUCCESS);
break;
default:
taskUsageRecord.setOperationResult(OperationResult.FAILED);
break;
}
taskUsageRecord.setResourceProperty(FINAL_STATE, pluginState.toString());
accountingPersistence.account(taskUsageRecord);
} catch (InvalidValueException e) {
logger.error("Unable to account {}", taskUsageRecord, e);
}
}
}