nickva commented on code in PR #4672: URL: https://github.com/apache/couchdb/pull/4672#discussion_r1266208655
########## src/couch_stats/src/couch_stats_server.erl: ########## @@ -0,0 +1,246 @@ +% 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. + +% couch_stats_server is in charge of: +% - Initial metric loading from application stats descriptions. +% - Recycling(resetting to 0) stale histogram counters. +% - Checking and reloading if stats descriptions change. +% - Checking and reloading if histogram window size config value changes. +% + +-module(couch_stats_server). + +-behaviour(gen_server). + +-export([ + reload/0 +]). + +-export([ + start_link/0, + init/1, + handle_call/3, + handle_cast/2, + handle_info/2 +]). + +-define(RELOAD_INTERVAL_SEC, 600). + +-record(st, { + hist_interval, + histograms, + hist_tref, + reload_tref +}). + +reload() -> + gen_server:call(?MODULE, reload). + +start_link() -> + gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). + +init([]) -> + St = #st{ + hist_interval = config:get("stats", "interval"), + hist_tref = erlang:send_after(hist_msec(), self(), clean), + reload_tref = erlang:send_after(reload_msec(), self(), reload) + }, + {_, Stats} = try_reload(St), + {ok, St#st{histograms = couch_stats_util:histograms(Stats)}}. + +handle_call(reload, _From, #st{} = St) -> + {reply, ok, do_reload(St)}; +handle_call(Msg, _From, #st{} = St) -> + {stop, {unknown_call, Msg}, unknown_call, St}. + +handle_cast(Msg, #st{} = St) -> + {stop, {unknown_cast, Msg}, St}. + +handle_info(reload, #st{} = St) -> + {noreply, do_reload(St)}; +handle_info(clean, #st{} = St) -> + {noreply, do_clean(St)}; +handle_info(Msg, #st{} = St) -> + {stop, {unknown_info, Msg}, St}. + +do_clean(#st{} = St) -> + timer:cancel(St#st.hist_tref), + HistTRef = erlang:send_after(hist_msec(), self(), clean), + NowSec = erlang:monotonic_time(second), + BufferSec = couch_stats_util:histogram_safety_buffer_size_sec(), + IntervalSec = couch_stats_util:histogram_interval_sec(), + % The histogram timeline looks something like: + % + % |<--buffer-->|<--stale-->|<--buffer-->|<--current-->| + % ^ ^ + % StartSec NowSec + % + % To get to the start of "stale" part to clean it, subtract one interval, + % then a buffer, then another interval from NowSec. + % + StartSec = NowSec - IntervalSec - BufferSec - (IntervalSec - 1), + % Last -1 is because the interval ends are inclusive + maps:foreach( + fun(_, {_, Ctx, _}) -> + couch_stats_histogram:clear(Ctx, StartSec, IntervalSec) + end, + St#st.histograms + ), + St#st{hist_tref = HistTRef}. + +do_reload(#st{} = St) -> + timer:cancel(St#st.reload_tref), + RTRef = erlang:send_after(reload_msec(), self(), reload), + case try_reload(St) of + {true, NewStats} -> + timer:cancel(St#st.hist_tref), + Histograms = couch_stats_util:histograms(NewStats), + HTRef = erlang:send_after(hist_msec(), self(), clean), + St#st{ + histograms = Histograms, + hist_tref = HTRef, + reload_tref = RTRef, + hist_interval = config:get("stats", "interval") + }; + {false, _} -> + St#st{reload_tref = RTRef} + end. + +try_reload(#st{} = St) -> + NewDefs = couch_stats_util:load_metrics_for_applications(), + Stats = couch_stats_util:stats(), + MetricsChanged = couch_stats_util:metrics_changed(Stats, NewDefs), + IntervalChanged = interval_changed(St), + case MetricsChanged orelse IntervalChanged of + true -> + couch_stats_util:reset_histogram_interval_sec(), + NewStats = couch_stats_util:create_metrics(NewDefs), + couch_stats_util:replace_stats(NewStats), + {true, NewStats}; + false -> + {false, Stats} + end. + +interval_changed(#st{hist_interval = OldInterval}) -> + case config:get("stats", "interval") of + Interval when OldInterval =:= Interval -> + false; + _ -> + true + end. + +reload_msec() -> + 1000 * ?RELOAD_INTERVAL_SEC. + +hist_msec() -> + (couch_stats_util:histogram_interval_sec() * 1000) div 2. Review Comment: The idea is that we'd want the cleaner to wake up more often, and I picked at least twice as often as our interval. I'll add a comment about it as it's a bit cryptic. -- 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]
