From 901f0ce0c60c1b2c243e0bd6cfe0ac61cf3184a8 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Fri, 13 Nov 2015 11:33:50 +0000 Subject: [PATCH] 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 --- .../AccountingPersistenceBackendFactory.java | 70 ++++++++++--------- 1 file changed, 38 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java index 1dd6ece..5e27451 100644 --- a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java +++ b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java @@ -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;