/
WorkerLifeCycle.java
260 lines (227 loc) · 9.18 KB
/
WorkerLifeCycle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package org.pytorch.serve.wlm;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.pytorch.serve.metrics.Metric;
import org.pytorch.serve.util.ConfigManager;
import org.pytorch.serve.util.Connector;
import org.pytorch.serve.util.messages.EnvironmentUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class WorkerLifeCycle {
private static final Logger logger = LoggerFactory.getLogger(WorkerLifeCycle.class);
private ConfigManager configManager;
private Model model;
private int pid = -1;
private Process process;
private CountDownLatch latch;
private boolean success;
private Connector connector;
private ReaderThread errReader;
private ReaderThread outReader;
private String launcherArgs;
public WorkerLifeCycle(ConfigManager configManager, Model model) {
this.configManager = configManager;
this.model = model;
}
public Process getProcess() {
return process;
}
public ArrayList<String> launcherArgsToList() {
ArrayList<String> arrlist = new ArrayList<String>();
arrlist.add("-m");
arrlist.add("intel_extension_for_pytorch.cpu.launch");
arrlist.add("--ninstance");
arrlist.add("1");
if (launcherArgs != null && launcherArgs.length() > 1) {
String[] argarray = launcherArgs.split(" ");
for (int i = 0; i < argarray.length; i++) {
arrlist.add(argarray[i]);
}
}
return arrlist;
}
public boolean isLauncherAvailable()
throws WorkerInitializationException, InterruptedException {
boolean launcherAvailable = false;
try {
ArrayList<String> cmd = new ArrayList<String>();
cmd.add("python");
ArrayList<String> args = launcherArgsToList();
cmd.addAll(args);
cmd.add("--no_python");
// try launching dummy command to check launcher availability
String dummyCmd = "hostname";
cmd.add(dummyCmd);
String[] cmdList = new String[cmd.size()];
cmdList = cmd.toArray(cmdList);
Process processLauncher = Runtime.getRuntime().exec(cmdList);
int ret = processLauncher.waitFor();
launcherAvailable = (ret == 0);
} catch (IOException | InterruptedException e) {
throw new WorkerInitializationException("Failed to start launcher", e);
}
return launcherAvailable;
}
public void startWorker(int port) throws WorkerInitializationException, InterruptedException {
File workingDir = new File(configManager.getModelServerHome());
File modelPath;
setPort(port);
try {
modelPath = model.getModelDir().getCanonicalFile();
} catch (IOException e) {
throw new WorkerInitializationException("Failed get TS home directory", e);
}
ArrayList<String> argl = new ArrayList<String>();
argl.add(EnvironmentUtils.getPythonRunTime(model));
if (configManager.isCPULauncherEnabled()) {
launcherArgs = configManager.getCPULauncherArgs();
boolean launcherAvailable = isLauncherAvailable();
if (launcherAvailable) {
ArrayList<String> args = launcherArgsToList();
argl.addAll(args);
} else {
logger.warn(
"CPU launcher is enabled but launcher is not available. Proceeding without launcher.");
}
}
argl.add(new File(workingDir, "ts/model_service_worker.py").getAbsolutePath());
argl.add("--sock-type");
argl.add(connector.getSocketType());
argl.add(connector.isUds() ? "--sock-name" : "--port");
argl.add(connector.getSocketPath());
String[] envp =
EnvironmentUtils.getEnvString(
workingDir.getAbsolutePath(),
modelPath.getAbsolutePath(),
model.getModelArchive().getManifest().getModel().getHandler());
try {
latch = new CountDownLatch(1);
String[] args = argl.toArray(new String[argl.size()]);
logger.debug("Worker cmdline: {}", argl.toString());
synchronized (this) {
process = Runtime.getRuntime().exec(args, envp, modelPath);
String threadName =
"W-" + port + '-' + model.getModelVersionName().getVersionedModelName();
errReader = new ReaderThread(threadName, process.getErrorStream(), true, this);
outReader = new ReaderThread(threadName, process.getInputStream(), false, this);
errReader.start();
outReader.start();
}
if (latch.await(2, TimeUnit.MINUTES)) {
if (!success) {
throw new WorkerInitializationException("Backend stream closed.");
}
return;
}
throw new WorkerInitializationException("Backend worker startup time out.");
} catch (IOException e) {
throw new WorkerInitializationException("Failed start worker process", e);
} finally {
if (!success) {
exit();
}
}
}
public synchronized void terminateIOStreams() {
if (errReader != null) {
logger.warn("terminateIOStreams() threadName={}", errReader.getName());
errReader.terminate();
}
if (outReader != null) {
logger.warn("terminateIOStreams() threadName={}", outReader.getName());
outReader.terminate();
}
}
public synchronized void exit() {
if (process != null) {
process.destroyForcibly();
connector.clean();
terminateIOStreams();
}
}
public synchronized Integer getExitValue() {
if (process != null && !process.isAlive()) {
return process.exitValue();
}
return null;
}
public void setSuccess(boolean success) {
this.success = success;
latch.countDown();
}
public synchronized int getPid() {
return pid;
}
public synchronized void setPid(int pid) {
this.pid = pid;
}
private synchronized void setPort(int port) {
connector = new Connector(port);
}
private static final class ReaderThread extends Thread {
private InputStream is;
private boolean error;
private WorkerLifeCycle lifeCycle;
private AtomicBoolean isRunning = new AtomicBoolean(true);
private static final Logger loggerModelMetrics =
LoggerFactory.getLogger(ConfigManager.MODEL_METRICS_LOGGER);
private static final Logger loggerModelOutput =
LoggerFactory.getLogger(ConfigManager.MODEL_LOGGER);
public ReaderThread(String name, InputStream is, boolean error, WorkerLifeCycle lifeCycle) {
super(name + (error ? "-stderr" : "-stdout"));
this.is = is;
this.error = error;
this.lifeCycle = lifeCycle;
}
public void terminate() {
isRunning.set(false);
}
@Override
public void run() {
try (Scanner scanner = new Scanner(is, StandardCharsets.UTF_8.name())) {
while (isRunning.get() && scanner.hasNext()) {
String result = scanner.nextLine();
if (result == null) {
break;
}
if (result.startsWith("[METRICS]")) {
Metric parsedMetric = Metric.parse(result.substring("[METRICS]".length()));
if (parsedMetric != null) {
loggerModelMetrics.info(parsedMetric.toString());
} else {
logger.error("Failed to parse metrics line: \"{}\".", result);
}
continue;
}
if ("Torch worker started.".equals(result)) {
lifeCycle.setSuccess(true);
} else if (result.startsWith("[PID]")) {
lifeCycle.setPid(Integer.parseInt(result.substring("[PID]".length())));
}
if (error) {
loggerModelOutput.warn(result);
} else {
loggerModelOutput.info(result);
}
}
} catch (Exception e) {
logger.error("Couldn't create scanner - {}", getName(), e);
} finally {
logger.info("Stopped Scanner - {}", getName());
lifeCycle.setSuccess(false);
try {
is.close();
} catch (IOException e) {
logger.error("Failed to close stream for thread {}", this.getName(), e);
}
}
}
}
}