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
use std::{collections::HashMap, time::Duration};

use crate::{self as utils};
use crows_service::service;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use uuid::Uuid;

#[derive(Error, Debug, Serialize, Deserialize, Clone)]
pub enum CoordinatorError {
    #[error("could not upload a module")]
    UploadModuleError,
    #[error("couldn't find module {0}")]
    NoSuchModule(String),
    #[error("Failed to create runtime: {0}")]
    FailedToCreateRuntime(String),
    #[error("Failed to compile module")]
    FailedToCompileModule,
    #[error("Couldn't fetch config: {0}")]
    CouldNotFetchConfig(String),
}

#[derive(Error, Debug, Serialize, Deserialize, Clone)]
pub enum WorkerError {
    #[error("could not upload a module")]
    UploadModuleError,
    #[error("could not find a requested scenario")]
    ScenarioNotFound,
    #[error("could not create a module from binary")]
    CouldNotCreateModule,
    #[error("could not create runtime: {0}")]
    CouldNotCreateRuntime(String),
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct RunId(Uuid);

impl RunId {
    pub fn new() -> Self {
        Self(Uuid::new_v4())
    }
}

impl Into<Uuid> for RunId {
    fn into(self) -> Uuid {
        self.0
    }
}

// TODO: I don't like the fact that I need to specify the "other_side"
// here. It would be better if it was only needed when connecting, then
// all the trait definitions wouldn't have to be here
#[service(variant = "server", other_side = Worker)]
pub trait WorkerToCoordinator {
    async fn ping(&self) -> String;
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum WorkerStatus {
    Available,
    Busy,
}

#[service(variant = "server", other_side = Client)]
pub trait Coordinator {
    async fn upload_scenario(name: String, content: Vec<u8>) -> Result<(), CoordinatorError>;
    async fn start(
        name: String,
        workers_number: usize,
    ) -> Result<(RunId, Vec<String>), CoordinatorError>;
    async fn list_workers() -> Vec<String>;
    async fn get_run_status(&self, id: RunId) -> Option<HashMap<String, RunInfo>>;
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct WorkerData {
    pub id: Uuid,
    pub hostname: String,
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct RequestInfo {
    pub latency: Duration,
    pub successful: bool,
}

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct IterationInfo {
    pub latency: Duration,
}

#[derive(Default, Clone, Serialize, Deserialize, Debug)]
pub struct RunInfo {
    pub iteration_stats: Vec<IterationInfo>,
    pub request_stats: Vec<RequestInfo>,
    pub stderr: Vec<Vec<u8>>,
    pub stdout: Vec<Vec<u8>>,
    pub done: bool,
    pub elapsed: Option<Duration>,
    pub left: Option<Duration>,
    pub active_instances_delta: isize,
    pub capacity_delta: isize,
}

#[service(variant = "client", other_side = WorkerToCoordinator)]
pub trait Worker {
    async fn upload_scenario(&self, name: String, content: Vec<u8>);
    async fn ping(&self) -> String;
    async fn start(
        &self,
        name: String,
        config: crows_shared::Config,
        run_id: RunId,
    ) -> Result<(), WorkerError>;
    async fn get_data(&self) -> WorkerData;
    async fn get_run_status(&self, id: RunId) -> RunInfo;
}

#[service(variant = "client", other_side = Coordinator)]
pub trait Client {}