-
Notifications
You must be signed in to change notification settings - Fork 2
/
sampling_detector.js
251 lines (226 loc) · 9.65 KB
/
sampling_detector.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright (c) 2012 Matt Weagle ([email protected])
// Permission is hereby granted, free of charge, to
// any person obtaining a copy of this software and
// associated documentation files (the "Software"),
// to deal in the Software without restriction,
// including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so,
// subject to the following conditions:
// The above copyright notice and this permission
// notice shall be included in all copies or substantial
// portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE
var util = require('util');
var events = require('events');
var phi = require('../phi');
var _ = require('underscore');
/*****************************************************************************/
// Privates
/*****************************************************************************/
var AVAILABILITY_TIMEOUT_MULTIPLE = 5;
/**
* Sampling detector adapts a function (either sync or async) into one that
* can drive a phi-detector. The following function signatures are allowed:
* function():Boolean
* function(callback):Undefined, where callback is the standard
* callback(error, result) signature.
* The async form is considered to have "signaled" if there is no error AND
* the result is a "truthy" value.
*
* @param {Function} sampling_function function():Boolean or function(callback(e, result)):Undefined
* that is periodically called to determine if a provider
* is available
* @param {Number} interval_ms Frequency with which sampling_function() should
* be called
* @param {Object} phi_detector phi detector object that the results of the
* sampling_function should be directed.
*/
var SamplingDetector = function(sampling_function, interval_ms, phi_detector)
{
events.EventEmitter.call(this);
this._sampling_function = sampling_function;
this._interval_ms = interval_ms;
this._phi_detector = phi_detector;
this._phi_detector.on('available', this._on_available.bind(this));
this._phi_detector.on('unavailable', this._on_unavailable.bind(this));
this._cached_availability = this._phi_detector.is_available();
// Setup a timeout that manages checking the availability and
// can therefore broadcast state changes in the absence of signaling
// events
this._on_availability_check();
this._timeout_id = setTimeout(this._on_sample.bind(this), interval_ms);
var self = this;
// Accessors
Object.defineProperty(this, 'phi', {
get :function () {
return self._phi_detector.phi();
}});
Object.defineProperty(this, 'settings', {
get :function () {
return self._phi_detector.settings;
}});
};
util.inherits(SamplingDetector, events.EventEmitter);
SamplingDetector.prototype.is_available = function()
{
return this._phi_detector.is_available();
};
SamplingDetector.prototype._on_availability_check = function()
{
var now_available = this.is_available();
if (this._cached_availability && !now_available)
{
this._on_unavailable(this._phi_detector.phi());
}
else if (!this._cached_availability && now_available)
{
this._on_available(this._phi_detector.phi());
}
this._cached_availability = now_available;
// Repeat
this._availability_timeout_id = setTimeout(this._on_availability_check.bind(this),
AVAILABILITY_TIMEOUT_MULTIPLE * this._interval_ms);
};
SamplingDetector.prototype._on_available = function(phi)
{
this.emit('available', phi);
};
SamplingDetector.prototype._on_unavailable = function(phi)
{
this.emit('unavailable', phi);
};
SamplingDetector.prototype._on_sample = function()
{
try
{
var self = this;
if (this._sampling_function.length > 0)
{
var on_sample_result = function(error, result)
{
if (!error && result)
{
self._phi_detector.signal();
}
self._timeout_id = setTimeout(self._on_sample.bind(self),
self._interval_ms);
// In case an event listener
// wants to stop the polling loop we emit
// after assigning the new timeoutID
self.emit('sample');
};
this._sampling_function(on_sample_result);
}
else
{
try
{
if (this._sampling_function())
{
self._phi_detector.signal();
}
else
{
}
}
catch (e)
{
}
this._timeout_id = setTimeout(this._on_sample.bind(this),
this._interval_ms);
this.emit('sample');
}
}
catch (e)
{
// No signaling, but keep trying...
this._timeout_id = setTimeout(this._on_sample.bind(this),
this.interval_ms);
}
};
SamplingDetector.prototype.stop = function()
{
if (this._timeout_id)
{
clearTimeout(this._timeout_id);
this._timeout_id = null;
}
};
/*****************************************************************************/
// Exports
/*****************************************************************************/
/**
* Create a new sampling detector using the supplied arguments. Two forms
* are allowed:
* (sampling_function,
sampling_frequency_ms,
phi_detector_object)
* OR:
* (sampling_function,
sampling_frequency_ms,
threshold,
max_sample_size,
min_std_deviation,
acceptable_heartbeat_pause_ms,
first_heartbeat_estimate_ms,
optional_name)
* @param {Function} sampling_function Sampling function to call.
* Function supports two signatures:
* sampling_function():Boolean [sync]
* sampling_function(callback):Undefined [async]
* The callback is the standard fn(error, result) signature
* and is considered a successful test if both the error
* is null and the result object is truthy.
* @param {Number} sampling_frequency_ms Frequency with which sampling_function should be
* called
* @param {Number/Object} threshold_or_phi_detector Either a phi_threshold or a phi_detector object.
* @param {Number} max_sample_size Max samples to use for phi calculation.
* May be undefined if phi_detector is provided.
* @param {Number} min_std_deviation Minimum standard deviation of readings
* May be undefined if phi_detector is provided.
* @param {Number} acceptable_heartbeat_pause_ms Duration (ms) corresponding to the
number of potentially lost/delayed
events that will be accepted before
it is considered anomalous.
* May be undefined if phi_detector is provided.
* @param {Number} first_heartbeat_estimate_ms Duration (ms) values with which to bootstrap
* the event history.
* May be undefined if phi_detector is provided.
* @param {String} optional_name Optional name for phi_detector.
* May be undefined if phi_detector is provided.
* @return {Object} A new SamplingDetector object
*/
module.exports.new_sampling_detector = function(sampling_function,
sampling_frequency_ms,
threshold_or_phi_detector,
max_sample_size,
min_std_deviation,
acceptable_heartbeat_pause_ms,
first_heartbeat_estimate_ms,
optional_name)
{
var phi_detector = _.isObject(threshold_or_phi_detector) ?
threshold_or_phi_detector : null;
if (!phi_detector)
{
phi_detector = phi.new_detector(threshold_or_phi_detector,
max_sample_size,
min_std_deviation,
acceptable_heartbeat_pause_ms,
first_heartbeat_estimate_ms,
optional_name);
}
return new SamplingDetector(sampling_function,
sampling_frequency_ms,
phi_detector);
};