Skip to content

cluster: drop instances removed from config in cluster.sync #424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

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

## 1.1.0

Expand Down
21 changes: 16 additions & 5 deletions luatest/cluster.lua
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ end
--
-- * Write the new config into the config file.
-- * Update the internal list of instances.
-- * Drops instances removed from the config.
--
-- @tab config New config.
function Cluster:sync(config)
Expand All @@ -206,16 +207,26 @@ function Cluster:sync(config)

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

for i, name in ipairs(instance_names) do
if self._server_map[name] == nil then
local iserver = server:new(fun.chain(self._server_opts, {
local old_server_map = self._server_map
self._server_map = {}
self._servers = {}

for _, name in ipairs(instance_names) do
local iserver = old_server_map[name]
if iserver == nil then
iserver = server:new(fun.chain(self._server_opts, {
alias = name,
}):tomap())
table.insert(self._servers, i, iserver)
self._server_map[name] = iserver
else
old_server_map[name] = nil
end
self._server_map[name] = iserver
table.insert(self._servers, iserver)
end

for _, iserver in pairs(old_server_map) do
iserver:drop()
end
Comment on lines +227 to +229
Copy link
Member

Choose a reason for hiding this comment

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

That's interesting. We don't start new instances in :sync(), just create server objects in Lua. So, it looks like we shouldn't stop them too.

But,

  1. If we don't stop the server, it remains running after the test case.
  2. A user may want some automation about synchronization of the configured and present instances.

The first point is simple: just track the excluded server objects in some list to call :drop() on it in the after_each hook. self._excluded_server_map or something like this. We can also reuse the server object from this mapping if a previously deleted server is added again.

The second point is really interesting. Since tarantool 3.3 a user can configure autoexpelling if needed. So, we really need just update the config and start/stop processes to manage the cluster.

It looks like we really have two suitable methods:

  • update config + update server objects
  • update config + update server objects + start/stop processes

The first one is what :sync() do now. We can implement the second as a :sync() option. What do you think?

end

--- Reload configuration on all the instances.
Expand Down
23 changes: 15 additions & 8 deletions test/cluster_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -133,34 +133,41 @@ g.test_sync = function()
:use_group('g-001')
:use_replicaset('r-001')
:add_instance('i-001', {})
:use_replicaset('r-002')
:add_instance('i-002', {})
:config()

local c = cluster:new(config, server_opts)

t.assert_equals(c:size(), 1)
t.assert_equals(c:size(), 2)

c:start()
assert_instance_running(c, 'i-001')

c:stop()
assert_instance_stopped(c, 'i-001')
local server1 = c['i-001']
local server2 = c['i-002']
t.assert_is_not(server1.process, nil)
t.assert_is_not(server2.process, nil)

local config2 = cbuilder:new()
:use_group('g-001')
:use_replicaset('r-001')
:use_replicaset('r-002')
:add_instance('i-002', {})

:use_group('g-002')
:use_replicaset('r-002')
:use_replicaset('r-003')
:add_instance('i-003', {})

:config()

c:sync(config2)

t.assert_equals(c:size(), 3)
t.assert_is(c['i-001'], nil)
t.assert_is(c['i-002'], server2)
t.assert_is(server1.process, nil)
t.assert_is_not(server2.process, nil)

t.assert_equals(c:size(), 2)

c:start_instance('i-002')
c:start_instance('i-003')
assert_instance_running(c, 'i-002')
assert_instance_running(c, 'i-003')
Expand Down
Loading