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
use std::collections::HashMap;
use std::mem::MaybeUninit;
use std::sync::{Arc, Once};

use nando_support::{activation_intent, ObjectId, ObjectVersion};
use ownership_support;
use parking_lot::RwLock;
use reqwest;

use crate::orchestration::host_manager::HostManager;
use crate::orchestration::{error as orchestration_error, HostIdx};

pub(crate) struct OrchestrationClientManager {
    host_clients: RwLock<HashMap<HostIdx, Arc<reqwest::Client>>>,
}

impl OrchestrationClientManager {
    pub fn new() -> Self {
        Self {
            host_clients: RwLock::new(HashMap::new()),
        }
    }

    fn get_client_manager() -> &'static Arc<OrchestrationClientManager> {
        static mut INSTANCE: MaybeUninit<Arc<OrchestrationClientManager>> = MaybeUninit::uninit();
        static mut ONCE: Once = Once::new();

        unsafe {
            ONCE.call_once(|| {
                INSTANCE
                    .as_mut_ptr()
                    .write(Arc::new(OrchestrationClientManager::new()));
            });
        }

        unsafe { &*INSTANCE.as_ptr() }
    }

    fn get_client(
        &self,
        host_idx: &HostIdx,
    ) -> Result<Arc<reqwest::Client>, orchestration_error::InternalError> {
        let cached_client = {
            let host_clients = self.host_clients.read();
            match host_clients.get(host_idx) {
                Some(c) => Some(c.clone()),
                None => None,
            }
        };

        if cached_client.is_some() {
            return Ok(cached_client.unwrap());
        }

        let client = Arc::new(reqwest::Client::new());
        {
            let mut host_clients = self.host_clients.write();
            host_clients.insert(*host_idx, client.clone());
        }

        Ok(client)
    }

    pub async fn request_ownership_change(
        objects_to_move: &Vec<ObjectId>,
        current_owner: HostIdx,
        new_owner: HostIdx,
    ) -> Result<Vec<(ObjectId, ObjectVersion)>, orchestration_error::OwnershipTransferError> {
        let client_manager = Self::get_client_manager();
        let client = client_manager.get_client(&new_owner).expect(&format!(
            "failed to get orchestration client for host {}",
            new_owner
        ));

        let host_manager = HostManager::get_host_manager().clone();
        let current_owner_hostname = host_manager
            .get_hostname_by_idx(current_owner)
            .expect(&format!("no host for idx {}", current_owner));
        let new_owner_hostname = host_manager
            .get_hostname_by_idx(new_owner)
            .expect(&format!("no host for idx {}", new_owner));

        let body = ownership_support::MoveOwnershipRequest {
            object_refs: objects_to_move.into_iter().map(|oid| *oid).collect(),
            new_host: new_owner_hostname,
        };
        match client
            .post(format!(
                "http://{}:52017/activation_router/move_ownership",
                current_owner_hostname,
            ))
            .json(&body)
            .send()
            .await
        {
            Ok(response_body) => Ok(response_body
                .json::<ownership_support::MoveOwnershipResponse>()
                .await
                .expect("failed to parse ownership move response")
                .whomstone_versions),
            Err(e) => {
                eprintln!(
                    "move ownership request to {} for {:?} failed: {}",
                    current_owner, objects_to_move, e
                );
                // FIXME actually check what the problem is and return appropriate error variant
                Err(orchestration_error::OwnershipTransferError::UnknownError())
            }
        }
    }

    pub async fn request_cache_spawn(
        cache_spawn_intent: &activation_intent::NandoActivationIntentSerializable,
        current_owner: HostIdx,
    ) -> Result<ObjectId, orchestration_error::InternalError> {
        let client_manager = Self::get_client_manager();
        let client = client_manager.get_client(&current_owner).expect(&format!(
            "failed to get orchestration client for host {}",
            current_owner
        ));

        let host_manager = HostManager::get_host_manager().clone();
        let current_owner_hostname = host_manager
            .get_hostname_by_idx(current_owner)
            .expect(&format!("no host for idx {}", current_owner));
        match client
            .post(format!(
                "http://{}:52017/activation_router/schedule",
                current_owner_hostname,
            ))
            .json(&cache_spawn_intent)
            .send()
            .await
        {
            Ok(response_body) => {
                let response = response_body
                    .json::<activation_intent::NandoActivationResolution>()
                    .await
                    .expect("failed to parse cache spawning response");
                let object_ref = &response.output[0];
                let activation_intent::NandoArgumentSerializable::Ref(iptr) = object_ref else {
                    panic!(
                        "unexpected response to cache spawning request: {:?}",
                        object_ref
                    );
                };

                Ok(iptr.get_object_id())
            }
            Err(e) => {
                eprintln!(
                    "cache spawning request {:?} to {} failed: {}",
                    cache_spawn_intent, current_owner, e
                );
                // FIXME actually check what the problem is and return appropriate error variant
                Err(orchestration_error::InternalError::UnknownError())
            }
        }
    }

    pub async fn notify_add_cache_mapping(
        to: HostIdx,
        original_object_id: ObjectId,
        cached_object_id: ObjectId,
        version: ObjectVersion,
        original_owner_idx: HostIdx,
    ) -> Result<(), orchestration_error::InternalError> {
        let client_manager = Self::get_client_manager();
        let client = client_manager.get_client(&to).expect(&format!(
            "failed to get orchestration client for host {}",
            to
        ));

        let host_manager = HostManager::get_host_manager().clone();
        let cache_owner_hostname = host_manager
            .get_hostname_by_idx(to)
            .expect(&format!("no host for idx {}", to));
        let notify_request = ownership_support::AddCacheMappingRequest {
            original_object_id,
            cached_object_id,
            first_version: version,
            original_owner_idx,
        };

        match client
            .post(format!(
                "http://{}:52017/activation_router/add_cache_mapping",
                cache_owner_hostname,
            ))
            .json(&notify_request)
            .send()
            .await
        {
            Ok(_response_body) => Ok(()),
            Err(e) => {
                eprintln!(
                    "cache mapping request to {} for {} / {} failed: {}",
                    to, original_object_id, cached_object_id, e
                );
                Err(orchestration_error::InternalError::UnknownError())
            }
        }
    }
}