Adding a connection renewal to refresh connection when to much fallback occurs

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/document-store-lib@131781 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-09-26 10:58:58 +00:00
parent 0d0925cb14
commit 0bb8ae81e0
2 changed files with 42 additions and 0 deletions

View File

@ -24,11 +24,13 @@ public abstract class PersistenceBackend {
protected PersistenceBackendMonitor persistenceBackendMonitor;
protected boolean closed;
protected PersistenceBackend(){
if(!(this instanceof FallbackPersistenceBackend)){
this.persistenceBackendMonitor = new PersistenceBackendMonitor(this);
}
closed = true;
}
protected PersistenceBackend(FallbackPersistenceBackend fallback){
@ -92,6 +94,8 @@ public abstract class PersistenceBackend {
this.reallyAccount(record);
logger.trace("{} accounted succesfully from {}.", record.toString(), persistenceName);
} catch (Exception e) {
// TODO Insert Renew HERE
try {
String fallabackPersistenceName = FallbackPersistenceBackend.class.getSimpleName();
logger.error("{} was not accounted succesfully from {}. Trying to use {}.",
@ -156,6 +160,14 @@ public abstract class PersistenceBackend {
aggregationScheduler.flush(new DefaultPersitenceExecutor(this));
}
public boolean isOpen(){
return !closed;
}
protected void setOpen(){
this.closed = false;
}
public abstract void close() throws Exception;
}

View File

@ -6,7 +6,9 @@ package org.gcube.documentstore.persistence;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.gcube.documentstore.records.RecordUtility;
@ -121,6 +123,7 @@ public abstract class PersistenceBackendFactory {
continue;
}
found.prepareConnection(configuration);
found.setOpen();
logger.trace("{} will be used.", foundClassName);
@ -207,6 +210,33 @@ public abstract class PersistenceBackendFactory {
return actual;
}
protected synchronized static void renew(PersistenceBackend persistenceBackend){
String context = null;
Set<Entry<String, PersistenceBackend>> entrySet = persistenceBackends.entrySet();
for(Entry<String, PersistenceBackend> entry : entrySet){
if(entry.getValue() == persistenceBackend){
context = entry.getKey();
}
return;
}
FallbackPersistenceBackend fallbackPersistenceBackend =
persistenceBackend.getFallbackPersistence();
try {
persistenceBackend.close();
} catch (Exception e) {
logger.error("Error while closing {} : {}",
PersistenceBackend.class.getSimpleName(),
persistenceBackend, e);
}
persistenceBackends.put(context, fallbackPersistenceBackend);
new PersistenceBackendRediscover(context,
fallbackPersistenceBackend, INITIAL_DELAY,
FALLBACK_RETRY_TIME, TimeUnit.MILLISECONDS);
}
public static void flush(String context, long timeout, TimeUnit timeUnit){
context = sanitizeContext(context);