summaryrefslogtreecommitdiff
path: root/src/couch_jobs/src/couch_jobs_activity_monitor.erl
blob: 9802f5798e914d210c7aff98585b0db167a9f29a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
% 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.

-module(couch_jobs_activity_monitor).

-behaviour(gen_server).


-export([
    start_link/1
]).

-export([
    init/1,
    terminate/2,
    handle_call/3,
    handle_cast/2,
    handle_info/2,
    code_change/3
]).

-record(st, {
    jtx,
    type,
    tref,
    timeout = 0,
    vs = not_found
}).


-define(MAX_JITTER_DEFAULT, 10000).
-define(MISSING_TIMEOUT_CHECK, 5000).


start_link(Type) ->
    gen_server:start_link(?MODULE, [Type], []).


%% gen_server callbacks

init([Type]) ->
    St = #st{jtx = couch_jobs_fdb:get_jtx(), type = Type},
    {ok, schedule_check(St)}.


terminate(_, _St) ->
    ok.


handle_call(Msg, _From, St) ->
    {stop, {bad_call, Msg}, {bad_call, Msg}, St}.


handle_cast(Msg, St) ->
    {stop, {bad_cast, Msg}, St}.


handle_info(check_activity, St) ->
    St1 = try
        check_activity(St)
    catch
        error:{erlfdb_error, Err} when Err =:= 1020 orelse Err =:= 1031 ->
            LogMsg = "~p : type:~p got ~p error, possibly from overload",
            couch_log:error(LogMsg, [?MODULE, St#st.type, Err]),
            St
    end,
    St2 = schedule_check(St1),
    {noreply, St2};

handle_info({Ref, ready}, St) when is_reference(Ref) ->
    % Don't crash out couch_jobs_server and the whole application would need to
    % eventually do proper cleanup in erlfdb:wait timeout code.
    LogMsg = "~p : spurious erlfdb future ready message ~p",
    couch_log:error(LogMsg, [?MODULE, Ref]),
    {noreply, St};

handle_info(Msg, St) ->
    {stop, {bad_info, Msg}, St}.


code_change(_OldVsn, St, _Extra) ->
    {ok, St}.


% Private helper functions

check_activity(#st{jtx = JTx, type = Type, vs = not_found} = St) ->
    NewVS = couch_jobs_fdb:tx(JTx, fun(JTx1) ->
        couch_jobs_fdb:get_activity_vs(JTx1, Type)
    end),
    St#st{vs = NewVS};

check_activity(#st{jtx = JTx, type = Type, vs = VS} = St) ->
    NewVS = couch_jobs_fdb:tx(JTx, fun(JTx1) ->
        NewVS = couch_jobs_fdb:get_activity_vs(JTx1, Type),
        JobIds = couch_jobs_fdb:get_inactive_since(JTx1, Type, VS),
        couch_jobs_fdb:re_enqueue_inactive(JTx1, Type, JobIds),
        NewVS
    end),
    St#st{vs = NewVS}.


get_timeout_msec(JTx, Type) ->
    TimeoutVal = couch_jobs_fdb:tx(JTx, fun(JTx1) ->
        couch_jobs_fdb:get_type_timeout(JTx1, Type)
    end),
    case TimeoutVal of
        not_found -> not_found;
        ValSeconds -> timer:seconds(ValSeconds)
    end.


schedule_check(#st{jtx = JTx, type = Type, timeout = OldTimeout} = St) ->
    % Reset versionstamp if timeout changed.
    St1 = case get_timeout_msec(JTx, Type) of
        not_found ->
            St#st{vs = not_found, timeout = ?MISSING_TIMEOUT_CHECK};
        OldTimeout ->
            St;
        NewTimeout ->
            St#st{vs = not_found, timeout = NewTimeout}
    end,
    #st{timeout = Timeout} = St1,
    MaxJitter = min(Timeout div 2, get_max_jitter_msec()),
    Wait = Timeout + rand:uniform(max(1, MaxJitter)),
    St1#st{tref = erlang:send_after(Wait, self(), check_activity)}.


get_max_jitter_msec()->
    config:get_integer("couch_jobs", "activity_monitor_max_jitter_msec",
        ?MAX_JITTER_DEFAULT).