mongreldb-server 0.64.2

HTTP daemon for MongrelDB — serves SQL, native queries, and typed Kit API over HTTP for multi-process access.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! Server-hosted NodeRuntime product path (Stage 2/3):
//! - After `cluster init`, start the app with a live runtime under plaintext
//!   test mode and assert status / SHOW CLUSTER report a live runtime.
//! - TRANSFER LEADER / SPLIT TABLET accept immediately as durable ops jobs
//!   (P1.6: HTTP returns `status: accepted` + `job_id`, not hold-to-complete).
//! - Standalone mode still fails closed for MERGE / MOVE REPLICA when the
//!   cluster runtime is not running.
//! - TRANSFER LEADER against a real single-replica tablet accepts an ops job.

use axum::body::{to_bytes, Body};
use axum::http::{Request, StatusCode};
use mongreldb_cluster::bootstrap::{cluster_init, InitRequest, TrustConfig};
use mongreldb_cluster::meta::{DatabaseDescriptor, DatabaseState, MetaCommand, TableSchemaRecord};
use mongreldb_cluster::node::{Locality, NodeCapacity, NodeIdentity};
use mongreldb_cluster::runtime::NodeRuntime;
use mongreldb_cluster::tablet::{
    ColumnId, ReplicaDescriptor, ReplicaRole, TablePartitioningRecord, TabletDescriptor,
    TabletState,
};
use mongreldb_core::Database;
use mongreldb_log::commit_log::ExecutionControl;
use mongreldb_server::cluster_runtime::{ClusterRuntimeHandle, ClusterRuntimeOptions};
use mongreldb_server::{build_app, build_app_full, build_app_with_storage, ServerStorageRuntime};
use mongreldb_types::hlc::HlcTimestamp;
use mongreldb_types::ids::{
    DatabaseId, MetadataVersion, NodeId, RaftGroupId, SchemaVersion, TableId, TabletId,
};
use serde_json::{json, Value};
use std::path::Path;
use std::sync::Arc;
use std::time::Duration;
use tempfile::tempdir;
use tower::ServiceExt;

const CA_PEM: &str = "-----BEGIN CERTIFICATE-----\nY2E=\n-----END CERTIFICATE-----\n";
const CERT_PEM: &str = "-----BEGIN CERTIFICATE-----\nbm9kZQ==\n-----END CERTIFICATE-----\n";
const KEY_PEM: &str = "-----BEGIN PRIVATE KEY-----\nc2VjcmV0\n-----END PRIVATE KEY-----\n";
const LEADER_TIMEOUT: Duration = Duration::from_secs(15);

fn free_addr() -> String {
    let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
    listener.local_addr().unwrap().to_string()
}

fn bootstrap_cluster(data: &Path, rpc: &str) -> NodeIdentity {
    let mut counter = 0u64;
    let mut csprng = |buf: &mut [u8]| {
        for chunk in buf.chunks_mut(8) {
            counter += 1;
            let bytes = counter.to_le_bytes();
            chunk.copy_from_slice(&bytes[..chunk.len()]);
        }
        Ok(())
    };
    let identity = NodeIdentity::load_or_create(data, &mut csprng).unwrap();
    let request = InitRequest {
        rpc_address: rpc.to_owned(),
        locality: Locality::default(),
        capacity: NodeCapacity::default(),
        trust: TrustConfig::from_pems(
            CA_PEM.to_owned(),
            CERT_PEM.to_owned(),
            KEY_PEM.to_owned(),
            vec![identity.node_id],
        )
        .unwrap(),
    };
    cluster_init(data, &request, &mut csprng).unwrap().identity
}

fn authorized_request(method: &str, uri: &str, body: Value, authorization: &str) -> Request<Body> {
    Request::builder()
        .method(method)
        .uri(uri)
        .header("content-type", "application/json")
        .header("authorization", authorization)
        .body(Body::from(body.to_string()))
        .unwrap()
}

fn empty_authorized(method: &str, uri: &str, authorization: &str) -> Request<Body> {
    Request::builder()
        .method(method)
        .uri(uri)
        .header("authorization", authorization)
        .body(Body::empty())
        .unwrap()
}

async fn json_body(response: axum::response::Response) -> Value {
    let bytes = to_bytes(response.into_body(), usize::MAX).await.unwrap();
    serde_json::from_slice(&bytes).unwrap()
}

async fn sql_json(app: &axum::Router, admin: &str, sql: &str) -> (StatusCode, Value) {
    let response = app
        .clone()
        .oneshot(authorized_request(
            "POST",
            "/sql",
            json!({ "sql": sql }),
            admin,
        ))
        .await
        .unwrap();
    let status = response.status();
    (status, json_body(response).await)
}

fn command_id(seq: u8) -> [u8; 16] {
    let mut id = [0u8; 16];
    id[0] = seq;
    id
}

#[tokio::test]
async fn standalone_transfer_and_split_accept_ops_jobs() {
    let directory = tempdir().unwrap();
    let database =
        Arc::new(Database::create_with_credentials(directory.path(), "admin", "admin-pw").unwrap());
    let app = build_app_full(Arc::clone(&database), std::iter::empty(), None, None, true);
    let admin = "Basic YWRtaW46YWRtaW4tcHc=";
    let tablet = TabletId::from_bytes([0x11; 16]);
    let node = NodeId::from_bytes([0x22; 16]);

    // P1.6: transfer/split accept immediately as durable ops jobs (execution is
    // separate from the HTTP accept path).
    let (status, body) =
        sql_json(&app, admin, &format!("TRANSFER LEADER {tablet} TO {node}")).await;
    assert_eq!(status, StatusCode::OK, "{body}");
    assert_eq!(body["status"], "accepted");
    assert_eq!(body["command"], "TRANSFER LEADER");
    assert_eq!(body["kind"], "transfer_leader");
    assert_eq!(body["state"], "pending");
    assert!(body["job_id"].as_str().unwrap().contains("transfer_leader"));

    let (status, body) = sql_json(&app, admin, &format!("SPLIT TABLET {tablet}")).await;
    assert_eq!(status, StatusCode::OK, "{body}");
    assert_eq!(body["status"], "accepted");
    assert_eq!(body["command"], "SPLIT TABLET");
    assert_eq!(body["kind"], "split_tablet");
    assert!(body["job_id"].as_str().unwrap().contains("split_tablet"));

    // MERGE still requires a live runtime (synchronous product path).
    let (status, body) = sql_json(&app, admin, &format!("MERGE TABLETS {tablet} {tablet}")).await;
    assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE, "{body}");
    assert_eq!(body["error"], "cluster runtime not running");

    let (status, body) = sql_json(
        &app,
        admin,
        &format!("MOVE REPLICA {tablet} FROM {node} TO {node}"),
    )
    .await;
    assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE, "{body}");
    assert_eq!(body["error"], "cluster runtime not running");
}

#[tokio::test]
async fn live_runtime_status_and_missing_tablet_ops() {
    let directory = tempdir().unwrap();
    let data = directory.path();
    let listen = free_addr();
    let identity = bootstrap_cluster(data, &listen);

    let handle = ClusterRuntimeHandle::start(ClusterRuntimeOptions {
        node_data: data.to_path_buf(),
        rpc_listen: listen.clone(),
        plaintext_test: true,
        fast_timing: true,
    })
    .await
    .expect("plaintext runtime starts after cluster init");

    // P0.2: cluster AppState has no peer standalone user database. Use bearer
    // token admin for the control plane (no catalog user-auth dual-root).
    let sessions = Arc::new(mongreldb_server::SessionStore::new(
        32,
        Duration::from_secs(60),
    ));
    let (app, control) = build_app_with_storage(
        ServerStorageRuntime::cluster(handle),
        std::iter::empty(),
        Some("cluster-token".into()),
        None,
        false,
        sessions,
    );
    let admin = "Bearer cluster-token";
    assert!(control.storage().is_cluster());
    assert!(control.storage().standalone_db().is_none());

    let status_response = app
        .clone()
        .oneshot(empty_authorized("GET", "/admin/cluster/status", admin))
        .await
        .unwrap();
    assert_eq!(status_response.status(), StatusCode::OK);
    let status = json_body(status_response).await;
    assert_eq!(status["mode"], "cluster");
    assert_eq!(status["runtime"]["live"], true);
    assert_eq!(status["runtime"]["node_id"], identity.node_id.to_string());
    assert_eq!(status["runtime"]["rpc_address"], listen);
    assert_eq!(status["runtime"]["meta_present"], true);
    assert_eq!(status["runtime"]["tablet_count"], 0);

    let (code, cluster) = sql_json(&app, admin, "SHOW CLUSTER").await;
    assert_eq!(code, StatusCode::OK, "{cluster}");
    assert_eq!(cluster["result"]["runtime"]["live"], true);
    assert_eq!(
        cluster["result"]["runtime"]["node_id"],
        identity.node_id.to_string()
    );

    let missing = TabletId::from_bytes([0xCD; 16]);
    // P1.6: missing-tablet transfer/split still accept as ops jobs; execution
    // fails later when the worker runs (HTTP accept is not validation).
    let (code, body) = sql_json(
        &app,
        admin,
        &format!("TRANSFER LEADER {missing} TO {}", identity.node_id),
    )
    .await;
    assert_eq!(code, StatusCode::OK, "{body}");
    assert_eq!(body["status"], "accepted");
    assert_eq!(body["kind"], "transfer_leader");
    assert!(body["job_id"].is_string(), "{body}");

    let (code, body) = sql_json(&app, admin, &format!("SPLIT TABLET {missing}")).await;
    assert_eq!(code, StatusCode::OK, "{body}");
    assert_eq!(body["status"], "accepted");
    assert_eq!(body["kind"], "split_tablet");
    assert!(body["job_id"].is_string(), "{body}");

    let (code, body) = sql_json(
        &app,
        admin,
        &format!(
            "MOVE REPLICA {missing} FROM {} TO {}",
            identity.node_id, identity.node_id
        ),
    )
    .await;
    assert_eq!(code, StatusCode::SERVICE_UNAVAILABLE, "{body}");
    assert!(
        body["error"]
            .as_str()
            .unwrap_or("")
            .contains("not yet live"),
        "{body}"
    );

    control.shutdown().await;
}

#[tokio::test]
async fn transfer_leader_on_live_single_replica_tablet() {
    let directory = tempdir().unwrap();
    let data = directory.path();
    let listen = free_addr();
    let identity = bootstrap_cluster(data, &listen);

    let handle = ClusterRuntimeHandle::start(ClusterRuntimeOptions {
        node_data: data.to_path_buf(),
        rpc_listen: listen,
        plaintext_test: true,
        fast_timing: true,
    })
    .await
    .unwrap();

    let table_id = TableId::new(1);
    let database_id = DatabaseId::from_bytes([0x42; 16]);
    let tablet_id = seed_single_replica_tablet(&handle, &identity, table_id, database_id).await;

    let sessions = Arc::new(mongreldb_server::SessionStore::new(
        32,
        Duration::from_secs(60),
    ));
    let (app, control) = build_app_with_storage(
        ServerStorageRuntime::cluster(handle),
        std::iter::empty(),
        Some("cluster-token".into()),
        None,
        false,
        sessions,
    );
    let admin = "Bearer cluster-token";

    // P1.6: transfer accepts immediately as a durable ops job (worker executes later).
    let (code, body) = sql_json(
        &app,
        admin,
        &format!("TRANSFER LEADER {tablet_id} TO {}", identity.node_id),
    )
    .await;
    assert_eq!(code, StatusCode::OK, "{body}");
    assert_eq!(body["status"], "accepted");
    assert_eq!(body["command"], "TRANSFER LEADER");
    assert_eq!(body["kind"], "transfer_leader");
    assert_eq!(body["state"], "pending");
    assert!(body["job_id"].as_str().unwrap().contains("transfer_leader"));

    control.shutdown().await;
}

/// Create + bootstrap one single-replica tablet on the live runtime.
async fn seed_single_replica_tablet(
    handle: &ClusterRuntimeHandle,
    identity: &NodeIdentity,
    table_id: TableId,
    database_id: DatabaseId,
) -> TabletId {
    let mutex = handle.runtime_mutex();
    let mut guard = mutex.lock().await;
    let runtime: &mut NodeRuntime = guard.as_mut().expect("runtime live");
    let meta = runtime.meta_group().expect("meta present");
    meta.group()
        .wait_leader(LEADER_TIMEOUT)
        .await
        .expect("meta leader");
    let control = ExecutionControl::default();

    meta.propose(
        command_id(1),
        MetaCommand::CreateDatabase {
            descriptor: DatabaseDescriptor {
                database_id,
                name: "app".into(),
                created_at: HlcTimestamp::ZERO,
                state: DatabaseState::Online,
                metadata_version: MetadataVersion::ZERO,
            },
        },
        &control,
    )
    .await
    .unwrap();
    meta.propose(
        command_id(2),
        MetaCommand::SetTableSchema {
            record: TableSchemaRecord {
                table_id,
                database_id,
                schema_version: SchemaVersion::new(1),
                schema: serde_json::json!({"columns": [{"name": "pk", "type": "u64"}]}),
                metadata_version: MetadataVersion::ZERO,
            },
        },
        &control,
    )
    .await
    .unwrap();

    let raft_ids = meta.allocate_raft_node_ids(1, &control).await.unwrap();
    let tablet_id = TabletId::from_bytes([0x77; 16]);
    let raft_group_id = RaftGroupId::from_bytes([0x88; 16]);
    let descriptor = TabletDescriptor {
        tablet_id,
        table_id,
        database_id,
        raft_group_id,
        partition: mongreldb_cluster::tablet::PartitionBounds::unbounded(),
        replicas: vec![ReplicaDescriptor {
            node_id: identity.node_id,
            role: ReplicaRole::Voter,
            raft_node_id: raft_ids[0],
        }],
        leader_hint: Some(identity.node_id),
        generation: 1,
        state: TabletState::Active,
    };
    let address = runtime.rpc_address().to_owned();
    let peers = [(identity.node_id, address)];
    let partitioning =
        TablePartitioningRecord::automatic_default(table_id, vec![ColumnId::new(1)], 16);
    runtime
        .create_tablet(&descriptor, &partitioning, Some(&peers), true, &control)
        .await
        .expect("create single-replica tablet");
    runtime
        .tablet_group(tablet_id)
        .expect("tablet opened")
        .wait_leader(LEADER_TIMEOUT)
        .await
        .expect("tablet leader");
    tablet_id
}

#[tokio::test]
async fn standalone_build_app_still_healthy() {
    let directory = tempdir().unwrap();
    let database = Arc::new(Database::create(directory.path()).unwrap());
    let app = build_app(database);
    let response = app
        .oneshot(
            Request::builder()
                .method("GET")
                .uri("/health")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(response.status(), StatusCode::OK);
}