Skip to content

Commit

Permalink
Added option cpusetcpus
Browse files Browse the repository at this point in the history
  • Loading branch information
Alathreon committed May 18, 2024
1 parent 1713e0a commit dfd06a1
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.togetherjava.jshellapi;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.lang.Nullable;

@ConfigurationProperties("jshellapi")
public record Config(
Expand All @@ -12,6 +13,7 @@ public record Config(
long maxAliveSessions,
int dockerMaxRamMegaBytes,
double dockerCPUsUsage,
@Nullable String dockerCPUSetCPUs,
long schedulerSessionKillScanRateSeconds,
long dockerResponseTimeout,
long dockerConnectionTimeout) {
Expand All @@ -24,6 +26,8 @@ public record Config(
if(maxAliveSessions <= 0) throw new IllegalArgumentException("Invalid value " + maxAliveSessions);
if(dockerMaxRamMegaBytes <= 0) throw new IllegalArgumentException("Invalid value " + dockerMaxRamMegaBytes);
if(dockerCPUsUsage <= 0) throw new IllegalArgumentException("Invalid value " + dockerCPUsUsage);
if(dockerCPUSetCPUs != null && !dockerCPUSetCPUs.matches("[1-9]?\\d([-,]\\d?\\d)?"))
throw new IllegalArgumentException("Invalid value " + dockerCPUSetCPUs);
if(schedulerSessionKillScanRateSeconds <= 0) throw new IllegalArgumentException("Invalid value " + schedulerSessionKillScanRateSeconds);
if(dockerResponseTimeout <= 0) throw new IllegalArgumentException("Invalid value " + dockerResponseTimeout);
if(dockerConnectionTimeout <= 0) throw new IllegalArgumentException("Invalid value " + dockerConnectionTimeout);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.togetherjava.jshellapi.Config;

Expand Down Expand Up @@ -52,7 +53,7 @@ private void cleanupLeftovers(UUID currentId) {
}

public String spawnContainer(
long maxMemoryMegs, long cpus, String name, Duration evalTimeout, long sysoutLimit
long maxMemoryMegs, long cpus, @Nullable String cpuSetCpus, String name, Duration evalTimeout, long sysoutLimit
) throws InterruptedException {
String imageName = "togetherjava.org:5001/togetherjava/jshellwrapper";
boolean presentLocally = client.listImagesCmd()
Expand Down Expand Up @@ -82,6 +83,7 @@ public String spawnContainer(
.withReadonlyRootfs(true)
.withMemory(maxMemoryMegs * 1024 * 1024)
.withCpuCount(cpus)
.withCpusetCpus(cpuSetCpus)
)
.withStdinOpen(true)
.withAttachStdin(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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.exceptions.DockerException;

Expand All @@ -29,7 +30,7 @@ public class JShellService implements Closeable {
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, String startupScript) throws DockerException {
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;
Expand All @@ -46,6 +47,7 @@ public JShellService(DockerService dockerService, JShellSessionService sessionSe
String containerId = dockerService.spawnContainer(
maxMemory,
(long) Math.ceil(cpus),
cpuSetCpus,
containerName(),
Duration.ofSeconds(evalTimeout),
sysOutCharLimit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ private synchronized JShellService createSession(SessionInfo sessionInfo) throws
sessionInfo.sysOutCharLimit(),
config.dockerMaxRamMegaBytes(),
config.dockerCPUsUsage(),
config.dockerCPUSetCPUs(),
startupScriptsService.get(sessionInfo.startupScriptId()));
jshellSessions.put(sessionInfo.id(), service);
return service;
Expand Down
1 change: 1 addition & 0 deletions JShellAPI/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jshellapi.maxAliveSessions=10
# Docker limits config
jshellapi.dockerMaxRamMegaBytes=128
jshellapi.dockerCPUsUsage=0.5
jshellapi.dockerCPUSetCPUs=0

# Internal config
jshellapi.schedulerSessionKillScanRateSeconds=60
Expand Down

0 comments on commit dfd06a1

Please sign in to comment.