This commit is contained in:
Alessandro Pieve 2016-09-19 09:06:46 +00:00
parent f71a86ad74
commit 392c09eaae
5 changed files with 91 additions and 34 deletions

View File

@ -13,6 +13,9 @@ import org.gcube.accounting.datamodel.usagerecords.JobUsageRecord;
import org.gcube.documentstore.exception.InvalidValueException; import org.gcube.documentstore.exception.InvalidValueException;
import org.gcube.documentstore.exception.NotAggregatableRecordsExceptions; import org.gcube.documentstore.exception.NotAggregatableRecordsExceptions;
import org.gcube.documentstore.records.aggregation.AggregationUtility; import org.gcube.documentstore.records.aggregation.AggregationUtility;
import org.gcube.documentstore.records.implementation.AggregatedField;
import org.gcube.documentstore.records.implementation.RequiredField;
import org.gcube.documentstore.records.implementation.validations.annotations.ValidLong;
/** /**
* This Class is for library internal use only * This Class is for library internal use only
@ -20,34 +23,66 @@ import org.gcube.documentstore.records.aggregation.AggregationUtility;
*/ */
public class AggregatedJobUsageRecord extends AbstractJobUsageRecord implements AggregatedUsageRecord<AggregatedJobUsageRecord, JobUsageRecord> { public class AggregatedJobUsageRecord extends AbstractJobUsageRecord implements AggregatedUsageRecord<AggregatedJobUsageRecord, JobUsageRecord> {
/** /**
* Generated Serial Version UID * Generated Serial Version UID
*/ */
private static final long serialVersionUID = -3376423316219914682L; private static final long serialVersionUID = -3376423316219914682L;
/*add a field max e min invocation*/
@RequiredField @ValidLong @AggregatedField
public static final String MAX_INVOCATION_TIME = "maxInvocationTime";
@RequiredField @ValidLong @AggregatedField
public static final String MIN_INVOCATION_TIME = "minInvocationTime";
public AggregatedJobUsageRecord(){ public AggregatedJobUsageRecord(){
super(); super();
} }
public AggregatedJobUsageRecord(Map<String, ? extends Serializable> properties) throws InvalidValueException{ public AggregatedJobUsageRecord(Map<String, ? extends Serializable> properties) throws InvalidValueException{
super(properties); super(properties);
} }
// TODO // TODO
public AggregatedJobUsageRecord(JobUsageRecord jobUsageRecord) throws InvalidValueException{ public AggregatedJobUsageRecord(JobUsageRecord record) throws InvalidValueException{
throw new UnsupportedOperationException(); //throw new UnsupportedOperationException();
super(record.getResourceProperties());
this.setOperationCount(1);
long duration = record.getDuration();
this.setMinInvocationTime(duration);
this.setMaxInvocationTime(duration);
Calendar creationTime = record.getCreationTime();
this.setCreationTime(Calendar.getInstance());
this.setStartTime(creationTime);
this.setEndTime(creationTime);
} }
@Override @Override
public int getOperationCount() { public int getOperationCount() {
return super.getOperationCount(); return super.getOperationCount();
} }
@Override @Override
public void setOperationCount(int operationCount) throws InvalidValueException { public void setOperationCount(int operationCount) throws InvalidValueException {
super.setOperationCount(operationCount); super.setOperationCount(operationCount);
} }
public long getMaxInvocationTime() {
return (Long) this.resourceProperties.get(MAX_INVOCATION_TIME);
}
public void setMaxInvocationTime(long maxInvocationTime) throws InvalidValueException {
super.setResourceProperty(MAX_INVOCATION_TIME, maxInvocationTime);
}
public long getMinInvocationTime() {
return (Long) this.resourceProperties.get(MIN_INVOCATION_TIME);
}
public void setMinInvocationTime(long minInvocationTime) throws InvalidValueException {
setResourceProperty(MIN_INVOCATION_TIME, minInvocationTime);
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -55,7 +90,7 @@ public class AggregatedJobUsageRecord extends AbstractJobUsageRecord implements
public Calendar getStartTime() { public Calendar getStartTime() {
return super.getStartTimeAsCalendar(); return super.getStartTimeAsCalendar();
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -63,7 +98,7 @@ public class AggregatedJobUsageRecord extends AbstractJobUsageRecord implements
public void setStartTime(Calendar startTime) throws InvalidValueException { public void setStartTime(Calendar startTime) throws InvalidValueException {
super.setStartTime(startTime); super.setStartTime(startTime);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -71,7 +106,7 @@ public class AggregatedJobUsageRecord extends AbstractJobUsageRecord implements
public Calendar getEndTime() { public Calendar getEndTime() {
return super.getEndTimeAsCalendar(); return super.getEndTimeAsCalendar();
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -80,6 +115,14 @@ public class AggregatedJobUsageRecord extends AbstractJobUsageRecord implements
super.setEndTime(endTime); super.setEndTime(endTime);
} }
protected long durationWeightedAverage(AggregatedJobUsageRecord record) throws InvalidValueException{
long thisDuration = this.getDuration() * this.getOperationCount();
long recordDuration = record.getDuration() * record.getOperationCount();
long totalOperationCount = this.getOperationCount() + record.getOperationCount();
return (thisDuration + recordDuration) / totalOperationCount;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -87,17 +130,26 @@ public class AggregatedJobUsageRecord extends AbstractJobUsageRecord implements
public AggregatedJobUsageRecord aggregate(AggregatedJobUsageRecord record) public AggregatedJobUsageRecord aggregate(AggregatedJobUsageRecord record)
throws NotAggregatableRecordsExceptions { throws NotAggregatableRecordsExceptions {
try { try {
/* TODO
AggregationUtility<AggregatedJobUsageRecord> aggregationUtility = new AggregationUtility<AggregatedJobUsageRecord>(this); AggregationUtility<AggregatedJobUsageRecord> aggregationUtility = new AggregationUtility<AggregatedJobUsageRecord>(this);
setDuration(durationWeightedAverage(record));
long max = record.getMaxInvocationTime();
if(max > this.getMaxInvocationTime()){
this.setMaxInvocationTime(max);
}
long min = record.getMinInvocationTime();
if(min < this.getMinInvocationTime()){
this.setMinInvocationTime(min);
}
aggregationUtility.aggregate(record); aggregationUtility.aggregate(record);
} catch(NotAggregatableRecordsExceptions e){ } catch(NotAggregatableRecordsExceptions e){
throw e; */ throw e;
} catch(Exception ex){ } catch(Exception ex){
throw new NotAggregatableRecordsExceptions(ex); throw new NotAggregatableRecordsExceptions(ex);
} }
//return this; return this;
//throw new UnsupportedOperationException();
throw new UnsupportedOperationException();
} }
/** /**
@ -114,7 +166,7 @@ public class AggregatedJobUsageRecord extends AbstractJobUsageRecord implements
throw new NotAggregatableRecordsExceptions(ex); throw new NotAggregatableRecordsExceptions(ex);
} }
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -124,7 +176,7 @@ public class AggregatedJobUsageRecord extends AbstractJobUsageRecord implements
return aggregationUtility.isAggregable(record); return aggregationUtility.isAggregable(record);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -138,7 +190,7 @@ public class AggregatedJobUsageRecord extends AbstractJobUsageRecord implements
throw new NotAggregatableRecordsExceptions(ex); throw new NotAggregatableRecordsExceptions(ex);
} }
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -147,4 +199,8 @@ public class AggregatedJobUsageRecord extends AbstractJobUsageRecord implements
return JobUsageRecord.class; return JobUsageRecord.class;
} }
@Override
protected void cleanExtraFields(){
return;
}
} }

View File

@ -34,13 +34,13 @@ public abstract class AbstractJobUsageRecord extends BasicUsageRecord {
@NotEmptyIfNotNull @NotEmptyIfNotNull
public static final String JOB_QUALIFIER = "jobQualifier"; public static final String JOB_QUALIFIER = "jobQualifier";
@ValidLong @RequiredField @ValidLong
public static final String JOB_START_TIME = "jobStartTime"; public static final String JOB_START_TIME = "jobStartTime";
@ValidLong @RequiredField @ValidLong
public static final String JOB_END_TIME = "jobEndTime"; public static final String JOB_END_TIME = "jobEndTime";
@RequiredField @ComputedField(action=CalculateJobWallDurationAction.class) @ValidLong @RequiredField @ComputedField(action=CalculateJobWallDurationAction.class) @ValidLong
public static final String WALL_DURATION = "wallDuration"; public static final String DURATION = "duration";
public AbstractJobUsageRecord(){ public AbstractJobUsageRecord(){
super(); super();
@ -114,7 +114,7 @@ public abstract class AbstractJobUsageRecord extends BasicUsageRecord {
long endTime = (Long) this.resourceProperties.get(JOB_END_TIME); long endTime = (Long) this.resourceProperties.get(JOB_END_TIME);
long startTime = (Long) this.resourceProperties.get(JOB_START_TIME); long startTime = (Long) this.resourceProperties.get(JOB_START_TIME);
long wallDuration = endTime - startTime; long wallDuration = endTime - startTime;
setResourceProperty(AbstractJobUsageRecord.WALL_DURATION, wallDuration); setResourceProperty(AbstractJobUsageRecord.DURATION, wallDuration);
return wallDuration; return wallDuration;
}catch(Exception e){ }catch(Exception e){
throw new InvalidValueException(String.format("To calculate Wall Duration both %s and %s must be set", throw new InvalidValueException(String.format("To calculate Wall Duration both %s and %s must be set",
@ -122,8 +122,8 @@ public abstract class AbstractJobUsageRecord extends BasicUsageRecord {
} }
} }
public long getWallDuration() throws InvalidValueException { public long getDuration() throws InvalidValueException {
Long wallDuration = (Long) this.resourceProperties.get(WALL_DURATION); Long wallDuration = (Long) this.resourceProperties.get(DURATION);
if(wallDuration == null){ if(wallDuration == null){
try { try {
wallDuration = calculateWallDuration(); wallDuration = calculateWallDuration();
@ -133,5 +133,8 @@ public abstract class AbstractJobUsageRecord extends BasicUsageRecord {
} }
return wallDuration; return wallDuration;
} }
public void setDuration(Long duration) throws InvalidValueException {
setResourceProperty(DURATION, duration);
}
} }

View File

@ -19,9 +19,9 @@ public class CalculateJobWallDurationAction implements FieldAction {
public Serializable validate(String key, Serializable value, Record record) throws InvalidValueException { public Serializable validate(String key, Serializable value, Record record) throws InvalidValueException {
try { try {
long wallDuration = ((AbstractJobUsageRecord) record).calculateWallDuration(); long wallDuration = ((AbstractJobUsageRecord) record).calculateWallDuration();
if(key.compareTo(AbstractJobUsageRecord.WALL_DURATION)==0){ if(key.compareTo(AbstractJobUsageRecord.DURATION)==0){
logger.warn("{} is automatically computed using {} and {}. This invocation has the only effect of recalculating the value. Any provided value is ignored.", logger.warn("{} is automatically computed using {} and {}. This invocation has the only effect of recalculating the value. Any provided value is ignored.",
AbstractJobUsageRecord.WALL_DURATION, AbstractJobUsageRecord.JOB_START_TIME, AbstractJobUsageRecord.JOB_END_TIME); AbstractJobUsageRecord.DURATION, AbstractJobUsageRecord.JOB_START_TIME, AbstractJobUsageRecord.JOB_END_TIME);
value = wallDuration; value = wallDuration;
} }
}catch(InvalidValueException e){ } }catch(InvalidValueException e){ }

View File

@ -34,7 +34,9 @@ public class JobUsageRecordTest extends ScopedTest {
expectedRequiredFields.add(UsageRecord.SCOPE); expectedRequiredFields.add(UsageRecord.SCOPE);
expectedRequiredFields.add(UsageRecord.OPERATION_RESULT); expectedRequiredFields.add(UsageRecord.OPERATION_RESULT);
expectedRequiredFields.add(AbstractJobUsageRecord.JOB_ID); expectedRequiredFields.add(AbstractJobUsageRecord.JOB_ID);
expectedRequiredFields.add(AbstractJobUsageRecord.WALL_DURATION); expectedRequiredFields.add(AbstractJobUsageRecord.DURATION);
expectedRequiredFields.add(AbstractJobUsageRecord.JOB_END_TIME);
expectedRequiredFields.add(AbstractJobUsageRecord.JOB_START_TIME);
return expectedRequiredFields; return expectedRequiredFields;
} }

View File

@ -37,6 +37,7 @@ public class TestUsageRecord {
public final static String TEST_SERVICE_CLASS = "TestServiceClass"; public final static String TEST_SERVICE_CLASS = "TestServiceClass";
public final static String TEST_SERVICE_NAME = "TestServiceName"; public final static String TEST_SERVICE_NAME = "TestServiceName";
public final static String TEST_CALLED_METHOD = "TestCalledMethod"; public final static String TEST_CALLED_METHOD = "TestCalledMethod";
public final static String TEST_CALLER_QUALIFIER = "TestCallerQualifier";
public final static String TEST_CALLER_HOST = "remotehost"; public final static String TEST_CALLER_HOST = "remotehost";
public final static String TEST_HOST = "localhost"; public final static String TEST_HOST = "localhost";
@ -85,7 +86,7 @@ public class TestUsageRecord {
usageRecord.setCallerHost(TEST_CALLER_HOST); usageRecord.setCallerHost(TEST_CALLER_HOST);
usageRecord.setHost(TEST_HOST); usageRecord.setHost(TEST_HOST);
usageRecord.setCallerQualifier(TEST_CALLER_QUALIFIER);
usageRecord.setServiceClass(TEST_SERVICE_CLASS); usageRecord.setServiceClass(TEST_SERVICE_CLASS);
usageRecord.setServiceName(TEST_SERVICE_NAME); usageRecord.setServiceName(TEST_SERVICE_NAME);
usageRecord.setCalledMethod(TEST_CALLED_METHOD); usageRecord.setCalledMethod(TEST_CALLED_METHOD);
@ -151,11 +152,7 @@ public class TestUsageRecord {
usageRecord.setOperationResult(TEST_OPERATION_RESULT); usageRecord.setOperationResult(TEST_OPERATION_RESULT);
usageRecord.setJobId(TEST_JOB_ID); usageRecord.setJobId(TEST_JOB_ID);
usageRecord.setJobName(TEST_JOB_NAME); usageRecord.setJobName(TEST_JOB_NAME);
usageRecord.setJobQualifier(TEST_JOB_QUALIFIER);
Calendar startTime = Calendar.getInstance(); Calendar startTime = Calendar.getInstance();
Calendar endTime = Calendar.getInstance(); Calendar endTime = Calendar.getInstance();
endTime.setTimeInMillis(startTime.getTimeInMillis() + HALF_DURATION); endTime.setTimeInMillis(startTime.getTimeInMillis() + HALF_DURATION);
@ -164,7 +161,6 @@ public class TestUsageRecord {
usageRecord.setJobStartTime(startTime); usageRecord.setJobStartTime(startTime);
usageRecord.setJobEndTime(endTime); usageRecord.setJobEndTime(endTime);
} catch (InvalidValueException e) { } catch (InvalidValueException e) {
logger.error(" ------ You SHOULD NOT SEE THIS MESSAGE. Error Creating a test Usage Record", e); logger.error(" ------ You SHOULD NOT SEE THIS MESSAGE. Error Creating a test Usage Record", e);
} }