Skip to content

Commit dfd06a1

Browse files
committedMay 18, 2024·
Added option cpusetcpus
1 parent 1713e0a commit dfd06a1

File tree

5 files changed

+12
-2
lines changed

5 files changed

+12
-2
lines changed
 

‎JShellAPI/src/main/java/org/togetherjava/jshellapi/Config.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.togetherjava.jshellapi;
22

33
import org.springframework.boot.context.properties.ConfigurationProperties;
4+
import org.springframework.lang.Nullable;
45

56
@ConfigurationProperties("jshellapi")
67
public record Config(
@@ -12,6 +13,7 @@ public record Config(
1213
long maxAliveSessions,
1314
int dockerMaxRamMegaBytes,
1415
double dockerCPUsUsage,
16+
@Nullable String dockerCPUSetCPUs,
1517
long schedulerSessionKillScanRateSeconds,
1618
long dockerResponseTimeout,
1719
long dockerConnectionTimeout) {
@@ -24,6 +26,8 @@ public record Config(
2426
if(maxAliveSessions <= 0) throw new IllegalArgumentException("Invalid value " + maxAliveSessions);
2527
if(dockerMaxRamMegaBytes <= 0) throw new IllegalArgumentException("Invalid value " + dockerMaxRamMegaBytes);
2628
if(dockerCPUsUsage <= 0) throw new IllegalArgumentException("Invalid value " + dockerCPUsUsage);
29+
if(dockerCPUSetCPUs != null && !dockerCPUSetCPUs.matches("[1-9]?\\d([-,]\\d?\\d)?"))
30+
throw new IllegalArgumentException("Invalid value " + dockerCPUSetCPUs);
2731
if(schedulerSessionKillScanRateSeconds <= 0) throw new IllegalArgumentException("Invalid value " + schedulerSessionKillScanRateSeconds);
2832
if(dockerResponseTimeout <= 0) throw new IllegalArgumentException("Invalid value " + dockerResponseTimeout);
2933
if(dockerConnectionTimeout <= 0) throw new IllegalArgumentException("Invalid value " + dockerConnectionTimeout);

‎JShellAPI/src/main/java/org/togetherjava/jshellapi/service/DockerService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.slf4j.Logger;
1111
import org.slf4j.LoggerFactory;
1212
import org.springframework.beans.factory.DisposableBean;
13+
import org.springframework.lang.Nullable;
1314
import org.springframework.stereotype.Service;
1415
import org.togetherjava.jshellapi.Config;
1516

@@ -52,7 +53,7 @@ private void cleanupLeftovers(UUID currentId) {
5253
}
5354

5455
public String spawnContainer(
55-
long maxMemoryMegs, long cpus, String name, Duration evalTimeout, long sysoutLimit
56+
long maxMemoryMegs, long cpus, @Nullable String cpuSetCpus, String name, Duration evalTimeout, long sysoutLimit
5657
) throws InterruptedException {
5758
String imageName = "togetherjava.org:5001/togetherjava/jshellwrapper";
5859
boolean presentLocally = client.listImagesCmd()
@@ -82,6 +83,7 @@ public String spawnContainer(
8283
.withReadonlyRootfs(true)
8384
.withMemory(maxMemoryMegs * 1024 * 1024)
8485
.withCpuCount(cpus)
86+
.withCpusetCpus(cpuSetCpus)
8587
)
8688
.withStdinOpen(true)
8789
.withAttachStdin(true)

‎JShellAPI/src/main/java/org/togetherjava/jshellapi/service/JShellService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.apache.tomcat.util.http.fileupload.util.Closeable;
44
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
6+
import org.springframework.lang.Nullable;
67
import org.togetherjava.jshellapi.dto.*;
78
import org.togetherjava.jshellapi.exceptions.DockerException;
89

@@ -29,7 +30,7 @@ public class JShellService implements Closeable {
2930
private final DockerService dockerService;
3031
private final int startupScriptSize;
3132

32-
public JShellService(DockerService dockerService, JShellSessionService sessionService, String id, long timeout, boolean renewable, long evalTimeout, long evalTimeoutValidationLeeway, int sysOutCharLimit, int maxMemory, double cpus, String startupScript) throws DockerException {
33+
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 {
3334
this.dockerService = dockerService;
3435
this.sessionService = sessionService;
3536
this.id = id;
@@ -46,6 +47,7 @@ public JShellService(DockerService dockerService, JShellSessionService sessionSe
4647
String containerId = dockerService.spawnContainer(
4748
maxMemory,
4849
(long) Math.ceil(cpus),
50+
cpuSetCpus,
4951
containerName(),
5052
Duration.ofSeconds(evalTimeout),
5153
sysOutCharLimit

‎JShellAPI/src/main/java/org/togetherjava/jshellapi/service/JShellSessionService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ private synchronized JShellService createSession(SessionInfo sessionInfo) throws
9595
sessionInfo.sysOutCharLimit(),
9696
config.dockerMaxRamMegaBytes(),
9797
config.dockerCPUsUsage(),
98+
config.dockerCPUSetCPUs(),
9899
startupScriptsService.get(sessionInfo.startupScriptId()));
99100
jshellSessions.put(sessionInfo.id(), service);
100101
return service;

‎JShellAPI/src/main/resources/application.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ jshellapi.maxAliveSessions=10
1010
# Docker limits config
1111
jshellapi.dockerMaxRamMegaBytes=128
1212
jshellapi.dockerCPUsUsage=0.5
13+
jshellapi.dockerCPUSetCPUs=0
1314

1415
# Internal config
1516
jshellapi.schedulerSessionKillScanRateSeconds=60

0 commit comments

Comments
 (0)
Please sign in to comment.