refs #200: Create accouting-lib library

https://support.d4science.org/issues/200
Implementing aggregation facilities 

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@115510 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2015-06-24 13:24:57 +00:00
parent 47ade73adc
commit e6ea9f388d
8 changed files with 53 additions and 68 deletions

View File

@ -11,6 +11,6 @@ import org.gcube.accounting.exception.InvalidValueException;
public interface AggregatedUsageRecord<T extends AggregatedUsageRecord<T, B>, B extends SingleUsageRecord> extends SingleUsageRecord { public interface AggregatedUsageRecord<T extends AggregatedUsageRecord<T, B>, B extends SingleUsageRecord> extends SingleUsageRecord {
public T getAggregatedUsageRecord(B b) throws InvalidValueException ; public T getAggregatedUsageRecord(B usageRecord) throws InvalidValueException ;
} }

View File

@ -13,7 +13,6 @@ import org.gcube.accounting.exception.NotAggregatableRecordsExceptions;
/** /**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/ */
public abstract class AggregationStrategy<T extends AggregatedUsageRecord<T, B>, B extends SingleUsageRecord> { public abstract class AggregationStrategy<T extends AggregatedUsageRecord<T, B>, B extends SingleUsageRecord> {
@ -23,8 +22,10 @@ public abstract class AggregationStrategy<T extends AggregatedUsageRecord<T, B>,
public AggregationStrategy(T t){ public AggregationStrategy(T t){
this.t = t; this.t = t;
this.aggregationField = new HashSet<String>(); this.aggregationField = new HashSet<String>();
this.aggregationField.add(BasicUsageRecord.CONSUMER_ID);
this.aggregationField.add(BasicUsageRecord.USAGE_RECORD_TYPE); this.aggregationField.add(BasicUsageRecord.USAGE_RECORD_TYPE);
this.aggregationField.add(BasicUsageRecord.SCOPE); this.aggregationField.add(BasicUsageRecord.SCOPE);
this.aggregationField.add(BasicUsageRecord.OPERATION_RESULT);
} }
protected String commonFieldHash(B record) { protected String commonFieldHash(B record) {

View File

@ -62,6 +62,7 @@ public abstract class BasicUsageRecord implements UsageRecord, Serializable {
protected static final String START_TIME = "startTime"; protected static final String START_TIME = "startTime";
@AggregatedField @ValidLong @AggregatedField @ValidLong
protected static final String END_TIME = "endTime"; protected static final String END_TIME = "endTime";
@AggregatedField @NotEmptyIfNotNull @AggregatedField @NotEmptyIfNotNull
protected static final String AGGREGATED = "aggregated"; protected static final String AGGREGATED = "aggregated";

View File

@ -12,7 +12,6 @@ import org.gcube.accounting.datamodel.decorators.RequiredField;
import org.gcube.accounting.datamodel.validations.annotations.NotEmpty; import org.gcube.accounting.datamodel.validations.annotations.NotEmpty;
import org.gcube.accounting.datamodel.validations.annotations.NotEmptyIfNotNull; import org.gcube.accounting.datamodel.validations.annotations.NotEmptyIfNotNull;
import org.gcube.accounting.datamodel.validations.annotations.ValidIP; import org.gcube.accounting.datamodel.validations.annotations.ValidIP;
import org.gcube.accounting.datamodel.validations.annotations.ValidInteger;
import org.gcube.accounting.datamodel.validations.annotations.ValidLong; import org.gcube.accounting.datamodel.validations.annotations.ValidLong;
import org.gcube.accounting.datamodel.validations.annotations.ValidOperationType; import org.gcube.accounting.datamodel.validations.annotations.ValidOperationType;
import org.gcube.accounting.exception.InvalidValueException; import org.gcube.accounting.exception.InvalidValueException;
@ -48,10 +47,9 @@ public class StorageUsageRecord extends BasicUsageRecord implements SingleUsageR
public static final String QUALIFIER = "qualifier"; public static final String QUALIFIER = "qualifier";
@NotEmpty @NotEmpty
public static final String DATA_TYPE = "dataType"; public static final String DATA_TYPE = "dataType";
@ValidLong @RequiredField @ValidLong
public static final String DATA_VOLUME = "dataVolume"; public static final String DATA_VOLUME = "dataVolume";
@ValidInteger
public static final String DATA_COUNT = "dataCount";
@ValidIP @ValidIP
public static final String CALLER_IP = "callerIP"; public static final String CALLER_IP = "callerIP";
@ -136,12 +134,6 @@ public class StorageUsageRecord extends BasicUsageRecord implements SingleUsageR
setResourceProperty(DATA_VOLUME, dataVolume); setResourceProperty(DATA_VOLUME, dataVolume);
} }
public int getDataCount() {
return (Integer) this.resourceProperties.get(DATA_COUNT);
}
public void setDataCount(int dataCount) throws InvalidValueException {
setResourceProperty(DATA_COUNT, dataCount);
}
} }

View File

@ -4,6 +4,7 @@
package org.gcube.accounting.datamodel.implementations.aggregated; package org.gcube.accounting.datamodel.implementations.aggregated;
import java.io.Serializable; import java.io.Serializable;
import java.util.Calendar;
import java.util.Map; import java.util.Map;
import org.gcube.accounting.datamodel.AggregatedUsageRecord; import org.gcube.accounting.datamodel.AggregatedUsageRecord;
@ -24,11 +25,11 @@ public class ServiceUsageRecord extends org.gcube.accounting.datamodel.implement
*/ */
private static final long serialVersionUID = 6387584974618335623L; private static final long serialVersionUID = 6387584974618335623L;
@ValidInteger @AggregatedField @RequiredField @RequiredField @ValidInteger @AggregatedField
protected static final String INVOCATION_COUNT = "invocationCount"; protected static final String INVOCATION_COUNT = "invocationCount";
@ValidLong @AggregatedField @RequiredField @RequiredField @ValidLong @AggregatedField
protected static final String MAX_INVOCATION_TIME = "maxInvocationTime"; protected static final String MAX_INVOCATION_TIME = "maxInvocationTime";
@ValidLong @AggregatedField @RequiredField @RequiredField @ValidLong @AggregatedField
protected static final String MIN_INVOCATION_TIME = "minInvocationTime"; protected static final String MIN_INVOCATION_TIME = "minInvocationTime";
public ServiceUsageRecord(){ public ServiceUsageRecord(){
@ -36,17 +37,20 @@ public class ServiceUsageRecord extends org.gcube.accounting.datamodel.implement
this.resourceProperties.put(AGGREGATED, true); this.resourceProperties.put(AGGREGATED, true);
} }
public ServiceUsageRecord(Map<String, Serializable> properties) throws InvalidValueException{ protected ServiceUsageRecord(Map<String, Serializable> properties) throws InvalidValueException{
super(properties); super(properties);
this.resourceProperties.put(AGGREGATED, true); this.resourceProperties.put(AGGREGATED, true);
} }
protected ServiceUsageRecord(org.gcube.accounting.datamodel.implementations.ServiceUsageRecord record) throws InvalidValueException{ public ServiceUsageRecord(org.gcube.accounting.datamodel.implementations.ServiceUsageRecord record) throws InvalidValueException{
super(record.getResourceProperties()); super(record.getResourceProperties());
this.setInvocationCount(1); this.setInvocationCount(1);
long duration = record.getDuration(); long duration = record.getDuration();
this.setMinInvocationTime(duration); this.setMinInvocationTime(duration);
this.setMaxInvocationTime(duration); this.setMaxInvocationTime(duration);
Calendar creationTime = record.getCreationTime();
this.setStartTime(creationTime);
this.setEndTime(creationTime);
this.resourceProperties.put(AGGREGATED, true); this.resourceProperties.put(AGGREGATED, true);
} }
@ -74,40 +78,17 @@ public class ServiceUsageRecord extends org.gcube.accounting.datamodel.implement
setResourceProperty(MIN_INVOCATION_TIME, minInvocationTime); setResourceProperty(MIN_INVOCATION_TIME, minInvocationTime);
} }
/*
public void aggregate(ServiceUsageRecord record) {
try {
this.setInvocationCount(this.getInvocationCount() + record.getInvocationCount());
this.setDuration((this.getDuration() + record.getDuration()) / 2);
long max = ((ServiceUsageRecord) record).getMaxInvocationTime();
if(max > this.getMaxInvocationTime()){
this.setMaxInvocationTime(max);
}
long min = ((ServiceUsageRecord) record).getMinInvocationTime();
if(min < this.getMinInvocationTime()){
this.setMinInvocationTime(min);
}
}catch(Exception e){
throw new UnsupportedOperationException(e.getCause());
}
}
*/
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public ServiceUsageRecord getAggregatedUsageRecord( public ServiceUsageRecord getAggregatedUsageRecord(
org.gcube.accounting.datamodel.implementations.ServiceUsageRecord record) org.gcube.accounting.datamodel.implementations.ServiceUsageRecord usageRecord)
throws InvalidValueException { throws InvalidValueException {
if(record instanceof ServiceUsageRecord){ if(usageRecord instanceof ServiceUsageRecord){
return (ServiceUsageRecord) record; return (ServiceUsageRecord) usageRecord;
} }
return new ServiceUsageRecord(record); return new ServiceUsageRecord(usageRecord);
} }
} }

View File

@ -7,6 +7,9 @@ import java.io.Serializable;
import java.util.Map; import java.util.Map;
import org.gcube.accounting.datamodel.AggregatedUsageRecord; import org.gcube.accounting.datamodel.AggregatedUsageRecord;
import org.gcube.accounting.datamodel.decorators.AggregatedField;
import org.gcube.accounting.datamodel.decorators.RequiredField;
import org.gcube.accounting.datamodel.validations.annotations.ValidInteger;
import org.gcube.accounting.exception.InvalidValueException; import org.gcube.accounting.exception.InvalidValueException;
/** /**
@ -20,6 +23,12 @@ public class StorageUsageRecord extends org.gcube.accounting.datamodel.implement
*/ */
private static final long serialVersionUID = 1082525518686785682L; private static final long serialVersionUID = 1082525518686785682L;
/**
* Indicate The Number of Aggregated Operation
*/
@RequiredField @ValidInteger @AggregatedField
public static final String OPERATION_COUNT = "operationCount";
public StorageUsageRecord(){ public StorageUsageRecord(){
super(); super();
this.resourceProperties.put(AGGREGATED, true); this.resourceProperties.put(AGGREGATED, true);
@ -30,15 +39,30 @@ public class StorageUsageRecord extends org.gcube.accounting.datamodel.implement
this.resourceProperties.put(AGGREGATED, true); this.resourceProperties.put(AGGREGATED, true);
} }
protected StorageUsageRecord(org.gcube.accounting.datamodel.implementations.StorageUsageRecord usageRecord) throws InvalidValueException{
super(usageRecord.getResourceProperties());
// TODO
}
public int getOperationCount() {
return (Integer) this.resourceProperties.get(OPERATION_COUNT);
}
public void setOperationCount(int operationCount) throws InvalidValueException {
setResourceProperty(OPERATION_COUNT, operationCount);
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public StorageUsageRecord getAggregatedUsageRecord( public StorageUsageRecord getAggregatedUsageRecord(
org.gcube.accounting.datamodel.implementations.StorageUsageRecord b) org.gcube.accounting.datamodel.implementations.StorageUsageRecord usageRecord)
throws InvalidValueException { throws InvalidValueException {
// TODO Auto-generated method stub if(usageRecord instanceof StorageUsageRecord){
return null; return (StorageUsageRecord) usageRecord;
}
return new StorageUsageRecord(usageRecord);
} }
} }

View File

@ -8,7 +8,7 @@ import java.util.ServiceLoader;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import org.gcube.accounting.datamodel.BasicUsageRecord; import org.gcube.accounting.datamodel.SingleUsageRecord;
import org.gcube.accounting.datamodel.UsageRecord; import org.gcube.accounting.datamodel.UsageRecord;
import org.gcube.accounting.datamodel.implementations.ServiceUsageRecord; import org.gcube.accounting.datamodel.implementations.ServiceUsageRecord;
import org.gcube.accounting.exception.InvalidValueException; import org.gcube.accounting.exception.InvalidValueException;
@ -81,33 +81,19 @@ public abstract class Persistence {
} }
} }
public static BasicUsageRecord createTestUsageRecord(){ public static SingleUsageRecord createTestUsageRecord(){
ServiceUsageRecord serviceUsageRecord = new ServiceUsageRecord(); ServiceUsageRecord serviceUsageRecord = new ServiceUsageRecord();
try { try {
serviceUsageRecord.setConsumerId("accounting"); serviceUsageRecord.setConsumerId("accounting");
serviceUsageRecord.setScope("/gcube/devsec"); serviceUsageRecord.setScope("/gcube/devsec");
//Calendar creationTime = new GregorianCalendar();
//Calendar startTime = new GregorianCalendar();
//Calendar endTime = new GregorianCalendar();
//serviceUsageRecord.setCreationTime(creationTime);
//usageRecord.setStartTime(startTime);
//usageRecord.setEndTime(endTime);
serviceUsageRecord.setResourceProperty("ConnectionTest", "Test"); serviceUsageRecord.setResourceProperty("ConnectionTest", "Test");
serviceUsageRecord.setServiceClass("Accounting"); serviceUsageRecord.setServiceClass("Accounting");
serviceUsageRecord.setServiceName("Accounting-Lib"); serviceUsageRecord.setServiceName("Accounting-Lib");
serviceUsageRecord.setRefHost("localhost"); serviceUsageRecord.setRefHost("localhost");
serviceUsageRecord.setRefVM("local"); serviceUsageRecord.setRefVM("local");
} catch (InvalidValueException e1) { } catch (InvalidValueException e1) {
} }
return serviceUsageRecord; return serviceUsageRecord;
} }
@ -158,7 +144,7 @@ public abstract class Persistence {
*/ */
protected abstract void reallyAccount(UsageRecord usageRecord) throws Exception; protected abstract void reallyAccount(UsageRecord usageRecord) throws Exception;
private void accountWithFallback(BasicUsageRecord usageRecord) throws Exception { private void accountWithFallback(SingleUsageRecord usageRecord) throws Exception {
String persistenceName = getInstance().getClass().getSimpleName(); String persistenceName = getInstance().getClass().getSimpleName();
try { try {
//logger.debug("Going to account {} using {}", usageRecord, persistenceName); //logger.debug("Going to account {} using {}", usageRecord, persistenceName);
@ -187,7 +173,7 @@ public abstract class Persistence {
* so that the {@link #UsageRecord} can be recorder later. * so that the {@link #UsageRecord} can be recorder later.
* @param usageRecord the {@link #UsageRecord} to persist * @param usageRecord the {@link #UsageRecord} to persist
*/ */
public void account(final BasicUsageRecord usageRecord){ public void account(final SingleUsageRecord usageRecord){
Runnable runnable = new Runnable(){ Runnable runnable = new Runnable(){
@Override @Override
public void run(){ public void run(){

View File

@ -6,7 +6,7 @@ package org.gcube.accounting.datamodel.persistence;
import java.util.Calendar; import java.util.Calendar;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import org.gcube.accounting.datamodel.BasicUsageRecord; import org.gcube.accounting.datamodel.SingleUsageRecord;
import org.gcube.accounting.persistence.Persistence; import org.gcube.accounting.persistence.Persistence;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -36,7 +36,7 @@ public class PersistenceTest {
int quantity = 3000; int quantity = 3000;
Calendar startTestTime = new GregorianCalendar(); Calendar startTestTime = new GregorianCalendar();
for(int i=0; i< quantity; i++){ for(int i=0; i< quantity; i++){
BasicUsageRecord usageRecord = Persistence.createTestUsageRecord(); SingleUsageRecord usageRecord = Persistence.createTestUsageRecord();
persistence.account(usageRecord); persistence.account(usageRecord);
} }
Calendar stopTestTime = new GregorianCalendar(); Calendar stopTestTime = new GregorianCalendar();