From 8d888861a27af6173eef34517432a8cdbd4bc992 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 12 Nov 2015 13:26:31 +0000 Subject: [PATCH] refs #1349: Recheck Accounting Persistence when using fallback as default https://support.d4science.org/issues/1349 git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@120261 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../persistence/AccountingPersistence.java | 2 +- .../AccountingPersistenceBackendFactory.java | 47 ++++++++++++++----- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/gcube/accounting/persistence/AccountingPersistence.java b/src/main/java/org/gcube/accounting/persistence/AccountingPersistence.java index 21f2850..8a93c62 100644 --- a/src/main/java/org/gcube/accounting/persistence/AccountingPersistence.java +++ b/src/main/java/org/gcube/accounting/persistence/AccountingPersistence.java @@ -40,7 +40,7 @@ public class AccountingPersistence { } public void flush(long timeout, TimeUnit timeUnit) throws Exception { - AccountingPersistenceBackendFactory.flush(timeout, timeUnit); + AccountingPersistenceBackendFactory.flushAll(timeout, timeUnit); } public void close() throws Exception{ diff --git a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java index d379cd5..32bdec8 100644 --- a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java +++ b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java @@ -4,6 +4,7 @@ package org.gcube.accounting.persistence; import java.io.File; +import java.util.Calendar; import java.util.HashMap; import java.util.Map; import java.util.ServiceLoader; @@ -31,22 +32,23 @@ public abstract class AccountingPersistenceBackendFactory { private static Map persistencePersistenceBackends; + private static final long FALLBACK_RETRY_TIME = 1000*60*10; // 10 min + private static Map falbackLastCheck; + static { persistencePersistenceBackends = new HashMap(); + falbackLastCheck = new HashMap(); } private static File file(File file) throws IllegalArgumentException { - if(!file.isDirectory()){ file = file.getParentFile(); } - - //create folder structure if not exist - if (!file.exists()) + // Create folder structure if not exist + if (!file.exists()) { file.mkdirs(); - + } return file; - } protected synchronized static void setFallbackLocation(String path){ @@ -61,6 +63,7 @@ public abstract class AccountingPersistenceBackendFactory { protected static synchronized AccountingPersistenceBackend getPersistenceBackend() { String scope = ScopeProvider.instance.get(); + if(scope==null){ logger.error("No Scope available. FallbackPersistence will be used"); File fallbackFile = new File(fallbackLocation, ACCOUTING_FALLBACK_FILENAME); @@ -68,6 +71,16 @@ public abstract class AccountingPersistenceBackendFactory { } AccountingPersistenceBackend persistence = persistencePersistenceBackends.get(scope); + if(persistence.getClass().isInstance(FallbackPersistence.class) && falbackLastCheck.get(scope)!=null){ + long now = Calendar.getInstance().getTimeInMillis(); + Long lastCheckTimestamp = falbackLastCheck.get(scope); + if(lastCheckTimestamp <= (now + FALLBACK_RETRY_TIME)){ + // Setting persistence to null so that the AccountingPersistenceBackend + // is rechecked + persistence = null; + } + } + if(persistence==null){ ScopeBean bean = new ScopeBean(scope); @@ -104,17 +117,26 @@ public abstract class AccountingPersistenceBackendFactory { } catch (Exception e) { logger.error(String.format("%s not initialized correctly. It will not be used. Trying the next one if any.", foundPersistence.getClass().getSimpleName()), e); } - } if(persistence==null){ + } + + if(persistence==null){ + logger.error("Unable to instatiate a {}. {} will be used.", AccountingPersistenceBackend.class.getSimpleName(), FallbackPersistence.class.getSimpleName()); persistence = fallbackPersistence; } + } catch(Exception e){ - logger.error("Unable to instance a Persistence Implementation. Using fallback as default", - e); + //logger.error("Unable to instance a Persistence Implementation. Using fallback as default", e); + logger.error("Unable to instatiate a {}. {} will be used.", AccountingPersistenceBackend.class.getSimpleName(), FallbackPersistence.class.getSimpleName(), e); persistence = fallbackPersistence; } + persistence.setAggregationScheduler(AggregationScheduler.newInstance()); persistence.setFallback(fallbackPersistence); - persistencePersistenceBackends.put(scope, persistence); + if(persistence.getClass().isInstance(FallbackPersistence.class)){ + long now = Calendar.getInstance().getTimeInMillis(); + falbackLastCheck.put(scope, now); + } + persistencePersistenceBackends.put(scope, persistence); } return persistence; @@ -125,10 +147,9 @@ public abstract class AccountingPersistenceBackendFactory { * @param timeUnit * @throws Exception */ - public static void flush(long timeout, TimeUnit timeUnit) { + public static void flushAll(long timeout, TimeUnit timeUnit) { for(String scope : persistencePersistenceBackends.keySet()){ - AccountingPersistenceBackend apb = - persistencePersistenceBackends.get(scope); + AccountingPersistenceBackend apb = persistencePersistenceBackends.get(scope); try { logger.debug("Flushing records in scope {}", scope); apb.flush(timeout, timeUnit);