Skip to content

Commit

Permalink
follow Nils's wise advice, add membership checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
benfleis committed Feb 18, 2016
1 parent bcb9949 commit 93ed41c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
1 change: 1 addition & 0 deletions lib/on_membership_event.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function createChecksumComputedHandler(ringpop) {

return function onMembershipChecksumComputed(event) {
ringpop.stat('increment', 'membership.checksum-computed');
ringpop.stat('gauge', 'membership.checksum', event.checksum);
ringpop.emit('membershipChecksumComputed');

if (event.oldChecksum !== event.checksum) {
Expand Down
1 change: 1 addition & 0 deletions lib/on_ring_event.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function createChecksumComputedHandler(ringpop) {

return function onRingChecksumComputed(event) {
ringpop.stat('increment', 'ring.checksum-computed');
ringpop.stat('gauge', 'ring.checksum', event.checksum);
ringpop.emit('ringChecksumComputed');

if (event.oldChecksum !== event.checksum) {
Expand Down
34 changes: 28 additions & 6 deletions lib/stats-periodic.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ var periodMinimum = 10;
//
// Currently supported:
// {
// timers: { ... }
// timers: { ... } // (like timer-shim, built in timers module)
// periods: {
// default: ...
// minimum: ...
// ringChecksum: default
// default: period-in-ms
// minimum: period-in-ms // never allow below this, typ. 10ms
// membershipChecksum: period-in-ms
// ringChecksum: period-in-ms
// }
// }
//
Expand All @@ -54,19 +55,35 @@ function PeriodicStats(ringpop, options) {

this.periodDefault = periods.default || periodDefault;
this.periodMinimum = periods.minimum || periodMinimum;

this.membershipChecksumPeriod = this.normalizePeriod(periods.membershipChecksum);
this.ringChecksumPeriod = this.normalizePeriod(periods.ringChecksum);
}

PeriodicStats.prototype.start = function start() {
var self = this;

var setInterval = self.timers.setInterval;
self.ringChecksumTimer = setInterval(emitChecksum, self.ringChecksumPeriod);

self.membershipChecksumTimer =
setInterval(emitMembershipChecksum, self.membershipChecksumPeriod);
self.membershipChecksumTimer.unref();

self.ringChecksumTimer =
setInterval(emitRingChecksum, self.ringChecksumPeriod);
self.ringChecksumTimer.unref();

self.running = true;
return;

function emitChecksum() {
function emitMembershipChecksum() {
var checksum = self.ringpop.membership.checksum;
if (checksum !== null && checksum !== undefined) {
self.ringpop.stat('gauge', 'membership.checksum-periodic', checksum);
}
}

function emitRingChecksum() {
var checksum = self.ringpop.ring.checksum;
if (checksum !== null && checksum !== undefined) {
self.ringpop.stat('gauge', 'ring.checksum-periodic', checksum);
Expand All @@ -76,8 +93,13 @@ PeriodicStats.prototype.start = function start() {

PeriodicStats.prototype.stop = function stop() {
var clearInterval = this.timers.clearInterval;

this.membershipChecksumTimer && clearInterval(this.membershipChecksumTimer);
this.membershipChecksumTimer = null;

this.ringChecksumTimer && clearInterval(this.ringChecksumTimer);
this.ringChecksumTimer = null;

this.running = false;
}

Expand Down
20 changes: 16 additions & 4 deletions test/unit/stats-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ test('periodic stats have correct defaults', function t(assert) {
stats.start();
assert.deepEqual(ringpop.journal, [], 'empty journal after start');
ringpop.timers.advance(5000);
assert.deepEqual(ringpop.journal, [['stat', 'gauge', 'ring.checksum-periodic', 0xDEADBEEF]]);
debugger;
assert.deepEqual(ringpop.journal, [
// hard ordering, not worth changing for 2. _.contains doesn't work :(
['stat', 'gauge', 'membership.checksum-periodic', 0xCAFED00D],
['stat', 'gauge', 'ring.checksum-periodic', 0xDEADBEEF]
]);

stats.stop();
assert.end();
Expand All @@ -60,7 +65,11 @@ test('periodic stats respect period overrides', function t(assert) {
stats.start();
assert.deepEqual(ringpop.journal, [], 'empty journal after start');
ringpop.timers.advance(500);
assert.deepEqual(ringpop.journal, [['stat', 'gauge', 'ring.checksum-periodic', 0xDEADBEEF]]);
assert.deepEqual(ringpop.journal, [
// hard ordering, not worth changing for 2. _.contains doesn't work :(
['stat', 'gauge', 'membership.checksum-periodic', 0xCAFED00D],
['stat', 'gauge', 'ring.checksum-periodic', 0xDEADBEEF]
]);

stats.stop();
assert.end();
Expand Down Expand Up @@ -127,8 +136,10 @@ test('start/stop works too', function t(assert) {

stats.start();
ringpop.timers.advance(5000);
assert.deepEqual(ringpop.journal.length, 1, 'journal contains 1 entry');
assert.deepEqual(ringpop.journal[0][3], 0xDEADBEEF, 'journal contains checksum');
// hard ordering, not worth changing for 2. _.contains doesn't work :(
assert.deepEqual(ringpop.journal.length, 2, 'journal contains 1 entry');
assert.deepEqual(ringpop.journal[0][3], 0xCAFED00D, 'journal contains checksum');
assert.deepEqual(ringpop.journal[1][3], 0xDEADBEEF, 'journal contains checksum');
ringpop.journal = [];

stats.stop();
Expand Down Expand Up @@ -198,6 +209,7 @@ function RingpopMock() {
info: push.bind(null, 'logger.info'),
warn: push.bind(null, 'logger.warn'),
};
this.membership = {checksum: 0xCAFED00D};
this.ring = {checksum: 0xDEADBEEF};
this.stat = push.bind(null, 'stat');
this.timers = makeTimersMock();
Expand Down

0 comments on commit 93ed41c

Please sign in to comment.