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:
parent
fe78dc48f1
commit
8d888861a2
|
@ -40,7 +40,7 @@ public class AccountingPersistence {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void flush(long timeout, TimeUnit timeUnit) throws Exception {
|
public void flush(long timeout, TimeUnit timeUnit) throws Exception {
|
||||||
AccountingPersistenceBackendFactory.flush(timeout, timeUnit);
|
AccountingPersistenceBackendFactory.flushAll(timeout, timeUnit);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void close() throws Exception{
|
public void close() throws Exception{
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
package org.gcube.accounting.persistence;
|
package org.gcube.accounting.persistence;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.util.Calendar;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.ServiceLoader;
|
import java.util.ServiceLoader;
|
||||||
|
@ -31,22 +32,23 @@ 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
|
||||||
|
private static Map<String,Long> falbackLastCheck;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
persistencePersistenceBackends = new HashMap<String, AccountingPersistenceBackend>();
|
persistencePersistenceBackends = new HashMap<String, AccountingPersistenceBackend>();
|
||||||
|
falbackLastCheck = new HashMap<String, Long>();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static File file(File file) throws IllegalArgumentException {
|
private static File file(File file) throws IllegalArgumentException {
|
||||||
|
|
||||||
if(!file.isDirectory()){
|
if(!file.isDirectory()){
|
||||||
file = file.getParentFile();
|
file = file.getParentFile();
|
||||||
}
|
}
|
||||||
|
// Create folder structure if not exist
|
||||||
//create folder structure if not exist
|
if (!file.exists()) {
|
||||||
if (!file.exists())
|
|
||||||
file.mkdirs();
|
file.mkdirs();
|
||||||
|
}
|
||||||
return file;
|
return file;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected synchronized static void setFallbackLocation(String path){
|
protected synchronized static void setFallbackLocation(String path){
|
||||||
|
@ -61,6 +63,7 @@ public abstract class AccountingPersistenceBackendFactory {
|
||||||
|
|
||||||
protected static synchronized AccountingPersistenceBackend getPersistenceBackend() {
|
protected static synchronized 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");
|
||||||
File fallbackFile = new File(fallbackLocation, ACCOUTING_FALLBACK_FILENAME);
|
File fallbackFile = new File(fallbackLocation, ACCOUTING_FALLBACK_FILENAME);
|
||||||
|
@ -68,6 +71,16 @@ public abstract class AccountingPersistenceBackendFactory {
|
||||||
}
|
}
|
||||||
|
|
||||||
AccountingPersistenceBackend persistence = persistencePersistenceBackends.get(scope);
|
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){
|
if(persistence==null){
|
||||||
|
|
||||||
ScopeBean bean = new ScopeBean(scope);
|
ScopeBean bean = new ScopeBean(scope);
|
||||||
|
@ -104,17 +117,26 @@ public abstract class AccountingPersistenceBackendFactory {
|
||||||
} catch (Exception e) {
|
} 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);
|
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;
|
persistence = fallbackPersistence;
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch(Exception e){
|
} catch(Exception e){
|
||||||
logger.error("Unable to instance a Persistence Implementation. Using fallback as default",
|
//logger.error("Unable to instance a Persistence Implementation. Using fallback as default", e);
|
||||||
e);
|
logger.error("Unable to instatiate 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);
|
||||||
persistencePersistenceBackends.put(scope, persistence);
|
if(persistence.getClass().isInstance(FallbackPersistence.class)){
|
||||||
|
long now = Calendar.getInstance().getTimeInMillis();
|
||||||
|
falbackLastCheck.put(scope, now);
|
||||||
|
}
|
||||||
|
persistencePersistenceBackends.put(scope, persistence);
|
||||||
}
|
}
|
||||||
|
|
||||||
return persistence;
|
return persistence;
|
||||||
|
@ -125,10 +147,9 @@ public abstract class AccountingPersistenceBackendFactory {
|
||||||
* @param timeUnit
|
* @param timeUnit
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static void flush(long timeout, TimeUnit timeUnit) {
|
public static void flushAll(long timeout, TimeUnit timeUnit) {
|
||||||
for(String scope : persistencePersistenceBackends.keySet()){
|
for(String scope : persistencePersistenceBackends.keySet()){
|
||||||
AccountingPersistenceBackend apb =
|
AccountingPersistenceBackend apb = persistencePersistenceBackends.get(scope);
|
||||||
persistencePersistenceBackends.get(scope);
|
|
||||||
try {
|
try {
|
||||||
logger.debug("Flushing records in scope {}", scope);
|
logger.debug("Flushing records in scope {}", scope);
|
||||||
apb.flush(timeout, timeUnit);
|
apb.flush(timeout, timeUnit);
|
||||||
|
|
Loading…
Reference in New Issue