Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rotate workers when they are all idling #4921

Merged
merged 1 commit into from
Nov 20, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions packages/jest-worker/src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,9 @@ it('checks that once a sticked task finishes, next time is sent to that worker',
await promise;

// Note that the stickiness is not created by the method name or the arguments
// it is solely controlled by the provided "computeWorkerKey" method, which in the
// test example always returns the same key, so all calls should be redirected
// to worker 1 (which is the one that resolved the first call).
// it is solely controlled by the provided "computeWorkerKey" method, which in
// the test example always returns the same key, so all calls should be
// redirected to worker 1 (which is the one that resolved the first call).
farm.bar();

// The first time, a call with a "1234567890abcdef" hash had never been done
Expand Down Expand Up @@ -325,3 +325,42 @@ it('checks that once a non-sticked task finishes, next time is sent to all worke
expect(mockWorkers[1].send).toHaveBeenCalledTimes(2);
expect(mockWorkers[2].send).toHaveBeenCalledTimes(2);
});

it('rotates workers when they are idling', async () => {
let order;
let promise;

// Note there is no "computeWorkerKey".
const farm = new Farm('/tmp/baz.js', {
exposedMethods: ['foo', 'bar'],
numWorkers: 3,
});

[0, 1, 2].forEach(i => {
mockWorkers[i].send.mockReset();
mockWorkers[i].send.mockImplementation(() => order.push(i));
});

// First time, the order is 0, 1, 2.
order = [];
promise = farm.foo('car', 'plane');
expect(order).toEqual([0, 1, 2]);

// Worker 1 successfully replies with "17" as a result.
workerReply(1, null, 17);
await promise;

[0, 1, 2].forEach(i => {
mockWorkers[i].send.mockReset();
mockWorkers[i].send.mockImplementation(() => order.push(i));
});

// Now, the order is 1, 2, 0 (shifted one).
order = [];
promise = farm.foo('car', 'plane');
expect(order).toEqual([1, 2, 0]);

// Worker 1 successfully replies again.
workerReply(1, null, 17);
await promise;
});
9 changes: 7 additions & 2 deletions packages/jest-worker/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export default class {
_cacheKeys: {[string]: Worker, __proto__: null};
_options: FarmOptions;
_workers: Array<Worker>;
_offset: number;

constructor(workerPath: string, options?: FarmOptions = {}) {
const numWorkers = options.numWorkers || os.cpus().length - 1;
Expand Down Expand Up @@ -116,6 +117,7 @@ export default class {
this._cacheKeys = Object.create(null);
this._options = options;
this._workers = workers;
this._offset = 0;
}

getStdout(): Readable {
Expand Down Expand Up @@ -151,6 +153,7 @@ export default class {
return new Promise((resolve, reject) => {
const {computeWorkerKey} = this._options;
const workers = this._workers;
const length = workers.length;
const cacheKeys = this._cacheKeys;
const request = [CHILD_MESSAGE_CALL, false, method, args];

Expand Down Expand Up @@ -183,9 +186,11 @@ export default class {
}

// ... otherwise use all workers, so the first one available will pick it.
for (let i = 0; i < workers.length; i++) {
workers[i].send(request, callback);
for (let i = 0; i < length; i++) {
workers[(i + this._offset) % length].send(request, callback);
}

this._offset++;
});
}
}