anya-core 1.2.0

Enterprise-grade Bitcoin Infrastructure Platform
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
use serde::{Deserialize, Serialize};
use std::time::Duration;

/// Configuration for high availability system
/// [AIR-3][AIS-3][PFM-3][SCL-3][RES-3]
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct HighAvailabilityConfig {
    /// General high availability settings
    pub general: GeneralConfig,

    /// Cluster management configuration
    pub cluster: ClusterConfig,

    /// Health check configuration
    pub health_check: HealthCheckConfig,

    /// Failover configuration
    pub failover: FailoverConfig,

    /// Replication configuration
    pub replication: ReplicationConfig,

    /// Load balancing configuration
    pub load_balancing: LoadBalancingConfig,

    /// Disaster recovery configuration
    pub disaster_recovery: DisasterRecoveryConfig,
}

impl HighAvailabilityConfig {
    /// Creates a new configuration for development environment
    pub fn development() -> Self {
        Self {
            general: GeneralConfig {
                enabled: true,
                environment: Environment::Development,
                log_level: LogLevel::Debug,
            },
            cluster: ClusterConfig {
                node_count: 2,
                cluster_name: "anya-dev-cluster".to_string(),
                discovery_method: DiscoveryMethod::Static,
                static_nodes: vec!["localhost:5001".to_string(), "localhost:5002".to_string()],
                ..Default::default()
            },
            ..Default::default()
        }
    }

    /// Creates a new configuration for production environment
    pub fn production() -> Self {
        Self {
            general: GeneralConfig {
                enabled: true,
                environment: Environment::Production,
                log_level: LogLevel::Info,
            },
            cluster: ClusterConfig {
                node_count: 3,
                cluster_name: "anya-prod-cluster".to_string(),
                discovery_method: DiscoveryMethod::Dns,
                dns_discovery_url: Some("anya-cluster.example.com".to_string()),
                heartbeat_interval: Duration::from_secs(5),
                ..Default::default()
            },
            health_check: HealthCheckConfig {
                enabled: true,
                check_interval: Duration::from_secs(10),
                critical_threshold: 3,
                warning_threshold: 2,
                ..Default::default()
            },
            failover: FailoverConfig {
                enabled: true,
                auto_failover: true,
                failover_timeout: Duration::from_secs(30),
                min_nodes_for_failover: 2,
                ..Default::default()
            },
            replication: ReplicationConfig {
                mode: ReplicationMode::Synchronous,
                sync_timeout: Duration::from_secs(10),
                ..Default::default()
            },
            load_balancing: LoadBalancingConfig {
                algorithm: LoadBalancingAlgorithm::RoundRobin,
                health_check_enabled: true,
                auto_scaling: true,
                ..Default::default()
            },
            disaster_recovery: DisasterRecoveryConfig {
                backup_interval: Duration::from_secs(3600),
                backup_retention: 7,
                auto_restore: true,
                ..Default::default()
            },
        }
    }
}

/// General high availability settings
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GeneralConfig {
    /// Whether high availability is enabled
    pub enabled: bool,

    /// Environment type
    pub environment: Environment,

    /// Log level
    pub log_level: LogLevel,
}

impl Default for GeneralConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            environment: Environment::Development,
            log_level: LogLevel::Info,
        }
    }
}

/// Environment types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Environment {
    Development,
    Staging,
    Production,
}

/// Log levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum LogLevel {
    Trace,
    Debug,
    Info,
    Warn,
    Error,
}

/// Cluster configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClusterConfig {
    /// Number of nodes in the cluster
    pub node_count: usize,

    /// Cluster name
    pub cluster_name: String,

    /// Method used for node discovery
    pub discovery_method: DiscoveryMethod,

    /// List of static nodes (for static discovery)
    #[serde(default)]
    pub static_nodes: Vec<String>,

    /// DNS address for DNS discovery
    pub dns_discovery_url: Option<String>,

    /// Kubernetes service name for K8s discovery
    pub k8s_service_name: Option<String>,

    /// Heartbeat interval
    #[serde(with = "humantime_serde")]
    pub heartbeat_interval: Duration,

    /// Node timeout
    #[serde(with = "humantime_serde")]
    pub node_timeout: Duration,

    /// Gossip interval
    #[serde(with = "humantime_serde")]
    pub gossip_interval: Duration,
}

impl Default for ClusterConfig {
    fn default() -> Self {
        Self {
            node_count: 1,
            cluster_name: "anya-cluster".to_string(),
            discovery_method: DiscoveryMethod::Static,
            static_nodes: vec!["localhost:5001".to_string()],
            dns_discovery_url: None,
            k8s_service_name: None,
            heartbeat_interval: Duration::from_secs(10),
            node_timeout: Duration::from_secs(30),
            gossip_interval: Duration::from_secs(1),
        }
    }
}

/// Methods for node discovery
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DiscoveryMethod {
    Static,
    Dns,
    Kubernetes,
    Consul,
    Etcd,
}

/// Health check configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthCheckConfig {
    /// Whether health checking is enabled
    pub enabled: bool,

    /// Health check interval
    #[serde(with = "humantime_serde")]
    pub check_interval: Duration,

    /// Number of failed checks to trigger warning
    pub warning_threshold: u32,

    /// Number of failed checks to trigger critical
    pub critical_threshold: u32,

    /// Timeout for health checks
    #[serde(with = "humantime_serde")]
    pub check_timeout: Duration,

    /// Components to check
    #[serde(default)]
    pub components: Vec<String>,
}

impl Default for HealthCheckConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            check_interval: Duration::from_secs(30),
            warning_threshold: 2,
            critical_threshold: 3,
            check_timeout: Duration::from_secs(5),
            components: vec![
                "cluster".to_string(),
                "storage".to_string(),
                "network".to_string(),
            ],
        }
    }
}

/// Failover configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FailoverConfig {
    /// Whether failover is enabled
    pub enabled: bool,

    /// Whether automatic failover is enabled
    pub auto_failover: bool,

    /// Timeout before triggering failover
    #[serde(with = "humantime_serde")]
    pub failover_timeout: Duration,

    /// Minimum number of nodes required for failover
    pub min_nodes_for_failover: usize,

    /// Maximum number of automatic failovers per period
    pub max_auto_failovers: Option<u32>,

    /// Period for auto failover limits
    #[serde(with = "humantime_serde")]
    pub auto_failover_period: Duration,

    /// Fencing enabled
    pub fencing_enabled: bool,
}

impl Default for FailoverConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            auto_failover: true,
            failover_timeout: Duration::from_secs(60),
            min_nodes_for_failover: 2,
            max_auto_failovers: Some(3),
            auto_failover_period: Duration::from_secs(3600),
            fencing_enabled: true,
        }
    }
}

/// Replication configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReplicationConfig {
    /// Replication mode
    pub mode: ReplicationMode,

    /// Timeout for synchronous replication
    #[serde(with = "humantime_serde")]
    pub sync_timeout: Duration,

    /// Maximum lag for semi-sync replication
    #[serde(with = "humantime_serde")]
    pub max_lag: Duration,

    /// Number of acknowledgments required (for semi-sync)
    pub ack_count: Option<usize>,

    /// Compression enabled
    pub compression_enabled: bool,

    /// Encryption enabled
    pub encryption_enabled: bool,
}

impl Default for ReplicationConfig {
    fn default() -> Self {
        Self {
            mode: ReplicationMode::SemiSynchronous,
            sync_timeout: Duration::from_secs(5),
            max_lag: Duration::from_millis(500),
            ack_count: None,
            compression_enabled: true,
            encryption_enabled: true,
        }
    }
}

/// Replication modes
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ReplicationMode {
    Synchronous,
    SemiSynchronous,
    Asynchronous,
}

/// Load balancing configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoadBalancingConfig {
    /// Load balancing algorithm
    pub algorithm: LoadBalancingAlgorithm,

    /// Whether to check health for load balancing
    pub health_check_enabled: bool,

    /// Whether to use sticky sessions
    pub sticky_sessions: bool,

    /// Whether to enable auto scaling
    pub auto_scaling: bool,

    /// Minimum number of nodes
    pub min_nodes: Option<usize>,

    /// Maximum number of nodes
    pub max_nodes: Option<usize>,

    /// Scale up threshold (load percentage)
    pub scale_up_threshold: Option<f32>,

    /// Scale down threshold (load percentage)
    pub scale_down_threshold: Option<f32>,
}

impl Default for LoadBalancingConfig {
    fn default() -> Self {
        Self {
            algorithm: LoadBalancingAlgorithm::LeastConnections,
            health_check_enabled: true,
            sticky_sessions: false,
            auto_scaling: false,
            min_nodes: Some(1),
            max_nodes: Some(10),
            scale_up_threshold: Some(0.75),
            scale_down_threshold: Some(0.25),
        }
    }
}

/// Load balancing algorithms
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum LoadBalancingAlgorithm {
    RoundRobin,
    LeastConnections,
    LeastResponseTime,
    WeightedRoundRobin,
    ResourceBased,
}

/// Disaster recovery configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DisasterRecoveryConfig {
    /// Backup interval
    #[serde(with = "humantime_serde")]
    pub backup_interval: Duration,

    /// Backup retention days
    pub backup_retention: u32,

    /// Whether auto restore is enabled
    pub auto_restore: bool,

    /// Remote backup location
    pub remote_backup_location: Option<String>,

    /// Encryption key for backups
    pub backup_encryption_key: Option<String>,
}

impl Default for DisasterRecoveryConfig {
    fn default() -> Self {
        Self {
            backup_interval: Duration::from_secs(3600), // 1 hour
            backup_retention: 30,                       // 30 days
            auto_restore: false,
            remote_backup_location: None,
            backup_encryption_key: None,
        }
    }
}