refs #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@120261 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2015-11-12 13:26:31 +00:00
parent fe78dc48f1
commit 8d888861a2
2 changed files with 35 additions and 14 deletions

View File

@ -40,7 +40,7 @@ public class AccountingPersistence {
}
public void flush(long timeout, TimeUnit timeUnit) throws Exception {
AccountingPersistenceBackendFactory.flush(timeout, timeUnit);
AccountingPersistenceBackendFactory.flushAll(timeout, timeUnit);
}
public void close() throws Exception{

View File

@ -4,6 +4,7 @@
package org.gcube.accounting.persistence;
import java.io.File;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;
@ -31,22 +32,23 @@ 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;
static {
persistencePersistenceBackends = new HashMap<String, AccountingPersistenceBackend>();
falbackLastCheck = new HashMap<String, Long>();
}
private static File file(File file) throws IllegalArgumentException {
if(!file.isDirectory()){
file = file.getParentFile();
}
//create folder structure if not exist
if (!file.exists())
// Create folder structure if not exist
if (!file.exists()) {
file.mkdirs();
}
return file;
}
protected synchronized static void setFallbackLocation(String path){
@ -61,6 +63,7 @@ public abstract class AccountingPersistenceBackendFactory {
protected static synchronized AccountingPersistenceBackend getPersistenceBackend() {
String scope = ScopeProvider.instance.get();
if(scope==null){
logger.error("No Scope available. FallbackPersistence will be used");
File fallbackFile = new File(fallbackLocation, ACCOUTING_FALLBACK_FILENAME);
@ -68,6 +71,16 @@ public abstract class AccountingPersistenceBackendFactory {
}
AccountingPersistenceBackend persistence = persistencePersistenceBackends.get(scope);
if(persistence.getClass().isInstance(FallbackPersistence.class) && falbackLastCheck.get(scope)!=null){
long now = Calendar.getInstance().getTimeInMillis();
Long lastCheckTimestamp = falbackLastCheck.get(scope);
if(lastCheckTimestamp <= (now + FALLBACK_RETRY_TIME)){
// Setting persistence to null so that the AccountingPersistenceBackend
// is rechecked
persistence = null;
}
}
if(persistence==null){
ScopeBean bean = new ScopeBean(scope);
@ -104,16 +117,25 @@ public abstract class AccountingPersistenceBackendFactory {
} catch (Exception e) {
logger.error(String.format("%s not initialized correctly. It will not be used. Trying the next one if any.", foundPersistence.getClass().getSimpleName()), e);
}
} if(persistence==null){
}
if(persistence==null){
logger.error("Unable to instatiate a {}. {} will be used.", AccountingPersistenceBackend.class.getSimpleName(), FallbackPersistence.class.getSimpleName());
persistence = fallbackPersistence;
}
} 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);
persistence = fallbackPersistence;
}
persistence.setAggregationScheduler(AggregationScheduler.newInstance());
persistence.setFallback(fallbackPersistence);
if(persistence.getClass().isInstance(FallbackPersistence.class)){
long now = Calendar.getInstance().getTimeInMillis();
falbackLastCheck.put(scope, now);
}
persistencePersistenceBackends.put(scope, persistence);
}
@ -125,10 +147,9 @@ public abstract class AccountingPersistenceBackendFactory {
* @param timeUnit
* @throws Exception
*/
public static void flush(long timeout, TimeUnit timeUnit) {
public static void flushAll(long timeout, TimeUnit timeUnit) {
for(String scope : persistencePersistenceBackends.keySet()){
AccountingPersistenceBackend apb =
persistencePersistenceBackends.get(scope);
AccountingPersistenceBackend apb = persistencePersistenceBackends.get(scope);
try {
logger.debug("Flushing records in scope {}", scope);
apb.flush(timeout, timeUnit);