trialDispatcher.ts 45.7 KB
Newer Older
1
2
3
4
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { EventEmitter } from 'events';
5
6
import fs from 'fs';
import path from 'path';
7
import { Writable } from 'stream';
8
import { Container, Scope } from 'typescript-ioc';
9
import { String } from 'typescript-string-operations';
10
11
12
13
14
15
16
17
18
import * as component from 'common/component';
import { NNIError, NNIErrorNames, MethodNotImplementedError } from 'common/errors';
import { getBasePort, getExperimentId } from 'common/experimentStartupInfo';
import { getLogger, Logger } from 'common/log';
import { TrainingService, TrialJobApplicationForm, TrialJobMetric, TrialJobStatus } from 'common/trainingService';
import { delay, getExperimentRootDir, getIPV4Address, getLogLevel, getVersion, mkDirPSync, randomSelect, uniqueString } from 'common/utils';
import { ExperimentConfig, SharedStorageConfig } from 'common/experimentConfig';
import { GPU_INFO, INITIALIZED, KILL_TRIAL_JOB, NEW_TRIAL_JOB, REPORT_METRIC_DATA, SEND_TRIAL_JOB_PARAMETER, STDOUT, TRIAL_END, VERSION_CHECK } from 'core/commands';
import { ScheduleResultType } from 'training_service/common/gpuData';
19
import { CONTAINER_INSTALL_NNI_SHELL_FORMAT } from '../common/containerJobData';
Ni Hao's avatar
Ni Hao committed
20
import { CONTAINER_INSTALL_NNI_SHELL_FORMAT_FOR_WIN } from '../common/containerJobData';
21
22
23
import { TrialConfig } from '../common/trialConfig';
import { validateCodeDir } from '../common/util';
import { Command, CommandChannel } from './commandChannel';
J-shang's avatar
J-shang committed
24
import { EnvironmentInformation, EnvironmentService, NodeInformation, RunnerSettings, TrialGpuSummary } from './environment';
25
import { createEnvironmentService } from './environments/environmentServiceFactory';
26
import { GpuScheduler } from './gpuScheduler';
SparkSnail's avatar
SparkSnail committed
27
import { MountedStorageService } from './storages/mountedStorageService';
28
import { StorageService } from './storageService';
29
import { SharedStorageService } from './sharedStorage';
30
31
import { NFSSharedStorageService } from './shared_storages/nfsStorageService'
import { AzureBlobSharedStorageService } from './shared_storages/azureblobStorageService'
32
33
34
35
36
37
38
39
import { TrialDetail } from './trial';

/**
 * It uses to manage jobs on training platforms 
 * and expose trial as trial job to upper level.
**/
@component.Singleton
class TrialDispatcher implements TrainingService {
40
41
    private log: Logger;
    private isDeveloping: boolean = false;
42
43
    private stopping: boolean = false;

44
45
46
    private metricsEmitter: EventEmitter;
    private experimentId: string;
    private experimentRootDir: string;
47
48
49
50
51

    private enableVersionCheck: boolean = true;

    private trialConfig: TrialConfig | undefined;

52
53
    private trials: Map<string, TrialDetail>;
    private environments: Map<string, EnvironmentInformation>;
54
55
56
57
58
59
    // make public for ut
    public environmentServiceList: EnvironmentService[] = [];
    public commandChannelSet: Set<CommandChannel>;
    public commandEmitter: EventEmitter;
    public environmentMaintenceLoopInterval: number = -1;

60
61
62
63
64
65
66
67
68
    // uses to accelerate trial manager loop
    // true means there is updates, and trial loop should run a cycle immediately.
    private shouldUpdateTrials: boolean = true;
    // uses to decide environment assign strategy.
    // true means use gpu scheduler to decide if there is free resource for new trial.
    // false means one env run one trial in same time.
    private enableGpuScheduler: boolean = false;
    // uses to save if user like to reuse environment
    private reuseEnvironment: boolean = true;
69
    private logCollection: string = 'none';
70
71
72
73
74
75
76

    private gpuScheduler: GpuScheduler;

    // uses to reduce log count.
    private isLoggedNoMoreEnvironment: boolean = false;
    private isLoggedNoGpuAvailable: boolean = false;

77
78
79
80
    // uses to mark whether to use shared storage
    private useSharedStorage: boolean = false;
    private fileCopyCompleted: boolean = false;

81
82
    private config: ExperimentConfig;

83
84
85
86
87
88
89
90
    public static async construct(config: ExperimentConfig): Promise<TrialDispatcher> {
        const instance = new TrialDispatcher(config);
        await instance.asyncConstructor(config);
        return instance;
    }

    private constructor(config: ExperimentConfig) {
        this.log = getLogger('TrialDispatcher');
91
92
93
94
        this.trials = new Map<string, TrialDetail>();
        this.environments = new Map<string, EnvironmentInformation>();
        this.metricsEmitter = new EventEmitter();
        this.experimentId = getExperimentId();
SparkSnail's avatar
SparkSnail committed
95
        this.experimentRootDir = getExperimentRootDir();
96
        this.commandChannelSet = new Set<CommandChannel>();
97
98
99
100
101
102
103
104

        const logLevel = getLogLevel();
        this.log.debug(`current folder ${__dirname}`);
        // different source folder in Linux and Windows
        if (logLevel == "debug" && (fs.existsSync("../../../src/nni_manager") || __dirname.endsWith("src\\nni_manager\\dist\\training_service\\reusable"))) {
            this.log.debug("log level is debug, and exist code folder, so set to developing mode.");
            this.isDeveloping = true;
        }
105

106
107
        this.commandEmitter = new EventEmitter();

108
        this.gpuScheduler = new GpuScheduler();
109
110
111
112
113
114
115

        this.config = config;

        this.enableGpuScheduler = !!config.trialGpuNumber;
        if (this.enableGpuScheduler) {
            this.log.info(`TrialDispatcher: GPU scheduler is enabled.`)
        }
116
    }
117

118
119
    private async asyncConstructor(config: ExperimentConfig): Promise<void> {
        await validateCodeDir(config.trialCodeDirectory);
120

121
        const serviceConfigs = Array.isArray(config.trainingService) ? config.trainingService : [ config.trainingService ];
122
        const servicePromises = serviceConfigs.map(serviceConfig => createEnvironmentService(serviceConfig));
123
        this.environmentServiceList = await Promise.all(servicePromises);
124
125
126
127
128
129
130
131
132
133
134

        this.environmentMaintenceLoopInterval = Math.max(
            ...this.environmentServiceList.map((env) => env.environmentMaintenceLoopInterval)
        );

        for (const env of this.environmentServiceList) {
            env.initCommandChannel(this.commandEmitter);
            this.commandChannelSet.add(env.getCommandChannel);
        }

        if (this.config.sharedStorage !== undefined) {
135
            await this.initializeSharedStorage(this.config.sharedStorage);
136
        }
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
    }

    public async listTrialJobs(): Promise<TrialDetail[]> {
        const trials: TrialDetail[] = [];

        for (const key of this.trials.keys()) {
            trials.push(await this.getTrialJob(key));
        }

        return trials;
    }

    public async getTrialJob(trialJobId: string): Promise<TrialDetail> {
        const trial: TrialDetail | undefined = this.trials.get(trialJobId);
        if (trial === undefined) {
            throw new Error(`trial job ${trialJobId} not found`);
        }

        return trial;
    }

Yuge Zhang's avatar
Yuge Zhang committed
158
    public async getTrialFile(_trialJobId: string, _fileName: string): Promise<string | Buffer> {
159
160
161
        throw new MethodNotImplementedError();
    }

162
163
164
    public async submitTrialJob(form: TrialJobApplicationForm): Promise<TrialDetail> {
        const trialId: string = uniqueString(5);

165
        const trialJobDetail: TrialDetail = new TrialDetail(trialId, "WAITING", Date.now(), "", form);
166
167
168
169
170
171
172
173
174
175
176
177
178

        this.trials.set(trialId, trialJobDetail);

        return trialJobDetail;
    }

    // to support multi phase
    public async updateTrialJob(trialJobId: string, form: TrialJobApplicationForm): Promise<TrialDetail> {
        const trialDetail = await this.getTrialJob(trialJobId);
        const environment = trialDetail.environment;
        if (environment === undefined) {
            throw new Error(`TrialDispatcher: trial ${trialJobId}'s env shouldn't be undefined in updateTrialJob.`);
        }
179
180
        if (environment.environmentService === undefined) {
            throw new Error(`Environment ${environment.id} does not assigned environment service.`);
181
182
183
184
185
186
        }

        const message = {
            "trialId": trialJobId,
            "parameters": form.hyperParameters,
        }
187
        await environment.environmentService.getCommandChannel.sendCommand(environment, SEND_TRIAL_JOB_PARAMETER, message);
188
189
190
191
192
193
194
195
196
197
198
199

        return trialDetail;
    }

    public async cancelTrialJob(trialJobId: string, isEarlyStopped?: boolean | undefined): Promise<void> {
        const trial = await this.getTrialJob(trialJobId);
        switch (trial.status) {
            case "RUNNING":
            case "WAITING":
            case "UNKNOWN":
                {
                    const environment = trial.environment;
200
201
                    if (environment && environment.environmentService) {
                        await environment.environmentService.getCommandChannel.sendCommand(environment, KILL_TRIAL_JOB, trial.id);
202
203
204
205
206
207
208
209
210
211
                        trial.isEarlyStopped = isEarlyStopped;
                        trial.status = trial.isEarlyStopped === true ?
                            'EARLY_STOPPED' : 'USER_CANCELED';
                        this.releaseEnvironment(trial);
                    }
                }
                break;
        }
    }

SparkSnail's avatar
SparkSnail committed
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
    private getStorageService(environmentService: EnvironmentService): StorageService {
        let storageService: StorageService;
        if (this.useSharedStorage) {
            this.log.debug(`TrialDispatcher: use shared storage service.`);
            storageService = component.get<SharedStorageService>(SharedStorageService).storageService;
        } else if (environmentService.hasStorageService) {
            this.log.debug(`TrialDispatcher: use existing storage service.`);
            storageService = component.get<StorageService>(StorageService);
        } else {
            this.log.debug(`TrialDispatcher: create temp storage service to temp folder.`);
            storageService = new MountedStorageService();
            const environmentLocalTempFolder = path.join(this.experimentRootDir, "environment-temp");
            storageService.initialize(this.config.trialCodeDirectory, environmentLocalTempFolder);
        }
        return storageService;
    }
228
    public async run(): Promise<void> {
liuzhe-lz's avatar
liuzhe-lz committed
229
        await Promise.all(this.environmentServiceList.map(env => env.init()));
230
231
        for(const environmentService of this.environmentServiceList) {
            
SparkSnail's avatar
SparkSnail committed
232
            
233
234
235
236

            await environmentService.getCommandChannel.start();
            this.log.info(`TrialDispatcher: started channel: ${environmentService.getCommandChannel.constructor.name}`);
    
SparkSnail's avatar
SparkSnail committed
237
            this.log.info(`TrialDispatcher: copying code.`);
238
239
240
241
            if (this.useSharedStorage) {
                if (this.fileCopyCompleted) {
                    continue;
                }
242
            }
SparkSnail's avatar
SparkSnail committed
243
244
            const storageService: StorageService = this.getStorageService(environmentService);

245
            // Copy the compressed file to remoteDirectory and delete it
246
            const codeDir = path.resolve(this.config.trialCodeDirectory);
247
248
249
250
            const envDir = storageService.joinPath("envs");
            const codeFileName = await storageService.copyDirectory(codeDir, envDir, true);
            storageService.rename(codeFileName, "nni-code.tar.gz");

Ni Hao's avatar
Ni Hao committed
251
252
            const installFileName = storageService.joinPath(envDir, `install_nni.sh`);
            const installFileNameForWin = storageService.joinPath(envDir, `install_nni.ps1`);
253
            await storageService.save(CONTAINER_INSTALL_NNI_SHELL_FORMAT, installFileName);
Ni Hao's avatar
Ni Hao committed
254
            await storageService.save(CONTAINER_INSTALL_NNI_SHELL_FORMAT_FOR_WIN, installFileNameForWin);
255
256
257
258
259
260
261
262

            if (this.isDeveloping) {
                let trialToolsPath = path.join(__dirname, "../../../../../tools/nni_trial_tool");
                if (false === fs.existsSync(trialToolsPath)) {
                    trialToolsPath = path.join(__dirname, "..\\..\\..\\..\\..\\tools\\nni_trial_tool");
                }
                await storageService.copyDirectory(trialToolsPath, envDir, true);
            }
263
264
265
266

            if (this.useSharedStorage) {
                this.fileCopyCompleted = true;
            }
267
        }
268
269
270
271
272
273
        // start channel
        this.commandEmitter.on("command", (command: Command): void => {
            this.handleCommand(command).catch((err: Error) => {
                this.log.error(`TrialDispatcher: error on handle env ${command.environment.id} command: ${command.command}, data: ${command.data}, error: ${err}`);
            })
        });
274
        await this.prefetchEnvironments();
275
        this.log.info(`TrialDispatcher: run loop started.`);
276
277
278
279
280
281
282
        const promiseList: Promise<void>[] = [];
        for(const commandChannel of this.commandChannelSet) {
            promiseList.push(commandChannel.run());
        }
        promiseList.push(this.environmentMaintenanceLoop());
        promiseList.push(this.trialManagementLoop());
        await Promise.all(promiseList);
283
284
285
286
287
288
289
290
291
292
    }

    public addTrialJobMetricListener(listener: (metric: TrialJobMetric) => void): void {
        this.metricsEmitter.on('metric', listener);
    }

    public removeTrialJobMetricListener(listener: (metric: TrialJobMetric) => void): void {
        this.metricsEmitter.off('metric', listener);
    }

293
294
    public async setClusterMetadata(_key: string, _value: string): Promise<void> { return; }
    public async getClusterMetadata(_key: string): Promise<string> { return ""; }
295

296
297
298
299
300
301
302
303
304
305
    public async stopEnvironment(environment: EnvironmentInformation): Promise<void> {
        if (environment.environmentService === undefined) {
            throw new Error(`${environment.id} do not have environmentService!`);
        }
        this.log.info(`stopping environment ${environment.id}...`);
        await environment.environmentService.stopEnvironment(environment);
        this.log.info(`stopped environment ${environment.id}.`);
        return;
    }

306
307
308
309
310
    public async cleanUp(): Promise<void> {
        if (this.commandEmitter === undefined) {
            throw new Error(`TrialDispatcher: commandEmitter shouldn't be undefined in cleanUp.`);
        }
        this.stopping = true;
311
        this.shouldUpdateTrials = true;
312
        const environments = [...this.environments.values()];
313
314
        
        const stopEnvironmentPromise: Promise<void>[] = []; 
315
        for (let index = 0; index < environments.length; index++) {
316
            stopEnvironmentPromise.push(this.stopEnvironment(environments[index]));
317
        }
318
        await Promise.all(stopEnvironmentPromise);
319
        this.commandEmitter.off("command", this.handleCommand);
320
321
322
        for (const commandChannel of this.commandChannelSet) {
            await commandChannel.stop();
        }
323
324
325
326
327
        if (this.useSharedStorage) {
            this.log.info(`stopping shared storage...`)
            await component.get<SharedStorageService>(SharedStorageService).cleanUp();
            this.log.info(`shared storage stopped.`)
        }
328
329
330
331
332
333
334
335
336
    }

    private async environmentMaintenanceLoop(): Promise<void> {
        while (!this.stopping) {
            const environments: EnvironmentInformation[] = [];
            for (const environment of this.environments.values()) {
                if (environment.isAlive === true) {
                    environments.push(environment);
                } else {
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
                    if (environment.environmentService === undefined) {
                        throw new Error(`${environment.id} do not have environment service!`);
                    }
                    await environment.environmentService.getCommandChannel.close(environment);
                }
            }
            // Group environments according to environmentService
            const environmentServiceDict: Map<EnvironmentService, EnvironmentInformation[]> =
                new Map<EnvironmentService, EnvironmentInformation[]>();
            for (const environment of environments) {
                if (environment.environmentService === undefined) {
                    throw new Error(`${environment.id} do not have environment service!`);
                }
                if (!environmentServiceDict.has(environment.environmentService)) {
                    environmentServiceDict.set(environment.environmentService, [environment]);
                } else {
                    const environmentsList: EnvironmentInformation[] | undefined = environmentServiceDict.get(environment.environmentService);
                    if (environmentsList === undefined) {
                        throw new Error(`Environment list not initialized!`);
                    }
                    environmentsList.push(environment);
                    environmentServiceDict.set(environment.environmentService, environmentsList);
                }
            }
            // Refresh all environments
            const taskList: Promise<void>[] = [];
            for (const environmentService of environmentServiceDict.keys()) {
                const environmentsList: EnvironmentInformation[] | undefined = environmentServiceDict.get(environmentService);
                if (environmentsList) {
                    taskList.push(environmentService.refreshEnvironmentsStatus(environmentsList));
367
368
                }
            }
369
            await Promise.all(taskList);
370

371
372
373
374
            for (const environment of environments) {
                if (environment.environmentService === undefined) {
                    throw new Error(`${environment.id} do not have environment service!`);
                }
375
376
377
378
379
380
381
382
383
384
385
386
387
388
                const oldIsAlive = environment.isAlive;
                switch (environment.status) {
                    case 'WAITING':
                    case 'RUNNING':
                    case 'UNKNOWN':
                        environment.isAlive = true;
                        break;
                    default:
                        environment.isAlive = false;
                        break;
                }
                if (oldIsAlive !== environment.isAlive) {
                    this.log.debug(`set environment ${environment.id} isAlive from ${oldIsAlive} to ${environment.isAlive} due to status is ${environment.status}.`);
                }
389
            }
390
            this.shouldUpdateTrials = true;
391
392
393
394
            if (this.environmentMaintenceLoopInterval === -1) {
                throw new Error("EnvironmentMaintenceLoopInterval not initialized!");
            }
            await delay(this.environmentMaintenceLoopInterval);
395
396
397
398
        }
    }

    private async trialManagementLoop(): Promise<void> {
399
        const interval = 1;
400
401

        while (!this.stopping) {
402
403
404
405
406
407
408
409
410
            let totalInterval = 1000;
            while (totalInterval > 0) {
                if (this.shouldUpdateTrials) {
                    this.shouldUpdateTrials = false;
                    break;
                }
                totalInterval -= interval;
                await delay(interval);
            }
411
412
413
414
415
416
417
418
419
420
421
422

            const toRefreshedTrials: TrialDetail[] = [];
            for (const trial of this.trials.values()) {
                if (trial.status === "RUNNING" || trial.status === "WAITING" || trial.status === "UNKNOWN") {
                    toRefreshedTrials.push(trial);
                }
            }

            if (toRefreshedTrials.length == 0) {
                continue;
            }

423
            let waitingTrials: TrialDetail[] = [];
424
425
426
427
428
429
430
431
432
433
434
435
436
437
            let liveTrialsCount = 0;
            for (const trial of toRefreshedTrials) {
                const currentStatus = trial.status;
                switch (currentStatus) {
                    case "RUNNING":
                        {
                            const environment = trial.environment;

                            if (environment === undefined) {
                                this.log.error(`found running trial ${trial.id} has no environment, set trial to UNKNOWN.`);
                                trial.status = "UNKNOWN";
                                liveTrialsCount++;
                                continue;
                            }
438
439
440
441

                            if (environment.environmentService === undefined) {
                                throw new Error(`${environment.id} does not has environment service!`);
                            }
SparkSnail's avatar
SparkSnail committed
442
                            trial.url = environment.trackingUrl;
443
444
445
446
447
448
449
450
451
452
453
454
455
                            const environmentStatus = environment.status;

                            // any node exit, then make sure the whole trial stopped.
                            if (trial.nodes.size > 0) {
                                const completedCount = trial.nodes.size;
                                let finalStatus: TrialJobStatus = "SUCCEEDED";
                                let lastTimestamp: number | undefined;
                                this.log.debug(`found ${completedCount} completed trial node(s), nodeCount: ${environment.nodeCount}`);

                                // if some trial processes doesn't exit, kill it for next one.
                                // for example, in horovod, it's just sleep command, has no impact on trial result.
                                if (environment.nodeCount > completedCount) {
                                    this.log.info(`stop partial completed trial ${trial.id}`);
456
                                    await environment.environmentService.getCommandChannel.sendCommand(environment, KILL_TRIAL_JOB, trial.id);
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
                                }
                                for (const node of trial.nodes.values()) {
                                    if (node.status === "FAILED") {
                                        finalStatus = "FAILED";
                                    }
                                    if (node.endTime !== undefined) {
                                        if (lastTimestamp === undefined) {
                                            lastTimestamp = node.endTime
                                        } else {
                                            lastTimestamp = Math.max(node.endTime, lastTimestamp);
                                        }
                                    }
                                }
                                trial.status = finalStatus;
                                if (lastTimestamp === undefined) {
                                    trial.endTime = lastTimestamp;
                                }
                                this.releaseEnvironment(trial);
                            } else if (environmentStatus !== "RUNNING") {
476
                                this.log.error(`found running trial ${trial.id} on '${environment.envId}' with '${environmentStatus}', set trial to environment status.`);
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
                                this.releaseEnvironment(trial);
                                trial.status = environmentStatus;
                            } else {
                                liveTrialsCount++;
                            }
                        }
                        break;
                    case "WAITING":
                    case "UNKNOWN":
                        // deal it later, if there is free environment.
                        waitingTrials.push(trial);
                        liveTrialsCount++;
                        break;
                }
            }
492

493
            let liveEnvironmentsCount = 0;
494
495
            const reusableEnvironments: EnvironmentInformation[] = [];
            for (const environment of this.environments.values()) {
496
497
                if (environment.isAlive === true) {
                    liveEnvironmentsCount++;
498
499
                    if (environment.status === "RUNNING" && environment.isRunnerReady) {
                        // if environment is not reusable and used, stop and not count as idle;
liuzhe-lz's avatar
liuzhe-lz committed
500
                        const reuseMode = Array.isArray(this.config.trainingService) || (this.config.trainingService as any).reuseMode;
501
502
                        if (
                            0 === environment.runningTrialCount &&
liuzhe-lz's avatar
liuzhe-lz committed
503
                            reuseMode === false &&
504
505
                            environment.assignedTrialCount > 0
                        ) {
506
507
508
509
                            if (environment.environmentService === undefined) {
                                throw new Error(`${environment.id} does not has environment service!`);
                            }
                            await environment.environmentService.stopEnvironment(environment);
510
511
512
513
514
515
516
517
518
                            continue;
                        }

                        // if gpu scheduler is not enabled, and there is running trial, skip it.
                        if (false === this.enableGpuScheduler && environment.runningTrialCount > 0) {
                            continue;
                        }

                        reusableEnvironments.push(environment);
519
520
                    }
                }
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
            }

            let neededEnvironmentCount = 0;
            if (true === this.enableGpuScheduler) {
                let noGpuAvailable: boolean = false;
                while (waitingTrials.length > 0) {
                    // skip following trials, if first trial doesn't find available GPU.
                    if (true === noGpuAvailable) {
                        // break loop to try next time.
                        break;
                    }
                    const trial = waitingTrials.shift();
                    if (undefined === trial) {
                        throw new Error(`TrialDispatcher: waiting trial shouldn't be undefined!`);
                    }
536
537
                    const defaultGpuNum = this.config.trialGpuNumber;
                    const result = this.gpuScheduler.scheduleMachine(reusableEnvironments, trial.form.placementConstraint!, defaultGpuNum, trial);
538
539
540
541
542
543
544
545
546
                    switch (result.resultType) {
                        case ScheduleResultType.REQUIRE_EXCEED_TOTAL:
                            {
                                if (liveEnvironmentsCount == 0) {
                                    this.log.debug(`TrialDispatcher: no live environment, so request one.`);
                                    neededEnvironmentCount = 1;
                                    waitingTrials = [];
                                    this.isLoggedNoGpuAvailable = false;
                                } else if (reusableEnvironments.length > 0) {
547
                                    const errorMessage: string = `TrialDispatcher: REQUIRE_EXCEED_TOTAL Required GPU number ${defaultGpuNum} is too large, no machine can meet`;
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
                                    this.log.error(errorMessage);
                                    throw new NNIError(NNIErrorNames.RESOURCE_NOT_AVAILABLE, errorMessage);
                                } else {
                                    if (false === this.isLoggedNoGpuAvailable) {
                                        this.log.debug(`TrialDispatcher: wait GPU, live environment ${liveEnvironmentsCount}, no reusable, REQUIRE_EXCEED_TOTAL.`)
                                        this.isLoggedNoGpuAvailable = true;
                                    }
                                }
                                break;
                            }
                        case ScheduleResultType.TMP_NO_AVAILABLE_GPU:
                            {
                                if (false === this.isLoggedNoGpuAvailable) {
                                    this.log.debug(`TrialDispatcher: wait GPU, live environment ${liveEnvironmentsCount}, reusable ${reusableEnvironments.length}, TMP_NO_AVAILABLE_GPU.`)
                                    this.isLoggedNoGpuAvailable = true;
                                }

                                // if some environment is alive, but not ready, no need to create more.
                                if (liveEnvironmentsCount <= reusableEnvironments.length) {
                                    neededEnvironmentCount = 1;
                                    this.isLoggedNoGpuAvailable = false;
                                    this.log.info(`TrialDispatcher: ${liveEnvironmentsCount} live env, and ${reusableEnvironments.length} reusable, but no GPU available so request a new one.`);
                                }
                                noGpuAvailable = true;
                            }
                            break
                        case ScheduleResultType.SUCCEED:
                            {
                                const environment = result.environment;
                                if (undefined === environment) {
                                    throw new Error(`TrialDispatcher: scheduled env shouldn't be undefined!`);
                                }
                                trial.assignedGpus = result.gpuIndices;
                                await this.allocateEnvironment(trial, environment);
                                this.isLoggedNoGpuAvailable = false;
                            }
                            break
                        default:
                            throw new Error(`TrialDispatcher: Unknown gpu schecduler type: ${result.resultType}`);
                    }
                }
            } else {
                while (reusableEnvironments.length > 0 && waitingTrials.length > 0) {
                    const trial = waitingTrials.shift();
                    const idleEnvironment = reusableEnvironments.shift();
                    if (trial !== undefined && idleEnvironment != undefined) {
                        await this.allocateEnvironment(trial, idleEnvironment);
                    }
596
                }
597
                neededEnvironmentCount = liveTrialsCount - liveEnvironmentsCount;
598
599
            }

600
601
            if (neededEnvironmentCount > 0) {
                let requestedCount = 0;
602
                let hasMoreEnvironments = false;
603
                for (let index = 0; index < neededEnvironmentCount; index++) {
604
605
606
607
                    const environmentService: EnvironmentService | undefined = this.selectEnvironmentService();
                    if (environmentService !== undefined) {
                        hasMoreEnvironments = true;
                        await this.requestEnvironment(environmentService);
608
609
610
611
612
613
614
615
616
                        requestedCount++;
                        this.isLoggedNoMoreEnvironment = false;
                    } else {
                        if (this.isLoggedNoMoreEnvironment === false) {
                            this.isLoggedNoMoreEnvironment = true;
                            this.log.info(`no more environment so far, so skip to request environment.`)
                        }
                    }
                }
617
                if (hasMoreEnvironments === true || requestedCount > 0) {
618
619
620
                    this.log.info(`requested new environment, live trials: ${liveTrialsCount}, ` +
                        `live environments: ${liveEnvironmentsCount}, neededEnvironmentCount: ${neededEnvironmentCount}, ` +
                        `requestedCount: ${requestedCount}`);
621
622
                }
            }
623

624
625
        }
    }
626

627
628
629
630
631
632
633
634
635
636
    // Schedule a environment platform for environment
    private selectEnvironmentService(): EnvironmentService | undefined {
        const validEnvironmentServiceList = [];
        for(const environmentService of this.environmentServiceList){
            if (environmentService.hasMoreEnvironments) {
                validEnvironmentServiceList.push(environmentService);
            }
        }
        if (validEnvironmentServiceList.length === 0) {
            return undefined;
637
        }
638
639
        // Random scheduler
        return randomSelect(validEnvironmentServiceList);
640
    }
641

642
643
644
645
646
647
648
    private async prefetchEnvironments (): Promise<void> {
        for (const environmentService of this.environmentServiceList) {
            const number = environmentService.prefetchedEnvironmentCount;
            this.log.info(`Initialize environments total number: ${number}`);
            for (let index = 0; index < number; index++) {
                await this.requestEnvironment(environmentService);
            }
649
        }
650
    }
651

SparkSnail's avatar
SparkSnail committed
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
    private async setEnvironmentSetting(environment: EnvironmentInformation): Promise<void> {
        if (environment.environmentService === undefined) {
            throw new Error(`Environmentservice for ${environment.id} not initialized!`);
        }
        const environmentService = environment.environmentService;
        const runnerSettings: RunnerSettings = new RunnerSettings();
        runnerSettings.nniManagerIP = this.config.nniManagerIp === undefined? await getIPV4Address() : this.config.nniManagerIp;
        runnerSettings.nniManagerPort = getBasePort() + 1;
        runnerSettings.commandChannel = environmentService.getCommandChannel.channelName;
        runnerSettings.enableGpuCollector = this.enableGpuScheduler;
        runnerSettings.command = this.config.trialCommand;
        runnerSettings.nniManagerVersion = this.enableVersionCheck ? await getVersion() : '';
        runnerSettings.logCollection = this.logCollection;
        runnerSettings.platform = environmentService.getName;
        runnerSettings.experimentId = this.experimentId;
        const storageService: StorageService = this.getStorageService(environmentService);
        const envDir = storageService.joinPath("envs");
        const runnerSettingsConfig = storageService.joinPath(envDir, environment.id, "settings.json");
        await storageService.save(JSON.stringify(runnerSettings), runnerSettingsConfig);
    }

673
    private async requestEnvironment(environmentService: EnvironmentService): Promise<void> {
674
675
676
677
        if (this.stopping) {
            this.log.info(`Experiment is stopping, stop creating new environment`);
            return;
        }
678
679
        const envId = uniqueString(5);
        const envName = `nni_exp_${this.experimentId}_env_${envId}`;
J-shang's avatar
J-shang committed
680
        const environment = environmentService.createEnvironmentInformation(envId, envName);
681
682
        environment.environmentService = environmentService;
        this.log.info(`Assign environment service ${environmentService.getName} to environment ${envId}`);
683
        environment.command = `sh ../install_nni.sh && python3 -m nni.tools.trial_tool.trial_runner`;
684
685
686
687
688

        if (this.isDeveloping) {
            environment.command = "[ -d \"nni_trial_tool\" ] && echo \"nni_trial_tool exists already\" || (mkdir ./nni_trial_tool && tar -xof ../nni_trial_tool.tar.gz -C ./nni_trial_tool) && pip3 install websockets && " + environment.command;
        }

SparkSnail's avatar
SparkSnail committed
689
        environment.command = `mkdir -p envs/${envId} && cd envs/${envId} && ${environment.command}`;
690

691
        environment.useSharedStorage = this.useSharedStorage;
SparkSnail's avatar
SparkSnail committed
692
693
        // Generate setting.json file per environment to avoid conflict
        await this.setEnvironmentSetting(environment);
694

695
        await environmentService.startEnvironment(environment);
SparkSnail's avatar
SparkSnail committed
696
        this.environments.set(environment.id, environment);
697
698
699

        if (environment.status === "FAILED") {
            environment.isAlive = false;
700
            throw new Error(`error on request environment ${environment.envId}, please check log for more details.`);
701
702
703
        } else {
            environment.isAlive = true;
        }
704
        await environment.environmentService.getCommandChannel.open(environment);
705
        this.log.info(`requested environment ${environment.id} and job id is ${environment.envId}.`);
706
707
    }

708
    private async allocateEnvironment(trial: TrialDetail, environment: EnvironmentInformation): Promise<void> {
709
        if (trial.environment) {
710
            throw new Error(`TrialDispatcher: trial ${trial.id} has assigned environment ${trial.environment.id} already, not assign to ${environment.id}!`);
711
        }
712
713
        if (environment.runningTrialCount > 0 && false === this.enableGpuScheduler) {
            throw new Error(`TrialDispatcher: environment ${environment.id} has running trial, and gpu scheduler is not enabled, it cannot be assigned again!`);
714
715
716
        }
        this.log.info(`assigning environment ${environment.id} to trial ${trial.id}.`);

717
718
719
        // convert assigned gpus to string for nvidia visible settings
        // undefined means no constraint, [] means no gpu visible.
        let gpuIndices: string | undefined = undefined;
720
        if (undefined !== this.config.trialGpuNumber) {
721
722
723
724
725
726
727
728
729
730
731
            const gpuArray: number[] = [];
            if (undefined !== trial.assignedGpus) {
                trial.assignedGpus.map((value) => {
                    gpuArray.push(value.index);
                });
            }
            gpuIndices = gpuArray.join(',');
        }

        environment.runningTrialCount++;
        environment.assignedTrialCount++;
732
        trial.environment = environment;
733
734
735
        if (environment.environmentService === undefined) {
            throw new Error(`${environment.id} environmentService not initialized!`);
        }
736
        trial.message = `Platform: ${environment.environmentService.getName}, environment: ${environment.id}`;
J-shang's avatar
J-shang committed
737
738
739
740
741
        if (this.useSharedStorage) {
            const storageService = component.get<SharedStorageService>(SharedStorageService).storageService;
            trial.workingDirectory = storageService.joinPath('trials', trial.id);
        } else if (environment.environmentService.hasStorageService) {	
            const storageService = component.get<StorageService>(StorageService);
742
743
            trial.workingDirectory = storageService.joinPath('trials', trial.id);
        }	
744
745
        trial.settings = {
            trialId: trial.id,
746
            gpuIndices: gpuIndices,
747
748
749
750
751
            sequenceId: trial.form.sequenceId,
            parameter: trial.form.hyperParameters,
        }
        trial.startTime = Date.now();
        trial.status = "RUNNING";
752
753
754
755
        if (environment.environmentService === undefined) {
            throw new Error(`${environment.id} does not have environment service!`);
        }
        await environment.environmentService.getCommandChannel.sendCommand(trial.environment, NEW_TRIAL_JOB, trial.settings);
756
    }
SparkSnail's avatar
SparkSnail committed
757
758
759
760
761
    
    /**
     * release the trial assigned environment resources
     * @param trial 
     */
762
    private releaseEnvironment(trial: TrialDetail): void {
763
764
765
        if (true === this.enableGpuScheduler) {
            this.gpuScheduler.removeGpuReservation(trial);
        }
SparkSnail's avatar
SparkSnail committed
766
767
768
769
770
        if (trial.environment !== undefined) {
            if (trial.environment.runningTrialCount <= 0) {
                throw new Error(`TrialDispatcher: environment ${trial.environment.id} has no counted running trial!`);
            }
            trial.environment.runningTrialCount--;
771
            trial.environment.latestTrialReleasedTime = Date.now();
SparkSnail's avatar
SparkSnail committed
772
            trial.environment = undefined;
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
        }
    }

    private async handleMetricData(trialId: string, data: any): Promise<void> {
        if (Array.isArray(data)) {
            for (const subItem of data) {
                this.metricsEmitter.emit('metric', {
                    id: trialId,
                    data: subItem
                });
            }
        } else {
            this.metricsEmitter.emit('metric', {
                id: trialId,
                data: data
            });
        }
    }

    private async handleStdout(commandData: any): Promise<void> {
793
        const metricPattern: RegExp = /NNISDK_MEb'(?<metrics>.*a?)'$/gm;
794
795
796
797
798
799
        const trialLogDir: string = path.join(getExperimentRootDir(), 'trials', commandData["trial"]);
        mkDirPSync(trialLogDir);
        const trialLogPath: string = path.join(trialLogDir, 'stdout_log_collection.log');
        try {
            let skipLogging: boolean = false;
            if (commandData["tag"] === 'trial' && commandData["msg"] !== undefined) {
800
801
802
                const message: string = commandData["msg"];
                let metricsContent = metricPattern.exec(message);
                while (metricsContent && metricsContent.groups) {
803
804
                    const key: string = 'metrics';
                    const data = metricsContent.groups[key];
805
806
                    await this.handleMetricData(commandData["trial"], data);
                    metricsContent = metricPattern.exec(message);
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
                    skipLogging = true;
                }
            }

            if (!skipLogging) {
                // Construct write stream to write remote trial's log into local file
                const writeStream: Writable = fs.createWriteStream(trialLogPath, {
                    flags: 'a+',
                    encoding: 'utf8',
                    autoClose: true
                });

                writeStream.write(String.Format('{0}\n', commandData["msg"]));
                writeStream.end();
            }
        } catch (err) {
            this.log.error(`TrialDispatcher: handleStdout error: ${err}`);
        }
    }

    private async handleCommand(command: Command): Promise<void> {
828
        this.log.debug(`TrialDispatcher: env ${command.environment.id} received command ${command.command}.`);
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
        const environment = command.environment;
        const data = command.data;
        const nodeId = data["node"];
        switch (command.command) {
            case REPORT_METRIC_DATA:
                this.log.error(`TrialDispatcher: TODO: not implement to handle direct REPORT_METRIC_DATA command yet.`);
                break;
            case STDOUT:
                await this.handleStdout(data);
                break;
            case INITIALIZED:
                {
                    let isAllReady = true;
                    if (environment.nodeCount > 1) {
                        let node = environment.nodes.get(nodeId);
                        if (node === undefined) {
J-shang's avatar
J-shang committed
845
                            node = new NodeInformation(nodeId);
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
                            environment.nodes.set(nodeId, node);
                        }
                        const oldNodeStatus = node.status;
                        if (oldNodeStatus === "UNKNOWN" || oldNodeStatus === "WAITING") {
                            node.status = "RUNNING";
                        }

                        if (environment.nodes.size === environment.nodeCount) {
                            for (const node of environment.nodes.values()) {
                                if (node.status !== "RUNNING") {
                                    isAllReady = false;
                                    break;
                                }
                            }
                        } else {
                            isAllReady = false;
                        }
                    }

                    // single node is always ready to set env status
866
867
868
                    if (isAllReady) {
                        environment.isRunnerReady = true;
                        this.log.info(`TrialDispatcher: env ${environment.id} received initialized message and runner is ready, env status: ${environment.status}.`);
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
                    }
                }
                break;
            case VERSION_CHECK:
                {
                    if (this.enableVersionCheck) {
                        const checkResultSuccess: boolean = data["tag"] === 'VCSuccess' ? true : false;
                        if (checkResultSuccess) {
                            this.log.info(`TrialDispatcher: Version check in trialKeeper success!`);
                        } else {
                            const errorMessage = `TrialDispatcher: Version check error, ${data["msg"]}!`;
                            this.log.error(errorMessage);
                        }
                    }
                }
                break;
            case GPU_INFO:
886
887
888
889
                {
                    const gpuData = <TrialGpuSummary>(data);
                    environment.setGpuSummary(nodeId, gpuData);
                }
890
891
892
893
894
895
896
897
898
899
900
901
902
903
                break;
            case TRIAL_END:
                {
                    const trialId = data["trial"];
                    const trial = await this.getTrialJob(trialId);
                    const code = parseInt(data["code"]);
                    const timestamp = parseInt(data["time"]);
                    let exitStatus: TrialJobStatus = "SUCCEEDED";
                    if (code !== 0) {
                        exitStatus = "FAILED";
                    }

                    let node = environment.nodes.get(nodeId);
                    if (node === undefined) {
J-shang's avatar
J-shang committed
904
                        node = new NodeInformation(nodeId);
905
906
907
908
909
910
911
912
913
914
                        trial.nodes.set(nodeId, node);
                    }
                    if (undefined === node) {
                        throw new Error("node is impossible to be undefined (see above code), but make eslint happy!");
                    }
                    node.status = exitStatus;
                    node.endTime = timestamp;
                }
                break;
        }
915
        this.shouldUpdateTrials = true;
916
    }
917

918
919
    private async initializeSharedStorage(config: SharedStorageConfig): Promise<void> {
        switch (config.storageType) {
920
921
922
923
924
925
926
927
928
929
930
            case 'NFS':
                Container.bind(SharedStorageService)
                         .to(NFSSharedStorageService)
                         .scope(Scope.Singleton);
                break;
            case 'AzureBlob':
                Container.bind(SharedStorageService)
                         .to(AzureBlobSharedStorageService)
                         .scope(Scope.Singleton);
                break;
            default: {
931
                const errorMessage = `Shared storage type '${config.storageType}' not support.`;
932
933
934
935
                this.log.error(errorMessage)
                return Promise.reject(errorMessage);
            }
        }
936
        await component.get<SharedStorageService>(SharedStorageService).config(config);
937
938
939
        this.useSharedStorage = true;
        return Promise.resolve();
    }
J-shang's avatar
J-shang committed
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962

    public async getTrialOutputLocalPath(trialJobId: string): Promise<string> {
        // TODO: support non shared storage
        if (this.useSharedStorage) {
            const localWorkingRoot = component.get<SharedStorageService>(SharedStorageService).localWorkingRoot;
            return Promise.resolve(path.join(localWorkingRoot, 'trials', trialJobId));
        } else {
            return Promise.reject(new Error('Only support shared storage right now.'));
        }
    }

    public async fetchTrialOutput(trialJobId: string, subpath: string | undefined): Promise<void> {
        // TODO: support non shared storage
        let trialLocalPath = await this.getTrialOutputLocalPath(trialJobId);
        if (subpath !== undefined) {
            trialLocalPath = path.join(trialLocalPath, subpath);
        }
        if (fs.existsSync(trialLocalPath)) {
            return Promise.resolve();
        } else {
            return Promise.reject(new Error('Trial local path not exist.'));
        }
    }
963
964
965
}

export { TrialDispatcher };