-
Notifications
You must be signed in to change notification settings - Fork 52
/
LocalTaskLauncher.java
392 lines (331 loc) · 11.8 KB
/
LocalTaskLauncher.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
* Copyright 2016-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.deployer.spi.local;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.Inet4Address;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.annotation.PreDestroy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.deployer.spi.core.AppDeploymentRequest;
import org.springframework.cloud.deployer.spi.core.RuntimeEnvironmentInfo;
import org.springframework.cloud.deployer.spi.task.LaunchState;
import org.springframework.cloud.deployer.spi.task.TaskLauncher;
import org.springframework.cloud.deployer.spi.task.TaskStatus;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
/**
* A {@link TaskLauncher} implementation that spins off a new JVM process per task launch.
*
* @author Eric Bottard
* @author Marius Bogoevici
* @author Mark Fisher
* @author Janne Valkealahti
* @author Thomas Risberg
* @author Oleg Zhurakousky
* @author Michael Minella
* @author Christian Tzolov
* @author David Turanski
* @author Glenn Renfro
* @author Ben Blinebury
*/
public class LocalTaskLauncher extends AbstractLocalDeployerSupport implements TaskLauncher {
private static final Logger logger = LoggerFactory.getLogger(LocalTaskLauncher.class);
private static final String JMX_DEFAULT_DOMAIN_KEY = "spring.jmx.default-domain";
private final Map<String, TaskInstance> running = new ConcurrentHashMap<>();
private final Map<String, CopyOnWriteArrayList<String>> taskInstanceHistory = new ConcurrentHashMap<>();
/**
* Instantiates a new local task launcher.
*
* @param properties the properties
*/
public LocalTaskLauncher(LocalDeployerProperties properties) {
super(properties);
}
@Override
public String launch(AppDeploymentRequest request) {
if (this.maxConcurrentExecutionsReached()) {
throw new IllegalStateException(
String.format("Cannot launch task %s. The maximum concurrent task executions is at its limit [%d].",
request.getDefinition().getName(), this.getMaximumConcurrentTasks())
);
}
String taskLaunchId = request.getDefinition().getName() + "-" + UUID.randomUUID().toString();
pruneTaskInstanceHistory(request.getDefinition().getName(), taskLaunchId);
HashMap<String, String> args = new HashMap<>();
args.putAll(request.getDefinition().getProperties());
args.put(JMX_DEFAULT_DOMAIN_KEY, taskLaunchId);
args.put("endpoints.shutdown.enabled", "true");
args.put("endpoints.jmx.unique-names", "true");
try {
Path workingDirectory = createWorkingDirectory(request.getDeploymentProperties(), taskLaunchId);
boolean useDynamicPort = isDynamicPort(request);
int port = calcServerPort(request, useDynamicPort, args);
ProcessBuilder builder = buildProcessBuilder(request, args, Optional.empty(), taskLaunchId).inheritIO();
TaskInstance instance = new TaskInstance(builder, workingDirectory, port);
if (this.shouldInheritLogging(request)) {
instance.start(builder);
logger.info("launching task {}\n Logs will be inherited.", taskLaunchId);
}
else {
instance.start(builder, getLocalDeployerProperties().isDeleteFilesOnExit());
logger.info("launching task {}\n Logs will be in {}", taskLaunchId, workingDirectory);
}
running.put(taskLaunchId, instance);
}
catch (IOException e) {
throw new RuntimeException("Exception trying to launch " + request, e);
}
return taskLaunchId;
}
private void pruneTaskInstanceHistory(String taskDefinitionName, String taskLaunchId) {
CopyOnWriteArrayList<String> oldTaskInstanceIds = taskInstanceHistory.get(taskDefinitionName);
if (oldTaskInstanceIds == null) {
oldTaskInstanceIds = new CopyOnWriteArrayList<>();
taskInstanceHistory.put(taskDefinitionName, oldTaskInstanceIds);
}
for (String oldTaskInstanceId : oldTaskInstanceIds) {
TaskInstance oldTaskInstance = running.get(oldTaskInstanceId);
if (oldTaskInstance != null && oldTaskInstance.getState() != LaunchState.running
&& oldTaskInstance.getState() != LaunchState.launching) {
running.remove(oldTaskInstanceId);
oldTaskInstanceIds.remove(oldTaskInstanceId);
} else {
oldTaskInstanceIds.remove(oldTaskInstanceId);
}
}
oldTaskInstanceIds.add(taskLaunchId);
}
private boolean isDynamicPort(AppDeploymentRequest request) {
boolean isServerPortKeyonArgs = isServerPortKeyPresentOnArgs(request) != null;
return !request.getDefinition().getProperties().containsKey(SERVER_PORT_KEY)
&& !isServerPortKeyonArgs;
}
@Override
public void cancel(String id) {
TaskInstance instance = running.get(id);
if (instance != null) {
instance.cancelled = true;
if (isAlive(instance.getProcess())) {
shutdownAndWait(instance);
}
}
}
@Override
public TaskStatus status(String id) {
TaskInstance instance = running.get(id);
if (instance != null) {
return new TaskStatus(id, instance.getState(), instance.getAttributes());
}
return new TaskStatus(id, LaunchState.unknown, null);
}
@Override
public String getLog(String id) {
TaskInstance instance = running.get(id);
if (instance != null) {
StringBuilder stringBuilder = new StringBuilder();
String stderr = instance.getStdErr();
if (StringUtils.hasText(stderr)) {
stringBuilder.append("stderr:\n");
stringBuilder.append(stderr);
}
String stdout = instance.getStdOut();
if (StringUtils.hasText(stdout)) {
stringBuilder.append("stdout:\n");
stringBuilder.append(stdout);
}
return stringBuilder.toString();
}
else {
return "Log could not be retrieved as the task instance is not running.";
}
}
@Override
public void cleanup(String id) {
}
@Override
public void destroy(String appName) {
}
@Override
public RuntimeEnvironmentInfo environmentInfo() {
return super.createRuntimeEnvironmentInfo(TaskLauncher.class, this.getClass());
}
@Override
public int getMaximumConcurrentTasks() {
return getLocalDeployerProperties().getMaximumConcurrentTasks();
}
@Override
public int getRunningTaskExecutionCount() {
int runningExecutionCount = 0;
for (TaskInstance taskInstance: running.values()) {
if (taskInstance.getProcess().isAlive()) {
runningExecutionCount++;
}
}
return runningExecutionCount;
}
private synchronized boolean maxConcurrentExecutionsReached() {
return getRunningTaskExecutionCount() >= getMaximumConcurrentTasks();
}
@PreDestroy
public void shutdown() throws Exception {
for (String taskLaunchId : running.keySet()) {
cancel(taskLaunchId);
}
taskInstanceHistory.clear();
}
private Path createWorkingDirectory(Map<String, String> deploymentProperties, String taskLaunchId) throws IOException {
LocalDeployerProperties localDeployerPropertiesToUse = bindDeploymentProperties(deploymentProperties);
Path workingDirectoryRoot = Files.isSymbolicLink(localDeployerPropertiesToUse.getWorkingDirectoriesRoot())
? Files.readSymbolicLink(localDeployerPropertiesToUse.getWorkingDirectoriesRoot())
: Files.createDirectories(localDeployerPropertiesToUse.getWorkingDirectoriesRoot());
Path workingDirectory = Files.createDirectories(workingDirectoryRoot.resolve(Long.toString(System.nanoTime())).resolve(taskLaunchId));
if (localDeployerPropertiesToUse.isDeleteFilesOnExit()) {
workingDirectory.toFile().deleteOnExit();
}
return workingDirectory;
}
private static class TaskInstance implements Instance {
private Process process;
private final Path workDir;
private File stdout;
private File stderr;
private final URL baseUrl;
private boolean cancelled;
private TaskInstance(ProcessBuilder builder, Path workDir, int port) throws IOException {
builder.directory(workDir.toFile());
this.workDir = workDir;
this.baseUrl = new URL("http", Inet4Address.getLocalHost().getHostAddress(), port, "");
if (logger.isDebugEnabled()) {
logger.debug("Local Task Launcher Commands: " + String.join(",", builder.command())
+ ", Environment: " + builder.environment());
}
}
@Override
public URL getBaseUrl() {
return this.baseUrl;
}
@Override
public Process getProcess() {
return this.process;
}
public LaunchState getState() {
if (cancelled) {
return LaunchState.cancelled;
}
Integer exit = getProcessExitValue(process);
// TODO: consider using exit code mapper concept from batch
if (exit != null) {
if (exit == 0) {
return LaunchState.complete;
}
else {
return LaunchState.failed;
}
}
try {
HttpURLConnection urlConnection = (HttpURLConnection) baseUrl.openConnection();
urlConnection.setConnectTimeout(100);
urlConnection.connect();
urlConnection.disconnect();
return LaunchState.running;
}
catch (IOException e) {
return LaunchState.launching;
}
}
public String getStdOut() {
try {
return FileCopyUtils.copyToString(new InputStreamReader(new FileInputStream(this.stdout)));
}
catch (IOException e) {
return "Log retrieval returned " + e.getMessage();
}
}
public String getStdErr() {
try {
return FileCopyUtils.copyToString(new InputStreamReader(new FileInputStream(this.stderr)));
}
catch (IOException e) {
return "Log retrieval returned " + e.getMessage();
}
}
/**
* Will start the process while redirecting 'out' and 'err' streams to the 'out' and 'err'
* streams of this process.
*/
private void start(ProcessBuilder builder) throws IOException {
if (logger.isDebugEnabled()) {
logger.debug("Local Task Launcher Commands: " + String.join(",", builder.command())
+ ", Environment: " + builder.environment());
}
this.process = builder.start();
}
private void start(ProcessBuilder builder, boolean deleteOnExit) throws IOException {
String workDirPath = workDir.toFile().getAbsolutePath();
this.stdout = Files.createFile(Paths.get(workDirPath, "stdout.log")).toFile();
this.stderr = Files.createFile(Paths.get(workDirPath, "stderr.log")).toFile();
builder.redirectOutput(this.stdout);
builder.redirectError(this.stderr);
this.process = builder.start();
if(deleteOnExit) {
this.stdout.deleteOnExit();
this.stderr.deleteOnExit();
}
}
private Map<String, String> getAttributes() {
HashMap<String, String> result = new HashMap<>();
result.put("working.dir", workDir.toFile().getAbsolutePath());
if(this.stdout != null) {
result.put("stdout", stdout.getAbsolutePath());
}
if(this.stderr != null) {
result.put("stderr", stderr.getAbsolutePath());
}
result.put("url", baseUrl.toString());
return result;
}
}
/**
* Returns the process exit value. We explicitly use Integer instead of int
* to indicate that if {@code NULL} is returned, the process is still running.
*
* @param process the process
* @return the process exit value or {@code NULL} if process is still alive
*/
private static Integer getProcessExitValue(Process process) {
try {
return process.exitValue();
}
catch (IllegalThreadStateException e) {
// process is still alive
return null;
}
}
}