From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp33.i.mail.ru (smtp33.i.mail.ru [94.100.177.93]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 8AA67445320 for ; Thu, 6 Aug 2020 11:30:24 +0300 (MSK) Received: by smtp33.i.mail.ru with esmtpa (envelope-from ) id 1k3bI7-0004V8-RD for tarantool-patches@dev.tarantool.org; Thu, 06 Aug 2020 11:30:24 +0300 Received: by mail-lj1-f171.google.com with SMTP id h19so50797949ljg.13 for ; Thu, 06 Aug 2020 01:30:23 -0700 (PDT) MIME-Version: 1.0 References: <0cd777f3990939b32d96bf964dea55e8073e65bb.1596665591.git.v.shpilevoy@tarantool.org> In-Reply-To: <0cd777f3990939b32d96bf964dea55e8073e65bb.1596665591.git.v.shpilevoy@tarantool.org> From: Yaroslav Dynnikov Date: Thu, 6 Aug 2020 11:30:12 +0300 Message-ID: Content-Type: multipart/alternative; boundary="000000000000ae6b3805ac314c47" Subject: Re: [Tarantool-patches] [PATCH v2 vshard 2/2] storage: allow replica to boot before master List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Vladislav Shpilevoy Cc: Yaroslav Dynnikov , tml --000000000000ae6b3805ac314c47 Content-Type: text/plain; charset="UTF-8" Thanks for the patch, LGTM. Best regards Yaroslav Dynnikov On Thu, 6 Aug 2020 at 01:15, Vladislav Shpilevoy wrote: > Before the patch a replica couldn't call vshard.storage.cfg() > before the master is bootstrapped. That could lead to an exception > in the middle of vshard.storage.cfg(). > > It sounds not so bad and even looks unreachable, because replica > bootstrap anyway is blocked in box.cfg() until a master node is > started, otherwise the replica instance is terminated. > But the box.cfg bootstrap != vshard bootstrap. > > It could be that both replica and master nodes are already > bootstrapped and working, but vshard.storage.cfg() wasn't called > yet. In that case vshard.storage.cfg() on the replica wouldn't > block in box.cfg(), and would happily fail a few lines later in > vshard.storage.cfg() at attempt to touch not existing > box.space._bucket. > > That was the case for cartridge. The framework configures > instances in its own way before vshard.storage.cfg(). Then it > calls vshard.storage.cfg() on them in arbitrary order, and > sometimes replicas were configured earlier than the master. > > The fail is fixed by simply skipping the _bucket's trigger > installation on the replica node. Because so far it is not needed > here for anything. The trigger's sole purpose is to increment > bucket generation used only for DML on _bucket on the master node. > > Now vshard.storage.cfg() on replica is able to finish before > master does the same. However the replica still won't be able to > handle vshard.storage.* requests until receives vshard schema > from master. > > Closes #237 > --- > test/lua_libs/storage_template.lua | 55 +++++++++++- > test/misc/reconfigure.result | 33 +++++++ > test/misc/reconfigure.test.lua | 9 ++ > test/reload_evolution/storage.result | 16 ++-- > test/reload_evolution/storage.test.lua | 14 +-- > test/router/boot_replica_first.result | 112 ++++++++++++++++++++++++ > test/router/boot_replica_first.test.lua | 42 +++++++++ > vshard/storage/init.lua | 21 ++++- > vshard/storage/reload_evolution.lua | 8 ++ > 9 files changed, 297 insertions(+), 13 deletions(-) > create mode 100644 test/router/boot_replica_first.result > create mode 100644 test/router/boot_replica_first.test.lua > > diff --git a/test/lua_libs/storage_template.lua > b/test/lua_libs/storage_template.lua > index 29a753d..84e4180 100644 > --- a/test/lua_libs/storage_template.lua > +++ b/test/lua_libs/storage_template.lua > @@ -1,5 +1,7 @@ > #!/usr/bin/env tarantool > > +local luri = require('uri') > + > NAME = require('fio').basename(arg[0], '.lua') > fiber = require('fiber') > test_run = require('test_run').new() > @@ -10,12 +12,63 @@ log = require('log') > if not cfg.shard_index then > cfg.shard_index = 'bucket_id' > end > +instance_uuid = util.name_to_uuid[NAME] > + > +-- > +-- Bootstrap the instance exactly like vshard does. But don't > +-- initialize any vshard-specific code. > +-- > +local function boot_like_vshard() > + assert(type(box.cfg) == 'function') > + for rs_uuid, rs in pairs(cfg.sharding) do > + for replica_uuid, replica in pairs(rs.replicas) do > + if replica_uuid == instance_uuid then > + local box_cfg = {replication = {}} > + box_cfg.instance_uuid = replica_uuid > + box_cfg.replicaset_uuid = rs_uuid > + box_cfg.listen = replica.uri > + box_cfg.read_only = not replica.master > + box_cfg.replication_connect_quorum = 0 > + box_cfg.replication_timeout = 0.1 > + for _, replica in pairs(rs.replicas) do > + table.insert(box_cfg.replication, replica.uri) > + end > + box.cfg(box_cfg) > + if not replica.master then > + return > + end > + local uri = luri.parse(replica.uri) > + box.schema.user.create(uri.login, { > + password = uri.password, if_not_exists = true, > + }) > + box.schema.user.grant(uri.login, 'super') > + return > + end > + end > + end > + assert(false) > +end > + > +local omit_cfg = false > +local i = 1 > +while arg[i] ~= nil do > + local key = arg[i] > + i = i + 1 > + if key == 'boot_before_cfg' then > + boot_like_vshard() > + omit_cfg = true > + end > +end > > vshard = require('vshard') > echo_count = 0 > cfg.replication_connect_timeout = 3 > cfg.replication_timeout = 0.1 > -vshard.storage.cfg(cfg, util.name_to_uuid[NAME]) > + > +if not omit_cfg then > + vshard.storage.cfg(cfg, instance_uuid) > +end > + > function bootstrap_storage(engine) > box.once("testapp:schema:1", function() > if rawget(_G, 'CHANGE_SPACE_IDS') then > diff --git a/test/misc/reconfigure.result b/test/misc/reconfigure.result > index be58eca..168be5d 100644 > --- a/test/misc/reconfigure.result > +++ b/test/misc/reconfigure.result > @@ -277,6 +277,14 @@ box.cfg.replication > --- > - - storage:storage@127.0.0.1:3301 > ... > +box.cfg.read_only > +--- > +- false > +... > +#box.space._bucket:on_replace() > +--- > +- 1 > +... > _ = test_run:switch('storage_2_a') > --- > ... > @@ -303,6 +311,15 @@ box.cfg.replication > - - storage:storage@127.0.0.1:3303 > - storage:storage@127.0.0.1:3304 > ... > +box.cfg.read_only > +--- > +- true > +... > +-- Should be zero on the slave node. Even though earlier the node was a > master. > +#box.space._bucket:on_replace() > +--- > +- 0 > +... > _ = test_run:switch('storage_2_b') > --- > ... > @@ -329,6 +346,14 @@ box.cfg.replication > - - storage:storage@127.0.0.1:3303 > - storage:storage@127.0.0.1:3304 > ... > +box.cfg.read_only > +--- > +- false > +... > +#box.space._bucket:on_replace() > +--- > +- 1 > +... > _ = test_run:switch('storage_3_a') > --- > ... > @@ -354,6 +379,14 @@ box.cfg.replication > --- > - - storage:storage@127.0.0.1:3306 > ... > +box.cfg.read_only > +--- > +- false > +... > +#box.space._bucket:on_replace() > +--- > +- 1 > +... > _ = test_run:switch('router_1') > --- > ... > diff --git a/test/misc/reconfigure.test.lua > b/test/misc/reconfigure.test.lua > index 1b894c8..e891010 100644 > --- a/test/misc/reconfigure.test.lua > +++ b/test/misc/reconfigure.test.lua > @@ -111,6 +111,8 @@ for k,v in pairs(info.replicasets) do > table.insert(uris, v.master.uri) end > table.sort(uris) > uris > box.cfg.replication > +box.cfg.read_only > +#box.space._bucket:on_replace() > > _ = test_run:switch('storage_2_a') > info = vshard.storage.info() > @@ -119,6 +121,9 @@ for k,v in pairs(info.replicasets) do > table.insert(uris, v.master.uri) end > table.sort(uris) > uris > box.cfg.replication > +box.cfg.read_only > +-- Should be zero on the slave node. Even though earlier the node was a > master. > +#box.space._bucket:on_replace() > > _ = test_run:switch('storage_2_b') > info = vshard.storage.info() > @@ -127,6 +132,8 @@ for k,v in pairs(info.replicasets) do > table.insert(uris, v.master.uri) end > table.sort(uris) > uris > box.cfg.replication > +box.cfg.read_only > +#box.space._bucket:on_replace() > > _ = test_run:switch('storage_3_a') > info = vshard.storage.info() > @@ -135,6 +142,8 @@ for k,v in pairs(info.replicasets) do > table.insert(uris, v.master.uri) end > table.sort(uris) > uris > box.cfg.replication > +box.cfg.read_only > +#box.space._bucket:on_replace() > > _ = test_run:switch('router_1') > info = vshard.router.info() > diff --git a/test/reload_evolution/storage.result > b/test/reload_evolution/storage.result > index dde0a1f..4652c4f 100644 > --- a/test/reload_evolution/storage.result > +++ b/test/reload_evolution/storage.result > @@ -86,16 +86,22 @@ package.loaded['vshard.storage'] = nil > vshard.storage = require("vshard.storage") > --- > ... > --- Should be nil. Because there was a bug that reload always reapplied > the last > --- migration callback. When it was fixed, the last callback wasn't called > twice. > --- But since the callback was only one, now nothing is called, and > nothing is > --- logged. > -test_run:grep_log('storage_2_a', 'vshard.storage.reload_evolution: > upgraded to') == nil > +test_run:grep_log('storage_2_a', 'vshard.storage.reload_evolution: > upgraded to') ~= nil > --- > - true > ... > vshard.storage.internal.reload_version > --- > +- 2 > +... > +-- > +-- gh-237: should be only one trigger. During gh-237 the trigger > installation > +-- became conditional and therefore required to remember the current > trigger > +-- somewhere. When reloaded from the old version, the trigger needed to be > +-- fetched from _bucket:on_replace(). > +-- > +#box.space._bucket:on_replace() > +--- > - 1 > ... > -- Make sure storage operates well. > diff --git a/test/reload_evolution/storage.test.lua > b/test/reload_evolution/storage.test.lua > index 7858112..06f7117 100644 > --- a/test/reload_evolution/storage.test.lua > +++ b/test/reload_evolution/storage.test.lua > @@ -35,12 +35,16 @@ box.space.test:insert({42, bucket_id_to_move}) > package.path = original_package_path > package.loaded['vshard.storage'] = nil > vshard.storage = require("vshard.storage") > --- Should be nil. Because there was a bug that reload always reapplied > the last > --- migration callback. When it was fixed, the last callback wasn't called > twice. > --- But since the callback was only one, now nothing is called, and > nothing is > --- logged. > -test_run:grep_log('storage_2_a', 'vshard.storage.reload_evolution: > upgraded to') == nil > +test_run:grep_log('storage_2_a', 'vshard.storage.reload_evolution: > upgraded to') ~= nil > vshard.storage.internal.reload_version > +-- > +-- gh-237: should be only one trigger. During gh-237 the trigger > installation > +-- became conditional and therefore required to remember the current > trigger > +-- somewhere. When reloaded from the old version, the trigger needed to be > +-- fetched from _bucket:on_replace(). > +-- > +#box.space._bucket:on_replace() > + > -- Make sure storage operates well. > vshard.storage.bucket_force_drop(2000) > vshard.storage.bucket_force_create(2000) > diff --git a/test/router/boot_replica_first.result > b/test/router/boot_replica_first.result > new file mode 100644 > index 0000000..1705230 > --- /dev/null > +++ b/test/router/boot_replica_first.result > @@ -0,0 +1,112 @@ > +-- test-run result file version 2 > +test_run = require('test_run').new() > + | --- > + | ... > +REPLICASET_1 = { 'box_1_a', 'box_1_b', 'box_1_c' } > + | --- > + | ... > +test_run:create_cluster(REPLICASET_1, 'router', {args = > 'boot_before_cfg'}) > + | --- > + | ... > +util = require('util') > + | --- > + | ... > +util.wait_master(test_run, REPLICASET_1, 'box_1_a') > + | --- > + | ... > +_ = test_run:cmd("create server router with script='router/router_2.lua'") > + | --- > + | ... > +_ = test_run:cmd("start server router") > + | --- > + | ... > + > +-- > +-- gh-237: replica should be able to boot before master. Before the issue > was > +-- fixed, replica always tried to install a trigger on _bucket space even > when > +-- it was not created on a master yet - that lead to an exception in > +-- storage.cfg. Now it should not install the trigger at all, because > anyway it > +-- is not needed on replica for anything. > +-- > + > +test_run:switch('box_1_b') > + | --- > + | - true > + | ... > +vshard.storage.cfg(cfg, instance_uuid) > + | --- > + | ... > +-- _bucket is not created yet. Will fail. > +util.check_error(vshard.storage.call, 1, 'read', 'echo', {100}) > + | --- > + | - attempt to index field '_bucket' (a nil value) > + | ... > + > +test_run:switch('default') > + | --- > + | - true > + | ... > +util.map_evals(test_run, {REPLICASET_1}, 'bootstrap_storage(\'memtx\')') > + | --- > + | ... > + > +test_run:switch('box_1_a') > + | --- > + | - true > + | ... > +vshard.storage.cfg(cfg, instance_uuid) > + | --- > + | ... > + > +test_run:switch('box_1_b') > + | --- > + | - true > + | ... > +test_run:wait_lsn('box_1_b', 'box_1_a') > + | --- > + | ... > +-- Fails, but gracefully. Because the bucket is not found here. > +vshard.storage.call(1, 'read', 'echo', {100}) > + | --- > + | - null > + | - bucket_id: 1 > + | reason: Not found > + | code: 1 > + | type: ShardingError > + | message: 'Cannot perform action with bucket 1, reason: Not found' > + | name: WRONG_BUCKET > + | ... > +-- Should not have triggers. > +#box.space._bucket:on_replace() > + | --- > + | - 0 > + | ... > + > +test_run:switch('router') > + | --- > + | - true > + | ... > +vshard.router.bootstrap() > + | --- > + | - true > + | ... > +vshard.router.callro(1, 'echo', {100}) > + | --- > + | - 100 > + | ... > + > +test_run:switch("default") > + | --- > + | - true > + | ... > +test_run:cmd('stop server router') > + | --- > + | - true > + | ... > +test_run:cmd('delete server router') > + | --- > + | - true > + | ... > +test_run:drop_cluster(REPLICASET_1) > + | --- > + | ... > diff --git a/test/router/boot_replica_first.test.lua > b/test/router/boot_replica_first.test.lua > new file mode 100644 > index 0000000..7b1b3fd > --- /dev/null > +++ b/test/router/boot_replica_first.test.lua > @@ -0,0 +1,42 @@ > +test_run = require('test_run').new() > +REPLICASET_1 = { 'box_1_a', 'box_1_b', 'box_1_c' } > +test_run:create_cluster(REPLICASET_1, 'router', {args = > 'boot_before_cfg'}) > +util = require('util') > +util.wait_master(test_run, REPLICASET_1, 'box_1_a') > +_ = test_run:cmd("create server router with script='router/router_2.lua'") > +_ = test_run:cmd("start server router") > + > +-- > +-- gh-237: replica should be able to boot before master. Before the issue > was > +-- fixed, replica always tried to install a trigger on _bucket space even > when > +-- it was not created on a master yet - that lead to an exception in > +-- storage.cfg. Now it should not install the trigger at all, because > anyway it > +-- is not needed on replica for anything. > +-- > + > +test_run:switch('box_1_b') > +vshard.storage.cfg(cfg, instance_uuid) > +-- _bucket is not created yet. Will fail. > +util.check_error(vshard.storage.call, 1, 'read', 'echo', {100}) > + > +test_run:switch('default') > +util.map_evals(test_run, {REPLICASET_1}, 'bootstrap_storage(\'memtx\')') > + > +test_run:switch('box_1_a') > +vshard.storage.cfg(cfg, instance_uuid) > + > +test_run:switch('box_1_b') > +test_run:wait_lsn('box_1_b', 'box_1_a') > +-- Fails, but gracefully. Because the bucket is not found here. > +vshard.storage.call(1, 'read', 'echo', {100}) > +-- Should not have triggers. > +#box.space._bucket:on_replace() > + > +test_run:switch('router') > +vshard.router.bootstrap() > +vshard.router.callro(1, 'echo', {100}) > + > +test_run:switch("default") > +test_run:cmd('stop server router') > +test_run:cmd('delete server router') > +test_run:drop_cluster(REPLICASET_1) > diff --git a/vshard/storage/init.lua b/vshard/storage/init.lua > index c6a78fe..5464824 100644 > --- a/vshard/storage/init.lua > +++ b/vshard/storage/init.lua > @@ -94,6 +94,17 @@ if not M then > -- detect that _bucket was not changed between yields. > -- > bucket_generation = 0, > + -- > + -- Reference to the function used as on_replace trigger on > + -- _bucket space. It is used to replace the trigger with > + -- a new function when reload happens. It is kept > + -- explicitly because the old function is deleted on > + -- reload from the global namespace. On the other hand, it > + -- is still stored in _bucket:on_replace() somewhere, but > + -- it is not known where. The only 100% way to be able to > + -- replace the old function is to keep its reference. > + -- > + bucket_on_replace = nil, > > ------------------- Garbage collection ------------------- > -- Fiber to remove garbage buckets data. > @@ -2435,8 +2446,14 @@ local function storage_cfg(cfg, this_replica_uuid, > is_reload) > local uri = luri.parse(this_replica.uri) > schema_upgrade(is_master, uri.login, uri.password) > > - local old_trigger = box.space._bucket:on_replace()[1] > - box.space._bucket:on_replace(bucket_generation_increment, old_trigger) > + if M.bucket_on_replace then > + box.space._bucket:on_replace(nil, M.bucket_on_replace) > + M.bucket_on_replace = nil > + end > + if is_master then > + box.space._bucket:on_replace(bucket_generation_increment) > + M.bucket_on_replace = bucket_generation_increment > + end > > lreplicaset.rebind_replicasets(new_replicasets, M.replicasets) > lreplicaset.outdate_replicasets(M.replicasets) > diff --git a/vshard/storage/reload_evolution.lua > b/vshard/storage/reload_evolution.lua > index 1abc9e2..f38af74 100644 > --- a/vshard/storage/reload_evolution.lua > +++ b/vshard/storage/reload_evolution.lua > @@ -17,6 +17,14 @@ migrations[#migrations + 1] = function(M) > -- Code to update Lua objects. > end > > +migrations[#migrations + 1] = function(M) > + local bucket = box.space._bucket > + if bucket then > + assert(M.bucket_on_replace == nil) > + M.bucket_on_replace = bucket:on_replace()[1] > + end > +end > + > -- > -- Perform an update based on a version stored in `M` (internals). > -- @param M Old module internals which should be updated. > -- > 2.21.1 (Apple Git-122.3) > > --000000000000ae6b3805ac314c47 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Thanks for the patch, LGTM.

=
<= span>
Best regards
Yaroslav Dynnikov


On Thu, 6 Aug 2020 at 01:15, Vla= dislav Shpilevoy <v.shpilevoy@tarantool.org> wrote:
Before the patch a replica couldn't c= all vshard.storage.cfg()
before the master is bootstrapped. That could lead to an exception
in the middle of vshard.storage.cfg().

It sounds not so bad and even looks unreachable, because replica
bootstrap anyway is blocked in box.cfg() until a master node is
started, otherwise the replica instance is terminated.
But the box.cfg bootstrap !=3D vshard bootstrap.

It could be that both replica and master nodes are already
bootstrapped and working, but vshard.storage.cfg() wasn't called
yet. In that case vshard.storage.cfg() on the replica wouldn't
block in box.cfg(), and would happily fail a few lines later in
vshard.storage.cfg() at attempt to touch not existing
box.space._bucket.

That was the case for cartridge. The framework configures
instances in its own way before vshard.storage.cfg(). Then it
calls vshard.storage.cfg() on them in arbitrary order, and
sometimes replicas were configured earlier than the master.

The fail is fixed by simply skipping the _bucket's trigger
installation on the replica node. Because so far it is not needed
here for anything. The trigger's sole purpose is to increment
bucket generation used only for DML on _bucket on the master node.

Now vshard.storage.cfg() on replica is able to finish before
master does the same. However the replica still won't be able to
handle vshard.storage.* requests until receives vshard schema
from master.

Closes #237
---
=C2=A0test/lua_libs/storage_template.lua=C2=A0 =C2=A0 =C2=A0 |=C2=A0 55 +++= ++++++++-
=C2=A0test/misc/reconfigure.result=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= |=C2=A0 33 +++++++
=C2=A0test/misc/reconfigure.test.lua=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 |=C2= =A0 =C2=A09 ++
=C2=A0test/reload_evolution/storage.result=C2=A0 =C2=A0 |=C2=A0 16 ++--
=C2=A0test/reload_evolution/storage.test.lua=C2=A0 |=C2=A0 14 +--
=C2=A0test/router/boot_replica_first.result=C2=A0 =C2=A0| 112 +++++++++++++= +++++++++++
=C2=A0test/router/boot_replica_first.test.lua |=C2=A0 42 +++++++++
=C2=A0vshard/storage/init.lua=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0|=C2=A0 21 ++++-
=C2=A0vshard/storage/reload_evolution.lua=C2=A0 =C2=A0 =C2=A0|=C2=A0 =C2=A0= 8 ++
=C2=A09 files changed, 297 insertions(+), 13 deletions(-)
=C2=A0create mode 100644 test/router/boot_replica_first.result
=C2=A0create mode 100644 test/router/boot_replica_first.test.lua

diff --git a/test/lua_libs/storage_template.lua b/test/lua_libs/storage_tem= plate.lua
index 29a753d..84e4180 100644
--- a/test/lua_libs/storage_template.lua
+++ b/test/lua_libs/storage_template.lua
@@ -1,5 +1,7 @@
=C2=A0#!/usr/bin/env tarantool

+local luri =3D require('uri')
+
=C2=A0NAME =3D require('fio').basename(arg[0], '.lua')
=C2=A0fiber =3D require('fiber')
=C2=A0test_run =3D require('test_run').new()
@@ -10,12 +12,63 @@ log =3D require('log')
=C2=A0if not cfg.shard_index then
=C2=A0 =C2=A0 =C2=A0cfg.shard_index =3D 'bucket_id'
=C2=A0end
+instance_uuid =3D util.name_to_uuid[NAME]
+
+--
+-- Bootstrap the instance exactly like vshard does. But don't
+-- initialize any vshard-specific code.
+--
+local function boot_like_vshard()
+=C2=A0 =C2=A0 assert(type(box.cfg) =3D=3D 'function')
+=C2=A0 =C2=A0 for rs_uuid, rs in pairs(cfg.sharding) do
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 for replica_uuid, replica in pairs(rs.replicas= ) do
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if replica_uuid =3D=3D instance_= uuid then
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 local box_cfg =3D = {replication =3D {}}
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 box_cfg.instance_u= uid =3D replica_uuid
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 box_cfg.replicaset= _uuid =3D rs_uuid
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 box_cfg.listen =3D= replica.uri
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 box_cfg.read_only = =3D not replica.master
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 box_cfg.replicatio= n_connect_quorum =3D 0
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 box_cfg.replicatio= n_timeout =3D 0.1
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 for _, replica in = pairs(rs.replicas) do
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 tabl= e.insert(box_cfg.replication, replica.uri)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 end
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 box.cfg(box_cfg) +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if not replica.mas= ter then
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 retu= rn
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 end
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 local uri =3D luri= .parse(replica.uri)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 box.schema.user.cr= eate(uri.login, {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 pass= word =3D uri.password, if_not_exists =3D true,
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 })
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 box.schema.user.gr= ant(uri.login, 'super')
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 end
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 end
+=C2=A0 =C2=A0 end
+=C2=A0 =C2=A0 assert(false)
+end
+
+local omit_cfg =3D false
+local i =3D 1
+while arg[i] ~=3D nil do
+=C2=A0 =C2=A0 local key =3D arg[i]
+=C2=A0 =C2=A0 i =3D i + 1
+=C2=A0 =C2=A0 if key =3D=3D 'boot_before_cfg' then
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 boot_like_vshard()
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 omit_cfg =3D true
+=C2=A0 =C2=A0 end
+end

=C2=A0vshard =3D require('vshard')
=C2=A0echo_count =3D 0
=C2=A0cfg.replication_connect_timeout =3D 3
=C2=A0cfg.replication_timeout =3D 0.1
-vshard.storage.cfg(cfg, util.name_to_uuid[NAME])
+
+if not omit_cfg then
+=C2=A0 =C2=A0 vshard.storage.cfg(cfg, instance_uuid)
+end
+
=C2=A0function bootstrap_storage(engine)
=C2=A0 =C2=A0 =C2=A0box.once("testapp:schema:1", function()
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if rawget(_G, 'CHANGE_SPACE_IDS')= then
diff --git a/test/misc/reconfigure.result b/test/misc/reconfigure.result index be58eca..168be5d 100644
--- a/test/misc/reconfigure.result
+++ b/test/misc/reconfigure.result
@@ -277,6 +277,14 @@ box.cfg.replication
=C2=A0---
=C2=A0- - storage:storage@127.0.0.1:3301
=C2=A0...
+box.cfg.read_only
+---
+- false
+...
+#box.space._bucket:on_replace()
+---
+- 1
+...
=C2=A0_ =3D test_run:switch('storage_2_a')
=C2=A0---
=C2=A0...
@@ -303,6 +311,15 @@ box.cfg.replication
=C2=A0- - storage:storage@127.0.0.1:3303
=C2=A0 =C2=A0- storage:storage@127.0.0.1:3304
=C2=A0...
+box.cfg.read_only
+---
+- true
+...
+-- Should be zero on the slave node. Even though earlier the node was a ma= ster.
+#box.space._bucket:on_replace()
+---
+- 0
+...
=C2=A0_ =3D test_run:switch('storage_2_b')
=C2=A0---
=C2=A0...
@@ -329,6 +346,14 @@ box.cfg.replication
=C2=A0- - storage:storage@127.0.0.1:3303
=C2=A0 =C2=A0- storage:storage@127.0.0.1:3304
=C2=A0...
+box.cfg.read_only
+---
+- false
+...
+#box.space._bucket:on_replace()
+---
+- 1
+...
=C2=A0_ =3D test_run:switch('storage_3_a')
=C2=A0---
=C2=A0...
@@ -354,6 +379,14 @@ box.cfg.replication
=C2=A0---
=C2=A0- - storage:storage@127.0.0.1:3306
=C2=A0...
+box.cfg.read_only
+---
+- false
+...
+#box.space._bucket:on_replace()
+---
+- 1
+...
=C2=A0_ =3D test_run:switch('router_1')
=C2=A0---
=C2=A0...
diff --git a/test/misc/reconfigure.test.lua b/test/misc/reconfigure.test.lu= a
index 1b894c8..e891010 100644
--- a/test/misc/reconfigure.test.lua
+++ b/test/misc/reconfigure.test.lua
@@ -111,6 +111,8 @@ for k,v in pairs(info.replicasets) do table.insert(uris= , v.master.uri) end
=C2=A0table.sort(uris)
=C2=A0uris
=C2=A0box.cfg.replication
+box.cfg.read_only
+#box.space._bucket:on_replace()

=C2=A0_ =3D test_run:switch('storage_2_a')
=C2=A0info =3D vshard.storage.info()
@@ -119,6 +121,9 @@ for k,v in pairs(info.replicasets) do table.insert(uris= , v.master.uri) end
=C2=A0table.sort(uris)
=C2=A0uris
=C2=A0box.cfg.replication
+box.cfg.read_only
+-- Should be zero on the slave node. Even though earlier the node was a ma= ster.
+#box.space._bucket:on_replace()

=C2=A0_ =3D test_run:switch('storage_2_b')
=C2=A0info =3D vshard.storage.info()
@@ -127,6 +132,8 @@ for k,v in pairs(info.replicasets) do table.insert(uris= , v.master.uri) end
=C2=A0table.sort(uris)
=C2=A0uris
=C2=A0box.cfg.replication
+box.cfg.read_only
+#box.space._bucket:on_replace()

=C2=A0_ =3D test_run:switch('storage_3_a')
=C2=A0info =3D vshard.storage.info()
@@ -135,6 +142,8 @@ for k,v in pairs(info.replicasets) do table.insert(uris= , v.master.uri) end
=C2=A0table.sort(uris)
=C2=A0uris
=C2=A0box.cfg.replication
+box.cfg.read_only
+#box.space._bucket:on_replace()

=C2=A0_ =3D test_run:switch('router_1')
=C2=A0info =3D vshard.router.info()
diff --git a/test/reload_evolution/storage.result b/test/reload_evolution/s= torage.result
index dde0a1f..4652c4f 100644
--- a/test/reload_evolution/storage.result
+++ b/test/reload_evolution/storage.result
@@ -86,16 +86,22 @@ package.loaded['vshard.storage'] =3D nil
=C2=A0vshard.storage =3D require("vshard.storage")
=C2=A0---
=C2=A0...
--- Should be nil. Because there was a bug that reload always reapplied the= last
--- migration callback. When it was fixed, the last callback wasn't cal= led twice.
--- But since the callback was only one, now nothing is called, and nothing= is
--- logged.
-test_run:grep_log('storage_2_a', 'vshard.storage.reload_evolut= ion: upgraded to') =3D=3D nil
+test_run:grep_log('storage_2_a', 'vshard.storage.reload_evolut= ion: upgraded to') ~=3D nil
=C2=A0---
=C2=A0- true
=C2=A0...
=C2=A0vshard.storage.internal.reload_version
=C2=A0---
+- 2
+...
+--
+-- gh-237: should be only one trigger. During gh-237 the trigger installat= ion
+-- became conditional and therefore required to remember the current trigg= er
+-- somewhere. When reloaded from the old version, the trigger needed to be=
+-- fetched from _bucket:on_replace().
+--
+#box.space._bucket:on_replace()
+---
=C2=A0- 1
=C2=A0...
=C2=A0-- Make sure storage operates well.
diff --git a/test/reload_evolution/storage.test.lua b/test/reload_evolution= /storage.test.lua
index 7858112..06f7117 100644
--- a/test/reload_evolution/storage.test.lua
+++ b/test/reload_evolution/storage.test.lua
@@ -35,12 +35,16 @@ box.space.test:insert({42, bucket_id_to_move})
=C2=A0package.path =3D original_package_path
=C2=A0package.loaded['vshard.storage'] =3D nil
=C2=A0vshard.storage =3D require("vshard.storage")
--- Should be nil. Because there was a bug that reload always reapplied the= last
--- migration callback. When it was fixed, the last callback wasn't cal= led twice.
--- But since the callback was only one, now nothing is called, and nothing= is
--- logged.
-test_run:grep_log('storage_2_a', 'vshard.storage.reload_evolut= ion: upgraded to') =3D=3D nil
+test_run:grep_log('storage_2_a', 'vshard.storage.reload_evolut= ion: upgraded to') ~=3D nil
=C2=A0vshard.storage.internal.reload_version
+--
+-- gh-237: should be only one trigger. During gh-237 the trigger installat= ion
+-- became conditional and therefore required to remember the current trigg= er
+-- somewhere. When reloaded from the old version, the trigger needed to be=
+-- fetched from _bucket:on_replace().
+--
+#box.space._bucket:on_replace()
+
=C2=A0-- Make sure storage operates well.
=C2=A0vshard.storage.bucket_force_drop(2000)
=C2=A0vshard.storage.bucket_force_create(2000)
diff --git a/test/router/boot_replica_first.result b/test/router/boot_repli= ca_first.result
new file mode 100644
index 0000000..1705230
--- /dev/null
+++ b/test/router/boot_replica_first.result
@@ -0,0 +1,112 @@
+-- test-run result file version 2
+test_run =3D require('test_run').new()
+ | ---
+ | ...
+REPLICASET_1 =3D { 'box_1_a', 'box_1_b', 'box_1_c'= }
+ | ---
+ | ...
+test_run:create_cluster(REPLICASET_1, 'router', {args =3D 'boo= t_before_cfg'})
+ | ---
+ | ...
+util =3D require('util')
+ | ---
+ | ...
+util.wait_master(test_run, REPLICASET_1, 'box_1_a')
+ | ---
+ | ...
+_ =3D test_run:cmd("create server router with script=3D'router/ro= uter_2.lua'")
+ | ---
+ | ...
+_ =3D test_run:cmd("start server router")
+ | ---
+ | ...
+
+--
+-- gh-237: replica should be able to boot before master. Before the issue = was
+-- fixed, replica always tried to install a trigger on _bucket space even = when
+-- it was not created on a master yet - that lead to an exception in
+-- storage.cfg. Now it should not install the trigger at all, because anyw= ay it
+-- is not needed on replica for anything.
+--
+
+test_run:switch('box_1_b')
+ | ---
+ | - true
+ | ...
+vshard.storage.cfg(cfg, instance_uuid)
+ | ---
+ | ...
+-- _bucket is not created yet. Will fail.
+util.check_error(vshard.storage.call, 1, 'read', 'echo', {= 100})
+ | ---
+ | - attempt to index field '_bucket' (a nil value)
+ | ...
+
+test_run:switch('default')
+ | ---
+ | - true
+ | ...
+util.map_evals(test_run, {REPLICASET_1}, 'bootstrap_storage(\'memt= x\')')
+ | ---
+ | ...
+
+test_run:switch('box_1_a')
+ | ---
+ | - true
+ | ...
+vshard.storage.cfg(cfg, instance_uuid)
+ | ---
+ | ...
+
+test_run:switch('box_1_b')
+ | ---
+ | - true
+ | ...
+test_run:wait_lsn('box_1_b', 'box_1_a')
+ | ---
+ | ...
+-- Fails, but gracefully. Because the bucket is not found here.
+vshard.storage.call(1, 'read', 'echo', {100})
+ | ---
+ | - null
+ | - bucket_id: 1
+ |=C2=A0 =C2=A0reason: Not found
+ |=C2=A0 =C2=A0code: 1
+ |=C2=A0 =C2=A0type: ShardingError
+ |=C2=A0 =C2=A0message: 'Cannot perform action with bucket 1, reason: = Not found'
+ |=C2=A0 =C2=A0name: WRONG_BUCKET
+ | ...
+-- Should not have triggers.
+#box.space._bucket:on_replace()
+ | ---
+ | - 0
+ | ...
+
+test_run:switch('router')
+ | ---
+ | - true
+ | ...
+vshard.router.bootstrap()
+ | ---
+ | - true
+ | ...
+vshard.router.callro(1, 'echo', {100})
+ | ---
+ | - 100
+ | ...
+
+test_run:switch("default")
+ | ---
+ | - true
+ | ...
+test_run:cmd('stop server router')
+ | ---
+ | - true
+ | ...
+test_run:cmd('delete server router')
+ | ---
+ | - true
+ | ...
+test_run:drop_cluster(REPLICASET_1)
+ | ---
+ | ...
diff --git a/test/router/boot_replica_first.test.lua b/test/router/boot_rep= lica_first.test.lua
new file mode 100644
index 0000000..7b1b3fd
--- /dev/null
+++ b/test/router/boot_replica_first.test.lua
@@ -0,0 +1,42 @@
+test_run =3D require('test_run').new()
+REPLICASET_1 =3D { 'box_1_a', 'box_1_b', 'box_1_c'= }
+test_run:create_cluster(REPLICASET_1, 'router', {args =3D 'boo= t_before_cfg'})
+util =3D require('util')
+util.wait_master(test_run, REPLICASET_1, 'box_1_a')
+_ =3D test_run:cmd("create server router with script=3D'router/ro= uter_2.lua'")
+_ =3D test_run:cmd("start server router")
+
+--
+-- gh-237: replica should be able to boot before master. Before the issue = was
+-- fixed, replica always tried to install a trigger on _bucket space even = when
+-- it was not created on a master yet - that lead to an exception in
+-- storage.cfg. Now it should not install the trigger at all, because anyw= ay it
+-- is not needed on replica for anything.
+--
+
+test_run:switch('box_1_b')
+vshard.storage.cfg(cfg, instance_uuid)
+-- _bucket is not created yet. Will fail.
+util.check_error(vshard.storage.call, 1, 'read', 'echo', {= 100})
+
+test_run:switch('default')
+util.map_evals(test_run, {REPLICASET_1}, 'bootstrap_storage(\'memt= x\')')
+
+test_run:switch('box_1_a')
+vshard.storage.cfg(cfg, instance_uuid)
+
+test_run:switch('box_1_b')
+test_run:wait_lsn('box_1_b', 'box_1_a')
+-- Fails, but gracefully. Because the bucket is not found here.
+vshard.storage.call(1, 'read', 'echo', {100})
+-- Should not have triggers.
+#box.space._bucket:on_replace()
+
+test_run:switch('router')
+vshard.router.bootstrap()
+vshard.router.callro(1, 'echo', {100})
+
+test_run:switch("default")
+test_run:cmd('stop server router')
+test_run:cmd('delete server router')
+test_run:drop_cluster(REPLICASET_1)
diff --git a/vshard/storage/init.lua b/vshard/storage/init.lua
index c6a78fe..5464824 100644
--- a/vshard/storage/init.lua
+++ b/vshard/storage/init.lua
@@ -94,6 +94,17 @@ if not M then
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0-- detect that _bucket was not changed be= tween yields.
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0--
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0bucket_generation =3D 0,
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 --
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 -- Reference to the function used as on_replac= e trigger on
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 -- _bucket space. It is used to replace the tr= igger with
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 -- a new function when reload happens. It is k= ept
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 -- explicitly because the old function is dele= ted on
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 -- reload from the global namespace. On the ot= her hand, it
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 -- is still stored in _bucket:on_replace() som= ewhere, but
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 -- it is not known where. The only 100% way to= be able to
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 -- replace the old function is to keep its ref= erence.
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 --
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 bucket_on_replace =3D nil,

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0------------------- Garbage collection --= -----------------
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0-- Fiber to remove garbage buckets data.<= br> @@ -2435,8 +2446,14 @@ local function storage_cfg(cfg, this_replica_uuid, i= s_reload)
=C2=A0 =C2=A0 =C2=A0local uri =3D luri.parse(this_replica.uri)
=C2=A0 =C2=A0 =C2=A0schema_upgrade(is_master, uri.login, uri.password)

-=C2=A0 =C2=A0 local old_trigger =3D box.space._bucket:on_replace()[1]
-=C2=A0 =C2=A0 box.space._bucket:on_replace(bucket_generation_increment, ol= d_trigger)
+=C2=A0 =C2=A0 if M.bucket_on_replace then
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 box.space._bucket:on_replace(nil, M.bucket_on_= replace)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 M.bucket_on_replace =3D nil
+=C2=A0 =C2=A0 end
+=C2=A0 =C2=A0 if is_master then
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 box.space._bucket:on_replace(bucket_generation= _increment)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 M.bucket_on_replace =3D bucket_generation_incr= ement
+=C2=A0 =C2=A0 end

=C2=A0 =C2=A0 =C2=A0lreplicaset.rebind_replicasets(new_replicasets, M.repli= casets)
=C2=A0 =C2=A0 =C2=A0lreplicaset.outdate_replicasets(M.replicasets)
diff --git a/vshard/storage/reload_evolution.lua b/vshard/storage/reload_ev= olution.lua
index 1abc9e2..f38af74 100644
--- a/vshard/storage/reload_evolution.lua
+++ b/vshard/storage/reload_evolution.lua
@@ -17,6 +17,14 @@ migrations[#migrations + 1] =3D function(M)
=C2=A0 =C2=A0 =C2=A0-- Code to update Lua objects.
=C2=A0end

+migrations[#migrations + 1] =3D function(M)
+=C2=A0 =C2=A0 local bucket =3D box.space._bucket
+=C2=A0 =C2=A0 if bucket then
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 assert(M.bucket_on_replace =3D=3D nil)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 M.bucket_on_replace =3D bucket:on_replace()[1]=
+=C2=A0 =C2=A0 end
+end
+
=C2=A0--
=C2=A0-- Perform an update based on a version stored in `M` (internals). =C2=A0-- @param M Old module internals which should be updated.
--
2.21.1 (Apple Git-122.3)

--000000000000ae6b3805ac314c47--