ODLPARENT-139: re-use caches when restarting 66/67966/1
authorStephen Kitt <skitt@redhat.com>
Tue, 6 Feb 2018 15:46:51 +0000 (16:46 +0100)
committerStephen Kitt <skitt@redhat.com>
Tue, 6 Feb 2018 15:46:51 +0000 (16:46 +0100)
When the Shiro bundles restart, for whatever reason, they always
attempt to re-create their caches; that fails because the caches are
still present. This patch re-uses existing cache managers and caches
when possible.

This doesn’t entirely resolve the SSH issues reported in
ODLPARENT-139, but it helps the SSH connection survive longer (which
makes it easier to debug).

Change-Id: I27944a87cfbd78b385274dee0c7c17b9aac4dd58
Signed-off-by: Stephen Kitt <skitt@redhat.com>
aaa-cli/src/main/java/org/opendaylight/aaa/cli/SessionsManager.java
aaa-shiro/impl/src/main/java/org/opendaylight/aaa/datastore/h2/H2TokenStore.java

index 4c538d18a59b096ecc7bc1365f1a3a91799d9cfc..75f01ca783dcaf02f5f58812df9724bc51b36a4a 100644 (file)
@@ -39,12 +39,21 @@ public final class SessionsManager implements AutoCloseable {
     private static final String CLI_CACHE = "users";
 
     private SessionsManager() {
-        CacheManager cm = CacheManager.newInstance();
-        authUsers = new Cache(new CacheConfiguration(CLI_CACHE, MAX_CACHED_USERS_IN_MEMORY)
-                .maxEntriesLocalDisk(MAX_CACHED_USERS_ON_DISK).timeToLiveSeconds(SECONDS_TO_LIVE)
-                .timeToIdleSeconds(SECONDS_TO_IDLE));
-        cm.addCache(authUsers);
-        cm.setName(CLI_CACHE_MANAGER);
+        // When we restart, the cache manager and CLI cache are already there
+        CacheManager cm = CacheManager.getCacheManager(CLI_CACHE_MANAGER);
+        if (cm == null) {
+            cm = CacheManager.newInstance();
+            cm.setName(CLI_CACHE_MANAGER);
+        }
+        Cache existingCache = cm.getCache(CLI_CACHE);
+        if (existingCache != null) {
+            authUsers = existingCache;
+        } else {
+            authUsers = new Cache(new CacheConfiguration(CLI_CACHE, MAX_CACHED_USERS_IN_MEMORY)
+                    .maxEntriesLocalDisk(MAX_CACHED_USERS_ON_DISK).timeToLiveSeconds(SECONDS_TO_LIVE)
+                    .timeToIdleSeconds(SECONDS_TO_IDLE));
+            cm.addCache(authUsers);
+        }
         LOG.info("Initialized cli authorized users cache manager");
     }
 
@@ -56,7 +65,7 @@ public final class SessionsManager implements AutoCloseable {
     }
 
     @Override
-    public void close() throws Exception {
+    public void close() {
         LOG.info("Shutting down cli authorized users cache manager");
         CacheManager.getInstance().shutdown();
     }
index 7764f4d1ad3e579fe9c7f72db799f335e7b10bf7..39b2cb92d4993404e02a9d8c5b96aa2b243c3850 100644 (file)
@@ -28,18 +28,27 @@ public class H2TokenStore implements AutoCloseable, TokenStore {
     private final Cache tokens;
 
     public H2TokenStore(long secondsToLive, long secondsToIdle) {
-        CacheManager cm = CacheManager.newInstance();
-        tokens = new Cache(new CacheConfiguration(TOKEN_CACHE, maxCachedTokensInMemory)
-                                    .maxEntriesLocalDisk(maxCachedTokensOnDisk)
-                                    .timeToLiveSeconds(secondsToLive)
-                                    .timeToIdleSeconds(secondsToIdle));
-        cm.addCache(tokens);
-        cm.setName(TOKEN_CACHE_MANAGER);
+        // When we restart, the cache manager and token cache are already there
+        CacheManager cm = CacheManager.getCacheManager(TOKEN_CACHE_MANAGER);
+        if (cm == null) {
+            cm = CacheManager.newInstance();
+            cm.setName(TOKEN_CACHE_MANAGER);
+        }
+        Cache existingCache = cm.getCache(TOKEN_CACHE);
+        if (existingCache != null) {
+            tokens = existingCache;
+        } else {
+            tokens = new Cache(new CacheConfiguration(TOKEN_CACHE, maxCachedTokensInMemory)
+                    .maxEntriesLocalDisk(maxCachedTokensOnDisk)
+                    .timeToLiveSeconds(secondsToLive)
+                    .timeToIdleSeconds(secondsToIdle));
+            cm.addCache(tokens);
+        }
         LOG.info("Initialized token store with default cache config");
     }
 
     @Override
-    public void close() throws Exception {
+    public void close() {
         LOG.info("Shutting down token store...");
         CacheManager.getInstance().shutdown();
     }