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
use actix_web::{get, post, put, web, HttpResponse, Responder};
use nando_support::activation_intent::{NandoActivationIntentSerializable, SchedulerIntent};
use nando_support::ObjectId;
use ownership_support::{
    ConsolidationIntent, MultiPublishRequest, PublishRequest, RegisterWorkerRequest,
    RegisterWorkerResponse, ScheduleResponse, WorkerMapping,
};
use rand::{rngs::SmallRng, Rng, SeedableRng};

use crate::orchestration::{self, registry};

macro_rules! resolve_host_idx {
    ($idx:literal) => {
        orchestration::get_worker_hostname_by_idx($idx)
            .expect(&format!("idx {} does not correspond to host", $idx))
    };
    ($idx:ident) => {
        orchestration::get_worker_hostname_by_idx($idx)
            .expect(&format!("idx {} does not correspond to host", $idx))
    };
}

#[get("/healthcheck")]
pub async fn healthcheck_handler() -> impl Responder {
    println!("accepting healthcheck");
    HttpResponse::Ok().body("Global Scheduler Instance is up\n")
}

// TODO replace json with bincode
#[post("/schedule")]
pub async fn schedule_handler(
    // intent: web::Json<NandoActivationIntentSerializable>,
    scheduler_intent: web::Json<SchedulerIntent>,
) -> impl Responder {
    println!("Got request to schedule {:#?}", scheduler_intent);
    #[cfg(feature = "timing")]
    let start = tokio::time::Instant::now();
    let object_dependencies = match scheduler_intent.mutable_argument_indices.is_empty() {
        true => scheduler_intent.get_object_references(),
        false => scheduler_intent.get_mut_object_references(),
    };
    let requesting_host_idx = scheduler_intent.intent.host_idx;
    let response = 'response: {
        if object_dependencies.len() == 0 {
            // randomly pick a host
            // FIXME this should pick the least-busy host
            let host_idx = {
                let mut host_idx_rng = SmallRng::from_entropy();
                host_idx_rng.gen::<u64>() % orchestration::get_num_workers() as u64
            };

            break 'response ScheduleResponse {
                target_host: resolve_host_idx!(host_idx),
                had_to_consolidate: false,
            };
        }

        let ownership_registry = registry::OwnershipRegistry::get_ownership_registry().clone();
        #[cfg(not(feature = "always_move"))]
        if let Some(h) = ownership_registry.objects_are_colocated(&object_dependencies) {
            break 'response ScheduleResponse {
                target_host: resolve_host_idx!(h),
                had_to_consolidate: false,
            };
        };

        let activation_site =
            match orchestration::consolidate(requesting_host_idx, &object_dependencies).await {
                None => {
                    return HttpResponse::Conflict().body(format!(
                        "failed to consolidate objects {:?} to {:?}",
                        object_dependencies, requesting_host_idx,
                    ));
                }
                Some((activation_site, _)) => activation_site,
            };
        ScheduleResponse {
            target_host: resolve_host_idx!(activation_site),
            had_to_consolidate: true,
        }
    };

    #[cfg(feature = "timing")]
    {
        let duration = start.elapsed();
        println!(
            "Took {}ns to service ({}us)",
            duration.as_nanos(),
            duration.as_micros()
        );
    }

    HttpResponse::Ok().json(response)
}

#[post("/consolidate")]
pub async fn consolidate_handler(intent: web::Json<ConsolidationIntent>) -> impl Responder {
    println!("[DEBUG] accepting consolidate");
    let object_dependencies: Vec<ObjectId> = intent.args.clone();
    let activation_site =
        match orchestration::force_consolidate(intent.to_host, &object_dependencies).await {
            Some((activation_site, _)) => activation_site,
            None => {
                return HttpResponse::Conflict().body(format!(
                    "failed to consolidate objects {:?} to {}",
                    object_dependencies, intent.to_host
                ));
            }
        };

    HttpResponse::Ok().body(format!(
        "consolidated redir {}",
        resolve_host_idx!(activation_site)
    ))
}

#[post("/register_worker")]
pub async fn register_worker_handler(
    register_worker_request: web::Json<RegisterWorkerRequest>,
) -> impl Responder {
    // FIXME the response here should also contain a snapshot of the worker and ownership caches,
    // given that the worker that submitted this request did so during startup and hence only has a
    // view of its local ownership.
    println!(
        "accepting registration of worker '{}'",
        register_worker_request.hostname
    );
    match orchestration::register_worker(
        register_worker_request.host_idx,
        register_worker_request.hostname.clone(),
    ) {
        Ok(idx) => HttpResponse::Ok().json(&RegisterWorkerResponse { host_idx: idx }),
        // FIXME match on error type
        Err(e) => {
            eprintln!("Could not register worker: {}", e);
            HttpResponse::InternalServerError()
                .body("idx was taken or something (pls fix this error)")
        }
    }
}

#[post("/publish_object")]
pub async fn publish_handler(publish_request: web::Json<PublishRequest>) -> impl Responder {
    let ownership_registry = registry::OwnershipRegistry::get_ownership_registry().clone();
    let object_id = publish_request.object.get_object_id().clone();
    let host_idx = publish_request.host_idx;
    println!("Accepting publish of {object_id} from host with idx {host_idx}");

    // FIXME error handling (once we change the return type)
    if !ownership_registry.handle_publish(object_id, host_idx) {
        // FIXME response type
        return HttpResponse::Conflict().body(format!("{} has already been published", object_id));
    }

    // FIXME response type
    HttpResponse::Ok().body(format!("publish of {} ok", object_id,))
}

#[post("/publish_objects")]
pub async fn multi_publish_handler(
    publish_request: web::Json<MultiPublishRequest>,
) -> impl Responder {
    let ownership_registry = registry::OwnershipRegistry::get_ownership_registry().clone();
    let object_ids: Vec<ObjectId> = publish_request
        .objects
        .iter()
        .map(|o| o.get_object_id())
        .collect();
    let host_idx = publish_request.host_idx;
    println!(
        "Accepting multi-publish of {:?} from host with idx {host_idx}",
        object_ids
    );

    for object_id in object_ids.into_iter() {
        if !ownership_registry.handle_publish(object_id, host_idx) {
            // FIXME
            continue;
        }
    }

    // FIXME response type
    HttpResponse::Ok().body("Multi-publish ok")
}

#[get("/worker_mapping")]
pub async fn get_worker_mapping_handler() -> impl Responder {
    println!("getting worker mapping");
    let worker_mapping = WorkerMapping {
        mapping: orchestration::get_hostname_projection(),
    };

    HttpResponse::Ok().json(&worker_mapping)
}

// Only used for benchmarking runs
#[put("/reset_state")]
pub async fn reset_state_handler() -> impl Responder {
    orchestration::reset_host_mgr_state();

    let ownership_registry = registry::OwnershipRegistry::get_ownership_registry().clone();
    ownership_registry.reset_state();

    HttpResponse::Ok()
}

// Only used for benchmarking runs
#[put("/cache_all_owned_objects")]
pub async fn cache_all_owned_objects_handler() -> impl Responder {
    println!("About to spread caches around the cluster");
    let ownership_registry = registry::OwnershipRegistry::get_ownership_registry().clone();

    let host_projection = orchestration::get_hostname_projection();
    let ownership_map = ownership_registry.current_ownership.read();
    for (object_id, ownership_range) in &*ownership_map {
        let owning_host_idx = ownership_range.read().host_idx;
        println!("Will spawn caches for {object_id}");

        for (non_owner_host_idx, _non_owner_host) in &host_projection {
            if owning_host_idx == *non_owner_host_idx {
                continue;
            }

            let cached_object_id = vec![
                orchestration::request_cache_spawn(
                    *object_id,
                    owning_host_idx,
                    *non_owner_host_idx,
                )
                .await,
            ];
            println!("Done spawning cache, will request move");
            let ownership_change_result = orchestration::request_ownership_change(
                &cached_object_id,
                owning_host_idx,
                *non_owner_host_idx,
                false,
            )
            .await;
            let whomstone_version = ownership_change_result.get(0).unwrap();

            orchestration::notify_add_cache_mapping(
                *non_owner_host_idx,
                *object_id,
                cached_object_id[0],
                whomstone_version.1,
                owning_host_idx,
            )
            .await;

            println!("Moved object {object_id}");
        }
    }
    println!("Done spreading caches of owned objects around");

    HttpResponse::Ok()
}

#[put("/cache_single_object")]
pub async fn cache_object(intent: web::Json<ConsolidationIntent>) -> impl Responder {
    let object_to_cache: ObjectId = intent.args.get(0).unwrap().clone();
    println!("About to cache object {object_to_cache} cluster");
    let ownership_registry = registry::OwnershipRegistry::get_ownership_registry().clone();

    let owning_host_idx = ownership_registry
        .get_owning_host(&object_to_cache)
        .expect("attempt to cache non-published object");

    let host_projection = orchestration::get_hostname_projection();
    for (non_owner_host_idx, _non_owner_host) in &host_projection {
        if owning_host_idx == *non_owner_host_idx {
            continue;
        }

        let cached_object_id = vec![
            orchestration::request_cache_spawn(
                object_to_cache,
                owning_host_idx,
                *non_owner_host_idx,
            )
            .await,
        ];
        println!("Done spawning cache, will request move");
        let ownership_change_result = orchestration::request_ownership_change(
            &cached_object_id,
            owning_host_idx,
            *non_owner_host_idx,
            false,
        )
        .await;
        let whomstone_version = ownership_change_result.get(0).unwrap();

        orchestration::notify_add_cache_mapping(
            *non_owner_host_idx,
            object_to_cache,
            cached_object_id[0],
            whomstone_version.1,
            owning_host_idx,
        )
        .await;

        println!("Moved object {object_to_cache}");
    }
    println!("Done spreading caches of {object_to_cache} around");

    HttpResponse::Ok()
}

#[get("/location")]
pub async fn get_location_handler(
    intent: web::Json<NandoActivationIntentSerializable>,
) -> impl Responder {
    println!("Got request for location {:#?}", intent);
    #[cfg(feature = "timing")]
    let start = tokio::time::Instant::now();
    let object_dependencies = intent.get_object_references();
    let response = 'response: {
        if object_dependencies.len() == 0 {
            // randomly pick a host
            // FIXME this should pick the least-busy host
            let host_idx = {
                let mut host_idx_rng = SmallRng::from_entropy();
                host_idx_rng.gen::<u64>() % orchestration::get_num_workers() as u64
            };

            break 'response ScheduleResponse {
                target_host: resolve_host_idx!(host_idx),
                had_to_consolidate: false,
            };
        }

        let ownership_registry = registry::OwnershipRegistry::get_ownership_registry().clone();
        #[cfg(not(feature = "always_move"))]
        if let Some(h) = ownership_registry.objects_are_colocated(&object_dependencies) {
            break 'response ScheduleResponse {
                target_host: resolve_host_idx!(h),
                had_to_consolidate: false,
            };
        } else {
            return HttpResponse::Conflict().body(format!(
                "objects {:?} are not colocated",
                object_dependencies
            ));
        }
    };

    #[cfg(feature = "timing")]
    {
        let duration = start.elapsed();
        println!(
            "Took {}ns to service ({}us)",
            duration.as_nanos(),
            duration.as_micros()
        );
    }

    HttpResponse::Ok().json(response)
}

#[put("/peer_location_change")]
pub async fn peer_location_change_handler(
    intent: web::Json<ConsolidationIntent>,
) -> impl Responder {
    let object_to_move = intent.args.get(0).unwrap();
    let whomstone_version = intent.versions.get(0).unwrap();
    let target_host = intent.to_host;

    #[cfg(debug_assertions)]
    println!("Accepting peer location change of {object_to_move} to {target_host}");

    orchestration::peer_location_change(target_host, *object_to_move, *whomstone_version);

    HttpResponse::Ok()
}