This commit is contained in:
Lucio Lelii 2015-10-30 15:56:38 +00:00
parent 0871f66b87
commit fc9b9bb023
1 changed files with 55 additions and 32 deletions

View File

@ -8,6 +8,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.ServiceLoader; import java.util.ServiceLoader;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import org.gcube.accounting.aggregation.scheduler.AggregationScheduler; import org.gcube.accounting.aggregation.scheduler.AggregationScheduler;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
@ -29,6 +30,10 @@ public abstract class AccountingPersistenceBackendFactory {
private static String fallbackLocation; private static String fallbackLocation;
private static ReentrantLock lock = new ReentrantLock();
private static ReentrantLock persistenceLock = new ReentrantLock();
private static Map<String, AccountingPersistenceBackend> persistencePersistenceBackends; private static Map<String, AccountingPersistenceBackend> persistencePersistenceBackends;
static { static {
@ -49,17 +54,23 @@ public abstract class AccountingPersistenceBackendFactory {
} }
protected synchronized static void setFallbackLocation(String path){ protected static void setFallbackLocation(String path){
if(fallbackLocation == null){ lock.lock();
if(path==null){ try{
path = System.getProperty(HOME_SYSTEM_PROPERTY); if(fallbackLocation == null){
if(path==null){
path = System.getProperty(HOME_SYSTEM_PROPERTY);
}
file(new File(path));
fallbackLocation = path;
} }
file(new File(path)); }finally{
fallbackLocation = path; lock.unlock();
} }
} }
protected static synchronized AccountingPersistenceBackend getPersistenceBackend() { protected static AccountingPersistenceBackend getPersistenceBackend() {
String scope = ScopeProvider.instance.get(); String scope = ScopeProvider.instance.get();
if(scope==null){ if(scope==null){
logger.error("No Scope available. FallbackPersistence will be used"); logger.error("No Scope available. FallbackPersistence will be used");
@ -67,19 +78,28 @@ public abstract class AccountingPersistenceBackendFactory {
return new FallbackPersistence(fallbackFile); return new FallbackPersistence(fallbackFile);
} }
persistenceLock.lock();
AccountingPersistenceBackend persistence = persistencePersistenceBackends.get(scope); AccountingPersistenceBackend persistence = persistencePersistenceBackends.get(scope);
if(persistence==null){ if(persistence==null){
FallbackPersistence fallbackPersistence;
try{
ScopeBean bean = new ScopeBean(scope);
/*
if(bean.is(Type.VRE)){
bean = bean.enclosingScope();
}
*/
String name = bean.name();
ScopeBean bean = new ScopeBean(scope); File fallbackFile = new File(fallbackLocation, String.format("%s.%s", name, ACCOUTING_FALLBACK_FILENAME));
/* fallbackPersistence = new FallbackPersistence(fallbackFile);
if(bean.is(Type.VRE)){ // set only to avoid deadlock
bean = bean.enclosingScope(); persistencePersistenceBackends.put(scope, fallbackPersistence);
}finally{
persistenceLock.unlock();
} }
*/
String name = bean.name();
File fallbackFile = new File(fallbackLocation, String.format("%s.%s", name, ACCOUTING_FALLBACK_FILENAME));
FallbackPersistence fallbackPersistence = new FallbackPersistence(fallbackFile);
try { try {
ServiceLoader<AccountingPersistenceBackend> serviceLoader = ServiceLoader.load(AccountingPersistenceBackend.class); ServiceLoader<AccountingPersistenceBackend> serviceLoader = ServiceLoader.load(AccountingPersistenceBackend.class);
for (AccountingPersistenceBackend foundPersistence : serviceLoader) { for (AccountingPersistenceBackend foundPersistence : serviceLoader) {
@ -115,8 +135,11 @@ public abstract class AccountingPersistenceBackendFactory {
persistence.setAggregationScheduler(AggregationScheduler.newInstance()); persistence.setAggregationScheduler(AggregationScheduler.newInstance());
persistence.setFallback(fallbackPersistence); persistence.setFallback(fallbackPersistence);
persistencePersistenceBackends.put(scope, persistence); persistencePersistenceBackends.put(scope, persistence);
} else{
persistenceLock.unlock();
} }
return persistence; return persistence;
} }