diff --git a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java index dda84b7..f039caa 100644 --- a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java +++ b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java @@ -113,6 +113,7 @@ public abstract class AccountingPersistenceBackendFactory { protected static AccountingPersistenceBackend rediscoverAccountingPersistenceBackend(AccountingPersistenceBackend actual, String scope){ long now = Calendar.getInstance().getTimeInMillis(); Long lastCheckTimestamp = fallbackLastCheck.get(scope); + logger.trace("Last check was {}", lastCheckTimestamp); if( (lastCheckTimestamp + FALLBACK_RETRY_TIME) <= now ){ logger.debug("The {} for scope {} is {}. Is time to rediscover if there is another possibility.", @@ -138,12 +139,18 @@ public abstract class AccountingPersistenceBackendFactory { }else{ // Renewing timestamp + logger.trace("Renewing Last check Timestamp. The new one will be {}", now); fallbackLastCheck.put(scope, now); + lastCheckTimestamp = fallbackLastCheck.get(scope); } } - logger.debug("The {} for scope {} is going to be used is {}. Next retry in {} msec", - AccountingPersistenceBackend.class.getSimpleName(), scope, actual.getClass().getSimpleName(), FALLBACK_RETRY_TIME); + long nextCheck = (lastCheckTimestamp + FALLBACK_RETRY_TIME) - Calendar.getInstance().getTimeInMillis(); + float nextCheckInSec = nextCheck/1000; + logger.debug("The {} for scope {} is going to be used is {}. Next retry in {} msec (about {} sec)", + AccountingPersistenceBackend.class.getSimpleName(), scope, + actual.getClass().getSimpleName(), nextCheck, nextCheckInSec); + return actual; } diff --git a/src/test/java/org/gcube/accounting/persistence/AccountingPersistenceBackendTest.java b/src/test/java/org/gcube/accounting/persistence/AccountingPersistenceBackendTest.java index 717fd47..676564c 100644 --- a/src/test/java/org/gcube/accounting/persistence/AccountingPersistenceBackendTest.java +++ b/src/test/java/org/gcube/accounting/persistence/AccountingPersistenceBackendTest.java @@ -103,22 +103,27 @@ public class AccountingPersistenceBackendTest { @Test public void testScopeRecheck() throws Exception { ScopeProvider.instance.set("/fakeScope"); - AccountingPersistenceBackendFactory.getPersistenceBackend(); + logger.debug("Going to check First Time"); + AccountingPersistenceBackend first = AccountingPersistenceBackendFactory.getPersistenceBackend(); + logger.debug("First {} : {}", AccountingPersistenceBackend.class.getSimpleName(), first); Long firstCheck = AccountingPersistenceBackendFactory.getFallbackLastCheck(ScopeProvider.instance.get()); logger.debug("First Check Time {}", firstCheck); long startTime = Calendar.getInstance().getTimeInMillis(); long endTime = startTime; - while(endTime <= (startTime + (AccountingPersistenceBackendFactory.FALLBACK_RETRY_TIME+1000))){ + while(endTime <= (startTime + (AccountingPersistenceBackendFactory.FALLBACK_RETRY_TIME + 100))){ endTime = Calendar.getInstance().getTimeInMillis(); } - AccountingPersistenceBackendFactory.getPersistenceBackend(); + logger.debug("Going to check Second Time"); + AccountingPersistenceBackend second = AccountingPersistenceBackendFactory.getPersistenceBackend(); + logger.debug("Second {} : {}", AccountingPersistenceBackend.class.getSimpleName(), second); Long secondCheck = AccountingPersistenceBackendFactory.getFallbackLastCheck(ScopeProvider.instance.get()); logger.debug("Second Check Time {}", secondCheck); Assert.assertNotEquals(firstCheck, secondCheck); + Assert.assertEquals(first, second); } } diff --git a/src/test/java/org/gcube/accounting/persistence/AccountingPersistenceConfigurationTest.java b/src/test/java/org/gcube/accounting/persistence/AccountingPersistenceConfigurationTest.java index 119efb1..b473bcc 100644 --- a/src/test/java/org/gcube/accounting/persistence/AccountingPersistenceConfigurationTest.java +++ b/src/test/java/org/gcube/accounting/persistence/AccountingPersistenceConfigurationTest.java @@ -3,11 +3,6 @@ */ package org.gcube.accounting.persistence; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.io.PrintWriter; import java.io.StringWriter; import java.net.URL; import java.util.Arrays;