diff --git a/test/storage-luatest/rebalancer_test.lua b/test/storage-luatest/rebalancer_test.lua new file mode 100644 index 00000000..00912dd8 --- /dev/null +++ b/test/storage-luatest/rebalancer_test.lua @@ -0,0 +1,191 @@ +local t = require('luatest') +local fiber = require('fiber') +local vtest = require('test.luatest_helpers.vtest') +local vutil = require('vshard.util') +local verror = require('vshard.error') + +local group_config = {{}} + +-- if vutil.feature.memtx_mvcc then +-- table.insert(group_config, {memtx_use_mvcc_engine = true}) +-- end + +local test_group = t.group('storage', group_config) + +local cfg_template = { + rebalancer_mode = 'manual', + sharding = { + { + master = 'auto', + rebalancer = true, + replicas = { + replica_1_a = { + read_only = false, + }, + replica_1_b = { + read_only = true, + }, + }, + }, + { + master = 'auto', + replicas = { + replica_2_a = { + read_only = false, + }, + replica_2_b = { + read_only = true, + }, + }, + }, + { + master = 'auto', + replicas = { + replica_3_a = { + read_only = false, + }, + replica_3_b = { + read_only = true, + }, + }, + }, + }, + bucket_count = 30 +} +local global_cfg + +test_group.before_all(function(g) + cfg_template.memtx_use_mvcc_engine = g.params.memtx_use_mvcc_engine + global_cfg = vtest.config_new(cfg_template) + + vtest.cluster_new(g, global_cfg) + vtest.cluster_bootstrap(g, global_cfg) + vtest.cluster_wait_vclock_all(g) + vtest.cluster_exec_each(g, function() + ivconst.MASTER_SEARCH_WORK_INTERVAL = ivtest.busy_step + end) + + vtest.cluster_exec_each_master(g, function(engine) + local s = box.schema.space.create('test', { + engine = engine, + format = { + {'id', 'unsigned'}, + {'bucket_id', 'unsigned'}, + }, + }) + s:create_index('id', {parts = {'id'}}) + s:create_index('bucket_id', { + parts = {'bucket_id'}, unique = false + }) + end, {g.params.engine}) +end) + +test_group.after_all(function(g) + -- g.cluster:drop() +end) + +test_group.test_locate_with_flag = function(g) + t.assert_equals(vtest.cluster_rebalancer_find(g), 'replica_1_a') + + local new_cfg_template = table.deepcopy(cfg_template) + new_cfg_template.sharding[1].rebalancer = false + new_cfg_template.sharding[2].rebalancer = true + local new_global_cfg = vtest.config_new(new_cfg_template) + vtest.cluster_cfg(g, new_global_cfg) + t.helpers.retrying({timeout = vtest.wait_timeout}, function() + t.assert_equals(vtest.cluster_rebalancer_find(g), 'replica_2_a') + end) + + new_cfg_template.sharding[2].replicas.replica_2_a.read_only = true + new_cfg_template.sharding[2].replicas.replica_2_b.read_only = false + new_global_cfg = vtest.config_new(new_cfg_template) + vtest.cluster_cfg(g, new_global_cfg) + t.helpers.retrying({timeout = vtest.wait_timeout}, function() + t.assert_equals(vtest.cluster_rebalancer_find(g), 'replica_2_b') + end) + + new_cfg_template.sharding[2].rebalancer = false + new_cfg_template.sharding[3].rebalancer = true + new_global_cfg = vtest.config_new(new_cfg_template) + vtest.cluster_cfg(g, new_global_cfg) + fiber.sleep(0.1) + t.helpers.retrying({timeout = vtest.wait_timeout}, function() + t.assert_equals(vtest.cluster_rebalancer_find(g), 'replica_3_a') + end) + + new_cfg_template.sharding[3].master = nil + new_cfg_template.sharding[3].replicas.replica_3_b.master = true + new_global_cfg = vtest.config_new(new_cfg_template) + vtest.cluster_cfg(g, new_global_cfg) + t.helpers.retrying({timeout = vtest.wait_timeout}, function() + t.assert_equals(vtest.cluster_rebalancer_find(g), 'replica_3_b') + end) + + new_cfg_template = table.deepcopy(cfg_template) + new_cfg_template.sharding[1].rebalancer = false + new_global_cfg = vtest.config_new(new_cfg_template) + vtest.cluster_cfg(g, new_global_cfg) + t.helpers.retrying({timeout = vtest.wait_timeout}, function() + t.assert_equals(vtest.cluster_rebalancer_find(g), 'replica_2_a') + end) + + new_cfg_template.sharding[2].replicas.replica_2_a.rebalancer = false + new_global_cfg = vtest.config_new(new_cfg_template) + vtest.cluster_cfg(g, new_global_cfg) + t.helpers.retrying({timeout = vtest.wait_timeout}, function() + t.assert_equals(vtest.cluster_rebalancer_find(g), nil) + end) + + new_cfg_template.sharding[2].replicas.replica_2_b.read_only = false + new_global_cfg = vtest.config_new(new_cfg_template) + vtest.cluster_cfg(g, new_global_cfg) + t.helpers.retrying({timeout = vtest.wait_timeout}, function() + t.assert_equals(vtest.cluster_rebalancer_find(g), 'replica_2_b') + end) + + new_cfg_template = table.deepcopy(cfg_template) + new_cfg_template.sharding[1].rebalancer = nil + new_cfg_template.sharding[2].replicas.replica_2_b.rebalancer = true + new_global_cfg = vtest.config_new(new_cfg_template) + vtest.cluster_cfg(g, new_global_cfg) + t.helpers.retrying({timeout = vtest.wait_timeout}, function() + t.assert_equals(vtest.cluster_rebalancer_find(g), 'replica_2_b') + end) + + vtest.cluster_cfg(g, global_cfg) + t.helpers.retrying({timeout = vtest.wait_timeout}, function() + t.assert_equals(vtest.cluster_rebalancer_find(g), 'replica_1_a') + end) +end + +test_group.test_rebalancer_mode = function(g) + local new_cfg_template = table.deepcopy(cfg_template) + new_cfg_template.rebalancer_mode = 'auto' + new_cfg_template.sharding[1].rebalancer = nil + new_cfg_template.sharding[2].rebalancer = true + local new_global_cfg = vtest.config_new(new_cfg_template) + vtest.cluster_cfg(g, new_global_cfg) + t.helpers.retrying({timeout = vtest.wait_timeout}, function() + t.assert_equals(vtest.cluster_rebalancer_find(g), 'replica_1_a') + end) + + new_cfg_template.sharding[1].rebalancer = false + new_cfg_template.sharding[2].rebalancer = nil + new_global_cfg = vtest.config_new(new_cfg_template) + vtest.cluster_cfg(g, new_global_cfg) + t.helpers.retrying({timeout = vtest.wait_timeout}, function() + t.assert_equals(vtest.cluster_rebalancer_find(g), 'replica_2_a') + end) + + new_cfg_template.rebalancer_mode = 'off' + new_global_cfg = vtest.config_new(new_cfg_template) + vtest.cluster_cfg(g, new_global_cfg) + t.helpers.retrying({timeout = vtest.wait_timeout}, function() + t.assert_equals(vtest.cluster_rebalancer_find(g), nil) + end) + + vtest.cluster_cfg(g, global_cfg) + t.helpers.retrying({timeout = vtest.wait_timeout}, function() + t.assert_equals(vtest.cluster_rebalancer_find(g), 'replica_1_a') + end) +end