Skip to content

Commit

Permalink
* Moved to -alpine docker runtime images
Browse files Browse the repository at this point in the history
* Disabled client building for docker container
* Switched to using platform threads for RemoteCLU objects (on older JVM versions pining threads on DatagramSockets)

Signed-off-by: Piotr Sobiech <piotr@sobie.ch>
  • Loading branch information
psobiech committed Jan 22, 2024
1 parent 3a1ff54 commit 3a16dbb
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 29 deletions.
9 changes: 4 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,20 @@ COPY assembly/jar-with-dependencies.xml assembly/

# https://issues.apache.org/jira/browse/MDEP-689
#RUN mvn -B -T 4 dependency:go-offline
RUN mvn -B -T 4 compile -Dorg.slf4j.simpleLogger.defaultLogLevel=ERROR -Dmaven.test.skip=true -Dmaven.site.skip=true -Dmaven.source.skip=true -Dmaven.javadoc.skip=true
RUN mvn -B -T 4 -pl '!modules/client' compile -Dorg.slf4j.simpleLogger.defaultLogLevel=ERROR -Dmaven.test.skip=true -Dmaven.site.skip=true -Dmaven.source.skip=true -Dmaven.javadoc.skip=true

FROM app-deps AS app-build

COPY modules/tftp modules/tftp
COPY modules/common modules/common
COPY modules/lib modules/lib
COPY modules/parsers modules/parsers
COPY modules/client modules/client
COPY modules/vclu modules/vclu
COPY .git .git

RUN mvn -B -T 4 clean package -Dorg.slf4j.simpleLogger.defaultLogLevel=WARN -Dmaven.test.skip=true -Dmaven.site.skip=true -Dmaven.source.skip=true -Dmaven.javadoc.skip=true
RUN mvn -B -T 4 -pl '!modules/client' clean package -Dorg.slf4j.simpleLogger.defaultLogLevel=WARN -Dmaven.test.skip=true -Dmaven.site.skip=true -Dmaven.source.skip=true -Dmaven.javadoc.skip=true

FROM --platform=$BUILDPLATFORM eclipse-temurin:21 AS jre-build
FROM --platform=$BUILDPLATFORM eclipse-temurin:21-alpine AS jre-build

RUN mkdir -p /opt/build
WORKDIR /opt/build
Expand All @@ -55,7 +54,7 @@ RUN $JAVA_HOME/bin/jlink \
--compress=2 \
--output /opt/build/jre

FROM --platform=$BUILDPLATFORM ubuntu:22.04 AS app-runtime
FROM --platform=$BUILDPLATFORM alpine:latest AS app-runtime

ENV JAVA_HOME=/opt/java/openjdk
ENV PATH "${JAVA_HOME}/bin:${PATH}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ public static ExecutorService daemonExecutor(String name) {
* @return true, if JVM version has fixed <a href="https://bugs.openjdk.org/browse/JDK-8312166">JDK-8312166</a> (>= 21.0.2+2)
*/
public static boolean supportsNonBlockingDatagramSockets() {
return Runtime.version().compareTo(JVM_VERSION_NON_BLOCKING_DATAGRAM_SOCKET) >= 0;
return Runtime.version()
.compareTo(JVM_VERSION_NON_BLOCKING_DATAGRAM_SOCKET) >= 0;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package pl.psobiech.opengr8on.vclu.system.objects;

import java.net.Inet4Address;
import java.util.Optional;
import java.util.concurrent.ExecutionException;

import org.apache.commons.lang3.StringUtils;
import org.luaj.vm2.Globals;
Expand All @@ -29,7 +31,10 @@
import org.slf4j.LoggerFactory;
import pl.psobiech.opengr8on.client.CLUClient;
import pl.psobiech.opengr8on.client.CipherKey;
import pl.psobiech.opengr8on.exceptions.UncheckedInterruptedException;
import pl.psobiech.opengr8on.exceptions.UnexpectedException;
import pl.psobiech.opengr8on.util.IOUtil;
import pl.psobiech.opengr8on.util.ThreadUtil;
import pl.psobiech.opengr8on.vclu.system.VirtualSystem;

public class RemoteCLU extends VirtualObject {
Expand All @@ -44,7 +49,8 @@ public class RemoteCLU extends VirtualObject {
public RemoteCLU(VirtualSystem virtualSystem, String name, Inet4Address address, Inet4Address localAddress, CipherKey cipherKey, int port) {
super(
virtualSystem, name,
IFeature.EMPTY.class, Methods.class, IEvent.EMPTY.class
IFeature.EMPTY.class, Methods.class, IEvent.EMPTY.class,
ThreadUtil.supportsNonBlockingDatagramSockets() ? ThreadUtil::virtualScheduler : ThreadUtil::daemonScheduler
);

this.localLuaContext = new Globals();
Expand All @@ -56,27 +62,39 @@ public RemoteCLU(VirtualSystem virtualSystem, String name, Inet4Address address,
register(Methods.EXECUTE, arg1 -> {
final String script = arg1.checkjstring();

return client.execute(script)
.map(returnValue -> {
returnValue = StringUtils.stripToNull(returnValue);
if (returnValue == null) {
return null;
}

if (returnValue.startsWith("{")) {
return localLuaContext.load("return %s".formatted(returnValue))
.call();
}

final LuaString luaString = LuaValue.valueOf(returnValue);
if (luaString.isnumber()) {
return luaString.checknumber();
}

return luaString;
}
)
.orElse(LuaValue.NIL);
final Optional<String> returnValueOptional;
try {
// TODO: remove this workaround, when DockerSockets stop thread pinning
returnValueOptional = scheduler.submit(() ->
client.execute(script)
)
.get();
} catch (InterruptedException e) {
throw new UncheckedInterruptedException(e);
} catch (ExecutionException e) {
throw new UnexpectedException(e);
}

return returnValueOptional.map(returnValue -> {
returnValue = StringUtils.stripToNull(returnValue);
if (returnValue == null) {
return null;
}

if (returnValue.startsWith("{")) {
return localLuaContext.load("return %s".formatted(returnValue))
.call();
}

final LuaString luaString = LuaValue.valueOf(returnValue);
if (luaString.isnumber()) {
return luaString.checknumber();
}

return luaString;
}
)
.orElse(LuaValue.NIL);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Optional;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.Function;

import org.luaj.vm2.LuaFunction;
import org.luaj.vm2.LuaValue;
Expand Down Expand Up @@ -77,10 +78,26 @@ public VirtualObject(
Class<? extends Enum<? extends IFeature>> featureClass,
Class<? extends Enum<? extends IMethod>> methodClass,
Class<? extends Enum<? extends IEvent>> eventClass
) {
this(
virtualSystem,
name,
featureClass, methodClass, eventClass,
ThreadUtil::virtualScheduler
);
}

public VirtualObject(
VirtualSystem virtualSystem,
String name,
Class<? extends Enum<? extends IFeature>> featureClass,
Class<? extends Enum<? extends IMethod>> methodClass,
Class<? extends Enum<? extends IEvent>> eventClass,
Function<String, ScheduledExecutorService> schedulerSupplier
) {
this.virtualSystem = virtualSystem;
this.name = name;
this.scheduler = ThreadUtil.virtualScheduler(name);
this.scheduler = schedulerSupplier.apply(name);

this.featureClass = featureClass;
this.methodClass = methodClass;
Expand Down

0 comments on commit 3a16dbb

Please sign in to comment.