From 2ca97d8c4d38293859f1f35893028ca64d7d7d11 Mon Sep 17 00:00:00 2001 From: "Dmitry E. Oboukhov" <unera@debian.org> Date: Wed, 29 Aug 2012 01:00:40 +0400 Subject: [PATCH] box.info realized --- include/tarantool.h | 1 + include/tarantool_lua.h | 8 ++++ mod/box/CMakeLists.txt | 1 + mod/box/box.m | 4 ++ mod/box/box_lua.m | 6 ++- mod/box/box_lua_info.h | 7 ++++ mod/box/box_lua_info.m | 90 +++++++++++++++++++++++++++++++++++++++++ src/tarantool_lua.m | 2 +- test/box/info.result | 40 ++++++++++++++++++ test/box/info.test | 16 ++++++++ test/box/lua.result | 1 + 11 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 mod/box/box_lua_info.h create mode 100644 mod/box/box_lua_info.m create mode 100644 test/box/info.result create mode 100644 test/box/info.test diff --git a/include/tarantool.h b/include/tarantool.h index 5abc213667..675ddfb9c3 100644 --- a/include/tarantool.h +++ b/include/tarantool.h @@ -45,6 +45,7 @@ i32 mod_reload_config(struct tarantool_cfg *old_conf, struct tarantool_cfg *new_ int mod_cat(const char *filename); void mod_snapshot(struct log_io *, struct nbatch *batch); void mod_info(struct tbuf *out); +const char * mod_status(void); extern struct tarantool_cfg cfg; extern const char *cfg_filename; diff --git a/include/tarantool_lua.h b/include/tarantool_lua.h index 83aefc03e6..53c65fb2ec 100644 --- a/include/tarantool_lua.h +++ b/include/tarantool_lua.h @@ -102,4 +102,12 @@ void tarantool_lua(struct lua_State *L, struct tbuf *out, const char *str); +/** + * push uint64_t to Lua stack + * + * @param L is a Lua State + * @param val is a value to push + * + */ +int luaL_pushnumber64(struct lua_State *L, uint64_t val); #endif /* INCLUDES_TARANTOOL_LUA_H */ diff --git a/mod/box/CMakeLists.txt b/mod/box/CMakeLists.txt index 9b3ec9b45f..477404b273 100644 --- a/mod/box/CMakeLists.txt +++ b/mod/box/CMakeLists.txt @@ -39,4 +39,5 @@ set_source_files_properties(memcached.m PROPERTIES COMPILE_FLAGS "-Wno-uninitialized") tarantool_module("box" tuple.m index.m tree.m space.m port.m request.m + box_lua_info.m txn.m box.m box.lua.c box_lua.m memcached.m memcached-grammar.m) diff --git a/mod/box/box.m b/mod/box/box.m index 5bacba6c96..c8c96c77a1 100644 --- a/mod/box/box.m +++ b/mod/box/box.m @@ -585,3 +585,7 @@ mod_info(struct tbuf *out) tbuf_printf(out, " status: %s" CRLF, status); } + +const char * mod_status(void) { + return status; +} diff --git a/mod/box/box_lua.m b/mod/box/box_lua.m index 3d8b9caea2..aac3cdab94 100644 --- a/mod/box/box_lua.m +++ b/mod/box/box_lua.m @@ -46,7 +46,7 @@ #include "tuple.h" #include "space.h" #include "port.h" - +#include "box_lua_info.h" /* contents of box.lua */ extern const char box_lua[]; @@ -1049,7 +1049,11 @@ mod_lua_init(struct lua_State *L) /* Load box.lua */ if (luaL_dostring(L, box_lua)) panic("Error loading box.lua: %s", lua_tostring(L, -1)); + + lbox_info_init(L); + assert(lua_gettop(L) == 0); + return L; } diff --git a/mod/box/box_lua_info.h b/mod/box/box_lua_info.h new file mode 100644 index 0000000000..c1b4bef531 --- /dev/null +++ b/mod/box/box_lua_info.h @@ -0,0 +1,7 @@ +#ifndef __BOX_LUA_INFO_H__ +#define __BOX_LUA_INFO_H__ + +struct lua_State; +void lbox_info_init(struct lua_State *L); + +#endif /* __BOX_LUA_INFO_H__ */ diff --git a/mod/box/box_lua_info.m b/mod/box/box_lua_info.m new file mode 100644 index 0000000000..c088274ded --- /dev/null +++ b/mod/box/box_lua_info.m @@ -0,0 +1,90 @@ +#include "box_lua_info.h" +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" +#include <tarantool_lua.h> +#include <tarantool.h> +#include <say.h> +#include <string.h> +#include <recovery.h> + + +static int lbox_info_index(struct lua_State *L) { + + const char *key = lua_tolstring(L, -1, NULL); + + if (strcmp(key, "lsn") == 0) { + luaL_pushnumber64(L, recovery_state->confirmed_lsn); + return 1; + } + + if (strcmp(key, "recovery_lag") == 0) { + + if (recovery_state->remote) + lua_pushnumber(L, recovery_state->remote->recovery_lag); + else + lua_pushnil(L); + return 1; + } + + if (strcmp(key, "recovery_last_update") == 0) { + + if (recovery_state->remote) + lua_pushnumber(L, + recovery_state->remote->recovery_last_update_tstamp); + else + lua_pushnil(L); + return 1; + } + + if (strcmp(key, "status") == 0) { + lua_pushstring(L, mod_status()); + return 1; + } + + + return 0; +} + + +static const struct luaL_reg lbox_info_meta [] = { + {"__index", lbox_info_index}, + {NULL, NULL} +}; + +void lbox_info_init(struct lua_State *L) { + + lua_getfield(L, LUA_GLOBALSINDEX, "box"); + + lua_pushstring(L, "info"); + lua_newtable(L); + + lua_newtable(L); + luaL_register(L, NULL, lbox_info_meta); + lua_setmetatable(L, -2); + + + /* tarantool version */ + lua_pushstring(L, "version"); + lua_pushstring(L, tarantool_version()); + lua_settable(L, -3); + + /* pid */ + lua_pushstring(L, "pid"); + lua_pushnumber(L, getpid()); + lua_settable(L, -3); + + /* logger_pid */ + lua_pushstring(L, "logger_pid"); + lua_pushnumber(L, logger_pid); + lua_settable(L, -3); + + /* config */ + lua_pushstring(L, "config"); + lua_pushstring(L, cfg_filename_fullpath); + lua_settable(L, -3); + + + lua_settable(L, -3); /* box.info = created table */ + lua_pop(L, 1); /* cleanup stack */ +} diff --git a/src/tarantool_lua.m b/src/tarantool_lua.m index 679a531473..c5509930e6 100644 --- a/src/tarantool_lua.m +++ b/src/tarantool_lua.m @@ -299,7 +299,7 @@ luaL_pushcdata(struct lua_State *L, CTypeID id, int bits) return cd; } -static int +int luaL_pushnumber64(struct lua_State *L, uint64_t val) { GCcdata *cd = luaL_pushcdata(L, CTID_UINT64, 8); diff --git a/test/box/info.result b/test/box/info.result new file mode 100644 index 0000000000..b078877074 --- /dev/null +++ b/test/box/info.result @@ -0,0 +1,40 @@ +lua +--- +unknown command. try typing help. +... +lua 1 +--- + - 1 +... +lua box.info.version +--- + - 1.4.7-92-g4ba95ca +... +lua string.match(box.info.pid, '^[1-9][0-9]*$') ~= nil +--- + - true +... +lua string.match(box.info.logger_pid, '^[1-9][0-9]*$') ~= nil +--- + - true +... +lua box.info.lsn > 0 +--- + - true +... +lua box.info.recovery_lag +--- + - nil +... +lua box.info.recovery_last_update +--- + - nil +... +lua box.info.status +--- + - primary +... +lua box.info.config +--- + - /home/unera/work/tarantool/test/var/tarantool.cfg +... diff --git a/test/box/info.test b/test/box/info.test new file mode 100644 index 0000000000..1690d68c8d --- /dev/null +++ b/test/box/info.test @@ -0,0 +1,16 @@ +# encoding: tarantool +import os +import sys +# Test Lua from admin console. Whenever producing output, +# make sure it's a valid YAML. +exec admin "lua" +exec admin "lua 1" +exec admin "lua box.info.version" +exec admin "lua string.match(box.info.pid, '^[1-9][0-9]*$') ~= nil" +exec admin "lua string.match(box.info.logger_pid, '^[1-9][0-9]*$') ~= nil" +exec admin "lua box.info.lsn > 0" +exec admin "lua box.info.recovery_lag" +exec admin "lua box.info.recovery_last_update" +exec admin "lua box.info.status" +exec admin "lua box.info.config" + diff --git a/test/box/lua.result b/test/box/lua.result index 7d77cb8f84..31566418fc 100644 --- a/test/box/lua.result +++ b/test/box/lua.result @@ -20,6 +20,7 @@ lua for n in pairs(box) do print(' - box.', n) end - box.replace - box.update - box.insert + - box.info - box.space - box.cfg - box.dostring -- GitLab