diff options
Diffstat (limited to 'configserver/src/main/java/com/yahoo/vespa/config/server/zookeeper/ZKApplication.java')
-rw-r--r-- | configserver/src/main/java/com/yahoo/vespa/config/server/zookeeper/ZKApplication.java | 103 |
1 files changed, 35 insertions, 68 deletions
diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/zookeeper/ZKApplication.java b/configserver/src/main/java/com/yahoo/vespa/config/server/zookeeper/ZKApplication.java index 084f26bd368..afdc7c8bad4 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/zookeeper/ZKApplication.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/zookeeper/ZKApplication.java @@ -3,11 +3,12 @@ package com.yahoo.vespa.config.server.zookeeper; import com.yahoo.io.reader.NamedReader; import com.yahoo.path.Path; +import com.yahoo.text.Utf8; +import com.yahoo.vespa.curator.Curator; import java.io.Reader; import java.io.StringReader; import java.util.ArrayList; -import java.util.Collections; import java.util.List; /** @@ -18,11 +19,11 @@ import java.util.List; */ public class ZKApplication { - private final ConfigCurator zk; + private final Curator curator; private final Path appPath; - ZKApplication(ConfigCurator zk, Path appPath) { - this.zk = zk; + ZKApplication(Curator curator, Path appPath) { + this.curator = curator; this.appPath = appPath; } @@ -37,7 +38,7 @@ public class ZKApplication { * @return the files in the given path, or an empty list if the directory does not exist or is empty. * The list gets owned by the caller and can be modified freely. */ - List<NamedReader> getAllDataFromDirectory(String path, String fileNameSuffix, boolean recursive) { + List<NamedReader> getAllDataFromDirectory(Path path, String fileNameSuffix, boolean recursive) { return getAllDataFromDirectory(path, "", fileNameSuffix, recursive); } @@ -46,23 +47,22 @@ public class ZKApplication { * * @param namePrefix the prefix to prepend to the returned reader names */ - private List<NamedReader> getAllDataFromDirectory(String path, String namePrefix, String fileNameSuffix, boolean recursive) { - String fullPath = getFullPath(path); + private List<NamedReader> getAllDataFromDirectory(Path path, String namePrefix, String fileNameSuffix, boolean recursive) { List<NamedReader> result = new ArrayList<>(); List<String> children = getChildren(path); try { for (String child : children) { if (fileNameSuffix == null || child.endsWith(fileNameSuffix)) { - result.add(new NamedReader(namePrefix + child, reader(zk.getData(fullPath, child)))); + result.add(new NamedReader(namePrefix + child, reader(getData(path.append(child))))); } if (recursive) - result.addAll(getAllDataFromDirectory(path + "/" + child, + result.addAll(getAllDataFromDirectory(path.append(child), namePrefix + child + "/", fileNameSuffix, recursive)); } return result; } catch (Exception e) { - throw new RuntimeException("Could not retrieve all data from '" + fullPath + "' in zookeeper", e); + throw new RuntimeException("Could not retrieve all data from '" + path + "' in zookeeper", e); } } @@ -70,45 +70,33 @@ public class ZKApplication { * Retrieves a node relative to the node of the live application * * @param path a path relative to the currently active application - * @param node a path relative to the path above * @return a Reader that can be used to get the data */ - Reader getDataReader(String path, String node) { - return reader(getData(path, node)); + Reader getDataReader(Path path) { + return reader(getData(path)); } - public String getData(String path, String node) { - if ( ! exists(path, node)) throw new IllegalArgumentException("No node for " + getFullPath(path) + "/" + node + " exists"); - - try { - return zk.getData(getFullPath(path), node); - } catch (Exception e) { - throw new IllegalArgumentException("Could not retrieve node '" + - getFullPath(path) + "/" + node + "' in zookeeper", e); - } + NamedReader getNamedReader(String name, Path path) { + return new NamedReader(name, reader(getData(path))); } - public String getData(String path) { - if ( ! exists(path)) throw new IllegalArgumentException("No node for " + getFullPath(path) + " exists"); + public String getData(Path path) { + return Utf8.toString(getBytesInternal(getFullPath(path))); + } - try { - return zk.getData(getFullPath(path)); - } catch (RuntimeException e) { - throw new IllegalArgumentException("Could not retrieve path '" + getFullPath(path) + "' in zookeeper", e); - } + private byte[] getBytesInternal(Path path) { + return curator.getData(path) + .orElseThrow(() -> new IllegalArgumentException("Could not get data from '" + + path + "' in zookeeper")); } - public byte[] getBytes(String path) { - try { - return zk.getBytes(getFullPath(path)); - } catch (RuntimeException e) { - throw new IllegalArgumentException("Could not retrieve path '" + getFullPath(path) + "' in zookeeper", e); - } + public byte[] getBytes(Path path) { + return getBytesInternal(getFullPath(path)); } - void putData(String path, String data) { + void putData(Path path, String data) { try { - zk.putData(getFullPath(path), data); + curator.set(getFullPath(path), Utf8.toBytes(data)); } catch (RuntimeException e) { throw new IllegalArgumentException("Could not put data to node '" + getFullPath(path) + "' in zookeeper", e); } @@ -118,29 +106,18 @@ public class ZKApplication { * Checks if the given node exists under path under this live app * * @param path a zookeeper path - * @param node a zookeeper node * @return true if the node exists in the path, false otherwise */ - public boolean exists(String path, String node) { - return zk.exists(getFullPath(path), node); + public boolean exists(Path path) { + return curator.exists(getFullPath(path)); } - /** - * Checks if the given node exists under path under this live app - * - * @param path a zookeeper path - * @return true if the node exists in the path, false otherwise - */ - public boolean exists(String path) { - return zk.exists(getFullPath(path)); - } - - private String getFullPath(String path) { + private Path getFullPath(Path path) { Path fullPath = appPath; if (path != null) { fullPath = appPath.append(path); } - return fullPath.getAbsolute(); + return fullPath; } /** @@ -148,8 +125,8 @@ public class ZKApplication { * * @param path path to delete */ - void deleteRecurse(String path) { - zk.deleteRecurse(getFullPath(path)); + void deleteRecurse(Path path) { + curator.delete(getFullPath(path)); } /** @@ -158,31 +135,21 @@ public class ZKApplication { * @param path a path relative to the currently active application * @return a list of file names, which is empty (never null) if the path does not exist */ - public List<String> getChildren(String path) { - String fullPath = getFullPath(path); - if (! zk.exists(fullPath)) return Collections.emptyList(); - return zk.getChildren(fullPath); + public List<String> getChildren(Path path) { + return curator.getChildren(getFullPath(path)); } private static Reader reader(String string) { return new StringReader(string); } - public void create(String path) { - if (path != null && !path.startsWith("/")) path = "/" + path; + public void create(Path path) { try { - zk.createNode(getFullPath(path)); + curator.create(getFullPath(path)); } catch (RuntimeException e) { throw new IllegalArgumentException(e); } } - Reader getDataReader(String path) { - String data = getData(path); - if (data == null) - throw new IllegalArgumentException("No node for " + getFullPath(path) + " exists"); - return reader(data); - } - } |