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
use std::fmt;
use std::future::Future;
use std::pin::Pin;
use std::sync::{
    atomic::{AtomicU8, Ordering},
    Arc,
};
use std::task::{Context, Poll, Waker};

use atomic_refcell::{AtomicRef, AtomicRefCell};
use nando_support::{
    activation_intent::{NandoResult, NandoResultSerializable, ScalarValue},
    ecb_id::EcbId,
    ObjectId, ObjectVersion,
};

// TODO different error type, this has been duplicated from nando_lib
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub enum ExecutionError {
    TransactionNotFound(String),
    UnresolvableObject(String),
    UnknownError(),
}

impl fmt::Display for ExecutionError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ExecutionError::TransactionNotFound(name) => f.write_fmt(format_args!(
                "No nanotransaction with the name `{}` has been registered.",
                name,
            )),
            ExecutionError::UnresolvableObject(obj_id) => {
                f.write_fmt(format_args!("Could not resolve object {}", obj_id,))
            }
            _ => write!(f, "An unknown error occured"),
        }
    }
}

#[derive(Clone, Debug)]
pub enum ActivationOutput {
    Result(NandoResult),
    Epic(EcbId),
}

impl ActivationOutput {
    pub fn get_inner_result(&self) -> Option<NandoResult> {
        match self {
            ActivationOutput::Result(ref r) => Some(r.clone()),
            _ => None,
        }
    }
}

impl From<NandoResult> for ActivationOutput {
    fn from(value: NandoResult) -> Self {
        Self::Result(value)
    }
}

impl From<EcbId> for ActivationOutput {
    fn from(value: EcbId) -> Self {
        Self::Epic(value)
    }
}

impl From<&ActivationOutput> for NandoResultSerializable {
    fn from(value: &ActivationOutput) -> Self {
        match value {
            ActivationOutput::Result(ref r) => r.into(),
            ActivationOutput::Epic(ecb_id) => NandoResultSerializable::Epic(*ecb_id),
        }
    }
}

#[derive(PartialEq, Eq, Clone)]
pub enum HandleTransactionType {
    Nando,
    Epic(EcbId),
}

#[derive(Debug)]
pub enum TransactionStatus {
    Pending = 0,
    EpicTriggered = 1,
    Failed = 2,
    Done = 3,
}

impl TransactionStatus {
    fn is_pending(&self) -> bool {
        match self {
            Self::Pending => true,
            _ => false,
        }
    }
}

impl Into<u8> for TransactionStatus {
    fn into(self) -> u8 {
        match self {
            Self::Pending => 0,
            Self::EpicTriggered => 1,
            Self::Failed => 2,
            Self::Done => 3,
        }
    }
}

impl TryInto<TransactionStatus> for u8 {
    type Error = &'static str;

    fn try_into(self) -> Result<TransactionStatus, Self::Error> {
        let txn_status = match self {
            0 => TransactionStatus::Pending,
            1 => TransactionStatus::EpicTriggered,
            2 => TransactionStatus::Failed,
            3 => TransactionStatus::Done,
            _ => return Err("failed to convert u8 to TransactionStatus, value out of range"),
        };

        Ok(txn_status)
    }
}

pub struct HandleCompletionState {
    waker: AtomicRefCell<Option<Waker>>,
    spinlock: AtomicU8,
    txn_status: AtomicU8,
    execution_error: Option<ExecutionError>,
    output: AtomicRefCell<Vec<ActivationOutput>>,
    cacheable_dependencies: Vec<(ObjectId, ObjectVersion)>,
}

impl HandleCompletionState {
    pub fn new() -> Self {
        Self {
            waker: AtomicRefCell::new(None),
            spinlock: AtomicU8::new(0),
            txn_status: AtomicU8::new(TransactionStatus::Pending.into()),
            execution_error: None,
            output: AtomicRefCell::new(vec![]),
            cacheable_dependencies: vec![],
        }
    }

    pub fn wake(&self) {
        let Ok(waker) = self.waker.try_borrow() else {
            return;
        };

        match waker.as_ref() {
            Some(w) => w.wake_by_ref(),
            // NOTE If no waker has been assigned, then whoever polls the associated future will
            // just read the done status, so this is fine.
            None => (),
        }
    }

    pub fn mark_triggered_and_wake(&self) {
        while self
            .spinlock
            .compare_exchange(0, 1, Ordering::Acquire, Ordering::Relaxed)
            .is_err()
        {}

        self.txn_status
            .store(TransactionStatus::EpicTriggered.into(), Ordering::Relaxed);
        self.wake();

        self.spinlock.store(0, Ordering::Release);
    }

    pub fn mark_done_and_wake(&self) {
        while self
            .spinlock
            .compare_exchange(0, 1, Ordering::Acquire, Ordering::Relaxed)
            .is_err()
        {}

        self.txn_status
            .store(TransactionStatus::Done.into(), Ordering::Relaxed);
        self.wake();

        self.spinlock.store(0, Ordering::Release);
    }

    pub fn mark_failed_and_wake(&mut self, execution_error: ExecutionError) {
        while self
            .spinlock
            .compare_exchange(0, 1, Ordering::Acquire, Ordering::Relaxed)
            .is_err()
        {}

        self.execution_error = Some(execution_error);
        self.txn_status
            .store(TransactionStatus::Failed.into(), Ordering::Relaxed);
        self.wake();

        self.spinlock.store(0, Ordering::Release);
    }

    pub fn append_result(&self, result: NandoResult) {
        let mut output = self.output.borrow_mut();
        output.push(result.into());
    }

    pub fn append_spawn(&self, ecb_id: EcbId) {
        let mut output = self.output.borrow_mut();
        output.push(ecb_id.into());
    }

    pub fn append_cacheable_object(&mut self, object_id: ObjectId, version: ObjectVersion) {
        self.cacheable_dependencies.push((object_id, version));
    }

    pub fn get_result(&self) -> ActivationOutput {
        match self.output.borrow().last() {
            None => ActivationOutput::Result(NandoResult::Value(ScalarValue::Nil)),
            Some(e) => e.clone(),
        }
    }

    pub fn get_output(&self) -> AtomicRef<'_, Vec<ActivationOutput>> {
        self.output.borrow()
    }

    pub fn is_dummy(&self) -> bool {
        self.waker.borrow().is_none()
    }
}

pub type SharedHandleState = Arc<AtomicRefCell<HandleCompletionState>>;

#[derive(Clone)]
pub struct NandoHandle {
    txn_kind: HandleTransactionType,
    completion_state: SharedHandleState,
}

impl NandoHandle {
    pub fn new_nando_handle() -> Self {
        Self {
            txn_kind: HandleTransactionType::Nando,
            completion_state: Arc::new(AtomicRefCell::new(HandleCompletionState::new())),
        }
    }

    pub fn new_epic_top_level_handle(root_ecb_id: EcbId) -> Self {
        Self {
            txn_kind: HandleTransactionType::Epic(root_ecb_id),
            completion_state: Arc::new(AtomicRefCell::new(HandleCompletionState::new())),
        }
    }

    pub fn set_completion_state(
        &mut self,
        completion_state: Arc<AtomicRefCell<HandleCompletionState>>,
    ) {
        self.completion_state = completion_state;
    }

    pub fn get_completion_state(&self) -> Arc<AtomicRefCell<HandleCompletionState>> {
        Arc::clone(&self.completion_state)
    }

    pub fn get_ecb_id(&self) -> Option<EcbId> {
        match self.txn_kind {
            HandleTransactionType::Nando => None,
            HandleTransactionType::Epic(ecb_id) => Some(ecb_id),
        }
    }
}

pub enum NandoHandleOutputType {
    // Processing(EcbId),
    Processing(EcbId, Vec<(ObjectId, ObjectVersion)>),
    Result(Vec<ActivationOutput>, Vec<(ObjectId, ObjectVersion)>),
}

impl Future for NandoHandle {
    type Output = Result<NandoHandleOutputType, ExecutionError>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let completion_state = loop {
            match self.completion_state.try_borrow() {
                Err(_) => {}
                Ok(txn_state) => break txn_state,
            }
        };
        let mut txn_status = {
            while completion_state.spinlock.load(Ordering::Acquire) != 0 {}
            completion_state
                .txn_status
                .load(Ordering::Relaxed)
                .try_into()
                .unwrap()
        };

        loop {
            match txn_status {
                TransactionStatus::Pending => {
                    while completion_state
                        .spinlock
                        .compare_exchange(0, 1, Ordering::Acquire, Ordering::Relaxed)
                        .is_err()
                    {}
                    txn_status = completion_state
                        .txn_status
                        .load(Ordering::Relaxed)
                        .try_into()
                        .unwrap();

                    if !txn_status.is_pending() {
                        completion_state.spinlock.store(0, Ordering::Release);
                        continue;
                    }

                    match completion_state.waker.try_borrow_mut() {
                        Ok(mut waker) => {
                            *waker = Some(cx.waker().clone());
                        }
                        Err(_) => {}
                    }
                    completion_state.spinlock.store(0, Ordering::Release);

                    return Poll::Pending;
                }
                TransactionStatus::Failed => {
                    let Some(ref error) = self.completion_state.borrow().execution_error else {
                        panic!("no error found for failed transaction");
                    };
                    return Poll::Ready(Err(error.clone()));
                }
                TransactionStatus::EpicTriggered => {
                    let ecb_id = match self.txn_kind {
                        HandleTransactionType::Nando => panic!("should not happen"),
                        HandleTransactionType::Epic(ecb_id) => ecb_id,
                    };

                    return Poll::Ready(Ok(NandoHandleOutputType::Processing(
                        ecb_id,
                        self.completion_state
                            .borrow()
                            .cacheable_dependencies
                            .clone(),
                    )));
                }
                TransactionStatus::Done => {
                    let (output, cacheable_dependencies) = {
                        let completion_state = self.completion_state.borrow();
                        let output = completion_state.output.borrow();
                        (
                            output.clone(),
                            completion_state.cacheable_dependencies.clone(),
                        )
                    };

                    return Poll::Ready(Ok(NandoHandleOutputType::Result(
                        output,
                        cacheable_dependencies,
                    )));
                }
            }
        }
    }
}