remoteMachineData.ts 4.79 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

'use strict';

6
import { TrialJobApplicationForm, TrialJobDetail, TrialJobStatus } from '../../common/trainingService';
7
import { GPUInfo, GPUSummary, ScheduleResultType } from '../common/gpuData';
8
import { ShellExecutor } from './shellExecutor';
Deshui Yu's avatar
Deshui Yu committed
9
10
11
12
13

/**
 * Metadata of remote machine for configuration and statuc query
 */
export class RemoteMachineMeta {
chicm-ms's avatar
chicm-ms committed
14
15
16
    public readonly ip: string = '';
    public readonly port: number = 22;
    public readonly username: string = '';
17
    public readonly passwd: string = '';
18
19
    public readonly sshKeyPath?: string;
    public readonly passphrase?: string;
chicm-ms's avatar
chicm-ms committed
20
    public gpuSummary: GPUSummary | undefined;
21
    public readonly gpuIndices?: string;
22
    public readonly maxTrialNumPerGpu?: number;
23
24
    //TODO: initialize varialbe in constructor
    public occupiedGpuIndexMap?: Map<number, number>;
25
    public readonly useActiveGpu?: boolean = false;
26
27
}

Deshui Yu's avatar
Deshui Yu committed
28
29
30
31
/**
 * The execution result for command executed on remote machine
 */
export class RemoteCommandResult {
chicm-ms's avatar
chicm-ms committed
32
33
34
    public readonly stdout: string;
    public readonly stderr: string;
    public readonly exitCode: number;
Deshui Yu's avatar
Deshui Yu committed
35

chicm-ms's avatar
chicm-ms committed
36
    constructor(stdout: string, stderr: string, exitCode: number) {
Deshui Yu's avatar
Deshui Yu committed
37
38
39
40
41
42
43
44
45
46
47
48
        this.stdout = stdout;
        this.stderr = stderr;
        this.exitCode = exitCode;
    }
}

/**
 * RemoteMachineTrialJobDetail
 */
export class RemoteMachineTrialJobDetail implements TrialJobDetail {
    public id: string;
    public status: TrialJobStatus;
49
50
51
    public submitTime: number;
    public startTime?: number;
    public endTime?: number;
Deshui Yu's avatar
Deshui Yu committed
52
53
54
    public tags?: string[];
    public url?: string;
    public workingDirectory: string;
55
    public form: TrialJobApplicationForm;
Deshui Yu's avatar
Deshui Yu committed
56
    public rmMeta?: RemoteMachineMeta;
57
    public isEarlyStopped?: boolean;
58
    public gpuIndices: GPUInfo[];
Deshui Yu's avatar
Deshui Yu committed
59

60
    constructor(id: string, status: TrialJobStatus, submitTime: number,
61
        workingDirectory: string, form: TrialJobApplicationForm) {
Deshui Yu's avatar
Deshui Yu committed
62
63
64
65
66
67
        this.id = id;
        this.status = status;
        this.submitTime = submitTime;
        this.workingDirectory = workingDirectory;
        this.form = form;
        this.tags = [];
68
        this.gpuIndices = [];
Deshui Yu's avatar
Deshui Yu committed
69
70
71
    }
}

SparkSnail's avatar
SparkSnail committed
72
/**
73
 * The remote machine executor manager
SparkSnail's avatar
SparkSnail committed
74
 */
75
export class ExecutorManager {
76
    private readonly executorMap: Map<string, ShellExecutor> = new Map<string, ShellExecutor>();
SparkSnail's avatar
SparkSnail committed
77
    private readonly rmMeta: RemoteMachineMeta;
78
79
80
81

    private executors: ShellExecutor[] = [];

    constructor(rmMeta: RemoteMachineMeta) {
SparkSnail's avatar
SparkSnail committed
82
83
84
        this.rmMeta = rmMeta;
    }

85
86
87
    public async getExecutor(id: string): Promise<ShellExecutor> {
        let isFound = false;
        let executor: ShellExecutor | undefined;
88

89
90
91
92
93
        // already assigned
        if (this.executorMap.has(id)) {
            executor = this.executorMap.get(id);
            if (executor === undefined) {
                throw new Error("executor shouldn't be undefined before return!");
SparkSnail's avatar
SparkSnail committed
94
            }
95
            return executor;
96
97
        }

98
99
100
101
102
103
104
105
106
107
108
        for (const candidateExecutor of this.executors) {
            if (candidateExecutor.addUsage()) {
                isFound = true;
                executor = candidateExecutor;
                break;
            }
        }
        // init a new executor if no free one.
        if (!isFound) {
            executor = await this.createShellExecutor();
        }
109

110
111
112
113
        if (executor === undefined) {
            throw new Error("executor shouldn't be undefined before set!");
        }
        this.executorMap.set(id, executor);
114

115
        return executor;
SparkSnail's avatar
SparkSnail committed
116
    }
117

SparkSnail's avatar
SparkSnail committed
118
    /**
119
     * close all of executor
SparkSnail's avatar
SparkSnail committed
120
     */
121
122
123
    public releaseAllExecutor(): void {
        this.executorMap.clear();
        for (const executor of this.executors) {
124
            executor.close();
SparkSnail's avatar
SparkSnail committed
125
        }
126
        this.executors = [];
SparkSnail's avatar
SparkSnail committed
127
    }
128

SparkSnail's avatar
SparkSnail committed
129
    /**
130
131
     * retrieve resource, minus a number for given executor
     * @param executor executor
SparkSnail's avatar
SparkSnail committed
132
     */
133
134
    public releaseExecutor(id: string): void {
        const executor = this.executorMap.get(id);
135
        if (executor === undefined) {
136
            throw new Error(`executor for ${id} is not found`);
SparkSnail's avatar
SparkSnail committed
137
        }
138
139
        executor.releaseUsage();
        this.executorMap.delete(id);
SparkSnail's avatar
SparkSnail committed
140
141
    }

142
    /**
143
     * Create a new connection executor and initialize it
144
     */
145
    private async createShellExecutor(): Promise<ShellExecutor> {
146
147
        const executor = new ShellExecutor();
        await executor.initialize(this.rmMeta);
148
149
150
151
        if (!executor.addUsage()) {
            throw new Error("failed to add usage on new created Executor! It's a wired bug!");
        }
        this.executors.push(executor);
152
        return executor;
153
154
    }
}
SparkSnail's avatar
SparkSnail committed
155

156
export type RemoteMachineScheduleResult = { scheduleInfo: RemoteMachineScheduleInfo | undefined; resultType: ScheduleResultType };
Deshui Yu's avatar
Deshui Yu committed
157

158
export type RemoteMachineScheduleInfo = { rmMeta: RemoteMachineMeta; cudaVisibleDevice: string };