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 Map<String, AccountingPersistenceBackend> persistencePersistenceBackends;
|
||||||
|
|
||||||
private static final long FALLBACK_RETRY_TIME = 1000*60*10; // 10 min
|
public static final long FALLBACK_RETRY_TIME = 1000*60*10; // 10 min
|
||||||
private static Map<String,Long> falbackLastCheck;
|
|
||||||
|
private static Map<String,Long> fallbackLastCheck;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
persistencePersistenceBackends = new HashMap<String, AccountingPersistenceBackend>();
|
persistencePersistenceBackends = new HashMap<String, AccountingPersistenceBackend>();
|
||||||
falbackLastCheck = new HashMap<String, Long>();
|
fallbackLastCheck = new HashMap<String, Long>();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static File file(File file) throws IllegalArgumentException {
|
private static File file(File file) throws IllegalArgumentException {
|
||||||
|
@ -70,14 +71,18 @@ public abstract class AccountingPersistenceBackendFactory {
|
||||||
return new FallbackPersistence(fallbackFile);
|
return new FallbackPersistence(fallbackFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
AccountingPersistenceBackend persistence = persistencePersistenceBackends.get(scope);
|
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 now = Calendar.getInstance().getTimeInMillis();
|
||||||
Long lastCheckTimestamp = falbackLastCheck.get(scope);
|
Long lastCheckTimestamp = fallbackLastCheck.get(scope);
|
||||||
if(lastCheckTimestamp <= (now + FALLBACK_RETRY_TIME)){
|
if(lastCheckTimestamp <= (now + FALLBACK_RETRY_TIME)){
|
||||||
// Setting persistence to null so that the AccountingPersistenceBackend
|
// Setting persistence to null so that the
|
||||||
// is rechecked
|
// 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;
|
persistence = null;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,7 +101,7 @@ public abstract class AccountingPersistenceBackendFactory {
|
||||||
try {
|
try {
|
||||||
ServiceLoader<AccountingPersistenceBackend> serviceLoader = ServiceLoader.load(AccountingPersistenceBackend.class);
|
ServiceLoader<AccountingPersistenceBackend> serviceLoader = ServiceLoader.load(AccountingPersistenceBackend.class);
|
||||||
for (AccountingPersistenceBackend foundPersistence : serviceLoader) {
|
for (AccountingPersistenceBackend foundPersistence : serviceLoader) {
|
||||||
if(foundPersistence.getClass().isInstance(FallbackPersistence.class)){
|
if(foundPersistence instanceof FallbackPersistence){
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
@ -126,15 +131,15 @@ public abstract class AccountingPersistenceBackendFactory {
|
||||||
|
|
||||||
} catch(Exception e){
|
} 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);
|
logger.error("Unable to instantiate a {}. {} will be used.", AccountingPersistenceBackend.class.getSimpleName(), FallbackPersistence.class.getSimpleName(), e);
|
||||||
persistence = fallbackPersistence;
|
persistence = fallbackPersistence;
|
||||||
}
|
}
|
||||||
|
|
||||||
persistence.setAggregationScheduler(AggregationScheduler.newInstance());
|
persistence.setAggregationScheduler(AggregationScheduler.newInstance());
|
||||||
persistence.setFallback(fallbackPersistence);
|
persistence.setFallback(fallbackPersistence);
|
||||||
if(persistence.getClass().isInstance(FallbackPersistence.class)){
|
if(persistence instanceof FallbackPersistence){
|
||||||
long now = Calendar.getInstance().getTimeInMillis();
|
long now = Calendar.getInstance().getTimeInMillis();
|
||||||
falbackLastCheck.put(scope, now);
|
fallbackLastCheck.put(scope, now);
|
||||||
}
|
}
|
||||||
persistencePersistenceBackends.put(scope, persistence);
|
persistencePersistenceBackends.put(scope, persistence);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
*/
|
*/
|
||||||
package org.gcube.accounting.persistence;
|
package org.gcube.accounting.persistence;
|
||||||
|
|
||||||
|
import java.util.Calendar;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import org.gcube.accounting.datamodel.SingleUsageRecord;
|
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.gcube.common.scope.api.ScopeProvider;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
||||||
|
@ -20,6 +23,8 @@ import org.junit.Test;
|
||||||
*/
|
*/
|
||||||
public class AccountingPersistenceBackendTest {
|
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[] SCOPES = new String[]{"/gcube", "/gcube/devNext"};
|
||||||
public static final String GCUBE_SCOPE = SCOPES[0];
|
public static final String GCUBE_SCOPE = SCOPES[0];
|
||||||
public static final String GCUBE_DEVNEXT_SCOPE = SCOPES[1];
|
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