diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2019-01-10 22:30:25 +0100 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2019-01-10 22:30:54 +0100 |
commit | 74133ed765c29c674d654d93f63b61b236e72309 (patch) | |
tree | 274cc708bc033c65c3299a3e391910f1cc58ef08 /node-repository | |
parent | 5cf62f18b232d8989edfecf3097585f22e997278 (diff) |
Use inner classes and make an Session interface implemented by a Cache.
Diffstat (limited to 'node-repository')
3 files changed, 27 insertions, 34 deletions
diff --git a/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/persistence/CuratorCache.java b/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/persistence/CuratorCache.java deleted file mode 100644 index 674aa1b8f22..00000000000 --- a/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/persistence/CuratorCache.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.yahoo.vespa.hosted.provision.persistence; - -import com.yahoo.path.Path; - -import java.util.List; -import java.util.Optional; - -public abstract class CuratorCache { - - public abstract List<String> getChildren(Path path); - - public abstract Optional<byte[]> getData(Path path); - -}
\ No newline at end of file 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 3da54a3db8c..8f4788a9206 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 @@ -36,7 +36,7 @@ public class CuratorDatabase { private final CuratorCounter changeGenerationCounter; /** A partial cache of the Curator database, which is only valid if generations match */ - private final AtomicReference<CuratorDatabaseCache> cache = new AtomicReference<>(); + private final AtomicReference<Cache> cache = new AtomicReference<>(); /** Whether we should return data from the cache or always read fro ZooKeeper */ private final boolean useCache; @@ -125,7 +125,7 @@ public class CuratorDatabase { Optional<byte[]> getData(Path path) { return getSession().getData(path); } /** Invalidates the current cache if outdated. */ - CuratorCache getSession() { + Session getSession() { if (changeGenerationCounter.get() != cache.get().generation) synchronized (cacheCreationLock) { while (changeGenerationCounter.get() != cache.get().generation) @@ -136,8 +136,8 @@ public class CuratorDatabase { } /** Caches must only be instantiated using this method */ - private CuratorDatabaseCache newCache(long generation) { - return useCache ? new CuratorDatabaseCache(generation, curator) : new DeactivatedCache(generation, curator); + private Cache newCache(long generation) { + return useCache ? new Cache(generation, curator) : new NoCache(generation, curator); } /** @@ -145,10 +145,10 @@ public class CuratorDatabase { * This is merely a recording of what Curator returned at various points in time when * it had the counter at this generation. */ - private static class CuratorDatabaseCache extends CuratorCache { + private static class Cache implements Session { private final long generation; - + /** The curator instance used to fetch missing data */ protected final Curator curator; @@ -159,23 +159,17 @@ public class CuratorDatabase { private final Map<Path, Optional<byte[]>> data = new ConcurrentHashMap<>(); /** Create an empty snapshot at a given generation (as an empty snapshot is a valid partial snapshot) */ - private CuratorDatabaseCache(long generation, Curator curator) { + private Cache(long generation, Curator curator) { this.generation = generation; this.curator = curator; } - public long generation() { return generation; } - - /** - * Returns the children of this path, which may be empty. - */ + @Override public List<String> getChildren(Path path) { return children.computeIfAbsent(path, key -> ImmutableList.copyOf(curator.getChildren(path))); } - /** - * Returns the a copy of the content of this child - which may be empty. - */ + @Override public Optional<byte[]> getData(Path path) { return data.computeIfAbsent(path, key -> curator.getData(path)).map(data -> Arrays.copyOf(data, data.length)); } @@ -183,9 +177,9 @@ public class CuratorDatabase { } /** An implementation of the curator database cache which does no caching */ - private static class DeactivatedCache extends CuratorDatabaseCache { - - private DeactivatedCache(long generation, Curator curator) { super(generation, curator); } + private static class NoCache extends Cache { + + private NoCache(long generation, Curator curator) { super(generation, curator); } @Override public List<String> getChildren(Path path) { return curator.getChildren(path); } @@ -195,4 +189,17 @@ public class CuratorDatabase { } + interface Session { + + /** + * Returns the children of this path, which may be empty. + */ + List<String> getChildren(Path path); + + /** + * Returns the a copy of the content of this child - which may be empty. + */ + Optional<byte[]> getData(Path path); + + } } diff --git a/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/persistence/CuratorDatabaseClient.java b/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/persistence/CuratorDatabaseClient.java index 049f242ed2e..a2869a82654 100644 --- a/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/persistence/CuratorDatabaseClient.java +++ b/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/persistence/CuratorDatabaseClient.java @@ -258,7 +258,7 @@ public class CuratorDatabaseClient { List<Node> nodes = new ArrayList<>(); if (states.length == 0) states = Node.State.values(); - CuratorCache session = curatorDatabase.getSession(); + CuratorDatabase.Session session = curatorDatabase.getSession(); for (Node.State state : states) { for (String hostname : session.getChildren(toPath(state))) { Optional<Node> node = getNode(session, hostname, state); @@ -282,7 +282,7 @@ public class CuratorDatabaseClient { * Returns a particular node, or empty if this noe is not in any of the given states. * If no states are given this returns the node if it is present in any state. */ - public Optional<Node> getNode(CuratorCache session, String hostname, Node.State ... states) { + public Optional<Node> getNode(CuratorDatabase.Session session, String hostname, Node.State ... states) { if (states.length == 0) states = Node.State.values(); for (Node.State state : states) { |