From 54f2a86e07c2bdf1ff0085ab8f45c21e6e3263c8 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Fri, 13 Nov 2015 11:24:57 +0000 Subject: [PATCH] Changed the way to retrieve accounting persistence configuration due to deadlock which happen only in IC node git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@120277 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../AccountingPersistenceBackendFactory.java | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java index 998649e..1dd6ece 100644 --- a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java +++ b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java @@ -30,14 +30,14 @@ public abstract class AccountingPersistenceBackendFactory { private static String fallbackLocation; - private static Map persistencePersistenceBackends; + private static Map accountingPersistenceBackends; public static final long FALLBACK_RETRY_TIME = 1000*60*10; // 10 min protected static Map fallbackLastCheck; static { - persistencePersistenceBackends = new HashMap(); + accountingPersistenceBackends = new HashMap(); fallbackLastCheck = new HashMap(); } @@ -65,9 +65,6 @@ public abstract class AccountingPersistenceBackendFactory { protected static AccountingPersistenceBackend discoverAccountingPersistenceBackend(String scope){ ServiceLoader serviceLoader = ServiceLoader.load(AccountingPersistenceBackend.class); for (AccountingPersistenceBackend foundPersistence : serviceLoader) { - if(foundPersistence instanceof FallbackPersistence){ - continue; - } try { String foundPersistenceClassName = foundPersistence.getClass().getSimpleName(); logger.debug("Testing {}", foundPersistenceClassName); @@ -114,18 +111,16 @@ public abstract class AccountingPersistenceBackendFactory { return createFallback(null); } - AccountingPersistenceBackend persistence = persistencePersistenceBackends.get(scope); + AccountingPersistenceBackend persistence = accountingPersistenceBackends.get(scope); if(persistence==null){ persistence = discoverAccountingPersistenceBackend(scope); - if(persistence==null){ logger.warn("Unable to find a usable {}. {} will be used.", AccountingPersistenceBackend.class.getSimpleName(), FallbackPersistence.class.getSimpleName()); long now = Calendar.getInstance().getTimeInMillis(); fallbackLastCheck.put(scope, now); persistence = createFallback(scope); } - - persistencePersistenceBackends.put(scope, persistence); + accountingPersistenceBackends.put(scope, persistence); } else { if(persistence instanceof FallbackPersistence && fallbackLastCheck.get(scope)!=null){ @@ -137,9 +132,13 @@ public abstract class AccountingPersistenceBackendFactory { 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); - persistencePersistenceBackends.put(scope, discoveredPersistenceBackend); + accountingPersistenceBackends.put(scope, discoveredPersistenceBackend); try { persistence.close(); } catch (Exception e) { @@ -148,7 +147,9 @@ public abstract class AccountingPersistenceBackendFactory { discoveredPersistenceBackend.getClass().getSimpleName(), e); } persistence = discoveredPersistenceBackend; + }else{ + // Renewing timestamp fallbackLastCheck.put(scope, now); } } @@ -164,8 +165,8 @@ public abstract class AccountingPersistenceBackendFactory { * @throws Exception */ public static void flushAll(long timeout, TimeUnit timeUnit) { - for(String scope : persistencePersistenceBackends.keySet()){ - AccountingPersistenceBackend apb = persistencePersistenceBackends.get(scope); + for(String scope : accountingPersistenceBackends.keySet()){ + AccountingPersistenceBackend apb = accountingPersistenceBackends.get(scope); try { logger.debug("Flushing records in scope {}", scope); apb.flush(timeout, timeUnit);