"vscode:/vscode.git/clone" did not exist on "e02732be8841d199fc01de400fe869ddcb331efe"
main.ts 6.48 KB
Newer Older
liuzhe-lz's avatar
liuzhe-lz committed
1
2
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
Deshui Yu's avatar
Deshui Yu committed
3
4
5
6
7

'use strict';

import { Container, Scope } from 'typescript-ioc';

8
import * as fs from 'fs';
9
import * as component from './common/component';
Deshui Yu's avatar
Deshui Yu committed
10
11
import { Database, DataStore } from './common/datastore';
import { setExperimentStartupInfo } from './common/experimentStartupInfo';
chicm-ms's avatar
chicm-ms committed
12
import { getLogger, Logger, logLevelNameMap } from './common/log';
SparkSnail's avatar
SparkSnail committed
13
import { Manager, ExperimentStartUpMode } from './common/manager';
Deshui Yu's avatar
Deshui Yu committed
14
import { TrainingService } from './common/trainingService';
15
import { getLogDir, mkDirP, parseArg, uniqueString } from './common/utils';
Deshui Yu's avatar
Deshui Yu committed
16
17
18
import { NNIDataStore } from './core/nniDataStore';
import { NNIManager } from './core/nnimanager';
import { SqlDB } from './core/sqlDatabase';
19
import { NNIRestServer } from './rest_server/nniRestServer';
20
21
22
import { FrameworkControllerTrainingService } from './training_service/kubernetes/frameworkcontroller/frameworkcontrollerTrainingService';
import { KubeflowTrainingService } from './training_service/kubernetes/kubeflow/kubeflowTrainingService';
import { LocalTrainingService } from './training_service/local/localTrainingService';
23
24
import { PAIK8STrainingService } from './training_service/pai/paiK8S/paiK8STrainingService';
import { PAIYarnTrainingService } from './training_service/pai/paiYarn/paiYarnTrainingService';
Deshui Yu's avatar
Deshui Yu committed
25
26
27
28
import {
    RemoteMachineTrainingService
} from './training_service/remote_machine/remoteMachineTrainingService';

29
30
function initStartupInfo(
    startExpMode: string, resumeExperimentId: string, basePort: number,
SparkSnail's avatar
SparkSnail committed
31
32
    logDirectory: string, experimentLogLevel: string, readonly: boolean): void {
    const createNew: boolean = (startExpMode === ExperimentStartUpMode.NEW);
Deshui Yu's avatar
Deshui Yu committed
33
    const expId: string = createNew ? uniqueString(8) : resumeExperimentId;
SparkSnail's avatar
SparkSnail committed
34
    setExperimentStartupInfo(createNew, expId, basePort, logDirectory, experimentLogLevel, readonly);
Deshui Yu's avatar
Deshui Yu committed
35
36
}

SparkSnail's avatar
SparkSnail committed
37
async function initContainer(platformMode: string, logFileName?: string): Promise<void> {
Deshui Yu's avatar
Deshui Yu committed
38
    if (platformMode === 'local') {
39
40
41
        Container.bind(TrainingService)
            .to(LocalTrainingService)
            .scope(Scope.Singleton);
Deshui Yu's avatar
Deshui Yu committed
42
    } else if (platformMode === 'remote') {
43
44
45
        Container.bind(TrainingService)
            .to(RemoteMachineTrainingService)
            .scope(Scope.Singleton);
46
    } else if (platformMode === 'pai') {
47
        Container.bind(TrainingService)
48
49
50
51
52
            .to(PAIK8STrainingService)
            .scope(Scope.Singleton);
    } else if (platformMode === 'paiYarn') {
            Container.bind(TrainingService)
            .to(PAIYarnTrainingService)
53
            .scope(Scope.Singleton);
54
    } else if (platformMode === 'kubeflow') {
55
56
57
        Container.bind(TrainingService)
            .to(KubeflowTrainingService)
            .scope(Scope.Singleton);
58
    } else if (platformMode === 'frameworkcontroller') {
59
60
61
62
        Container.bind(TrainingService)
            .to(FrameworkControllerTrainingService)
            .scope(Scope.Singleton);
    } else {
chicm-ms's avatar
chicm-ms committed
63
        throw new Error(`Error: unsupported mode: ${platformMode}`);
Deshui Yu's avatar
Deshui Yu committed
64
    }
65
66
67
68
69
70
71
72
73
    Container.bind(Manager)
        .to(NNIManager)
        .scope(Scope.Singleton);
    Container.bind(Database)
        .to(SqlDB)
        .scope(Scope.Singleton);
    Container.bind(DataStore)
        .to(NNIDataStore)
        .scope(Scope.Singleton);
SparkSnail's avatar
SparkSnail committed
74
75
76
    Container.bind(Logger).provider({
        get: (): Logger => new Logger(logFileName)
    });
Deshui Yu's avatar
Deshui Yu committed
77
78
79
80
81
82
    const ds: DataStore = component.get(DataStore);

    await ds.init();
}

function usage(): void {
83
    console.info('usage: node main.js --port <port> --mode \
84
    <local/remote/pai/kubeflow/frameworkcontroller/paiYarn> --start_mode <new/resume> --experiment_id <id>');
Deshui Yu's avatar
Deshui Yu committed
85
86
87
}

const strPort: string = parseArg(['--port', '-p']);
goooxu's avatar
goooxu committed
88
89
90
if (!strPort || strPort.length === 0) {
    usage();
    process.exit(1);
Deshui Yu's avatar
Deshui Yu committed
91
92
}

goooxu's avatar
goooxu committed
93
94
const port: number = parseInt(strPort, 10);

Deshui Yu's avatar
Deshui Yu committed
95
const mode: string = parseArg(['--mode', '-m']);
96
if (!['local', 'remote', 'pai', 'kubeflow', 'frameworkcontroller', 'paiYarn'].includes(mode)) {
97
    console.log(`FATAL: unknown mode: ${mode}`);
Deshui Yu's avatar
Deshui Yu committed
98
99
100
101
102
    usage();
    process.exit(1);
}

const startMode: string = parseArg(['--start_mode', '-s']);
SparkSnail's avatar
SparkSnail committed
103
if (![ExperimentStartUpMode.NEW, ExperimentStartUpMode.RESUME].includes(startMode)) {
104
    console.log(`FATAL: unknown start_mode: ${startMode}`);
Deshui Yu's avatar
Deshui Yu committed
105
106
107
108
109
    usage();
    process.exit(1);
}

const experimentId: string = parseArg(['--experiment_id', '-id']);
SparkSnail's avatar
SparkSnail committed
110
111
if ((startMode === ExperimentStartUpMode.RESUME) && experimentId.trim().length < 1) {
    console.log(`FATAL: cannot resume the experiment, invalid experiment_id: ${experimentId}`);
Deshui Yu's avatar
Deshui Yu committed
112
113
114
115
    usage();
    process.exit(1);
}

116
117
118
119
120
121
122
123
const logDir: string = parseArg(['--log_dir', '-ld']);
if (logDir.length > 0) {
    if (!fs.existsSync(logDir)) {
        console.log(`FATAL: log_dir ${logDir} does not exist`);
    }
}

const logLevel: string = parseArg(['--log_level', '-ll']);
chicm-ms's avatar
chicm-ms committed
124
if (logLevel.length > 0 && !logLevelNameMap.has(logLevel)) {
125
126
127
    console.log(`FATAL: invalid log_level: ${logLevel}`);
}

SparkSnail's avatar
SparkSnail committed
128
129
130
131
132
133
134
135
136
const readonlyArg: string = parseArg(['--readonly', '-r']);
if (!('true' || 'false').includes(readonlyArg.toLowerCase())) {
    console.log(`FATAL: readonly property should only be true or false`);
    usage();
    process.exit(1);
}
const readonly = readonlyArg.toLowerCase() == 'true' ? true : false;

initStartupInfo(startMode, experimentId, port, logDir, logLevel, readonly);
Deshui Yu's avatar
Deshui Yu committed
137

138
139
mkDirP(getLogDir())
    .then(async () => {
Deshui Yu's avatar
Deshui Yu committed
140
141
    try {
        await initContainer(mode);
142
        const restServer: NNIRestServer = component.get(NNIRestServer);
143
        await restServer.start();
SparkSnail's avatar
SparkSnail committed
144
        const log: Logger = getLogger();
Deshui Yu's avatar
Deshui Yu committed
145
146
        log.info(`Rest server listening on: ${restServer.endPoint}`);
    } catch (err) {
SparkSnail's avatar
SparkSnail committed
147
        const log: Logger = getLogger();
Deshui Yu's avatar
Deshui Yu committed
148
        log.error(`${err.stack}`);
149
        throw err;
Deshui Yu's avatar
Deshui Yu committed
150
    }
151
152
})
.catch((err: Error) => {
Deshui Yu's avatar
Deshui Yu committed
153
154
    console.error(`Failed to create log dir: ${err.stack}`);
});
155

156
157
158
159
160
161
162
163
164
165
function getStopSignal(): any {
    if (process.platform === "win32") {
        return 'SIGBREAK';
    }
    else{
        return 'SIGTERM';
    }
}

process.on(getStopSignal(), async () => {
166
    const log: Logger = getLogger();
SparkSnail's avatar
SparkSnail committed
167
    let hasError: boolean = false;
168
    try {
SparkSnail's avatar
SparkSnail committed
169
170
171
172
173
174
        const nniManager: Manager = component.get(Manager);
        await nniManager.stopExperiment();
        const ds: DataStore = component.get(DataStore);
        await ds.close();
        const restServer: NNIRestServer = component.get(NNIRestServer);
        await restServer.stop();
175
    } catch (err) {
SparkSnail's avatar
SparkSnail committed
176
177
        hasError = true;
        log.error(`${err.stack}`);
178
    } finally {
SparkSnail's avatar
SparkSnail committed
179
        await log.close();
180
        process.exit(hasError ? 1 : 0);
SparkSnail's avatar
SparkSnail committed
181
    }
182
});