harmos-sdk 0.1.5

Harmos SDK for authoring plugins and operations over the Harmos runtime.
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
/* ***
 *
 * Harmos
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * ***
 */

/* === Modules === */

/* === Re-Exports === */

/* === Imports === */

// Standard Library Imports
use std::future::Future;
use std::pin::Pin;
use std::sync::{
    Arc,
    atomic::{AtomicBool, Ordering},
};
use std::task::Poll;

// Third Party Imports
use tokio::sync::{Mutex, watch};

// Local Imports
use super::ServingError;
use crate::operations::{EndpointSet, StreamInput, StreamOutput, StreamSettings, streaming};
use crate::{MemorySize, Message};

/* === Constants === */

/* === Macros === */

/* === Type Aliases === */

type Pending<'a, T> = Pin<Box<dyn Future<Output = Result<T, ServingError>> + Send + 'a>>;

/* === Structs === */

#[derive(Clone, Copy)]
pub struct WireStreamLimits {
    pub input: StreamSettings,
    pub output: StreamSettings,
}

#[derive(Clone)]
pub struct WireStreams {
    input: Option<Arc<dyn Input>>,
    output: Option<Arc<dyn Encoder>>,
    state: Arc<watch::Sender<State>>,

    #[cfg(feature = "grpc")]
    input_codec: Option<super::protobuf::ItemCodec>,

    #[cfg(feature = "grpc")]
    output_codec: Option<super::protobuf::ItemCodec>,
}

struct Sending<T>(Mutex<Option<StreamOutput<T>>>, Arc<AtomicBool>);
struct Receiving<T>(Mutex<StreamInput<T>>, Arc<AtomicBool>);
#[derive(Clone, Copy)]
struct State {
    epoch: u64,
    phase: Phase,
}

pub(super) struct Attempt {
    streams: WireStreams,
    epoch: u64,
    ended: bool,
}

/* === Enums === */

#[derive(Clone, Copy, PartialEq, Eq)]
enum Phase {
    Waiting,
    Running,
    Ended,
    Interrupted,
    Closed,
}

/* === Traits === */

pub trait WireEndpoints: EndpointSet + Sized {
    fn empty() -> Option<Self> {
        None
    }

    fn bind(limits: WireStreamLimits) -> Result<(Self, WireStreams), ServingError>;
}

trait Input: Send + Sync {
    fn send<'a>(&'a self, payload: &'a [u8], #[cfg(feature = "grpc")] codec: Option<super::protobuf::ItemCodec>) -> Pending<'a, ()>;

    #[cfg(feature = "grpc")]
    fn item_type(&self) -> std::any::TypeId;

    fn finish(&self) -> Result<(), ServingError>;
    fn transferred(&self) -> bool;
}
trait Encoder: Send + Sync {
    #[cfg(feature = "grpc")]
    fn item_type(&self) -> std::any::TypeId;

    fn receive<'a>(&'a self, streams: &'a WireStreams, epoch: u64) -> Pending<'a, Option<Vec<u8>>>;
    fn transferred(&self) -> bool;
}

/* === Implementations === */

impl WireEndpoints for () {
    fn empty() -> Option<Self> {
        Some(())
    }
    fn bind(_: WireStreamLimits) -> Result<(Self, WireStreams), ServingError> {
        Err(ServingError::StreamUnsupported)
    }
}

impl WireStreamLimits {
    pub fn validate(&self) -> Result<(), ServingError> {
        streaming::validate(&self.input).map_err(ServingError::Stream)?;
        streaming::validate(&self.output).map_err(ServingError::Stream)
    }
}

impl WireStreams {
    fn new(input: Option<Arc<dyn Input>>, output: Option<Arc<dyn Encoder>>) -> Self {
        let (state, _) = watch::channel(State { epoch: 0, phase: Phase::Waiting });
        Self {
            input,
            output,
            state: Arc::new(state),
            #[cfg(feature = "grpc")]
            input_codec: None,
            #[cfg(feature = "grpc")]
            output_codec: None,
        }
    }

    #[cfg(feature = "grpc")]
    pub(super) fn protobuf(mut self, codec: super::protobuf::Codec) -> Result<Self, ServingError> {
        if self.input.as_ref().map(|input| input.item_type()) != codec.input.map(|codec| codec.item_type)
            || self.output.as_ref().map(|output| output.item_type()) != codec.output.map(|codec| codec.item_type)
        {
            return Err(ServingError::Catalog);
        }
        self.input_codec = codec.input;
        self.output_codec = codec.output;
        Ok(self)
    }

    pub fn transferred(&self) -> bool {
        self.input.as_ref().is_some_and(|input| input.transferred()) || self.output.as_ref().is_some_and(|output| output.transferred())
    }

    pub async fn send(&self, epoch: u64, payload: &[u8]) -> Result<(), ServingError> {
        if payload.len() > super::codec::LIMIT {
            return Err(ServingError::Stream(streaming::Error::Capacity));
        }
        let input = self.input.as_ref().ok_or(ServingError::StreamPhase)?;
        let mut changed = self.state.subscribe();
        self.check_input(epoch)?;
        let mut sending = input.send(
            payload,
            #[cfg(feature = "grpc")]
            self.input_codec,
        );
        loop {
            let mut change = std::pin::pin!(changed.changed());
            let result = std::future::poll_fn(|cx| {
                if change.as_mut().poll(cx).is_ready() {
                    return Poll::Ready(None);
                }
                sending.as_mut().poll(cx).map(Some)
            })
            .await;
            if let Some(result) = result {
                return result;
            }
            self.check_input(epoch)?;
        }
    }

    pub fn finish_input(&self, epoch: u64) -> Result<(), ServingError> {
        self.check_input(epoch)?;
        self.input.as_ref().ok_or(ServingError::StreamPhase)?.finish()
    }

    pub async fn receive(&self, epoch: u64) -> Result<Option<Vec<u8>>, ServingError> {
        self.output.as_ref().ok_or(ServingError::StreamPhase)?.receive(self, epoch).await
    }

    fn check_input(&self, epoch: u64) -> Result<(), ServingError> {
        let state = *self.state.borrow();
        if (state.epoch == epoch && state.phase == Phase::Running) || (state.epoch == 0 && epoch == 1 && state.phase == Phase::Waiting) {
            return Ok(());
        }
        Err(ServingError::StreamPhase)
    }

    pub(super) fn start(&self, expected: u64) -> Result<Attempt, ServingError> {
        let mut epoch = None;
        self.state.send_if_modified(|state| {
            if matches!(state.phase, Phase::Running | Phase::Closed) || (state.epoch != 0 && self.transferred()) {
                return false;
            }
            let Some(next) = state.epoch.checked_add(1) else {
                return false;
            };
            if expected != next {
                return false;
            }
            state.epoch = next;
            state.phase = Phase::Running;
            epoch = Some(next);
            true
        });
        Ok(Attempt { streams: self.clone(), epoch: epoch.ok_or(ServingError::StreamPhase)?, ended: false })
    }

    pub(super) fn close(&self) {
        self.state.send_modify(|state| state.phase = Phase::Closed);
    }

    fn output_phase(&self, epoch: u64) -> Result<Phase, ServingError> {
        let state = *self.state.borrow();
        if state.phase == Phase::Closed {
            return Err(ServingError::Closing);
        }
        if state.epoch == epoch && epoch != 0 {
            return match state.phase {
                Phase::Running | Phase::Ended => Ok(state.phase),
                _ => Err(ServingError::StreamPhase),
            };
        }
        if state.epoch.checked_add(1) == Some(epoch)
            && matches!(state.phase, Phase::Waiting | Phase::Ended | Phase::Interrupted)
            && (state.epoch == 0 || !self.transferred())
        {
            return Ok(Phase::Waiting);
        }
        Err(ServingError::StreamPhase)
    }
}

impl Attempt {
    pub(super) fn end(mut self) {
        self.streams.state.send_if_modified(|state| {
            if state.epoch != self.epoch || state.phase != Phase::Running {
                return false;
            }
            state.phase = Phase::Ended;
            true
        });
        self.ended = true;
    }
}
impl Drop for Attempt {
    fn drop(&mut self) {
        if self.ended {
            return;
        }
        self.streams.state.send_if_modified(|state| {
            if state.epoch != self.epoch || state.phase != Phase::Running {
                return false;
            }
            state.phase = Phase::Interrupted;
            true
        });
    }
}

impl<T: Message + MemorySize + Send + 'static> WireEndpoints for StreamInput<T> {
    fn bind(limits: WireStreamLimits) -> Result<(Self, WireStreams), ServingError> {
        let (sender, receiver) = streaming::stream(limits.input).map_err(ServingError::Stream)?;
        let transferred = sender.transfer_flag();
        Ok((receiver, WireStreams::new(Some(Arc::new(Sending(Mutex::new(Some(sender)), transferred))), None)))
    }
}
impl<T: Message + MemorySize + Send + 'static, E: 'static> WireEndpoints for StreamOutput<T, E> {
    fn bind(limits: WireStreamLimits) -> Result<(Self, WireStreams), ServingError> {
        let (sender, receiver) = streaming::stream(limits.output).map_err(ServingError::Stream)?;
        let transferred = sender.transfer_flag();
        Ok((sender.failing_with(), WireStreams::new(None, Some(Arc::new(Receiving(Mutex::new(receiver), transferred))))))
    }
}
impl<I, O, E> WireEndpoints for (StreamInput<I>, StreamOutput<O, E>)
where
    I: Message + MemorySize + Send + 'static,
    O: Message + MemorySize + Send + 'static,
    E: 'static,
{
    fn bind(limits: WireStreamLimits) -> Result<(Self, WireStreams), ServingError> {
        let (input, incoming) = StreamInput::<I>::bind(limits)?;
        let (output, outgoing) = StreamOutput::<O, E>::bind(limits)?;
        Ok(((input, output), WireStreams::new(incoming.input, outgoing.output)))
    }
}

impl<T: Message + MemorySize + Send + 'static> Input for Sending<T> {
    #[cfg(feature = "grpc")]
    fn item_type(&self) -> std::any::TypeId {
        std::any::TypeId::of::<T>()
    }
    fn send<'a>(&'a self, payload: &'a [u8], #[cfg(feature = "grpc")] codec: Option<super::protobuf::ItemCodec>) -> Pending<'a, ()> {
        Box::pin(async move {
            let input = self.0.try_lock().map_err(|_| ServingError::StreamBusy)?;
            let input = input.as_ref().ok_or(ServingError::StreamPhase)?;
            #[cfg(feature = "grpc")]
            if let Some(codec) = codec {
                let bytes = (codec.memory)(payload).map_err(ServingError::Codec)?;
                return input
                    .send_with(bytes, || {
                        let mut item = None::<T>;
                        (codec.decode)(payload, &mut item).map_err(streaming::Error::Codec)?;
                        item.ok_or_else(|| streaming::Error::Codec(crate::MessageError::borrowed("protobuf stream item projection failed")))
                    })
                    .await
                    .map_err(ServingError::Stream);
            }
            input.decode(payload).await.map_err(ServingError::Stream)
        })
    }
    fn finish(&self) -> Result<(), ServingError> {
        let mut input = self.0.try_lock().map_err(|_| ServingError::StreamBusy)?;
        input.take();
        Ok(())
    }
    fn transferred(&self) -> bool {
        self.1.load(Ordering::Acquire)
    }
}
impl<T: Message + MemorySize + Send + 'static> Encoder for Receiving<T> {
    #[cfg(feature = "grpc")]
    fn item_type(&self) -> std::any::TypeId {
        std::any::TypeId::of::<T>()
    }
    fn receive<'a>(&'a self, streams: &'a WireStreams, epoch: u64) -> Pending<'a, Option<Vec<u8>>> {
        Box::pin(async move {
            let mut output = self.0.try_lock().map_err(|_| ServingError::StreamBusy)?;
            let mut changed = streams.state.subscribe();
            loop {
                let phase = streams.output_phase(epoch)?;
                if phase == Phase::Ended {
                    return match output.try_recv() {
                        Ok(item) => encode(
                            &*item,
                            #[cfg(feature = "grpc")]
                            streams.output_codec,
                        )
                        .map(Some),
                        Err(_) => Ok(None),
                    };
                }
                if phase == Phase::Waiting {
                    changed.changed().await.map_err(|_| ServingError::Closing)?;
                    continue;
                }
                let mut receiving = std::pin::pin!(output.recv());
                let mut change = std::pin::pin!(changed.changed());
                let result = std::future::poll_fn(|cx| {
                    if change.as_mut().poll(cx).is_ready() {
                        return Poll::Ready(None);
                    }
                    receiving.as_mut().poll(cx).map(Some)
                })
                .await;
                if let Some(item) = result {
                    return item
                        .map(|item| {
                            encode(
                                &*item,
                                #[cfg(feature = "grpc")]
                                streams.output_codec,
                            )
                        })
                        .transpose();
                }
            }
        })
    }
    fn transferred(&self) -> bool {
        self.1.load(Ordering::Acquire)
    }
}

/* === Public Functions === */

/* === Private Functions === */

fn encode<T: Message + 'static>(
    item: &T, #[cfg(feature = "grpc")] codec: Option<super::protobuf::ItemCodec>,
) -> Result<Vec<u8>, ServingError> {
    #[cfg(feature = "grpc")]
    if let Some(codec) = codec {
        let bytes = (codec.length)(item).map_err(ServingError::Codec)?;
        if bytes > super::codec::LIMIT {
            return Err(ServingError::Stream(streaming::Error::Capacity));
        }
        return (codec.encode)(item).map_err(ServingError::Codec);
    }
    if item.encoded_len() > super::codec::LIMIT {
        return Err(ServingError::Stream(streaming::Error::Capacity));
    }
    Ok(item.encode_to_vec())
}

/* === Tests === */