-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
379 lines (317 loc) · 10.6 KB
/
index.js
File metadata and controls
379 lines (317 loc) · 10.6 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
process.on('uncaughtException', function(err) {
console.error('msg %s, name %s, stack->\n%s', err.message, err.name, err.stack);
});
var _async = require('async');
var _exec = require('child_process').exec;
var _fs = require('fs');
var _jmx = require('jmx');
var _os = require('os');
var _param = require('./param.json');
var _tools = require('graphdat-plugin-tools');
var DEBUG = false;
var HBASE_VERSION_WHERE_JMX_KEYS_CHANGED = 95;
var MAX_RETRY_CONNECTIONS = 10;
var RECONNECT_INTERVAL = 30000; // If HBase is restarted, it takes about 30s for custom metrics to be available
var RETRY_CONNECTION_INTERVAL = 5000;
var JMX_ATTRIBUTES = {
"hadoop:service=RegionServer,name=RegionServerStatistics": {
blockCacheExpressCachingRatio: { type:'percentage', graphdatKey: 'HBASE_BLOCK_CACHE_EXPRESS' },
compactionQueueLength: { type:'int', graphdatKey: 'HBASE_COMPACTION_QUEUE' },
flushQueueSize: { type:'int', graphdatKey: 'HBASE_FLUSH_QUEUE' },
hdfsBlocksLocalityIndex: { type:'percentage', graphdatKey: 'HBASE_LOCAL_BLOCK_RATIO' },
memstoreSizeMB: { type:'MB', graphdatKey: 'HBASE_MEMSTORE_SIZE' },
regions: { type:'int', graphdatKey: 'HBASE_REGIONS' },
readRequestsCount: { type:'sum', graphdatKey: 'HBASE_READ_REQUESTS' },
slowHLogAppendCount: { type:'sum', graphdatKey: 'HBASE_SLOW_LOG_APPENDS' },
writeRequestsCount: { type:'sum', graphdatKey: 'HBASE_WRITE_REQUESTS' },
},
"java.lang:type=Memory": {
HeapMemoryUsage: { type: 'MB', subtype: 'used', graphdatKey: 'HBASE_MEMORY' }
}
};
var _functionsToPoll; // functions to fetch the metrics
var _pollInterval; // the time interval to poll the metrics
var _pollTimeout; // the handler for the poll
var _previous; // the previous process count
var _source; // the source of the metrics
var _client;
var _connectionAttempt = 0;
var _isApplicationClosing = false;
var _isConnected = false;
var _isReconnect = false;
var _reconnectAt;
var _reconnectTimeout;
// ==========
// VALIDATION
// ==========
// where is the JMX endpoint
var _hostname = _param.hostname || 'localhost';
var _port = _param.port && parseInt(_param.port, 10) || 10102;
var _protocol = _param.protocol || 'rmi';
var _username = _param.username;
var _password = _param.password;
// how often should we poll
_pollInterval = (_param.pollSeconds && parseFloat(_param.pollSeconds) * 1000) ||
(_param.pollInterval) ||
5000;
// set the source if we do not have one
_source = (_param.source && _param.source.trim() !== '') ? _param.source : _os.hostname();
// Is the the plugin being run on a server with the region service
function checkRegionServer(cb) {
_exec('ps aux | grep HRegionServer | grep -v grep', function (err, stdout, stderr) {
if (err || stderr || !stdout)
return cb('The Graphdat HBase plugin should be run on a Region Server');
else
return cb(null);
});
}
// Is the the plugin being run on a server with the region service
function checkHBaseVersion(cb) {
var errmsg = 'Cannot detect the HBase version';
_exec('hbase version', function (err, stdout, stderr) {
if (err)
return cb(errmsg);
var lines = (stderr || stdout).split('\n');
if (!lines || lines.length === 0)
return cb(errmsg);
var match = lines[0].match(/HBase \w+.(\w+).\w+/);
if (!match)
return cb(errmsg);
var version = parseInt(match[1], 10);
if (isNaN(version))
return cb(errmsg);
if (version < HBASE_VERSION_WHERE_JMX_KEYS_CHANGED) {
var regionServers = JMX_ATTRIBUTES["hadoop:service=RegionServer,name=RegionServerStatistics"];
// blockCacheExpressCachingRatio used to be called blockCacheHitCachingRatio
regionServers.blockCacheHitCachingRatio = regionServers.blockCacheExpressCachingRatio;
delete regionServers.blockCacheExpressCachingRatio;
// compactionQueueLength used to be called compactionQueueSize
regionServers.compactionQueueSize = regionServers.compactionQueueLength;
delete regionServers.compactionQueueLength;
}
return cb(null);
});
}
// create the polling functions
function generate(cb) {
_functionsToPoll = [];
Object.keys(JMX_ATTRIBUTES).forEach(function(key) {
Object.keys(JMX_ATTRIBUTES[key]).forEach(function(name) {
_functionsToPoll.push(function(inner_cb) {
if (DEBUG) console.log('attempting to get %s from %s', name, key);
_client.getAttribute(key, name, function(data) {
var attribute = JMX_ATTRIBUTES[key][name];
var result;
if (data !== undefined) {
result = { key: key, name:name, type:attribute.type, graphdatKey:attribute.graphdatKey };
switch(attribute.type) {
case 'int':
result.value = parseInt(data,10);
break;
case 'percentage':
result.value = parseInt(data,10) / 100;
break;
case 'sum':
result.value = parseFloat(data.longValue,10);
break;
case 'MB':
if ('subtype' in attribute)
result.value = parseFloat(data.getSync(attribute.subtype).longValue, 10);
else
result.value = parseInt(data) * 1024 * 1024;
break;
}
}
return inner_cb && inner_cb(null, result);
});
});
});
});
return cb(null);
}
// helper to diff two numbers
function diff(a, b) {
if (a == null || b == null || isNaN(a) || isNaN(b))
return undefined;
else if (a<b)
return undefined; // int rolled over, ignore the value
else
return a - b;
}
// get the stats, format the output and send to stdout
function poll(cb) {
_async.series(_functionsToPoll, function(err, current) {
if (DEBUG) {
console.log('poll() - current');
console.log(current);
}
if (err || current == null) {
console.error('error in poll()');
console.error(err);
_previous = undefined;
_pollTimeout = setTimeout(poll, _pollInterval);
return;
}
if (_previous === undefined) {
// skip the first value otherwise it would be 0
if (DEBUG) console.log('skipping first record in poll');
_previous = current;
_pollTimeout = setTimeout(poll, _pollInterval);
return;
}
// things are good, lets process the values
if (DEBUG) console.log('poll() has values');
current.forEach(function(metric) {
var value;
switch(metric.type) {
case 'sum':
var prev = _previous.filter(function(p) { return p.graphdatKey === metric.graphdatKey; });
if (prev && prev.length === 1)
value = diff(metric.value, prev[0].value);
break;
default:
value = metric.value;
break;
}
if (value !== undefined)
console.log('%s %s %s', metric.graphdatKey, value, _source);
});
_previous = current;
_pollTimeout = setTimeout(poll, _pollInterval);
});
}
// ===============
// LET GET STARTED
// ===============
// create the JMX client to poll with
_client = _jmx.createClient({
host: _hostname,
port: _port,
protocol: _protocol,
username: _username,
password: _password
});
function connect() {
if (_isConnected === true) {
if (DEBUG) console.log('skipping connect(), we are already connected');
return;
}
if (_reconnectTimeout) {
clearTimeout(_reconnectTimeout);
_reconnectTimeout = null;
}
if (DEBUG) console.log('Attempting connection');
_connectionAttempt++;
_client.connect();
}
function reconnect() {
if (_isConnected === true) {
if (DEBUG) console.log('skipping reconnect(), we are already connected');
return;
}
if (_connectionAttempt > MAX_RETRY_CONNECTIONS) {
if (DEBUG) console.log('exceeded the number of attempts');
return;
}
var interval = (RETRY_CONNECTION_INTERVAL * _connectionAttempt) + RETRY_CONNECTION_INTERVAL;
var now = Date.now();
var reconnectTime = now + interval;
if (!_reconnectAt || // have not scheduled a reconnection
_reconnectAt < now || // the reconnection is in the past
reconnectTime < _reconnectAt) { // the reconnection is sooner as we have another error
console.log('Will attempt to reconnect to in ' + interval/1000 + ' secs');
_isReconnect = true;
_reconnectAt = reconnectTime;
clearTimeout(_reconnectTimeout);
_reconnectTimeout = setTimeout(connect, interval);
}
else {
if (DEBUG) console.log('ignoring reconnect()');
}
}
function disconnect() {
if (!_isConnected) {
if (DEBUG) console.log('skipping disconnect(), we are not connected');
return;
}
if (DEBUG) console.log('disconnect()');
if (_client)
_client.disconnect();
}
_client.on("error", function(err) {
if (DEBUG) console.log('on client.error');
// If the connection was refused, re-try
if (err && err.message && err.message.match(/Connection refused to host/)) {
_isConnected = false;
if (_pollTimeout) {
clearTimeout(_pollTimeout);
_pollTimeout = null;
}
reconnect();
}
else {
console.error(err);
process.exit(1);
}
});
_client.on("connect", function() {
if (DEBUG) console.log('on client.connect');
var isReconnect = _isReconnect;
_connectionAttempt = 0;
_isConnected = true;
_isReconnect = false;
_reconnectAt = undefined;
if (_reconnectTimeout) {
clearTimeout(_reconnectTimeout);
_reconnectTimeout = null;
}
if (_pollTimeout) {
clearTimeout(_pollTimeout);
_pollTimeout = null;
}
// start the polling for our metrics
_pollTimeout = setTimeout(poll, (isReconnect) ? RECONNECT_INTERVAL : _pollInterval);
});
_client.on('disconnect', function() {
if (DEBUG) console.log('on client.disconnect');
_isConnected = false;
if (_pollTimeout) {
clearTimeout(_pollTimeout);
_pollTimeout = null;
}
if (!_isApplicationClosing)
reconnect();
});
process.on('SIGINT', closeAndExit);
process.on('SIGKILL', closeAndExit);
process.on('SIGTERM', closeAndExit);
function closeAndExit()
{
_isApplicationClosing = true;
disconnect();
if (_pollTimeout)
{
clearTimeout(_pollTimeout);
_pollTimeout = null;
}
if (_reconnectTimeout)
{
clearTimeout(_reconnectTimeout);
_reconnectTimeout = null;
}
process.exit(0);
}
_async.series([
// function(cb) { checkRegionServer(cb); }, // make sure this is a region server
function(cb) { checkHBaseVersion(cb); }, // different HBase version use different keys
function(cb) { generate(cb); } // now we have the keys, generate the polling functions
],
function(err, results) {
if (err) {
console.error('startup');
console.error(err);
process.exit(1);
}
connect();
}
);