reddb-io-server 1.12.0

RedDB server-side engine: storage, runtime, replication, MCP, AI, and the gRPC/HTTP/RedWire/PG-wire dispatchers. Re-exported by the umbrella `reddb` crate.
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
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
//! Operational bootstrap planning for deployment topology and runtime config.
//!
//! This module is the CLI/runtime seam for deployment shape. It translates the
//! human container contract (`REDDB_TOPOLOGY`, `REDDB_NODE_ROLE`,
//! `REDDB_CONFIG_FILE`, storage preset/profile/env overrides, and explicit CLI
//! role flags) into the process role, config overlay path, and
//! [`StorageProfileSelection`] that the server runtime already understands.

use crate::storage::{
    DeployProfile, StorageDeployPreset, StoragePackaging, StorageProfileSelection,
};

pub const DEFAULT_CONFIG_FILE_PATH: &str = "/etc/reddb/config.json";

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OperationalTopology {
    Standalone,
    Serverless,
    PrimaryReplica,
    Cluster,
}

impl OperationalTopology {
    pub fn parse(raw: &str) -> Option<Self> {
        match raw {
            "standalone" => Some(Self::Standalone),
            "serverless" => Some(Self::Serverless),
            "primary-replica" => Some(Self::PrimaryReplica),
            "cluster" => Some(Self::Cluster),
            _ => None,
        }
    }

    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Standalone => "standalone",
            Self::Serverless => "serverless",
            Self::PrimaryReplica => "primary-replica",
            Self::Cluster => "cluster",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OperationalNodeRole {
    Standalone,
    Serverless,
    Primary,
    Replica,
    ClusterMember,
}

impl OperationalNodeRole {
    pub fn parse(raw: &str) -> Option<Self> {
        match raw {
            "standalone" => Some(Self::Standalone),
            "serverless" => Some(Self::Serverless),
            "primary" => Some(Self::Primary),
            "replica" => Some(Self::Replica),
            "cluster-member" => Some(Self::ClusterMember),
            _ => None,
        }
    }

    pub const fn process_role(self) -> &'static str {
        match self {
            Self::Primary => "primary",
            Self::Replica => "replica",
            Self::Standalone | Self::Serverless | Self::ClusterMember => "standalone",
        }
    }

    pub const fn implied_topology(self) -> OperationalTopology {
        match self {
            Self::Standalone => OperationalTopology::Standalone,
            Self::Serverless => OperationalTopology::Serverless,
            Self::Primary | Self::Replica => OperationalTopology::PrimaryReplica,
            Self::ClusterMember => OperationalTopology::Cluster,
        }
    }

    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Standalone => "standalone",
            Self::Serverless => "serverless",
            Self::Primary => "primary",
            Self::Replica => "replica",
            Self::ClusterMember => "cluster-member",
        }
    }
}

#[derive(Debug, Clone, Default)]
pub struct OperationalBootstrapInput {
    /// Command-level override. `red replica` sets this to `replica`; it wins
    /// over all env and flag input.
    pub forced_role: Option<String>,
    /// `red server --role <standalone|primary|replica>`.
    pub role_flag: Option<String>,
    /// Human deployment topology, normally `REDDB_TOPOLOGY`.
    pub topology: Option<String>,
    /// Human node role, normally `REDDB_NODE_ROLE`.
    pub node_role: Option<String>,
    pub storage_preset: Option<String>,
    pub storage_profile: Option<String>,
    pub storage_packaging: Option<String>,
    pub replica_count: Option<String>,
    pub managed_backup: bool,
    pub wal_retention: bool,
    /// Mounted config file path, normally `REDDB_CONFIG_FILE`.
    pub config_file_path: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OperationalBootstrapPlan {
    pub topology: OperationalTopology,
    pub node_role: OperationalNodeRole,
    pub process_role: String,
    pub storage_profile: StorageProfileSelection,
    pub config_file_path: String,
}

pub fn resolve_operational_bootstrap(
    input: OperationalBootstrapInput,
) -> Result<OperationalBootstrapPlan, String> {
    let topology = parse_optional_topology(input.topology.as_deref())?;
    let env_node_role = parse_optional_node_role(input.node_role.as_deref())?;
    let process_node_role =
        parse_process_role(input.forced_role.as_deref().or(input.role_flag.as_deref()))?;

    let node_role = if let Some(forced) = input.forced_role.as_deref() {
        parse_process_role(Some(forced))?.expect("forced process role is present")
    } else {
        env_node_role
            .or_else(|| match process_node_role {
                Some(OperationalNodeRole::Primary | OperationalNodeRole::Replica) => {
                    process_node_role
                }
                _ => topology.map(default_node_role_for_topology),
            })
            .or(process_node_role)
            .unwrap_or(OperationalNodeRole::Standalone)
    };

    let topology = topology.unwrap_or_else(|| node_role.implied_topology());
    validate_topology_node_role(topology, node_role)?;

    let process_role = process_node_role
        .unwrap_or(node_role)
        .process_role()
        .to_string();

    let storage_profile = resolve_storage_selection(&input, topology)?;
    let config_file_path = resolve_config_file_path(input.config_file_path.as_deref());

    Ok(OperationalBootstrapPlan {
        topology,
        node_role,
        process_role,
        storage_profile,
        config_file_path,
    })
}

pub fn resolve_config_file_path(raw: Option<&str>) -> String {
    raw.filter(|value| !value.trim().is_empty())
        .unwrap_or(DEFAULT_CONFIG_FILE_PATH)
        .to_string()
}

fn parse_optional_topology(raw: Option<&str>) -> Result<Option<OperationalTopology>, String> {
    raw.filter(|value| !value.trim().is_empty())
        .map(|value| {
            OperationalTopology::parse(value).ok_or_else(|| {
                format!(
                    "topology {value:?} is not recognised (expected standalone, serverless, primary-replica, or cluster)"
                )
            })
        })
        .transpose()
}

fn parse_optional_node_role(raw: Option<&str>) -> Result<Option<OperationalNodeRole>, String> {
    raw.filter(|value| !value.trim().is_empty())
        .map(|value| {
            OperationalNodeRole::parse(value).ok_or_else(|| {
                format!(
                    "node role {value:?} is not recognised (expected standalone, serverless, primary, replica, or cluster-member)"
                )
            })
        })
        .transpose()
}

fn parse_process_role(raw: Option<&str>) -> Result<Option<OperationalNodeRole>, String> {
    raw.filter(|value| !value.trim().is_empty())
        .map(|value| match value {
            "standalone" => Ok(OperationalNodeRole::Standalone),
            "primary" => Ok(OperationalNodeRole::Primary),
            "replica" => Ok(OperationalNodeRole::Replica),
            _ => Err(format!(
                "process role {value:?} is not recognised (expected standalone, primary, or replica)"
            )),
        })
        .transpose()
}

fn default_node_role_for_topology(topology: OperationalTopology) -> OperationalNodeRole {
    match topology {
        OperationalTopology::Standalone => OperationalNodeRole::Standalone,
        OperationalTopology::Serverless => OperationalNodeRole::Serverless,
        OperationalTopology::PrimaryReplica => OperationalNodeRole::Primary,
        OperationalTopology::Cluster => OperationalNodeRole::ClusterMember,
    }
}

fn validate_topology_node_role(
    topology: OperationalTopology,
    node_role: OperationalNodeRole,
) -> Result<(), String> {
    let ok = matches!(
        (topology, node_role),
        (
            OperationalTopology::Standalone,
            OperationalNodeRole::Standalone
        ) | (
            OperationalTopology::Serverless,
            OperationalNodeRole::Serverless
        ) | (
            OperationalTopology::Serverless,
            OperationalNodeRole::Standalone
        ) | (
            OperationalTopology::PrimaryReplica,
            OperationalNodeRole::Primary
        ) | (
            OperationalTopology::PrimaryReplica,
            OperationalNodeRole::Replica
        ) | (
            OperationalTopology::Cluster,
            OperationalNodeRole::ClusterMember
        ) | (
            OperationalTopology::Cluster,
            OperationalNodeRole::Standalone
        )
    );
    if ok {
        Ok(())
    } else {
        Err(format!(
            "node role {:?} is not valid for topology {:?}",
            node_role.as_str(),
            topology.as_str()
        ))
    }
}

fn resolve_storage_selection(
    input: &OperationalBootstrapInput,
    topology: OperationalTopology,
) -> Result<StorageProfileSelection, String> {
    let mut selection = if let Some(raw) = input
        .storage_preset
        .as_deref()
        .filter(|value| !value.is_empty())
    {
        let preset = StorageDeployPreset::parse(raw).ok_or_else(|| {
            format!(
                "storage preset {raw:?} is not recognised (expected embedded, serverless, primary-replica-dev, primary-replica-small, primary-replica-production-ha, primary-replica-backup, primary-replica-wal-retention, or cluster)"
            )
        })?;
        preset.selection()
    } else {
        default_storage_selection(topology)
    };

    if let Some(raw) = input
        .storage_profile
        .as_deref()
        .filter(|value| !value.is_empty())
    {
        selection.deploy_profile = DeployProfile::parse(raw).ok_or_else(|| {
            format!(
                "storage profile {raw:?} is not recognised (expected embedded, serverless, primary-replica, or cluster)"
            )
        })?;
    }

    if let Some(raw) = input
        .storage_packaging
        .as_deref()
        .filter(|value| !value.is_empty())
    {
        selection.packaging = StoragePackaging::parse(raw).ok_or_else(|| {
            format!(
                "storage packaging {raw:?} is not recognised (expected single-file or operational-directory)"
            )
        })?;
    }

    if let Some(raw) = input
        .replica_count
        .as_deref()
        .filter(|value| !value.is_empty())
    {
        selection.replica_count = raw
            .parse::<u16>()
            .map_err(|_| format!("replica-count must be a non-negative integer, got {raw:?}"))?;
    }

    if input.managed_backup {
        selection.managed_backup = true;
    }
    if input.wal_retention {
        selection.wal_retention = true;
    }

    selection.validate()
}

fn default_storage_selection(topology: OperationalTopology) -> StorageProfileSelection {
    match topology {
        OperationalTopology::Standalone => StorageProfileSelection::embedded_single_file(),
        OperationalTopology::Serverless => StorageDeployPreset::Serverless.selection(),
        OperationalTopology::PrimaryReplica => StorageDeployPreset::PrimaryReplicaDev.selection(),
        OperationalTopology::Cluster => StorageDeployPreset::Cluster.selection(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn serverless_topology_defaults_to_serverless_storage_and_standalone_process() {
        let plan = resolve_operational_bootstrap(OperationalBootstrapInput {
            topology: Some("serverless".to_string()),
            ..Default::default()
        })
        .unwrap();

        assert_eq!(plan.topology, OperationalTopology::Serverless);
        assert_eq!(plan.node_role, OperationalNodeRole::Serverless);
        assert_eq!(plan.process_role, "standalone");
        assert_eq!(
            plan.storage_profile.deploy_profile,
            DeployProfile::Serverless
        );
    }

    #[test]
    fn primary_replica_node_role_selects_process_role() {
        let plan = resolve_operational_bootstrap(OperationalBootstrapInput {
            topology: Some("primary-replica".to_string()),
            node_role: Some("replica".to_string()),
            ..Default::default()
        })
        .unwrap();

        assert_eq!(plan.node_role, OperationalNodeRole::Replica);
        assert_eq!(plan.process_role, "replica");
        assert_eq!(
            plan.storage_profile.deploy_profile,
            DeployProfile::PrimaryReplica
        );
    }

    #[test]
    fn cluster_member_uses_cluster_storage_but_standalone_process_role() {
        let plan = resolve_operational_bootstrap(OperationalBootstrapInput {
            topology: Some("cluster".to_string()),
            node_role: Some("cluster-member".to_string()),
            ..Default::default()
        })
        .unwrap();

        assert_eq!(plan.topology, OperationalTopology::Cluster);
        assert_eq!(plan.node_role, OperationalNodeRole::ClusterMember);
        assert_eq!(plan.process_role, "standalone");
        assert_eq!(plan.storage_profile.deploy_profile, DeployProfile::Cluster);
    }

    #[test]
    fn standalone_process_role_does_not_hide_cluster_topology_default() {
        let plan = resolve_operational_bootstrap(OperationalBootstrapInput {
            topology: Some("cluster".to_string()),
            role_flag: Some("standalone".to_string()),
            ..Default::default()
        })
        .unwrap();

        assert_eq!(plan.node_role, OperationalNodeRole::ClusterMember);
        assert_eq!(plan.process_role, "standalone");
        assert_eq!(plan.storage_profile.deploy_profile, DeployProfile::Cluster);
    }

    #[test]
    fn explicit_storage_preset_wins_over_topology_default() {
        let plan = resolve_operational_bootstrap(OperationalBootstrapInput {
            topology: Some("serverless".to_string()),
            storage_preset: Some("embedded".to_string()),
            ..Default::default()
        })
        .unwrap();

        assert_eq!(plan.topology, OperationalTopology::Serverless);
        assert_eq!(plan.storage_profile.deploy_profile, DeployProfile::Embedded);
    }

    #[test]
    fn incompatible_topology_and_node_role_is_rejected() {
        let err = resolve_operational_bootstrap(OperationalBootstrapInput {
            topology: Some("serverless".to_string()),
            node_role: Some("replica".to_string()),
            ..Default::default()
        })
        .unwrap_err();

        assert!(err.contains("not valid for topology"), "{err}");
    }

    #[test]
    fn config_file_path_defaults_to_container_path() {
        let plan = resolve_operational_bootstrap(OperationalBootstrapInput::default()).unwrap();

        assert_eq!(plan.config_file_path, DEFAULT_CONFIG_FILE_PATH);
    }

    #[test]
    fn explicit_config_file_path_wins() {
        let plan = resolve_operational_bootstrap(OperationalBootstrapInput {
            config_file_path: Some("/custom/reddb.json".to_string()),
            ..Default::default()
        })
        .unwrap();

        assert_eq!(plan.config_file_path, "/custom/reddb.json");
    }
}