diff --git a/cfg/core_cfg.cfg_tmpl b/cfg/core_cfg.cfg_tmpl index 09a1b6da862d370d0beafa1d00959ebc16849701..0bf06176f640c3631157e0b5ec74c6f27f870f80 100644 --- a/cfg/core_cfg.cfg_tmpl +++ b/cfg/core_cfg.cfg_tmpl @@ -41,6 +41,9 @@ snap_dir=".", ro # WAL directory (where WALs get saved/read) wal_dir=".", ro +# script directory (where init.lua is expected to be) +script_dir=".", ro + # name of pid file pid_file="tarantool.pid", ro diff --git a/cfg/tarantool_box_cfg.c b/cfg/tarantool_box_cfg.c index d2168e727e8c5601128d285c04d627187cfb69f8..7cf14f73cf3ff713fd74a51f79e072962224fcc6 100644 --- a/cfg/tarantool_box_cfg.c +++ b/cfg/tarantool_box_cfg.c @@ -41,6 +41,7 @@ init_tarantool_cfg(tarantool_cfg *c) { c->work_dir = NULL; c->snap_dir = NULL; c->wal_dir = NULL; + c->script_dir = NULL; c->pid_file = NULL; c->logger = NULL; c->logger_nonblock = false; @@ -88,6 +89,8 @@ fill_default_tarantool_cfg(tarantool_cfg *c) { if (c->snap_dir == NULL) return CNF_NOMEMORY; c->wal_dir = strdup("."); if (c->wal_dir == NULL) return CNF_NOMEMORY; + c->script_dir = strdup("."); + if (c->script_dir == NULL) return CNF_NOMEMORY; c->pid_file = strdup("tarantool.pid"); if (c->pid_file == NULL) return CNF_NOMEMORY; c->logger = NULL; @@ -190,6 +193,9 @@ static NameAtom _name__snap_dir[] = { static NameAtom _name__wal_dir[] = { { "wal_dir", -1, NULL } }; +static NameAtom _name__script_dir[] = { + { "script_dir", -1, NULL } +}; static NameAtom _name__pid_file[] = { { "pid_file", -1, NULL } }; @@ -540,6 +546,18 @@ acceptValue(tarantool_cfg* c, OptDef* opt, int check_rdonly) { if (opt->paramValue.stringval && c->wal_dir == NULL) return CNF_NOMEMORY; } + else if ( cmpNameAtoms( opt->name, _name__script_dir) ) { + if (opt->paramType != stringType ) + return CNF_WRONGTYPE; + c->__confetti_flags &= ~CNF_FLAG_STRUCT_NOTSET; + errno = 0; + if (check_rdonly && ( (opt->paramValue.stringval == NULL && c->script_dir == NULL) || strcmp(opt->paramValue.stringval, c->script_dir) != 0)) + return CNF_RDONLY; + if (c->script_dir) free(c->script_dir); + c->script_dir = (opt->paramValue.stringval) ? strdup(opt->paramValue.stringval) : NULL; + if (opt->paramValue.stringval && c->script_dir == NULL) + return CNF_NOMEMORY; + } else if ( cmpNameAtoms( opt->name, _name__pid_file) ) { if (opt->paramType != stringType ) return CNF_WRONGTYPE; @@ -1229,6 +1247,7 @@ typedef enum IteratorState { S_name__work_dir, S_name__snap_dir, S_name__wal_dir, + S_name__script_dir, S_name__pid_file, S_name__logger, S_name__logger_nonblock, @@ -1426,6 +1445,16 @@ tarantool_cfg_iterator_next(tarantool_cfg_iterator_t* i, tarantool_cfg *c, char return NULL; } snprintf(buf, PRINTBUFLEN-1, "wal_dir"); + i->state = S_name__script_dir; + return buf; + case S_name__script_dir: + *v = (c->script_dir) ? strdup(c->script_dir) : NULL; + if (*v == NULL && c->script_dir) { + free(i); + out_warning(CNF_NOMEMORY, "No memory to output value"); + return NULL; + } + snprintf(buf, PRINTBUFLEN-1, "script_dir"); i->state = S_name__pid_file; return buf; case S_name__pid_file: @@ -1986,6 +2015,9 @@ dup_tarantool_cfg(tarantool_cfg* dst, tarantool_cfg* src) { if (dst->wal_dir) free(dst->wal_dir);dst->wal_dir = src->wal_dir == NULL ? NULL : strdup(src->wal_dir); if (src->wal_dir != NULL && dst->wal_dir == NULL) return CNF_NOMEMORY; + if (dst->script_dir) free(dst->script_dir);dst->script_dir = src->script_dir == NULL ? NULL : strdup(src->script_dir); + if (src->script_dir != NULL && dst->script_dir == NULL) + return CNF_NOMEMORY; if (dst->pid_file) free(dst->pid_file);dst->pid_file = src->pid_file == NULL ? NULL : strdup(src->pid_file); if (src->pid_file != NULL && dst->pid_file == NULL) return CNF_NOMEMORY; @@ -2093,6 +2125,8 @@ destroy_tarantool_cfg(tarantool_cfg* c) { free(c->snap_dir); if (c->wal_dir != NULL) free(c->wal_dir); + if (c->script_dir != NULL) + free(c->script_dir); if (c->pid_file != NULL) free(c->pid_file); if (c->logger != NULL) @@ -2231,6 +2265,11 @@ cmp_tarantool_cfg(tarantool_cfg* c1, tarantool_cfg* c2, int only_check_rdonly) { return diff; } + if (confetti_strcmp(c1->script_dir, c2->script_dir) != 0) { + snprintf(diff, PRINTBUFLEN - 1, "%s", "c->script_dir"); + + return diff; +} if (confetti_strcmp(c1->pid_file, c2->pid_file) != 0) { snprintf(diff, PRINTBUFLEN - 1, "%s", "c->pid_file"); diff --git a/cfg/tarantool_box_cfg.h b/cfg/tarantool_box_cfg.h index 81cbf5ab55b4b3149bf3d7a1fe5a5477ce879ed3..3ea64814d2b8034c97debfcafe1d607a203353ae 100644 --- a/cfg/tarantool_box_cfg.h +++ b/cfg/tarantool_box_cfg.h @@ -94,6 +94,9 @@ typedef struct tarantool_cfg { /* WAL directory (where WALs get saved/read) */ char* wal_dir; + /* script directory (where init.lua is expected to be) */ + char* script_dir; + /* name of pid file */ char* pid_file; diff --git a/src/tarantool_lua.m b/src/tarantool_lua.m index b49960e85803d6927e1b37be853391f3a1a3dd6e..7759f2045167a5f1698887037fdfa64dcbb9ab21 100644 --- a/src/tarantool_lua.m +++ b/src/tarantool_lua.m @@ -1081,28 +1081,32 @@ static void load_init_script(void *L_ptr) { struct lua_State *L = (struct lua_State *) L_ptr; - struct stat st; - /* checking that Lua start-up file exist. */ - if (stat(TARANTOOL_LUA_INIT_SCRIPT, &st)) { - /* - * File doesn't exist. It's OK, tarantool may not have - * start-up file. - */ - return; - } - /* execute start-up file */ - if (tarantool_lua_dofile(L, TARANTOOL_LUA_INIT_SCRIPT)) - panic("%s", lua_tostring(L, -1)); + char path[PATH_MAX + 1]; + snprintf(path, PATH_MAX, "%s/%s", + cfg.script_dir, TARANTOOL_LUA_INIT_SCRIPT); + + + if (access(path, F_OK) == 0) { + say_info("loading %s", path); + /* Execute the init file. */ + if (tarantool_lua_dofile(L, path)) + panic("%s", lua_tostring(L, -1)); + } + /* + * The file doesn't exist. It's OK, tarantool may + * have no init file. + */ } void tarantool_lua_load_init_script(struct lua_State *L) { /* * init script can call box.fiber.yield (including implicitly via - * box.insert, box.update, etc...) but yield which called in sched - * fiber will crash the server. That why, to avoid the problem, we must - * run init script in to separate fiber. + * box.insert, box.update, etc...) but box.fiber.yield() today, + * when called from 'sched' fiber crashes the server. + * To work this problem around we must run init script in + * a separate fiber. */ struct fiber *loader = fiber_create(TARANTOOL_LUA_INIT_SCRIPT, -1, load_init_script, L); diff --git a/test/box/admin.result b/test/box/admin.result index a40aaf1deef36c4817be77109bb643a957219c36..894f4473166ba69eac3cf6b245538f6b239045f8 100644 --- a/test/box/admin.result +++ b/test/box/admin.result @@ -42,6 +42,7 @@ configuration: work_dir: (null) snap_dir: "." wal_dir: "." + script_dir: "." pid_file: "box.pid" logger: "cat - >> tarantool.log" logger_nonblock: "true" diff --git a/test/box/configuration.result b/test/box/configuration.result index 02ce2663e0ea8ef5208d62d04dc437e0c3129514..e31e09a18b6412e5ad99d4174a6635ce278d4f62 100644 --- a/test/box/configuration.result +++ b/test/box/configuration.result @@ -20,6 +20,7 @@ configuration: work_dir: (null) snap_dir: "." wal_dir: "." + script_dir: "." pid_file: "box.pid" logger: "cat - >> tarantool.log" logger_nonblock: "true" @@ -83,6 +84,7 @@ configuration: work_dir: (null) snap_dir: "." wal_dir: "." + script_dir: "." pid_file: "box.pid" logger: "cat - >> tarantool.log" logger_nonblock: "true" @@ -210,3 +212,40 @@ tarantool_box -c tarantool_bad_type.cfg tarantool_box: can't load config: - (space = 0 fieldno = 0) index field type mismatch +lua print_config() +--- +io_collect_interval = 0 +pid_file = box.pid +slab_alloc_minimal = 64 +primary_port = 33013 +log_level = 4 +logger_nonblock = true +memcached_expire_per_loop = 1024 +snap_dir = . +coredump = false +panic_on_snap_error = true +memcached_expire_full_sweep = 3600 +replication_port = 0 +wal_fsync_delay = 0 +too_long_threshold = 0.5 +slab_alloc_factor = 2 +admin_port = 33015 +logger = cat - >> tarantool.log +snap_io_rate_limit = 0 +wal_writer_inbox_size = 16384 +memcached_expire = false +backlog = 1024 +memcached_space = 23 +memcached_port = 0 +rows_per_wal = 50 +wal_mode = fsync_delay +local_hot_standby = false +secondary_port = 33014 +panic_on_wal_error = false +script_dir = script_dir +wal_dir = . +bind_ipaddr = INADDR_ANY +readahead = 16320 +slab_alloc_arena = 0.1 +wal_dir_rescan_delay = 0.1 +... diff --git a/test/box/configuration.test b/test/box/configuration.test index b1a9ba605e7ee777c5f9f1a74c8d3ca5615e4834..c3971ae35a03550ed72319fe601e65271af44df9 100644 --- a/test/box/configuration.test +++ b/test/box/configuration.test @@ -3,6 +3,7 @@ import os import sys +import shutil print """ # Bug #708685: @@ -62,7 +63,19 @@ sys.stdout.push_filter("(/\S+)+/tarantool", "tarantool") server.test_option("-c " + os.path.join(os.getcwd(), "box/tarantool_bad_type.cfg")) sys.stdout.pop_filter() + +script_dir_path = os.path.join(vardir, "script_dir") +os.mkdir(script_dir_path) +shutil.copy("box/test_init.lua", os.path.join(script_dir_path, "init.lua")) + +server.stop() +server.deploy("box/tarantool_scriptdir.cfg") +exec admin "lua print_config()" + + # restore default server server.stop() +shutil.rmtree(script_dir_path, True) server.deploy(self.suite_ini["config"]) + # vim: syntax=python diff --git a/test/box/lua.result b/test/box/lua.result index b341480344c3bcf1e15f1241546a57898b3df8ef..f8117e9204547ccc58c89f31039fb8b1dfaaa73e 100644 --- a/test/box/lua.result +++ b/test/box/lua.result @@ -316,7 +316,7 @@ lua for k,v in pairs(box.cfg) do print(' - ', k, ': ', v) end - io_collect_interval: 0 - pid_file: box.pid - slab_alloc_minimal: 64 - - primary_port: 33013 + - slab_alloc_arena: 0.1 - log_level: 4 - logger_nonblock: true - memcached_expire_per_loop: 1024 @@ -332,18 +332,19 @@ lua for k,v in pairs(box.cfg) do print(' - ', k, ': ', v) end - logger: cat - >> tarantool.log - snap_io_rate_limit: 0 - wal_writer_inbox_size: 16384 - - backlog: 1024 - wal_dir_rescan_delay: 0.1 + - backlog: 1024 + - secondary_port: 33014 - wal_dir: . - - memcached_port: 0 - - wal_mode: fsync_delay - local_hot_standby: false + - wal_mode: fsync_delay + - rows_per_wal: 50 - readahead: 16320 - panic_on_wal_error: false - - rows_per_wal: 50 - - secondary_port: 33014 + - script_dir: . + - primary_port: 33013 - bind_ipaddr: INADDR_ANY - - slab_alloc_arena: 0.1 + - memcached_port: 0 - memcached_space: 23 - memcached_expire: false ... @@ -363,7 +364,7 @@ lua for k,v in pairs(box.cfg) do print(' - ', k, ': ', v) end - io_collect_interval: 0 - pid_file: box.pid - slab_alloc_minimal: 64 - - primary_port: 33013 + - slab_alloc_arena: 0.1 - log_level: 4 - logger_nonblock: true - memcached_expire_per_loop: 1024 @@ -379,18 +380,19 @@ lua for k,v in pairs(box.cfg) do print(' - ', k, ': ', v) end - logger: cat - >> tarantool.log - snap_io_rate_limit: 0 - wal_writer_inbox_size: 16384 - - backlog: 1024 - wal_dir_rescan_delay: 0.1 + - backlog: 1024 + - secondary_port: 33014 - wal_dir: . - - memcached_port: 0 - - wal_mode: fsync_delay - local_hot_standby: false + - wal_mode: fsync_delay + - rows_per_wal: 50 - readahead: 16320 - panic_on_wal_error: false - - rows_per_wal: 50 - - secondary_port: 33014 + - script_dir: . + - primary_port: 33013 - bind_ipaddr: INADDR_ANY - - slab_alloc_arena: 0.1 + - memcached_port: 0 - memcached_space: 23 - memcached_expire: false ... @@ -403,11 +405,11 @@ lua for k,v in pairs(box.space[0]) do if type(v) ~= 'table' then print(' - ', k, ... lua box.cfg.nosuchoption = 1 --- -error: 'Lua error: [string "box.cfg = {}..."]:51: Attempt to modify a read-only table' +error: 'Lua error: [string "box.cfg = {}..."]:52: Attempt to modify a read-only table' ... lua box.space[300] = 1 --- -error: 'Lua error: [string "box.cfg = {}..."]:51: Attempt to modify a read-only table' +error: 'Lua error: [string "box.cfg = {}..."]:52: Attempt to modify a read-only table' ... lua box.index.new('abc', 'cde') --- @@ -1163,7 +1165,7 @@ lua print_config() io_collect_interval = 0 pid_file = box.pid slab_alloc_minimal = 64 -slab_alloc_arena = 0.1 +primary_port = 33013 log_level = 4 logger_nonblock = true memcached_expire_per_loop = 1024 @@ -1173,25 +1175,26 @@ panic_on_snap_error = true memcached_expire_full_sweep = 3600 replication_port = 0 wal_fsync_delay = 0 -secondary_port = 33014 +too_long_threshold = 0.5 slab_alloc_factor = 2 admin_port = 33015 -memcached_space = 23 +logger = cat - >> tarantool.log snap_io_rate_limit = 0 wal_writer_inbox_size = 16384 -backlog = 1024 memcached_expire = false -logger = cat - >> tarantool.log -readahead = 16320 +backlog = 1024 +memcached_space = 23 +memcached_port = 0 +rows_per_wal = 50 wal_mode = fsync_delay local_hot_standby = false -wal_dir = . +secondary_port = 33014 panic_on_wal_error = false -memcached_port = 0 -too_long_threshold = 0.5 +script_dir = . +wal_dir = . bind_ipaddr = INADDR_ANY -rows_per_wal = 50 -primary_port = 33013 +readahead = 16320 +slab_alloc_arena = 0.1 wal_dir_rescan_delay = 0.1 ... diff --git a/test/box/tarantool_scriptdir.cfg b/test/box/tarantool_scriptdir.cfg new file mode 100644 index 0000000000000000000000000000000000000000..44405de86e09d3c329a88cf42acd889d85390196 --- /dev/null +++ b/test/box/tarantool_scriptdir.cfg @@ -0,0 +1,49 @@ +# +# Limit of memory used to store tuples to 100MB +# (0.1 GB) +# This effectively limits the memory, used by +# Tarantool. However, index and connection memory +# is stored outside the slab allocator, hence +# the effective memory usage can be higher (sometimes +# twice as high). +# +slab_alloc_arena = 0.1 + +# +# Store the pid in this file. Relative to +# startup dir. +# +pid_file = "box.pid" + +# +# Pipe the logs into the following process. +# +logger="cat - >> tarantool.log" + +# +# Read only and read-write port. +primary_port = 33013 +# Read-only port. +secondary_port = 33014 +# +# The port for administrative commands. +# +admin_port = 33015 +# +# Each write ahead log contains this many rows. +# When the limit is reached, Tarantool closes +# the WAL and starts a new one. +rows_per_wal = 50 + +# +# script directory (where init.lua is expected to be) +script_dir = "script_dir" + +# Define a simple space with 1 HASH-based +# primary key. +space[0].enabled = 1 +space[0].index[0].type = "HASH" +space[0].index[0].unique = 1 +space[0].index[0].key_field[0].fieldno = 0 +space[0].index[0].key_field[0].type = "NUM" + diff --git a/test/box_memcached/off.result b/test/box_memcached/off.result index 09a66bf7754dd681f95093698f8bb76967666aa2..b8237b3492267ff921ab20cb76d923b4d4acfde8 100644 --- a/test/box_memcached/off.result +++ b/test/box_memcached/off.result @@ -19,6 +19,7 @@ configuration: work_dir: (null) snap_dir: "." wal_dir: "." + script_dir: "." pid_file: "box.pid" logger: "cat - >> tarantool.log" logger_nonblock: "true"