From 0bb8ae81e0d2dd9769bc45627ad3b3efd28204a1 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Mon, 26 Sep 2016 10:58:58 +0000 Subject: [PATCH] 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 --- .../persistence/PersistenceBackend.java | 12 ++++++++ .../PersistenceBackendFactory.java | 30 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/main/java/org/gcube/documentstore/persistence/PersistenceBackend.java b/src/main/java/org/gcube/documentstore/persistence/PersistenceBackend.java index b1ee8fc..5d7447c 100644 --- a/src/main/java/org/gcube/documentstore/persistence/PersistenceBackend.java +++ b/src/main/java/org/gcube/documentstore/persistence/PersistenceBackend.java @@ -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; } diff --git a/src/main/java/org/gcube/documentstore/persistence/PersistenceBackendFactory.java b/src/main/java/org/gcube/documentstore/persistence/PersistenceBackendFactory.java index ba3c44d..293d45a 100644 --- a/src/main/java/org/gcube/documentstore/persistence/PersistenceBackendFactory.java +++ b/src/main/java/org/gcube/documentstore/persistence/PersistenceBackendFactory.java @@ -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> entrySet = persistenceBackends.entrySet(); + for(Entry 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);