[PATCH v2 0/9] box: rework functions machinery

Kirill Shcherbatov kshcherbatov at tarantool.org
Thu Jun 6 15:03:56 MSK 2019


This patchset introduces a set of changes in Tarantool function
machinery. We need them to implement functional indexes in future.
The reworked func object acts like a function frontend and allows
to call a function on any supported language and return result to
obuf, Lua and region.

1. At first, the format of _func space is changed. A new space
format is
[<id> UINT, <owner> UINT, <name> STR, <setuid> UINT,
  <language> STR, <body> STR, <returns> STR,
  <is_deterministic> BOOL, <opts> MAP]

and box.schema.func.create endpoint is changed correspondingly:

box.schema.func.create('funcname', <setuid = true|FALSE>,
                <if_not_exists = true|FALSE>, <language = LUA|c>,
                <body = string ('')>, <returns = string (ANY)>,
                <is_deterministic = true|FALSE>,
                <opts = { <is_sandboxed = true|FALSE>} >)

2. Now all registered with box.schema.func.create functions are
exported in box.func folder.
Each function have :call and :drop method. The :drop method
just a shortcut for legacy box.schema.func.drop interface.
The :call method is similar to net.box connection:call method
except the format or returned value: the func:call method
returns a table of values like space:select does.

3. This patchset also introduces 'persistent' Lua functions.
Such functions are stored in snapshoot and are available after
restart.
To create a persistent Lua function, specify function body
in box.schema.func.create call:
e.g. body = "function(a, b) return a + b end"

4. A Lua persistent function may be 'sandboxed'. The 'sandboxed'
function executed in isolated environment:
  a. only limited set of Lua functions and modules are available:
    -assert -error -pairs -ipairs -next -pcall -xpcall -type
    -print -select -string -tonumber -tostring -unpack -math -utf8;
  b. global variables are forbidden

Branch: http://github.com/tarantool/tarantool/tree/kshch/gh-4182-persistent-lua-functions
Issue: https://github.com/tarantool/tarantool/issues/4182

Kirill Shcherbatov (9):
  box: refactor box_lua_find helper
  box: move box_module_reload routine to func.c
  box: rework func cache update machinery
  box: rework func object as a function frontend
  schema: rework _func system space format
  box: load persistent Lua functions on creation
  box: sandbox option for persistent functions
  box: implement lua_port dump to region and to Lua
  box: export _func functions with box.func folder

 src/box/CMakeLists.txt            |   1 +
 src/box/alter.cc                  | 118 +++++--
 src/box/bootstrap.snap            | Bin 4393 -> 4449 bytes
 src/box/call.c                    | 157 +--------
 src/box/call.h                    |  14 -
 src/box/errcode.h                 |   1 +
 src/box/execute.c                 |   1 +
 src/box/func.c                    | 564 ++++++++++++++++++++++++++++--
 src/box/func.h                    |  84 +++--
 src/box/func_def.c                |   9 +
 src/box/func_def.h                |  43 ++-
 src/box/lua/call.c                | 379 ++------------------
 src/box/lua/init.c                |   2 +
 src/box/lua/port_lua.c            | 318 +++++++++++++++++
 src/box/lua/schema.lua            |  38 +-
 src/box/lua/upgrade.lua           |  25 +-
 src/box/port.h                    |   2 +-
 src/box/schema.cc                 |  47 ++-
 src/box/schema.h                  |  31 +-
 src/box/schema_def.h              |   4 +
 src/lib/core/port.h               |  16 +
 src/lua/utils.c                   | 126 +++++++
 src/lua/utils.h                   |  19 +
 test/box-py/bootstrap.result      |   6 +-
 test/box/access_misc.result       |   6 +-
 test/box/misc.result              |   2 +
 test/box/persistent_func.result   | 257 ++++++++++++++
 test/box/persistent_func.test.lua | 112 ++++++
 28 files changed, 1734 insertions(+), 648 deletions(-)
 create mode 100644 src/box/lua/port_lua.c
 create mode 100644 test/box/persistent_func.result
 create mode 100644 test/box/persistent_func.test.lua

-- 
2.21.0




More information about the Tarantool-patches mailing list