iilyak commented on a change in pull request #3766:
URL: https://github.com/apache/couchdb/pull/3766#discussion_r820730024
##########
File path: src/smoosh/src/smoosh_channel.erl
##########
@@ -194,18 +236,122 @@ handle_info(check_window, State0) ->
end,
erlang:send_after(60 * 1000, self(), check_window),
{noreply, FinalState};
-handle_info(pause, State0) ->
- {ok, State} = code_change(nil, State0, nil),
+handle_info(start_recovery, #state{name = Name, waiting = Waiting0} = State0)
->
+ RecActive = recover(active_file_name(Name)),
+ Waiting1 = lists:foldl(
+ fun(DbName, Acc) ->
+ case couch_db:is_compacting(DbName) of
+ true ->
+ Priority = smoosh_server:get_priority(Name, DbName),
+ smoosh_priority_queue:in(DbName, Priority, Priority, Acc);
+ false ->
+ Acc
+ end
+ end,
+ Waiting0,
+ RecActive
+ ),
+ State1 = maybe_start_compaction(State0#state{paused = false, waiting =
Waiting1}),
+ couch_log:notice(
+ "~p Previously active compaction jobs (if any) have been successfully
recovered and restarted.",
+ [?MODULE]
+ ),
+ erlang:send_after(?ACTIVATE_DELAY_IN_MSEC, self(), activate),
+ {noreply, State1#state{paused = true}};
+handle_info(activate, State) ->
+ {noreply, activate_channel(State)};
+handle_info(persist, State) ->
+ persist_queue(State),
+ erlang:send_after(?CHECKPOINT_INTERVAL_IN_MSEC, self(), persist),
+ {noreply, State};
+handle_info(pause, State) ->
{noreply, State#state{paused = true}};
-handle_info(unpause, State0) ->
- {ok, State} = code_change(nil, State0, nil),
+handle_info(unpause, State) ->
{noreply, maybe_start_compaction(State#state{paused = false})}.
terminate(_Reason, _State) ->
ok.
-code_change(_OldVsn, #state{} = State, _Extra) ->
- {ok, State}.
+persist_queue(#state{waiting = Waiting} = State) ->
+ write_state_to_file(State),
+ smoosh_priority_queue:write_to_file(Waiting).
+
+recover(FilePath) ->
+ case do_recover(FilePath) of
+ {ok, List} ->
+ List;
+ error ->
+ []
+ end.
+
+do_recover(FilePath) ->
+ case file:read_file(FilePath) of
+ {ok, Content} ->
+ <<Vsn, Binary/binary>> = Content,
+ try parse_state(Vsn, ?VSN, Binary) of
+ Term ->
+ couch_log:notice(
+ "~p Successfully restored state file ~s", [?MODULE,
FilePath]
+ ),
+ {ok, Term}
+ catch
+ error:Reason ->
+ couch_log:error(
+ "~p Invalid state file (~p). Deleting ~s", [?MODULE,
Reason, FilePath]
+ ),
+ file:delete(FilePath),
+ error
+ end;
+ {error, enoent} ->
+ couch_log:notice(
+ "~p (~p) State file ~s does not exist. Not restoring.",
[?MODULE, enoent, FilePath]
+ ),
+ error;
+ {error, Reason} ->
+ couch_log:error(
+ "~p Cannot read the state file (~p). Deleting ~s", [?MODULE,
Reason, FilePath]
+ ),
+ file:delete(FilePath),
+ error
+ end.
+
+parse_state(1, ?VSN, Binary) ->
+ erlang:binary_to_term(Binary, [safe]);
+parse_state(Vsn, ?VSN, _) ->
+ error({unsupported_version, Vsn}).
+
+write_state_to_file(#state{name = Name, active = Active, starting = Starting})
->
+ Active1 = lists:foldl(
+ fun({DbName, _}, Acc) ->
+ [DbName | Acc]
+ end,
+ [],
+ Active
+ ),
+ Starting1 = lists:foldl(
+ fun({_, DbName}, Acc) ->
+ [DbName | Acc]
+ end,
+ [],
+ Starting
+ ),
+ write_to_file(Active1, active_file_name(Name)),
+ write_to_file(Starting1, starting_file_name(Name)).
+
+write_to_file(List, FileName) ->
+ couch_log:notice("~p Writing state to state file ~s", [?MODULE, FileName]),
+ OnDisk = <<?VSN, (erlang:term_to_binary(List, [compressed, {minor_version,
1}]))/binary>>,
+ TmpFileName = FileName ++ ".tmp",
+ file:delete(TmpFileName),
+ file:write_file(TmpFileName, OnDisk, [sync]),
+ file:delete(FileName),
+ file:rename(TmpFileName, FileName).
Review comment:
Consider logging cases when `{error, Reason}` is returned.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]