anchor.diff:  -undef(EVENT_STATE).
   -undef(EVENT_INCREASE).
   -undef(EVENT_DECREASE).
  --define(EVENT_RUN,      'run').
  +-define(EVENT_RUN, 'run').
   -define(EVENT_DIAGNOSE, 'diagnose').
  --define(EVENT_LOCK,     'lock').
  --define(EVENT_SUSPEND,  'suspend').
  --define(EVENT_RESUME,   'resume').
  --define(EVENT_FINISH,   'finish').
  --define(EVENT_STATE,    'state').
  --define(EVENT_INCREASE,  'increase').
  --define(EVENT_DECREASE,  'decrease').
  --type(compaction_event() ::?EVENT_RUN      |
  -                           ?EVENT_DIAGNOSE |
  -                           ?EVENT_LOCK     |
  -                           ?EVENT_SUSPEND  |
  -                           ?EVENT_RESUME   |
  -                           ?EVENT_FINISH   |
  -                           ?EVENT_STATE    |
  -                           ?EVENT_INCREASE |
  -                           ?EVENT_DECREASE
  -                           ).
  +-define(EVENT_LOCK, 'lock').
  +-define(EVENT_SUSPEND, 'suspend').
  +-define(EVENT_RESUME, 'resume').
  +-define(EVENT_FINISH, 'finish').
  +-define(EVENT_STATE, 'state').
  +-define(EVENT_INCREASE, 'increase').
  +-define(EVENT_DECREASE, 'decrease').
  +-type(compaction_event() ::?EVENT_RUN |
  +?EVENT_DIAGNOSE |
  +?EVENT_LOCK |
  +?EVENT_SUSPEND |
  +?EVENT_RESUME |
  +?EVENT_FINISH |
  +?EVENT_STATE |
  +?EVENT_INCREASE |
  +?EVENT_DECREASE
  +).
   
   %% @doc Compaction related definitions
   -define(RET_SUCCESS, 'success').
   -define(RET_FAIL,    'fail').
   -type(compaction_ret() :: ?RET_SUCCESS |
  -                          ?RET_FAIL |
  -                          undefined).
  +?RET_FAIL |
  +undefined).
   
   -define(MAX_LEN_HIST, 50).
   -define(MAX_RETRY_TIMES, 2).
   -define(WAIT_TIME_AFTER_ERROR, 200). %% 200ms
   
   -record(compaction_report, {
  -          file_path = [] :: string(),
  -          avs_ver = <<>> :: binary(),
  -          num_of_active_objs  = 0 :: non_neg_integer(),
  -          size_of_active_objs = 0 :: non_neg_integer(),
  -          total_num_of_objs   = 0 :: non_neg_integer(),
  -          total_size_of_objs  = 0 :: non_neg_integer(),
  -          start_datetime = [] :: string(),
  -          end_datetime   = [] :: string(),
  -          errors = []  :: [{non_neg_integer(),non_neg_integer()}],
  -          duration = 0 :: non_neg_integer(),
  -          result :: atom()
  -         }).
  +file_path = [] :: string(),
  +avs_ver = <<>> :: binary(),
  +num_of_active_objs = 0 :: non_neg_integer(),
  +size_of_active_objs = 0 :: non_neg_integer(),
  +total_num_of_objs = 0 :: non_neg_integer(),
  +total_size_of_objs = 0 :: non_neg_integer(),
  +start_datetime = [] :: string(),
  +end_datetime = [] :: string(),
  +errors = [] :: [{non_neg_integer(),non_neg_integer()}],
  +duration = 0 :: non_neg_integer(),
  +result :: atom()
  +}).
   
   -record(compaction_hist, {
  -          start_datetime = 0 :: non_neg_integer(),
  -          end_datetime   = 0 :: non_neg_integer(),
  -          duration = 0 :: non_neg_integer(),
  -          result :: compaction_ret()
  -         }).
  +start_datetime = 0 :: non_neg_integer(),
  +end_datetime   = 0 :: non_neg_integer(),
  +duration = 0 :: non_neg_integer(),
  +result :: compaction_ret()
  +}).
   
   -record(compaction_stats, {
  -          status = ?ST_IDLING :: compaction_state(),
  -          total_num_of_targets    = 0  :: non_neg_integer(),
  -          num_of_reserved_targets = 0  :: non_neg_integer(),
  -          num_of_pending_targets  = 0  :: non_neg_integer(),
  -          num_of_ongoing_targets  = 0  :: non_neg_integer(),
  -          reserved_targets = []        :: [atom()],
  -          pending_targets  = []        :: [atom()],
  -          ongoing_targets  = []        :: [atom()],
  -          locked_targets   = []        :: [atom()],
  -          latest_exec_datetime = 0     :: non_neg_integer(),
  -          acc_reports = []             :: [#compaction_report{}]
  -         }).
  +status = ?ST_IDLING :: compaction_state(),
  +total_num_of_targets = 0 :: non_neg_integer(),
  +num_of_reserved_targets = 0 :: non_neg_integer(),
  +num_of_pending_targets = 0 :: non_neg_integer(),
  +num_of_ongoing_targets = 0 :: non_neg_integer(),
  +reserved_targets = [] :: [atom()],
  +pending_targets = [] :: [atom()],
  +ongoing_targets = [] :: [atom()],
  +locked_targets = [] :: [atom()],
  +latest_exec_datetime = 0 :: non_neg_integer(),
  +acc_reports = [] :: [#compaction_report{}]
  +}).
   
   
   %% Error Constants
   %%
  --define(ERROR_FD_CLOSED,                "already closed file-descriptor").
  --define(ERROR_FILE_OPEN,                "file open error").
  --define(ERROR_INVALID_DATA,             "invalid data").
  --define(ERROR_DATA_SIZE_DID_NOT_MATCH,  "data-size did not match").
  --define(ERROR_COMPACT_SUSPEND_FAILURE,  "comaction-suspend filure").
  --define(ERROR_COMPACT_RESUME_FAILURE,   "comaction-resume filure").
  --define(ERROR_PROCESS_NOT_FOUND,        "server process not found").
  +-define(ERROR_FD_CLOSED, "already closed file-descriptor").
  +-define(ERROR_FILE_OPEN, "file open error").
  +-define(ERROR_INVALID_DATA, "invalid data").
  +-define(ERROR_DATA_SIZE_DID_NOT_MATCH, "data-size did not match").
  +-define(ERROR_COMPACT_SUSPEND_FAILURE, "comaction-suspend filure").
  +-define(ERROR_COMPACT_RESUME_FAILURE, "comaction-resume filure").
  +-define(ERROR_PROCESS_NOT_FOUND, "server process not found").
   -define(ERROR_COULD_NOT_GET_MOUNT_PATH, "could not get mout path").
  --define(ERROR_LOCKED_CONTAINER,         "locked obj-conatainer").
  --define(ERROR_NOT_ALLOWED_ACCESS,       "not allowed access").
  --define(ERROR_COULD_NOT_START_WORKER,   "could NOT start worker processes").
  --define(ERROR_FREESPACE_LT_AVS,   "The disk free space is less than the size of the AVS file").
  +-define(ERROR_LOCKED_CONTAINER, "locked obj-conatainer").
  +-define(ERROR_NOT_ALLOWED_ACCESS, "not allowed access").
  +-define(ERROR_COULD_NOT_START_WORKER, "could NOT start worker processes").
  +-define(ERROR_FREESPACE_LT_AVS, "The disk free space is less than the size of the AVS file").
   
   -define(ERROR_MSG_SLOW_OPERATION, 'slow_operation').
  --define(ERROR_MSG_TIMEOUT,        'timeout').
  +-define(ERROR_MSG_TIMEOUT, 'timeout').
   -define(MSG_ITEM_SLOW_OP, 'slow_operation').
   -define(MSG_ITEM_TIMEOUT, 'timeout').
   
  
positive.code: -define(SYNC_MODE_PERIODIC, 'periodic').
  -define(SYNC_MODE_WRITETHROUGH, 'writethrough').
  -type(sync_mode() :: ?SYNC_MODE_NONE |
                       ?SYNC_MODE_PERIODIC |
                       ?SYNC_MODE_WRITETHROUGH).
  -define(OBJ_PRV_READ_WRITE, 'read_and_write').
  -define(OBJ_PRV_READ_ONLY, 'read').
  -define(OBJ_PRV_WRITE_ONLY, 'write').
  -type(obj_privilege() :: ?OBJ_PRV_READ_WRITE |
                           ?OBJ_PRV_READ_ONLY |
                           ?OBJ_PRV_WRITE_ONLY).
  -record(obj_server_state, {
            id :: atom(),
            seq_num = 0 :: non_neg_integer(),
            privilege = ?OBJ_PRV_READ_WRITE :: obj_privilege(),
            meta_db_id :: atom(),
            compaction_worker_id :: atom(),
            diagnosis_logger_id :: atom(),
            root_path = [] :: string(),
            object_storage = #backend_info{}  :: #backend_info{},
            storage_stats  = #storage_stats{} :: #storage_stats{},
            state_filepath :: string(),
            sync_mode = ?SYNC_MODE_NONE :: sync_mode(),
            sync_interval_in_ms = ?DEF_SYNC_INTERVAL :: pos_integer(),
            is_strict_check = false :: boolean(),
            is_locked = false :: boolean(),
            is_del_blocked = false  :: boolean(),
            threshold_slow_processing = ?DEF_THRESHOLD_SLOW_PROC :: non_neg_integer(),
            is_able_to_write = true :: boolean()
           }).
  
  %% apllication-env
  -define(env_metadata_db(),
          case application:get_env(?APP_NAME, metadata_storage) of
              {ok, EnvMetadataDB} ->
                  EnvMetadataDB;
              _ ->
                  ?DEF_METADATA_DB
          end).
  
  -define(env_sync_mode(),
          case application:get_env(?APP_NAME, sync_mode) of
              {ok, SyncMode} ->
                  SyncMode;
              _ ->
                  ?SYNC_MODE_NONE
          end).
  
  -define(env_sync_interval(),
          case application:get_env(?APP_NAME, sync_interval_in_ms) of
              {ok, SyncInterval} ->
                  SyncInterval;
              _ ->
                  ?DEF_SYNC_INTERVAL
          end).
  
  -ifdef(TEST).
  -define(env_strict_check(), true).
  -else.
  -define(env_strict_check(),
          case application:get_env(?APP_NAME, is_strict_check) of
              {ok, EnvStrictCheck} ->
                  EnvStrictCheck;
              _ ->
                  false
          end).
  -endif.
  
  -define(env_enable_diagnosis_log(),
          case application:get_env(?APP_NAME,
                                   is_enable_diagnosis_log) of
              {ok, true} ->
                  true;
              _ ->
                  false
          end).
  
  -define(env_num_of_obj_storage_read_procs(),
          case application:get_env(?APP_NAME,
                                   num_of_obj_storage_read_procs) of
              {ok, EnvNumOfObjStorageReadProcs} ->
                  EnvNumOfObjStorageReadProcs;
              _ ->
                  ?DEF_NUM_OF_OBJ_STORAGE_READ_PROCS
          end).
  
  -define(get_obj_storage_read_proc(_PidL,_AddrId),
          begin
              lists:nth((_AddrId rem erlang:length(_PidL)) + 1,_PidL)
          end).
  
  
  -define(env_limit_num_of_compaction_procs(),
          case application:get_env(?APP_NAME,
                                   limit_num_of_compaction_procs) of
              {ok, EnvLimitCompactionProcs} when is_integer(EnvLimitCompactionProcs) ->
                  EnvLimitCompactionProcs;
              _ ->
                  ?DEF_LIMIT_COMPACTION_PROCS
          end).
  
  -define(env_threshold_slow_processing(),
          case application:get_env(?APP_NAME,
                                   threshold_slow_processing) of
              {ok, EnvThresholdSlowProc} when is_integer(EnvThresholdSlowProc) ->
                  EnvThresholdSlowProc;
              _ ->
                  ?DEF_THRESHOLD_SLOW_PROC
          end).
  
  %% [Interval between batch processes]
  -define(env_compaction_interval_reg(),
          case application:get_env(?APP_NAME,
                                   compaction_waiting_time_regular) of
              {ok, EnvRegCompactionWT} when is_integer(EnvRegCompactionWT) ->
                  EnvRegCompactionWT;
              _ ->
                  ?DEF_REG_COMPACTION_WT
          end).
  
  -define(env_compaction_interval_max(),
          case application:get_env(?APP_NAME,
                                   compaction_waiting_time_max) of
              {ok, EnvMaxCompactionWT} when is_integer(EnvMaxCompactionWT) ->
                  EnvMaxCompactionWT;
              _ ->
                  ?DEF_MAX_COMPACTION_WT
          end).
  
  %% [Number of batch processes]
  -define(env_compaction_num_of_batch_procs_max(),
          case application:get_env(?APP_NAME,
                                   batch_procs_max) of
              {ok, EnvMaxCompactionBP} when is_integer(EnvMaxCompactionBP) ->
                  EnvMaxCompactionBP;
              _ ->
                  ?DEF_MAX_COMPACTION_BP
          end).
  
  -define(env_compaction_num_of_batch_procs_reg(),
          case application:get_env(?APP_NAME,
                                   batch_procs_regular) of
              {ok, EnvRegCompactionBP} when is_integer(EnvRegCompactionBP) ->
                  EnvRegCompactionBP;
              _ ->
                  ?DEF_REG_COMPACTION_BP
          end).
  
  -define(env_compaction_skip_prefetch_size(),
          case application:get_env(?APP_NAME,
                                   skip_prefetch_size) of
              {ok, EnvPrefetchSize} when is_integer(EnvPrefetchSize) ->
                  EnvPrefetchSize;
              _ ->
                  ?DEF_COMPACTION_SKIP_PS
          end).
  
  -define(num_of_compaction_concurrency(_PrmNumOfConcurrency),
          begin
              _EnvNumOfConcurrency = ?env_limit_num_of_compaction_procs(),
              case (_PrmNumOfConcurrency > _EnvNumOfConcurrency) of
                  true  -> _EnvNumOfConcurrency;
                  false -> _PrmNumOfConcurrency
              end
          end).
  
  -define(env_compaction_force_quit_in_bytes(),
          case application:get_env(?APP_NAME, force_quit_in_bytes) of
              {ok, ForceQuitInBytes} ->
                  ForceQuitInBytes;
              _ ->
                  ?DEF_COMPACTION_FQ_IN_BYTES
          end).
  
  -define(get_object_storage_id(_TargetContainers),
          begin
              lists:flatten(
                lists:map(
                  fun(_Id) ->
                          case leo_object_storage_api:get_object_storage_pid_by_container_id(_Id) of
                              not_found -> [];
                              _Pid -> _Pid
                          end
                  end,_TargetContainers))
          end).
  
  -define(env_diskspace_check_intervals(),
          case application:get_env(?APP_NAME, diskspace_check_intervals) of
              {ok, EnvDiskSpaceCheckIntervals} ->
                  EnvDiskSpaceCheckIntervals;
              _ ->
                  timer:minutes(1)
          end).
  
  -define(env_enable_msg_collector(),
          case application:get_env(?APP_NAME, msg_collector) of
              {ok, true} ->
                  true;
              _ ->
                  false
          end).
  
  %% custom-metadata's items for MDC-replication:
  -define(PROP_CMETA_CLUSTER_ID, 'cluster_id').
  
negative.code: -define(DEF_THRESHOLD_SLOW_PROC, 1000).
  
  -ifdef(TEST).
  -define(DEF_MIN_COMPACTION_WT, 10).  %% 10msec
  -define(DEF_REG_COMPACTION_WT, 100). %% 100msec
  -define(DEF_MAX_COMPACTION_WT, 300). %% 300msec
  -define(DEF_COMPACTION_TIMEOUT, timer:minutes(1)). %% 1min
  -else.
  -define(DEF_MIN_COMPACTION_WT,  300). %% 300msec
  