harn-vm 0.8.6

Async bytecode virtual machine for the Harn programming language
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
use std::collections::{HashMap, HashSet, VecDeque};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Duration;

use serde_json::json;
use time::OffsetDateTime;
use tokio::sync::{Mutex, Notify};

use crate::event_log::{
    sanitize_topic_component, AnyEventLog, EventLog, LogError, LogEvent, Topic,
};
use crate::triggers::test_util::clock;
use crate::TriggerEvent;

#[derive(Debug)]
pub enum BatchDecision {
    Dispatch(Vec<TriggerEvent>),
    Merged,
}

#[derive(Clone, Debug)]
pub struct ConcurrencyPermit {
    gate: String,
}

#[derive(Debug, Default)]
struct FlowControlState {
    concurrency_active: HashMap<String, u32>,
    concurrency_waiters: HashMap<String, Vec<ConcurrencyWaiter>>,
    singleton_active: HashSet<String>,
    throttle_hits: HashMap<String, VecDeque<OffsetDateTime>>,
    rate_limit_hits: HashMap<String, VecDeque<OffsetDateTime>>,
    debounce_latest: HashMap<String, u64>,
    batch_groups: HashMap<String, BatchGroup>,
    batch_consumed: HashSet<u64>,
}

#[derive(Clone, Debug)]
struct ConcurrencyWaiter {
    token: u64,
    priority_rank: usize,
    queued_order: u64,
}

#[derive(Clone, Debug)]
struct BatchMember {
    token: u64,
    event: TriggerEvent,
}

#[derive(Clone, Debug)]
struct BatchGroup {
    leader: u64,
    deadline: OffsetDateTime,
    members: Vec<BatchMember>,
}

#[derive(Clone)]
pub struct FlowControlManager {
    event_log: Arc<AnyEventLog>,
    state: Arc<Mutex<FlowControlState>>,
    notify: Arc<Notify>,
    sequence: Arc<AtomicU64>,
}

impl std::fmt::Debug for FlowControlManager {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FlowControlManager").finish_non_exhaustive()
    }
}

impl FlowControlManager {
    pub fn new(event_log: Arc<AnyEventLog>) -> Self {
        Self {
            event_log,
            state: Arc::new(Mutex::new(FlowControlState::default())),
            notify: Arc::new(Notify::new()),
            sequence: Arc::new(AtomicU64::new(1)),
        }
    }

    pub async fn debounce(&self, gate: &str, period: Duration) -> Result<bool, LogError> {
        let token = self.sequence.fetch_add(1, Ordering::Relaxed);
        {
            let mut state = self.state.lock().await;
            state.debounce_latest.insert(gate.to_string(), token);
        }
        self.append_event(
            "debounce",
            gate,
            "debounce_seen",
            json!({"gate": gate, "token": token}),
        )
        .await?;
        clock::sleep(period).await;
        let latest = {
            let state = self.state.lock().await;
            state.debounce_latest.get(gate).copied()
        };
        let latest = latest == Some(token);
        if latest {
            self.append_event(
                "debounce",
                gate,
                "debounce_selected",
                json!({"gate": gate, "token": token}),
            )
            .await?;
        }
        Ok(latest)
    }

    pub async fn check_rate_limit(
        &self,
        gate: &str,
        period: Duration,
        max: u32,
    ) -> Result<bool, LogError> {
        let now = clock::now_utc();
        let allowed = {
            let mut state = self.state.lock().await;
            let hits = state.rate_limit_hits.entry(gate.to_string()).or_default();
            trim_window(hits, now, period);
            if hits.len() >= max as usize {
                false
            } else {
                hits.push_back(now);
                true
            }
        };
        self.append_event(
            "rate_limit",
            gate,
            if allowed {
                "rate_limit_allowed"
            } else {
                "rate_limit_blocked"
            },
            json!({"gate": gate, "max": max}),
        )
        .await?;
        Ok(allowed)
    }

    pub async fn wait_for_throttle(
        &self,
        gate: &str,
        period: Duration,
        max: u32,
    ) -> Result<(), LogError> {
        loop {
            let wait_for = {
                let now = clock::now_utc();
                let mut state = self.state.lock().await;
                let hits = state.throttle_hits.entry(gate.to_string()).or_default();
                trim_window(hits, now, period);
                if hits.len() < max as usize {
                    hits.push_back(now);
                    None
                } else {
                    hits.front().map(|first| {
                        let deadline =
                            *first + time::Duration::try_from(period).unwrap_or_default();
                        (deadline - now)
                            .try_into()
                            .unwrap_or(Duration::from_millis(1))
                    })
                }
            };
            match wait_for {
                Some(delay) => {
                    self.append_event(
                        "throttle",
                        gate,
                        "throttle_wait",
                        json!({"gate": gate, "delay_ms": delay.as_millis()}),
                    )
                    .await?;
                    clock::sleep(delay).await;
                }
                None => {
                    self.append_event(
                        "throttle",
                        gate,
                        "throttle_acquired",
                        json!({"gate": gate, "max": max}),
                    )
                    .await?;
                    return Ok(());
                }
            }
        }
    }

    pub async fn try_acquire_singleton(&self, gate: &str) -> Result<bool, LogError> {
        let acquired = {
            let mut state = self.state.lock().await;
            state.singleton_active.insert(gate.to_string())
        };
        self.append_event(
            "singleton",
            gate,
            if acquired {
                "singleton_acquired"
            } else {
                "singleton_skipped"
            },
            json!({"gate": gate}),
        )
        .await?;
        Ok(acquired)
    }

    pub async fn acquire_singleton(&self, gate: &str) -> Result<(), LogError> {
        loop {
            if self.try_acquire_singleton(gate).await? {
                return Ok(());
            }
            self.notify.notified().await;
        }
    }

    pub async fn release_singleton(&self, gate: &str) -> Result<(), LogError> {
        {
            let mut state = self.state.lock().await;
            state.singleton_active.remove(gate);
        }
        self.notify.notify_waiters();
        self.append_event(
            "singleton",
            gate,
            "singleton_released",
            json!({"gate": gate}),
        )
        .await
    }

    pub async fn acquire_concurrency(
        &self,
        gate: &str,
        max: u32,
        priority_rank: usize,
    ) -> Result<ConcurrencyPermit, LogError> {
        let token = self.sequence.fetch_add(1, Ordering::Relaxed);
        loop {
            let acquired = {
                let mut state = self.state.lock().await;
                let head_token = {
                    let waiters = state
                        .concurrency_waiters
                        .entry(gate.to_string())
                        .or_default();
                    if !waiters.iter().any(|waiter| waiter.token == token) {
                        waiters.push(ConcurrencyWaiter {
                            token,
                            priority_rank,
                            queued_order: token,
                        });
                    }
                    waiters.sort_by(|left, right| {
                        left.priority_rank
                            .cmp(&right.priority_rank)
                            .then(left.queued_order.cmp(&right.queued_order))
                    });
                    waiters.first().map(|waiter| waiter.token)
                };
                let active = state
                    .concurrency_active
                    .entry(gate.to_string())
                    .or_default();
                if *active < max && head_token == Some(token) {
                    *active += 1;
                    if let Some(waiters) = state.concurrency_waiters.get_mut(gate) {
                        waiters.retain(|waiter| waiter.token != token);
                    }
                    true
                } else {
                    false
                }
            };
            if acquired {
                self.append_event(
                    "concurrency",
                    gate,
                    "concurrency_acquired",
                    json!({"gate": gate, "token": token, "max": max}),
                )
                .await?;
                return Ok(ConcurrencyPermit {
                    gate: gate.to_string(),
                });
            }
            self.notify.notified().await;
        }
    }

    pub async fn release_concurrency(&self, permit: ConcurrencyPermit) -> Result<(), LogError> {
        {
            let mut state = self.state.lock().await;
            if let Some(active) = state.concurrency_active.get_mut(&permit.gate) {
                *active = active.saturating_sub(1);
                if *active == 0 {
                    state.concurrency_active.remove(&permit.gate);
                }
            }
        }
        self.notify.notify_waiters();
        self.append_event(
            "concurrency",
            &permit.gate,
            "concurrency_released",
            json!({"gate": permit.gate}),
        )
        .await
    }

    pub async fn consume_batch(
        &self,
        gate: &str,
        size: u32,
        timeout: Duration,
        event: TriggerEvent,
    ) -> Result<BatchDecision, LogError> {
        let token = self.sequence.fetch_add(1, Ordering::Relaxed);
        {
            let mut state = self.state.lock().await;
            let group = state
                .batch_groups
                .entry(gate.to_string())
                .or_insert_with(|| BatchGroup {
                    leader: token,
                    deadline: clock::now_utc()
                        + time::Duration::try_from(timeout).unwrap_or_default(),
                    members: Vec::new(),
                });
            group.members.push(BatchMember { token, event });
        }
        self.notify.notify_waiters();
        self.append_event(
            "batch",
            gate,
            "batch_enqueued",
            json!({"gate": gate, "token": token, "size": size}),
        )
        .await?;

        loop {
            let maybe_batch = {
                let mut state = self.state.lock().await;
                if state.batch_consumed.remove(&token) {
                    return Ok(BatchDecision::Merged);
                }
                let now = clock::now_utc();
                let Some(group) = state.batch_groups.get_mut(gate) else {
                    continue;
                };
                if group.leader == token
                    && (group.members.len() >= size as usize || now >= group.deadline)
                {
                    let members = std::mem::take(&mut group.members);
                    for member in members.iter().skip(1) {
                        state.batch_consumed.insert(member.token);
                    }
                    state.batch_groups.remove(gate);
                    Some(
                        members
                            .into_iter()
                            .map(|member| member.event)
                            .collect::<Vec<_>>(),
                    )
                } else {
                    None
                }
            };
            if let Some(events) = maybe_batch {
                self.notify.notify_waiters();
                self.append_event(
                    "batch",
                    gate,
                    "batch_dispatched",
                    json!({"gate": gate, "count": events.len()}),
                )
                .await?;
                return Ok(BatchDecision::Dispatch(events));
            }

            let deadline = {
                let state = self.state.lock().await;
                state.batch_groups.get(gate).map(|group| group.deadline)
            };
            match deadline {
                Some(deadline) => {
                    let notified = self.notify.notified();
                    tokio::pin!(notified);
                    tokio::select! {
                        _ = sleep_until(deadline) => {}
                        _ = &mut notified => {}
                    }
                }
                None => self.notify.notified().await,
            }
        }
    }

    async fn append_event(
        &self,
        primitive: &str,
        gate: &str,
        kind: &str,
        payload: serde_json::Value,
    ) -> Result<(), LogError> {
        let topic = Topic::new(format!(
            "trigger.{primitive}.{}",
            sanitize_topic_component(gate)
        ))?;
        self.event_log
            .append(&topic, LogEvent::new(kind, payload))
            .await
            .map(|_| ())
    }
}

fn trim_window(hits: &mut VecDeque<OffsetDateTime>, now: OffsetDateTime, period: Duration) {
    let period = time::Duration::try_from(period).unwrap_or_default();
    while let Some(first) = hits.front().copied() {
        if now - first >= period {
            hits.pop_front();
        } else {
            break;
        }
    }
}

async fn sleep_until(deadline: OffsetDateTime) {
    let now = clock::now_utc();
    if deadline <= now {
        return;
    }
    if let Ok(duration) = (deadline - now).try_into() {
        clock::sleep(duration).await;
    }
}