Recoded the code redesign. Recoded the function which retrieve the AccountingPersistenceBackend class to use.

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@120278 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2015-11-13 11:33:50 +00:00
parent 54f2a86e07
commit 901f0ce0c6
1 changed files with 38 additions and 32 deletions

View File

@ -102,6 +102,40 @@ public abstract class AccountingPersistenceBackendFactory {
return fallbackPersistence;
}
protected static AccountingPersistenceBackend rediscoverAccountingPersistenceBackend(AccountingPersistenceBackend actual, String scope){
long now = Calendar.getInstance().getTimeInMillis();
Long lastCheckTimestamp = fallbackLastCheck.get(scope);
if(lastCheckTimestamp <= (now + FALLBACK_RETRY_TIME)){
logger.debug("The {} for scope {} is {}. Is time to rediscover if there is another possibility.",
AccountingPersistenceBackend.class.getSimpleName(), scope, actual.getClass().getSimpleName());
AccountingPersistenceBackend discoveredPersistenceBackend = discoverAccountingPersistenceBackend(scope);
if(discoveredPersistenceBackend!=null){
// Passing the aggregator to the new AccountingPersistenceBackend
// so that the buffered records will be persisted with the new method
discoveredPersistenceBackend.setAggregationScheduler(actual.getAggregationScheduler());
// Removing timestamp wich is no more needed
fallbackLastCheck.remove(scope);
accountingPersistenceBackends.put(scope, discoveredPersistenceBackend);
try {
actual.close();
} catch (Exception e) {
logger.error("Error closing {} for scope {} which has been substituted with {}.",
actual.getClass().getSimpleName(), scope,
discoveredPersistenceBackend.getClass().getSimpleName(), e);
}
return discoveredPersistenceBackend;
}else{
// Renewing timestamp
fallbackLastCheck.put(scope, now);
}
}
return actual;
}
protected static synchronized AccountingPersistenceBackend getPersistenceBackend() {
String scope = ScopeProvider.instance.get();
@ -122,38 +156,10 @@ public abstract class AccountingPersistenceBackendFactory {
}
accountingPersistenceBackends.put(scope, persistence);
} else {
if(persistence instanceof FallbackPersistence && fallbackLastCheck.get(scope)!=null){
long now = Calendar.getInstance().getTimeInMillis();
Long lastCheckTimestamp = fallbackLastCheck.get(scope);
if(lastCheckTimestamp <= (now + FALLBACK_RETRY_TIME)){
logger.debug("The {} for scope {} is {}. Is time to rediscover if there is another possibility.",
AccountingPersistenceBackend.class.getSimpleName(), scope, persistence.getClass().getSimpleName());
AccountingPersistenceBackend discoveredPersistenceBackend = discoverAccountingPersistenceBackend(scope);
if(discoveredPersistenceBackend!=null){
// Passing the aggregator to the new AccountingPersistenceBackend
// so that the buffered records will be persisted with the new method
discoveredPersistenceBackend.setAggregationScheduler(persistence.getAggregationScheduler());
// Removing timestamp wich is no more needed
fallbackLastCheck.remove(scope);
accountingPersistenceBackends.put(scope, discoveredPersistenceBackend);
try {
persistence.close();
} catch (Exception e) {
logger.error("Error closing {} for scope {} which has been substituted with {}.",
persistence.getClass().getSimpleName(), scope,
discoveredPersistenceBackend.getClass().getSimpleName(), e);
}
persistence = discoveredPersistenceBackend;
}else{
// Renewing timestamp
fallbackLastCheck.put(scope, now);
}
}
}
} else if(persistence instanceof FallbackPersistence && fallbackLastCheck.get(scope)!=null){
// Trying to rediscover AccountingPersistenceBackend
persistence = rediscoverAccountingPersistenceBackend(persistence, scope);
}
return persistence;