|
| 1 | +/* |
| 2 | +Copyright 2024-2025 The Spice.ai OSS Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +//! JSON-RPC protocol definitions for system adapter communication. |
| 18 | +//! |
| 19 | +//! This crate defines the request/response types for the system adapter |
| 20 | +//! JSON-RPC protocol, which allows spicebench to communicate with external |
| 21 | +//! benchmark execution environments. |
| 22 | +
|
| 23 | +use serde::{Deserialize, Serialize}; |
| 24 | +use std::collections::HashMap; |
| 25 | +use uuid::Uuid; |
| 26 | + |
| 27 | +/// ETL type for data ingestion configuration |
| 28 | +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] |
| 29 | +#[serde(rename_all = "snake_case")] |
| 30 | +pub enum EtlType { |
| 31 | + S3, |
| 32 | +} |
| 33 | + |
| 34 | +/// ADBC driver types supported by the system adapter |
| 35 | +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] |
| 36 | +#[serde(rename_all = "snake_case")] |
| 37 | +pub enum AdbcDriver { |
| 38 | + #[serde(rename = "flightsql")] |
| 39 | + Flightsql, |
| 40 | + #[serde(rename = "databricks")] |
| 41 | + Databricks, |
| 42 | +} |
| 43 | + |
| 44 | +/// Configuration for a single dataset's ETL source |
| 45 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 46 | +pub struct DatasetConfig { |
| 47 | + /// Type of ETL to configure |
| 48 | + pub etl_type: EtlType, |
| 49 | + /// ETL-specific configuration parameters |
| 50 | + pub params: HashMap<String, serde_json::Value>, |
| 51 | +} |
| 52 | + |
| 53 | +/// Request to setup a benchmark run with ETL configuration |
| 54 | +/// |
| 55 | +/// JSON-RPC method: `setup` |
| 56 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 57 | +pub struct SetupRequest { |
| 58 | + /// Unique identifier for this benchmark run |
| 59 | + pub run_id: Uuid, |
| 60 | + /// Map of dataset name to its ETL configuration |
| 61 | + pub datasets: HashMap<String, DatasetConfig>, |
| 62 | +} |
| 63 | + |
| 64 | +/// Response from setup request |
| 65 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 66 | +pub struct SetupResponse { |
| 67 | + /// Indicates if setup was successful |
| 68 | + pub ok: bool, |
| 69 | +} |
| 70 | + |
| 71 | +/// Request to get query method/driver information |
| 72 | +/// |
| 73 | +/// JSON-RPC method: `query_method` |
| 74 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 75 | +pub struct QueryMethodRequest { |
| 76 | + /// Unique identifier for the benchmark run |
| 77 | + pub run_id: Uuid, |
| 78 | +} |
| 79 | + |
| 80 | +/// Response containing database connection information |
| 81 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 82 | +pub struct QueryMethodResponse { |
| 83 | + /// ADBC driver to use for database connections |
| 84 | + pub driver: AdbcDriver, |
| 85 | + /// Driver-specific connection parameters |
| 86 | + pub db_kwargs: HashMap<String, serde_json::Value>, |
| 87 | +} |
| 88 | + |
| 89 | +/// Request to teardown a benchmark run |
| 90 | +/// |
| 91 | +/// JSON-RPC method: `teardown` |
| 92 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 93 | +pub struct TeardownRequest { |
| 94 | + /// Unique identifier for the benchmark run to clean up |
| 95 | + pub run_id: Uuid, |
| 96 | +} |
| 97 | + |
| 98 | +/// Response from teardown request |
| 99 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 100 | +pub struct TeardownResponse { |
| 101 | + /// Indicates if teardown was successful |
| 102 | + pub ok: bool, |
| 103 | +} |
| 104 | + |
| 105 | +/// Standard JSON-RPC 2.0 request envelope |
| 106 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 107 | +pub struct JsonRpcRequest<T> { |
| 108 | + pub jsonrpc: String, |
| 109 | + pub id: serde_json::Value, |
| 110 | + pub method: String, |
| 111 | + pub params: T, |
| 112 | +} |
| 113 | + |
| 114 | +impl<T> JsonRpcRequest<T> { |
| 115 | + /// Create a new JSON-RPC 2.0 request |
| 116 | + pub fn new(id: impl Into<serde_json::Value>, method: impl Into<String>, params: T) -> Self { |
| 117 | + Self { |
| 118 | + jsonrpc: "2.0".to_string(), |
| 119 | + id: id.into(), |
| 120 | + method: method.into(), |
| 121 | + params, |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +/// Standard JSON-RPC 2.0 response envelope |
| 127 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 128 | +pub struct JsonRpcResponse<T> { |
| 129 | + pub jsonrpc: String, |
| 130 | + pub id: serde_json::Value, |
| 131 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 132 | + pub result: Option<T>, |
| 133 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 134 | + pub error: Option<JsonRpcError>, |
| 135 | +} |
| 136 | + |
| 137 | +impl<T> JsonRpcResponse<T> { |
| 138 | + /// Create a successful response |
| 139 | + pub fn success(id: impl Into<serde_json::Value>, result: T) -> Self { |
| 140 | + Self { |
| 141 | + jsonrpc: "2.0".to_string(), |
| 142 | + id: id.into(), |
| 143 | + result: Some(result), |
| 144 | + error: None, |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + /// Create an error response |
| 149 | + pub fn error(id: impl Into<serde_json::Value>, error: JsonRpcError) -> Self { |
| 150 | + Self { |
| 151 | + jsonrpc: "2.0".to_string(), |
| 152 | + id: id.into(), |
| 153 | + result: None, |
| 154 | + error: Some(error), |
| 155 | + } |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +/// JSON-RPC 2.0 error object |
| 160 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 161 | +pub struct JsonRpcError { |
| 162 | + pub code: i32, |
| 163 | + pub message: String, |
| 164 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 165 | + pub data: Option<serde_json::Value>, |
| 166 | +} |
| 167 | + |
| 168 | +impl JsonRpcError { |
| 169 | + /// Create a new error |
| 170 | + pub fn new(code: i32, message: impl Into<String>) -> Self { |
| 171 | + Self { |
| 172 | + code, |
| 173 | + message: message.into(), |
| 174 | + data: None, |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + /// Create an error with additional data |
| 179 | + pub fn with_data( |
| 180 | + code: i32, |
| 181 | + message: impl Into<String>, |
| 182 | + data: impl Into<serde_json::Value>, |
| 183 | + ) -> Self { |
| 184 | + Self { |
| 185 | + code, |
| 186 | + message: message.into(), |
| 187 | + data: Some(data.into()), |
| 188 | + } |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +/// Standard JSON-RPC error codes |
| 193 | +pub mod error_codes { |
| 194 | + pub const PARSE_ERROR: i32 = -32700; |
| 195 | + pub const INVALID_REQUEST: i32 = -32600; |
| 196 | + pub const METHOD_NOT_FOUND: i32 = -32601; |
| 197 | + pub const INVALID_PARAMS: i32 = -32602; |
| 198 | + pub const INTERNAL_ERROR: i32 = -32603; |
| 199 | +} |
| 200 | + |
| 201 | +/// Method names for the system adapter protocol |
| 202 | +pub mod methods { |
| 203 | + pub const SETUP: &str = "setup"; |
| 204 | + pub const QUERY_METHOD: &str = "query_method"; |
| 205 | + pub const TEARDOWN: &str = "teardown"; |
| 206 | + pub const RPC_METHODS: &str = "rpc.methods"; |
| 207 | +} |
0 commit comments