feat(agents): move agents into dbx repo
This commit is contained in:
1 parent
ab35fda299
commit
3a25487dea
215 files changed
+29740
-15
No files matched your search
@@ -0,0 +1,549 @@
|
||||
package com.dbx.agent.zookeeper;
|
||||
|
||||
import com.dbx.agent.AgentProtocol;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.CharacterCodingException;
|
||||
import java.nio.charset.CharsetDecoder;
|
||||
import java.nio.charset.CodingErrorAction;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.apache.curator.framework.CuratorFramework;
|
||||
import org.apache.curator.framework.CuratorFrameworkFactory;
|
||||
import org.apache.curator.retry.ExponentialBackoffRetry;
|
||||
import org.apache.zookeeper.KeeperException;
|
||||
import org.apache.zookeeper.data.Stat;
|
||||
|
||||
public final class ZooKeeperAgent {
|
||||
private static final Gson GSON = new GsonBuilder().serializeNulls().create();
|
||||
private static final int DEFAULT_LIMIT = 100;
|
||||
private static final int DEFAULT_SESSION_TIMEOUT_MS = 30000;
|
||||
private static final int DEFAULT_CONNECTION_TIMEOUT_MS = 15000;
|
||||
private static final int DEFAULT_BASE_SLEEP_TIME_MS = 1000;
|
||||
private static final int DEFAULT_MAX_RETRIES = 3;
|
||||
private static final List<String> CAPABILITIES = Collections.unmodifiableList(Arrays.asList(
|
||||
AgentProtocol.CAPABILITY_CONNECT,
|
||||
AgentProtocol.CAPABILITY_TEST_CONNECTION,
|
||||
AgentProtocol.CAPABILITY_KV
|
||||
));
|
||||
private static CuratorFramework client;
|
||||
|
||||
private ZooKeeperAgent() {
|
||||
}
|
||||
|
||||
private static Object handshakeResult() {
|
||||
return new HandshakeResult(AgentProtocol.PROTOCOL_VERSION, AgentProtocol.PROTOCOL_VERSION, CAPABILITIES);
|
||||
}
|
||||
|
||||
private static Object connect(JsonObject params) throws Exception {
|
||||
JsonObject connection = connectionObject(params);
|
||||
CuratorFramework nextClient = buildClient(connection);
|
||||
try {
|
||||
startAndVerify(nextClient, connection);
|
||||
closeClient();
|
||||
client = nextClient;
|
||||
return Collections.singletonMap("ok", true);
|
||||
} catch (Exception e) {
|
||||
nextClient.close();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
private static Object testConnection(JsonObject params) throws Exception {
|
||||
JsonObject connection = connectionObject(params);
|
||||
CuratorFramework probe = buildClient(connection);
|
||||
try {
|
||||
startAndVerify(probe, connection);
|
||||
return Collections.singletonMap("ok", true);
|
||||
} finally {
|
||||
probe.close();
|
||||
}
|
||||
}
|
||||
|
||||
static CuratorFramework buildClient(JsonObject connection) {
|
||||
if (hasTlsOptions(connection)) {
|
||||
throw new IllegalArgumentException("ZooKeeper TLS is not supported");
|
||||
}
|
||||
|
||||
int baseSleepTimeMs = intOrDefault(connection, "base_sleep_time_ms", DEFAULT_BASE_SLEEP_TIME_MS);
|
||||
int maxRetries = intOrDefault(connection, "max_retries", DEFAULT_MAX_RETRIES);
|
||||
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
|
||||
.connectString(connectString(connection))
|
||||
.sessionTimeoutMs(intOrDefault(connection, "session_timeout_ms", DEFAULT_SESSION_TIMEOUT_MS))
|
||||
.connectionTimeoutMs(intOrDefault(connection, "connection_timeout_ms", DEFAULT_CONNECTION_TIMEOUT_MS))
|
||||
.retryPolicy(new ExponentialBackoffRetry(baseSleepTimeMs, maxRetries));
|
||||
|
||||
String namespace = trimSlashes(stringOrEmpty(connection, "namespace"));
|
||||
if (!namespace.isBlank()) {
|
||||
builder.namespace(namespace);
|
||||
}
|
||||
|
||||
String username = stringOrEmpty(connection, "username");
|
||||
String password = stringOrEmpty(connection, "password");
|
||||
if (!username.isBlank()) {
|
||||
builder.authorization("digest", (username + ":" + password).getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
static String connectString(JsonObject connection) {
|
||||
String configured = firstNonBlank(
|
||||
stringOrNull(connection, "zookeeper_connect_string"),
|
||||
stringOrNull(connection, "connect_string"),
|
||||
stringOrNull(connection, "connection_string")
|
||||
);
|
||||
if (configured != null) {
|
||||
return configured;
|
||||
}
|
||||
return stringOrDefault(connection, "host", "127.0.0.1") + ":"
|
||||
+ intOrDefault(connection, "port", 2181);
|
||||
}
|
||||
|
||||
private static void startAndVerify(CuratorFramework active, JsonObject connection) throws Exception {
|
||||
active.start();
|
||||
int timeoutMs = intOrDefault(connection, "connection_timeout_ms", DEFAULT_CONNECTION_TIMEOUT_MS);
|
||||
if (!active.blockUntilConnected(timeoutMs, TimeUnit.MILLISECONDS)) {
|
||||
throw new IllegalStateException("Connection timed out");
|
||||
}
|
||||
if (active.checkExists().forPath("/") == null) {
|
||||
throw new IllegalStateException("Root znode is not readable");
|
||||
}
|
||||
}
|
||||
|
||||
private static Object listPrefix(JsonObject params) throws Exception {
|
||||
CuratorFramework active = requireClient();
|
||||
String root = normalizePath(stringOrDefault(params, "prefix", "/"));
|
||||
boolean recursive = boolOrDefault(params, "recursive", true);
|
||||
int limit = Math.max(1, intOrDefault(params, "limit", DEFAULT_LIMIT));
|
||||
Cursor cursor = null;
|
||||
String continuation = stringOrNull(params, "continuation");
|
||||
if (continuation != null && !continuation.isBlank()) {
|
||||
cursor = decodeCursor(continuation);
|
||||
if (!root.equals(cursor.root) || recursive != cursor.recursive) {
|
||||
throw new IllegalArgumentException("Continuation does not match request");
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
List<Map<String, Object>> keys = new ArrayList<>();
|
||||
Stat rootStat = active.checkExists().forPath(root);
|
||||
if (rootStat == null) {
|
||||
result.put("keys", keys);
|
||||
result.put("continuation", null);
|
||||
return result;
|
||||
}
|
||||
|
||||
List<String> paths = recursive ? listRecursive(active, root) : listDirectChildren(active, root);
|
||||
paths.removeIf(path -> shouldHideFromRootListing(root, path));
|
||||
Collections.sort(paths);
|
||||
int offset = cursor == null ? 0 : Math.max(0, cursor.offset);
|
||||
int end = Math.min(paths.size(), offset + limit);
|
||||
for (int index = offset; index < end; index++) {
|
||||
Stat stat = active.checkExists().forPath(paths.get(index));
|
||||
if (stat == null) {
|
||||
continue;
|
||||
}
|
||||
Map<String, Object> row = new LinkedHashMap<>();
|
||||
row.put("key", paths.get(index));
|
||||
row.putAll(statMetadata(stat));
|
||||
keys.add(row);
|
||||
}
|
||||
|
||||
result.put("keys", keys);
|
||||
result.put("continuation", end < paths.size() ? encodeCursor(new Cursor(root, recursive, end)) : null);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Object get(JsonObject params) throws Exception {
|
||||
CuratorFramework active = requireClient();
|
||||
String path = normalizePath(stringOrDefault(params, "key", ""));
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
Stat exists = active.checkExists().forPath(path);
|
||||
if (exists == null) {
|
||||
result.put("found", false);
|
||||
result.put("key", path);
|
||||
result.put("value", null);
|
||||
result.put("metadata", null);
|
||||
return result;
|
||||
}
|
||||
|
||||
Stat stat = new Stat();
|
||||
byte[] bytes = active.getData().storingStatIn(stat).forPath(path);
|
||||
result.put("found", true);
|
||||
result.put("key", path);
|
||||
result.put("value", valueObject(bytes));
|
||||
result.put("metadata", statMetadata(stat));
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Object put(JsonObject params) throws Exception {
|
||||
CuratorFramework active = requireClient();
|
||||
String path = normalizePath(stringOrDefault(params, "key", ""));
|
||||
requireMutablePath(path, "modified");
|
||||
byte[] bytes = parseValue(params.getAsJsonObject("value"));
|
||||
|
||||
Stat stat = active.checkExists().forPath(path);
|
||||
if (stat == null) {
|
||||
active.create().creatingParentsIfNeeded().forPath(path, bytes);
|
||||
stat = active.checkExists().forPath(path);
|
||||
} else {
|
||||
stat = active.setData().forPath(path, bytes);
|
||||
}
|
||||
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
result.put("version", stat.getVersion());
|
||||
result.put("mtime", stat.getMtime());
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Object delete(JsonObject params) throws Exception {
|
||||
CuratorFramework active = requireClient();
|
||||
String path = normalizePath(stringOrDefault(params, "key", ""));
|
||||
requireMutablePath(path, "deleted");
|
||||
boolean recursive = boolOrDefault(params, "recursive", false);
|
||||
int deleted;
|
||||
try {
|
||||
if (active.checkExists().forPath(path) == null) {
|
||||
deleted = 0;
|
||||
} else if (recursive) {
|
||||
deleted = countSubtree(active, path);
|
||||
active.delete().deletingChildrenIfNeeded().forPath(path);
|
||||
} else {
|
||||
active.delete().forPath(path);
|
||||
deleted = 1;
|
||||
}
|
||||
} catch (KeeperException.NoNodeException e) {
|
||||
deleted = 0;
|
||||
}
|
||||
return Collections.singletonMap("deleted", deleted);
|
||||
}
|
||||
|
||||
private static Object dispatch(String method, JsonObject params) throws Exception {
|
||||
return switch (method) {
|
||||
case AgentProtocol.METHOD_HANDSHAKE -> handshakeResult();
|
||||
case AgentProtocol.METHOD_CONNECT -> connect(params);
|
||||
case AgentProtocol.METHOD_TEST_CONNECTION -> testConnection(params);
|
||||
case AgentProtocol.KV_METHOD_LIST_PREFIX -> listPrefix(params);
|
||||
case AgentProtocol.KV_METHOD_GET -> get(params);
|
||||
case AgentProtocol.KV_METHOD_PUT -> put(params);
|
||||
case AgentProtocol.KV_METHOD_DELETE -> delete(params);
|
||||
case AgentProtocol.METHOD_DISCONNECT -> {
|
||||
closeClient();
|
||||
yield Collections.singletonMap("ok", true);
|
||||
}
|
||||
case AgentProtocol.METHOD_SHUTDOWN -> {
|
||||
closeClient();
|
||||
System.exit(0);
|
||||
yield Collections.singletonMap("ok", true);
|
||||
}
|
||||
default -> throw new IllegalArgumentException("Unknown method: " + method);
|
||||
};
|
||||
}
|
||||
|
||||
static String handleRequest(String line) {
|
||||
JsonObject req = JsonParser.parseString(line).getAsJsonObject();
|
||||
JsonElement id = req.get("id");
|
||||
String method = req.get("method").getAsString();
|
||||
JsonObject params = req.has("params") && req.get("params").isJsonObject()
|
||||
? req.getAsJsonObject("params")
|
||||
: new JsonObject();
|
||||
|
||||
JsonObject response = new JsonObject();
|
||||
response.addProperty("jsonrpc", "2.0");
|
||||
response.add("id", id);
|
||||
|
||||
try {
|
||||
Object result = dispatch(method, params);
|
||||
response.add("result", GSON.toJsonTree(result));
|
||||
} catch (Exception e) {
|
||||
JsonObject error = new JsonObject();
|
||||
error.addProperty("code", -1);
|
||||
error.addProperty("message", e.getMessage() == null ? "Unknown error" : e.getMessage());
|
||||
response.add("error", error);
|
||||
}
|
||||
|
||||
return GSON.toJson(response);
|
||||
}
|
||||
|
||||
private static JsonObject connectionObject(JsonObject params) {
|
||||
JsonElement connection = params.get("connection");
|
||||
return connection != null && connection.isJsonObject() ? connection.getAsJsonObject() : params;
|
||||
}
|
||||
|
||||
private static CuratorFramework requireClient() {
|
||||
if (client == null) {
|
||||
throw new IllegalStateException("Not connected");
|
||||
}
|
||||
return client;
|
||||
}
|
||||
|
||||
private static void closeClient() {
|
||||
if (client != null) {
|
||||
client.close();
|
||||
client = null;
|
||||
}
|
||||
}
|
||||
|
||||
static String normalizePath(String path) {
|
||||
String normalized = path == null ? "" : path;
|
||||
if (normalized.isEmpty() || "/".equals(normalized)) {
|
||||
return "/";
|
||||
}
|
||||
if (!normalized.startsWith("/")) {
|
||||
normalized = "/" + normalized;
|
||||
}
|
||||
while (normalized.length() > 1 && normalized.endsWith("/")) {
|
||||
normalized = normalized.substring(0, normalized.length() - 1);
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
private static String childPath(String parent, String child) {
|
||||
return "/".equals(parent) ? "/" + child : parent + "/" + child;
|
||||
}
|
||||
|
||||
private static void requireMutablePath(String path, String action) {
|
||||
if ("/".equals(path)) {
|
||||
throw new IllegalArgumentException("Root znode cannot be " + action);
|
||||
}
|
||||
}
|
||||
|
||||
private static Map<String, Object> statMetadata(Stat stat) {
|
||||
Map<String, Object> metadata = new LinkedHashMap<>();
|
||||
long czxid = stat.getCzxid();
|
||||
long mzxid = stat.getMzxid();
|
||||
int dataLength = stat.getDataLength();
|
||||
metadata.put("czxid", czxid);
|
||||
metadata.put("mzxid", mzxid);
|
||||
metadata.put("ctime", stat.getCtime());
|
||||
metadata.put("mtime", stat.getMtime());
|
||||
metadata.put("version", stat.getVersion());
|
||||
metadata.put("cversion", stat.getCversion());
|
||||
metadata.put("aversion", stat.getAversion());
|
||||
metadata.put("ephemeralOwner", stat.getEphemeralOwner());
|
||||
metadata.put("dataLength", dataLength);
|
||||
metadata.put("numChildren", stat.getNumChildren());
|
||||
metadata.put("createRevision", czxid);
|
||||
metadata.put("modRevision", mzxid);
|
||||
metadata.put("valueSize", dataLength);
|
||||
return metadata;
|
||||
}
|
||||
|
||||
private static Map<String, Object> valueObject(byte[] bytes) {
|
||||
Map<String, Object> value = new LinkedHashMap<>();
|
||||
String utf8 = strictUtf8(bytes);
|
||||
if (utf8 != null) {
|
||||
value.put("encoding", "utf8");
|
||||
value.put("data", utf8);
|
||||
} else {
|
||||
value.put("encoding", "base64");
|
||||
value.put("data", Base64.getEncoder().encodeToString(bytes));
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private static byte[] parseValue(JsonObject value) {
|
||||
if (value == null) {
|
||||
return new byte[0];
|
||||
}
|
||||
String encoding = stringOrDefault(value, "encoding", "utf8");
|
||||
String data = stringOrDefault(value, "data", "");
|
||||
if ("base64".equals(encoding)) {
|
||||
return Base64.getDecoder().decode(data);
|
||||
}
|
||||
if (!"utf8".equals(encoding)) {
|
||||
throw new IllegalArgumentException("Unsupported value encoding: " + encoding);
|
||||
}
|
||||
return data.getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private static String strictUtf8(byte[] bytes) {
|
||||
CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder()
|
||||
.onMalformedInput(CodingErrorAction.REPORT)
|
||||
.onUnmappableCharacter(CodingErrorAction.REPORT);
|
||||
try {
|
||||
return decoder.decode(ByteBuffer.wrap(bytes)).toString();
|
||||
} catch (CharacterCodingException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static String encodeCursor(Cursor cursor) {
|
||||
JsonObject object = new JsonObject();
|
||||
object.addProperty("root", cursor.root);
|
||||
object.addProperty("recursive", cursor.recursive);
|
||||
object.addProperty("offset", cursor.offset);
|
||||
return Base64.getEncoder().encodeToString(GSON.toJson(object).getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private static Cursor decodeCursor(String continuation) {
|
||||
String json = new String(Base64.getDecoder().decode(continuation), StandardCharsets.UTF_8);
|
||||
JsonObject object = JsonParser.parseString(json).getAsJsonObject();
|
||||
return new Cursor(
|
||||
object.get("root").getAsString(),
|
||||
object.get("recursive").getAsBoolean(),
|
||||
object.get("offset").getAsInt()
|
||||
);
|
||||
}
|
||||
|
||||
private static List<String> listDirectChildren(CuratorFramework active, String root) throws Exception {
|
||||
List<String> paths = new ArrayList<>();
|
||||
List<String> children = new ArrayList<>(active.getChildren().forPath(root));
|
||||
Collections.sort(children);
|
||||
for (String child : children) {
|
||||
paths.add(childPath(root, child));
|
||||
}
|
||||
return paths;
|
||||
}
|
||||
|
||||
private static List<String> listRecursive(CuratorFramework active, String root) throws Exception {
|
||||
List<String> result = new ArrayList<>();
|
||||
collectRecursive(active, root, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void collectRecursive(CuratorFramework active, String path, List<String> result) throws Exception {
|
||||
List<String> children;
|
||||
try {
|
||||
children = new ArrayList<>(active.getChildren().forPath(path));
|
||||
} catch (KeeperException.NoNodeException e) {
|
||||
return;
|
||||
}
|
||||
Collections.sort(children);
|
||||
for (String child : children) {
|
||||
String childPath = childPath(path, child);
|
||||
result.add(childPath);
|
||||
collectRecursive(active, childPath, result);
|
||||
}
|
||||
}
|
||||
|
||||
private static int countSubtree(CuratorFramework active, String path) throws Exception {
|
||||
try {
|
||||
if (active.checkExists().forPath(path) == null) {
|
||||
return 0;
|
||||
}
|
||||
int count = 1;
|
||||
for (String child : active.getChildren().forPath(path)) {
|
||||
count += countSubtree(active, childPath(path, child));
|
||||
}
|
||||
return count;
|
||||
} catch (KeeperException.NoNodeException e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean shouldHideFromRootListing(String root, String path) {
|
||||
return "/".equals(root) && ("/zookeeper".equals(path) || path.startsWith("/zookeeper/"));
|
||||
}
|
||||
|
||||
private static boolean hasTlsOptions(JsonObject connection) {
|
||||
return boolOrDefault(connection, "ssl", false)
|
||||
|| firstNonBlank(
|
||||
stringOrNull(connection, "ca_cert_path"),
|
||||
stringOrNull(connection, "client_cert_path"),
|
||||
stringOrNull(connection, "client_key_path"),
|
||||
stringOrNull(connection, "cert_path"),
|
||||
stringOrNull(connection, "key_path")
|
||||
) != null;
|
||||
}
|
||||
|
||||
private static String trimSlashes(String value) {
|
||||
String trimmed = value.trim();
|
||||
while (trimmed.startsWith("/")) {
|
||||
trimmed = trimmed.substring(1);
|
||||
}
|
||||
while (trimmed.endsWith("/")) {
|
||||
trimmed = trimmed.substring(0, trimmed.length() - 1);
|
||||
}
|
||||
return trimmed;
|
||||
}
|
||||
|
||||
private static String stringOrNull(JsonObject object, String key) {
|
||||
JsonElement element = object.get(key);
|
||||
return element == null || element.isJsonNull() ? null : element.getAsString();
|
||||
}
|
||||
|
||||
private static String stringOrEmpty(JsonObject object, String key) {
|
||||
return stringOrDefault(object, key, "");
|
||||
}
|
||||
|
||||
private static String stringOrDefault(JsonObject object, String key, String fallback) {
|
||||
String value = stringOrNull(object, key);
|
||||
return value == null ? fallback : value;
|
||||
}
|
||||
|
||||
private static int intOrDefault(JsonObject object, String key, int fallback) {
|
||||
JsonElement element = object.get(key);
|
||||
return element == null || element.isJsonNull() ? fallback : element.getAsInt();
|
||||
}
|
||||
|
||||
private static boolean boolOrDefault(JsonObject object, String key, boolean fallback) {
|
||||
JsonElement element = object.get(key);
|
||||
return element == null || element.isJsonNull() ? fallback : element.getAsBoolean();
|
||||
}
|
||||
|
||||
private static String firstNonBlank(String... values) {
|
||||
for (String value : values) {
|
||||
if (value != null && !value.isBlank()) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
// Entry point for the standalone DBX agent process.
|
||||
System.setProperty("org.slf4j.simpleLogger.logFile", "System.err");
|
||||
System.out.println("{\"ready\":true}");
|
||||
System.out.flush();
|
||||
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
|
||||
while (true) {
|
||||
String line = reader.readLine();
|
||||
if (line == null) {
|
||||
break;
|
||||
}
|
||||
|
||||
System.out.println(handleRequest(line));
|
||||
System.out.flush();
|
||||
}
|
||||
}
|
||||
|
||||
private static final class Cursor {
|
||||
private final String root;
|
||||
private final boolean recursive;
|
||||
private final int offset;
|
||||
|
||||
private Cursor(String root, boolean recursive, int offset) {
|
||||
this.root = root;
|
||||
this.recursive = recursive;
|
||||
this.offset = offset;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class HandshakeResult {
|
||||
private final int protocolVersion;
|
||||
private final int agentProtocolVersion;
|
||||
private final List<String> capabilities;
|
||||
|
||||
private HandshakeResult(int protocolVersion, int agentProtocolVersion, List<String> capabilities) {
|
||||
this.protocolVersion = protocolVersion;
|
||||
this.agentProtocolVersion = agentProtocolVersion;
|
||||
this.capabilities = capabilities;
|
||||
}
|
||||
}
|
||||
}
|
||||
+396
@@ -0,0 +1,396 @@
|
||||
package com.dbx.agent.zookeeper;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.apache.curator.test.TestingServer;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
final class ZooKeeperAgentTest {
|
||||
@AfterEach
|
||||
void disconnect() {
|
||||
ZooKeeperAgent.handleRequest("{\"jsonrpc\":\"2.0\",\"id\":999,\"method\":\"disconnect\",\"params\":{}}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void handshakeAdvertisesKvCapability() {
|
||||
String response = ZooKeeperAgent.handleRequest(
|
||||
"{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"handshake\",\"params\":{}}"
|
||||
);
|
||||
|
||||
JsonObject result = JsonParser.parseString(response).getAsJsonObject().getAsJsonObject("result");
|
||||
|
||||
Assertions.assertEquals(1, result.get("protocolVersion").getAsInt());
|
||||
Assertions.assertTrue(result.getAsJsonArray("capabilities").contains(JsonParser.parseString("\"kv\"")));
|
||||
Assertions.assertTrue(result.getAsJsonArray("capabilities").contains(JsonParser.parseString("\"connect\"")));
|
||||
Assertions.assertTrue(result.getAsJsonArray("capabilities").contains(JsonParser.parseString("\"test_connection\"")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void kvMethodDispatchReturnsJsonRpcErrorWhenDisconnected() {
|
||||
String response = ZooKeeperAgent.handleRequest(
|
||||
"{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"kv_get\",\"params\":{\"key\":\"/app/name\"}}"
|
||||
);
|
||||
|
||||
JsonObject error = JsonParser.parseString(response).getAsJsonObject().getAsJsonObject("error");
|
||||
|
||||
Assertions.assertEquals(-1, error.get("code").getAsInt());
|
||||
Assertions.assertEquals("Not connected", error.get("message").getAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownMethodReturnsJsonRpcError() {
|
||||
String response = ZooKeeperAgent.handleRequest(
|
||||
"{\"jsonrpc\":\"2.0\",\"id\":3,\"method\":\"missing_method\",\"params\":{}}"
|
||||
);
|
||||
|
||||
JsonObject error = JsonParser.parseString(response).getAsJsonObject().getAsJsonObject("error");
|
||||
|
||||
Assertions.assertEquals(-1, error.get("code").getAsInt());
|
||||
Assertions.assertEquals("Unknown method: missing_method", error.get("message").getAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void connectStringUsesConfiguredValuesAndHostPortFallback() {
|
||||
Assertions.assertEquals(
|
||||
"zk-1:2181,zk-2:2181",
|
||||
ZooKeeperAgent.connectString(
|
||||
JsonParser.parseString("{\"connect_string\":\"zk-1:2181,zk-2:2181\"}").getAsJsonObject()
|
||||
)
|
||||
);
|
||||
Assertions.assertEquals(
|
||||
"zk-main:2181",
|
||||
ZooKeeperAgent.connectString(
|
||||
JsonParser.parseString("{\"zookeeper_connect_string\":\"zk-main:2181\"}").getAsJsonObject()
|
||||
)
|
||||
);
|
||||
Assertions.assertEquals(
|
||||
"127.0.0.1:2181",
|
||||
ZooKeeperAgent.connectString(JsonParser.parseString("{}").getAsJsonObject())
|
||||
);
|
||||
Assertions.assertEquals(
|
||||
"zk.local:2281",
|
||||
ZooKeeperAgent.connectString(JsonParser.parseString("{\"host\":\"zk.local\",\"port\":2281}").getAsJsonObject())
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildClientRejectsTlsOptionsUntilTlsIsSupported() {
|
||||
JsonObject connection = JsonParser.parseString(
|
||||
"{\"ssl\":true,\"ca_cert_path\":\"ca.pem\",\"client_cert_path\":\"client.pem\",\"client_key_path\":\"client.key\"}"
|
||||
).getAsJsonObject();
|
||||
|
||||
IllegalArgumentException error = Assertions.assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> ZooKeeperAgent.buildClient(connection)
|
||||
);
|
||||
|
||||
Assertions.assertEquals("ZooKeeper TLS is not supported", error.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalizePathStandardizesPublicPaths() {
|
||||
Assertions.assertEquals("/", ZooKeeperAgent.normalizePath(""));
|
||||
Assertions.assertEquals("/", ZooKeeperAgent.normalizePath("/"));
|
||||
Assertions.assertEquals("/app", ZooKeeperAgent.normalizePath("app"));
|
||||
Assertions.assertEquals("/app", ZooKeeperAgent.normalizePath("/app/"));
|
||||
Assertions.assertEquals("/app/name", ZooKeeperAgent.normalizePath("/app/name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void connectAndTestConnectionWorkAgainstTestingServer() throws Exception {
|
||||
try (TestingServer server = new TestingServer()) {
|
||||
JsonObject connect = result(request(
|
||||
1,
|
||||
"connect",
|
||||
"{\"connection\":{\"connect_string\":\"" + server.getConnectString() + "\"}}"
|
||||
));
|
||||
Assertions.assertTrue(connect.get("ok").getAsBoolean());
|
||||
|
||||
JsonObject test = result(request(
|
||||
2,
|
||||
"test_connection",
|
||||
"{\"connection\":{\"connect_string\":\"" + server.getConnectString() + "\"}}"
|
||||
));
|
||||
Assertions.assertTrue(test.get("ok").getAsBoolean());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testConnectionDoesNotReplaceActiveClient() throws Exception {
|
||||
try (TestingServer active = new TestingServer(); TestingServer probe = new TestingServer()) {
|
||||
result(request(1, "connect", "{\"connection\":{\"connect_string\":\"" + active.getConnectString() + "\"}}"));
|
||||
result(request(
|
||||
2,
|
||||
"kv_put",
|
||||
"{\"key\":\"/active\",\"value\":{\"encoding\":\"utf8\",\"data\":\"still-here\"}}"
|
||||
));
|
||||
|
||||
result(request(3, "test_connection", "{\"connection\":{\"connect_string\":\"" + probe.getConnectString() + "\"}}"));
|
||||
|
||||
JsonObject get = result(request(4, "kv_get", "{\"key\":\"/active\"}"));
|
||||
Assertions.assertTrue(get.get("found").getAsBoolean());
|
||||
Assertions.assertEquals("still-here", get.getAsJsonObject("value").get("data").getAsString());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void kvPutCreatesMissingParentsAndGetReadsUtf8Value() throws Exception {
|
||||
try (TestingServer server = new TestingServer()) {
|
||||
connect(server);
|
||||
|
||||
JsonObject put = result(request(
|
||||
2,
|
||||
"kv_put",
|
||||
"{\"key\":\"/app/name\",\"value\":{\"encoding\":\"utf8\",\"data\":\"dbx\"}}"
|
||||
));
|
||||
Assertions.assertTrue(put.has("version"));
|
||||
|
||||
JsonObject get = result(request(3, "kv_get", "{\"key\":\"/app/name\"}"));
|
||||
|
||||
Assertions.assertTrue(get.get("found").getAsBoolean());
|
||||
Assertions.assertEquals("utf8", get.getAsJsonObject("value").get("encoding").getAsString());
|
||||
Assertions.assertEquals("dbx", get.getAsJsonObject("value").get("data").getAsString());
|
||||
JsonObject metadata = get.getAsJsonObject("metadata");
|
||||
Assertions.assertEquals(0, metadata.get("numChildren").getAsInt());
|
||||
Assertions.assertEquals(metadata.get("czxid").getAsLong(), metadata.get("createRevision").getAsLong());
|
||||
Assertions.assertEquals(metadata.get("mzxid").getAsLong(), metadata.get("modRevision").getAsLong());
|
||||
Assertions.assertEquals(metadata.get("dataLength").getAsInt(), metadata.get("valueSize").getAsInt());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void kvPutUpdatesExistingZnode() throws Exception {
|
||||
try (TestingServer server = new TestingServer()) {
|
||||
connect(server);
|
||||
|
||||
result(request(2, "kv_put", "{\"key\":\"/app/name\",\"value\":{\"encoding\":\"utf8\",\"data\":\"dbx\"}}"));
|
||||
result(request(
|
||||
3,
|
||||
"kv_put",
|
||||
"{\"key\":\"/app/name\",\"value\":{\"encoding\":\"utf8\",\"data\":\"dbx-updated\"}}"
|
||||
));
|
||||
|
||||
JsonObject value = result(request(4, "kv_get", "{\"key\":\"/app/name\"}")).getAsJsonObject("value");
|
||||
Assertions.assertEquals("dbx-updated", value.get("data").getAsString());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void kvGetReturnsBase64ForBinaryData() throws Exception {
|
||||
try (TestingServer server = new TestingServer()) {
|
||||
connect(server);
|
||||
|
||||
result(request(2, "kv_put", "{\"key\":\"/bin\",\"value\":{\"encoding\":\"base64\",\"data\":\"/wAB\"}}"));
|
||||
|
||||
JsonObject value = result(request(3, "kv_get", "{\"key\":\"/bin\"}")).getAsJsonObject("value");
|
||||
Assertions.assertEquals("base64", value.get("encoding").getAsString());
|
||||
Assertions.assertEquals("/wAB", value.get("data").getAsString());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void kvPutRejectsRootZnode() throws Exception {
|
||||
try (TestingServer server = new TestingServer()) {
|
||||
connect(server);
|
||||
|
||||
JsonObject error = error(request(
|
||||
2,
|
||||
"kv_put",
|
||||
"{\"key\":\"/\",\"value\":{\"encoding\":\"utf8\",\"data\":\"dbx\"}}"
|
||||
));
|
||||
|
||||
Assertions.assertEquals("Root znode cannot be modified", error.get("message").getAsString());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void kvDeleteReturnsZeroForMissingZnode() throws Exception {
|
||||
try (TestingServer server = new TestingServer()) {
|
||||
connect(server);
|
||||
|
||||
JsonObject delete = result(request(4, "kv_delete", "{\"key\":\"/missing\"}"));
|
||||
|
||||
Assertions.assertEquals(0, delete.get("deleted").getAsInt());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void kvDeleteRemovesLeafZnode() throws Exception {
|
||||
try (TestingServer server = new TestingServer()) {
|
||||
connect(server);
|
||||
result(request(2, "kv_put", "{\"key\":\"/leaf\",\"value\":{\"encoding\":\"utf8\",\"data\":\"dbx\"}}"));
|
||||
|
||||
JsonObject delete = result(request(3, "kv_delete", "{\"key\":\"/leaf\"}"));
|
||||
JsonObject get = result(request(4, "kv_get", "{\"key\":\"/leaf\"}"));
|
||||
|
||||
Assertions.assertEquals(1, delete.get("deleted").getAsInt());
|
||||
Assertions.assertFalse(get.get("found").getAsBoolean());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void kvDeleteRejectsNonRecursiveDeleteOfNonEmptyZnode() throws Exception {
|
||||
try (TestingServer server = new TestingServer()) {
|
||||
connect(server);
|
||||
result(request(2, "kv_put", "{\"key\":\"/app/name\",\"value\":{\"encoding\":\"utf8\",\"data\":\"dbx\"}}"));
|
||||
|
||||
JsonObject error = error(request(3, "kv_delete", "{\"key\":\"/app\"}"));
|
||||
|
||||
Assertions.assertTrue(error.get("message").getAsString().contains("not empty"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void kvDeleteRecursiveDeletesFullSubtreeAndCountsZnodes() throws Exception {
|
||||
try (TestingServer server = new TestingServer()) {
|
||||
connect(server);
|
||||
result(request(2, "kv_put", "{\"key\":\"/app/name\",\"value\":{\"encoding\":\"utf8\",\"data\":\"dbx\"}}"));
|
||||
result(request(3, "kv_put", "{\"key\":\"/app/config\",\"value\":{\"encoding\":\"utf8\",\"data\":\"cfg\"}}"));
|
||||
|
||||
JsonObject delete = result(request(4, "kv_delete", "{\"key\":\"/app\",\"recursive\":true}"));
|
||||
JsonObject get = result(request(5, "kv_get", "{\"key\":\"/app/name\"}"));
|
||||
|
||||
Assertions.assertEquals(3, delete.get("deleted").getAsInt());
|
||||
Assertions.assertFalse(get.get("found").getAsBoolean());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void kvDeleteRejectsRootZnode() throws Exception {
|
||||
try (TestingServer server = new TestingServer()) {
|
||||
connect(server);
|
||||
|
||||
JsonObject error = error(request(2, "kv_delete", "{\"key\":\"/\",\"recursive\":true}"));
|
||||
|
||||
Assertions.assertEquals("Root znode cannot be deleted", error.get("message").getAsString());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPrefixListsDirectChildrenOfRootWhenRecursiveIsFalse() throws Exception {
|
||||
try (TestingServer server = new TestingServer()) {
|
||||
connect(server);
|
||||
result(request(2, "kv_put", "{\"key\":\"/app/name\",\"value\":{\"encoding\":\"utf8\",\"data\":\"dbx\"}}"));
|
||||
result(request(3, "kv_put", "{\"key\":\"/config\",\"value\":{\"encoding\":\"utf8\",\"data\":\"cfg\"}}"));
|
||||
|
||||
JsonObject list = result(request(5, "kv_list_prefix", "{\"prefix\":\"/\",\"recursive\":false}"));
|
||||
|
||||
Assertions.assertEquals(List.of("/app", "/config"), listedKeys(list));
|
||||
Assertions.assertTrue(list.get("continuation").isJsonNull());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPrefixDefaultsToRecursiveForDbxKvBrowser() throws Exception {
|
||||
try (TestingServer server = new TestingServer()) {
|
||||
connect(server);
|
||||
result(request(2, "kv_put", "{\"key\":\"/app/name\",\"value\":{\"encoding\":\"utf8\",\"data\":\"dbx\"}}"));
|
||||
result(request(3, "kv_put", "{\"key\":\"/config\",\"value\":{\"encoding\":\"utf8\",\"data\":\"cfg\"}}"));
|
||||
|
||||
JsonObject list = result(request(5, "kv_list_prefix", "{\"prefix\":\"/\"}"));
|
||||
|
||||
Assertions.assertEquals(List.of("/app", "/app/name", "/config"), listedKeys(list));
|
||||
Assertions.assertTrue(list.get("continuation").isJsonNull());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPrefixListsRecursiveDescendantsOfRoot() throws Exception {
|
||||
try (TestingServer server = new TestingServer()) {
|
||||
connect(server);
|
||||
result(request(2, "kv_put", "{\"key\":\"/app/name\",\"value\":{\"encoding\":\"utf8\",\"data\":\"dbx\"}}"));
|
||||
result(request(3, "kv_put", "{\"key\":\"/config\",\"value\":{\"encoding\":\"utf8\",\"data\":\"cfg\"}}"));
|
||||
|
||||
JsonObject list = result(request(6, "kv_list_prefix", "{\"prefix\":\"/\",\"recursive\":true}"));
|
||||
|
||||
Assertions.assertEquals(List.of("/app", "/app/name", "/config"), listedKeys(list));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPrefixReturnsEmptyResultForMissingPrefix() throws Exception {
|
||||
try (TestingServer server = new TestingServer()) {
|
||||
connect(server);
|
||||
|
||||
JsonObject list = result(request(7, "kv_list_prefix", "{\"prefix\":\"/missing\"}"));
|
||||
|
||||
Assertions.assertEquals(List.of(), listedKeys(list));
|
||||
Assertions.assertTrue(list.get("continuation").isJsonNull());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPrefixPaginatesBySortedPath() throws Exception {
|
||||
try (TestingServer server = new TestingServer()) {
|
||||
connect(server);
|
||||
result(request(2, "kv_put", "{\"key\":\"/c\",\"value\":{\"encoding\":\"utf8\",\"data\":\"c\"}}"));
|
||||
result(request(3, "kv_put", "{\"key\":\"/a\",\"value\":{\"encoding\":\"utf8\",\"data\":\"a\"}}"));
|
||||
result(request(4, "kv_put", "{\"key\":\"/b\",\"value\":{\"encoding\":\"utf8\",\"data\":\"b\"}}"));
|
||||
|
||||
JsonObject first = result(request(5, "kv_list_prefix", "{\"prefix\":\"/\",\"limit\":2}"));
|
||||
String continuation = first.get("continuation").getAsString();
|
||||
JsonObject second = result(request(
|
||||
6,
|
||||
"kv_list_prefix",
|
||||
"{\"prefix\":\"/\",\"limit\":2,\"continuation\":\"" + continuation + "\"}"
|
||||
));
|
||||
|
||||
Assertions.assertEquals(List.of("/a", "/b"), listedKeys(first));
|
||||
Assertions.assertFalse(continuation.isBlank());
|
||||
Assertions.assertEquals(List.of("/c"), listedKeys(second));
|
||||
Assertions.assertTrue(second.get("continuation").isJsonNull());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void listPrefixRejectsContinuationForDifferentRequest() throws Exception {
|
||||
try (TestingServer server = new TestingServer()) {
|
||||
connect(server);
|
||||
result(request(2, "kv_put", "{\"key\":\"/app/a\",\"value\":{\"encoding\":\"utf8\",\"data\":\"a\"}}"));
|
||||
result(request(3, "kv_put", "{\"key\":\"/app/b\",\"value\":{\"encoding\":\"utf8\",\"data\":\"b\"}}"));
|
||||
|
||||
JsonObject first = result(request(4, "kv_list_prefix", "{\"prefix\":\"/app\",\"limit\":1}"));
|
||||
String continuation = first.get("continuation").getAsString();
|
||||
JsonObject error = error(request(
|
||||
5,
|
||||
"kv_list_prefix",
|
||||
"{\"prefix\":\"/\",\"limit\":1,\"continuation\":\"" + continuation + "\"}"
|
||||
));
|
||||
|
||||
Assertions.assertEquals("Continuation does not match request", error.get("message").getAsString());
|
||||
}
|
||||
}
|
||||
|
||||
private static JsonObject connect(TestingServer server) {
|
||||
return result(request(1, "connect", "{\"connection\":{\"connect_string\":\"" + server.getConnectString() + "\"}}"));
|
||||
}
|
||||
|
||||
private static String request(int id, String method, String params) {
|
||||
return ZooKeeperAgent.handleRequest(
|
||||
"{\"jsonrpc\":\"2.0\",\"id\":" + id + ",\"method\":\"" + method + "\",\"params\":" + params + "}"
|
||||
);
|
||||
}
|
||||
|
||||
private static JsonObject result(String response) {
|
||||
return JsonParser.parseString(response).getAsJsonObject().getAsJsonObject("result");
|
||||
}
|
||||
|
||||
private static JsonObject error(String response) {
|
||||
return JsonParser.parseString(response).getAsJsonObject().getAsJsonObject("error");
|
||||
}
|
||||
|
||||
private static List<String> listedKeys(JsonObject listResult) {
|
||||
List<String> keys = new ArrayList<>();
|
||||
JsonArray rows = listResult.getAsJsonArray("keys");
|
||||
for (JsonElement row : rows) {
|
||||
keys.add(row.getAsJsonObject().get("key").getAsString());
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user