Skip to content

Commit fe15245

Browse files
committed
rebalancer: refactoring of rebalancer_request_state
Before this patch the function `rebalancer_request_state` returned only nil in case of errors (e.g. presence of SENDING, RECEIVING, GARBAGE buckets, not active rebalancer). This makes it inconsistent compared to other rebalancer functions. Now, in case of errors we return `(nil, err)` instead of `nil`. It can help us to propagate a meaningful error in rebalancer service. NO_TEST=refactoring NO_DOC=refactoring
1 parent 06c15b0 commit fe15245

File tree

5 files changed

+35
-18
lines changed

5 files changed

+35
-18
lines changed

test/rebalancer/rebalancer.result

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,6 @@ _bucket:update({150}, {{'=', 2, vshard.consts.BUCKET.RECEIVING}})
318318
---
319319
- [150, 'receiving']
320320
...
321-
wait_rebalancer_state("Some buckets are not active", test_run)
322-
---
323-
...
324321
_bucket:update({150}, {{'=', 2, vshard.consts.BUCKET.ACTIVE}})
325322
---
326323
- [150, 'active']

test/rebalancer/rebalancer.test.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ util.map_bucket_protection(test_run, {REPLICASET_1}, false)
156156
test_run:switch('box_1_a')
157157
vshard.storage.rebalancer_enable()
158158
_bucket:update({150}, {{'=', 2, vshard.consts.BUCKET.RECEIVING}})
159-
wait_rebalancer_state("Some buckets are not active", test_run)
160159
_bucket:update({150}, {{'=', 2, vshard.consts.BUCKET.ACTIVE}})
161160
vshard.storage.sync()
162161

test/unit/rebalancer.result

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,13 @@ build_routes(replicasets)
290290
vshard.storage.internal.is_master = true
291291
---
292292
...
293-
get_state = vshard.storage._rebalancer_request_state
294-
---
295-
...
293+
get_state = function() \
294+
local replicaset, active_buckets = vshard.storage._rebalancer_request_state() \
295+
if replicaset == nil then \
296+
active_buckets.trace = nil \
297+
end \
298+
return replicaset, active_buckets \
299+
end \
296300
_bucket = box.schema.create_space('_bucket')
297301
---
298302
...
@@ -318,13 +322,21 @@ get_state()
318322
---
319323
- bucket_active_count: 2
320324
bucket_pinned_count: 0
325+
- null
321326
...
322327
_bucket:replace{1, consts.BUCKET.RECEIVING}
323328
---
324329
- [1, 'receiving']
325330
...
326331
get_state()
327332
---
333+
- null
334+
- name: PROC_LUA
335+
code: 32
336+
base_type: ClientError
337+
type: ClientError
338+
details: Replica _ has receiving buckets
339+
message: Replica _ has receiving buckets
328340
...
329341
vshard.storage.internal.is_master = false
330342
---

test/unit/rebalancer.test.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,13 @@ build_routes(replicasets)
7676
-- Test rebalancer local state.
7777
--
7878
vshard.storage.internal.is_master = true
79-
get_state = vshard.storage._rebalancer_request_state
79+
get_state = function() \
80+
local replicaset, active_buckets = vshard.storage._rebalancer_request_state() \
81+
if replicaset == nil then \
82+
active_buckets.trace = nil \
83+
end \
84+
return replicaset, active_buckets \
85+
end \
8086
_bucket = box.schema.create_space('_bucket')
8187
pk = _bucket:create_index('pk')
8288
status = _bucket:create_index('status', {parts = {{2, 'string'}}, unique = false})

vshard/storage/init.lua

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2851,12 +2851,10 @@ local function rebalancer_service_f(service, limiter)
28512851
return
28522852
end
28532853
if not status or replicasets == nil then
2854-
local err = status and total_bucket_active_count or replicasets
2855-
if err then
2856-
limiter:log_error(err, service:set_status_error(
2857-
'Error during downloading rebalancer states: %s', err))
2858-
end
2859-
log.info('Some buckets are not active, retry rebalancing later')
2854+
local err = total_bucket_active_count
2855+
limiter:log_error(err, service:set_status_error(
2856+
'Error during downloading rebalancer states: %s',
2857+
err))
28602858
service:set_activity('idling')
28612859
lfiber.testcancel()
28622860
lfiber.sleep(consts.REBALANCER_WORK_INTERVAL)
@@ -2945,18 +2943,23 @@ local function rebalancer_request_state()
29452943
return nil, err
29462944
end
29472945
if not M.is_rebalancer_active or rebalancing_is_in_progress() then
2948-
return
2946+
return nil, lerror.make('Rebalancer is not active or is in progress')
29492947
end
29502948
local _bucket = box.space._bucket
29512949
local status_index = _bucket.index.status
2950+
local repl_id = M.this_replica and M.this_replica.id or '_'
2951+
local buckets_invalid_state = 'Replica %s has %s buckets'
29522952
if #status_index:select({BSENDING}, {limit = 1}) > 0 then
2953-
return
2953+
return nil, lerror.make(string.format(buckets_invalid_state, repl_id,
2954+
consts.BUCKET.SENDING))
29542955
end
29552956
if #status_index:select({BRECEIVING}, {limit = 1}) > 0 then
2956-
return
2957+
return nil, lerror.make(string.format(buckets_invalid_state, repl_id,
2958+
consts.BUCKET.RECEIVING))
29572959
end
29582960
if #status_index:select({BGARBAGE}, {limit = 1}) > 0 then
2959-
return
2961+
return nil, lerror.make(string.format(buckets_invalid_state, repl_id,
2962+
consts.BUCKET.GARBAGE))
29602963
end
29612964
return {
29622965
bucket_active_count = status_index:count({BACTIVE}),

0 commit comments

Comments
 (0)