discovery.rs 7.58 KB
Newer Older
Ryan Olson's avatar
Ryan Olson committed
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
// SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! Discovery configuration for Nova peer discovery.
//!
//! Supports three discovery backends:
//! - **Etcd**: Centralized discovery using etcd key-value store
//! - **P2P**: Decentralized discovery using libp2p DHT
//! - **Filesystem**: File-based discovery for development/testing

use std::path::PathBuf;

use serde::{Deserialize, Serialize};
use validator::Validate;

/// Discovery configuration - only one type can be active at a time.
///
/// # JSON Configuration Examples
///
/// ## Etcd Discovery
/// ```json
/// {
///   "type": "etcd",
///   "cluster_id": "my-cluster",
///   "endpoints": ["http://etcd1:2379", "http://etcd2:2379"],
///   "ttl_secs": 60
/// }
/// ```
///
/// ## P2P Discovery
/// ```json
/// {
///   "type": "p2p",
///   "cluster_id": "my-cluster",
///   "listen_port": 0,
///   "bootstrap_peers": ["192.168.1.10:4001"],
///   "enable_mdns": true
/// }
/// ```
///
/// ## Filesystem Discovery
/// ```json
/// {
///   "type": "filesystem",
///   "path": "/tmp/discovery.json"
/// }
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum DiscoveryConfig {
    /// Etcd-based discovery (centralized).
    Etcd(EtcdDiscoveryConfig),
    /// P2P discovery using libp2p DHT (decentralized).
    P2p(P2pDiscoveryConfig),
    /// Filesystem-based discovery (for dev/testing).
    Filesystem(FilesystemDiscoveryConfig),
}

/// Etcd discovery configuration.
#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
pub struct EtcdDiscoveryConfig {
    /// Cluster ID / key prefix for discovery (required).
    pub cluster_id: String,

    /// Etcd endpoints (default: ["http://localhost:2379"]).
    #[serde(default = "default_etcd_endpoints")]
    pub endpoints: Vec<String>,

    /// Lease TTL in seconds (default: 60, range: 10-600).
    #[serde(default = "default_etcd_ttl")]
    #[validate(range(min = 10, max = 600))]
    pub ttl_secs: u64,

    /// Operation timeout in seconds (default: 30).
    #[serde(default = "default_operation_timeout")]
    pub operation_timeout_secs: u64,

    /// Max retries for operations (default: 3).
    #[serde(default = "default_max_retries")]
    #[validate(range(min = 0, max = 10))]
    pub max_retries: u32,
}

/// P2P discovery configuration.
#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
pub struct P2pDiscoveryConfig {
    /// Cluster ID / swarm key (required).
    pub cluster_id: String,

    /// Listen port (default: 0 = OS-assigned).
    #[serde(default)]
    pub listen_port: u16,

    /// Bootstrap peer addresses.
    #[serde(default)]
    pub bootstrap_peers: Vec<String>,

    /// DHT replication factor (default: 3).
    #[serde(default = "default_replication_factor")]
    pub replication_factor: usize,

    /// Enable mDNS for local network discovery (default: false).
    #[serde(default)]
    pub enable_mdns: bool,

    /// Record TTL in seconds (default: 600).
    #[serde(default = "default_record_ttl")]
    pub record_ttl_secs: u64,
}

/// Filesystem discovery configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FilesystemDiscoveryConfig {
    /// Path to the discovery JSON file.
    pub path: PathBuf,
}

fn default_etcd_endpoints() -> Vec<String> {
    vec!["http://localhost:2379".to_string()]
}

fn default_etcd_ttl() -> u64 {
    60
}

fn default_operation_timeout() -> u64 {
    30
}

fn default_max_retries() -> u32 {
    3
}

fn default_replication_factor() -> usize {
    3
}

fn default_record_ttl() -> u64 {
    600
}

impl Default for EtcdDiscoveryConfig {
    fn default() -> Self {
        Self {
            cluster_id: String::new(),
            endpoints: default_etcd_endpoints(),
            ttl_secs: default_etcd_ttl(),
            operation_timeout_secs: default_operation_timeout(),
            max_retries: default_max_retries(),
        }
    }
}

impl Default for P2pDiscoveryConfig {
    fn default() -> Self {
        Self {
            cluster_id: String::new(),
            listen_port: 0,
            bootstrap_peers: Vec::new(),
            replication_factor: default_replication_factor(),
            enable_mdns: false,
            record_ttl_secs: default_record_ttl(),
        }
    }
}

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

    #[test]
    fn test_deserialize_etcd_config() {
        let json = r#"{
            "type": "etcd",
            "cluster_id": "test-cluster",
            "endpoints": ["http://etcd1:2379"],
            "ttl_secs": 120
        }"#;

        let config: DiscoveryConfig = serde_json::from_str(json).unwrap();
        match config {
            DiscoveryConfig::Etcd(etcd) => {
                assert_eq!(etcd.cluster_id, "test-cluster");
                assert_eq!(etcd.endpoints, vec!["http://etcd1:2379"]);
                assert_eq!(etcd.ttl_secs, 120);
                assert_eq!(etcd.operation_timeout_secs, 30); // default
                assert_eq!(etcd.max_retries, 3); // default
            }
            _ => panic!("Expected Etcd config"),
        }
    }

    #[test]
    fn test_deserialize_p2p_config() {
        let json = r#"{
            "type": "p2p",
            "cluster_id": "test-cluster",
            "listen_port": 4001,
            "bootstrap_peers": ["192.168.1.10:4001"],
            "enable_mdns": true
        }"#;

        let config: DiscoveryConfig = serde_json::from_str(json).unwrap();
        match config {
            DiscoveryConfig::P2p(p2p) => {
                assert_eq!(p2p.cluster_id, "test-cluster");
                assert_eq!(p2p.listen_port, 4001);
                assert_eq!(p2p.bootstrap_peers, vec!["192.168.1.10:4001"]);
                assert!(p2p.enable_mdns);
                assert_eq!(p2p.replication_factor, 3); // default
                assert_eq!(p2p.record_ttl_secs, 600); // default
            }
            _ => panic!("Expected P2p config"),
        }
    }

    #[test]
    fn test_deserialize_filesystem_config() {
        let json = r#"{
            "type": "filesystem",
            "path": "/tmp/discovery.json"
        }"#;

        let config: DiscoveryConfig = serde_json::from_str(json).unwrap();
        match config {
            DiscoveryConfig::Filesystem(fs) => {
                assert_eq!(fs.path, PathBuf::from("/tmp/discovery.json"));
            }
            _ => panic!("Expected Filesystem config"),
        }
    }

    #[test]
    fn test_serialize_etcd_config() {
        let config = DiscoveryConfig::Etcd(EtcdDiscoveryConfig {
            cluster_id: "my-cluster".to_string(),
            endpoints: vec!["http://localhost:2379".to_string()],
            ttl_secs: 60,
            operation_timeout_secs: 30,
            max_retries: 3,
        });

        let json = serde_json::to_string(&config).unwrap();
        assert!(json.contains(r#""type":"etcd""#));
        assert!(json.contains(r#""cluster_id":"my-cluster""#));
    }

    #[test]
    fn test_etcd_default() {
        let config = EtcdDiscoveryConfig::default();
        assert!(config.cluster_id.is_empty());
        assert_eq!(config.endpoints, vec!["http://localhost:2379"]);
        assert_eq!(config.ttl_secs, 60);
        assert_eq!(config.operation_timeout_secs, 30);
        assert_eq!(config.max_retries, 3);
    }

    #[test]
    fn test_p2p_default() {
        let config = P2pDiscoveryConfig::default();
        assert!(config.cluster_id.is_empty());
        assert_eq!(config.listen_port, 0);
        assert!(config.bootstrap_peers.is_empty());
        assert_eq!(config.replication_factor, 3);
        assert!(!config.enable_mdns);
        assert_eq!(config.record_ttl_secs, 600);
    }
}