Skip to content

Commit

Permalink
lib: changed functional logic in cluster schedulers
Browse files Browse the repository at this point in the history
Free pool in round_robin scheduler is implemented as an array. There
were constant lookups being for distributing load on other workers in
free pool. Reimplementing in Map will create will be more performant as
compared to Array implementation. This was done for all in past but free
wasn't implemented at that time.

PR-URL: #32505
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
  • Loading branch information
yashLadha authored and BethGriggs committed Apr 14, 2020
1 parent 7a46be2 commit ce79923
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
19 changes: 10 additions & 9 deletions lib/internal/cluster/round_robin_handle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const {
ArrayIsArray,
Boolean,
Map,
} = primordials;
Expand All @@ -15,7 +16,7 @@ module.exports = RoundRobinHandle;
function RoundRobinHandle(key, address, port, addressType, fd, flags) {
this.key = key;
this.all = new Map();
this.free = [];
this.free = new Map();
this.handles = [];
this.handle = null;
this.server = net.createServer(assert.fail);
Expand Down Expand Up @@ -73,10 +74,7 @@ RoundRobinHandle.prototype.remove = function(worker) {
if (!existed)
return false;

const index = this.free.indexOf(worker);

if (index !== -1)
this.free.splice(index, 1);
this.free.delete(worker.id);

if (this.all.size !== 0)
return false;
Expand All @@ -93,21 +91,24 @@ RoundRobinHandle.prototype.remove = function(worker) {

RoundRobinHandle.prototype.distribute = function(err, handle) {
this.handles.push(handle);
const worker = this.free.shift();
const [ workerEntry ] = this.free;

if (worker)
if (ArrayIsArray(workerEntry)) {
const [ workerId, worker ] = workerEntry;
this.free.delete(workerId);
this.handoff(worker);
}
};

RoundRobinHandle.prototype.handoff = function(worker) {
if (this.all.has(worker.id) === false) {
if (!this.all.has(worker.id)) {
return; // Worker is closing (or has closed) the server.
}

const handle = this.handles.shift();

if (handle === undefined) {
this.free.push(worker); // Add to ready queue again.
this.free.set(worker.id, worker); // Add to ready queue again.
return;
}

Expand Down
17 changes: 8 additions & 9 deletions lib/internal/cluster/shared_handle.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
const { Map } = primordials;
const assert = require('internal/assert');
const dgram = require('internal/dgram');
const net = require('net');
Expand All @@ -7,7 +8,7 @@ module.exports = SharedHandle;

function SharedHandle(key, address, port, addressType, fd, flags) {
this.key = key;
this.workers = [];
this.workers = new Map();
this.handle = null;
this.errno = 0;

Expand All @@ -24,20 +25,18 @@ function SharedHandle(key, address, port, addressType, fd, flags) {
}

SharedHandle.prototype.add = function(worker, send) {
assert(!this.workers.includes(worker));
this.workers.push(worker);
assert(!this.workers.has(worker.id));
this.workers.set(worker.id, worker);
send(this.errno, null, this.handle);
};

SharedHandle.prototype.remove = function(worker) {
const index = this.workers.indexOf(worker);

if (index === -1)
return false; // The worker wasn't sharing this handle.
if (!this.workers.has(worker.id))
return false;

this.workers.splice(index, 1);
this.workers.delete(worker.id);

if (this.workers.length !== 0)
if (this.workers.size !== 0)
return false;

this.handle.close();
Expand Down

0 comments on commit ce79923

Please sign in to comment.