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
This commit is contained in:
parent
8d888861a2
commit
49f67ae804
|
@ -32,12 +32,13 @@ public abstract class AccountingPersistenceBackendFactory {
|
|||
|
||||
private static Map<String, AccountingPersistenceBackend> persistencePersistenceBackends;
|
||||
|
||||
private static final long FALLBACK_RETRY_TIME = 1000*60*10; // 10 min
|
||||
private static Map<String,Long> falbackLastCheck;
|
||||
public static final long FALLBACK_RETRY_TIME = 1000*60*10; // 10 min
|
||||
|
||||
private static Map<String,Long> fallbackLastCheck;
|
||||
|
||||
static {
|
||||
persistencePersistenceBackends = new HashMap<String, AccountingPersistenceBackend>();
|
||||
falbackLastCheck = new HashMap<String, Long>();
|
||||
fallbackLastCheck = new HashMap<String, Long>();
|
||||
}
|
||||
|
||||
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<AccountingPersistenceBackend> 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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue