diff --git a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java index 611ae9b..6b5a742 100644 --- a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java +++ b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java @@ -115,23 +115,23 @@ public abstract class AccountingPersistenceBackendFactory { Long now = Calendar.getInstance().getTimeInMillis(); Long lastCheckTimestamp = fallbackLastCheck.get(scope); logger.debug("Last check for scope {} was {}", scope, lastCheckTimestamp); - - if( (lastCheckTimestamp + FALLBACK_RETRY_TIME) <= now ){ - logger.debug("The {} for scope {} is {}. Is time to rediscover if there is another possibility.", + boolean myTurn = false; + synchronized (accountingPersistenceBackends) { + if( (lastCheckTimestamp + FALLBACK_RETRY_TIME) <= now ){ + logger.debug("The {} for scope {} is {}. Is time to rediscover if there is another possibility.", AccountingPersistenceBackend.class.getSimpleName(), scope, actual.getClass().getSimpleName()); - + logger.trace("Renewing Last check Timestamp. The next one will be {}", now); + fallbackLastCheck.put(scope, now); + myTurn=true; + logger.debug("I win. It is my turn to rediscover {} in scope {}", + AccountingPersistenceBackend.class.getSimpleName(), scope); + } + } + + if(myTurn){ AccountingPersistenceBackend discoveredPersistenceBackend = discoverAccountingPersistenceBackend(scope); synchronized (accountingPersistenceBackends) { - logger.trace("Renewing Last check Timestamp. The next one will be {}", now); - Long reCheckTimestamp = fallbackLastCheck.get(scope); - if(lastCheckTimestamp.compareTo(reCheckTimestamp)!=0){ - // Someone else arrived here before. Stopping to try to - logger.debug("Someone else arrived here before. Stopping to try to rediscover AccountingPersistenceBackend and getting the current"); - return accountingPersistenceBackends.get(scope); - } - - fallbackLastCheck.put(scope, now); if(discoveredPersistenceBackend!=null){ /* * Passing the aggregator to the new AccountingPersistenceBackend