{"record":{"id":"2f4764886dea2951","repo":"databendlabs/databend","slug":"failed-to-register-now-ms-function","errorCode":null,"errorMessage":"Failed to register now_ms function: {}","messagePattern":"Failed to register now_ms function: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/meta/control/src/lua_support.rs","lineNumber":474,"sourceCode":"            Ok(())\n        })\n        .map_err(|e| anyhow::anyhow!(\"Failed to create sleep function: {}\", e))?;\n\n    metactl_table\n        .set(\"sleep\", sleep_fn)\n        .map_err(|e| anyhow::anyhow!(\"Failed to register sleep function: {}\", e))?;\n\n    // Register a monotonic millisecond clock for timing and benchmarks. The\n    // epoch is this environment's setup time; only differences between calls are\n    // meaningful. `Instant` guarantees the value never goes backwards.\n    let start = std::time::Instant::now();\n    let now_ms_fn = lua\n        .create_function(move |_lua, ()| Ok(start.elapsed().as_secs_f64() * 1000.0))\n        .map_err(|e| anyhow::anyhow!(\"Failed to create now_ms function: {}\", e))?;\n\n    metactl_table\n        .set(\"now_ms\", now_ms_fn)\n        .map_err(|e| anyhow::anyhow!(\"Failed to register now_ms function: {}\", e))?;\n\n    // Register the Zipf load generator. The module returns the `ZipfGenerator`\n    // table, exposed as `metactl.ZipfGenerator` so any script can call\n    // `metactl.ZipfGenerator:new(num_keys, alpha)` directly.\n    let zipf_generator = lua\n        .load(ZIPF_LOAD_GENERATOR)\n        .eval::<Table>()\n        .map_err(|e| anyhow::anyhow!(\"Failed to load zipf_load_generator: {}\", e))?;\n\n    metactl_table\n        .set(\"ZipfGenerator\", zipf_generator)\n        .map_err(|e| anyhow::anyhow!(\"Failed to register ZipfGenerator: {}\", e))?;\n\n    // Set the metactl table as a global\n    lua.globals()\n        .set(\"metactl\", metactl_table)\n        .map_err(|e| anyhow::anyhow!(\"Failed to register metactl namespace: {}\", e))?;\n","sourceCodeStart":456,"sourceCodeEnd":492,"githubUrl":"https://github.com/databendlabs/databend/blob/288d84d76e20a2f8f7173bda9691eb6ece301aa9/src/meta/control/src/lua_support.rs#L456-L492","documentation":"After creation, the `now_ms` closure is stored into the `metactl` table with `Table::set`. If mlua fails to push or store that function value, the error is surfaced as `Failed to register now_ms function` and setup aborts before scripts execute.","triggerScenarios":"`metactl_table.set(\"now_ms\", now_ms_fn)` returning an mlua::Error during `setup_lua_environment`, i.e. during any Lua-script run or its unit tests.","commonSituations":"Lua stack/memory exhaustion in the shared Lua instance, typically caused by an earlier script that exhausted resources or corrupted the state.","solutions":["Inspect the inner mlua error; recreate the Lua instance for memory/stack errors.","Ensure `now_ms_fn` and `metactl_table` come from the same `Lua` instance.","Raise or remove custom Lua memory limits if the inner error is a memory error.","If it reproduces on a clean state, collect the inner error and report/upgrade mlua."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if let Err(e) = setup_lua_environment(&lua) {\n    if e.to_string().contains(\"Failed to register now_ms function\") {\n        // recreate the Lua environment before retrying the script\n    }\n}","preventionTips":["Avoid reusing a Lua state after a fatal error; rebuild it.","Do not set restrictive MemoryLimits on the runtime.","Create the value and the target table in the same Lua instance.","Always inspect the wrapped inner mlua error."],"tags":["lua","mlua","runtime-setup","rust"],"backgroundTag":"module-init-failed","analyzedSha":"288d84d76e20a2f8f7173bda9691eb6ece301aa9","analyzedAt":"2026-09-11T11:29:36.208Z","contentChangedAt":"2026-09-11T11:29:36.208Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}