feat(etcd): add agent-backed etcd support

This commit is contained in:
t8y2 committed 2026-06-08 00:58:42 +08:00
1 parent 7bd8d29a79
commit e6062b2b07
49 files changed
+1524 -15

No files matched your search

+60 -3
View File
@@ -48,10 +48,11 @@ pub enum AgentCapability {
PagedQuery,
Transaction,
Ddl,
Kv,
}
impl AgentCapability {
pub const ALL: [Self; 7] = [
pub const ALL: [Self; 8] = [
Self::Connect,
Self::TestConnection,
Self::Metadata,
@@ -59,6 +60,7 @@ impl AgentCapability {
Self::PagedQuery,
Self::Transaction,
Self::Ddl,
Self::Kv,
];
pub fn as_str(self) -> &'static str {
@@ -70,6 +72,7 @@ impl AgentCapability {
Self::PagedQuery => "paged_query",
Self::Transaction => "transaction",
Self::Ddl => "ddl",
Self::Kv => "kv",
}
}
}
@@ -180,6 +183,27 @@ impl MongoAgentMethod {
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AgentKvMethod {
ListPrefix,
Get,
Put,
Delete,
}
impl AgentKvMethod {
pub const ALL: [Self; 4] = [Self::ListPrefix, Self::Get, Self::Put, Self::Delete];
pub fn as_str(self) -> &'static str {
match self {
Self::ListPrefix => "kv_list_prefix",
Self::Get => "kv_get",
Self::Put => "kv_put",
Self::Delete => "kv_delete",
}
}
}
struct StderrTail {
lines: VecDeque<String>,
capacity: usize,
@@ -542,6 +566,14 @@ impl AgentDriverClient {
self.call(method.as_str(), params).await
}
pub async fn call_kv_method<T: DeserializeOwned + Send + 'static>(
&mut self,
method: AgentKvMethod,
params: Value,
) -> Result<T, String> {
self.call(method.as_str(), params).await
}
pub async fn mongo_list_databases<T: DeserializeOwned + Send + 'static>(&mut self) -> Result<T, String> {
self.call_mongo_method(MongoAgentMethod::ListDatabases, serde_json::json!({})).await
}
@@ -664,6 +696,9 @@ pub fn is_unsupported_handshake_error(error: &str) -> bool {
}
pub fn agent_supports_capability(handshake: Option<&AgentHandshake>, capability: AgentCapability) -> bool {
if capability == AgentCapability::Kv {
return handshake.map(|value| value.supports(capability)).unwrap_or(false);
}
handshake.map(|value| value.supports(capability)).unwrap_or(true)
}
@@ -844,7 +879,7 @@ mod tests {
agent_proxy_env_vars, agent_schema_params, agent_schema_table_params, agent_supports_capability,
agent_transaction_params, format_agent_process_error, is_unsupported_handshake_error, mongo_collection_params,
mongo_database_params, mongo_document_id_params, read_agent_line, AgentCapability, AgentDriverClient,
AgentHandshake, AgentMethod, MongoAgentMethod, StderrTail, AGENT_PROTOCOL_VERSION,
AgentHandshake, AgentKvMethod, AgentMethod, MongoAgentMethod, StderrTail, AGENT_PROTOCOL_VERSION,
};
use std::io::Cursor;
@@ -976,7 +1011,8 @@ mod tests {
assert_eq!(AgentCapability::PagedQuery.as_str(), "paged_query");
assert_eq!(AgentCapability::Transaction.as_str(), "transaction");
assert_eq!(AgentCapability::Ddl.as_str(), "ddl");
assert_eq!(AgentCapability::ALL.len(), 7);
assert_eq!(AgentCapability::Kv.as_str(), "kv");
assert_eq!(AgentCapability::ALL.len(), 8);
}
#[test]
@@ -1013,6 +1049,15 @@ mod tests {
assert_eq!(MongoAgentMethod::DeleteDocument.as_str(), "delete_document");
}
#[test]
fn defines_kv_agent_protocol_methods() {
assert_eq!(AgentKvMethod::ListPrefix.as_str(), "kv_list_prefix");
assert_eq!(AgentKvMethod::Get.as_str(), "kv_get");
assert_eq!(AgentKvMethod::Put.as_str(), "kv_put");
assert_eq!(AgentKvMethod::Delete.as_str(), "kv_delete");
assert_eq!(AgentKvMethod::ALL.len(), 4);
}
#[test]
fn exposes_schema_and_query_protocol_wrappers() {
let _list_databases = AgentDriverClient::list_databases::<serde_json::Value>;
@@ -1042,6 +1087,11 @@ mod tests {
let _mongo_delete_document = AgentDriverClient::mongo_delete_document::<serde_json::Value>;
}
#[test]
fn exposes_kv_protocol_wrapper() {
let _call_kv_method = AgentDriverClient::call_kv_method::<serde_json::Value>;
}
#[test]
fn builds_mongo_agent_request_params() {
assert_eq!(mongo_database_params("app"), serde_json::json!({ "database": "app" }));
@@ -1104,6 +1154,10 @@ mod tests {
string_array(&contract["mongoLegacyMethods"]),
MongoAgentMethod::ALL.iter().map(|method| method.as_str()).collect::<Vec<_>>()
);
assert_eq!(
string_array(&contract["kvMethods"]),
AgentKvMethod::ALL.iter().map(|method| method.as_str()).collect::<Vec<_>>()
);
}
#[test]
@@ -1117,6 +1171,7 @@ mod tests {
assert!(handshake.supports(AgentCapability::Connect));
assert!(handshake.supports(AgentCapability::Metadata));
assert!(!handshake.supports(AgentCapability::Query));
assert!(!handshake.supports(AgentCapability::Kv));
}
#[test]
@@ -1130,6 +1185,8 @@ mod tests {
assert!(agent_supports_capability(None, AgentCapability::Query));
assert!(agent_supports_capability(Some(&handshake), AgentCapability::Connect));
assert!(!agent_supports_capability(Some(&handshake), AgentCapability::Query));
assert!(!agent_supports_capability(None, AgentCapability::Kv));
assert!(!agent_supports_capability(Some(&handshake), AgentCapability::Kv));
}
#[test]