Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Merge pull request #516 from braydonf/master
Browse files Browse the repository at this point in the history
Send mirrors to nodes based on timeout rates
  • Loading branch information
braydonf authored Oct 9, 2017
2 parents 8cd8091 + e2c4a26 commit b93d0f9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/server/routes/reports.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ ReportsRouter.prototype._handleExchangeReport = function(report, callback) {
}
};

ReportsRouter._sortByTimeoutRate = function(a, b) {
const a1 = a.contact.timeoutRate >= 0 ? a.contact.timeoutRate : 0;
const b1 = b.contact.timeoutRate >= 0 ? b.contact.timeoutRate : 0;
return (a1 === b1) ? 0 : (a1 > b1) ? 1 : -1;
};

ReportsRouter._sortByResponseTime = function(a, b) {
const aTime = a.contact.responseTime || Infinity;
const bTime = b.contact.responseTime || Infinity;
Expand Down Expand Up @@ -144,7 +150,7 @@ ReportsRouter.prototype._triggerMirrorEstablish = function(n, hash, done) {
return callback(new Error('Auto mirroring limit is reached'));
}

available.sort(ReportsRouter._sortByResponseTime);
available.sort(ReportsRouter._sortByTimeoutRate);

callback(null, available.shift());
}
Expand Down
40 changes: 39 additions & 1 deletion test/server/routes/reports.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,44 @@ describe('ReportsRouter', function() {

});

describe('@_sortByTimeoutRate', function() {
it('will sort with the best timeout rate (0) at top', function() {
const mirrors = [{
contact: { timeoutRate: 0.99 }
}, {
contact: { timeoutRate: 0.03 }
}, {
contact: { }
}, {
contact: { timeoutRate: 0.98 }
}, {
contact: { timeoutRate: 0.98 }
}, {
contact: { timeoutRate: 1 }
}, {
contact: { timeoutRate: 0 }
}];

mirrors.sort(ReportsRouter._sortByTimeoutRate);

expect(mirrors).to.eql([{
contact: { }
}, {
contact: { timeoutRate: 0 }
}, {
contact: { timeoutRate: 0.03 }
}, {
contact: { timeoutRate: 0.98 }
}, {
contact: { timeoutRate: 0.98 }
}, {
contact: { timeoutRate: 0.99 }
}, {
contact: { timeoutRate: 1 }
}]);
});
});

describe('@_sortByResponseTime', function() {
it('will sort correctly with best response time at index 0', function() {
var available = [
Expand Down Expand Up @@ -382,7 +420,7 @@ describe('ReportsRouter', function() {
expect(err).to.equal(null);
expect(Array.prototype.sort.callCount).to.equal(1);
expect(Array.prototype.sort.args[0][0])
.to.equal(ReportsRouter._sortByResponseTime);
.to.equal(ReportsRouter._sortByTimeoutRate);
done();
});
});
Expand Down

0 comments on commit b93d0f9

Please sign in to comment.