diff options
author | Jon Marius Venstad <jvenstad@yahoo-inc.com> | 2018-12-12 11:32:56 +0100 |
---|---|---|
committer | Jon Marius Venstad <jvenstad@yahoo-inc.com> | 2018-12-12 11:32:56 +0100 |
commit | 515a8af2497b94d6eaeb0eb7b2758786c3871c75 (patch) | |
tree | 12ad6cba1b4e4b387ef3e92989b0ab1cf5566b32 | |
parent | 57ea9daa0b3781b5591129ed7e25379aaea9bfcf (diff) |
Simplify cache handling
-rw-r--r-- | node-repository/src/main/java/com/yahoo/vespa/hosted/provision/persistence/CuratorDatabase.java | 40 |
1 files changed, 6 insertions, 34 deletions
diff --git a/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/persistence/CuratorDatabase.java b/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/persistence/CuratorDatabase.java index e3a879dc405..b82dae06007 100644 --- a/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/persistence/CuratorDatabase.java +++ b/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/persistence/CuratorDatabase.java @@ -113,42 +113,14 @@ public class CuratorDatabase { Optional<byte[]> getData(Path path) { return getCache().getData(path); } - private static class CacheAndGeneration { - public CacheAndGeneration(CuratorDatabaseCache cache, long generation) - { - this.cache = cache; - this.generation = generation; - } - public boolean expired() { - return generation != cache.generation(); - } - public CuratorDatabaseCache validCache() { - if (expired()) { - throw new IllegalStateException("The cache has generation " + cache.generation() + - " while the root genration counter in zookeeper says " + generation + - ". That is totally unacceptable and must be a sever programming error in my close vicinity."); - } - return cache; - } - - private CuratorDatabaseCache cache; - private long generation; - } - private CacheAndGeneration getCacheSnapshot() { - return new CacheAndGeneration(cache.get(), changeGenerationCounter.get()); - } + /** Invalidates the current cache if outdated. */ private CuratorDatabaseCache getCache() { - CacheAndGeneration cacheAndGeneration = getCacheSnapshot(); - while (cacheAndGeneration.expired()) { - synchronized (cacheCreationLock) { // Prevent a race for creating new caches - cacheAndGeneration = getCacheSnapshot(); - if (cacheAndGeneration.expired()) { - cache.set(newCache(changeGenerationCounter.get())); - cacheAndGeneration = getCacheSnapshot(); - } - } + long generation = changeGenerationCounter.get(); + synchronized (cacheCreationLock) { + if (generation != cache.get().generation) + cache.set(newCache(generation)); } - return cacheAndGeneration.validCache(); + return cache.get(); } /** Caches must only be instantiated using this method */ |