Skip to content

Commit

Permalink
Merge pull request #94 from uber/use-ring-changed-event
Browse files Browse the repository at this point in the history
Have dissemination component subscribe to ringChanged to reduce times…
  • Loading branch information
jwolski2 committed Jun 21, 2015
2 parents ddd6fab + 24cea10 commit c09fa46
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
23 changes: 19 additions & 4 deletions lib/dissemination.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,24 @@
// 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.

'use strict';

var EventEmitter = require('events').EventEmitter;
var util = require('util');

var LOG_10 = Math.log(10);

function Dissemination(ringpop) {
this.ringpop = ringpop;
this.ringpop.on('changed', this.onRingChanged.bind(this));
this.ringpop.on('ringChanged', this.onRingChanged.bind(this));

this.changes = {};
this.maxPiggybackCount = 1;
this.piggybackFactor = 15; // A lower piggyback factor leads to more full-syncs
this.maxPiggybackCount = Dissemination.Defaults.maxPiggybackCount;
this.piggybackFactor = Dissemination.Defaults.piggybackFactor;
}

util.inherits(Dissemination, EventEmitter);

Dissemination.prototype.adjustMaxPiggybackCount = function adjustMaxPiggybackCount() {
var serverCount = this.ringpop.ring.getServerCount();
var prevPiggybackCount = this.maxPiggybackCount;
Expand All @@ -45,6 +49,8 @@ Dissemination.prototype.adjustMaxPiggybackCount = function adjustMaxPiggybackCou
piggybackFactor: this.piggybackFactor,
serverCount: serverCount
});

this.emit('maxPiggybackCountAdjusted');
}
};

Expand Down Expand Up @@ -126,4 +132,13 @@ Dissemination.prototype.recordChange = function recordChange(change) {
this.changes[change.address] = change;
};

Dissemination.prototype.resetMaxPiggybackCount = function resetMaxPiggybackCount() {
this.maxPiggybackCount = Dissemination.Defaults.maxPiggybackCount;
};

Dissemination.Defaults = {
maxPiggybackCount: 1,
piggybackFactor: 15 // A lower piggyback factor leads to more full-syncs
};

module.exports = Dissemination;
1 change: 0 additions & 1 deletion lib/membership.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
// 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.

'use strict';

var _ = require('underscore');
Expand Down
37 changes: 37 additions & 0 deletions test/index_test.js → test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var recvAdminLeave = require('../lib/admin-leave-recvr.js');
var recvJoin = require('../lib/swim/join-recvr.js');
var Ringpop = require('../index.js');
var test = require('tape');
var testRingpop = require('./lib/test-ringpop.js');

function createRingpop(opts) {
return new Ringpop(_.extend({
Expand Down Expand Up @@ -406,6 +407,42 @@ test('emits ring changed event', function t(assert) {
assert.end();
});

testRingpop('max piggyback not adjusted on membership update', function t(deps, assert) {
assert.plan(0);

var dissemination = deps.dissemination;
var membership = deps.membership;

dissemination.on('maxPiggybackCountAdjusted', function onAdjusted() {
assert.fail('max piggyback count was adjusted');
});

// Reset count to prove that it goes unmodified.
dissemination.resetMaxPiggybackCount();

var address = '127.0.0.1:3002';
var incarnationNumber = Date.now();
membership.makeSuspect(address, incarnationNumber);
});

testRingpop('max piggyback adjusted on new members', function t(deps, assert) {
assert.plan(1);

var dissemination = deps.dissemination;
var membership = deps.membership;

dissemination.on('maxPiggybackCountAdjusted', function onAdjusted() {
assert.pass('max piggyback count was adjusted');
});

// Reset count to prove that it is modified.
dissemination.resetMaxPiggybackCount();

var address = '127.0.0.1:3002';
var incarnationNumber = Date.now();
membership.makeAlive(address, incarnationNumber);
});

test('first time member, not alive', function t(assert) {
var ringpop = createRingpop();
ringpop.membership.makeAlive(ringpop.whoami(), Date.now());
Expand Down

0 comments on commit c09fa46

Please sign in to comment.