<commit_before>-module(compaction_daemon).

-behaviour(gen_fsm).

-include("ns_common.hrl").
-include("couch_db.hrl").

%% API
-export([start_link/0,
         inhibit_view_compaction/3, uninhibit_view_compaction/3,
         force_compact_bucket/1, force_compact_db_files/1, force_compact_view/2,
         force_purge_compact_bucket/1,
         cancel_forced_bucket_compaction/1, cancel_forced_db_compaction/1,
         cancel_forced_view_compaction/2,
         set_global_purge_interval/1,
         get_purge_interval/1]).

%% gen_fsm callbacks
-export([init/1, handle_event/3, handle_sync_event/4, handle_info/3,
         code_change/4, terminate/3]).

-record(state, {buckets_to_compact :: [binary()],
                compactor_pid,
                compactor_config,
                compaction_start_ts,
                scheduled_compaction_tref,

                %% bucket for which view compaction is inhibited (note
                %% it's also set when we're running 'uninhibit' views
                %% compaction as 'final stage')
                view_compaction_inhibited_bucket :: undefined | binary(),
                %% if view compaction is inhibited this field will
                %% hold monitor ref of the guy who requested it. If
                %% that guy dies, inhibition will be canceled
                view_compaction_inhibited_ref :: (undefined | reference()),
                %% when we have 'uninhibit' request, we're starting
                %% our compaction with 'forced' view compaction asap
                %%
                %% requested indicates we have this request
                view_compaction_uninhibit_requested = false :: boolean(),
                %% started indicates we have started such compaction
                %% and waiting for it to complete
                view_compaction_uninhibit_started = false :: boolean(),
                %% this is From to reply to when such compaction is
                %% done
                view_compaction_uninhibit_requested_waiter,

                %% mapping from compactor pid to #forced_compaction{}
                running_forced_compactions,
                %% reverse mapping from #forced_compaction{} to compactor pid
                forced_compaction_pids}).

-record(config, {db_fragmentation,
                 view_fragmentation,
                 allowed_period,
                 parallel_view_compact = false,
                 do_purge = false,
                 daemon}).

-record(daemon_config, {check_interval,
                        min_file_size}).

-record(period, {from_hour,
                 from_minute,
                 to_hour,
                 to_minute,
                 abort_outside}).

-record(forced_compaction, {type :: bucket | bucket_purge | db | view,
                            name :: binary()}).

% If N vbucket databases of a bucket need to be compacted, we trigger compaction
% for all the vbucket databases of that bucket.
-define(NUM_SAMPLE_VBUCKETS, 16).

-define(RPC_TIMEOUT, 10000).


%% API
start_link() ->
    gen_fsm:start_link({local, ?MODULE}, ?MODULE, [], []).

-define(DEFAULT_DELETIONS_PURGE_INTERVAL, 7).

-spec get_purge_interval(global | binary()) -> integer().
get_purge_interval(BucketName) ->
    BucketConfig =
        case BucketName of
            global ->
                [];
            _ ->
                case ns_bucket:get_bucket(binary_to_list(BucketName)) of
                    not_present ->
                        [];
                    {ok, X} -> X
                end
        end,
    RawPurgeInterval = proplists:get_value(purge_interval, BucketConfig),
    UseGlobal = RawPurgeInterval =:= undefined
        orelse case proplists:get_value(autocompaction, BucketConfig) of
                   false -> true;
                   undefined -> true;<commit_msg>MB-8662: changed default deletions purge interval to 3<commit_after>-module(compaction_daemon).

-behaviour(gen_fsm).

-include("ns_common.hrl").
-include("couch_db.hrl").

%% API
-export([start_link/0,
         inhibit_view_compaction/3, uninhibit_view_compaction/3,
         force_compact_bucket/1, force_compact_db_files/1, force_compact_view/2,
         force_purge_compact_bucket/1,
         cancel_forced_bucket_compaction/1, cancel_forced_db_compaction/1,
         cancel_forced_view_compaction/2,
         set_global_purge_interval/1,
         get_purge_interval/1]).

%% gen_fsm callbacks
-export([init/1, handle_event/3, handle_sync_event/4, handle_info/3,
         code_change/4, terminate/3]).

-record(state, {buckets_to_compact :: [binary()],
                compactor_pid,
                compactor_config,
                compaction_start_ts,
                scheduled_compaction_tref,

                %% bucket for which view compaction is inhibited (note
                %% it's also set when we're running 'uninhibit' views
                %% compaction as 'final stage')
                view_compaction_inhibited_bucket :: undefined | binary(),
                %% if view compaction is inhibited this field will
                %% hold monitor ref of the guy who requested it. If
                %% that guy dies, inhibition will be canceled
                view_compaction_inhibited_ref :: (undefined | reference()),
                %% when we have 'uninhibit' request, we're starting
                %% our compaction with 'forced' view compaction asap
                %%
                %% requested indicates we have this request
                view_compaction_uninhibit_requested = false :: boolean(),
                %% started indicates we have started such compaction
                %% and waiting for it to complete
                view_compaction_uninhibit_started = false :: boolean(),
                %% this is From to reply to when such compaction is
                %% done
                view_compaction_uninhibit_requested_waiter,

                %% mapping from compactor pid to #forced_compaction{}
                running_forced_compactions,
                %% reverse mapping from #forced_compaction{} to compactor pid
                forced_compaction_pids}).

-record(config, {db_fragmentation,
                 view_fragmentation,
                 allowed_period,
                 parallel_view_compact = false,
                 do_purge = false,
                 daemon}).

-record(daemon_config, {check_interval,
                        min_file_size}).

-record(period, {from_hour,
                 from_minute,
                 to_hour,
                 to_minute,
                 abort_outside}).

-record(forced_compaction, {type :: bucket | bucket_purge | db | view,
                            name :: binary()}).

% If N vbucket databases of a bucket need to be compacted, we trigger compaction
% for all the vbucket databases of that bucket.
-define(NUM_SAMPLE_VBUCKETS, 16).

-define(RPC_TIMEOUT, 10000).


%% API
start_link() ->
    gen_fsm:start_link({local, ?MODULE}, ?MODULE, [], []).

-define(DEFAULT_DELETIONS_PURGE_INTERVAL, 3).

-spec get_purge_interval(global | binary()) -> integer().
get_purge_interval(BucketName) ->
    BucketConfig =
        case BucketName of
            global ->
                [];
            _ ->
                case ns_bucket:get_bucket(binary_to_list(BucketName)) of
                    not_present ->
                        [];
                    {ok, X} -> X
                end
        end,
    RawPurgeInterval = proplists:get_value(purge_interval, BucketConfig),
    UseGlobal = RawPurgeInterval =:= undefined
        orelse case proplists:get_value(autocompaction, BucketConfig) of
                   false -> true;
                   undefined -> true;
{'fraction_non_alphanumeric': 0.08486268384745542, 'fraction_numerical': 0.00741897696212417, 'mean_word_length': 1.9474491752972765, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6927936', 'n_tokens_mistral': 2227, 'n_tokens_neox': 2034, 'n_words': 490}