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

4
5
6
7
import fs from 'fs';
import path from 'path';
import * as component from 'common/component';
import { getLogger, Logger } from 'common/log';
8
import { EnvironmentInformation, EnvironmentService } from '../environment';
9
import { getLogLevel } from 'common/utils';
liuzhe-lz's avatar
liuzhe-lz committed
10
import { RemoteConfig, RemoteMachineConfig } from 'common/experimentConfig';
11
12
13
import { ExperimentStartupInfo } from 'common/experimentStartupInfo';
import { execMkdir } from 'training_service/common/util';
import { ExecutorManager } from 'training_service/remote_machine/remoteMachineData';
14
15
import { ShellExecutor } from 'training_service/remote_machine/shellExecutor';
import { RemoteMachineEnvironmentInformation } from '../remote/remoteConfig';
16
17
import { SharedStorageService } from '../sharedStorage';
import { createScriptFile } from 'common/shellUtils';
18
19
20
21
22

@component.Singleton
export class RemoteEnvironmentService extends EnvironmentService {

    private readonly initExecutorId = "initConnection";
23
    private readonly machineExecutorManagerMap: Map<RemoteMachineConfig, ExecutorManager>;
24
    private readonly environmentExecutorManagerMap: Map<string, ExecutorManager>;
25
    private readonly remoteMachineMetaOccupiedMap: Map<RemoteMachineConfig, boolean>;
26
    private readonly log: Logger;
liuzhe-lz's avatar
liuzhe-lz committed
27
    private sshConnectionPromises: Promise<void[]>;
28
    private experimentRootDir: string;
SparkSnail's avatar
SparkSnail committed
29
    private remoteExperimentRootDir: string = "";
30
    private experimentId: string;
liuzhe-lz's avatar
liuzhe-lz committed
31
    private config: RemoteConfig;
32

liuzhe-lz's avatar
liuzhe-lz committed
33
    constructor(config: RemoteConfig, info: ExperimentStartupInfo) {
34
        super();
35
        this.experimentId = info.experimentId;
36
        this.environmentExecutorManagerMap = new Map<string, ExecutorManager>();
37
38
        this.machineExecutorManagerMap = new Map<RemoteMachineConfig, ExecutorManager>();
        this.remoteMachineMetaOccupiedMap = new Map<RemoteMachineConfig, boolean>();
39
        this.experimentRootDir = info.logDir;
liuzhe-lz's avatar
liuzhe-lz committed
40
        this.log = getLogger('RemoteEnvironmentService');
liuzhe-lz's avatar
liuzhe-lz committed
41
        this.config = config;
42
43
44
45
46
47

        // codeDir is not a valid directory, throw Error
        if (!fs.lstatSync(this.config.trialCodeDirectory).isDirectory()) {
            throw new Error(`codeDir ${this.config.trialCodeDirectory} is not a directory`);
        }

liuzhe-lz's avatar
liuzhe-lz committed
48
        this.sshConnectionPromises = Promise.all(this.config.machineList.map(
49
            machine => this.initRemoteMachineOnConnected(machine)
liuzhe-lz's avatar
liuzhe-lz committed
50
51
52
53
54
55
56
57
58
59
        ));
    }

    public async init(): Promise<void> {
        await this.sshConnectionPromises;
        this.log.info('ssh connection initialized!');
        Array.from(this.machineExecutorManagerMap.keys()).forEach(rmMeta => {
            // initialize remoteMachineMetaOccupiedMap, false means not occupied
            this.remoteMachineMetaOccupiedMap.set(rmMeta, false);
        });
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
    }

    public get prefetchedEnvironmentCount(): number {
        return this.machineExecutorManagerMap.size;
    }

    public get environmentMaintenceLoopInterval(): number {
        return 5000;
    }

    public get hasMoreEnvironments(): boolean {
        return false;
    }

    public get hasStorageService(): boolean {
        return false;
    }

78
79
80
81
    public get getName(): string {
        return 'remote';
    }

82
    private scheduleMachine(): RemoteMachineConfig | undefined {
83
84
85
86
87
88
89
90
91
        for (const [rmMeta, occupied] of this.remoteMachineMetaOccupiedMap) {
            if (!occupied) {
                this.remoteMachineMetaOccupiedMap.set(rmMeta, true);
                return rmMeta;
            }
        }
        return undefined;
    }

92
    private async initRemoteMachineOnConnected(rmMeta: RemoteMachineConfig): Promise<void> {
93
        const executorManager: ExecutorManager = new ExecutorManager(rmMeta);
94
        this.log.info(`connecting to ${rmMeta.user}@${rmMeta.host}:${rmMeta.port}`);
95
96
97
98
99
100
        const executor: ShellExecutor = await executorManager.getExecutor(this.initExecutorId);
        this.log.debug(`reached ${executor.name}`);
        this.machineExecutorManagerMap.set(rmMeta, executorManager);
        this.log.debug(`initializing ${executor.name}`);

        // Create root working directory after executor is ready
101
        const nniRootDir: string = executor.joinPath(executor.getTempPath(), 'nni-experiments');
102
        await executor.createFolder(executor.getRemoteExperimentRootDir(this.experimentId));
103
104

        // the directory to store temp scripts in remote machine
105
        const remoteGpuScriptCollectorDir: string = executor.getRemoteScriptsPath(this.experimentId);
106
107
108
109
110

        // clean up previous result.
        await executor.createFolder(remoteGpuScriptCollectorDir, true);
        await executor.allowPermission(true, nniRootDir);
    }
111
112

    public async refreshEnvironmentsStatus(environments: EnvironmentInformation[]): Promise<void> {	
113
        const tasks = environments.map(environment => this.refreshEnvironment(environment));
114
115
116
        await Promise.all(tasks);	
    }

117
118
    private async refreshEnvironment(environment: EnvironmentInformation): Promise<void> {
        const executor = await this.getExecutor(environment.id);
SparkSnail's avatar
SparkSnail committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
        const jobpidPath: string = `${environment.runnerWorkingFolder}/pid`;
        const runnerReturnCodeFilePath: string = `${environment.runnerWorkingFolder}/code`;
        /* eslint-disable require-atomic-updates */
        try {
            // check if pid file exist
            const pidExist = await executor.fileExist(jobpidPath);
            if (!pidExist) {
                return;
            }
            const isAlive = await executor.isProcessAlive(jobpidPath);
            environment.status = 'RUNNING';
            // if the process of jobpid is not alive any more
            if (!isAlive) {
                const remoteEnvironment: RemoteMachineEnvironmentInformation = environment as RemoteMachineEnvironmentInformation;
                if (remoteEnvironment.rmMachineMeta === undefined) {
                    throw new Error(`${remoteEnvironment.id} machine meta not initialized!`);
                }
136
                this.log.info(`pid in ${remoteEnvironment.rmMachineMeta.host}:${jobpidPath} is not alive!`);
SparkSnail's avatar
SparkSnail committed
137
138
139
140
141
142
143
144
145
146
147
                if (fs.existsSync(runnerReturnCodeFilePath)) {
                    const runnerReturnCode: string = await executor.getRemoteFileContent(runnerReturnCodeFilePath);
                    const match: RegExpMatchArray | null = runnerReturnCode.trim()
                        .match(/^-?(\d+)\s+(\d+)$/);
                    if (match !== null) {
                        const { 1: code } = match;
                        // Update trial job's status based on result code
                        if (parseInt(code, 10) === 0) {
                            environment.setStatus('SUCCEEDED');
                        } else {
                            environment.setStatus('FAILED');
148
                        }
149
                        await this.releaseEnvironmentResource(environment);
150
151
152
                    }
                }
            }
SparkSnail's avatar
SparkSnail committed
153
154
155
        } catch (error) {
            this.log.error(`Update job status exception, error is ${error.message}`);
        }
156
157
158
159
160
161
    }

    /**
     * If a environment is finished, release the connection resource
     * @param environment remote machine environment job detail
     */
162
163
164
165
166
167
168
169
170
171
    private async releaseEnvironmentResource(environment: EnvironmentInformation): Promise<void> {
        if (environment.useSharedStorage) {
            const executor = await this.getExecutor(environment.id);
            const remoteUmountCommand = component.get<SharedStorageService>(SharedStorageService).remoteUmountCommand;
            const result = await executor.executeScript(remoteUmountCommand, false, false);
            if (result.exitCode !== 0) {
                this.log.error(`Umount shared storage on remote machine failed.\n ERROR: ${result.stderr}`);
            }
        }

172
173
174
175
176
177
178
179
180
181
182
183
184
185
        const executorManager = this.environmentExecutorManagerMap.get(environment.id);
        if (executorManager === undefined) {
            throw new Error(`ExecutorManager is not assigned for environment ${environment.id}`);
        }

        // Note, it still keep reference in trialExecutorManagerMap, as there may be following requests from nni manager.
        executorManager.releaseExecutor(environment.id);
        const remoteEnvironment: RemoteMachineEnvironmentInformation = environment as RemoteMachineEnvironmentInformation;
        if (remoteEnvironment.rmMachineMeta === undefined) {
            throw new Error(`${remoteEnvironment.id} rmMachineMeta not initialized!`);
        }
        this.remoteMachineMetaOccupiedMap.set(remoteEnvironment.rmMachineMeta, false);
    }

Ni Hao's avatar
Ni Hao committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
    private async getScript(environment: EnvironmentInformation): Promise<string> {
        const executor = await this.getExecutor(environment.id);
        const isDebug = getLogLevel() == "debug";
        let script: string = environment.command;
        environment.runnerWorkingFolder = executor.joinPath(this.remoteExperimentRootDir, 'envs', environment.id);

        let codeScript = `echo $? \`date +%s%3N\` >${environment.runnerWorkingFolder}/code`;
        if (executor.isWindows) {
            const prepare = `mkdir envs\\${environment.id} 2>NUL & cd envs\\${environment.id}`;
            const startrun = `powershell ..\\install_nni.ps1 && python -m nni.tools.trial_tool.trial_runner`;
            const developingScript = "IF EXIST nni_trial_tool (ECHO \"nni_trial_tool exists already\") ELSE (mkdir nni_trial_tool && tar -xof ../nni_trial_tool.tar.gz -C ./nni_trial_tool) && pip3 install websockets";

            script = isDebug ? `${prepare} && ${developingScript} && ${startrun}` : `${prepare} && ${startrun}`;
            codeScript = `powershell -command "Write $? " " (((New-TimeSpan -Start (Get-Date "01/01/1970") -End (Get-Date).ToUniversalTime()).TotalMilliseconds).ToString("0")) | Out-file ${path.join(environment.runnerWorkingFolder, 'code')} -Append -NoNewline -encoding utf8"`;
        }

        script = `cd ${this.remoteExperimentRootDir} && \
            ${script} --job_pid_file ${environment.runnerWorkingFolder}/pid \
            1>${environment.runnerWorkingFolder}/trialrunner_stdout 2>${environment.runnerWorkingFolder}/trialrunner_stderr \
            && ${codeScript}`;

        return script;
    }

210
211
212
213
214
215
216
217
218
219
220
    public async startEnvironment(environment: EnvironmentInformation): Promise<void> {
        const remoteEnvironment: RemoteMachineEnvironmentInformation = environment as RemoteMachineEnvironmentInformation;
        remoteEnvironment.status = 'WAITING';
        // schedule machine for environment, generate command
        await this.prepareEnvironment(remoteEnvironment);
        // launch runner process in machine
        await this.launchRunner(environment);
    }

    private async prepareEnvironment(environment: RemoteMachineEnvironmentInformation): Promise<boolean> {
        // get an executor from scheduler
221
        const rmMachineMeta: RemoteMachineConfig | undefined = this.scheduleMachine();
222
223
224
225
226
227
228
229
230
231
232
        if (rmMachineMeta === undefined) {
            this.log.warning(`No available machine!`);
            return Promise.resolve(false);
        } else {
            environment.rmMachineMeta = rmMachineMeta;
            const executorManager: ExecutorManager | undefined = this.machineExecutorManagerMap.get(environment.rmMachineMeta);
            if (executorManager === undefined) {
                throw new Error(`executorManager not initialized`);
            }
            this.environmentExecutorManagerMap.set(environment.id, executorManager);
            const executor = await this.getExecutor(environment.id);
233
            if (environment.useSharedStorage) {
SparkSnail's avatar
SparkSnail committed
234
                this.remoteExperimentRootDir = component.get<SharedStorageService>(SharedStorageService).remoteWorkingRoot;
J-shang's avatar
J-shang committed
235
236
237
238
                if (!this.remoteExperimentRootDir.startsWith('/')) {
                    this.remoteExperimentRootDir = executor.joinPath((await executor.getCurrentPath()).trim(), this.remoteExperimentRootDir);
                }
                const remoteMountCommand = component.get<SharedStorageService>(SharedStorageService).remoteMountCommand.replace(/echo -e /g, `echo `).replace(/echo /g, `echo -e `).replace(/\\\$/g, `\\\\\\$`);
239
240
241
242
                const result = await executor.executeScript(remoteMountCommand, false, false);
                if (result.exitCode !== 0) {
                    throw new Error(`Mount shared storage on remote machine failed.\n ERROR: ${result.stderr}`);
                }
243
            } else {
244
                this.remoteExperimentRootDir = executor.getRemoteExperimentRootDir(this.experimentId);
245
            }
Ni Hao's avatar
Ni Hao committed
246
247

            environment.command = await this.getScript(environment);
248
            environment.useActiveGpu = rmMachineMeta.useActiveGpu;
249
250
251
252
253
254
255
            return Promise.resolve(true);
        }
    }

    private async launchRunner(environment: RemoteMachineEnvironmentInformation): Promise<void> {
        const executor = await this.getExecutor(environment.id);
        const environmentLocalTempFolder: string =  
SparkSnail's avatar
SparkSnail committed
256
            path.join(this.experimentRootDir, "environment-temp")
257
258
        await executor.createFolder(environment.runnerWorkingFolder);
        await execMkdir(environmentLocalTempFolder);
259
260
        await createScriptFile(path.join(environmentLocalTempFolder, executor.getScriptName("run")),
                environment.command);
261
        // Copy files in codeDir to remote working directory
SparkSnail's avatar
SparkSnail committed
262
        await executor.copyDirectoryToRemote(environmentLocalTempFolder, this.remoteExperimentRootDir);
263
        // Execute command in remote machine, set isInteractive=true to run script in conda environment
Ni Hao's avatar
Ni Hao committed
264
        executor.executeScript(executor.joinPath(this.remoteExperimentRootDir,
265
            executor.getScriptName("run")), true, true);
266
267
268
        if (environment.rmMachineMeta === undefined) {
            throw new Error(`${environment.id} rmMachineMeta not initialized!`);
        }
269
        environment.trackingUrl = `file://${environment.rmMachineMeta.host}:${environment.runnerWorkingFolder}`;
270
271
272
273
274
275
276
277
278
279
280
    }

    private async getExecutor(environmentId: string): Promise<ShellExecutor> {
        const executorManager = this.environmentExecutorManagerMap.get(environmentId);
        if (executorManager === undefined) {
            throw new Error(`ExecutorManager is not assigned for environment ${environmentId}`);
        }
        return await executorManager.getExecutor(environmentId);
    }

    public async stopEnvironment(environment: EnvironmentInformation): Promise<void> {
281
        if (environment.isAlive === false) {
282
            return;
283
284
        }

285
286
287
288
        const executor = await this.getExecutor(environment.id);

        if (environment.status === 'UNKNOWN') {
            environment.status = 'USER_CANCELED';
289
            await this.releaseEnvironmentResource(environment);
290
            return;
291
292
293
294
295
        }

        const jobpidPath: string = `${environment.runnerWorkingFolder}/pid`;
        try {
            await executor.killChildProcesses(jobpidPath);
296
            await this.releaseEnvironmentResource(environment);
297
298
299
300
301
        } catch (error) {
            this.log.error(`stopEnvironment: ${error}`);
        }
    }
}