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

Switch to select by reputation #548

Merged
merged 1 commit into from
Jan 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/server/routes/frames.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const log = require('../../logger');
const constants = require('../../constants');
const analytics = require('storj-analytics');
const limiter = require('../limiter').DEFAULTS;
const utils = require('../../utils');

/**
* Handles endpoints for all frame/file staging related operations
Expand Down Expand Up @@ -322,7 +323,7 @@ FramesRouter.prototype.addShardToFrame = function(req, res, next) {
return false;
});

filtered.sort(FramesRouter._sortByResponseTime);
filtered.sort(utils.sortByReputation);

mirror = filtered[0];
}
Expand Down
3 changes: 2 additions & 1 deletion lib/server/routes/reports.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const constants = require('../../constants');
const async = require('async');
const storj = require('storj-lib');
const limiter = require('../limiter').DEFAULTS;
const utils = require('../../utils');

/**
* Handles endpoints for reporting
Expand Down Expand Up @@ -150,7 +151,7 @@ ReportsRouter.prototype._triggerMirrorEstablish = function(n, hash, done) {
return callback(new Error('Auto mirroring limit is reached'));
}

available.sort(ReportsRouter._sortByTimeoutRate);
available.sort(utils.sortByReputation);

callback(null, available.shift());
}
Expand Down
11 changes: 11 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ module.exports.createArrayFormatter = function(transformer) {
});
};

/**
* Sort by reputation, to be used with Array.prototype.sort
* @param {Object} - a
* @param {Object} - b
*/
module.exports.sortByReputation = function(a, b) {
const a1 = a.contact.reputation >= 0 ? a.contact.reputation : 0;
const b1 = b.contact.reputation >= 0 ? b.contact.reputation : 0;
return (a1 === b1) ? 0 : (a1 > b1) ? -1 : 1;
};

/**
* Will get a timestamp integer from a string or number
* argument, including ISO formatted strings.
Expand Down
10 changes: 5 additions & 5 deletions test/server/routes/frames.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ describe('FramesRouter', function() {
).callsArgWith(1, null, frame0);

const farmer = {
responseTime: 100,
reputation: 5000,
nodeID: storj.utils.rmd160('farmer'),
address: '127.0.0.1',
port: 8080
Expand All @@ -1274,7 +1274,7 @@ describe('FramesRouter', function() {

const mirrors = [
{
contact: { responseTime: 10100 },
contact: { reputation: 1000 },
isEstablished: false
},
{
Expand All @@ -1289,15 +1289,15 @@ describe('FramesRouter', function() {
},
{ },
{
contact: { responseTime: 200 },
contact: { reputation: 4000 },
isEstablished: true
},
{
contact: { responseTime: 4100 },
contact: { responseTime: 2000 },
isEstablished: true
},
{
contact: { responseTime: 2100 },
contact: { reputation: 3000 },
isEstablished: false
}
];
Expand Down
3 changes: 2 additions & 1 deletion test/server/routes/reports.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const expect = require('chai').expect;
const errors = require('storj-service-error-types');
const EventEmitter = require('events').EventEmitter;
const ReportsRouter = require('../../../lib/server/routes/reports');
const utils = require('../../../lib/utils');

describe('ReportsRouter', function() {

Expand Down Expand Up @@ -420,7 +421,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._sortByTimeoutRate);
.to.equal(utils.sortByReputation);
done();
});
});
Expand Down
34 changes: 34 additions & 0 deletions test/utils.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,38 @@ describe('module:utils', function() {
});
});

describe('@sortByReputation', function() {
it('will sort with best reputation at the top', function() {
const mirrors = [{
contact: { reputation: 100 },
}, {
contact: { reputation: 1287 },
}, {
contact: { reputation: 5000 },
}, {
contact: { reputation: 4500 },
}, {
contact: { },
}, {
contact: { reputation: 300 },
}];

mirrors.sort(utils.sortByReputation);

expect(mirrors).to.eql([{
contact: { reputation: 5000 },
}, {
contact: { reputation: 4500 },
}, {
contact: { reputation: 1287 },
}, {
contact: { reputation: 300 },
}, {
contact: { reputation: 100 },
}, {
contact: { },
}]);
});
});

});