hydro_lang 0.16.0

A Rust framework for correct and performant distributed systems
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
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
#![allow(
    unused,
    reason = "unused in trybuild but the __staged version is needed"
)]
#![allow(missing_docs, reason = "used internally")]

use std::time::Duration;

use futures::{SinkExt, Stream, StreamExt};
use sinktools::lazy::{LazySink, LazySource};
use sinktools::lazy_sink_source::LazySinkSource;
use stageleft::{QuotedWithContext, q};
use tokio::net::TcpStream;
use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf};
use tokio_util::codec::{FramedRead, FramedWrite, LengthDelimitedCodec};
use tracing::{Instrument, debug, instrument, span, trace, trace_span};

pub use super::deploy_runtime_containerized::{
    CHANNEL_MAGIC, CHANNEL_MUX_PORT, CHANNEL_PROTOCOL_VERSION, ChannelHandshake, ChannelMagic,
    ChannelMux, ChannelProtocolVersion, SocketIdent, cluster_ids, get_or_init_channel_mux,
    send_handshake,
};
use crate::location::dynamic::LocationId;
use crate::location::member_id::TaglessMemberId;
use crate::location::{LocationKey, MembershipEvent};

pub fn deploy_containerized_o2o(
    target_task_family: &str,
    channel_name: &str,
) -> (syn::Expr, syn::Expr) {
    (
        q!(LazySink::<_, _, _, bytes::Bytes>::new(move || Box::pin(
            async move {
                let channel_name = channel_name;
                let target_task_family = target_task_family;
                let task_id = self::resolve_task_family_to_task_id(target_task_family).await;
                let ip = self::resolve_task_ip(&task_id).await;
                let target = format!("{}:{}", ip, self::CHANNEL_MUX_PORT);
                debug!(name: "connecting", %target, %target_task_family, %task_id, %channel_name);

                let stream = TcpStream::connect(&target).await?;
                let mut sink = FramedWrite::new(stream, LengthDelimitedCodec::new());

                self::send_handshake(&mut sink, channel_name, None).await?;

                Result::<_, std::io::Error>::Ok(sink)
            }
        )))
        .splice_untyped_ctx(&()),
        q!(LazySource::new(move || Box::pin(async move {
            let channel_name = channel_name;
            let mux = self::get_or_init_channel_mux();
            let mut rx = mux.register(channel_name.to_owned());

            let (_sender_id, source) = rx.recv().await.ok_or_else(|| {
                std::io::Error::new(std::io::ErrorKind::ConnectionReset, "channel mux closed")
            })?;

            debug!(name: "o2o_channel_connected", %channel_name);

            Result::<_, std::io::Error>::Ok(source)
        })))
        .splice_untyped_ctx(&()),
    )
}

pub fn deploy_containerized_o2m(channel_name: &str) -> (syn::Expr, syn::Expr) {
    (
        q!(sinktools::demux_map_lazy::<_, _, _, _>(
            move |key: &TaglessMemberId| {
                let key = key.clone();
                let channel_name = channel_name.to_owned();

                LazySink::<_, _, _, bytes::Bytes>::new(move || {
                    Box::pin(async move {
                        let task_id = key.get_container_name();
                        let ip = self::resolve_task_ip(task_id).await;
                        let target = format!("{}:{}", ip, self::CHANNEL_MUX_PORT);
                        debug!(name: "connecting", %target, %task_id, channel_name = %channel_name);

                        let stream = TcpStream::connect(&target).await?;
                        let mut sink = FramedWrite::new(stream, LengthDelimitedCodec::new());

                        self::send_handshake(&mut sink, &channel_name, None).await?;

                        Result::<_, std::io::Error>::Ok(sink)
                    })
                })
            }
        ))
        .splice_untyped_ctx(&()),
        q!(LazySource::new(move || Box::pin(async move {
            let channel_name = channel_name;
            let mux = self::get_or_init_channel_mux();
            let mut rx = mux.register(channel_name.to_owned());

            let (_sender_id, source) = rx.recv().await.ok_or_else(|| {
                std::io::Error::new(std::io::ErrorKind::ConnectionReset, "channel mux closed")
            })?;

            debug!(name: "o2m_channel_connected", %channel_name);

            Result::<_, std::io::Error>::Ok(source)
        })))
        .splice_untyped_ctx(&()),
    )
}

pub fn deploy_containerized_m2o(
    target_task_family: &str,
    channel_name: &str,
) -> (syn::Expr, syn::Expr) {
    (
        q!(LazySink::<_, _, _, bytes::Bytes>::new(move || {
            Box::pin(async move {
                let channel_name = channel_name;
                let target_task_family = target_task_family;
                let target_task_id = self::resolve_task_family_to_task_id(target_task_family).await;
                let ip = self::resolve_task_ip(&target_task_id).await;
                let target = format!("{}:{}", ip, self::CHANNEL_MUX_PORT);
                debug!(name: "connecting", %target, %target_task_family, %target_task_id, %channel_name);

                let stream = TcpStream::connect(&target).await?;
                let mut sink = FramedWrite::new(stream, LengthDelimitedCodec::new());

                let self_task_id = self::get_self_task_id();
                self::send_handshake(&mut sink, channel_name, Some(&self_task_id)).await?;

                Result::<_, std::io::Error>::Ok(sink)
            })
        }))
        .splice_untyped_ctx(&()),
        q!(LazySource::new(move || Box::pin(async move {
            let channel_name = channel_name;
            let mux = self::get_or_init_channel_mux();
            let mut rx = mux.register(channel_name.to_owned());

            Result::<_, std::io::Error>::Ok(
                futures::stream::unfold(rx, |mut rx| {
                    Box::pin(async move {
                        let (sender_id, source) = rx.recv().await?;
                        let from_task_id = sender_id
                            .expect("m2o sender must provide task ID");

                        debug!(name: "m2o_channel_connected", %from_task_id);

                        Some((
                            source.map(move |v| {
                                v.map(|v| (TaglessMemberId::from_container_name(from_task_id.clone()), v))
                            }),
                            rx,
                        ))
                    })
                })
                .flatten_unordered(None),
            )
        })))
        .splice_untyped_ctx(&()),
    )
}

pub fn deploy_containerized_m2m(channel_name: &str) -> (syn::Expr, syn::Expr) {
    (
        q!(sinktools::demux_map_lazy::<_, _, _, _>(
            move |key: &TaglessMemberId| {
                let key = key.clone();
                let channel_name = channel_name.to_owned();

                LazySink::<_, _, _, bytes::Bytes>::new(move || {
                    Box::pin(async move {
                        let task_id = key.get_container_name();
                        let ip = self::resolve_task_ip(task_id).await;
                        let target = format!("{}:{}", ip, self::CHANNEL_MUX_PORT);
                        debug!(name: "connecting", %target, %task_id, channel_name = %channel_name);

                        let stream = TcpStream::connect(&target).await?;
                        let mut sink = FramedWrite::new(stream, LengthDelimitedCodec::new());

                        let self_task_id = self::get_self_task_id();
                        self::send_handshake(&mut sink, &channel_name, Some(&self_task_id)).await?;

                        Result::<_, std::io::Error>::Ok(sink)
                    })
                })
            }
        ))
        .splice_untyped_ctx(&()),
        q!(LazySource::new(move || Box::pin(async move {
            let channel_name = channel_name;
            let mux = self::get_or_init_channel_mux();
            let mut rx = mux.register(channel_name.to_owned());

            Result::<_, std::io::Error>::Ok(
                futures::stream::unfold(rx, |mut rx| {
                    Box::pin(async move {
                        let (sender_id, source) = rx.recv().await?;
                        let from_task_id = sender_id.expect("m2m sender must provide task ID");

                        debug!(name: "m2m_channel_connected", %from_task_id);

                        Some((
                            source.map(move |v| {
                                v.map(|v| {
                                    (
                                        TaglessMemberId::from_container_name(from_task_id.clone()),
                                        v,
                                    )
                                })
                            }),
                            rx,
                        ))
                    })
                })
                .flatten_unordered(None),
            )
        })))
        .splice_untyped_ctx(&()),
    )
}

pub fn deploy_containerized_external_sink_source_ident(
    bind_addr: String,
    socket_ident: syn::Ident,
) -> syn::Expr {
    let socket_ident = SocketIdent { socket_ident };

    q!(LazySinkSource::<
        _,
        FramedRead<OwnedReadHalf, LengthDelimitedCodec>,
        FramedWrite<OwnedWriteHalf, LengthDelimitedCodec>,
        bytes::Bytes,
        std::io::Error,
    >::new(async move {
        let span = span!(tracing::Level::TRACE, "lazy_sink_source");
        let guard = span.enter();
        let bind_addr = bind_addr;
        trace!(name: "attempting to accept from external", %bind_addr);
        std::mem::drop(guard);
        let (stream, peer) = socket_ident.accept().instrument(span.clone()).await?;
        let guard = span.enter();

        debug!(name: "external accepting", ?peer);
        let (rx, tx) = stream.into_split();

        let fr = FramedRead::new(rx, LengthDelimitedCodec::new());
        let fw = FramedWrite::new(tx, LengthDelimitedCodec::new());

        Result::<_, std::io::Error>::Ok((fr, fw))
    },))
    .splice_untyped_ctx(&())
}

pub fn cluster_self_id<'a>() -> impl QuotedWithContext<'a, TaglessMemberId, ()> + Clone + 'a {
    q!(TaglessMemberId::from_container_name(
        self::get_self_task_id()
    ))
}

pub fn cluster_membership_stream<'a>(
    location_id: &LocationId,
) -> impl QuotedWithContext<'a, Box<dyn Stream<Item = (TaglessMemberId, MembershipEvent)> + Unpin>, ()>
{
    let location_key = location_id.key();

    q!(Box::new(self::ecs_membership_stream(
        std::env::var("CLUSTER_NAME").unwrap(),
        location_key
    ))
        as Box<
            dyn Stream<Item = (TaglessMemberId, MembershipEvent)> + Unpin,
        >)
}

#[instrument(skip_all, fields(%cluster_name, %location_key))]
fn ecs_membership_stream(
    cluster_name: String,
    location_key: LocationKey,
) -> impl Stream<Item = (TaglessMemberId, MembershipEvent)> + Unpin {
    use std::collections::HashSet;

    use futures::stream::{StreamExt, once};

    trace!(name: "ecs_membership_stream_created", %cluster_name, %location_key);

    let ecs_poller_span = trace_span!("ecs_poller");

    // Task family format: hy-{name_hint}-loc{idx}v{version}
    // Example: hy-p1-loc2v1
    let task_definition_arn_parser =
        regex::Regex::new(r#"arn:aws:ecs:(?<region>.*):(?<account_id>.*):task-definition\/(?<container_id>hy-(?<type>[^-]+)-loc(?<location_idx>[0-9]+)v(?<location_version>[0-9]+)(?:-(?<instance_id>.*))?):.*"#).unwrap();

    let poll_stream = futures::stream::unfold(
        (HashSet::<String>::new(), cluster_name, location_key),
        move |(known_tasks, cluster_name, location_key)| {
            let task_definition_arn_parser = task_definition_arn_parser.clone();

            async move {
                let config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
                let ecs_client = aws_sdk_ecs::Client::new(&config);

                let tasks = match ecs_client.list_tasks().cluster(&cluster_name).send().await {
                    Ok(tasks) => tasks,
                    Err(e) => {
                        trace!(name: "list_tasks_error", error = %e);
                        tokio::time::sleep(Duration::from_secs(2)).await;
                        return Some((Vec::new(), (known_tasks, cluster_name, location_key)));
                    }
                };

                let task_arns: Vec<String> =
                    tasks.task_arns().iter().map(|s| s.to_string()).collect();

                let mut events = Vec::new();
                let mut current_tasks = HashSet::<String>::new();

                if !task_arns.is_empty() {
                    let task_details = match ecs_client
                        .describe_tasks()
                        .cluster(&cluster_name)
                        .set_tasks(Some(task_arns.clone()))
                        .send()
                        .await
                    {
                        Ok(details) => details,
                        Err(e) => {
                            trace!(name: "describe_tasks_error", error = %e);
                            tokio::time::sleep(Duration::from_secs(2)).await;
                            return Some((Vec::new(), (known_tasks, cluster_name, location_key)));
                        }
                    };

                    for task in task_details.tasks() {
                        let Some(last_status) = task.last_status() else {
                            continue;
                        };

                        if last_status != "RUNNING" {
                            continue;
                        }

                        let Some(task_def_arn) = task.task_definition_arn() else {
                            continue;
                        };

                        let Some(captures) = task_definition_arn_parser.captures(task_def_arn)
                        else {
                            continue;
                        };

                        let Some(location_idx) = captures.name("location_idx") else {
                            continue;
                        };
                        let Some(location_version) = captures.name("location_version") else {
                            continue;
                        };
                        // Reconstruct the location key string and parse it
                        let location_key_str =
                            format!("loc{}v{}", location_idx.as_str(), location_version.as_str());
                        let task_location_key: LocationKey = match location_key_str.parse() {
                            Ok(key) => key,
                            Err(_) => {
                                continue;
                            }
                        };

                        // Filter by location_id - only include tasks for this specific cluster
                        if task_location_key != location_key {
                            continue;
                        }

                        // Extract task ID from task ARN (last segment after final /)
                        // Task ARN format: arn:aws:ecs:region:account:task/cluster-name/task-id
                        let Some(task_arn) = task.task_arn() else {
                            continue;
                        };
                        let Some(task_id) = task_arn.rsplit('/').next() else {
                            continue;
                        };

                        // Use task_id as the member identifier
                        current_tasks.insert(task_id.to_owned());
                        if !known_tasks.contains(task_id) {
                            trace!(name: "task_joined", %task_id);
                            events.push((task_id.to_owned(), MembershipEvent::Joined));
                        }
                    }
                }

                #[expect(
                    clippy::disallowed_methods,
                    reason = "nondeterministic iteration order, container events are not deterministically ordered"
                )]
                for task_id in known_tasks.iter() {
                    if !current_tasks.contains(task_id) {
                        trace!(name: "task_left", %task_id);
                        events.push((task_id.to_owned(), MembershipEvent::Left));
                    }
                }

                tokio::time::sleep(Duration::from_secs(2)).await;

                Some((events, (current_tasks, cluster_name, location_key)))
            }
            .instrument(ecs_poller_span.clone())
        },
    )
    .flat_map(futures::stream::iter);

    Box::pin(
        poll_stream
            .map(|(k, v)| (TaglessMemberId::from_container_name(k), v))
            .inspect(|(member_id, event)| trace!(name: "membership_event", ?member_id, ?event)),
    )
}

/// Resolve a task ID to its private IP address via ECS API.
async fn resolve_task_ip(task_id: &str) -> String {
    let cluster_name = std::env::var("CLUSTER_NAME").unwrap();

    let config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
    let ecs_client = aws_sdk_ecs::Client::new(&config);

    loop {
        let tasks = match ecs_client.list_tasks().cluster(&cluster_name).send().await {
            Ok(t) => t,
            Err(e) => {
                trace!(name: "resolve_ip_list_error", %task_id, error = %e);
                tokio::time::sleep(Duration::from_secs(1)).await;
                continue;
            }
        };

        let task_arns: Vec<_> = tasks.task_arns().to_vec();
        if task_arns.is_empty() {
            trace!(name: "resolve_ip_no_tasks", %task_id);
            tokio::time::sleep(Duration::from_secs(1)).await;
            continue;
        }

        let task_details = match ecs_client
            .describe_tasks()
            .cluster(&cluster_name)
            .set_tasks(Some(task_arns))
            .send()
            .await
        {
            Ok(d) => d,
            Err(e) => {
                trace!(name: "resolve_ip_describe_error", %task_id, error = %e);
                tokio::time::sleep(Duration::from_secs(1)).await;
                continue;
            }
        };

        // Find the task with matching task ID
        for task in task_details.tasks() {
            let Some(task_arn) = task.task_arn() else {
                continue;
            };
            let current_task_id = task_arn.rsplit('/').next().unwrap_or_default();

            if current_task_id == task_id
                && let Some(ip) = task
                    .attachments()
                    .iter()
                    .flat_map(|a| a.details())
                    .find(|d| d.name() == Some("privateIPv4Address"))
                    .and_then(|d| d.value())
            {
                trace!(name: "resolved_ip", %task_id, %ip);
                return ip.to_owned();
            }
        }

        trace!(name: "resolve_ip_not_found", %task_id);
        tokio::time::sleep(Duration::from_secs(1)).await;
    }
}

/// Resolve a task family name to its task ID via ECS API.
/// Used for process-to-process connections where the target is known by task family at compile time.
async fn resolve_task_family_to_task_id(task_family: &str) -> String {
    let cluster_name = std::env::var("CLUSTER_NAME").unwrap();

    let config = aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
    let ecs_client = aws_sdk_ecs::Client::new(&config);

    loop {
        let tasks = match ecs_client
            .list_tasks()
            .cluster(&cluster_name)
            .family(task_family)
            .send()
            .await
        {
            Ok(t) => t,
            Err(e) => {
                trace!(name: "resolve_family_list_error", %task_family, error = %e);
                tokio::time::sleep(Duration::from_secs(1)).await;
                continue;
            }
        };

        let Some(task_arn) = tasks.task_arns().first() else {
            trace!(name: "resolve_family_no_task", %task_family);
            tokio::time::sleep(Duration::from_secs(1)).await;
            continue;
        };

        // Extract task ID from ARN
        let task_id = task_arn.rsplit('/').next().unwrap_or_default();
        if !task_id.is_empty() {
            trace!(name: "resolved_task_id", %task_family, %task_id);
            return task_id.to_owned();
        }

        trace!(name: "resolve_family_invalid_arn", %task_family, %task_arn);
        tokio::time::sleep(Duration::from_secs(1)).await;
    }
}

fn get_self_task_id() -> String {
    let metadata_uri = std::env::var("ECS_CONTAINER_METADATA_URI_V4")
        .expect("ECS_CONTAINER_METADATA_URI_V4 not set - are we running in ECS?");
    metadata_uri
        .rsplit('/')
        .next()
        .expect("Invalid ECS metadata URI format")
        .to_owned()
}