@@ -2,21 +2,16 @@ local fiber = require('fiber')
22local vshard_consts = require (' vshard.consts' )
33local utils = require (' crud.common.utils' )
44
5- local MODULE_INTERNALS = ' __module_crud_rebalance'
65local SETTINGS_SPACE_NAME = ' _crud_settings'
6+ local SAFE_MOD_ENABLE_EVENT = ' _crud.safe_mode_enable'
77
88
9- local M = rawget (_G , MODULE_INTERNALS )
10- if not M then
11- M = {
12- safe_mode = false ,
13- safe_mode_enable_hooks = {},
14- safe_mode_disable_hooks = {},
15- _router_cache_last_clear_ts = fiber .time ()
16- }
17- else
18- return M
19- end
9+ local M = {
10+ safe_mode = false ,
11+ safe_mode_enable_hooks = {},
12+ safe_mode_disable_hooks = {},
13+ _router_cache_last_clear_ts = fiber .time ()
14+ }
2015
2116local function create_space ()
2217 local settings_space = box .schema .space .create (SETTINGS_SPACE_NAME , {
@@ -36,7 +31,7 @@ local function safe_mode_trigger(_, new, space, op)
3631 end
3732 if (op == ' INSERT' and new .status == vshard_consts .BUCKET .RECEIVING ) or
3833 (op == ' REPLACE' and new .status == vshard_consts .BUCKET .SENDING ) then
39- box .broadcast (' _crud.safe_mode_enable ' , true )
34+ box .broadcast (SAFE_MOD_ENABLE_EVENT , true )
4035 end
4136end
4237
8681
8782local function rebalance_init ()
8883 box .watch (' box.status' , function ()
89- if box .info .ro or box . space [ SETTINGS_SPACE_NAME ] == nil then
84+ if box .info .ro then
9085 return
9186 end
9287
93- local stored_safe_mode = box .space [SETTINGS_SPACE_NAME ]:get { ' safe_mode' }
88+ local stored_safe_mode
89+ if box .space [SETTINGS_SPACE_NAME ] == nil then
90+ create_space ()
91+ box .space [SETTINGS_SPACE_NAME ]:insert { ' safe_mode' , false }
92+ else
93+ stored_safe_mode = box .space [SETTINGS_SPACE_NAME ]:get { ' safe_mode' }
94+ end
9495 M .safe_mode = stored_safe_mode .value
9596
9697 if M .safe_mode then
@@ -105,36 +106,12 @@ local function rebalance_init()
105106 end
106107 end )
107108
108- box .watch (' _crud.safe_mode_enable ' , function (_ , do_enable )
109+ box .watch (SAFE_MOD_ENABLE_EVENT , function (_ , do_enable )
109110 if box .info .ro or not do_enable then
110111 return
111112 end
112113 safe_mode_enable ()
113114 end )
114-
115- if box .info .ro then
116- return
117- end
118-
119- local stored_safe_mode
120- if box .space [SETTINGS_SPACE_NAME ] == nil then
121- create_space ()
122- box .space [SETTINGS_SPACE_NAME ]:insert { ' safe_mode' , false }
123- else
124- stored_safe_mode = box .space [SETTINGS_SPACE_NAME ]:get { ' safe_mode' }
125- end
126- M .safe_mode = stored_safe_mode and stored_safe_mode .value or false
127-
128- if M .safe_mode then
129- for hook , _ in pairs (M .safe_mode_enable_hooks ) do
130- hook ()
131- end
132- else
133- box .space ._bucket :on_replace (safe_mode_trigger )
134- for hook , _ in pairs (M .safe_mode_disable_hooks ) do
135- hook ()
136- end
137- end
138115end
139116
140117local function rebalance_stop ()
0 commit comments