Skip to content
This repository was archived by the owner on Sep 26, 2023. It is now read-only.

Commit 0836862

Browse files
authored
Allow using temporary directory to speed up (#72)
1 parent c6a904e commit 0836862

12 files changed

Lines changed: 99 additions & 23 deletions

package-lock.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@techgreedy/singularity",
3-
"version": "0.0.14",
3+
"version": "0.0.15",
44
"description": "Large scale client tool with capabilities on onboard PB-scale data to Filecoin network",
55
"main": "dist/source/start.js",
66
"types": "dist/source/start.d.ts",
@@ -67,7 +67,7 @@
6767
"@goodware/task-queue": "^2.1.3",
6868
"@gzuidhof/go-npm": "^0.1.13",
6969
"@rauschma/stringio": "^1.4.0",
70-
"@techgreedy/generate-car": "^1.1.1",
70+
"@techgreedy/generate-car": "1.2.0",
7171
"async-retry": "^1.3.3",
7272
"await-exec": "^0.1.2",
7373
"axios": "^0.26.1",

src/common/Datastore.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ export default class Datastore {
197197
dataCid: Schema.Types.String,
198198
carSize: Schema.Types.Number,
199199
pieceCid: Schema.Types.String,
200-
pieceSize: Schema.Types.Number
200+
pieceSize: Schema.Types.Number,
201+
filenameOverride: Schema.Types.String,
202+
tmpDir: Schema.Types.String
201203
}, {
202204
timestamps: true
203205
});
@@ -219,7 +221,8 @@ export default class Datastore {
219221
index: 1
220222
},
221223
status: Schema.Types.String,
222-
errorMessage: Schema.Types.String
224+
errorMessage: Schema.Types.String,
225+
tmpDir: Schema.Types.String
223226
}, {
224227
timestamps: true
225228
});

src/common/model/GenerationRequest.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ export default interface GenerationRequest {
1212
carSize?: number,
1313
pieceCid?: string,
1414
pieceSize?: number,
15-
filenameOverride?: string // when the car name is different from cid
15+
filenameOverride?: string, // when the car name is different from cid
16+
tmpDir?: string
1617
}

src/common/model/ScanningRequest.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ export default interface ScanningRequest {
1010
maxSize: number,
1111
workerId?: string,
1212
status: 'active' | 'completed' | 'error' | 'paused',
13-
errorMessage?: string
13+
errorMessage?: string,
14+
tmpDir?: string
1415
}

src/deal-preparation/CreatePreparationRequest.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ export default interface CreatePreparationRequest {
55
outDir: string,
66
minRatio?: number,
77
maxRatio?: number,
8+
tmpDir?: string
89
}

src/deal-preparation/DealPreparationService.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ export default class DealPreparationService extends BaseService {
8585
this.logger.warn(`${dir} cannot be read during cleanup.`);
8686
}
8787
}
88+
let tmpDirs = (await Datastore.ScanningRequestModel.find()).map(r => r.tmpDir);
89+
tmpDirs = [...new Set(tmpDirs)];
90+
for (const dir of tmpDirs) {
91+
if (dir) {
92+
try {
93+
await fs.rm(dir, { recursive: true, force: true });
94+
this.logger.info(`Removing temporary folder ${dir}`);
95+
} catch (e) {
96+
this.logger.warn(`${dir} cannot be read during cleanup.`);
97+
}
98+
}
99+
}
88100
}
89101

90102
private async cleanupHealthCheck (): Promise<void> {
@@ -385,7 +397,8 @@ export default class DealPreparationService extends BaseService {
385397
dealSize,
386398
outDir,
387399
minRatio,
388-
maxRatio
400+
maxRatio,
401+
tmpDir
389402
} = <CreatePreparationRequest>request.body;
390403
this.logger.info(`Received request to start preparing dataset.`, { name, path, dealSize });
391404
const dealSizeNumber = xbytes.parseSize(dealSize);
@@ -420,6 +433,9 @@ export default class DealPreparationService extends BaseService {
420433
try {
421434
await fs.access(path, constants.F_OK);
422435
await fs.access(outDir, constants.F_OK);
436+
if (tmpDir) {
437+
await fs.access(tmpDir, constants.F_OK);
438+
}
423439
} catch (_) {
424440
this.sendError(response, ErrorCode.PATH_NOT_ACCESSIBLE);
425441
return;
@@ -432,6 +448,7 @@ export default class DealPreparationService extends BaseService {
432448
scanningRequest.path = path;
433449
scanningRequest.status = 'active';
434450
scanningRequest.outDir = outDir;
451+
scanningRequest.tmpDir = tmpDir;
435452
try {
436453
await scanningRequest.save();
437454
} catch (e: any) {
@@ -448,6 +465,7 @@ export default class DealPreparationService extends BaseService {
448465
maxSize,
449466
path,
450467
outDir,
468+
tmpDir,
451469
status: scanningRequest.status
452470
}));
453471
}

src/deal-preparation/DealPreparationWorker.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export default class DealPreparationWorker extends BaseService {
6363
datasetName: request.name,
6464
path: request.path,
6565
outDir: request.outDir,
66+
tmpDir: request.tmpDir,
6667
index,
6768
status: 'created'
6869
});
@@ -96,15 +97,27 @@ export default class DealPreparationWorker extends BaseService {
9697

9798
private async generate (request: GenerationRequest, input: string): Promise<[stdout: string, stderr: string, statusCode: number | null]> {
9899
await fs.mkdir(request.outDir, { recursive: true });
99-
this.logger.debug(`Spawning generate-car.`, { outPath: request.outDir, parentPath: request.path });
100-
const [stdout, stderr, exitCode] = await DealPreparationWorker.invokeGenerateCar(input, request.outDir, request.path);
100+
this.logger.debug(`Spawning generate-car.`, { outPath: request.outDir, parentPath: request.path, tmpDir: request.tmpDir });
101+
let tmpDir: string | undefined;
102+
if (request.tmpDir) {
103+
tmpDir = path.join(request.tmpDir, randomUUID());
104+
}
105+
const [stdout, stderr, exitCode] = await DealPreparationWorker.invokeGenerateCar(input, request.outDir, request.path, tmpDir);
106+
if (tmpDir) {
107+
await fs.rm(tmpDir, { recursive: true, force: true });
108+
}
101109
this.logger.debug(`Child process finished.`, { stdout, stderr, exitCode });
102110
return [stdout, stderr, exitCode];
103111
}
104112

105-
public static async invokeGenerateCar (input: string, outDir: string, p: string): Promise<[stdout: string, stderr: string, statusCode: number | null]> {
113+
public static async invokeGenerateCar (input: string, outDir: string, p: string, tmpDir?: string)
114+
: Promise<[stdout: string, stderr: string, statusCode: number | null]> {
106115
const cmd = await fs.pathExists(path.join(__dirname, 'generate-car')) ? path.join(__dirname, 'generate-car') : 'generate-car';
107-
const child = spawn(cmd, ['-o', outDir, '-p', p], {
116+
const args = ['-o', outDir, '-p', p];
117+
if (tmpDir) {
118+
args.push('-t', tmpDir);
119+
}
120+
const child = spawn(cmd, args, {
108121
stdio: ['pipe', 'pipe', 'pipe']
109122
});
110123
(async () => {

src/singularity-prepare.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { GeneratedFileList } from './common/model/OutputFileList';
1313
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
1414
// @ts-ignore
1515
import TaskQueue from '@goodware/task-queue';
16+
import { randomUUID } from 'crypto';
1617

1718
const version = packageJson.version;
1819
const program = new Command();
@@ -24,6 +25,7 @@ program.name('singularity-prepare')
2425
.argument('<outDir>', 'The output Directory to save CAR files and manifest files')
2526
.requiredOption('-l, --url-prefix <urlPrefix>', 'The prefix of the download link, which will be followed by datacid.car, i.e. http://download.mysite.org/')
2627
.option('-s, --deal-size <deal_size>', 'Target deal size, i.e. 32GiB', '32 GiB')
28+
.option('-t, --tmp-dir <tmp_dir>', 'Temporary directory, may be useful when dataset source is slow, such as on S3 mount or NFS')
2729
.addOption(new Option('-m, --min-ratio <min_ratio>', 'Min ratio of deal to sector size, i.e. 0.55').default('0.55').argParser(parseFloat))
2830
.addOption(new Option('-M, --max-ratio <max_ratio>', 'Max ratio of deal to sector size, i.e. 0.95').default('0.95').argParser(parseFloat))
2931
.addOption(new Option('-j, --parallel <parallel>', 'How many generation jobs to run at the same time').default('1'))
@@ -76,7 +78,16 @@ program.name('singularity-prepare')
7678
End: file.end
7779
})));
7880

79-
const [stdout, stderr, exitCode] = await DealPreparationWorker.invokeGenerateCar(input, outDir, p);
81+
let tmpDir: string | undefined;
82+
if (options.tmpDir) {
83+
tmpDir = path.join(options.tmpDir, randomUUID());
84+
}
85+
const [stdout, stderr, exitCode] = await DealPreparationWorker.invokeGenerateCar(input, outDir, p, tmpDir);
86+
87+
if (tmpDir) {
88+
await fs.rm(tmpDir, { recursive: true, force: true });
89+
}
90+
8091
if (exitCode !== 0) {
8192
console.error(stderr);
8293
}

src/singularity.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ preparation.command('create').description('Start deal preparation for a local da
165165
.argument('<datasetPath>', 'Directory path to the dataset')
166166
.argument('<outDir>', 'The output Directory to save CAR files')
167167
.option('-s, --deal-size <deal_size>', 'Target deal size, i.e. 32GiB', '32 GiB')
168+
.option('-t, --tmp-dir <tmp_dir>', 'Temporary directory, may be useful when dataset source is slow, such as on S3 mount or NFS')
168169
.addOption(new Option('-m, --min-ratio <min_ratio>', 'Min ratio of deal to sector size, i.e. 0.55').argParser(parseFloat))
169170
.addOption(new Option('-M, --max-ratio <max_ratio>', 'Max ratio of deal to sector size, i.e. 0.95').argParser(parseFloat))
170171
.action(async (name, p, outDir, options) => {
@@ -182,7 +183,8 @@ preparation.command('create').description('Start deal preparation for a local da
182183
dealSize: dealSize,
183184
outDir: path.resolve(outDir),
184185
minRatio: options.minRatio,
185-
maxRatio: options.maxRatio
186+
maxRatio: options.maxRatio,
187+
tmpDir: options.tmpDir
186188
});
187189
} catch (error) {
188190
CliUtil.renderErrorAndExit(error);

0 commit comments

Comments
 (0)