Skip to content

Wait for master sync during bootstrap - #667

Open
kamenkremen wants to merge 12 commits into
tarantool:masterfrom
kamenkremen:gh-650
Open

kamenkremen wants to merge 12 commits into
tarantool:masterfrom
kamenkremen:gh-650

Conversation

@kamenkremen

@kamenkremen kamenkremen commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

A new master may still have no buckets while it is catching up with the old master. In this state, bootstrap may consider the cluster empty and create duplicate buckets.

Closes #650
Closes #685
Closes #686
Closes #687
Closes #688
Closes #689
Closes #690
Closes #691
Closes #692

@Serpentian Serpentian left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great patch! It's very well done in general, we just should work with it a little bit

Comment thread test/luatest_helpers/vtest.lua Resolved
Comment thread vshard/router/init.lua Resolved
Comment thread vshard/router/init.lua Resolved
Comment thread vshard/storage/init.lua Resolved
Comment thread vshard/storage/init.lua Resolved
Comment thread test/router/boot_replica_first.test.lua Resolved
Comment thread vshard/storage/init.lua Resolved
Comment thread vshard/router/init.lua Resolved
Comment thread vshard/router/init.lua Resolved
Comment thread test/router-luatest/router_2_2_test.lua Resolved
@Serpentian Serpentian assigned kamenkremen and unassigned Serpentian Aug 27, 2026
@kamenkremen
kamenkremen force-pushed the gh-650 branch 2 times, most recently from 8087648 to 59b2464 Compare September 1, 2026 13:09
@kamenkremen kamenkremen assigned Serpentian and unassigned kamenkremen Sep 1, 2026

@Serpentian Serpentian left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done! Only nits, no major comments from me

Comment thread vshard/storage/init.lua Resolved
Comment thread vshard/storage/init.lua Resolved
Comment thread test/router/boot_replica_first.test.lua Resolved
Comment thread vshard/storage/init.lua Resolved
Comment thread vshard/router/init.lua Resolved
Comment thread vshard/router/init.lua Resolved
Comment thread vshard/storage/init.lua Resolved
Comment thread test/router-luatest/router_2_2_test.lua Resolved
Comment thread vshard/storage/init.lua Resolved
Comment thread vshard/storage/reload_evolution.lua Resolved
Comment thread vshard/storage/init.lua Resolved
Comment thread test/luatest_helpers/vtest.lua Resolved
Comment thread test/luatest_helpers/vtest.lua Resolved
Comment thread vshard/router/init.lua Outdated
for _, replicaset in pairs(router.replicasets) do
table.insert(replicasets, replicaset)
count, err = replicaset:callrw('vshard.storage.buckets_count', {}, opts)
count, err = replicaset:callrw(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously the buckets_count call was cheap and fast. Now we can wait pretty long and we should not do that synchronously anymore. So, the question here: can we use the replicaset.masters_map_call()?

@kamenkremen kamenkremen Oct 6, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we just used masters_map_call here instead of callrw, it would change the behavior, at least in the following scenario: bootstrap is called with if_not_bootstrapped = true, one replicaset is down, and another already has buckets. With the callrw loop, bootstrap will return true; with masters_map_call, it will return an error, since the latter fails if it cannot reach every master. Right now I've implemented it with a fallback to callrw when masters_map_call fails and if_not_bootstrapped is set

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This double call for every master in case of one connection error looks very crutchy. And indeed, it'll be the behavior change, which we should avoid.

Can we instead consider introducing option to the masters_map_call which will be called coverage = 'full/accidental'. The accidental mode will allow to call only on instances, which we managed to connect. You can check the rfc in the https://github.com/tarantool/vshard/issues/333#issuecomment-6440688326, point 4.8. This extension to the masters_map_call` will be also useful there. So, separate commit with tests, which can be marked as needed for #650 and #333. WDYT?

@Serpentian Serpentian left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The patchset looks good. I have only one minor and one major comments left and that's it, it seems. Thank you for working on that! And please, do not forget to rebase on master to resolve the conflicts in the test files

Comment thread vshard/router/init.lua Outdated
if not results and not if_not_bootstrapped then
return nil, map_err
end
for id, replicaset in pairs(router.replicasets) do

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The router.replicasets can change due to reconfiguration. So that we pass one table to masters_map_call and another one to this loop, and it"ll crash in unpack two lines below. Let's just save the replicasets in the temporary variable instead (I'm ok, if we won't add the test, LLM will wanna add it, I'd avoid)

@kamenkremen
kamenkremen force-pushed the gh-650 branch 2 times, most recently from bd3285a to a0d751e Compare November 30, 2026 23:50
A reconfiguration can replace the replicaset map while bootstrap waits
for counts or creates buckets. A removed replicaset can still serve
requests during the connection outdate delay.

Check the map before using the counts and after each successful create.
Return OBJECT_IS_OUTDATED if it changed. An in-flight create can still
complete, but bootstrap stops before sending the next one.

Part of tarantool#650
Closes tarantool#687

NO_DOC=bugfix
A suspended caller can retain the cached getter from before reload.
When the new module invalidates the shared entry, that getter can
return nil. Compute missing entries on the shared table so every
generation sees the same value and cache hits stay a single lookup.

Part of tarantool#650
Closes tarantool#686

NO_DOC=internal
An asynchronous net.box call returns a future before the replica has
replied. Counting submission as success resets the failure counter
and prevents repeated map timeouts from increasing the network timeout.

Update the counters when collecting replies. Count a local wait timeout
only if the RPC budget covered the replica's network timeout. A server
exception is returned to the caller and leaves the counters unchanged.
On failure, also count successful replies already available from the
other replicas.

Needed for tarantool#650
Closes tarantool#689

NO_DOC=bugfix
A connected net.box can still be waiting for the vshard handshake.
wait_connected() used to return immediately in this case, so the next
async call could fail with VHANDSHAKE_NOT_COMPLETE.

Wait for pending handshakes in both wait_connected() and
wait_connected_all(), using the remaining timeout. Recheck the other
connections after a wait.

Needed for tarantool#650
Closes tarantool#691

NO_DOC=bugfix
A master can disappear while masters_map_call() waits for the other
connections. Assigning nil to the result of the master lookup silently
left that replicaset out of the call.

Return MISSING_MASTER and the replicaset ID rather than reporting an
incomplete map as a success.

Needed for tarantool#650
Closes tarantool#692

NO_DOC=bugfix
With if_not_bootstrapped, bootstrap keeps checking other replicasets
after an error in case one of them already has buckets. If the last
replicaset is empty, its successful reply clears err and bootstrap
returns nil without an explanation.

Return the saved error when no replicaset reported any buckets.

Needed for tarantool#650
Closes tarantool#688

NO_DOC=bugfix
A replica using automatic master discovery may not know its master.
Bucket space operations tried to read its ID while building NON_MASTER
and raised a Lua error instead. Return NON_MASTER without a master ID.

Needed for tarantool#650
Closes tarantool#690

NO_DOC=bugfix
When bucket_force_create() runs inside an existing transaction,
box.begin() fails and the error handler rolls back the caller's writes.

Return the error from box.begin() without rolling back. Only roll back
a transaction that bucket_force_create() successfully started.

Needed for tarantool#650
Closes tarantool#685

NO_DOC=bugfix
storage/init.lua is close to the LuaJIT limit of 200 local variables.
Inline four single-use aliases to leave more local slots available.

Needed for tarantool#650

NO_DOC=refactoring
NO_TEST=refactoring
The force bucket creation operation can modify `_bucket` while an
instance is not a synchronized master. This is unsafe for callers that
must not create bucket records on a stale or catching-up master.

Introduce `bucket_create()`. It checks that the instance is a
synchronized master before each _bucket write.

Use bucket_create() in cluster_bootstrap() so test clusters also wait
for the master role and bucket synchronization before creating buckets.

Needed for tarantool#650

@TarantoolBot document
Title: vshard: introduce bucket_create

The storage public API now includes:

* `vshard.storage.bucket_create(first_bucket_id[, count])` creates
  `count` consecutive buckets in the `ACTIVE` state, starting with
  `first_bucket_id`. The default value of `count` is 1.

  Unlike `bucket_force_create()`, it requires a synchronized master
  for each write. It returns `true` on success and `nil, error` if a
  write fails.
Some callers can use replies even if a master is unreachable. Add an
optional 'accidental' mode that calls each master independently and
returns the replies received, plus one error and its replicaset ID.
If no RPC succeeds, the result map is empty. Keep 'full' as the default.

An explicit timeout covers discovery and the RPCs. Without one, allow
MASTER_SEARCH_TIMEOUT for discovery and use each master's network
timeout for accidental coverage, or the largest one for full coverage.
Callers can pass the remaining RPC budget divided by 1.5 to the
storage. Idempotent bootstrap can also stop waiting for other masters
as soon as one reports a positive count.

Needed for tarantool#650
Needed for tarantool#333

NO_DOC=internal
A newly promoted master can report zero buckets before its _bucket
space catches up. Bootstrap can then create buckets that already exist.

Ask masters in parallel for their synchronized bucket counts. With
if_not_bootstrapped, return on the first non-empty reply even if another
master is unreachable. Create buckets only after every master reports
zero.

Preserve storage errors from async replies and report a connection lost
on reconfiguration instead of sending on a detached master.

Use bucket_create() so that a master change after the count check
cannot bypass the synchronization check at write time.

Closes tarantool#650

NO_DOC=bugfix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment