Skip to content

Commit 05b71b0

Browse files
committed
storage: refactoring of wait_lsn logic
Before this patch we compared vclocks only in `wait_lsn` function in storage module. However in future patches (e.g. gh-214) we will need to do this even in tests. Also in gh-214 we will use very similar logic of waiting vclocks but with different sign (all vclock components of current storage should be "greater or equal" than components of replicas' vclocks instead of "less or equal") To avoid duplication of code we unify the process of vclocks' comparison and transform `vclock_lesseq` into more general `vclock_compare` function which can allow us to make different comparisons of vclocks by comparator. We move this function in `util` vshard module. Also we transform `wait_lsn` into `storage_wait_vclock_replicated`. This function does the similar thing like `wait_lsn`, but the main logic has migrated into `storage_wait_vclock_template` which is responsible for waiting for current vclock will satisfy the comparator condition. Needed for #214 NO_TEST=refactoring NO_DOC=refactoring
1 parent 39aada8 commit 05b71b0

File tree

1 file changed

+21
-26
lines changed

1 file changed

+21
-26
lines changed

vshard/storage/init.lua

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,23 +1142,6 @@ end
11421142
-- Replicaset
11431143
--------------------------------------------------------------------------------
11441144

1145-
-- Vclock comparing function
1146-
local function vclock_lesseq(vc1, vc2)
1147-
local lesseq = true
1148-
for i, lsn in ipairs(vc1) do
1149-
if i == 0 then
1150-
-- Skip local component.
1151-
goto continue
1152-
end
1153-
lesseq = lesseq and lsn <= (vc2[i] or 0)
1154-
if not lesseq then
1155-
break
1156-
end
1157-
::continue::
1158-
end
1159-
return lesseq
1160-
end
1161-
11621145
local function is_replica_in_configuration(replica)
11631146
local is_named = M.this_replica.id == M.this_replica.name
11641147
local id = is_named and replica.name or replica.uuid
@@ -1177,14 +1160,15 @@ local function is_replica_in_configuration(replica)
11771160
return true
11781161
end
11791162

1180-
local function wait_lsn(timeout, interval)
1181-
local info = box.info
1182-
local current_id = info.id
1183-
local vclock = info.vclock
1163+
local function storage_wait_vclock_template(timeout, interval, comparator)
1164+
local current_id = box.info.id
11841165
local deadline = fiber_clock() + timeout
11851166
repeat
11861167
local done = true
11871168
for _, replica in ipairs(box.info.replication) do
1169+
-- The current vclock may be changed between iterations. We need to
1170+
-- track the most recent one.
1171+
local vclock = box.info.vclock
11881172
-- We should not check the current instance as there's
11891173
-- no downstream. Moreover, it's not guaranteed that the
11901174
-- first replica is the same as the current one, so ids
@@ -1201,8 +1185,8 @@ local function wait_lsn(timeout, interval)
12011185
end
12021186

12031187
local down = replica.downstream
1204-
if not down or (down.status == 'stopped' or
1205-
not vclock_lesseq(vclock, down.vclock)) then
1188+
if not down or down.status == 'stopped' or
1189+
not util.vclock_compare(vclock, down.vclock, comparator) then
12061190
done = false
12071191
break
12081192
end
@@ -1216,11 +1200,22 @@ local function wait_lsn(timeout, interval)
12161200
return nil, lerror.timeout()
12171201
end
12181202

1203+
--
1204+
-- Waits until current master successfully replicates all data to other
1205+
-- replicas. It means that all components of master's vclock will not
1206+
-- superior to replcas' vclock components.
1207+
--
1208+
local function storage_wait_vclock_replicated(timeout, interval)
1209+
return storage_wait_vclock_template(timeout, interval, function(c1, c2)
1210+
return c1 <= (c2 or 0)
1211+
end)
1212+
end
1213+
12191214
local function sync(timeout)
12201215
if timeout ~= nil and type(timeout) ~= 'number' then
12211216
error('Usage: vshard.storage.sync([timeout: number])')
12221217
end
1223-
return wait_lsn(timeout or M.sync_timeout, 0.001)
1218+
return storage_wait_vclock_replicated(timeout or M.sync_timeout, 0.001)
12241219
end
12251220

12261221
--------------------------------------------------------------------------------
@@ -2099,8 +2094,8 @@ end
20992094
-- were approved for deletion.
21002095
--
21012096
local function gc_bucket_process_sent_one_batch_xc(batch)
2102-
local ok, err = wait_lsn(consts.GC_WAIT_LSN_TIMEOUT,
2103-
consts.GC_WAIT_LSN_STEP)
2097+
local ok, err = storage_wait_vclock_replicated(consts.GC_WAIT_LSN_TIMEOUT,
2098+
consts.GC_WAIT_LSN_STEP)
21042099
if not ok then
21052100
local msg = 'Failed to delete sent buckets - could not sync '..
21062101
'with replicas'

0 commit comments

Comments
 (0)