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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
//! Port negotiation utilities for PyWatt modules.
//!
//! This module provides an improved implementation of the port negotiation mechanism
//! used by PyWatt modules to request and allocate ports for HTTP servers.
use crate::ipc_types::{IpcPortNegotiation, IpcPortNegotiationResponse, ModuleToOrchestrator};
use std::net::{IpAddr, SocketAddr};
use std::str::FromStr;
use std::sync::{Mutex as StdMutex};
use tokio::io::{self, AsyncWriteExt};
use tokio::sync::oneshot;
use tokio::time::{Duration, timeout, Instant};
use tracing::{debug, error, info, warn, trace};
use uuid::Uuid;
use rand;
use serde_json;
/// Port for the negotiation service.
/// This is the initial port where modules connect to negotiate their actual working port.
pub const NEGOTIATION_PORT: u16 = 9998;
/// Maximum port in the range for port allocation
pub const MAX_PORT: u16 = 65535;
/// Minimum port in the dynamic range
pub const MIN_DYNAMIC_PORT: u16 = 49152;
/// Default timeout for port negotiation in seconds
const DEFAULT_PORT_NEGOTIATION_TIMEOUT_SECS: u64 = 3;
/// Initial timeout for port negotiation in seconds
const INITIAL_PORT_NEGOTIATION_TIMEOUT_SECS: u64 = 3;
/// Maximum timeout for port negotiation in seconds
const MAX_PORT_NEGOTIATION_TIMEOUT_SECS: u64 = 10;
/// Maximum retry attempts for port negotiation
const MAX_PORT_NEGOTIATION_RETRIES: u8 = 3;
/// Default port range for random port selection (if requested port is unavailable)
const DEFAULT_PORT_RANGE_START: u16 = 8000;
const DEFAULT_PORT_RANGE_END: u16 = 9000;
/// Circuit breaker threshold - after this many consecutive failures,
/// the circuit breaker opens and we use fallback mechanisms
const CIRCUIT_BREAKER_THRESHOLD: u8 = 5;
/// Circuit breaker reset time in seconds - after this time we'll try normal operation again
const CIRCUIT_BREAKER_RESET_SECS: u64 = 60; // 1 minute
/// Fallback port range for generating unique ports when orchestrator communication fails
const FALLBACK_PORT_RANGE_START: u16 = 10000;
const FALLBACK_PORT_RANGE_END: u16 = 11000;
// Global variables for port negotiation
lazy_static::lazy_static! {
static ref ALLOCATED_PORT: StdMutex<Option<u16>> = StdMutex::new(None);
// Global port negotiation response channel
static ref PORT_RESPONSE_CHANNEL: StdMutex<Option<oneshot::Sender<IpcPortNegotiationResponse>>> = {
StdMutex::new(None)
};
// Global port negotiation state
static ref PORT_NEGOTIATION_STATE: StdMutex<PortNegotiationState> = {
StdMutex::new(PortNegotiationState::default())
};
// Global mutex-wrapped stdout for sending messages to the orchestrator
static ref STDOUT_WRITER: tokio::sync::Mutex<io::Stdout> = tokio::sync::Mutex::new(io::stdout());
// Circuit breaker state
static ref CIRCUIT_BREAKER: StdMutex<CircuitBreakerState> = {
StdMutex::new(CircuitBreakerState::default())
};
}
/// Circuit breaker state
#[derive(Debug, Clone)]
#[derive(Default)]
enum CircuitBreakerStatus {
/// Circuit is closed, normal operation
#[default]
Closed,
/// Circuit is open, using fallback mechanisms
Open,
/// Circuit is half-open, testing if normal operation can resume
HalfOpen,
}
/// Circuit breaker state
#[derive(Debug, Clone)]
#[derive(Default)]
struct CircuitBreakerState {
/// Current status of the circuit breaker
status: CircuitBreakerStatus,
/// Number of consecutive failures
failure_count: u8,
/// Timestamp when the circuit was opened
opened_at: Option<Instant>,
/// Timestamp of the last attempt to close the circuit
last_attempt: Option<Instant>,
}
/// Port negotiation state
#[derive(Debug, Clone, Default)]
struct PortNegotiationState {
/// The current negotiation request ID, if any
current_request_id: Option<String>,
/// Number of retry attempts for the current request
retry_count: u8,
/// Timestamp of the last attempt
last_attempt_timestamp: Option<Instant>,
/// Whether a negotiation is in progress
in_progress: bool,
/// Current timeout duration in seconds
current_timeout_secs: u64,
/// Diagnostic information about failures
failure_diagnostics: Vec<String>,
}
/// Socket address validation error
#[derive(Debug, Clone)]
pub enum SocketAddressError {
/// Invalid format
InvalidFormat(String),
/// Invalid IP address
InvalidIpAddress(String),
/// Invalid port
InvalidPort(String),
}
impl std::fmt::Display for SocketAddressError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SocketAddressError::InvalidFormat(msg) => write!(f, "Invalid socket address format: {}", msg),
SocketAddressError::InvalidIpAddress(msg) => write!(f, "Invalid IP address: {}", msg),
SocketAddressError::InvalidPort(msg) => write!(f, "Invalid port: {}", msg),
}
}
}
/// Result of a port negotiation attempt
#[derive(Debug, Clone)]
pub enum PortNegotiationResult {
/// Port was successfully allocated
Success(u16),
/// Port negotiation failed
Failure {
/// Error message
message: String,
/// Detailed diagnostic information
diagnostics: Vec<String>,
},
/// Port negotiation timed out
Timeout {
/// Number of seconds that elapsed before timeout
elapsed_secs: u64,
/// Request ID
request_id: String,
},
/// Using fallback port due to persistent failures
UsingFallback {
/// Fallback port
port: u16,
/// Reason for using fallback
reason: String,
},
}
impl PortNegotiationResult {
/// Get a human-readable message describing the result
pub fn to_message(&self) -> String {
match self {
Self::Success(port) => format!("Port {} successfully allocated", port),
Self::Failure { message, diagnostics } => {
if diagnostics.is_empty() {
message.clone()
} else {
format!("{} ({})", message, diagnostics.join(", "))
}
},
Self::Timeout { elapsed_secs, request_id } => {
format!("Port negotiation timed out after {}s (request_id={})", elapsed_secs, request_id)
},
Self::UsingFallback { port, reason } => {
format!("Using fallback port {}: {}", port, reason)
},
}
}
/// Get the allocated port if successful or using fallback
pub fn port(&self) -> Option<u16> {
match self {
Self::Success(port) => Some(*port),
Self::UsingFallback { port, .. } => Some(*port),
_ => None,
}
}
/// Whether the result represents a successful port allocation
pub fn is_success(&self) -> bool {
matches!(self, Self::Success(_) | Self::UsingFallback { .. })
}
/// Whether the result represents a failure
pub fn is_failure(&self) -> bool {
matches!(self, Self::Failure { .. } | Self::Timeout { .. })
}
/// Whether the result is using a fallback port
pub fn is_fallback(&self) -> bool {
matches!(self, Self::UsingFallback { .. })
}
}
/// Port negotiation manager
pub struct PortNegotiationManager {
/// Configuration for timeouts and retries
config: PortNegotiationConfig,
}
/// Configuration for port negotiation
#[derive(Debug, Clone)]
pub struct PortNegotiationConfig {
/// Initial timeout in seconds
pub initial_timeout_secs: u64,
/// Maximum timeout in seconds
pub max_timeout_secs: u64,
/// Maximum number of retry attempts
pub max_retries: u8,
/// Whether to use fallback ports when negotiation fails
pub use_fallback: bool,
/// Custom fallback port to use
pub custom_fallback_port: Option<u16>,
/// Port range start for random port selection
pub port_range_start: u16,
/// Port range end for random port selection
pub port_range_end: u16,
}
impl Default for PortNegotiationConfig {
fn default() -> Self {
Self {
initial_timeout_secs: INITIAL_PORT_NEGOTIATION_TIMEOUT_SECS,
max_timeout_secs: MAX_PORT_NEGOTIATION_TIMEOUT_SECS,
max_retries: MAX_PORT_NEGOTIATION_RETRIES,
use_fallback: true,
custom_fallback_port: None,
port_range_start: DEFAULT_PORT_RANGE_START,
port_range_end: DEFAULT_PORT_RANGE_END,
}
}
}
impl PortNegotiationManager {
/// Create a new port negotiation manager with default configuration
pub fn new() -> Self {
Self {
config: PortNegotiationConfig::default(),
}
}
/// Create a new port negotiation manager with custom configuration
pub fn with_config(config: PortNegotiationConfig) -> Self {
Self { config }
}
/// Validate a socket address string
pub fn validate_socket_address(addr_str: &str) -> Result<SocketAddr, SocketAddressError> {
// Try to parse as a socket address
match SocketAddr::from_str(addr_str) {
Ok(addr) => {
// Validate IP address
match addr.ip() {
IpAddr::V4(ipv4) => {
if ipv4.is_unspecified() || ipv4.is_broadcast() {
return Err(SocketAddressError::InvalidIpAddress(
"IP address cannot be unspecified (0.0.0.0) or broadcast".to_string()
));
}
},
IpAddr::V6(ipv6) => {
if ipv6.is_unspecified() {
return Err(SocketAddressError::InvalidIpAddress(
"IP address cannot be unspecified (::)".to_string()
));
}
}
}
// Validate port
if addr.port() == 0 {
return Err(SocketAddressError::InvalidPort("Port cannot be 0".to_string()));
}
Ok(addr)
},
Err(e) => Err(SocketAddressError::InvalidFormat(e.to_string())),
}
}
/// Format a socket address from an IP and port
pub fn format_socket_address(ip: &str, port: u16) -> Result<String, SocketAddressError> {
// Parse the IP address
let ip_addr = match IpAddr::from_str(ip) {
Ok(addr) => addr,
Err(e) => return Err(SocketAddressError::InvalidIpAddress(e.to_string())),
};
// Validate the port
if port == 0 {
return Err(SocketAddressError::InvalidPort("Port cannot be 0".to_string()));
}
// Format the socket address
let socket_addr = SocketAddr::new(ip_addr, port);
Ok(socket_addr.to_string())
}
/// Get the circuit breaker state
fn check_circuit_breaker(&self) -> Result<CircuitBreakerStatus, String> {
let mut breaker = CIRCUIT_BREAKER.lock().map_err(|e| format!("Failed to lock CIRCUIT_BREAKER: {}", e))?;
match breaker.status {
CircuitBreakerStatus::Closed => {
// Normal operation
Ok(CircuitBreakerStatus::Closed)
},
CircuitBreakerStatus::Open => {
// Check if it's time to try closing the circuit
if let Some(opened_at) = breaker.opened_at {
let elapsed = opened_at.elapsed();
if elapsed >= Duration::from_secs(CIRCUIT_BREAKER_RESET_SECS) {
// Try half-open state
breaker.status = CircuitBreakerStatus::HalfOpen;
breaker.last_attempt = Some(Instant::now());
info!("Circuit breaker transitioning to half-open state after {} seconds", elapsed.as_secs());
Ok(CircuitBreakerStatus::HalfOpen)
} else {
// Still open
let remaining = CIRCUIT_BREAKER_RESET_SECS.saturating_sub(elapsed.as_secs());
debug!("Circuit breaker still open, {} seconds remaining until retry", remaining);
Ok(CircuitBreakerStatus::Open)
}
} else {
// No opened_at timestamp, reset to closed
breaker.status = CircuitBreakerStatus::Closed;
breaker.failure_count = 0;
Ok(CircuitBreakerStatus::Closed)
}
},
CircuitBreakerStatus::HalfOpen => {
// We're testing if normal operation can resume
Ok(CircuitBreakerStatus::HalfOpen)
}
}
}
/// Record a success in the circuit breaker
fn record_success(&self) -> Result<(), String> {
let mut breaker = CIRCUIT_BREAKER.lock().map_err(|e| format!("Failed to lock CIRCUIT_BREAKER: {}", e))?;
match breaker.status {
CircuitBreakerStatus::Closed => {
// Reset failure count on success
breaker.failure_count = 0;
},
CircuitBreakerStatus::HalfOpen => {
// If we succeed in half-open state, close the circuit
info!("Circuit breaker closed after successful operation in half-open state");
breaker.status = CircuitBreakerStatus::Closed;
breaker.failure_count = 0;
breaker.opened_at = None;
},
CircuitBreakerStatus::Open => {
// This shouldn't happen, but just in case
breaker.status = CircuitBreakerStatus::Closed;
breaker.failure_count = 0;
breaker.opened_at = None;
}
}
Ok(())
}
/// Record a failure in the circuit breaker
fn record_failure(&self) -> Result<CircuitBreakerStatus, String> {
let mut breaker = CIRCUIT_BREAKER.lock().map_err(|e| format!("Failed to lock CIRCUIT_BREAKER: {}", e))?;
match breaker.status {
CircuitBreakerStatus::Closed => {
// Increment failure count
breaker.failure_count += 1;
// Check if we've reached the threshold
if breaker.failure_count >= CIRCUIT_BREAKER_THRESHOLD {
// Open the circuit
breaker.status = CircuitBreakerStatus::Open;
breaker.opened_at = Some(Instant::now());
warn!("Circuit breaker opened after {} consecutive failures", breaker.failure_count);
}
},
CircuitBreakerStatus::HalfOpen => {
// If we fail in half-open state, go back to open
breaker.status = CircuitBreakerStatus::Open;
breaker.opened_at = Some(Instant::now());
warn!("Circuit breaker reopened after failure in half-open state");
},
CircuitBreakerStatus::Open => {
// Already open, nothing to do
}
}
Ok(breaker.status.clone())
}
/// Request a port from the orchestrator with detailed diagnostics
///
/// # Arguments
/// * `specific_port` - Optional specific port to request
/// * `timeout_secs` - Optional timeout in seconds (defaults to configured value)
///
/// # Returns
/// A detailed result of the port negotiation
pub async fn request_port_with_diagnostics(
&self,
specific_port: Option<u16>,
timeout_secs: Option<u64>
) -> PortNegotiationResult {
// Check if we already have an allocated port
{
let port_guard = match ALLOCATED_PORT.lock() {
Ok(guard) => guard,
Err(e) => {
return PortNegotiationResult::Failure {
message: format!("Failed to lock ALLOCATED_PORT: {}", e),
diagnostics: vec![format!("Lock error: {}", e)],
};
}
};
if let Some(port) = *port_guard {
info!("Port already allocated: {}", port);
return PortNegotiationResult::Success(port);
}
}
// Check circuit breaker state
let cb_status = match self.check_circuit_breaker() {
Ok(status) => status,
Err(e) => {
return PortNegotiationResult::Failure {
message: format!("Failed to check circuit breaker state: {}", e),
diagnostics: vec![format!("Circuit breaker error: {}", e)],
};
}
};
match cb_status {
CircuitBreakerStatus::Open => {
// Circuit is open, use fallback port
if self.config.use_fallback {
let fallback_port = if let Some(custom) = self.config.custom_fallback_port {
custom
} else {
// Generate a random port in the fallback range
let range = FALLBACK_PORT_RANGE_END - FALLBACK_PORT_RANGE_START;
FALLBACK_PORT_RANGE_START + (rand::random::<u16>() % range)
};
// Store the fallback port
{
let mut port_guard = match ALLOCATED_PORT.lock() {
Ok(guard) => guard,
Err(e) => {
return PortNegotiationResult::Failure {
message: format!("Failed to lock ALLOCATED_PORT: {}", e),
diagnostics: vec![
format!("Circuit breaker status: {:?}", cb_status),
format!("Using fallback port: {}", fallback_port),
format!("Lock error: {}", e)
],
};
}
};
*port_guard = Some(fallback_port);
}
warn!("Using fallback port {} due to open circuit breaker", fallback_port);
return PortNegotiationResult::UsingFallback {
port: fallback_port,
reason: "Circuit breaker is open due to persistent failures".to_string(),
};
} else {
warn!("Circuit breaker is open but fallback ports are disabled");
}
},
CircuitBreakerStatus::HalfOpen => {
debug!("Circuit breaker is half-open, attempting normal operation");
},
CircuitBreakerStatus::Closed => {
debug!("Circuit breaker is closed, proceeding with normal operation");
}
}
// Set timeout based on retry count and configuration
let timeout_duration = {
let state = match PORT_NEGOTIATION_STATE.lock() {
Ok(guard) => guard,
Err(e) => {
return PortNegotiationResult::Failure {
message: format!("Failed to lock PORT_NEGOTIATION_STATE: {}", e),
diagnostics: vec![format!("Lock error: {}", e)],
};
}
};
let timeout_secs = timeout_secs.unwrap_or_else(|| {
if state.retry_count == 0 {
self.config.initial_timeout_secs
} else {
// Linear backoff instead of exponential to avoid huge timeouts
let backoff = self.config.initial_timeout_secs + (state.retry_count as u64);
std::cmp::min(backoff, self.config.max_timeout_secs)
}
});
Duration::from_secs(timeout_secs)
};
// Generate a unique request ID
let request_id = Uuid::new_v4().to_string();
// Update negotiation state
{
let mut state = match PORT_NEGOTIATION_STATE.lock() {
Ok(guard) => guard,
Err(e) => {
return PortNegotiationResult::Failure {
message: format!("Failed to lock PORT_NEGOTIATION_STATE: {}", e),
diagnostics: vec![format!("Lock error: {}", e)],
};
}
};
state.current_request_id = Some(request_id.clone());
state.last_attempt_timestamp = Some(Instant::now());
state.in_progress = true;
state.current_timeout_secs = timeout_duration.as_secs();
// Clear previous diagnostics if this is a new request (not a retry)
if state.retry_count == 0 {
state.failure_diagnostics.clear();
}
debug!("Starting port negotiation (request_id={}, specific_port={:?}, timeout={}s, attempt={}/{})",
request_id, specific_port, timeout_duration.as_secs(),
state.retry_count + 1, self.config.max_retries);
}
// Prepare the port negotiation request
let request = IpcPortNegotiation {
request_id: request_id.clone(),
specific_port,
};
// Create a channel for receiving the response
let (tx, rx) = oneshot::channel();
// Store the sender for later use
{
let mut port_sender = match PORT_RESPONSE_CHANNEL.lock() {
Ok(guard) => guard,
Err(e) => {
return PortNegotiationResult::Failure {
message: format!("Failed to lock PORT_RESPONSE_CHANNEL: {}", e),
diagnostics: vec![format!("Lock error: {}", e)],
};
}
};
*port_sender = Some(tx);
}
// Send the request
let start_time = Instant::now();
match self.send_port_request(&request).await {
Ok(_) => {
trace!("Port request sent successfully, waiting for response (timeout={}s)", timeout_duration.as_secs());
},
Err(e) => {
// Record failure in circuit breaker
let _ = self.record_failure();
// Update negotiation state
{
let mut state = match PORT_NEGOTIATION_STATE.lock() {
Ok(guard) => guard,
Err(lock_err) => {
return PortNegotiationResult::Failure {
message: format!("Failed to lock PORT_NEGOTIATION_STATE: {}", lock_err),
diagnostics: vec![
format!("Request sending error: {}", e),
format!("Lock error: {}", lock_err)
],
};
}
};
state.in_progress = false;
state.failure_diagnostics.push(format!("Failed to send request: {}", e));
}
return PortNegotiationResult::Failure {
message: format!("Failed to send port request: {}", e),
diagnostics: vec![format!("Request sending error: {}", e)],
};
}
}
// Wait for the response with a timeout
match timeout(timeout_duration, rx).await {
Ok(Ok(response)) => {
let elapsed = start_time.elapsed();
debug!("Received port response after {}ms", elapsed.as_millis());
// Update negotiation state
{
let mut state = match PORT_NEGOTIATION_STATE.lock() {
Ok(guard) => guard,
Err(e) => {
return PortNegotiationResult::Failure {
message: format!("Failed to lock PORT_NEGOTIATION_STATE: {}", e),
diagnostics: vec![format!("Lock error: {}", e)],
};
}
};
state.in_progress = false;
}
if response.success {
info!("Port negotiation successful (request_id={}, port={}, took={}ms)",
request_id, response.port, elapsed.as_millis());
// Record success in circuit breaker
let _ = self.record_success();
// Store the allocated port
{
let mut port_guard = match ALLOCATED_PORT.lock() {
Ok(guard) => guard,
Err(e) => {
return PortNegotiationResult::Failure {
message: format!("Failed to lock ALLOCATED_PORT: {}", e),
diagnostics: vec![format!("Lock error: {}", e)],
};
}
};
*port_guard = Some(response.port);
}
PortNegotiationResult::Success(response.port)
} else {
let error_msg = response.error_message.unwrap_or_else(|| "Unknown error".to_string());
error!("Port negotiation failed: {} (request_id={})", error_msg, request_id);
// Record failure in circuit breaker
let _ = self.record_failure();
// Update diagnostics
{
let mut state = match PORT_NEGOTIATION_STATE.lock() {
Ok(guard) => guard,
Err(e) => {
return PortNegotiationResult::Failure {
message: format!("Failed to lock PORT_NEGOTIATION_STATE after receiving error: {}", e),
diagnostics: vec![
format!("Orchestrator error: {}", error_msg),
format!("Lock error: {}", e)
],
};
}
};
state.failure_diagnostics.push(format!("Orchestrator error: {}", error_msg));
}
PortNegotiationResult::Failure {
message: format!("Port negotiation failed: {}", error_msg),
diagnostics: vec![format!("Orchestrator error: {}", error_msg)],
}
}
},
Ok(Err(_)) => {
// The sender was dropped
error!("Port negotiation sender was dropped (request_id={})", request_id);
// Record failure in circuit breaker
let _ = self.record_failure();
// Update negotiation state
{
let mut state = match PORT_NEGOTIATION_STATE.lock() {
Ok(guard) => guard,
Err(e) => {
return PortNegotiationResult::Failure {
message: format!("Failed to lock PORT_NEGOTIATION_STATE: {}", e),
diagnostics: vec![
"Response channel was closed".to_string(),
format!("Lock error: {}", e)
],
};
}
};
state.in_progress = false;
state.failure_diagnostics.push("Response channel was closed".to_string());
}
PortNegotiationResult::Failure {
message: "Port negotiation failed: response channel was closed".to_string(),
diagnostics: vec!["Response channel was closed".to_string()],
}
},
Err(_) => {
// Timeout occurred
let elapsed = start_time.elapsed();
warn!("Port negotiation timed out after {}ms (request_id={})",
elapsed.as_millis(), request_id);
// Record failure in circuit breaker
let cb_status = match self.record_failure() {
Ok(status) => status,
Err(e) => {
return PortNegotiationResult::Failure {
message: format!("Failed to update circuit breaker after timeout: {}", e),
diagnostics: vec![
format!("Timeout after {}ms", elapsed.as_millis()),
format!("Circuit breaker error: {}", e)
],
};
}
};
// Update negotiation state for potential retry
let should_retry = {
let mut state = match PORT_NEGOTIATION_STATE.lock() {
Ok(guard) => guard,
Err(e) => {
return PortNegotiationResult::Failure {
message: format!("Failed to lock PORT_NEGOTIATION_STATE after timeout: {}", e),
diagnostics: vec![
format!("Timeout after {}ms", elapsed.as_millis()),
format!("Lock error: {}", e)
],
};
}
};
state.retry_count += 1;
state.failure_diagnostics.push(format!("Timeout after {}ms", elapsed.as_millis()));
let should_retry = state.retry_count < self.config.max_retries;
if !should_retry {
state.in_progress = false;
}
should_retry
};
if should_retry {
// We'll attempt a retry with a random port in the configured range
let random_port = if specific_port.is_none() {
Some(self.config.port_range_start +
(rand::random::<u16>() % (self.config.port_range_end - self.config.port_range_start)))
} else {
None // Keep the specific port if it was requested
};
info!("Retrying port negotiation with {}port (attempt {}/{})",
random_port.map_or("same ".to_string(), |p| format!("random port {} ", p)),
{
let state = PORT_NEGOTIATION_STATE.lock().unwrap();
state.retry_count + 1
},
self.config.max_retries);
// Recursive call for retry
// Must use Box::pin to avoid infinitely sized future with recursive async fn
Box::pin(self.request_port_with_diagnostics(random_port, None)).await
} else if matches!(cb_status, CircuitBreakerStatus::Open) && self.config.use_fallback {
// Circuit breaker is open and we've exhausted retries, use fallback port
let fallback_port = if let Some(custom) = self.config.custom_fallback_port {
custom
} else {
// Generate a random port in the fallback range
let range = FALLBACK_PORT_RANGE_END - FALLBACK_PORT_RANGE_START;
FALLBACK_PORT_RANGE_START + (rand::random::<u16>() % range)
};
warn!("Max retries ({}) reached and circuit breaker open, using fallback port {}",
self.config.max_retries, fallback_port);
// Store the fallback port
{
let mut port_guard = match ALLOCATED_PORT.lock() {
Ok(guard) => guard,
Err(e) => {
return PortNegotiationResult::Failure {
message: format!("Failed to lock ALLOCATED_PORT: {}", e),
diagnostics: vec![
format!("Circuit breaker status: {:?}", cb_status),
format!("Using fallback port: {}", fallback_port),
format!("Lock error: {}", e)
],
};
}
};
*port_guard = Some(fallback_port);
}
let _diagnostics = {
let state = match PORT_NEGOTIATION_STATE.lock() {
Ok(guard) => guard,
Err(_e) => {
return PortNegotiationResult::Timeout {
elapsed_secs: elapsed.as_secs(),
request_id,
};
}
};
state.failure_diagnostics.clone()
};
PortNegotiationResult::UsingFallback {
port: fallback_port,
reason: format!("Max retries ({}) reached and circuit breaker open", self.config.max_retries),
}
} else {
// No retry and no fallback, just return timeout error
// Get diagnostics from state
let _diagnostics = {
let state = match PORT_NEGOTIATION_STATE.lock() {
Ok(guard) => guard,
Err(_e) => {
return PortNegotiationResult::Timeout {
elapsed_secs: elapsed.as_secs(),
request_id,
};
}
};
state.failure_diagnostics.clone()
};
PortNegotiationResult::Timeout {
elapsed_secs: elapsed.as_secs(),
request_id,
}
}
}
}
}
/// Request a port from the orchestrator
///
/// # Arguments
/// * `specific_port` - Optional specific port to request
/// * `timeout_secs` - Optional timeout in seconds (defaults to 5)
///
/// # Returns
/// A result containing the allocated port or an error
pub async fn request_port(
&self,
specific_port: Option<u16>,
timeout_secs: Option<u64>
) -> Result<u16, String> {
let result = self.request_port_with_diagnostics(specific_port, timeout_secs).await;
// Log appropriate message based on result type
match &result {
PortNegotiationResult::Success(_) => {
debug!("{}", result.to_message());
},
PortNegotiationResult::Failure { message, diagnostics } => {
error!("Port negotiation failed: {} (diagnostics: {:?})", message, diagnostics);
},
PortNegotiationResult::Timeout { .. } => {
warn!("{}", result.to_message());
},
PortNegotiationResult::UsingFallback { .. } => {
warn!("{}", result.to_message());
}
}
// Convert to standard Result type
if let Some(port) = result.port() {
Ok(port)
} else {
Err(result.to_message())
}
}
/// Send a port negotiation request to the orchestrator
async fn send_port_request(&self, request: &IpcPortNegotiation) -> Result<(), String> {
let message = ModuleToOrchestrator::PortRequest(request.clone());
let json = serde_json::to_string(&message).map_err(|e| format!("Failed to serialize port request: {}", e))?;
info!("Sending port negotiation request to orchestrator: {}", json);
// Send the message to the orchestrator
let mut stdout_guard = STDOUT_WRITER.lock().await;
let start = Instant::now();
if let Err(e) = stdout_guard.write_all(json.as_bytes()).await {
error!("Failed to write port request to stdout: {}", e);
return Err(format!("Failed to write port request to stdout: {}", e));
}
if let Err(e) = stdout_guard.write_all(b"\n").await {
error!("Failed to write newline after port request: {}", e);
return Err(format!("Failed to write newline after port request: {}", e));
}
if let Err(e) = stdout_guard.flush().await {
error!("Failed to flush stdout after port request: {}", e);
return Err(format!("Failed to flush stdout after port request: {}", e));
}
let elapsed = start.elapsed();
info!("Port negotiation request sent successfully (took {}ms)", elapsed.as_millis());
Ok(())
}
/// Process a port negotiation response from the orchestrator
pub fn process_port_response(response: IpcPortNegotiationResponse) -> Result<(), String> {
info!("Processing port negotiation response: success={}, port={}, request_id={}",
response.success, response.port, response.request_id);
// Check if we have a pending request
let current_request_id = {
let state = PORT_NEGOTIATION_STATE.lock().map_err(|e| format!("Failed to lock PORT_NEGOTIATION_STATE: {}", e))?;
state.current_request_id.clone()
};
// Validate request ID if we have an ongoing negotiation
if let Some(req_id) = current_request_id {
if req_id != response.request_id && response.request_id != "auto-assigned" {
warn!("Received port response with mismatched request ID: expected {}, got {}",
req_id, response.request_id);
} else {
info!("Port response request ID matches: {}", req_id);
}
} else {
warn!("Received port response but no pending request found");
}
// Validate the port if successful
if response.success {
if response.port == 0 {
warn!("Received invalid port 0 in successful port response");
// Try to send a modified response through the channel
let modified_response = IpcPortNegotiationResponse {
success: false,
error_message: Some("Orchestrator returned invalid port 0".to_string()),
..response.clone()
};
let port_sender = {
let mut guard = PORT_RESPONSE_CHANNEL.lock().map_err(|e| format!("Failed to lock PORT_RESPONSE_CHANNEL: {}", e))?;
guard.take()
};
if let Some(sender) = port_sender {
if sender.send(modified_response).is_err() {
warn!("Failed to send modified port response - receiver dropped");
}
}
return Err("Received invalid port 0 in successful port response".to_string());
}
let mut port_guard = ALLOCATED_PORT.lock().map_err(|e| format!("Failed to lock ALLOCATED_PORT: {}", e))?;
*port_guard = Some(response.port);
info!("Successfully allocated port: {}", response.port);
} else {
warn!("Port allocation failed: {:?}", response.error_message);
}
// Send the response through the oneshot channel if available
let port_sender = {
let mut guard = PORT_RESPONSE_CHANNEL.lock().map_err(|e| format!("Failed to lock PORT_RESPONSE_CHANNEL: {}", e))?;
guard.take()
};
if let Some(sender) = port_sender {
if sender.send(response.clone()).is_err() {
warn!("Failed to send port response - receiver dropped");
} else {
info!("Port response sent to waiting receiver successfully");
}
} else {
warn!("No waiting receiver for port response - this indicates a synchronization issue");
}
Ok(())
}
/// Get the currently allocated port, if any
pub fn get_allocated_port() -> Option<u16> {
ALLOCATED_PORT.lock().ok().and_then(|guard| *guard)
}
/// Get diagnostic information about port negotiation
pub fn get_diagnostics() -> Result<Vec<String>, String> {
let state = PORT_NEGOTIATION_STATE.lock().map_err(|e| format!("Failed to lock PORT_NEGOTIATION_STATE: {}", e))?;
Ok(state.failure_diagnostics.clone())
}
/// Get the current port negotiation state for diagnostics
pub fn get_negotiation_state() -> Result<String, String> {
let state = PORT_NEGOTIATION_STATE.lock().map_err(|e| format!("Failed to lock PORT_NEGOTIATION_STATE: {}", e))?;
let cb_state = CIRCUIT_BREAKER.lock().map_err(|e| format!("Failed to lock CIRCUIT_BREAKER: {}", e))?;
let cb_status = match cb_state.status {
CircuitBreakerStatus::Closed => "closed",
CircuitBreakerStatus::Open => "open",
CircuitBreakerStatus::HalfOpen => "half-open",
};
let state_json = serde_json::json!({
"in_progress": state.in_progress,
"current_request_id": state.current_request_id,
"retry_count": state.retry_count,
"last_attempt": state.last_attempt_timestamp.map(|t| t.elapsed().as_secs()),
"timeout_secs": state.current_timeout_secs,
"circuit_breaker": {
"status": cb_status,
"failure_count": cb_state.failure_count,
"opened_at": cb_state.opened_at.map(|t| t.elapsed().as_secs()),
},
"allocated_port": Self::get_allocated_port(),
});
Ok(state_json.to_string())
}
/// Reset the port negotiation state (mainly for testing)
#[cfg(test)]
pub fn reset() {
if let Ok(mut port_guard) = ALLOCATED_PORT.lock() {
*port_guard = None;
}
if let Ok(mut state) = PORT_NEGOTIATION_STATE.lock() {
*state = PortNegotiationState::default();
}
if let Ok(mut breaker) = CIRCUIT_BREAKER.lock() {
*breaker = CircuitBreakerState::default();
}
}
/// Try to recover the port negotiation system
///
/// This method attempts to recover the port negotiation system by:
/// 1. Resetting internal state
/// 2. Trying to obtain a port from the orchestrator
/// 3. Falling back to a random port if orchestrator is unavailable
pub async fn try_recover(&self) -> Result<u16, String> {
info!("Attempting to recover port negotiation system");
// Reset internal state
{
let mut state = PORT_NEGOTIATION_STATE.lock().map_err(|e| format!("Failed to lock PORT_NEGOTIATION_STATE: {}", e))?;
state.current_request_id = None;
state.retry_count = 0;
state.last_attempt_timestamp = None;
state.in_progress = false;
state.failure_diagnostics.clear();
}
// Try to reset circuit breaker to half-open
{
let mut breaker = CIRCUIT_BREAKER.lock().map_err(|e| format!("Failed to lock CIRCUIT_BREAKER: {}", e))?;
if matches!(breaker.status, CircuitBreakerStatus::Open) {
breaker.status = CircuitBreakerStatus::HalfOpen;
breaker.last_attempt = Some(Instant::now());
info!("Reset circuit breaker to half-open state for recovery attempt");
}
}
// Try to get a random port from the orchestrator
let random_port = self.config.port_range_start +
(rand::random::<u16>() % (self.config.port_range_end - self.config.port_range_start));
info!("Requesting random port {} for recovery", random_port);
// Use a shorter timeout for recovery
let recovery_timeout = std::cmp::min(self.config.initial_timeout_secs, 3);
match self.request_port(Some(random_port), Some(recovery_timeout)).await {
Ok(port) => {
info!("Recovery successful, allocated port: {}", port);
Ok(port)
},
Err(e) => {
warn!("Recovery attempt failed: {}", e);
if self.config.use_fallback {
// Use fallback port
let fallback_port = if let Some(custom) = self.config.custom_fallback_port {
custom
} else {
// Generate a random port in the fallback range
let range = FALLBACK_PORT_RANGE_END - FALLBACK_PORT_RANGE_START;
FALLBACK_PORT_RANGE_START + (rand::random::<u16>() % range)
};
// Store the fallback port
{
let mut port_guard = match ALLOCATED_PORT.lock() {
Ok(guard) => guard,
Err(e) => {
return Err(format!("Failed to lock ALLOCATED_PORT: {}", e));
}
};
*port_guard = Some(fallback_port);
}
warn!("Using fallback port {} after failed recovery attempt", fallback_port);
Ok(fallback_port)
} else {
Err(format!("Recovery failed and fallback ports are disabled: {}", e))
}
}
}
}
/// Test port negotiation functionality
///
/// This is a diagnostic function to test the port negotiation system
pub async fn test_port_negotiation() -> Result<u16, String> {
info!("Starting port negotiation diagnostic test");
let manager = PortNegotiationManager::new();
// Test basic port request
info!("Testing basic port request (no specific port)");
match manager.request_port_with_diagnostics(None, Some(5)).await {
PortNegotiationResult::Success(port) => {
info!("Port negotiation test successful: allocated port {}", port);
Ok(port)
},
PortNegotiationResult::Failure { message, diagnostics } => {
error!("Port negotiation test failed: {}", message);
error!("Diagnostics: {:?}", diagnostics);
Err(format!("Test failed: {}", message))
},
PortNegotiationResult::Timeout { elapsed_secs, request_id } => {
error!("Port negotiation test timed out after {}s (request_id: {})", elapsed_secs, request_id);
Err(format!("Test timed out after {}s", elapsed_secs))
},
PortNegotiationResult::UsingFallback { port, reason } => {
warn!("Port negotiation test using fallback: port {}, reason: {}", port, reason);
Ok(port)
}
}
}
/// Get comprehensive diagnostic information
pub fn get_comprehensive_diagnostics() -> String {
let mut diagnostics = Vec::new();
// Port negotiation state
match Self::get_negotiation_state() {
Ok(state) => {
diagnostics.push(format!("Port Negotiation State: {}", state));
},
Err(e) => {
diagnostics.push(format!("Failed to get port negotiation state: {}", e));
}
}
// Circuit breaker state
if let Ok(cb_state) = CIRCUIT_BREAKER.lock() {
let status = match cb_state.status {
CircuitBreakerStatus::Closed => "closed",
CircuitBreakerStatus::Open => "open",
CircuitBreakerStatus::HalfOpen => "half-open",
};
diagnostics.push(format!("Circuit Breaker: status={}, failures={}", status, cb_state.failure_count));
}
// Allocated port
if let Some(port) = Self::get_allocated_port() {
diagnostics.push(format!("Currently allocated port: {}", port));
} else {
diagnostics.push("No port currently allocated".to_string());
}
// Failure diagnostics
match Self::get_diagnostics() {
Ok(failures) => {
if !failures.is_empty() {
diagnostics.push(format!("Recent failures: {:?}", failures));
}
},
Err(e) => {
diagnostics.push(format!("Failed to get failure diagnostics: {}", e));
}
}
diagnostics.join("\n")
}
}
impl Default for PortNegotiationManager {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use tokio::test;
#[test]
async fn test_socket_address_validation() {
// Valid addresses
assert!(PortNegotiationManager::validate_socket_address("127.0.0.1:8080").is_ok());
assert!(PortNegotiationManager::validate_socket_address("192.168.1.1:443").is_ok());
assert!(PortNegotiationManager::validate_socket_address("[::1]:8080").is_ok());
// Invalid addresses
assert!(PortNegotiationManager::validate_socket_address("localhost:8080").is_err());
assert!(PortNegotiationManager::validate_socket_address("127.0.0.1").is_err());
assert!(PortNegotiationManager::validate_socket_address("127.0.0.1:").is_err());
assert!(PortNegotiationManager::validate_socket_address("127.0.0.1:0").is_err());
assert!(PortNegotiationManager::validate_socket_address("0.0.0.0:8080").is_err());
}
#[test]
async fn test_format_socket_address() {
// Valid combinations
assert_eq!(PortNegotiationManager::format_socket_address("127.0.0.1", 8080).unwrap(), "127.0.0.1:8080");
assert_eq!(PortNegotiationManager::format_socket_address("192.168.1.1", 443).unwrap(), "192.168.1.1:443");
// Invalid combinations
assert!(PortNegotiationManager::format_socket_address("localhost", 8080).is_err());
assert!(PortNegotiationManager::format_socket_address("127.0.0.1", 0).is_err());
}
// More tests would be added here for circuit breaker, retry logic, etc.
}