box: add on_schema_init trigger
This patch introduces an on_schema_init trigger. The trigger may be set before box.cfg() is called and is called during box.cfg() right after prototypes of system spaces, such as _space, are created. This allows to set triggers on system spaces before any other non-system data is recovered. For example, it is possible to set an on_replace trigger on _space, which will work even during recovery. Part of #3159 @TarantoolBot document Title: document box.ctl.on_schema_init triggers on_schema_init triggers are set before the first call to box.cfg() and are fired during box.cfg() before user data recovery start. To set the trigger, say ``` box.ctl.on_schema_init(new_trig, old_trig) ``` where `old_trig` may be omitted. This will replace `old_trig` with `new_trig`. Such triggers let you, for example, set triggers on system spaces before recovery of any data, so that the triggers are fired even during recovery. For example, such triggers make it possible to change a specific space's storage engine or make a replicated space replica-local on a freshly bootstrapped replica. If you want to change space's `space_name` storage engine to `vinyl` , you may say: ``` function trig(old, new) if new[3] == 'space_name' and new[4] ~= 'vinyl' then return new:update{{'=', 4, 'vinyl'}} end end ``` Such a trigger may be set on `_space` as a `before_replace` trigger. And thanks to `on_schema_init` triggers, it will happen before any non-system spaces are recovered, so the trigger will work for all user-created spaces: ``` box.ctl.on_schema_init(function() box.space._space:before_replace(trig) end) ``` Note, that the above steps are done before initial `box.cfg{}` call. Othervise the spaces will be already recovered by the time you set any triggers. Now you can say `box.cfg{replication='master_uri', ...}` And replica will have the space `space_name` with same contents, as on master, but on `vinyl` storage engine.
Showing
- src/box/lua/ctl.c 8 additions, 0 deletionssrc/box/lua/ctl.c
- src/box/schema.cc 7 additions, 0 deletionssrc/box/schema.cc
- src/box/schema.h 3 additions, 0 deletionssrc/box/schema.h
- test/box-tap/on_schema_init.test.lua 31 additions, 0 deletionstest/box-tap/on_schema_init.test.lua
- test/replication/on_schema_init.result 119 additions, 0 deletionstest/replication/on_schema_init.result
- test/replication/on_schema_init.test.lua 49 additions, 0 deletionstest/replication/on_schema_init.test.lua
- test/replication/replica_on_schema_init.lua 25 additions, 0 deletionstest/replication/replica_on_schema_init.lua
- test/replication/suite.cfg 1 addition, 0 deletionstest/replication/suite.cfg
Loading
Please register or sign in to comment