Skip to content

Commit f21b8ae

Browse files
committed
cluster: drop instances removed from config in cluster.sync
Currently, cluster:sync() leaves instances removed from the config in the instance list. This makes cluster:reload() fail because it tries to reload the config for all instances including those that are not in the config anymore. To fix that, let's make cluster:sync() drop instances that were removed from the config. Closes #423
1 parent e03b037 commit f21b8ae

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
- Fixed a bug when `server:grep_log()` failed to find a string logged in
66
`server:exec()` called immediately before it (gh-421).
7+
- Fixed `cluster:sync()` to drop instances removed from the config. It is
8+
now possible to reload the config with `cluster:reload()` after removing
9+
an instance (gh-423).
710

811
## 1.1.0
912

luatest/cluster.lua

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ end
197197
--
198198
-- * Write the new config into the config file.
199199
-- * Update the internal list of instances.
200+
-- * Drops instances removed from the config.
200201
--
201202
-- @tab config New config.
202203
function Cluster:sync(config)
@@ -206,16 +207,26 @@ function Cluster:sync(config)
206207

207208
treegen.write_file(self._dir, self._config_file_rel, yaml.encode(config))
208209

209-
for i, name in ipairs(instance_names) do
210-
if self._server_map[name] == nil then
211-
local iserver = server:new(fun.chain(self._server_opts, {
210+
local old_server_map = self._server_map
211+
self._server_map = {}
212+
self._servers = {}
213+
214+
for _, name in ipairs(instance_names) do
215+
local iserver = old_server_map[name]
216+
if iserver == nil then
217+
iserver = server:new(fun.chain(self._server_opts, {
212218
alias = name,
213219
}):tomap())
214-
table.insert(self._servers, i, iserver)
215-
self._server_map[name] = iserver
220+
else
221+
old_server_map[name] = nil
216222
end
223+
self._server_map[name] = iserver
224+
table.insert(self._servers, iserver)
217225
end
218226

227+
for _, iserver in pairs(old_server_map) do
228+
iserver:drop()
229+
end
219230
end
220231

221232
--- Reload configuration on all the instances.

test/cluster_test.lua

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,34 +133,41 @@ g.test_sync = function()
133133
:use_group('g-001')
134134
:use_replicaset('r-001')
135135
:add_instance('i-001', {})
136+
:use_replicaset('r-002')
137+
:add_instance('i-002', {})
136138
:config()
137139

138140
local c = cluster:new(config, server_opts)
139141

140-
t.assert_equals(c:size(), 1)
142+
t.assert_equals(c:size(), 2)
141143

142144
c:start()
143-
assert_instance_running(c, 'i-001')
144145

145-
c:stop()
146-
assert_instance_stopped(c, 'i-001')
146+
local server1 = c['i-001']
147+
local server2 = c['i-002']
148+
t.assert_is_not(server1.process, nil)
149+
t.assert_is_not(server2.process, nil)
147150

148151
local config2 = cbuilder:new()
149152
:use_group('g-001')
150-
:use_replicaset('r-001')
153+
:use_replicaset('r-002')
151154
:add_instance('i-002', {})
152155

153156
:use_group('g-002')
154-
:use_replicaset('r-002')
157+
:use_replicaset('r-003')
155158
:add_instance('i-003', {})
156159

157160
:config()
158161

159162
c:sync(config2)
160163

161-
t.assert_equals(c:size(), 3)
164+
t.assert_is(c['i-001'], nil)
165+
t.assert_is(c['i-002'], server2)
166+
t.assert_is(server1.process, nil)
167+
t.assert_is_not(server2.process, nil)
168+
169+
t.assert_equals(c:size(), 2)
162170

163-
c:start_instance('i-002')
164171
c:start_instance('i-003')
165172
assert_instance_running(c, 'i-002')
166173
assert_instance_running(c, 'i-003')

0 commit comments

Comments
 (0)