Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
28 changes: 28 additions & 0 deletions packages/compass-user-data/src/semaphore.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { expect } from 'chai';
import { Semaphore } from './semaphore';

describe('semaphore', function () {
const maxConcurrentOps = 5;
let semaphore: Semaphore;
let taskHandler: (id: number) => Promise<number>;

beforeEach(() => {
semaphore = new Semaphore(maxConcurrentOps);
taskHandler = async (id: number) => {
const release = await semaphore.waitForRelease();
const delay = Math.floor(Math.random() * 450) + 50;
try {
await new Promise((resolve) => setTimeout(resolve, delay));
return id;
} finally {
release();
}
};
});

it('should run operations concurrently', async function () {
const tasks = Array.from({ length: 10 }, (_, i) => taskHandler(i));
const results = await Promise.all(tasks);
expect(results).to.have.lengthOf(10);
});
});
27 changes: 27 additions & 0 deletions packages/compass-user-data/src/semaphore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export class Semaphore {
private currentCount = 0;
private queue: (() => void)[] = [];
constructor(private maxConcurrentOps: number) {}

waitForRelease(): Promise<() => void> {
return new Promise((resolve) => {
const attempt = () => {
this.currentCount++;
resolve(this.release.bind(this));
};
if (this.currentCount < this.maxConcurrentOps) {
attempt();
} else {
this.queue.push(attempt);
}
});
}

private release() {
this.currentCount--;
if (this.queue.length > 0) {
const next = this.queue.shift();
next && next();
}
}
}
14 changes: 14 additions & 0 deletions packages/compass-user-data/src/user-data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,20 @@ describe('user-data', function () {
expect(mongoshData?.[1]).to.be.instanceOf(Stats);
}
});

it('reads many number of files', async function () {
const files = Array.from({ length: 10000 }, (_, i) => [
`data${i}.json`,
JSON.stringify({ name: `VSCode${i}` }),
]);

await Promise.all(
files.map(([filepath, data]) => writeFileToStorage(filepath, data))
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this test also run the risk of running into file descriptor limitations?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah that's a good point. i'll remove this test.


const result = await getUserData().readAll();
expect(result.data).to.have.lengthOf(10000);
});
});

context('UserData.readOne', function () {
Expand Down
5 changes: 5 additions & 0 deletions packages/compass-user-data/src/user-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createLogger } from '@mongodb-js/compass-logging';
import { getStoragePath } from '@mongodb-js/compass-utils';
import type { z } from 'zod';
import writeFile from 'write-file-atomic';
import { Semaphore } from './semaphore';

const { log, mongoLogId } = createLogger('COMPASS-USER-STORAGE');

Expand Down Expand Up @@ -68,6 +69,7 @@ export class UserData<T extends z.Schema> {
private readonly serialize: SerializeContent<z.input<T>>;
private readonly deserialize: DeserializeContent;
private readonly getFileName: GetFileName;
private readonly semaphore = new Semaphore(1000);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Node.js thread pool for fs operations (UV_THREADPOOL_SIZE) has a default size of 4 or so. I think a lower number, say 50 or 100, would also be fine here and result in similar performance while at the same time still being significantly better at reducing the risk of EMFILE/ENFILE errors


constructor(
private readonly validator: T,
Expand Down Expand Up @@ -122,7 +124,9 @@ export class UserData<T extends z.Schema> {
let data: string;
let stats: Stats;
let handle: fs.FileHandle | undefined = undefined;
let release: (() => void) | undefined = undefined;
try {
release = await this.semaphore.waitForRelease();
handle = await fs.open(absolutePath, 'r');
[stats, data] = await Promise.all([
handle.stat(),
Expand All @@ -139,6 +143,7 @@ export class UserData<T extends z.Schema> {
throw error;
} finally {
await handle?.close();
release?.();
}

try {
Expand Down
Loading