-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJShellService.java
291 lines (263 loc) · 10.9 KB
/
JShellService.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
package org.togetherjava.jshellapi.service;
import org.apache.tomcat.util.http.fileupload.util.Closeable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.Nullable;
import org.togetherjava.jshellapi.dto.*;
import org.togetherjava.jshellapi.dto.sessionstats.SessionStats;
import org.togetherjava.jshellapi.exceptions.DockerException;
import java.io.*;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class JShellService implements Closeable {
private static final Logger LOGGER = LoggerFactory.getLogger(JShellService.class);
private final JShellSessionService sessionService;
private final String id;
private final BufferedWriter writer;
private final BufferedReader reader;
private final Instant creationTime;
private long totalEvalTime;
private Instant lastTimeoutUpdate;
private final long timeout;
private final boolean renewable;
private final long evalTimeoutValidationLeeway;
private final long evalTimeout;
private boolean doingOperation;
private final DockerService dockerService;
private final int startupScriptSize;
public JShellService(DockerService dockerService, JShellSessionService sessionService,
String id, long timeout, boolean renewable, long evalTimeout,
long evalTimeoutValidationLeeway, int sysOutCharLimit, int maxMemory, double cpus,
@Nullable String cpuSetCpus, String startupScript) throws DockerException {
this.dockerService = dockerService;
this.sessionService = sessionService;
this.id = id;
this.timeout = timeout;
this.renewable = renewable;
this.evalTimeout = evalTimeout;
this.evalTimeoutValidationLeeway = evalTimeoutValidationLeeway;
this.lastTimeoutUpdate = Instant.now();
if (!dockerService.isDead(containerName())) {
LOGGER.warn("Tried to create an existing container {}.", containerName());
throw new DockerException("The session isn't completely destroyed, try again later.");
}
try {
String containerId = dockerService.spawnContainer(maxMemory, (long) Math.ceil(cpus),
cpuSetCpus, containerName(), Duration.ofSeconds(evalTimeout), sysOutCharLimit);
PipedInputStream containerInput = new PipedInputStream();
this.writer = new BufferedWriter(
new OutputStreamWriter(new PipedOutputStream(containerInput)));
InputStream containerOutput =
dockerService.startAndAttachToContainer(containerId, containerInput);
reader = new BufferedReader(new InputStreamReader(containerOutput));
writer.write(sanitize(startupScript));
writer.newLine();
writer.flush();
checkContainerOK();
startupScriptSize = Integer.parseInt(reader.readLine());
} catch (Exception e) {
LOGGER.warn("Unexpected error during creation.", e);
throw new DockerException("Creation of the session failed.", e);
}
this.doingOperation = false;
this.creationTime = Instant.now();
}
public Optional<JShellResult> eval(String code) throws DockerException {
synchronized (this) {
if (!tryStartOperation()) {
return Optional.empty();
}
}
if (isClosed()) {
close();
return Optional.empty();
}
updateLastTimeout();
sessionService.scheduleEvalTimeoutValidation(id, evalTimeout + evalTimeoutValidationLeeway);
Instant start = Instant.now();
if (!code.endsWith("\n"))
code += '\n';
try {
writer.write("eval");
writer.newLine();
writer.write(String.valueOf(code.lines().count()));
writer.newLine();
writer.write(code);
writer.flush();
checkContainerOK();
return Optional.of(readResult());
} catch (DockerException | IOException | NumberFormatException ex) {
LOGGER.warn("Unexpected error.", ex);
close();
throw new DockerException(ex);
} finally {
totalEvalTime += Duration.between(start, Instant.now()).getSeconds();
stopOperation();
}
}
private JShellResult readResult() throws IOException, NumberFormatException, DockerException {
final int snippetsCount = Integer.parseInt(reader.readLine());
List<JShellSnippetResult> snippetResults = new ArrayList<>();
for (int i = 0; i < snippetsCount; i++) {
SnippetStatus status = Utils.nameOrElseThrow(SnippetStatus.class, reader.readLine(),
name -> new DockerException(name + " isn't an enum constant"));
SnippetType type = Utils.nameOrElseThrow(SnippetType.class, reader.readLine(),
name -> new DockerException(name + " isn't an enum constant"));
int snippetId = Integer.parseInt(reader.readLine());
String source = cleanCode(reader.readLine());
String result = reader.readLine().transform(r -> r.equals("NONE") ? null : r);
snippetResults.add(new JShellSnippetResult(status, type, snippetId, source, result));
}
JShellEvalAbortion abortion = null;
String rawAbortionCause = reader.readLine();
if (!rawAbortionCause.isEmpty()) {
JShellEvalAbortionCause abortionCause = switch (rawAbortionCause) {
case "TIMEOUT" -> new JShellEvalAbortionCause.TimeoutAbortionCause();
case "UNCAUGHT_EXCEPTION" -> {
String[] split = reader.readLine().split(":");
yield new JShellEvalAbortionCause.UnhandledExceptionAbortionCause(split[0],
split[1]);
}
case "COMPILE_TIME_ERROR" -> {
int errorCount = Integer.parseInt(reader.readLine());
List<String> errors = new ArrayList<>();
for (int i = 0; i < errorCount; i++) {
errors.add(desanitize(reader.readLine()));
}
yield new JShellEvalAbortionCause.CompileTimeErrorAbortionCause(errors);
}
case "SYNTAX_ERROR" -> new JShellEvalAbortionCause.SyntaxErrorAbortionCause();
default -> throw new DockerException(
"Abortion cause " + rawAbortionCause + " doesn't exist");
};
String causeSource = cleanCode(reader.readLine());
String remainingSource = cleanCode(reader.readLine());
abortion = new JShellEvalAbortion(causeSource, remainingSource, abortionCause);
}
boolean stdoutOverflow = Boolean.parseBoolean(reader.readLine());
String stdout = desanitize(reader.readLine());
return new JShellResult(snippetResults, abortion, stdoutOverflow, stdout);
}
public Optional<List<String>> snippets(boolean includeStartupScript) throws DockerException {
synchronized (this) {
if (!tryStartOperation()) {
return Optional.empty();
}
}
updateLastTimeout();
try {
writer.write("snippets");
writer.newLine();
writer.flush();
checkContainerOK();
List<String> snippets = new ArrayList<>();
String snippet;
while (!(snippet = reader.readLine()).isEmpty()) {
snippets.add(cleanCode(snippet));
}
return Optional.of(includeStartupScript ? snippets
: snippets.subList(startupScriptSize, snippets.size()));
} catch (Exception ex) {
LOGGER.warn("Unexpected error.", ex);
close();
throw new DockerException(ex);
} finally {
stopOperation();
}
}
public String containerName() {
return "session_" + id;
}
public boolean isInvalidEvalTimeout() {
return doingOperation
&& lastTimeoutUpdate.plusSeconds(evalTimeout + evalTimeoutValidationLeeway)
.isBefore(Instant.now());
}
public boolean shouldDie() {
return lastTimeoutUpdate.plusSeconds(timeout).isBefore(Instant.now());
}
public void stop() throws DockerException {
try {
writer.write("exit");
writer.newLine();
writer.flush();
} catch (IOException e) {
throw new DockerException(e);
}
}
public String id() {
return id;
}
public SessionStats fetchSessionInfo() {
long timeSinceCreation = Duration.between(creationTime, Instant.now()).getSeconds();
long timeUntilExpiration =
Duration.between(Instant.now(), lastTimeoutUpdate.plusSeconds(timeout))
.getSeconds();
if (timeUntilExpiration < 0) {
timeUntilExpiration = 0;
}
return new SessionStats(
id, timeSinceCreation, timeUntilExpiration, totalEvalTime, doingOperation);
}
@Override
public void close() {
LOGGER.debug("Close called for session {}.", id);
try {
dockerService.killContainerByName(containerName());
try {
writer.close();
} finally {
reader.close();
}
} catch (IOException ex) {
LOGGER.error("Unexpected error while closing.", ex);
} finally {
sessionService.notifyDeath(id);
}
}
@Override
public boolean isClosed() {
return dockerService.isDead(containerName());
}
private void updateLastTimeout() {
if (renewable) {
lastTimeoutUpdate = Instant.now();
}
}
private void checkContainerOK() throws DockerException {
try {
if (dockerService.isDead(containerName())) {
throw new IOException("Container of session " + id + " is dead");
}
String ok = reader.readLine();
if (ok == null || !ok.equals("OK")) {
throw new IOException(
"Container of session " + id + " is dead because status was " + ok);
}
} catch (IOException ex) {
close();
throw new DockerException(ex);
}
}
private synchronized boolean tryStartOperation() {
if (doingOperation)
return false;
doingOperation = true;
return true;
}
private void stopOperation() {
doingOperation = false;
}
private static String sanitize(String s) {
return s.replace("\\", "\\\\").replace("\n", "\\n");
}
private static String desanitize(String text) {
return text.replace("\\n", "\n").replace("\\\\", "\\");
}
private static String cleanCode(String code) {
return code.translateEscapes();
}
}