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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

use std::collections::{HashMap, HashSet};
use std::ffi::OsStr;
use std::fmt::Debug;
use std::fs;
use std::net::{IpAddr, Ipv4Addr};
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::sync::{Arc, Mutex};

use anyhow::anyhow;
use async_trait::async_trait;
use chrono::Utc;
use futures::stream::BoxStream;
use itertools::Itertools;
use scopeguard::defer;
use sysinfo::{ProcessExt, ProcessStatus, SystemExt};
use tokio::process::{Child, Command};
use tokio::task::JoinHandle;
use tokio::time::{self, Duration};
use tracing::{error, info};

use mz_orchestrator::{
    NamespacedOrchestrator, Orchestrator, Service, ServiceAssignments, ServiceConfig, ServiceEvent,
    ServiceStatus,
};
use mz_ore::id_gen::PortAllocator;
use mz_pid_file::PidFile;

use crate::port_metadata_file::PortMetadataFile;

pub mod port_metadata_file;

/// Configures a [`ProcessOrchestrator`].
#[derive(Debug, Clone)]
pub struct ProcessOrchestratorConfig {
    /// The directory in which the orchestrator should look for executable
    /// images.
    pub image_dir: PathBuf,
    /// The ports to allocate.
    pub port_allocator: Arc<PortAllocator>,
    /// Whether to supress output from spawned subprocesses.
    pub suppress_output: bool,
    /// The directory in which the orchestrator should look for process
    /// lock files.
    pub data_dir: PathBuf,
    /// A command to wrap the child command invocation
    pub command_wrapper: Vec<String>,
}

/// An orchestrator backed by processes on the local machine.
///
/// **This orchestrator is for development only.** Due to limitations in the
/// Unix process API, it does not exactly conform to the documented semantics
/// of `Orchestrator`.
///
/// Processes launched by this orchestrator must support a `--pid-file-location`
/// command line flag which causes a PID file to be emitted at the specified
/// path.
#[derive(Debug)]
pub struct ProcessOrchestrator {
    image_dir: PathBuf,
    port_allocator: Arc<PortAllocator>,
    suppress_output: bool,
    namespaces: Mutex<HashMap<String, Arc<dyn NamespacedOrchestrator>>>,
    data_dir: PathBuf,
    command_wrapper: Vec<String>,
}

impl ProcessOrchestrator {
    /// Creates a new process orchestrator from the provided configuration.
    pub async fn new(
        ProcessOrchestratorConfig {
            image_dir,
            port_allocator,
            suppress_output,
            data_dir,
            command_wrapper,
        }: ProcessOrchestratorConfig,
    ) -> Result<ProcessOrchestrator, anyhow::Error> {
        Ok(ProcessOrchestrator {
            image_dir: fs::canonicalize(image_dir)?,
            port_allocator,
            suppress_output,
            namespaces: Mutex::new(HashMap::new()),
            data_dir: fs::canonicalize(data_dir)?,
            command_wrapper,
        })
    }
}

impl Orchestrator for ProcessOrchestrator {
    fn namespace(&self, namespace: &str) -> Arc<dyn NamespacedOrchestrator> {
        let mut namespaces = self.namespaces.lock().expect("lock poisoned");
        Arc::clone(namespaces.entry(namespace.into()).or_insert_with(|| {
            Arc::new(NamespacedProcessOrchestrator {
                namespace: namespace.into(),
                image_dir: self.image_dir.clone(),
                port_allocator: Arc::clone(&self.port_allocator),
                suppress_output: self.suppress_output,
                supervisors: Arc::new(Mutex::new(HashMap::new())),
                data_dir: self.data_dir.clone(),
                command_wrapper: self.command_wrapper.clone(),
            })
        }))
    }
}

#[derive(Debug)]
struct NamespacedProcessOrchestrator {
    namespace: String,
    image_dir: PathBuf,
    port_allocator: Arc<PortAllocator>,
    suppress_output: bool,
    supervisors: Arc<Mutex<HashMap<String, Vec<AbortOnDrop>>>>,
    data_dir: PathBuf,
    command_wrapper: Vec<String>,
}

#[async_trait]
impl NamespacedOrchestrator for NamespacedProcessOrchestrator {
    async fn ensure_service(
        &self,
        id: &str,
        ServiceConfig {
            image,
            args,
            ports: ports_in,
            memory_limit: _,
            cpu_limit: _,
            scale: scale_in,
            labels: _,
            availability_zone: _,
        }: ServiceConfig<'_>,
    ) -> Result<Box<dyn Service>, anyhow::Error> {
        let full_id = format!("{}-{}", self.namespace, id);
        let mut supervisors = self.supervisors.lock().expect("lock poisoned");
        if supervisors.contains_key(id) {
            unimplemented!("ProcessOrchestrator does not yet support updating existing services");
        }
        let path = self.image_dir.join(image);
        let mut processes = vec![];
        let mut handles = vec![];

        let system = sysinfo::System::new_all();

        // Decide on port mappings, and detect any non-dead processes that still exist.
        // Any such processes will be left alone; any others will be (re-)created.
        let mut processes_exist = vec![true; scale_in.get()];
        let mut pid_file_locations = vec![None; scale_in.get()];
        let mut port_metadata_file_locations = vec![None; scale_in.get()];
        for i in 0..(scale_in.get()) {
            let process_file_name = format!("{}-{}-{}", self.namespace, id, i);
            let pid_file_location = self.data_dir.join(format!("{}.pid", process_file_name));
            pid_file_locations[i] = Some(pid_file_location.clone());
            let port_metadata_file_location =
                self.data_dir.join(format!("{}.ports", process_file_name));
            port_metadata_file_locations[i] = Some(port_metadata_file_location.clone());

            if pid_file_location.exists() && port_metadata_file_location.exists() {
                let pid = PidFile::read(&pid_file_location)?;
                let port_metadata = PortMetadataFile::read(&port_metadata_file_location)?;
                if let Some(process) = system.process(pid.into()) {
                    if process.exe() == path {
                        if process.status() == ProcessStatus::Dead
                            || process.status() == ProcessStatus::Zombie
                        {
                            // Existing dead process, so we try and kill it and create a new one later
                            process.kill();
                        } else {
                            // Existing non-dead process, so we don't create a new one
                            for port in port_metadata.values() {
                                if !self.port_allocator.mark_allocated(*port) {
                                    // Somehow we've re-allocated an already used port which
                                    // shouldn't be possible. So we just kill the process and panic.
                                    process.kill();
                                    panic!("port re-use");
                                }
                            }
                            handles.push(AbortOnDrop(Box::new(ExternalProcess {
                                pid,
                                _port_metadata_file: PortMetadataFile::open_existing(
                                    port_metadata_file_location,
                                ),
                            })));
                            continue;
                        }
                    }
                }
            }
            // If we got here, we didn't find evidence of a live process, so we must (re-)create it later.
            processes_exist[i] = false;

            let mut ports = HashMap::new();
            for port in &ports_in {
                let p = self
                    .port_allocator
                    .alloc()
                    .ok_or_else(|| anyhow!("port exhaustion"))?;
                ports.insert(port.name.clone(), p);
            }
            processes.push(ports.clone());
        }

        // Now create all the processes that weren't detected as being still alive
        let peers = processes
            .iter()
            .map(|ports| ("localhost".to_string(), ports.clone()))
            .collect::<Vec<_>>();
        for i in 0..(scale_in.get()) {
            if !processes_exist[i] {
                let mut args = args(&ServiceAssignments {
                    listen_host: IpAddr::V4(Ipv4Addr::LOCALHOST),
                    ports: &peers[i].1,
                    index: Some(i),
                    peers: &peers,
                });
                args.push(format!(
                    "--pid-file-location={}",
                    pid_file_locations[i].as_ref().unwrap().display()
                ));
                args.push(format!(
                    "--secrets-path={}",
                    // TODO(benesch): avoid hardcoding this path. The
                    // integration between the orchestrator and secrets
                    // controller is about to change substantially, so I don't
                    // want to do this now.
                    self.data_dir.join("secrets").display()
                ));

                let command_wrapper = self.command_wrapper.clone();
                handles.push(AbortOnDrop(Box::new(mz_ore::task::spawn(
                    || format!("service-supervisor: {full_id}"),
                    supervise(
                        full_id.clone(),
                        path.clone(),
                        args.clone(),
                        command_wrapper,
                        Arc::clone(&self.port_allocator),
                        processes[i].clone(),
                        self.suppress_output,
                        std::mem::take(port_metadata_file_locations[i].as_mut().unwrap()),
                    ),
                ))));
            }
        }
        supervisors.insert(id.into(), handles);
        Ok(Box::new(ProcessService { processes }))
    }

    async fn drop_service(&self, id: &str) -> Result<(), anyhow::Error> {
        let mut supervisors = self.supervisors.lock().expect("lock poisoned");
        supervisors.remove(id);
        Ok(())
    }

    async fn list_services(&self) -> Result<Vec<String>, anyhow::Error> {
        let supervisors = self.supervisors.lock().expect("lock poisoned");
        Ok(supervisors.keys().cloned().collect())
    }

    fn watch_services(&self) -> BoxStream<'static, Result<ServiceEvent, anyhow::Error>> {
        // The process orchestrator currently doesn't provide good support for
        // tracking service status, so we punt and always return an "unknown"
        // status instead. We can still report the existence of individual
        // processes though.

        let supervisors = Arc::clone(&self.supervisors);
        let stream = async_stream::stream! {
            let mut events = Vec::new();
            // Keep track of services we have already seen, so we only emit
            // events for new services.
            let mut current_services = HashSet::new();
            loop {
                {
                    let supervisors = supervisors.lock().expect("lock poisoned");
                    for (service, processes) in supervisors.iter() {
                        if current_services.contains(service) {
                            continue;
                        }
                        for process_idx in 0..processes.len() {
                            events.push(ServiceEvent {
                                service_id: service.clone(),
                                process_id: process_idx as i64,
                                status: ServiceStatus::Unknown,
                                time: Utc::now(),
                            });
                        }
                    }
                    current_services = supervisors.keys().cloned().collect();
                }

                for event in events.drain(..) {
                    yield Ok(event);
                }

                time::sleep(Duration::from_secs(5)).await;
            }
        };
        Box::pin(stream)
    }
}

async fn supervise(
    full_id: String,
    path: impl AsRef<OsStr>,
    args: Vec<impl AsRef<OsStr>>,
    command_wrapper: Vec<String>,
    port_allocator: Arc<PortAllocator>,
    ports: HashMap<String, u16>,
    suppress_output: bool,
    port_metadata_file_location: PathBuf,
) {
    fn interpolate_command(
        command_part: &str,
        full_id: &str,
        ports: &HashMap<String, u16>,
    ) -> String {
        let mut command_part = command_part.replace("%N", full_id);
        for (endpoint, port) in ports {
            command_part = command_part.replace(&format!("%P:{endpoint}"), &format!("{port}"));
        }
        command_part
    }

    defer! {
        for port in ports.values() {
            port_allocator.free(*port);
        }
    }
    let _port_metadata_file = PortMetadataFile::open(&port_metadata_file_location, &ports)
        .unwrap_or_else(|_| {
            panic!(
                "unable to create port metadata file {}",
                port_metadata_file_location.as_os_str().to_str().unwrap()
            )
        });
    loop {
        let mut cmd = if command_wrapper.is_empty() {
            let mut cmd = Command::new(&path);
            cmd.args(&args);
            cmd
        } else {
            let mut cmd = Command::new(&command_wrapper[0]);
            let path = path.as_ref();
            cmd.args(
                command_wrapper[1..]
                    .iter()
                    .map(|part| interpolate_command(part, &full_id, &ports)),
            );
            cmd.arg(path);
            cmd.args(args.iter().map(AsRef::as_ref));
            cmd
        };
        info!(
            "Launching {}: {}...",
            full_id,
            cmd.as_std()
                .get_args()
                .map(|arg| arg.to_string_lossy())
                .join(" ")
        );
        if suppress_output {
            cmd.stdout(Stdio::null());
            cmd.stderr(Stdio::null());
        }
        match cmd.spawn() {
            Ok(process) => {
                let status = KillOnDropChild(process).0.wait().await;
                error!("{} exited: {:?}; relaunching in 5s", full_id, status);
            }
            Err(e) => {
                error!("{} failed to launch: {}; relaunching in 5s", full_id, e);
            }
        };
        time::sleep(Duration::from_secs(5)).await;
    }
}

struct KillOnDropChild(Child);

impl Drop for KillOnDropChild {
    fn drop(&mut self) {
        let _ = self.0.start_kill();
    }
}

trait Abortable: Send + Sync + Debug {
    fn abort_process(&self);
}

impl<T: Send + Sync + Debug> Abortable for JoinHandle<T> {
    fn abort_process(&self) {
        self.abort();
    }
}

#[derive(Debug)]
struct ExternalProcess<P>
where
    P: AsRef<Path> + Debug,
{
    pid: i32,
    _port_metadata_file: PortMetadataFile<P>,
}

impl<P> Abortable for ExternalProcess<P>
where
    P: AsRef<Path> + Debug + Send + Sync,
{
    fn abort_process(&self) {
        let system = sysinfo::System::new_all();
        if let Some(process) = system.process(self.pid.into()) {
            process.kill();
        }
    }
}

#[derive(Debug)]
struct AbortOnDrop(Box<dyn Abortable>);

impl Drop for AbortOnDrop {
    fn drop(&mut self) {
        self.0.abort_process();
    }
}

#[derive(Debug, Clone)]
struct ProcessService {
    /// For each process in order, the allocated ports by name.
    processes: Vec<HashMap<String, u16>>,
}

impl Service for ProcessService {
    fn addresses(&self, port: &str) -> Vec<String> {
        self.processes
            .iter()
            .map(|p| format!("localhost:{}", p[port]))
            .collect()
    }
}