From 49f67ae80405f165cc6c0f4beee72d088a54afe1 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 12 Nov 2015 15:56:22 +0000 Subject: [PATCH] fixes #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@120267 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../AccountingPersistenceBackendFactory.java | 27 +++++++++++-------- .../AccountingPersistenceBackendTest.java | 24 ++++++++++++++++- 2 files changed, 39 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 32bdec8..fd43b25 100644 --- a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java +++ b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java @@ -32,12 +32,13 @@ public abstract class AccountingPersistenceBackendFactory { private static Map persistencePersistenceBackends; - private static final long FALLBACK_RETRY_TIME = 1000*60*10; // 10 min - private static Map falbackLastCheck; + public static final long FALLBACK_RETRY_TIME = 1000*60*10; // 10 min + + private static Map fallbackLastCheck; static { persistencePersistenceBackends = new HashMap(); - falbackLastCheck = new HashMap(); + fallbackLastCheck = new HashMap(); } private static File file(File file) throws IllegalArgumentException { @@ -70,14 +71,18 @@ public abstract class AccountingPersistenceBackendFactory { return new FallbackPersistence(fallbackFile); } + AccountingPersistenceBackend persistence = persistencePersistenceBackends.get(scope); - if(persistence.getClass().isInstance(FallbackPersistence.class) && falbackLastCheck.get(scope)!=null){ + if(persistence!=null && persistence instanceof FallbackPersistence && fallbackLastCheck.get(scope)!=null){ long now = Calendar.getInstance().getTimeInMillis(); - Long lastCheckTimestamp = falbackLastCheck.get(scope); + Long lastCheckTimestamp = fallbackLastCheck.get(scope); if(lastCheckTimestamp <= (now + FALLBACK_RETRY_TIME)){ - // Setting persistence to null so that the AccountingPersistenceBackend - // is rechecked + // Setting persistence to null so that the + // AccountingPersistenceBackend is rechecked + logger.debug("The {} for scope {} is {}. Is time to rediscover if there is another possibility.", + AccountingPersistenceBackend.class.getSimpleName(), scope, persistence.getClass().getSimpleName()); persistence = null; + } } @@ -96,7 +101,7 @@ public abstract class AccountingPersistenceBackendFactory { try { ServiceLoader serviceLoader = ServiceLoader.load(AccountingPersistenceBackend.class); for (AccountingPersistenceBackend foundPersistence : serviceLoader) { - if(foundPersistence.getClass().isInstance(FallbackPersistence.class)){ + if(foundPersistence instanceof FallbackPersistence){ continue; } try { @@ -126,15 +131,15 @@ public abstract class AccountingPersistenceBackendFactory { } catch(Exception 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); + logger.error("Unable to instantiate a {}. {} will be used.", AccountingPersistenceBackend.class.getSimpleName(), FallbackPersistence.class.getSimpleName(), e); persistence = fallbackPersistence; } persistence.setAggregationScheduler(AggregationScheduler.newInstance()); persistence.setFallback(fallbackPersistence); - if(persistence.getClass().isInstance(FallbackPersistence.class)){ + if(persistence instanceof FallbackPersistence){ long now = Calendar.getInstance().getTimeInMillis(); - falbackLastCheck.put(scope, now); + fallbackLastCheck.put(scope, now); } persistencePersistenceBackends.put(scope, persistence); } diff --git a/src/test/java/org/gcube/accounting/persistence/AccountingPersistenceBackendTest.java b/src/test/java/org/gcube/accounting/persistence/AccountingPersistenceBackendTest.java index c9ad6c7..a2351ff 100644 --- a/src/test/java/org/gcube/accounting/persistence/AccountingPersistenceBackendTest.java +++ b/src/test/java/org/gcube/accounting/persistence/AccountingPersistenceBackendTest.java @@ -3,6 +3,7 @@ */ package org.gcube.accounting.persistence; +import java.util.Calendar; import java.util.concurrent.TimeUnit; import org.gcube.accounting.datamodel.SingleUsageRecord; @@ -13,6 +14,8 @@ import org.gcube.accounting.testutility.TestOperation; import org.gcube.common.scope.api.ScopeProvider; import org.junit.Assert; import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/ @@ -20,6 +23,8 @@ import org.junit.Test; */ public class AccountingPersistenceBackendTest { + private static Logger logger = LoggerFactory.getLogger(AccountingPersistenceBackendTest.class); + public static final String[] SCOPES = new String[]{"/gcube", "/gcube/devNext"}; public static final String GCUBE_SCOPE = SCOPES[0]; public static final String GCUBE_DEVNEXT_SCOPE = SCOPES[1]; @@ -95,6 +100,23 @@ public class AccountingPersistenceBackendTest { } - + @Test + public void testScopeRecheck() throws Exception { + ScopeProvider.instance.set("/fakeScope"); + AccountingPersistenceBackend first = AccountingPersistenceBackendFactory.getPersistenceBackend(); + logger.debug("First {} : {}", AccountingPersistenceBackend.class.getSimpleName(), first); + + long startTime = Calendar.getInstance().getTimeInMillis(); + long endTime = startTime; + + while(endTime <= (startTime + (AccountingPersistenceBackendFactory.FALLBACK_RETRY_TIME+1000))){ + endTime = Calendar.getInstance().getTimeInMillis(); + } + + AccountingPersistenceBackend second = AccountingPersistenceBackendFactory.getPersistenceBackend(); + logger.debug("Second {} : {}", AccountingPersistenceBackend.class.getSimpleName(), second); + + Assert.assertNotEquals(first, second); + } }