2

I want to implement an init function under box.once() to be executed only once in Tarantool, however, this is useful to me only if init has been executed successfully.

Question: How can I make the "onceinit" record valid only if init has run successfully??

To reproduce the case:

The following code of the init function fails because there is no create_space_BAD function, however, when scanning the schema it is registered as executed.

Any suggestions on how to fix this?

The code:

local function start()
    box.cfg{}
    box.once('init', function()
        if not pcall(box.schema.create_space_BAD, 'myspace') then
            print('ERROR: create_space_BAD NOT EXIST')
            return false
        end
   ...
   end)
end

Explore schema:

tarantool> box.space._schema:select{}
---
- - ['cluster', '1cb21086-51a3-46fb-900e-1983609fc396']
  - ['max_id', 511]
  - ['onceinit']
  - ['version', 1, 10, 2]
...
racherb
  • 335
  • 1
  • 10
  • Ilya K. from Tarantool Telegram Group answered me: Using box.space._schema:delete('onceinit') in case the init function fails should solve this problem – racherb May 05 '20 at 00:33
  • 1
    was about to say the same: just delete it manually. You can easily do that using `xpcall`, by the way :D – DarkWiiPlayer May 05 '20 at 07:19

2 Answers2

1

This problem can be solved using box.space._schema:delete('onceinit') to explicitly deregister your init function.

Like:

local function start()
  box.cfg{}
  box.once('init', function()
      if not pcall(box.schema.create_space_BAD, 'myspace') then
    print('ERROR: create_space_BAD NOT EXIST')
    box.space._schema:delete('onceinit')
    return false
      end
        end)
end

Then you will see:

tarantool> box.space._schema:select{}
---
- - ['cluster', 'd6a9d97b-3a3f-4f69-8d1a-65ae5a073c16']
  - ['max_id', 511]
  - ['version', 2, 3, 1]
...

For more details see https://www.tarantool.io/en/doc/1.10/reference/reference_lua/box_once/

1

Note that approach with box.space._schema:delete won't work if you create several spaces/indexes. The recommended way is to use if_not_exists option instead of box.once.

See https://www.tarantool.io/en/doc/2.3/reference/reference_lua/box_schema/#box-schema-space-create

and

https://www.tarantool.io/en/doc/2.3/reference/reference_lua/box_space/#box-space-create-index

Mike Siomkin
  • 667
  • 8
  • 15