diff --git a/src/leo_storage_api.erl b/src/leo_storage_api.erl index 702c2a4..633c0a9 100644 --- a/src/leo_storage_api.erl +++ b/src/leo_storage_api.erl @@ -222,33 +222,52 @@ synchronize(Key, ErrorType) -> %% -spec(compact(atom(), 'all' | integer(), integer()) -> ok | {error, any()}). compact(start, NumOfTargets, MaxProc) -> - TargetPids1 = - case leo_compaction_manager_fsm:status() of - {ok, #compaction_stats{status = Status, - pending_targets = PendingTargets}} when Status == ?COMPACTION_STATUS_SUSPEND; - Status == ?COMPACTION_STATUS_IDLE -> - PendingTargets; - _ -> - [] - end, + case leo_redundant_manager_api:get_member_by_node(erlang:node()) of + {ok, #member{state = ?STATE_RUNNING}} -> + TargetPids1 = + case leo_compaction_manager_fsm:status() of + {ok, #compaction_stats{status = Status, + pending_targets = PendingTargets}} + when Status == ?COMPACTION_STATUS_SUSPEND; + Status == ?COMPACTION_STATUS_IDLE -> + PendingTargets; + _ -> + [] + end, - case TargetPids1 of - [] -> - {error, "Not exists compaction-targets"}; + case TargetPids1 of + [] -> + {error, "Not exists compaction-targets"}; + _ -> + TargetPids2 = + case NumOfTargets of + 'all' -> + TargetPids1; + _Other -> + lists:sublist(TargetPids1, NumOfTargets) + end, + leo_compaction_manager_fsm:start( + TargetPids2, MaxProc, + fun leo_redundant_manager_api:has_charge_of_node/2) + end; _ -> - TargetPids2 = case NumOfTargets of - 'all' -> TargetPids1; - _Other -> lists:sublist(TargetPids1, NumOfTargets) - end, - leo_compaction_manager_fsm:start( - TargetPids2, MaxProc, fun leo_redundant_manager_api:has_charge_of_node/2) + {error,'not_running'} end. -compact(suspend) -> +compact(Method) -> + case leo_redundant_manager_api:get_member_by_node(erlang:node()) of + {ok, #member{state = ?STATE_RUNNING}} -> + compact_1(Method); + _ -> + {error,'not_running'} + end. + +%% @private +compact_1(suspend) -> leo_compaction_manager_fsm:suspend(); -compact(resume) -> +compact_1(resume) -> leo_compaction_manager_fsm:resume(); -compact(status) -> +compact_1(status) -> leo_compaction_manager_fsm:status().