remoteMachineData.ts 5.39 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 } 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
28
29
30
31
32
33
34
35
36
}

export function parseGpuIndices(gpuIndices?: string): Set<number> | undefined {
    if (gpuIndices !== undefined) {
        const indices: number[] = gpuIndices.split(',')
            .map((x: string) => parseInt(x, 10));
        if (indices.length > 0) {
            return new Set(indices);
        } else {
            throw new Error('gpuIndices can not be empty if specified.');
        }
Deshui Yu's avatar
Deshui Yu committed
37
38
39
40
41
42
43
    }
}

/**
 * The execution result for command executed on remote machine
 */
export class RemoteCommandResult {
chicm-ms's avatar
chicm-ms committed
44
45
46
    public readonly stdout: string;
    public readonly stderr: string;
    public readonly exitCode: number;
Deshui Yu's avatar
Deshui Yu committed
47

chicm-ms's avatar
chicm-ms committed
48
    constructor(stdout: string, stderr: string, exitCode: number) {
Deshui Yu's avatar
Deshui Yu committed
49
50
51
52
53
54
55
56
57
58
59
60
        this.stdout = stdout;
        this.stderr = stderr;
        this.exitCode = exitCode;
    }
}

/**
 * RemoteMachineTrialJobDetail
 */
export class RemoteMachineTrialJobDetail implements TrialJobDetail {
    public id: string;
    public status: TrialJobStatus;
61
62
63
    public submitTime: number;
    public startTime?: number;
    public endTime?: number;
Deshui Yu's avatar
Deshui Yu committed
64
65
66
    public tags?: string[];
    public url?: string;
    public workingDirectory: string;
67
    public form: TrialJobApplicationForm;
Deshui Yu's avatar
Deshui Yu committed
68
    public rmMeta?: RemoteMachineMeta;
69
    public isEarlyStopped?: boolean;
70
    public gpuIndices: GPUInfo[];
Deshui Yu's avatar
Deshui Yu committed
71

72
    constructor(id: string, status: TrialJobStatus, submitTime: number,
73
        workingDirectory: string, form: TrialJobApplicationForm) {
Deshui Yu's avatar
Deshui Yu committed
74
75
76
77
78
79
        this.id = id;
        this.status = status;
        this.submitTime = submitTime;
        this.workingDirectory = workingDirectory;
        this.form = form;
        this.tags = [];
80
        this.gpuIndices = [];
Deshui Yu's avatar
Deshui Yu committed
81
82
83
    }
}

SparkSnail's avatar
SparkSnail committed
84
/**
85
 * The remote machine executor manager
SparkSnail's avatar
SparkSnail committed
86
 */
87
export class ExecutorManager {
88
    private readonly executorMap: Map<string, ShellExecutor> = new Map<string, ShellExecutor>();
SparkSnail's avatar
SparkSnail committed
89
    private readonly rmMeta: RemoteMachineMeta;
90
91
92
93

    private executors: ShellExecutor[] = [];

    constructor(rmMeta: RemoteMachineMeta) {
SparkSnail's avatar
SparkSnail committed
94
95
96
        this.rmMeta = rmMeta;
    }

97
98
99
    public async getExecutor(id: string): Promise<ShellExecutor> {
        let isFound = false;
        let executor: ShellExecutor | undefined;
100

101
102
103
104
105
        // 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
106
            }
107
            return executor;
108
109
        }

110
111
112
113
114
115
116
117
118
119
120
        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();
        }
121

122
123
124
125
        if (executor === undefined) {
            throw new Error("executor shouldn't be undefined before set!");
        }
        this.executorMap.set(id, executor);
126

127
        return executor;
SparkSnail's avatar
SparkSnail committed
128
    }
129

SparkSnail's avatar
SparkSnail committed
130
    /**
131
     * close all of executor
SparkSnail's avatar
SparkSnail committed
132
     */
133
134
135
    public releaseAllExecutor(): void {
        this.executorMap.clear();
        for (const executor of this.executors) {
136
            executor.close();
SparkSnail's avatar
SparkSnail committed
137
        }
138
        this.executors = [];
SparkSnail's avatar
SparkSnail committed
139
    }
140

SparkSnail's avatar
SparkSnail committed
141
    /**
142
143
     * retrieve resource, minus a number for given executor
     * @param executor executor
SparkSnail's avatar
SparkSnail committed
144
     */
145
146
    public releaseExecutor(id: string): void {
        const executor = this.executorMap.get(id);
147
        if (executor === undefined) {
148
            throw new Error(`executor for ${id} is not found`);
SparkSnail's avatar
SparkSnail committed
149
        }
150
151
        executor.releaseUsage();
        this.executorMap.delete(id);
SparkSnail's avatar
SparkSnail committed
152
153
    }

154
    /**
155
     * Create a new connection executor and initialize it
156
     */
157
    private async createShellExecutor(): Promise<ShellExecutor> {
158
159
        const executor = new ShellExecutor();
        await executor.initialize(this.rmMeta);
160
161
162
163
        if (!executor.addUsage()) {
            throw new Error("failed to add usage on new created Executor! It's a wired bug!");
        }
        this.executors.push(executor);
164
        return executor;
165
166
    }
}
SparkSnail's avatar
SparkSnail committed
167

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

170
export type RemoteMachineScheduleInfo = { rmMeta: RemoteMachineMeta; cudaVisibleDevice: string };
Deshui Yu's avatar
Deshui Yu committed
171
172

export enum ScheduleResultType {
173
    // Schedule succeeded
Deshui Yu's avatar
Deshui Yu committed
174
175
    SUCCEED,

176
    // Temporarily, no enough available GPU right now
Deshui Yu's avatar
Deshui Yu committed
177
178
    TMP_NO_AVAILABLE_GPU,

179
    // Cannot match requirement even if all GPU are a
Deshui Yu's avatar
Deshui Yu committed
180
181
    REQUIRE_EXCEED_TOTAL
}