<commit_before>%% @author Couchbase <<EMAIL>>
%% @copyright 2012 Couchbase, Inc.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%      http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.

-module(cluster_compat_mode).

-include("ns_common.hrl").

-export([get_compat_version/0, is_enabled/1, is_enabled_at/2,
         is_cluster_20/0,
         force_compat_version/1, un_force_compat_version/0,
         consider_switching_compat_mode/0,
         is_index_aware_rebalance_on/0,
         is_index_pausing_on/0,
         rebalance_ignore_view_compactions/0,
         check_is_progress_tracking_supported/0]).

%% NOTE: this is rpc:call-ed by mb_master
-export([supported_compat_version/0, mb_master_advertised_version/0]).

-export([pre_force_compat_version/0, post_force_compat_version/0]).


get_compat_version() ->
    ns_config_ets_dup:unreliable_read_key(cluster_compat_version, undefined).

%% NOTE: this is rpc:call-ed by mb_master of 2.0.0
supported_compat_version() ->
    [2, 0].

%% NOTE: this is rpc:call-ed by mb_master of 2.0.1+
%%
%% I.e. we want later version to be able to take over mastership even
%% without requiring compat mode upgrade
mb_master_advertised_version() ->
    [2, 0, 2].

%% TODO: in 2.1 reimplement as compat_version 2.1 to make things
%% simpler. For 2.0.2 I don't want to prevent rolling downgrades yet.
check_is_progress_tracking_supported() ->
    is_cluster_20() andalso
        case rpc:multicall(ns_node_disco:nodes_wanted(), cluster_compat_mode, mb_master_advertised_version, [], 30000) of
            {Replies, []} ->
                [R || R <- Replies,
                      R < [2, 0, 2]] =:= [];
            _ ->
                false
        end.

is_enabled_at(undefined = _ClusterVersion, _FeatureVersion) ->
    false;
is_enabled_at(ClusterVersion, FeatureVersion) ->
    ClusterVersion >= FeatureVersion.

is_enabled(FeatureVersion) ->
    is_enabled_at(get_compat_version(), FeatureVersion).

is_cluster_20() ->
    is_enabled([2, 0]).

is_index_aware_rebalance_on() ->
    Disabled = ns_config_ets_dup:unreliable_read_key(index_aware_rebalance_disabled, false),
    (not Disabled) andalso is_enabled([2, 0]).

is_index_pausing_on() -><commit_msg>MB-100 Bump mb_master advertised version for 2.2.0<commit_after>%% @author Couchbase <<EMAIL>>
%% @copyright 2012 Couchbase, Inc.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%      http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.

-module(cluster_compat_mode).

-include("ns_common.hrl").

-export([get_compat_version/0, is_enabled/1, is_enabled_at/2,
         is_cluster_20/0,
         force_compat_version/1, un_force_compat_version/0,
         consider_switching_compat_mode/0,
         is_index_aware_rebalance_on/0,
         is_index_pausing_on/0,
         rebalance_ignore_view_compactions/0,
         check_is_progress_tracking_supported/0]).

%% NOTE: this is rpc:call-ed by mb_master
-export([supported_compat_version/0, mb_master_advertised_version/0]).

-export([pre_force_compat_version/0, post_force_compat_version/0]).


get_compat_version() ->
    ns_config_ets_dup:unreliable_read_key(cluster_compat_version, undefined).

%% NOTE: this is rpc:call-ed by mb_master of 2.0.0
supported_compat_version() ->
    [2, 0].

%% NOTE: this is rpc:call-ed by mb_master of 2.0.1+
%%
%% I.e. we want later version to be able to take over mastership even
%% without requiring compat mode upgrade
mb_master_advertised_version() ->
    [2, 2, 0].

%% TODO: in 2.1 reimplement as compat_version 2.1 to make things
%% simpler. For 2.0.2 I don't want to prevent rolling downgrades yet.
check_is_progress_tracking_supported() ->
    is_cluster_20() andalso
        case rpc:multicall(ns_node_disco:nodes_wanted(), cluster_compat_mode, mb_master_advertised_version, [], 30000) of
            {Replies, []} ->
                [R || R <- Replies,
                      R < [2, 0, 2]] =:= [];
            _ ->
                false
        end.

is_enabled_at(undefined = _ClusterVersion, _FeatureVersion) ->
    false;
is_enabled_at(ClusterVersion, FeatureVersion) ->
    ClusterVersion >= FeatureVersion.

is_enabled(FeatureVersion) ->
    is_enabled_at(get_compat_version(), FeatureVersion).

is_cluster_20() ->
    is_enabled([2, 0]).

is_index_aware_rebalance_on() ->
    Disabled = ns_config_ets_dup:unreliable_read_key(index_aware_rebalance_disabled, false),
    (not Disabled) andalso is_enabled([2, 0]).

is_index_pausing_on() ->
{'fraction_non_alphanumeric': 0.10840666541846096, 'fraction_numerical': 0.023216626099981277, 'mean_word_length': 3.7953321364452424, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6483033', 'n_tokens_mistral': 1772, 'n_tokens_neox': 1689, 'n_words': 440}