From 9050adfdd11dd1a5643f99c2c6812ee49b8a7eee Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 28 Jan 2016 09:42:30 +0000 Subject: [PATCH] Improving Model git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@122578 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../basetypes/AbstractJobUsageRecord.java | 40 ++-- .../basetypes/AbstractTaskUsageRecord.java | 226 ++++++------------ ...ion.java => CalculateJobWallDuration.java} | 4 +- ...va => CalculateJobWallDurationAction.java} | 4 +- .../basetypes/CalculateTaskWallDuration.java | 16 ++ .../CalculateTaskWallDurationAction.java | 30 +++ .../datamodel/basetypes/TestUsageRecord.java | 1 - .../validations/annotations/ValidMap.java | 16 ++ .../validators/ValidMapValidator.java | 33 +++ 9 files changed, 191 insertions(+), 179 deletions(-) rename src/main/java/org/gcube/accounting/datamodel/basetypes/{CalculateWallDuration.java => CalculateJobWallDuration.java} (77%) rename src/main/java/org/gcube/accounting/datamodel/basetypes/{CalculateWallDurationAction.java => CalculateJobWallDurationAction.java} (91%) create mode 100644 src/main/java/org/gcube/accounting/datamodel/basetypes/CalculateTaskWallDuration.java create mode 100644 src/main/java/org/gcube/accounting/datamodel/basetypes/CalculateTaskWallDurationAction.java create mode 100644 src/main/java/org/gcube/accounting/datamodel/validations/annotations/ValidMap.java create mode 100644 src/main/java/org/gcube/accounting/datamodel/validations/validators/ValidMapValidator.java diff --git a/src/main/java/org/gcube/accounting/datamodel/basetypes/AbstractJobUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/basetypes/AbstractJobUsageRecord.java index 47a34cc..6cbacab 100644 --- a/src/main/java/org/gcube/accounting/datamodel/basetypes/AbstractJobUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/basetypes/AbstractJobUsageRecord.java @@ -8,13 +8,11 @@ import java.util.Calendar; import java.util.Map; import org.gcube.accounting.datamodel.BasicUsageRecord; -import org.gcube.accounting.datamodel.backwardcompatibility.MoveToOperationResult; import org.gcube.documentstore.exception.InvalidValueException; -import org.gcube.documentstore.records.AggregatedRecord; import org.gcube.documentstore.records.implementation.ComputedField; import org.gcube.documentstore.records.implementation.RequiredField; import org.gcube.documentstore.records.implementation.validations.annotations.NotEmpty; -import org.gcube.documentstore.records.implementation.validations.annotations.ValidInteger; +import org.gcube.documentstore.records.implementation.validations.annotations.NotEmptyIfNotNull; import org.gcube.documentstore.records.implementation.validations.annotations.ValidLong; /** @@ -30,21 +28,19 @@ public abstract class AbstractJobUsageRecord extends BasicUsageRecord { @RequiredField @NotEmpty public static final String JOB_ID = "jobId"; - @RequiredField @NotEmpty - public static final String JOB_QUALIFIER = "jobQualifier"; - @RequiredField @NotEmpty - public static final String JOB_NAME = "jobName"; - @RequiredField @ValidLong @CalculateWallDuration - public static final String JOB_START_TIME = "jobStartTime"; - @RequiredField @ValidLong @CalculateWallDuration - public static final String JOB_END_TIME = "jobEndTime"; - @MoveToOperationResult @Deprecated - protected static final String JOB_STATUS = "jobStatus"; - @ValidInteger - public static final String VMS_USED = "vmsUsed"; - @ComputedField @ValidLong @CalculateWallDuration - protected static final String WALL_DURATION = "wallDuration"; + @NotEmptyIfNotNull + public static final String JOB_NAME = "jobName"; + @NotEmptyIfNotNull + public static final String JOB_QUALIFIER = "jobQualifier"; + + @RequiredField @ValidLong @CalculateJobWallDuration + public static final String JOB_START_TIME = "jobStartTime"; + @RequiredField @ValidLong @CalculateJobWallDuration + public static final String JOB_END_TIME = "jobEndTime"; + @RequiredField @ValidLong @CalculateJobWallDuration @ComputedField + protected static final String WALL_DURATION = "_wallDuration"; + public AbstractJobUsageRecord(){ super(); @@ -112,14 +108,6 @@ public abstract class AbstractJobUsageRecord extends BasicUsageRecord { public void setJobEndTime(Calendar jobEndTime) throws InvalidValueException { setResourceProperty(JOB_END_TIME, jobEndTime.getTimeInMillis()); } - - public int getVmsUsed() { - return (Integer) this.resourceProperties.get(VMS_USED); - } - - public void setVmsUsed(int vmsUsed) throws InvalidValueException { - setResourceProperty(VMS_USED, vmsUsed); - } protected long calculateWallDuration() throws InvalidValueException { try { @@ -129,7 +117,7 @@ public abstract class AbstractJobUsageRecord extends BasicUsageRecord { return wallDuration; }catch(Exception e){ throw new InvalidValueException(String.format("To calculate Wall Duration both %s and %s must be set", - AggregatedRecord.START_TIME, AggregatedRecord.END_TIME), e); + JOB_START_TIME, JOB_END_TIME), e); } } diff --git a/src/main/java/org/gcube/accounting/datamodel/basetypes/AbstractTaskUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/basetypes/AbstractTaskUsageRecord.java index b1fe002..805cc3b 100644 --- a/src/main/java/org/gcube/accounting/datamodel/basetypes/AbstractTaskUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/basetypes/AbstractTaskUsageRecord.java @@ -8,14 +8,11 @@ import java.util.Calendar; import java.util.Map; import org.gcube.accounting.datamodel.BasicUsageRecord; -import org.gcube.accounting.datamodel.backwardcompatibility.MoveToOperationResult; -import org.gcube.accounting.datamodel.backwardcompatibility.MoveToTaskEndTime; -import org.gcube.accounting.datamodel.backwardcompatibility.MoveToTaskStartTime; -import org.gcube.accounting.datamodel.deprecationmanagement.DeprecatedWarning; import org.gcube.documentstore.exception.InvalidValueException; +import org.gcube.documentstore.records.implementation.ComputedField; import org.gcube.documentstore.records.implementation.RequiredField; import org.gcube.documentstore.records.implementation.validations.annotations.NotEmpty; -import org.gcube.documentstore.records.implementation.validations.annotations.ValidInteger; +import org.gcube.documentstore.records.implementation.validations.annotations.NotEmptyIfNotNull; import org.gcube.documentstore.records.implementation.validations.annotations.ValidLong; /** @@ -29,45 +26,26 @@ public abstract class AbstractTaskUsageRecord extends BasicUsageRecord { */ private static final long serialVersionUID = -2208425042550641240L; + @NotEmpty + public static final String TASK_ID = "taskId"; + @NotEmptyIfNotNull + public static final String REF_JOB_ID = "refJobId"; + @RequiredField @NotEmpty - public static final String JOB_ID = "jobId"; - @RequiredField @NotEmpty - public static final String REF_HOST = "refHost"; - @RequiredField @NotEmpty - public static final String REF_VM = "refVM"; + public static final String HOST = "host"; + @NotEmptyIfNotNull + public static final String REF_HOSTING_NODE = "refHostingNode"; - @Deprecated @NotEmpty @DeprecatedWarning - protected static final String DOMAIN = "domain"; - - @Deprecated @DeprecatedWarning @MoveToTaskStartTime - protected static final String USAGE_START_TIME = "usageStartTime"; - @Deprecated @DeprecatedWarning @MoveToTaskEndTime - protected static final String USAGE_END_TIME = "usageEndTime"; - - @Deprecated @MoveToOperationResult @DeprecatedWarning - protected static final String USAGE_PHASE = "usagePhase"; - - @RequiredField @ValidLong + @RequiredField @ValidLong @CalculateTaskWallDuration public static final String TASK_START_TIME = "taskStartTime"; - @RequiredField @ValidLong + @RequiredField @ValidLong @CalculateTaskWallDuration public static final String TASK_END_TIME = "taskEndTime"; - @Deprecated @ValidInteger @DeprecatedWarning - protected static final String INPUT_FILES_NUMBER = "inputFilesNumber"; - @Deprecated @ValidLong @DeprecatedWarning - protected static final String INPUT_FILES_SIZE = "inputFilesSize"; - @Deprecated @ValidInteger @DeprecatedWarning - protected static final String OUTPUT_FILES_NUMBER = "outputFilesNumber"; - @Deprecated @ValidLong @DeprecatedWarning - protected static final String OUTPUT_FILES_SIZE = "outputFilesSize"; - @Deprecated @ValidLong @DeprecatedWarning - protected static final String OVERALL_NETWORK_IN = "overallNetworkIn"; - @Deprecated @ValidLong @DeprecatedWarning - protected static final String OVERALL_NETWORK_OUT = "overallNetworkOut"; - @Deprecated @ValidInteger @DeprecatedWarning - protected static final String CORES = "cores"; - @Deprecated @ValidInteger @DeprecatedWarning - protected static final String PROCESSORS = "processors"; + @RequiredField @ValidLong @CalculateTaskWallDuration @ComputedField + protected static final String WALL_DURATION = "wallDuration"; + + @RequiredField //@ValidMap + public static final String INPUT_PARAMETERS = "inputParameters"; public AbstractTaskUsageRecord(){ super(); @@ -88,57 +66,51 @@ public abstract class AbstractTaskUsageRecord extends BasicUsageRecord { } /** - * @return the Job Id + * @return the Task Id */ - public String getJobId() { - return (String) this.resourceProperties.get(JOB_ID); + public String getTaskId() { + return (String) this.resourceProperties.get(TASK_ID); } /** - * @param jobId Job Id + * @param taskId Task Id * @throws InvalidValueException if fails */ - public void setJobId(String jobId) throws InvalidValueException { - setResourceProperty(JOB_ID, jobId); + public void setTaskId(String taskId) throws InvalidValueException { + setResourceProperty(TASK_ID, taskId); } - public String getRefHost() { - return (String) this.resourceProperties.get(REF_HOST); + /** + * @return the Referenced Job Id + */ + public String getRefJobId() { + return (String) this.resourceProperties.get(REF_JOB_ID); } - public void setRefHost(String refHost) throws InvalidValueException { - setResourceProperty(REF_HOST, refHost); - } - - public String getRefVM() { - return (String) this.resourceProperties.get(REF_VM); - } - - public void setRefVM(String refVM) throws InvalidValueException { - setResourceProperty(REF_VM, refVM); - } - - @Deprecated - public Calendar getUsageStartTime() { - long millis = (Long) this.resourceProperties.get(USAGE_START_TIME); - return timestampToCalendar(millis); - } - @Deprecated - public void setUsageStartTime(Calendar usageStartTime) throws InvalidValueException { - setResourceProperty(USAGE_START_TIME, usageStartTime.getTimeInMillis()); - - } - @Deprecated - public Calendar getUsageEndTime() { - long millis = (Long) this.resourceProperties.get(USAGE_END_TIME); - return timestampToCalendar(millis); - } - @Deprecated - public void setUsageEndTime(Calendar usageEndTime) throws InvalidValueException { - setResourceProperty(USAGE_END_TIME, usageEndTime.getTimeInMillis()); - + /** + * @param refJobId Referenced Job Id + * @throws InvalidValueException if fails + */ + public void setRefJobId(String refJobId) throws InvalidValueException { + setResourceProperty(REF_JOB_ID, refJobId); } + public String getHost() { + return (String) this.resourceProperties.get(HOST); + } + + public void setRefHost(String host) throws InvalidValueException { + setResourceProperty(HOST, host); + } + + public String getRefHostingNode() { + return (String) this.resourceProperties.get(REF_HOSTING_NODE); + } + + public void setRefHostingNode(String refHostingNode) throws InvalidValueException { + setResourceProperty(REF_HOSTING_NODE, refHostingNode); + } + public Calendar getTaskStartTime() { long millis = (Long) this.resourceProperties.get(TASK_START_TIME); return timestampToCalendar(millis); @@ -156,82 +128,40 @@ public abstract class AbstractTaskUsageRecord extends BasicUsageRecord { public void setTaskEndTime(Calendar endTime) throws InvalidValueException { setResourceProperty(TASK_END_TIME, endTime.getTimeInMillis()); - } - /* - public USAGE_PHASE getUsagePhase() { - return (USAGE_PHASE) this.resourceProperties.get(USAGE_PHASE); + protected long calculateWallDuration() throws InvalidValueException { + try { + long endTime = (Long) this.resourceProperties.get(TASK_END_TIME); + long startTime = (Long) this.resourceProperties.get(TASK_START_TIME); + long wallDuration = endTime - startTime; + return wallDuration; + }catch(Exception e){ + throw new InvalidValueException(String.format("To calculate Wall Duration both %s and %s must be set", + TASK_START_TIME, TASK_END_TIME), e); + } } - - public void setUsagePhase(USAGE_PHASE usagePhase) throws InvalidValueException { - setResourceProperty(USAGE_PHASE, usagePhase); - } - */ - @Deprecated - public int getInputFilesNumber() { - return (Integer) this.resourceProperties.get(INPUT_FILES_NUMBER); + public long getWallDuration() throws InvalidValueException { + Long wallDuration = (Long) this.resourceProperties.get(WALL_DURATION); + if(wallDuration == null){ + try { + wallDuration = calculateWallDuration(); + } catch(InvalidValueException e){ + throw e; + } + } + return wallDuration; } - @Deprecated - public void setInputFilesNumber(int inputFilesNumber) throws InvalidValueException { - setResourceProperty(INPUT_FILES_NUMBER, inputFilesNumber); + + @SuppressWarnings("unchecked") + public Map getInputParameters(){ + return (Map) getResourceProperty(INPUT_PARAMETERS); } - @Deprecated - public long getInputFilesSize() { - return (Long) this.resourceProperties.get(INPUT_FILES_SIZE); - } - @Deprecated - public void setInputFilesSize(long inputFilesSize) throws InvalidValueException { - setResourceProperty(INPUT_FILES_SIZE, inputFilesSize); - } - @Deprecated - public int getOutputFilesNumber() { - return (Integer) this.resourceProperties.get(OUTPUT_FILES_NUMBER); - } - @Deprecated - public void setOutputFilesNumber(int outputFilesNumber) throws InvalidValueException { - setResourceProperty(OUTPUT_FILES_NUMBER, outputFilesNumber); - } - @Deprecated - public long getOutputFilesSize() { - return (Long) this.resourceProperties.get(OUTPUT_FILES_SIZE); - } - @Deprecated - public void setOutputFilesSize(long outputFilesSize) throws InvalidValueException { - setResourceProperty(OUTPUT_FILES_SIZE, outputFilesSize); - } - @Deprecated - public long getOverallNetworkIn() { - return (Long) this.resourceProperties.get(OVERALL_NETWORK_IN); - } - @Deprecated - public void setOverallNetworkIn(long overallNetworkIn) throws InvalidValueException { - setResourceProperty(OVERALL_NETWORK_IN, overallNetworkIn); - } - @Deprecated - public long getOverallNetworkOut() { - return (Long) this.resourceProperties.get(OVERALL_NETWORK_OUT); - } - @Deprecated - public void setOverallNetworkOut(long overallNetworkOut) throws InvalidValueException { - setResourceProperty(OVERALL_NETWORK_OUT, overallNetworkOut); - } - @Deprecated - public int getCores() { - return (Integer) this.resourceProperties.get(CORES); - } - @Deprecated - public void setCores(int cores) throws InvalidValueException { - setResourceProperty(CORES, cores); - } - @Deprecated - public int getProcessors() { - return (Integer) this.resourceProperties.get(PROCESSORS); - } - @Deprecated - public void setProcessors(int processors) throws InvalidValueException { - setResourceProperty(PROCESSORS, processors); + + @SuppressWarnings("unchecked") + public void setInputParameters(Map inputParameters) throws InvalidValueException{ + setResourceProperty(INPUT_PARAMETERS, (Comparable) inputParameters); } } diff --git a/src/main/java/org/gcube/accounting/datamodel/basetypes/CalculateWallDuration.java b/src/main/java/org/gcube/accounting/datamodel/basetypes/CalculateJobWallDuration.java similarity index 77% rename from src/main/java/org/gcube/accounting/datamodel/basetypes/CalculateWallDuration.java rename to src/main/java/org/gcube/accounting/datamodel/basetypes/CalculateJobWallDuration.java index 211656e..ce6cf27 100644 --- a/src/main/java/org/gcube/accounting/datamodel/basetypes/CalculateWallDuration.java +++ b/src/main/java/org/gcube/accounting/datamodel/basetypes/CalculateJobWallDuration.java @@ -12,5 +12,5 @@ import org.gcube.documentstore.records.implementation.FieldDecorator; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) -@FieldDecorator(action=CalculateWallDurationAction.class) -public @interface CalculateWallDuration {} \ No newline at end of file +@FieldDecorator(action=CalculateJobWallDurationAction.class) +public @interface CalculateJobWallDuration {} \ No newline at end of file diff --git a/src/main/java/org/gcube/accounting/datamodel/basetypes/CalculateWallDurationAction.java b/src/main/java/org/gcube/accounting/datamodel/basetypes/CalculateJobWallDurationAction.java similarity index 91% rename from src/main/java/org/gcube/accounting/datamodel/basetypes/CalculateWallDurationAction.java rename to src/main/java/org/gcube/accounting/datamodel/basetypes/CalculateJobWallDurationAction.java index 2761f4c..3339180 100644 --- a/src/main/java/org/gcube/accounting/datamodel/basetypes/CalculateWallDurationAction.java +++ b/src/main/java/org/gcube/accounting/datamodel/basetypes/CalculateJobWallDurationAction.java @@ -11,9 +11,9 @@ import org.gcube.documentstore.records.implementation.FieldAction; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class CalculateWallDurationAction implements FieldAction { +public class CalculateJobWallDurationAction implements FieldAction { - private static final Logger logger = LoggerFactory.getLogger(CalculateWallDurationAction.class); + private static final Logger logger = LoggerFactory.getLogger(CalculateJobWallDurationAction.class); @Override public Comparable validate(String key, Comparable value, Record record) throws InvalidValueException { diff --git a/src/main/java/org/gcube/accounting/datamodel/basetypes/CalculateTaskWallDuration.java b/src/main/java/org/gcube/accounting/datamodel/basetypes/CalculateTaskWallDuration.java new file mode 100644 index 0000000..1182863 --- /dev/null +++ b/src/main/java/org/gcube/accounting/datamodel/basetypes/CalculateTaskWallDuration.java @@ -0,0 +1,16 @@ +/** + * + */ +package org.gcube.accounting.datamodel.basetypes; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.gcube.documentstore.records.implementation.FieldDecorator; + +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +@FieldDecorator(action=CalculateTaskWallDurationAction.class) +public @interface CalculateTaskWallDuration {} \ No newline at end of file diff --git a/src/main/java/org/gcube/accounting/datamodel/basetypes/CalculateTaskWallDurationAction.java b/src/main/java/org/gcube/accounting/datamodel/basetypes/CalculateTaskWallDurationAction.java new file mode 100644 index 0000000..7d5baac --- /dev/null +++ b/src/main/java/org/gcube/accounting/datamodel/basetypes/CalculateTaskWallDurationAction.java @@ -0,0 +1,30 @@ +/** + * + */ +package org.gcube.accounting.datamodel.basetypes; + +import java.io.Serializable; + +import org.gcube.documentstore.exception.InvalidValueException; +import org.gcube.documentstore.records.Record; +import org.gcube.documentstore.records.implementation.FieldAction; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class CalculateTaskWallDurationAction implements FieldAction { + + private static final Logger logger = LoggerFactory.getLogger(CalculateTaskWallDurationAction.class); + + @Override + public Comparable validate(String key, Comparable value, Record record) throws InvalidValueException { + try { + long wallDuration = ((AbstractTaskUsageRecord) record).calculateWallDuration(); + if(key.compareTo(AbstractTaskUsageRecord.WALL_DURATION)==0){ + logger.warn("{} is automatically computed using {} and {}. This invocation has the only effect of recalculating the value. Any provided value is ignored.", + AbstractTaskUsageRecord.WALL_DURATION, AbstractTaskUsageRecord.TASK_START_TIME, AbstractTaskUsageRecord.TASK_END_TIME); + value = wallDuration; + } + }catch(InvalidValueException e){ } + return value; + } +} \ No newline at end of file diff --git a/src/main/java/org/gcube/accounting/datamodel/basetypes/TestUsageRecord.java b/src/main/java/org/gcube/accounting/datamodel/basetypes/TestUsageRecord.java index f87ac24..2a6061e 100644 --- a/src/main/java/org/gcube/accounting/datamodel/basetypes/TestUsageRecord.java +++ b/src/main/java/org/gcube/accounting/datamodel/basetypes/TestUsageRecord.java @@ -196,7 +196,6 @@ public class TestUsageRecord { usageRecord.setJobEndTime(endTime); usageRecord.setOperationResult(OperationResult.SUCCESS); - usageRecord.setVmsUsed(TEST_VMS_USED); } catch (InvalidValueException e) { logger.error(" ------ You SHOULD NOT SEE THIS MESSAGE. Error Creating a test Usage Record", e); diff --git a/src/main/java/org/gcube/accounting/datamodel/validations/annotations/ValidMap.java b/src/main/java/org/gcube/accounting/datamodel/validations/annotations/ValidMap.java new file mode 100644 index 0000000..571ab5b --- /dev/null +++ b/src/main/java/org/gcube/accounting/datamodel/validations/annotations/ValidMap.java @@ -0,0 +1,16 @@ +package org.gcube.accounting.datamodel.validations.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.gcube.accounting.datamodel.validations.validators.ValidMapValidator; +import org.gcube.documentstore.records.implementation.FieldDecorator; + +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +@FieldDecorator(action=ValidMapValidator.class) +public @interface ValidMap { + +} diff --git a/src/main/java/org/gcube/accounting/datamodel/validations/validators/ValidMapValidator.java b/src/main/java/org/gcube/accounting/datamodel/validations/validators/ValidMapValidator.java new file mode 100644 index 0000000..2b94a3c --- /dev/null +++ b/src/main/java/org/gcube/accounting/datamodel/validations/validators/ValidMapValidator.java @@ -0,0 +1,33 @@ +package org.gcube.accounting.datamodel.validations.validators; + +import java.io.Serializable; +import java.util.Map; + +import org.gcube.documentstore.exception.InvalidValueException; +import org.gcube.documentstore.records.Record; +import org.gcube.documentstore.records.implementation.FieldAction; + +public class ValidMapValidator implements FieldAction { + + private static final String ERROR = String.format("This Map cannot be serilized properly"); + + /** + * {@inheritDoc} + */ + @Override + public Comparable validate(String key, Comparable value, Record record) throws InvalidValueException { + + try { + if(value instanceof Map){ + + } + + }catch(Exception e){ + throw new InvalidValueException(ERROR, e); + } + + throw new InvalidValueException(ERROR); + } + + +}