solidb 1.0.2

A lightweight, high-performance structured database server written in Rust.
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
use super::system::AppState;
use crate::error::DbError;
use crate::storage::http_client::get_http_client;
use axum::{
    extract::{Path, State},
    http::HeaderMap,
    response::Json,
};
use serde_json::Value;
use std::collections::HashMap;

/// Format size in human-readable format
fn format_size(bytes: u64) -> String {
    const KB: u64 = 1024;
    const MB: u64 = 1024 * KB;
    const GB: u64 = 1024 * MB;

    if bytes >= GB {
        format!("{:.2} GB", bytes as f64 / GB as f64)
    } else if bytes >= MB {
        format!("{:.2} MB", bytes as f64 / MB as f64)
    } else if bytes >= KB {
        format!("{:.2} KB", bytes as f64 / KB as f64)
    } else {
        format!("{} B", bytes)
    }
}

/// Get detailed sharding information including per-shard document counts, disk sizes, and node assignments
pub async fn get_sharding_details(
    State(state): State<AppState>,
    Path((db_name, coll_name)): Path<(String, String)>,
    headers: HeaderMap,
) -> Result<Json<Value>, DbError> {
    let database = state.storage.get_database(&db_name)?;
    let collection = database.get_collection(&coll_name)?;
    let shard_config = collection.get_shard_config();

    let is_sharded = shard_config
        .as_ref()
        .map(|c| c.num_shards > 0)
        .unwrap_or(false);

    if !is_sharded {
        // Not a sharded collection
        let stats = collection.stats();
        return Ok(Json(serde_json::json!({
            "database": db_name,
            "collection": coll_name,
            "type": collection.get_type(),
            "sharded": false,
            "total_documents": stats.document_count,
            "total_size": stats.disk_usage.sst_files_size + stats.disk_usage.memtable_size,
            "shards": []
        })));
    }

    let config = shard_config.unwrap();

    // Get cluster nodes info
    let (nodes, healthy_nodes, node_id_to_address) =
        if let Some(ref coordinator) = state.shard_coordinator {
            let all_node_ids = coordinator.get_node_ids();
            let my_node_id = coordinator.my_node_id();

            // Build node ID to address mapping
            let mut id_to_addr: HashMap<String, String> = HashMap::new();
            if let Some(ref mgr) = state.cluster_manager {
                for member in mgr.state().get_all_members() {
                    id_to_addr.insert(member.node.id.clone(), member.node.api_address.clone());
                }
            }

            // Get healthy nodes from cluster manager
            let healthy = if let Some(ref mgr) = state.cluster_manager {
                mgr.get_healthy_nodes()
            } else {
                vec![my_node_id.clone()]
            };

            (all_node_ids, healthy, id_to_addr)
        } else {
            (
                vec!["local".to_string()],
                vec!["local".to_string()],
                HashMap::new(),
            )
        };

    // Get shard table for assignments
    let shard_table = if let Some(ref coordinator) = state.shard_coordinator {
        coordinator.get_shard_table(&db_name, &coll_name)
    } else {
        None
    };

    let mut shards_info: Vec<serde_json::Value> = Vec::new();
    let mut total_documents = 0u64;
    let mut total_size = 0u64;

    // Get my node ID to check if shard is local
    let my_node_id = if let Some(ref coordinator) = state.shard_coordinator {
        coordinator.my_node_id()
    } else {
        "local".to_string()
    };

    // Query each physical shard for actual stats
    // Use scatter-gather to query remote nodes when shard isn't local
    let client = get_http_client();
    let secret = state.cluster_secret();

    // Get auth token from request headers to forward to remote nodes
    let auth_header = headers
        .get("authorization")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("")
        .to_string();

    for shard_id in 0..config.num_shards {
        let physical_coll_name = format!("{}_s{}", coll_name, shard_id);

        // Get assignment info first
        let (primary_node, replica_nodes) = if let Some(ref table) = shard_table {
            if let Some(assignment) = table.assignments.get(&shard_id) {
                (
                    assignment.primary_node.clone(),
                    assignment.replica_nodes.clone(),
                )
            } else {
                ("unknown".to_string(), vec![])
            }
        } else {
            // Fall back to computing assignment based on modulo
            let num_nodes = nodes.len();
            if num_nodes > 0 {
                let primary_idx = (shard_id as usize) % num_nodes;
                let primary = nodes.get(primary_idx).cloned().unwrap_or_default();
                let mut replicas = Vec::new();
                for r in 1..config.replication_factor {
                    let replica_idx = (primary_idx + r as usize) % num_nodes;
                    if replica_idx != primary_idx {
                        if let Some(n) = nodes.get(replica_idx) {
                            replicas.push(n.clone());
                        }
                    }
                }
                (primary, replicas)
            } else {
                ("local".to_string(), vec![])
            }
        };

        // Get stats - either local or remote
        let mut stats_result: Option<(u64, u64, u64)> = None;

        // 1. Try Primary Node (Local or Remote)
        if primary_node == my_node_id || primary_node == "local" {
            // Local primary
            if let Ok(physical_coll) = database.get_collection(&physical_coll_name) {
                let stats = physical_coll.stats();
                stats_result = Some((
                    stats.document_count as u64,
                    stats.chunk_count as u64,
                    stats.disk_usage.sst_files_size + stats.disk_usage.memtable_size,
                ));
            }
        } else if let Some(primary_addr) = node_id_to_address.get(&primary_node) {
            // Remote primary
            let scheme =
                std::env::var("SOLIDB_CLUSTER_SCHEME").unwrap_or_else(|_| "http".to_string());
            let url = format!(
                "{}://{}/_api/database/{}/collection/{}/stats",
                scheme, primary_addr, db_name, physical_coll_name
            );
            let mut req = client
                .get(&url)
                .header("X-Cluster-Secret", &secret)
                .timeout(std::time::Duration::from_secs(3));

            if !auth_header.is_empty() {
                req = req.header("Authorization", &auth_header);
            }

            if let Ok(res) = req.send().await {
                if res.status().is_success() {
                    if let Ok(body) = res.json::<serde_json::Value>().await {
                        let count = body
                            .get("document_count")
                            .and_then(|c| c.as_u64())
                            .unwrap_or(0);
                        let chunk_count = body
                            .get("chunk_count")
                            .and_then(|c| c.as_u64())
                            .unwrap_or(0);
                        let disk = body.get("disk_usage");
                        let size = disk
                            .map(|d| {
                                let sst = d
                                    .get("sst_files_size")
                                    .and_then(|v| v.as_u64())
                                    .unwrap_or(0);
                                let mem =
                                    d.get("memtable_size").and_then(|v| v.as_u64()).unwrap_or(0);
                                sst + mem
                            })
                            .unwrap_or(0);
                        stats_result = Some((count, chunk_count, size));
                    }
                }
            }
        }

        // 2. Fallback to Replicas if Primary failed
        if stats_result.is_none() {
            for replica_node in &replica_nodes {
                if replica_node == &my_node_id {
                    // Local replica
                    if let Ok(physical_coll) = database.get_collection(&physical_coll_name) {
                        let stats = physical_coll.stats();
                        stats_result = Some((
                            stats.document_count as u64,
                            stats.chunk_count as u64,
                            stats.disk_usage.sst_files_size + stats.disk_usage.memtable_size,
                        ));
                        break;
                    }
                } else if let Some(replica_addr) = node_id_to_address.get(replica_node) {
                    // Remote replica
                    let scheme = std::env::var("SOLIDB_CLUSTER_SCHEME")
                        .unwrap_or_else(|_| "http".to_string());
                    let url = format!(
                        "{}://{}/_api/database/{}/collection/{}/stats",
                        scheme, replica_addr, db_name, physical_coll_name
                    );
                    let mut req = client
                        .get(&url)
                        .header("X-Cluster-Secret", &secret)
                        .timeout(std::time::Duration::from_secs(2));

                    if !auth_header.is_empty() {
                        req = req.header("Authorization", &auth_header);
                    }

                    if let Ok(res) = req.send().await {
                        if res.status().is_success() {
                            if let Ok(body) = res.json::<serde_json::Value>().await {
                                let count = body
                                    .get("document_count")
                                    .and_then(|c| c.as_u64())
                                    .unwrap_or(0);
                                let chunk_count = body
                                    .get("chunk_count")
                                    .and_then(|c| c.as_u64())
                                    .unwrap_or(0);
                                let disk = body.get("disk_usage");
                                let size = disk
                                    .map(|d| {
                                        let sst = d
                                            .get("sst_files_size")
                                            .and_then(|v| v.as_u64())
                                            .unwrap_or(0);
                                        let mem = d
                                            .get("memtable_size")
                                            .and_then(|v| v.as_u64())
                                            .unwrap_or(0);
                                        sst + mem
                                    })
                                    .unwrap_or(0);
                                stats_result = Some((count, chunk_count, size));
                                break;
                            }
                        }
                    }
                }
            }
        }

        let (doc_count, chunk_count, disk_size) = stats_result.unwrap_or((0, 0, 0));
        let fetch_failed = stats_result.is_none();

        let primary_healthy = healthy_nodes.contains(&primary_node);
        let primary_address = node_id_to_address
            .get(&primary_node)
            .cloned()
            .unwrap_or_else(|| primary_node.clone());

        // Build replica info with health status
        let replicas_info: Vec<serde_json::Value> = replica_nodes
            .iter()
            .map(|node_id| {
                let is_healthy = healthy_nodes.contains(node_id);
                let address = node_id_to_address
                    .get(node_id)
                    .cloned()
                    .unwrap_or_else(|| node_id.clone());
                serde_json::json!({
                    "node_id": node_id,
                    "address": address,
                    "healthy": is_healthy
                })
            })
            .collect();

        total_documents += doc_count;
        total_size += disk_size;

        // Status checks - distinguish between dead node and syncing shard
        let shard_status = if !primary_healthy {
            "dead" // Node is actually unhealthy
        } else if fetch_failed && primary_node != my_node_id {
            "syncing" // Node is healthy but shard data not available yet
        } else {
            "healthy"
        };

        shards_info.push(serde_json::json!({
            "shard_id": shard_id,
            "physical_collection": physical_coll_name,
            "document_count": doc_count,
            "chunk_count": chunk_count,
            "disk_size": disk_size,
            "disk_size_formatted": format_size(disk_size),
            "primary": {
                "node_id": primary_node,
                "address": primary_address,
                "healthy": primary_healthy
            },
            "replicas": replicas_info,
            "status": shard_status,
            "fetch_failed": fetch_failed
        }));
    }

    // Build node summary with actual status from cluster state
    // NodeId -> (PrimaryDocs, ReplicaDocs, PrimarySize, ReplicaSize, HasFailedFetch)
    struct NodeStat {
        primary_docs: u64,
        replica_docs: u64,
        primary_chunks: u64,
        replica_chunks: u64,
        primary_size: u64,
        replica_size: u64,
        has_failed: bool,
    }

    let mut node_stats: HashMap<String, NodeStat> = HashMap::new();

    // First, initialize with all primary nodes from assignment to ensure we track them even if fetch failed
    for shard in &shards_info {
        // Track Primary Stats
        if let Some(primary) = shard
            .get("primary")
            .and_then(|p| p.get("node_id"))
            .and_then(|n| n.as_str())
        {
            let doc_count = shard
                .get("document_count")
                .and_then(|d| d.as_u64())
                .unwrap_or(0);
            let chunk_count = shard
                .get("chunk_count")
                .and_then(|c| c.as_u64())
                .unwrap_or(0);
            let disk_size = shard.get("disk_size").and_then(|d| d.as_u64()).unwrap_or(0);
            let fetch_failed = shard
                .get("fetch_failed")
                .and_then(|f| f.as_bool())
                .unwrap_or(false);

            let entry = node_stats.entry(primary.to_string()).or_insert(NodeStat {
                primary_docs: 0,
                replica_docs: 0,
                primary_chunks: 0,
                replica_chunks: 0,
                primary_size: 0,
                replica_size: 0,
                has_failed: false,
            });

            entry.primary_docs += doc_count;
            entry.primary_chunks += chunk_count;
            entry.primary_size += disk_size;
            if fetch_failed {
                entry.has_failed = true;
            }
        }

        // Track Replica Stats
        if let Some(replicas) = shard.get("replicas").and_then(|r| r.as_array()) {
            for replica in replicas {
                if let Some(replica_node) = replica.get("node_id").and_then(|n| n.as_str()) {
                    // Replicas have the same doc count/disk size as the primary (they're copies)
                    let doc_count = shard
                        .get("document_count")
                        .and_then(|d| d.as_u64())
                        .unwrap_or(0);
                    let chunk_count = shard
                        .get("chunk_count")
                        .and_then(|c| c.as_u64())
                        .unwrap_or(0);
                    let disk_size = shard.get("disk_size").and_then(|d| d.as_u64()).unwrap_or(0);

                    let entry = node_stats
                        .entry(replica_node.to_string())
                        .or_insert(NodeStat {
                            primary_docs: 0,
                            replica_docs: 0,
                            primary_chunks: 0,
                            replica_chunks: 0,
                            primary_size: 0,
                            replica_size: 0,
                            has_failed: false,
                        });

                    entry.replica_docs += doc_count;
                    entry.replica_chunks += chunk_count;
                    entry.replica_size += disk_size;
                }
            }
        }
    }

    // ALSO include all cluster members - not just those in shard assignments
    // This ensures returning nodes that haven't been assigned shards yet still appear
    if let Some(ref mgr) = state.cluster_manager {
        for member in mgr.state().get_all_members() {
            // Add to node_stats if not already present
            node_stats
                .entry(member.node.id.clone())
                .or_insert(NodeStat {
                    primary_docs: 0,
                    replica_docs: 0,
                    primary_chunks: 0,
                    replica_chunks: 0,
                    primary_size: 0,
                    replica_size: 0,
                    has_failed: false,
                });
        }
    }

    let nodes_summary: Vec<serde_json::Value> = node_stats
        .iter()
        .map(|(node_id, stats)| {
            let is_healthy = healthy_nodes.contains(node_id);
            let address = node_id_to_address
                .get(node_id)
                .cloned()
                .unwrap_or_else(|| node_id.clone());
            let shard_count = shards_info
                .iter()
                .filter(|s| {
                    s.get("primary")
                        .and_then(|p| p.get("node_id"))
                        .and_then(|n| n.as_str())
                        == Some(node_id)
                })
                .count();

            // Count replica shards for this node
            let replica_count = shards_info
                .iter()
                .filter(|s| {
                    if let Some(replicas) = s.get("replicas").and_then(|r| r.as_array()) {
                        replicas
                            .iter()
                            .any(|r| r.get("node_id").and_then(|n| n.as_str()) == Some(node_id))
                    } else {
                        false
                    }
                })
                .count();

            // Get actual node status from cluster manager
            let mut status = if let Some(ref mgr) = state.cluster_manager {
                if let Some(member) = mgr.state().get_member(node_id) {
                    match member.status {
                        crate::cluster::state::NodeStatus::Syncing => "syncing",
                        crate::cluster::state::NodeStatus::Active => "healthy",
                        crate::cluster::state::NodeStatus::Joining => "joining",
                        crate::cluster::state::NodeStatus::Suspected => "suspected",
                        crate::cluster::state::NodeStatus::Dead => "dead",
                        crate::cluster::state::NodeStatus::Leaving => "leaving",
                    }
                } else if is_healthy {
                    // Node id not in cluster state but marked healthy (shouldn't happen normally)
                    "healthy"
                } else {
                    // Node was removed from cluster - mark as dead
                    "dead"
                }
            } else if is_healthy {
                "healthy"
            } else {
                // No cluster manager and not healthy - assume dead
                "dead"
            };

            // Override status if fetch failed - node is healthy but missing data
            if status == "healthy" && stats.has_failed {
                status = "syncing"; // Node is healthy but missing data, needs sync
            }

            let total_docs = stats.primary_docs + stats.replica_docs;
            let total_chunks = stats.primary_chunks + stats.replica_chunks;
            let total_size = stats.primary_size + stats.replica_size;

            serde_json::json!({
                "node_id": node_id,
                "address": address,
                "healthy": is_healthy,
                "status": status,
                "primary_shards": shard_count,
                "replica_shards": replica_count,
                "document_count": total_docs,
                "chunk_count": total_chunks,
                "primary_docs": stats.primary_docs,
                "replica_docs": stats.replica_docs,
                "primary_chunks": stats.primary_chunks,
                "replica_chunks": stats.replica_chunks,
                "disk_size": total_size,
                "primary_size": stats.primary_size,
                "replica_size": stats.replica_size,
                "disk_size_formatted": format_size(total_size)
            })
        })
        .collect();

    let mut nodes_sorted = nodes_summary;
    nodes_sorted.sort_by(|a, b| {
        let addr_a = a.get("address").and_then(|s| s.as_str()).unwrap_or("");
        let addr_b = b.get("address").and_then(|s| s.as_str()).unwrap_or("");
        addr_a.cmp(addr_b)
    });

    Ok(Json(serde_json::json!({
        "database": db_name,
        "collection": coll_name,
        "type": collection.get_type(),
        "sharded": true,
        "config": {
            "num_shards": config.num_shards,
            "shard_key": config.shard_key,
            "replication_factor": config.replication_factor
        },
        "total_documents": total_documents,
        // Calculate total chunks for the collection (sum of primary chunks)
        "total_chunks": node_stats.values().map(|s| s.primary_chunks).sum::<u64>(),
        "total_size": total_size,
        "total_size_formatted": format_size(total_size),
        "cluster": {
            "total_nodes": nodes.len(),
            "healthy_nodes": healthy_nodes.len()
        },
        "nodes": nodes_sorted,
        "shards": shards_info
    })))
}