92
92
93
93
-- {{{ Helpers
94
94
95
+ -- Start all instances in the list.
96
+ --
97
+ -- @tab[opt] opts Options.
98
+ -- @bool[opt] opts.wait_until_ready Wait until servers are ready
99
+ -- (default: true).
100
+ -- @bool[opt] opts.wait_until_running Wait until servers are running
101
+ -- (default: wait_until_ready).
102
+ local function start_instances (servers , opts )
103
+ for _ , iserver in ipairs (servers ) do
104
+ iserver :start ({wait_until_ready = false })
105
+ end
106
+
107
+ -- wait_until_ready is true by default.
108
+ local wait_until_ready = true
109
+ if opts ~= nil and opts .wait_until_ready ~= nil then
110
+ wait_until_ready = opts .wait_until_ready
111
+ end
112
+
113
+ if wait_until_ready then
114
+ for _ , iserver in ipairs (servers ) do
115
+ iserver :wait_until_ready ()
116
+ end
117
+ end
118
+
119
+ -- wait_until_running is equal to wait_until_ready by default.
120
+ local wait_until_running = wait_until_ready
121
+ if opts ~= nil and opts .wait_until_running ~= nil then
122
+ wait_until_running = opts .wait_until_running
123
+ end
124
+
125
+ if wait_until_running then
126
+ for _ , iserver in ipairs (servers ) do
127
+ helpers .retrying ({timeout = 60 }, function ()
128
+ assertions .assert_equals (iserver :eval (' return box.info.status' ),
129
+ ' running' )
130
+ end )
131
+ end
132
+ end
133
+ end
134
+
95
135
-- Collect names of all the instances defined in the config
96
136
-- in the alphabetical order.
97
137
local function instance_names_from_config (config )
@@ -131,39 +171,11 @@ end
131
171
--
132
172
-- @tab[opt] opts Cluster startup options.
133
173
-- @bool[opt] opts.wait_until_ready Wait until servers are ready
134
- -- (default: false).
174
+ -- (default: true).
175
+ -- @bool[opt] opts.wait_until_running Wait until servers are running
176
+ -- (default: wait_until_ready).
135
177
function Cluster :start (opts )
136
- self :each (function (iserver )
137
- iserver :start ({wait_until_ready = false })
138
- end )
139
-
140
- -- wait_until_ready is true by default.
141
- local wait_until_ready = true
142
- if opts ~= nil and opts .wait_until_ready ~= nil then
143
- wait_until_ready = opts .wait_until_ready
144
- end
145
-
146
- if wait_until_ready then
147
- self :each (function (iserver )
148
- iserver :wait_until_ready ()
149
- end )
150
- end
151
-
152
- -- wait_until_running is equal to wait_until_ready by default.
153
- local wait_until_running = wait_until_ready
154
- if opts ~= nil and opts .wait_until_running ~= nil then
155
- wait_until_running = opts .wait_until_running
156
- end
157
-
158
- if wait_until_running then
159
- self :each (function (iserver )
160
- helpers .retrying ({timeout = 60 }, function ()
161
- assertions .assert_equals (iserver :eval (' return box.info.status' ),
162
- ' running' )
163
- end )
164
-
165
- end )
166
- end
178
+ start_instances (self ._servers , opts )
167
179
end
168
180
169
181
--- Start the given instance.
@@ -187,8 +199,12 @@ function Cluster:drop()
187
199
for _ , iserver in ipairs (self ._servers or {}) do
188
200
iserver :drop ()
189
201
end
202
+ for _ , iserver in ipairs (self ._expelled_servers or {}) do
203
+ iserver :drop ()
204
+ end
190
205
self ._servers = nil
191
206
self ._server_map = nil
207
+ self ._expelled_servers = nil
192
208
end
193
209
194
210
--- Sync the cluster object with the new config.
@@ -197,25 +213,67 @@ end
197
213
--
198
214
-- * Write the new config into the config file.
199
215
-- * Update the internal list of instances.
216
+ -- * Optionally starts instances added to the config and stops instances
217
+ -- removed from the config.
200
218
--
201
219
-- @tab config New config.
202
- function Cluster :sync (config )
220
+ -- @tab[opt] opts Options.
221
+ -- @bool[opt] opts.start_stop Start/stop added/removed servers
222
+ -- (default: false).
223
+ -- @bool[opt] opts.wait_until_ready Wait until servers are ready
224
+ -- (default: true; used only if start_stop is set).
225
+ -- @bool[opt] opts.wait_until_running Wait until servers are running
226
+ -- (default: wait_until_ready; used only if start_stop is set).
227
+ function Cluster :sync (config , opts )
203
228
assert (type (config ) == ' table' )
204
229
205
230
local instance_names = instance_names_from_config (config )
206
231
207
232
treegen .write_file (self ._dir , self ._config_file_rel , yaml .encode (config ))
208
233
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 , {
234
+ local server_map = self ._server_map
235
+ self ._server_map = {}
236
+ self ._servers = {}
237
+ local new_servers = {}
238
+
239
+ for _ , name in ipairs (instance_names ) do
240
+ local iserver = server_map [name ]
241
+ if iserver == nil then
242
+ iserver = server :new (fun .chain (self ._server_opts , {
212
243
alias = name ,
213
244
}):tomap ())
214
- table.insert (self ._servers , i , iserver )
215
- self ._server_map [name ] = iserver
245
+ table.insert (new_servers , iserver )
246
+ else
247
+ server_map [name ] = nil
216
248
end
249
+ self ._server_map [name ] = iserver
250
+ table.insert (self ._servers , iserver )
217
251
end
218
252
253
+ local expelled_servers = {}
254
+ for _ , iserver in pairs (server_map ) do
255
+ table.insert (expelled_servers , iserver )
256
+ end
257
+
258
+ -- Sort expelled servers by name for reproducibility.
259
+ table.sort (expelled_servers , function (a , b ) return a .alias < b .alias end )
260
+
261
+ -- Add expelled servers to the list to be dropped with the cluster.
262
+ for _ , iserver in pairs (expelled_servers ) do
263
+ table.insert (self ._expelled_servers , iserver )
264
+ end
265
+
266
+ local start_stop = false
267
+ if opts ~= nil and opts .start_stop ~= nil then
268
+ start_stop = opts .start_stop
269
+ end
270
+
271
+ if start_stop then
272
+ start_instances (new_servers , opts )
273
+ for _ , iserver in ipairs (expelled_servers ) do
274
+ iserver :stop ()
275
+ end
276
+ end
219
277
end
220
278
221
279
--- Reload configuration on all the instances.
@@ -297,6 +355,7 @@ function Cluster:new(config, server_opts, opts)
297
355
-- Store a cluster object in 'g'.
298
356
self ._servers = servers
299
357
self ._server_map = server_map
358
+ self ._expelled_servers = {}
300
359
self ._dir = dir
301
360
self ._config_file_rel = config_file_rel
302
361
self ._server_opts = server_opts
0 commit comments