Fixed bug on timeout for buffering records and renamed Persistence* in AccountingPersistence* to avoid name misunderstanding in smartgears

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@117048 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2015-07-07 10:10:52 +00:00
parent cdceacdce8
commit 56be686649
11 changed files with 69 additions and 54 deletions

View File

@ -12,7 +12,7 @@ import org.gcube.accounting.datamodel.AggregationStrategy;
import org.gcube.accounting.datamodel.SingleUsageRecord; import org.gcube.accounting.datamodel.SingleUsageRecord;
import org.gcube.accounting.datamodel.UsageRecord; import org.gcube.accounting.datamodel.UsageRecord;
import org.gcube.accounting.exception.NotAggregatableRecordsExceptions; import org.gcube.accounting.exception.NotAggregatableRecordsExceptions;
import org.gcube.accounting.persistence.PersistenceExecutor; import org.gcube.accounting.persistence.AccountingPersistenceExecutor;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -158,11 +158,20 @@ public abstract class AggregationScheduler {
} }
public void flush(PersistenceExecutor persistenceExecutor) throws Exception{ public void flush(AccountingPersistenceExecutor persistenceExecutor) throws Exception{
aggregate(null, persistenceExecutor, true); aggregate(null, persistenceExecutor, true);
} }
protected synchronized void aggregate(SingleUsageRecord usageRecord, PersistenceExecutor persistenceExecutor, boolean forceFlush) throws Exception { protected abstract void specificClear();
protected void clear(){
totalBufferedRecords=0;
records.clear();
unaggregableRecords.clear();
specificClear();
}
protected synchronized void aggregate(SingleUsageRecord usageRecord, AccountingPersistenceExecutor persistenceExecutor, boolean forceFlush) throws Exception {
if(usageRecord!=null){ if(usageRecord!=null){
madeAggregation(usageRecord); madeAggregation(usageRecord);
} }
@ -185,12 +194,12 @@ public abstract class AggregationScheduler {
persistenceExecutor.persist(recordToPersist); persistenceExecutor.persist(recordToPersist);
totalBufferedRecords=0; clear();
records.clear();
unaggregableRecords.clear();
} }
} }
/** /**
* Get an usage records and try to aggregate with other buffered * Get an usage records and try to aggregate with other buffered
* Usage Record. * Usage Record.
@ -198,7 +207,7 @@ public abstract class AggregationScheduler {
* @return true if is time to persist the buffered Usage Record * @return true if is time to persist the buffered Usage Record
* @throws Exception if fails * @throws Exception if fails
*/ */
public void aggregate(SingleUsageRecord usageRecord, PersistenceExecutor persistenceExecutor) throws Exception { public void aggregate(SingleUsageRecord usageRecord, AccountingPersistenceExecutor persistenceExecutor) throws Exception {
aggregate(usageRecord, persistenceExecutor, false); aggregate(usageRecord, persistenceExecutor, false);
} }

View File

@ -36,6 +36,12 @@ public class BufferAggregationScheduler extends AggregationScheduler {
this.firstOfBuffer = true; this.firstOfBuffer = true;
} }
@Override
protected void specificClear(){
firstOfBuffer = true;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

View File

@ -2,8 +2,8 @@ package org.gcube.accounting.messaging;
import org.gcube.accounting.datamodel.RawUsageRecord; import org.gcube.accounting.datamodel.RawUsageRecord;
import org.gcube.accounting.exception.InvalidValueException; import org.gcube.accounting.exception.InvalidValueException;
import org.gcube.accounting.persistence.Persistence; import org.gcube.accounting.persistence.AccountingPersistence;
import org.gcube.accounting.persistence.PersistenceFactory; import org.gcube.accounting.persistence.AccountingPersistenceFactory;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -19,11 +19,11 @@ public class ResourceAccounting {
private static final Logger logger = LoggerFactory.getLogger(ResourceAccounting.class); private static final Logger logger = LoggerFactory.getLogger(ResourceAccounting.class);
@Deprecated @Deprecated
protected Persistence persistence; protected AccountingPersistence persistence;
@Deprecated @Deprecated
public ResourceAccounting() { public ResourceAccounting() {
persistence = PersistenceFactory.getPersistence(); persistence = AccountingPersistenceFactory.getPersistence();
} }
@Deprecated @Deprecated

View File

@ -16,9 +16,9 @@ import org.slf4j.LoggerFactory;
/** /**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*/ */
public abstract class Persistence { public abstract class AccountingPersistence {
private static final Logger logger = LoggerFactory.getLogger(Persistence.class); private static final Logger logger = LoggerFactory.getLogger(AccountingPersistence.class);
protected FallbackPersistence fallback; protected FallbackPersistence fallback;
protected AggregationScheduler aggregationScheduler; protected AggregationScheduler aggregationScheduler;
@ -28,11 +28,11 @@ public abstract class Persistence {
*/ */
private ExecutorService pool; private ExecutorService pool;
protected Persistence(){ protected AccountingPersistence(){
this.pool = Executors.newCachedThreadPool(); this.pool = Executors.newCachedThreadPool();
} }
protected Persistence(FallbackPersistence fallback, AggregationScheduler aggregationScheduler){ protected AccountingPersistence(FallbackPersistence fallback, AggregationScheduler aggregationScheduler){
this.fallback = fallback; this.fallback = fallback;
this.aggregationScheduler = aggregationScheduler; this.aggregationScheduler = aggregationScheduler;
this.pool = Executors.newCachedThreadPool(); this.pool = Executors.newCachedThreadPool();
@ -59,7 +59,7 @@ public abstract class Persistence {
* @param configuration The configuration to create the connection * @param configuration The configuration to create the connection
* @throws Exception if fails * @throws Exception if fails
*/ */
protected abstract void prepareConnection(PersistenceConfiguration configuration) throws Exception; protected abstract void prepareConnection(AccountingPersistenceConfiguration configuration) throws Exception;
/* * /* *
* Prepare the connection and try to write a test record on default * Prepare the connection and try to write a test record on default
@ -108,8 +108,8 @@ public abstract class Persistence {
usageRecord.validate(); usageRecord.validate();
} }
if(aggregate){ if(aggregate){
final Persistence persistence = this; final AccountingPersistence persistence = this;
aggregationScheduler.aggregate(usageRecord, new PersistenceExecutor(){ aggregationScheduler.aggregate(usageRecord, new AccountingPersistenceExecutor(){
@Override @Override
public void persist(UsageRecord... usageRecords) throws Exception { public void persist(UsageRecord... usageRecords) throws Exception {
@ -149,8 +149,8 @@ public abstract class Persistence {
} }
public void flush() throws Exception { public void flush() throws Exception {
final Persistence persistence = this; final AccountingPersistence persistence = this;
aggregationScheduler.flush(new PersistenceExecutor(){ aggregationScheduler.flush(new AccountingPersistenceExecutor(){
@Override @Override
public void persist(UsageRecord... usageRecords) throws Exception { public void persist(UsageRecord... usageRecords) throws Exception {

View File

@ -21,7 +21,7 @@ import org.gcube.resources.discovery.icclient.ICFactory;
/** /**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*/ */
public class PersistenceConfiguration { public class AccountingPersistenceConfiguration {
protected final static String SERVICE_ENDPOINT_CATEGORY = "Accounting"; protected final static String SERVICE_ENDPOINT_CATEGORY = "Accounting";
protected final static String SERVICE_ENDPOINT_NAME = "Persistence"; protected final static String SERVICE_ENDPOINT_NAME = "Persistence";
@ -34,11 +34,11 @@ public class PersistenceConfiguration {
protected Map<String, Property> propertyMap; protected Map<String, Property> propertyMap;
public PersistenceConfiguration(){ public AccountingPersistenceConfiguration(){
propertyMap = new HashMap<String, Property>(); propertyMap = new HashMap<String, Property>();
} }
public PersistenceConfiguration(URI uri, String username, String password){ public AccountingPersistenceConfiguration(URI uri, String username, String password){
this.uri = uri; this.uri = uri;
this.username = username; this.username = username;
this.password = password; this.password = password;
@ -117,8 +117,8 @@ public class PersistenceConfiguration {
return StringEncrypter.getEncrypter().decrypt(encrypted); return StringEncrypter.getEncrypter().decrypt(encrypted);
} }
protected static PersistenceConfiguration createPersistenceConfiguration(ServiceEndpoint serviceEndpoint) throws Exception{ protected static AccountingPersistenceConfiguration createPersistenceConfiguration(ServiceEndpoint serviceEndpoint) throws Exception{
PersistenceConfiguration persistenceConfiguration = new PersistenceConfiguration(); AccountingPersistenceConfiguration persistenceConfiguration = new AccountingPersistenceConfiguration();
Group<AccessPoint> accessPoints = serviceEndpoint.profile().accessPoints(); Group<AccessPoint> accessPoints = serviceEndpoint.profile().accessPoints();
for(AccessPoint accessPoint : accessPoints){ for(AccessPoint accessPoint : accessPoints){
persistenceConfiguration.uri = new URI(accessPoint.address()); persistenceConfiguration.uri = new URI(accessPoint.address());
@ -140,7 +140,7 @@ public class PersistenceConfiguration {
* @return * @return
* @throws Exception * @throws Exception
*/ */
public static PersistenceConfiguration getPersistenceConfiguration(String persistenceClassName) throws Exception { public static AccountingPersistenceConfiguration getPersistenceConfiguration(String persistenceClassName) throws Exception {
ServiceEndpoint serviceEndpoint = getServiceEndpoint(persistenceClassName); ServiceEndpoint serviceEndpoint = getServiceEndpoint(persistenceClassName);
return createPersistenceConfiguration(serviceEndpoint); return createPersistenceConfiguration(serviceEndpoint);
} }

View File

@ -9,7 +9,7 @@ import org.gcube.accounting.datamodel.UsageRecord;
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
* *
*/ */
public interface PersistenceExecutor { public interface AccountingPersistenceExecutor {
public void persist(UsageRecord... usageRecords)throws Exception; public void persist(UsageRecord... usageRecords)throws Exception;

View File

@ -19,9 +19,9 @@ import org.slf4j.LoggerFactory;
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
* *
*/ */
public abstract class PersistenceFactory { public abstract class AccountingPersistenceFactory {
private static final Logger logger = LoggerFactory.getLogger(PersistenceFactory.class); private static final Logger logger = LoggerFactory.getLogger(AccountingPersistenceFactory.class);
public final static String HOME_SYSTEM_PROPERTY = "user.home"; public final static String HOME_SYSTEM_PROPERTY = "user.home";
@ -29,10 +29,10 @@ public abstract class PersistenceFactory {
private static String fallbackLocation; private static String fallbackLocation;
private static Map<String, Persistence> persistences; private static Map<String, AccountingPersistence> persistences;
static { static {
persistences = new HashMap<String, Persistence>(); persistences = new HashMap<String, AccountingPersistence>();
} }
private static File file(File file) throws IllegalArgumentException { private static File file(File file) throws IllegalArgumentException {
@ -59,7 +59,7 @@ public abstract class PersistenceFactory {
} }
} }
public static Persistence getPersistence() { public static AccountingPersistence getPersistence() {
String scope = ScopeProvider.instance.get(); String scope = ScopeProvider.instance.get();
if(scope==null){ if(scope==null){
logger.error("No Scope available. FallbackPersistence will be used"); logger.error("No Scope available. FallbackPersistence will be used");
@ -67,7 +67,7 @@ public abstract class PersistenceFactory {
return new FallbackPersistence(fallbackFile); return new FallbackPersistence(fallbackFile);
} }
Persistence persistence = persistences.get(scope); AccountingPersistence persistence = persistences.get(scope);
if(persistence==null){ if(persistence==null){
ScopeBean bean = new ScopeBean(scope); ScopeBean bean = new ScopeBean(scope);
@ -79,15 +79,15 @@ public abstract class PersistenceFactory {
File fallbackFile = new File(fallbackLocation, String.format("%s.%s", name, ACCOUTING_FALLBACK_FILENAME)); File fallbackFile = new File(fallbackLocation, String.format("%s.%s", name, ACCOUTING_FALLBACK_FILENAME));
FallbackPersistence fallbackPersistence = new FallbackPersistence(fallbackFile); FallbackPersistence fallbackPersistence = new FallbackPersistence(fallbackFile);
try { try {
ServiceLoader<Persistence> serviceLoader = ServiceLoader.load(Persistence.class); ServiceLoader<AccountingPersistence> serviceLoader = ServiceLoader.load(AccountingPersistence.class);
for (Persistence foundPersistence : serviceLoader) { for (AccountingPersistence foundPersistence : serviceLoader) {
if(foundPersistence.getClass().isInstance(FallbackPersistence.class)){ if(foundPersistence.getClass().isInstance(FallbackPersistence.class)){
continue; continue;
} }
try { try {
String foundPersistenceClassName = foundPersistence.getClass().getSimpleName(); String foundPersistenceClassName = foundPersistence.getClass().getSimpleName();
logger.debug("Testing {}", foundPersistenceClassName); logger.debug("Testing {}", foundPersistenceClassName);
PersistenceConfiguration configuration = PersistenceConfiguration.getPersistenceConfiguration(foundPersistenceClassName); AccountingPersistenceConfiguration configuration = AccountingPersistenceConfiguration.getPersistenceConfiguration(foundPersistenceClassName);
foundPersistence.prepareConnection(configuration); foundPersistence.prepareConnection(configuration);
/* /*
* Uncomment the following line of code if you want to try * Uncomment the following line of code if you want to try

View File

@ -15,7 +15,7 @@ import org.gcube.accounting.datamodel.UsageRecord;
/** /**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*/ */
public class FallbackPersistence extends Persistence { public class FallbackPersistence extends AccountingPersistence {
private File accountingFallbackFile; private File accountingFallbackFile;
@ -28,7 +28,7 @@ public class FallbackPersistence extends Persistence {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void prepareConnection(PersistenceConfiguration configuration) { public void prepareConnection(AccountingPersistenceConfiguration configuration) {
// Nothing TO DO // Nothing TO DO
} }

View File

@ -7,7 +7,7 @@ import org.gcube.accounting.datamodel.SingleUsageRecord;
import org.gcube.accounting.datamodel.UsageRecord; import org.gcube.accounting.datamodel.UsageRecord;
import org.gcube.accounting.datamodel.basetypes.TestUsageRecord; import org.gcube.accounting.datamodel.basetypes.TestUsageRecord;
import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord; import org.gcube.accounting.datamodel.usagerecords.ServiceUsageRecord;
import org.gcube.accounting.persistence.PersistenceExecutor; import org.gcube.accounting.persistence.AccountingPersistenceExecutor;
import org.gcube.accounting.testutility.StressTestUtility; import org.gcube.accounting.testutility.StressTestUtility;
import org.gcube.accounting.testutility.TestOperation; import org.gcube.accounting.testutility.TestOperation;
import org.junit.Test; import org.junit.Test;
@ -26,7 +26,7 @@ public class AggregationSchedulerTest {
return AggregationScheduler.getInstance(); return AggregationScheduler.getInstance();
} }
public static PersistenceExecutor persistenceExecutor = new PersistenceExecutor(){ public static AccountingPersistenceExecutor persistenceExecutor = new AccountingPersistenceExecutor(){
@Override @Override
public void persist(UsageRecord... usageRecords) throws Exception { public void persist(UsageRecord... usageRecords) throws Exception {

View File

@ -117,8 +117,8 @@ public class PersistenceConfigurationTest {
logger.debug("Creating ServiceEndpoint to publish on IS available plugins and their own supported capabilities"); logger.debug("Creating ServiceEndpoint to publish on IS available plugins and their own supported capabilities");
ServiceEndpoint serviceEndpoint = new ServiceEndpoint(); ServiceEndpoint serviceEndpoint = new ServiceEndpoint();
Profile profile = serviceEndpoint.newProfile(); Profile profile = serviceEndpoint.newProfile();
profile.category(PersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY); profile.category(AccountingPersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY);
profile.name(PersistenceConfiguration.SERVICE_ENDPOINT_NAME); profile.name(AccountingPersistenceConfiguration.SERVICE_ENDPOINT_NAME);
profile.version(TEST_VERSION); profile.version(TEST_VERSION);
profile.description(PROFILE_DESCRIPTION); profile.description(PROFILE_DESCRIPTION);
@ -148,7 +148,7 @@ public class PersistenceConfigurationTest {
Group<Property> properties = accessPointElement.properties(); Group<Property> properties = accessPointElement.properties();
Property className = new Property(); Property className = new Property();
className.nameAndValue(PersistenceConfiguration.PERSISTENCE_CLASS_NAME, COUCHDB_CLASS_NAME); className.nameAndValue(AccountingPersistenceConfiguration.PERSISTENCE_CLASS_NAME, COUCHDB_CLASS_NAME);
properties.add(className); properties.add(className);
Property dbName = new Property(); Property dbName = new Property();
@ -166,8 +166,8 @@ public class PersistenceConfigurationTest {
ScopeProvider.instance.set(GCUBE_DEVNEXT_SCOPE); ScopeProvider.instance.set(GCUBE_DEVNEXT_SCOPE);
SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class) SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class)
.addCondition(String.format("$resource/Profile/Category/text() eq '%s'", PersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY)) .addCondition(String.format("$resource/Profile/Category/text() eq '%s'", AccountingPersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY))
.addCondition(String.format("$resource/Profile/Name/text() eq '%s'", PersistenceConfiguration.SERVICE_ENDPOINT_NAME)) .addCondition(String.format("$resource/Profile/Name/text() eq '%s'", AccountingPersistenceConfiguration.SERVICE_ENDPOINT_NAME))
.addCondition(String.format("$resource/Profile/RunTime/HostedOn/text() eq '%s'", RUNNING_ON)) .addCondition(String.format("$resource/Profile/RunTime/HostedOn/text() eq '%s'", RUNNING_ON))
.setResult("$resource"); .setResult("$resource");
@ -198,7 +198,7 @@ public class PersistenceConfigurationTest {
} }
try { try {
PersistenceConfiguration persitenceConfiguration = PersistenceConfiguration.getPersistenceConfiguration(COUCHDB_CLASS_NAME); AccountingPersistenceConfiguration persitenceConfiguration = AccountingPersistenceConfiguration.getPersistenceConfiguration(COUCHDB_CLASS_NAME);
if(createResource){ if(createResource){
Assert.assertTrue(persitenceConfiguration.getUri().toURL().equals(new URL(RUNNING_ON))); Assert.assertTrue(persitenceConfiguration.getUri().toURL().equals(new URL(RUNNING_ON)));
Assert.assertTrue(persitenceConfiguration.getUsername().compareTo(FAKE_USERNAME)==0); Assert.assertTrue(persitenceConfiguration.getUsername().compareTo(FAKE_USERNAME)==0);

View File

@ -17,16 +17,16 @@ import org.junit.Test;
*/ */
public class PersistenceTest { public class PersistenceTest {
public static Persistence getPersistence(){ public static AccountingPersistence getPersistence(){
ScopeProvider.instance.set(PersistenceConfigurationTest.GCUBE_DEVNEXT_SCOPE); ScopeProvider.instance.set(PersistenceConfigurationTest.GCUBE_DEVNEXT_SCOPE);
PersistenceFactory.setFallbackLocation(null); AccountingPersistenceFactory.setFallbackLocation(null);
return PersistenceFactory.getPersistence(); return AccountingPersistenceFactory.getPersistence();
} }
@Test @Test
public void singleTestNoScope() throws Exception { public void singleTestNoScope() throws Exception {
PersistenceFactory.setFallbackLocation(null); AccountingPersistenceFactory.setFallbackLocation(null);
final Persistence persistence = PersistenceFactory.getPersistence(); final AccountingPersistence persistence = AccountingPersistenceFactory.getPersistence();
Assert.assertTrue(persistence instanceof FallbackPersistence); Assert.assertTrue(persistence instanceof FallbackPersistence);
StressTestUtility.stressTest(new TestOperation() { StressTestUtility.stressTest(new TestOperation() {
@Override @Override
@ -41,7 +41,7 @@ public class PersistenceTest {
@Test @Test
public void singleTest() throws Exception { public void singleTest() throws Exception {
final Persistence persistence = getPersistence(); final AccountingPersistence persistence = getPersistence();
StressTestUtility.stressTest(new TestOperation() { StressTestUtility.stressTest(new TestOperation() {
@Override @Override
public void operate(int i) { public void operate(int i) {
@ -55,7 +55,7 @@ public class PersistenceTest {
@Test @Test
public void stressTestNoAggregation() throws Exception { public void stressTestNoAggregation() throws Exception {
final Persistence persistence = getPersistence(); final AccountingPersistence persistence = getPersistence();
StressTestUtility.stressTest(new TestOperation() { StressTestUtility.stressTest(new TestOperation() {
@Override @Override
public void operate(int i) { public void operate(int i) {
@ -67,7 +67,7 @@ public class PersistenceTest {
@Test @Test
public void stressTestWithAggregation() throws Exception { public void stressTestWithAggregation() throws Exception {
final Persistence persistence = getPersistence(); final AccountingPersistence persistence = getPersistence();
StressTestUtility.stressTest(new TestOperation() { StressTestUtility.stressTest(new TestOperation() {
@Override @Override