/
cluster_compat_mode.erl
375 lines (322 loc) · 12.8 KB
/
cluster_compat_mode.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
%% @author Couchbase <info@couchbase.com>
%% @copyright 2012-Present Couchbase, Inc.
%%
%% Use of this software is governed by the Business Source License included
%% in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
%% in that file, in accordance with the Business Source License, use of this
%% software will be governed by the Apache License, Version 2.0, included in
%% the file licenses/APL2.txt.
-module(cluster_compat_mode).
-include("ns_common.hrl").
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-endif.
-export([get_compat_version/0,
get_compat_version/1,
get_ns_config_compat_version/0,
is_enabled/1, is_enabled_at/2,
consider_switching_compat_mode/0,
is_index_aware_rebalance_on/0,
is_index_pausing_on/0,
rebalance_ignore_view_compactions/0,
is_cluster_66/0,
is_cluster_66/1,
is_version_66/1,
is_cluster_70/0,
is_cluster_70/1,
is_version_70/1,
is_cluster_NEO/0,
is_cluster_NEO/1,
is_version_NEO/1,
is_enterprise/0,
is_enterprise/1,
should_enforce_limits/0,
should_enforce_limits/1,
is_saslauthd_enabled/0,
is_cbas_enabled/0,
supported_compat_version/0,
min_supported_compat_version/0,
effective_cluster_compat_version/0,
effective_cluster_compat_version_for/1,
is_developer_preview/0,
is_developer_preview/1,
get_cluster_capabilities/1,
tls_supported/0,
tls_supported/1,
preserve_durable_mutations/0]).
%% NOTE: this is rpc:call-ed by mb_master
-export([mb_master_advertised_version/0]).
n1ql_cluster_capabilities(?VERSION_65, true) ->
[costBasedOptimizer, indexAdvisor, javaScriptFunctions, inlineFunctions |
n1ql_cluster_capabilities(?VERSION_65, false)];
n1ql_cluster_capabilities(?VERSION_65, false) ->
[enhancedPreparedStatements].
cluster_capabilities(_Version, IsDP) ->
[{n1ql, n1ql_cluster_capabilities(?VERSION_65, IsDP)}].
get_cluster_capabilities(Config) ->
cluster_capabilities(get_compat_version(Config),
is_developer_preview(Config)).
get_compat_version() ->
get_compat_version(ns_config:latest()).
get_ns_config_compat_version() ->
get_ns_config_compat_version(ns_config:latest()).
get_ns_config_compat_version(Config) ->
ns_config:search(Config, cluster_compat_version, undefined).
get_compat_version(Config) ->
case get_ns_config_compat_version(Config) of
V when V =:= undefined orelse V < ?VERSION_NEO ->
V;
_ ->
chronicle_compat:get(Config, cluster_compat_version,
#{default => ?VERSION_70})
end.
supported_compat_version() ->
case get_pretend_version() of
undefined ->
?LATEST_VERSION_NUM;
Version ->
Version
end.
min_supported_compat_version() ->
?VERSION_65.
%% NOTE: this is rpc:call-ed by mb_master
%%
%% I.e. we want later version to be able to take over mastership even
%% without requiring compat mode upgrade
mb_master_advertised_version() ->
case get_pretend_version() of
undefined ->
?MASTER_ADVERTISED_VERSION;
Version ->
Version ++ [0]
end.
is_enabled_at(undefined = _ClusterVersion, _FeatureVersion) ->
false;
is_enabled_at(ClusterVersion, FeatureVersion) ->
ClusterVersion >= FeatureVersion.
is_enabled(FeatureVersion) ->
is_enabled(ns_config:latest(), FeatureVersion).
is_enabled(Config, FeatureVersion) ->
CompatVersion =
case FeatureVersion > ?VERSION_70 of
true ->
get_compat_version(Config);
false ->
get_ns_config_compat_version(Config)
end,
is_enabled_at(CompatVersion, FeatureVersion).
is_version_66(ClusterVersion) ->
is_enabled_at(ClusterVersion, ?VERSION_66).
is_cluster_66() ->
is_cluster_66(ns_config:latest()).
is_cluster_66(Config) ->
is_enabled(Config, ?VERSION_66).
is_version_70(ClusterVersion) ->
is_enabled_at(ClusterVersion, ?VERSION_70).
is_cluster_70() ->
is_cluster_70(ns_config:latest()).
is_cluster_70(Config) ->
is_enabled(Config, ?VERSION_70).
is_version_NEO(ClusterVersion) ->
is_enabled_at(ClusterVersion, ?VERSION_NEO).
is_cluster_NEO() ->
is_cluster_NEO(ns_config:latest()).
is_cluster_NEO(Config) ->
is_enabled(Config, ?VERSION_NEO).
should_enforce_limits(Snapshot) ->
case maps:find(cluster_compat_version, Snapshot) of
{ok, {Version, _}} ->
ns_config:read_key_fast(enforce_limits, false) andalso
is_enabled_at(Version, ?VERSION_NEO);
error ->
false
end.
should_enforce_limits() ->
ns_config:read_key_fast(enforce_limits, false) andalso
is_cluster_NEO().
is_index_aware_rebalance_on() ->
not ns_config:read_key_fast(index_aware_rebalance_disabled, false).
is_index_pausing_on() ->
is_index_aware_rebalance_on() andalso
(not ns_config:read_key_fast(index_pausing_disabled, false)).
is_enterprise(Config) ->
ns_config:search(Config, {node, node(), is_enterprise}, false).
is_enterprise() ->
ns_config:read_key_fast({node, node(), is_enterprise}, false).
is_saslauthd_enabled() ->
is_enterprise() andalso
ns_config:search(ns_config:latest(),
{node, node(), saslauthd_enabled}, false).
is_cbas_enabled() ->
is_enterprise().
rebalance_ignore_view_compactions() ->
ns_config:read_key_fast(rebalance_ignore_view_compactions, false).
%% This function is used to determine if developer preview is enabled by
%% default. This is normally enabled (made true) during development of the
%% release and then disabled (made false) near release time.
is_developer_preview_enabled_by_default() ->
false.
consider_switching_compat_mode() ->
Config = ns_config:get(),
CurrentVersion = get_compat_version(Config),
case CurrentVersion =:= supported_compat_version() of
true ->
case is_developer_preview() of
false ->
%% Specifying the environment variable takes precedence.
Default = misc:get_env_default(
developer_preview_enabled_default,
is_developer_preview_enabled_by_default()),
case Default of
true ->
ns_config:set(developer_preview_enabled, Default);
_ ->
ok
end;
true ->
ok
end,
ok;
false ->
do_consider_switching_compat_mode(Config, CurrentVersion)
end.
upgrades() ->
[{?VERSION_66, rbac, menelaus_users, upgrade},
{?VERSION_70, rbac, menelaus_users, upgrade},
{?VERSION_NEO, rbac, menelaus_users, upgrade}].
do_upgrades(undefined, _, _, _) ->
%% this happens during the cluster initialization. no upgrade needed
ok;
do_upgrades(CurrentVersion, NewVersion, Config, NodesWanted) ->
do_upgrades(upgrades(), CurrentVersion, NewVersion, Config, NodesWanted).
do_upgrades([], _, _, _, _) ->
ok;
do_upgrades([{Version, Name, Module, Fun} | Rest],
CurrentVersion, NewVersion, Config, NodesWanted)
when CurrentVersion < Version andalso NewVersion >= Version ->
?log_debug("Initiating ~p upgrade due to version change from ~p to ~p",
[Name, CurrentVersion, NewVersion]),
case Module:Fun(Version, Config, NodesWanted) of
ok ->
do_upgrades(Rest, CurrentVersion, NewVersion, Config, NodesWanted);
_ ->
Name
end;
do_upgrades([_ | Rest], CurrentVersion, NewVersion, Config, NodesWanted) ->
do_upgrades(Rest, CurrentVersion, NewVersion, Config, NodesWanted).
do_consider_switching_compat_mode(Config, CurrentVersion) ->
NodesWanted = ns_node_disco:nodes_wanted(Config),
NodesUp = lists:sort([node() | nodes()]),
case ordsets:is_subset(NodesWanted, NodesUp) of
true ->
NodeInfos = ns_doctor:get_nodes(),
case consider_switching_compat_mode_loop(
NodeInfos, NodesWanted, supported_compat_version()) of
CurrentVersion ->
ok;
AnotherVersion ->
case is_enabled_at(AnotherVersion, CurrentVersion) of
true ->
case do_upgrades(CurrentVersion, AnotherVersion,
Config, NodesWanted) of
ok ->
do_switch_compat_mode(AnotherVersion,
NodesWanted),
changed;
Name ->
?log_error(
"Refusing to upgrade the compat "
"version from ~p to ~p due to failure "
"of ~p upgrade"
"~nNodesWanted: ~p~nNodeInfos: ~p",
[CurrentVersion, AnotherVersion, Name,
NodesWanted, NodeInfos])
end;
false ->
?log_error("Refusing to downgrade the compat "
"version from ~p to ~p."
"~nNodesWanted: ~p~nNodeInfos: ~p",
[CurrentVersion, AnotherVersion,
NodesWanted, NodeInfos]),
ok
end
end;
false ->
ok
end.
do_switch_compat_mode(NewVersion, NodesWanted) ->
case upgrade_ns_config(NewVersion, NodesWanted) of
ok ->
chronicle_upgrade:upgrade(NewVersion, NodesWanted);
error ->
error
end.
upgrade_ns_config(NewVersion, NodesWanted) ->
ns_online_config_upgrader:upgrade_config(NewVersion),
try
case ns_config_rep:ensure_config_seen_by_nodes(NodesWanted) of
ok -> ok;
{error, BadNodes} ->
ale:error(?USER_LOGGER,
"Was unable to sync cluster_compat_version update "
"to some nodes: ~p", [BadNodes]),
error
end
catch T:E:S ->
ale:error(?USER_LOGGER,
"Got problems trying to replicate cluster_compat_version "
"update~n~p", [{T, E, S}]),
error
end.
consider_switching_compat_mode_loop(_NodeInfos, _NodesWanted,
_Version = undefined) ->
undefined;
consider_switching_compat_mode_loop(_NodeInfos, [], Version) ->
Version;
consider_switching_compat_mode_loop(NodeInfos, [Node | RestNodesWanted],
Version) ->
case dict:find(Node, NodeInfos) of
{ok, Info} ->
NodeVersion = proplists:get_value(supported_compat_version, Info,
undefined),
AgreedVersion = case is_enabled_at(NodeVersion, Version) of
true ->
Version;
false ->
NodeVersion
end,
consider_switching_compat_mode_loop(NodeInfos, RestNodesWanted,
AgreedVersion);
_ ->
undefined
end.
%% undefined is "used" shortly after node is initialized and when
%% there's no compat mode yet
effective_cluster_compat_version_for(undefined) ->
1;
effective_cluster_compat_version_for([VersionMaj, VersionMin] =
_CompatVersion) ->
VersionMaj * 16#10000 + VersionMin.
effective_cluster_compat_version() ->
effective_cluster_compat_version_for(get_compat_version()).
get_pretend_version() ->
case application:get_env(ns_server, pretend_version) of
undefined ->
undefined;
{ok, VersionString} ->
{[A, B | _], _, _} = misc:parse_version(VersionString),
[A, B]
end.
is_developer_preview() -> is_developer_preview(ns_config:latest()).
is_developer_preview(Config) ->
ns_config:search(Config, developer_preview_enabled, false).
tls_supported(Config) ->
is_enterprise(Config).
tls_supported() ->
is_enterprise().
-ifdef(TEST).
mb_master_advertised_version_test() ->
true = mb_master_advertised_version() >= ?LATEST_VERSION_NUM ++ [0].
-endif.
preserve_durable_mutations() ->
ns_config:read_key_fast({failover, preserve_durable_mutations}, true).