feat(etcd): add cluster operations and access control
This commit is contained in:
55 files changed
+7502
-750
No files matched your search
@@ -70,6 +70,29 @@ public final class AgentProtocol {
|
||||
public static final String KV_METHOD_RENAME = "kv_rename";
|
||||
public static final String KV_METHOD_HISTORY = "kv_history";
|
||||
public static final String KV_METHOD_STATUS = "kv_status";
|
||||
public static final String ETCD_METHOD_COMPACT = "etcd_compact";
|
||||
public static final String ETCD_METHOD_DEFRAG = "etcd_defrag";
|
||||
public static final String ETCD_METHOD_WATCH_START = "etcd_watch_start";
|
||||
public static final String ETCD_METHOD_WATCH_POLL = "etcd_watch_poll";
|
||||
public static final String ETCD_METHOD_WATCH_STOP = "etcd_watch_stop";
|
||||
public static final String ETCD_METHOD_LEASE_LIST = "etcd_lease_list";
|
||||
public static final String ETCD_METHOD_LEASE_GET = "etcd_lease_get";
|
||||
public static final String ETCD_METHOD_LEASE_GRANT = "etcd_lease_grant";
|
||||
public static final String ETCD_METHOD_LEASE_KEEPALIVE = "etcd_lease_keepalive_once";
|
||||
public static final String ETCD_METHOD_LEASE_REVOKE = "etcd_lease_revoke";
|
||||
public static final String ETCD_METHOD_AUTH_USER_LIST = "etcd_auth_user_list";
|
||||
public static final String ETCD_METHOD_AUTH_USER_GET = "etcd_auth_user_get";
|
||||
public static final String ETCD_METHOD_AUTH_USER_ADD = "etcd_auth_user_add";
|
||||
public static final String ETCD_METHOD_AUTH_USER_DELETE = "etcd_auth_user_delete";
|
||||
public static final String ETCD_METHOD_AUTH_USER_CHANGE_PASSWORD = "etcd_auth_user_change_password";
|
||||
public static final String ETCD_METHOD_AUTH_USER_GRANT_ROLE = "etcd_auth_user_grant_role";
|
||||
public static final String ETCD_METHOD_AUTH_USER_REVOKE_ROLE = "etcd_auth_user_revoke_role";
|
||||
public static final String ETCD_METHOD_AUTH_ROLE_LIST = "etcd_auth_role_list";
|
||||
public static final String ETCD_METHOD_AUTH_ROLE_GET = "etcd_auth_role_get";
|
||||
public static final String ETCD_METHOD_AUTH_ROLE_ADD = "etcd_auth_role_add";
|
||||
public static final String ETCD_METHOD_AUTH_ROLE_DELETE = "etcd_auth_role_delete";
|
||||
public static final String ETCD_METHOD_AUTH_ROLE_GRANT_PERMISSION = "etcd_auth_role_grant_permission";
|
||||
public static final String ETCD_METHOD_AUTH_ROLE_REVOKE_PERMISSION = "etcd_auth_role_revoke_permission";
|
||||
|
||||
public static final String CAPABILITY_CONNECT = "connect";
|
||||
public static final String CAPABILITY_TEST_CONNECTION = "test_connection";
|
||||
@@ -84,6 +107,11 @@ public final class AgentProtocol {
|
||||
public static final String CAPABILITY_KV_LIST_VALUES = "kv_list_values";
|
||||
public static final String CAPABILITY_KV_STATUS = "kv_status";
|
||||
public static final String CAPABILITY_KV_HISTORY = "kv_history";
|
||||
public static final String CAPABILITY_ETCD_COMPACTION = "etcd_compaction";
|
||||
public static final String CAPABILITY_ETCD_DEFRAG = "etcd_defrag";
|
||||
public static final String CAPABILITY_ETCD_WATCH = "etcd_watch";
|
||||
public static final String CAPABILITY_ETCD_LEASE = "etcd_lease";
|
||||
public static final String CAPABILITY_ETCD_AUTH = "etcd_auth";
|
||||
public static final String CAPABILITY_MULTI_SESSION = "multi_session";
|
||||
|
||||
public static final List<String> CAPABILITIES = Collections.unmodifiableList(Arrays.asList(
|
||||
@@ -109,9 +137,17 @@ public final class AgentProtocol {
|
||||
CAPABILITY_KV_CAS,
|
||||
CAPABILITY_KV_LIST_VALUES,
|
||||
CAPABILITY_KV_STATUS,
|
||||
CAPABILITY_KV_HISTORY
|
||||
CAPABILITY_KV_HISTORY,
|
||||
CAPABILITY_ETCD_COMPACTION,
|
||||
CAPABILITY_ETCD_DEFRAG,
|
||||
CAPABILITY_ETCD_WATCH,
|
||||
CAPABILITY_ETCD_LEASE,
|
||||
CAPABILITY_ETCD_AUTH
|
||||
));
|
||||
|
||||
public static final List<String> MULTI_SESSION_CAPABILITIES;
|
||||
public static final List<String> MULTI_SESSION_ALL_CAPABILITIES;
|
||||
|
||||
public static final List<String> COMMON_METHODS = Collections.unmodifiableList(Arrays.asList(
|
||||
METHOD_HANDSHAKE,
|
||||
METHOD_CONNECT,
|
||||
@@ -150,6 +186,14 @@ public final class AgentProtocol {
|
||||
public static final List<String> MULTI_SESSION_METHODS;
|
||||
|
||||
static {
|
||||
List<String> capabilities = new java.util.ArrayList<>(CAPABILITIES);
|
||||
capabilities.add(CAPABILITY_MULTI_SESSION);
|
||||
MULTI_SESSION_CAPABILITIES = Collections.unmodifiableList(capabilities);
|
||||
|
||||
List<String> allCapabilities = new java.util.ArrayList<>(ALL_CAPABILITIES);
|
||||
allCapabilities.add(CAPABILITY_MULTI_SESSION);
|
||||
MULTI_SESSION_ALL_CAPABILITIES = Collections.unmodifiableList(allCapabilities);
|
||||
|
||||
List<String> methods = new java.util.ArrayList<>(COMMON_METHODS);
|
||||
int insertAt = methods.indexOf(METHOD_CONNECT) + 1;
|
||||
methods.addAll(insertAt, Arrays.asList(
|
||||
@@ -185,7 +229,30 @@ public final class AgentProtocol {
|
||||
KV_METHOD_DELETE,
|
||||
KV_METHOD_RENAME,
|
||||
KV_METHOD_HISTORY,
|
||||
KV_METHOD_STATUS
|
||||
KV_METHOD_STATUS,
|
||||
ETCD_METHOD_COMPACT,
|
||||
ETCD_METHOD_DEFRAG,
|
||||
ETCD_METHOD_WATCH_START,
|
||||
ETCD_METHOD_WATCH_POLL,
|
||||
ETCD_METHOD_WATCH_STOP,
|
||||
ETCD_METHOD_LEASE_LIST,
|
||||
ETCD_METHOD_LEASE_GET,
|
||||
ETCD_METHOD_LEASE_GRANT,
|
||||
ETCD_METHOD_LEASE_KEEPALIVE,
|
||||
ETCD_METHOD_LEASE_REVOKE,
|
||||
ETCD_METHOD_AUTH_USER_LIST,
|
||||
ETCD_METHOD_AUTH_USER_GET,
|
||||
ETCD_METHOD_AUTH_USER_ADD,
|
||||
ETCD_METHOD_AUTH_USER_DELETE,
|
||||
ETCD_METHOD_AUTH_USER_CHANGE_PASSWORD,
|
||||
ETCD_METHOD_AUTH_USER_GRANT_ROLE,
|
||||
ETCD_METHOD_AUTH_USER_REVOKE_ROLE,
|
||||
ETCD_METHOD_AUTH_ROLE_LIST,
|
||||
ETCD_METHOD_AUTH_ROLE_GET,
|
||||
ETCD_METHOD_AUTH_ROLE_ADD,
|
||||
ETCD_METHOD_AUTH_ROLE_DELETE,
|
||||
ETCD_METHOD_AUTH_ROLE_GRANT_PERMISSION,
|
||||
ETCD_METHOD_AUTH_ROLE_REVOKE_PERMISSION
|
||||
));
|
||||
|
||||
private AgentProtocol() {
|
||||
@@ -196,9 +263,11 @@ public final class AgentProtocol {
|
||||
}
|
||||
|
||||
public static HandshakeResult multiSessionHandshakeResult() {
|
||||
List<String> capabilities = new java.util.ArrayList<>(CAPABILITIES);
|
||||
capabilities.add(CAPABILITY_MULTI_SESSION);
|
||||
return new HandshakeResult(MULTI_SESSION_PROTOCOL_VERSION, MULTI_SESSION_PROTOCOL_VERSION, capabilities);
|
||||
return new HandshakeResult(
|
||||
MULTI_SESSION_PROTOCOL_VERSION,
|
||||
MULTI_SESSION_PROTOCOL_VERSION,
|
||||
MULTI_SESSION_CAPABILITIES
|
||||
);
|
||||
}
|
||||
|
||||
public static final class HandshakeResult {
|
||||
|
||||
@@ -26,6 +26,7 @@ public final class MultiSessionJsonRpcServer implements AutoCloseable {
|
||||
private static final long MAINTENANCE_INTERVAL_MILLIS = 60_000L;
|
||||
|
||||
private final Supplier<? extends DatabaseAgent> agentFactory;
|
||||
private final Supplier<? extends SessionRpcHandler> sessionHandlerFactory;
|
||||
private final Map<String, Session> sessions = new ConcurrentHashMap<>();
|
||||
private final ExecutorService requests = Executors.newCachedThreadPool();
|
||||
private final JdbcConnectionPoolRegistry poolRegistry;
|
||||
@@ -52,9 +53,21 @@ public final class MultiSessionJsonRpcServer implements AutoCloseable {
|
||||
JdbcConnectionPoolRegistry poolRegistry
|
||||
) {
|
||||
this.agentFactory = agentFactory;
|
||||
this.sessionHandlerFactory = null;
|
||||
this.poolRegistry = poolRegistry;
|
||||
}
|
||||
|
||||
private MultiSessionJsonRpcServer(Supplier<? extends SessionRpcHandler> sessionHandlerFactory, boolean customHandler) {
|
||||
this.agentFactory = null;
|
||||
this.sessionHandlerFactory = sessionHandlerFactory;
|
||||
this.poolRegistry = new JdbcConnectionPoolRegistry();
|
||||
}
|
||||
|
||||
/** Creates a protocol v2 server for a non-JDBC, session-scoped agent. */
|
||||
public static MultiSessionJsonRpcServer forSessionHandlers(Supplier<? extends SessionRpcHandler> sessionHandlerFactory) {
|
||||
return new MultiSessionJsonRpcServer(sessionHandlerFactory, true);
|
||||
}
|
||||
|
||||
public void run() {
|
||||
synchronized (outputLock) {
|
||||
protocolOutput.println("{\"ready\":true}");
|
||||
@@ -94,7 +107,7 @@ public final class MultiSessionJsonRpcServer implements AutoCloseable {
|
||||
try {
|
||||
Object result;
|
||||
if (AgentProtocol.METHOD_HANDSHAKE.equals(method)) {
|
||||
result = AgentProtocol.multiSessionHandshakeResult();
|
||||
result = sessionHandlerFactory == null ? AgentProtocol.multiSessionHandshakeResult() : customHandshake();
|
||||
} else if (AgentProtocol.METHOD_OPEN_SESSION.equals(method)) {
|
||||
result = openSession(requiredSessionId(params), params);
|
||||
} else if (AgentProtocol.METHOD_CLOSE_SESSION.equals(method)) {
|
||||
@@ -105,7 +118,9 @@ public final class MultiSessionJsonRpcServer implements AutoCloseable {
|
||||
session(requiredSessionId(params)).cancel();
|
||||
result = Collections.singletonMap("ok", true);
|
||||
} else if (AgentProtocol.METHOD_TEST_CONNECTION.equals(method)) {
|
||||
result = new JsonRpcServer(agentFactory.get()).dispatchForRuntime(method, params);
|
||||
result = sessionHandlerFactory == null
|
||||
? new JsonRpcServer(agentFactory.get()).dispatchForRuntime(method, params)
|
||||
: testSession(params);
|
||||
} else if (AgentProtocol.METHOD_CONNECT.equals(method)) {
|
||||
closeSession(LEGACY_SESSION_ID);
|
||||
result = openSession(LEGACY_SESSION_ID, params);
|
||||
@@ -131,18 +146,23 @@ public final class MultiSessionJsonRpcServer implements AutoCloseable {
|
||||
if (sessions.size() >= MAX_SESSIONS && !sessions.containsKey(sessionId)) {
|
||||
throw new IllegalStateException("Agent session limit reached: " + MAX_SESSIONS);
|
||||
}
|
||||
DatabaseAgent agent = agentFactory.get();
|
||||
if (poolRegistry.isEnabled() && agent instanceof AbstractJdbcAgent jdbcAgent) {
|
||||
jdbcAgent.attachConnectionPoolRegistry(poolRegistry);
|
||||
ensureMaintenanceStarted();
|
||||
Session session;
|
||||
if (sessionHandlerFactory != null) {
|
||||
session = new Session(sessionHandlerFactory.get());
|
||||
} else {
|
||||
DatabaseAgent agent = agentFactory.get();
|
||||
if (poolRegistry.isEnabled() && agent instanceof AbstractJdbcAgent jdbcAgent) {
|
||||
jdbcAgent.attachConnectionPoolRegistry(poolRegistry);
|
||||
ensureMaintenanceStarted();
|
||||
}
|
||||
session = new Session(new JsonRpcServer(agent));
|
||||
}
|
||||
Session session = new Session(new JsonRpcServer(agent));
|
||||
Session existing = sessions.putIfAbsent(sessionId, session);
|
||||
if (existing != null) {
|
||||
throw new IllegalStateException("Agent session already exists: " + sessionId);
|
||||
}
|
||||
try {
|
||||
return session.handle(AgentProtocol.METHOD_CONNECT, params);
|
||||
return session.connect(params);
|
||||
} catch (Exception error) {
|
||||
sessions.remove(sessionId, session);
|
||||
session.close();
|
||||
@@ -158,6 +178,24 @@ public final class MultiSessionJsonRpcServer implements AutoCloseable {
|
||||
return Collections.singletonMap("ok", true);
|
||||
}
|
||||
|
||||
private Object testSession(JsonObject params) throws Exception {
|
||||
Session session = new Session(sessionHandlerFactory.get());
|
||||
try {
|
||||
return session.connect(params);
|
||||
} finally {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
|
||||
private Object customHandshake() {
|
||||
SessionRpcHandler handler = sessionHandlerFactory.get();
|
||||
try {
|
||||
return handler.handshake();
|
||||
} finally {
|
||||
handler.close();
|
||||
}
|
||||
}
|
||||
|
||||
private Session session(String sessionId) {
|
||||
Session session = sessions.get(sessionId);
|
||||
if (session == null) {
|
||||
@@ -253,16 +291,34 @@ public final class MultiSessionJsonRpcServer implements AutoCloseable {
|
||||
|
||||
private static final class Session {
|
||||
private final JsonRpcServer server;
|
||||
private final SessionRpcHandler handler;
|
||||
private final ReentrantLock lock = new ReentrantLock();
|
||||
|
||||
private Session(JsonRpcServer server) {
|
||||
this.server = server;
|
||||
this.handler = null;
|
||||
}
|
||||
|
||||
private Session(SessionRpcHandler handler) {
|
||||
this.server = null;
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
private Object handle(String method, JsonObject params) throws Exception {
|
||||
lock.lock();
|
||||
try {
|
||||
return server.dispatchForRuntime(method, params);
|
||||
return handler == null ? server.dispatchForRuntime(method, params) : handler.handle(method, params);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
private Object connect(JsonObject params) throws Exception {
|
||||
lock.lock();
|
||||
try {
|
||||
return handler == null
|
||||
? server.dispatchForRuntime(AgentProtocol.METHOD_CONNECT, params)
|
||||
: handler.connect(params);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
@@ -271,7 +327,11 @@ public final class MultiSessionJsonRpcServer implements AutoCloseable {
|
||||
private void close() {
|
||||
lock.lock();
|
||||
try {
|
||||
server.dispatchForRuntime(AgentProtocol.METHOD_DISCONNECT, new JsonObject());
|
||||
if (handler != null) {
|
||||
handler.close();
|
||||
} else {
|
||||
server.dispatchForRuntime(AgentProtocol.METHOD_DISCONNECT, new JsonObject());
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
} finally {
|
||||
lock.unlock();
|
||||
@@ -279,10 +339,17 @@ public final class MultiSessionJsonRpcServer implements AutoCloseable {
|
||||
}
|
||||
|
||||
private void cancel() {
|
||||
server.cancelActiveStatements();
|
||||
if (handler == null) {
|
||||
server.cancelActiveStatements();
|
||||
} else {
|
||||
handler.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
private void expireIdleResources() {
|
||||
if (handler != null) {
|
||||
return;
|
||||
}
|
||||
if (!lock.tryLock()) {
|
||||
return;
|
||||
}
|
||||
@@ -294,6 +361,9 @@ public final class MultiSessionJsonRpcServer implements AutoCloseable {
|
||||
}
|
||||
|
||||
private void expireIdleResources(long nowMillis, long idleTimeoutMillis) {
|
||||
if (handler != null) {
|
||||
return;
|
||||
}
|
||||
if (!lock.tryLock()) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.dbx.agent;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
/**
|
||||
* Per-session handler for protocol v2 agents that do not implement the JDBC
|
||||
* {@link DatabaseAgent} contract. Implementations own every resource created
|
||||
* for a logical DBX connection and must release it from {@link #close()}.
|
||||
*/
|
||||
public interface SessionRpcHandler {
|
||||
default Object handshake() {
|
||||
return AgentProtocol.multiSessionHandshakeResult();
|
||||
}
|
||||
|
||||
Object connect(JsonObject params) throws Exception;
|
||||
|
||||
Object handle(String method, JsonObject params) throws Exception;
|
||||
|
||||
default void cancel() {
|
||||
}
|
||||
|
||||
void close();
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
"protocolVersion": 1,
|
||||
"handshakeMethod": "handshake",
|
||||
"handshakeResponseFields": ["protocolVersion", "agentProtocolVersion", "capabilities"],
|
||||
"allCapabilities": ["connect", "test_connection", "metadata", "query", "paged_query", "transaction", "ddl", "kv", "kv_ttl", "kv_cas", "kv_list_values", "kv_status", "kv_history"],
|
||||
"allCapabilities": ["connect", "test_connection", "metadata", "query", "paged_query", "transaction", "ddl", "kv", "kv_ttl", "kv_cas", "kv_list_values", "kv_status", "kv_history", "etcd_compaction", "etcd_defrag", "etcd_watch", "etcd_lease", "etcd_auth"],
|
||||
"capabilities": ["connect", "test_connection", "metadata", "query", "paged_query", "transaction", "ddl"],
|
||||
"defaultSqlCapabilities": ["connect", "test_connection", "metadata", "query", "paged_query", "transaction", "ddl"],
|
||||
"commonMethods": [
|
||||
@@ -40,5 +40,5 @@
|
||||
"shutdown"
|
||||
],
|
||||
"mongoLegacyMethods": ["list_databases", "list_collections", "find_documents", "find_documents_extended_json", "count_documents", "server_version", "create_index", "drop_indexes", "drop_collection", "insert_document", "update_document", "update_documents", "delete_document", "delete_documents"],
|
||||
"kvMethods": ["kv_list_prefix", "kv_get", "kv_put", "kv_delete", "kv_rename", "kv_history", "kv_status"]
|
||||
"kvMethods": ["kv_list_prefix", "kv_get", "kv_put", "kv_delete", "kv_rename", "kv_history", "kv_status", "etcd_compact", "etcd_defrag", "etcd_watch_start", "etcd_watch_poll", "etcd_watch_stop", "etcd_lease_list", "etcd_lease_get", "etcd_lease_grant", "etcd_lease_keepalive_once", "etcd_lease_revoke", "etcd_auth_user_list", "etcd_auth_user_get", "etcd_auth_user_add", "etcd_auth_user_delete", "etcd_auth_user_change_password", "etcd_auth_user_grant_role", "etcd_auth_user_revoke_role", "etcd_auth_role_list", "etcd_auth_role_get", "etcd_auth_role_add", "etcd_auth_role_delete", "etcd_auth_role_grant_permission", "etcd_auth_role_revoke_permission"]
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
"protocolVersion": 2,
|
||||
"handshakeMethod": "handshake",
|
||||
"handshakeResponseFields": ["protocolVersion", "agentProtocolVersion", "capabilities"],
|
||||
"allCapabilities": ["connect", "test_connection", "metadata", "query", "paged_query", "transaction", "ddl", "kv", "kv_ttl", "kv_cas", "kv_list_values", "kv_status", "kv_history", "multi_session"],
|
||||
"allCapabilities": ["connect", "test_connection", "metadata", "query", "paged_query", "transaction", "ddl", "kv", "kv_ttl", "kv_cas", "kv_list_values", "kv_status", "kv_history", "etcd_compaction", "etcd_defrag", "etcd_watch", "etcd_lease", "etcd_auth", "multi_session"],
|
||||
"capabilities": ["connect", "test_connection", "metadata", "query", "paged_query", "transaction", "ddl", "multi_session"],
|
||||
"defaultSqlCapabilities": ["connect", "test_connection", "metadata", "query", "paged_query", "transaction", "ddl", "multi_session"],
|
||||
"commonMethods": [
|
||||
@@ -44,7 +44,7 @@
|
||||
"shutdown"
|
||||
],
|
||||
"mongoLegacyMethods": ["list_databases", "list_collections", "find_documents", "find_documents_extended_json", "count_documents", "server_version", "create_index", "drop_indexes", "drop_collection", "insert_document", "update_document", "update_documents", "delete_document", "delete_documents"],
|
||||
"kvMethods": ["kv_list_prefix", "kv_get", "kv_put", "kv_delete", "kv_rename", "kv_history", "kv_status"],
|
||||
"kvMethods": ["kv_list_prefix", "kv_get", "kv_put", "kv_delete", "kv_rename", "kv_history", "kv_status", "etcd_compact", "etcd_defrag", "etcd_watch_start", "etcd_watch_poll", "etcd_watch_stop", "etcd_lease_list", "etcd_lease_get", "etcd_lease_grant", "etcd_lease_keepalive_once", "etcd_lease_revoke", "etcd_auth_user_list", "etcd_auth_user_get", "etcd_auth_user_add", "etcd_auth_user_delete", "etcd_auth_user_change_password", "etcd_auth_user_grant_role", "etcd_auth_user_revoke_role", "etcd_auth_role_list", "etcd_auth_role_get", "etcd_auth_role_add", "etcd_auth_role_delete", "etcd_auth_role_grant_permission", "etcd_auth_role_revoke_permission"],
|
||||
"sessionField": "agentSessionId",
|
||||
"cursorSessionField": "sessionId"
|
||||
}
|
||||
Reference in new issue
Block a user