kumo_server_common/
lib.rs

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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
use config::{
    any_err, decorate_callback_name, from_lua_value, get_or_create_module, load_config,
    CallbackSignature,
};
use mlua::{Function, Lua, LuaSerdeExt, Value, Variadic};
use mod_redis::RedisConnKey;
use serde::{Deserialize, Serialize};
use std::sync::atomic::AtomicUsize;
use tokio::task::LocalSet;

pub mod config_handle;
pub mod diagnostic_logging;
pub mod disk_space;
pub mod http_server;
pub mod nodeid;
pub mod panic;
pub mod start;
pub mod tls_helpers;

pub fn register(lua: &Lua) -> anyhow::Result<()> {
    for func in [
        mod_redis::register,
        data_loader::register,
        mod_digest::register,
        mod_encode::register,
        cidr_map::register,
        domain_map::register,
        mod_amqp::register,
        mod_filesystem::register,
        mod_http::register,
        mod_regex::register,
        mod_serde::register,
        mod_sqlite::register,
        mod_string::register,
        mod_time::register,
        mod_dns_resolver::register,
        mod_kafka::register,
        mod_memoize::register,
        mod_uuid::register,
        kumo_api_types::shaping::register,
        regex_set_map::register,
    ] {
        func(lua)?;
    }

    let kumo_mod = get_or_create_module(lua, "kumo")?;

    fn event_registrar_name(name: &str) -> String {
        format!("kumomta-event-registrars-{name}")
    }

    // Record the call stack of the code calling kumo.on so that
    // kumo.get_event_registrars can retrieve it later
    fn register_event_caller(lua: &Lua, name: &str) -> mlua::Result<()> {
        let decorated_name = event_registrar_name(name);
        let mut call_stack = vec![];
        for n in 1.. {
            match lua.inspect_stack(n) {
                Some(info) => {
                    let source = info.source();
                    call_stack.push(format!(
                        "{}:{}",
                        source
                            .short_src
                            .as_ref()
                            .map(|b| b.to_string())
                            .unwrap_or_else(String::new),
                        info.curr_line()
                    ));
                }
                None => break,
            }
        }

        let tbl: Value = lua.named_registry_value(&decorated_name)?;
        return match tbl {
            Value::Nil => {
                let tbl = lua.create_table()?;
                tbl.set(1, call_stack)?;
                lua.set_named_registry_value(&decorated_name, tbl)?;
                Ok(())
            }
            Value::Table(tbl) => {
                let len = tbl.raw_len();
                tbl.set(len + 1, call_stack)?;
                Ok(())
            }
            _ => Err(mlua::Error::external(format!(
                "registry key for {decorated_name} has invalid type",
            ))),
        };
    }

    // Returns the list of call-stacks of the code that registered
    // for a specific named event
    kumo_mod.set(
        "get_event_registrars",
        lua.create_function(move |lua, name: String| {
            let decorated_name = event_registrar_name(&name);
            let value: Value = lua.named_registry_value(&decorated_name)?;
            Ok(value)
        })?,
    )?;

    kumo_mod.set(
        "on",
        lua.create_function(move |lua, (name, func): (String, Function)| {
            let decorated_name = decorate_callback_name(&name);

            if let Ok(current_event) = lua.globals().get::<String>("_KUMO_CURRENT_EVENT") {
                if current_event != "main" {
                    return Err(mlua::Error::external(format!(
                        "Attempting to register an event handler via \
                    `kumo.on('{name}', ...)` from within the event handler \
                    '{current_event}'. You must move your event handler registration \
                    so that it is setup directly when the policy is loaded \
                    in order for it to consistently trigger and handle events."
                    )));
                }
            }

            register_event_caller(lua, &name)?;

            if config::does_callback_allow_multiple(&name) {
                let tbl: Value = lua.named_registry_value(&decorated_name)?;
                return match tbl {
                    Value::Nil => {
                        let tbl = lua.create_table()?;
                        tbl.set(1, func)?;
                        lua.set_named_registry_value(&decorated_name, tbl)?;
                        Ok(())
                    }
                    Value::Table(tbl) => {
                        let len = tbl.raw_len();
                        tbl.set(len + 1, func)?;
                        Ok(())
                    }
                    _ => Err(mlua::Error::external(format!(
                        "registry key for {decorated_name} has invalid type",
                    ))),
                };
            }

            let existing: Value = lua.named_registry_value(&decorated_name)?;
            match existing {
                Value::Nil => {}
                Value::Function(func) => {
                    let info = func.info();
                    let src = info.source.unwrap_or_else(|| "?".into());
                    let line = info.line_defined.unwrap_or(0);
                    return Err(mlua::Error::external(format!(
                        "{name} event already has a handler defined at {src}:{line}"
                    )));
                }
                _ => {
                    return Err(mlua::Error::external(format!(
                        "{name} event already has a handler"
                    )));
                }
            }

            lua.set_named_registry_value(&decorated_name, func)?;
            Ok(())
        })?,
    )?;

    kumo_mod.set(
        "set_diagnostic_log_filter",
        lua.create_function(move |_, filter: String| {
            diagnostic_logging::set_diagnostic_log_filter(&filter).map_err(any_err)
        })?,
    )?;

    fn variadic_to_string(args: Variadic<Value>) -> String {
        let mut output = String::new();
        for (idx, item) in args.into_iter().enumerate() {
            if idx > 0 {
                output.push(' ');
            }

            match item {
                Value::String(s) => match s.to_str() {
                    Ok(s) => output.push_str(&s),
                    Err(_) => {
                        let item = s.to_string_lossy();
                        output.push_str(&item);
                    }
                },
                item @ _ => match item.to_string() {
                    Ok(s) => output.push_str(&s),
                    Err(_) => output.push_str(&format!("{item:?}")),
                },
            }
        }
        output
    }

    fn get_caller(lua: &Lua) -> String {
        match lua.inspect_stack(1) {
            Some(info) => {
                let source = info.source();
                let file_name = source
                    .short_src
                    .as_ref()
                    .map(|b| b.to_string())
                    .unwrap_or_else(String::new);
                // Lua returns the somewhat obnoxious `[string "source.lua"]`
                // Let's fix that up to be a bit nicer
                let file_name = match file_name.strip_prefix("[string \"") {
                    Some(name) => name.strip_suffix("\"]").unwrap_or(name),
                    None => &file_name,
                };

                format!("{file_name}:{}", info.curr_line())
            }
            None => "?".to_string(),
        }
    }

    kumo_mod.set(
        "log_error",
        lua.create_function(move |lua, args: Variadic<Value>| {
            if tracing::event_enabled!(target: "lua", tracing::Level::ERROR) {
                let src = get_caller(lua);
                tracing::error!(target: "lua", "{src}: {}", variadic_to_string(args));
            }
            Ok(())
        })?,
    )?;
    kumo_mod.set(
        "log_info",
        lua.create_function(move |lua, args: Variadic<Value>| {
            if tracing::event_enabled!(target: "lua", tracing::Level::INFO) {
                let src = get_caller(lua);
                tracing::info!(target: "lua", "{src}: {}", variadic_to_string(args));
            }
            Ok(())
        })?,
    )?;
    kumo_mod.set(
        "log_warn",
        lua.create_function(move |lua, args: Variadic<Value>| {
            if tracing::event_enabled!(target: "lua", tracing::Level::WARN) {
                let src = get_caller(lua);
                tracing::warn!(target: "lua", "{src}: {}", variadic_to_string(args));
            }
            Ok(())
        })?,
    )?;
    kumo_mod.set(
        "log_debug",
        lua.create_function(move |lua, args: Variadic<Value>| {
            if tracing::event_enabled!(target: "lua", tracing::Level::DEBUG) {
                let src = get_caller(lua);
                tracing::debug!(target: "lua", "{src}: {}", variadic_to_string(args));
            }
            Ok(())
        })?,
    )?;

    kumo_mod.set(
        "set_max_spare_lua_contexts",
        lua.create_function(move |_, limit: usize| {
            config::set_max_spare(limit);
            Ok(())
        })?,
    )?;

    kumo_mod.set(
        "set_max_lua_context_use_count",
        lua.create_function(move |_, limit: usize| {
            config::set_max_use(limit);
            Ok(())
        })?,
    )?;

    kumo_mod.set(
        "set_max_lua_context_age",
        lua.create_function(move |_, limit: usize| {
            config::set_max_age(limit);
            Ok(())
        })?,
    )?;

    kumo_mod.set(
        "set_lua_gc_on_put",
        lua.create_function(move |_, enable: u8| {
            config::set_gc_on_put(enable);
            Ok(())
        })?,
    )?;

    kumo_mod.set(
        "set_lruttl_cache_capacity",
        lua.create_function(move |_, (name, capacity): (String, usize)| {
            if lruttl::set_cache_capacity(&name, capacity) {
                Ok(())
            } else {
                Err(mlua::Error::external(format!(
                    "could not set capacity for cache {name} \
                    as that is not a pre-defined lruttl cache name"
                )))
            }
        })?,
    )?;

    kumo_mod.set(
        "set_config_monitor_globs",
        lua.create_function(move |_, globs: Vec<String>| {
            config::epoch::set_globs(globs).map_err(any_err)?;
            Ok(())
        })?,
    )?;
    kumo_mod.set(
        "eval_config_monitor_globs",
        lua.create_async_function(|_, _: ()| async move {
            config::epoch::eval_globs().await.map_err(any_err)
        })?,
    )?;
    kumo_mod.set(
        "bump_config_epoch",
        lua.create_function(move |_, _: ()| {
            config::epoch::bump_current_epoch();
            Ok(())
        })?,
    )?;

    kumo_mod.set(
        "available_parallelism",
        lua.create_function(move |_, _: ()| {
            Ok(std::thread::available_parallelism().map_err(any_err)?.get())
        })?,
    )?;

    kumo_mod.set(
        "set_memory_hard_limit",
        lua.create_function(move |_, limit: usize| {
            kumo_server_memory::set_hard_limit(limit);
            Ok(())
        })?,
    )?;

    kumo_mod.set(
        "set_memory_low_thresh",
        lua.create_function(move |_, limit: usize| {
            kumo_server_memory::set_low_memory_thresh(limit);
            Ok(())
        })?,
    )?;

    kumo_mod.set(
        "set_memory_soft_limit",
        lua.create_function(move |_, limit: usize| {
            kumo_server_memory::set_soft_limit(limit);
            Ok(())
        })?,
    )?;

    kumo_mod.set(
        "configure_redis_throttles",
        lua.create_async_function(|lua, params: Value| async move {
            let key: RedisConnKey = from_lua_value(&lua, params)?;
            let conn = key.open().map_err(any_err)?;
            conn.ping().await.map_err(any_err)?;
            throttle::use_redis(conn).await.map_err(any_err)
        })?,
    )?;

    kumo_mod.set(
        "traceback",
        lua.create_function(move |lua: &Lua, level: usize| {
            #[derive(Debug, Serialize)]
            struct Frame {
                event: String,
                name: Option<String>,
                name_what: Option<String>,
                source: Option<String>,
                short_src: Option<String>,
                line_defined: Option<usize>,
                last_line_defined: Option<usize>,
                what: &'static str,
                curr_line: i32,
                is_tail_call: bool,
            }

            let mut frames = vec![];
            for n in level.. {
                match lua.inspect_stack(n) {
                    Some(info) => {
                        let source = info.source();
                        let names = info.names();
                        frames.push(Frame {
                            curr_line: info.curr_line(),
                            is_tail_call: info.is_tail_call(),
                            event: format!("{:?}", info.event()),
                            last_line_defined: source.last_line_defined,
                            line_defined: source.line_defined,
                            name: names.name.as_ref().map(|b| b.to_string()),
                            name_what: names.name_what.as_ref().map(|b| b.to_string()),
                            source: source.source.as_ref().map(|b| b.to_string()),
                            short_src: source.short_src.as_ref().map(|b| b.to_string()),
                            what: source.what,
                        });
                    }
                    None => break,
                }
            }

            lua.to_value(&frames)
        })?,
    )?;

    // TODO: options like restarting on error, delay between
    // restarts and so on
    #[derive(Deserialize, Debug)]
    struct TaskParams {
        event_name: String,
        args: Vec<serde_json::Value>,
    }

    impl TaskParams {
        async fn run(&self) -> anyhow::Result<()> {
            let mut config = load_config().await?;

            let sig = CallbackSignature::<Value, ()>::new(self.event_name.to_string());

            config
                .convert_args_and_call_callback(&sig, &self.args)
                .await?;

            config.put();

            Ok(())
        }
    }

    kumo_mod.set(
        "spawn_task",
        lua.create_function(|lua, params: Value| {
            let params: TaskParams = lua.from_value(params)?;

            if !config::is_validating() {
                std::thread::Builder::new()
                    .name(format!("spawned-task-{}", params.event_name))
                    .spawn(move || {
                        let runtime = tokio::runtime::Builder::new_current_thread()
                            .enable_io()
                            .enable_time()
                            .on_thread_park(|| kumo_server_memory::purge_thread_cache())
                            .build()
                            .unwrap();
                        let local_set = LocalSet::new();
                        let event_name = params.event_name.clone();

                        let result =
                            local_set.block_on(&runtime, async move { params.run().await });
                        if let Err(err) = result {
                            tracing::error!("Error while dispatching {event_name}: {err:#}");
                        }
                    })?;
            }

            Ok(())
        })?,
    )?;

    kumo_mod.set(
        "spawn_thread_pool",
        lua.create_function(|lua, params: Value| {
            #[derive(Deserialize, Debug)]
            struct ThreadPoolParams {
                name: String,
                num_threads: usize,
            }

            let params: ThreadPoolParams = lua.from_value(params)?;
            let num_threads = AtomicUsize::new(params.num_threads);

            if !config::is_validating() {
                // Create the runtime. We don't need to hold on
                // to it here, as it will be kept alive in the
                // runtimes map in that crate
                let _runtime = kumo_server_runtime::Runtime::new(
                    &params.name,
                    |_| params.num_threads,
                    &num_threads,
                )
                .map_err(any_err)?;
            }

            Ok(())
        })?,
    )?;

    kumo_mod.set(
        "validation_failed",
        lua.create_function(|_, ()| {
            config::set_validation_failed();
            Ok(())
        })?,
    )?;

    kumo_mod.set(
        "enable_memory_callstack_tracking",
        lua.create_function(|_, enable: bool| {
            kumo_server_memory::set_tracking_callstacks(enable);
            Ok(())
        })?,
    )?;

    Ok(())
}