Skip to content
Snippets Groups Projects
Commit 1a95f291 authored by Jari-Matti Mäkelä's avatar Jari-Matti Mäkelä
Browse files

Switch to JPMS + Junit5 + Maven + JDK collections

parent d2dd6b8a
No related branches found
No related tags found
No related merge requests found
Pipeline #85538 passed
......@@ -2,3 +2,4 @@ build/
.idea/
.gradle/
gradle.properties
target/
image: maven:3-eclipse-temurin-17
variables:
MAVEN_OPTS: "-Dhttps.protocols=TLSv1.2 -Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true"
MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version"
cache:
key: "$CI_JOB_NAME"
paths:
- .m2/repository
stages:
- build
- deploy
build:
stage: build
script:
- mkdir -p src/main/resources/sh/blake/niouring/util/Linux/amd64/
# disabled because compiling the drivers requires a c compiler toolchain
# - mvn -P jni compile native:compile native:link
# - cp target/libnio_uring.so src/main/resources/sh/blake/niouring/util/Linux/amd64/
- mvn install -s ci_settings.xml -DskipTests
artifacts:
paths:
- target/
# build the docker image for the tool, see the Dockerfile for details
deploy:
stage: deploy
rules:
- if: $CI_COMMIT_TAG
script:
- mvn deploy -s ci_settings.xml -DskipTests
mvn -P jni compile native:compile native:link
mkdir -p src/main/resources/sh/blake/niouring/util/Linux/amd64/
cp target/libnio_uring.so src/main/resources/sh/blake/niouring/util/Linux/amd64/
mvn package
<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
<servers>
<server>
<id>gitlab-maven</id>
<configuration>
<httpHeaders>
<property>
<name>Job-Token</name>
<value>${env.CI_JOB_TOKEN}</value>
</property>
</httpHeaders>
</configuration>
</server>
</servers>
</settings>
pom.xml 0 → 100644
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>sh.blake.niouring</groupId>
<artifactId>nio_uring-jpms</artifactId>
<version>0.1.4</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>17</jdk.version>
<junit.version>5.10.1</junit.version>
<junitplatform.version>1.10.1</junitplatform.version>
</properties>
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>${junitplatform.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.12.1</version>
<configuration>
<release>${jdk.version}</release>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>jni</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<executions>
<execution>
<phase>generate-sources</phase>
</execution>
</executions>
<configuration>
<compilerArgs>
<arg>-h</arg>
<arg>${project.build.directory}/include</arg>
</compilerArgs>
<compileSourceRoots>src/main/native</compileSourceRoots>
<outputDirectory>${project.build.directory}/include</outputDirectory>
<release>${jdk.version}</release>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>1.0-M1</version>
<extensions>true</extensions>
<configuration>
<sources>
<source>
<directory>src/main/c</directory>
<includes>
<include>**/*.c</include>
</includes>
</source>
</sources>
<linkerFinalName>libnio_uring</linkerFinalName>
<linkerFinalNameExt>so</linkerFinalNameExt>
<compilerStartOptions>
<compilerStartOption>-Os -c -fPIC -I${JAVA_HOME}/include -I${JAVA_HOME}/include/linux -I${project.build.directory}/include</compilerStartOption>
</compilerStartOptions>
<linkerStartOptions>
<linkerStartOption>-shared</linkerStartOption>
</linkerStartOptions>
<linkerEndOptions>
<linkerEndOption>-luring</linkerEndOption>
</linkerEndOptions>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<distributionManagement>
<repository>
<id>gitlab-maven</id>
<url>${env.CI_API_V4_URL}/projects/${env.CI_PROJECT_ID}/packages/maven</url>
</repository>
<snapshotRepository>
<id>gitlab-maven</id>
<url>${env.CI_API_V4_URL}/projects/${env.CI_PROJECT_ID}/packages/maven</url>
</snapshotRepository>
</distributionManagement>
</project>
module sh.blake.niouring {
opens sh.blake.niouring;
exports sh.blake.niouring;
exports sh.blake.niouring.util;
}
package sh.blake.niouring;
import org.eclipse.collections.impl.map.mutable.primitive.LongObjectHashMap;
import sh.blake.niouring.util.NativeLibraryLoader;
import sh.blake.niouring.util.ReferenceCounter;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
/**
......@@ -12,8 +13,8 @@ import java.util.function.Consumer;
*/
public abstract class AbstractIoUringChannel {
private final int fd;
private final LongObjectHashMap<ReferenceCounter<ByteBuffer>> readBufferMap = new LongObjectHashMap<>();
private final LongObjectHashMap<ReferenceCounter<ByteBuffer>> writeBufferMap = new LongObjectHashMap<>();
private final Map<Long,ReferenceCounter<ByteBuffer>> readBufferMap = new HashMap<>();
private final Map<Long,ReferenceCounter<ByteBuffer>> writeBufferMap = new HashMap<>();
private boolean closed = false;
private Consumer<ByteBuffer> readHandler;
private Consumer<ByteBuffer> writeHandler;
......@@ -146,7 +147,7 @@ public abstract class AbstractIoUringChannel {
*
* @return the read buffer map
*/
LongObjectHashMap<ReferenceCounter<ByteBuffer>> readBufferMap() {
Map<Long,ReferenceCounter<ByteBuffer>> readBufferMap() {
return readBufferMap;
}
......@@ -155,7 +156,7 @@ public abstract class AbstractIoUringChannel {
*
* @return the write buffer map
*/
LongObjectHashMap<ReferenceCounter<ByteBuffer>> writeBufferMap() {
Map<Long,ReferenceCounter<ByteBuffer>> writeBufferMap() {
return writeBufferMap;
}
......
package sh.blake.niouring;
import org.eclipse.collections.impl.map.mutable.primitive.IntObjectHashMap;
import sh.blake.niouring.util.ReferenceCounter;
import sh.blake.niouring.util.NativeLibraryLoader;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
/**
......@@ -20,7 +21,7 @@ public class IoUring {
private final long ring;
private final int ringSize;
private final IntObjectHashMap<AbstractIoUringChannel> fdToSocket = new IntObjectHashMap<>();
private final Map<Integer,AbstractIoUringChannel> fdToSocket = new HashMap<>();
private Consumer<Exception> exceptionHandler;
private boolean closed = false;
private final long cqes;
......
......@@ -15,7 +15,7 @@ public class ByteBufferUtil {
* @return the byte buffer
*/
public static ByteBuffer wrapDirect(byte[] data) {
return (ByteBuffer) ByteBuffer.allocateDirect(data.length).put(data).flip();
return ByteBuffer.allocateDirect(data.length).put(data).flip();
}
/**
......@@ -26,7 +26,7 @@ public class ByteBufferUtil {
*/
public static ByteBuffer wrapDirect(String utf8) {
byte[] data = utf8.getBytes(StandardCharsets.UTF_8);
return (ByteBuffer) ByteBuffer.allocateDirect(data.length).put(data).flip();
return ByteBuffer.allocateDirect(data.length).put(data).flip();
}
/**
......@@ -41,6 +41,6 @@ public class ByteBufferUtil {
return buffer;
}
buffer.flip();
return (ByteBuffer) ByteBuffer.allocateDirect(buffer.remaining()).put(buffer).flip();
return ByteBuffer.allocateDirect(buffer.remaining()).put(buffer).flip();
}
}
package sh.blake.niouring.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
public class NativeLibraryLoader {
private static boolean loadAttempted = false;
......@@ -16,20 +16,16 @@ public class NativeLibraryLoader {
}
loadAttempted = true;
try (InputStream inputStream = NativeLibraryLoader.class.getResourceAsStream("/libnio_uring.so")) {
if (inputStream == null) {
throw new IOException("Native library not found");
}
File tempFile = File.createTempFile("libnio_uring", ".tmp");
var arch = System.getProperty("os.arch");
var os = System.getProperty("os.name");
byte[] buffer = new byte[8192];
try (FileOutputStream fileOutputStream = new FileOutputStream(tempFile)) {
while (inputStream.available() > 0) {
int bytesRead = inputStream.read(buffer);
fileOutputStream.write(buffer, 0, bytesRead);
}
}
try (InputStream inputStream =
NativeLibraryLoader.class.getResourceAsStream(os + "/" + arch + "/libnio_uring.so")) {
if (inputStream == null)
throw new IOException("Native library not found");
File tempFile = File.createTempFile("libnio_uring", ".tmp");
Files.write(tempFile.toPath(), inputStream.readAllBytes());
System.load(tempFile.getAbsolutePath());
}
} catch (IOException ex) {
......
File added
package sh.blake.niouring;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
......@@ -31,7 +31,7 @@ public class IoUringFileTest extends TestBase {
attemptUntil(ioUring::execute, readSuccessfully::get);
Assert.assertTrue("File read successfully", readSuccessfully.get());
Assertions.assertTrue(readSuccessfully.get(), "File read successfully");
}
@Test
......@@ -62,6 +62,6 @@ public class IoUringFileTest extends TestBase {
ioUring.queueRead(file, buffer);
attemptUntil(ioUring::execute, seekSuccessfully::get);
Assert.assertTrue("File seek successfully", seekSuccessfully.get());
Assertions.assertTrue(seekSuccessfully.get(), "File seek successfully");
}
}
package sh.blake.niouring;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import sh.blake.niouring.util.ByteBufferUtil;
import java.nio.ByteBuffer;
......@@ -39,8 +39,8 @@ public class IoUringSocketTest extends TestBase {
ioUring.close();
Assert.assertTrue("Server accepted connection", accepted.get());
Assert.assertTrue("Client connected", connected.get());
Assertions.assertTrue(accepted.get(), "Server accepted connection");
Assertions.assertTrue(connected.get(), "Client connected");
}
@Test
......@@ -68,8 +68,8 @@ public class IoUringSocketTest extends TestBase {
serverSocket.close();
socket.close();
Assert.assertFalse("Server accepted connection", accepted.get());
Assert.assertTrue("Client to connect (wrong port)", exceptionProduced.get());
Assertions.assertFalse(accepted.get(), "Server accepted connection");
Assertions.assertTrue(exceptionProduced.get(), "Client to connect (wrong port)");
}
@Test
......@@ -117,9 +117,9 @@ public class IoUringSocketTest extends TestBase {
ioUring.close();
Assert.assertTrue("Server accepted connection", serverAccepted.get());
Assert.assertTrue("Server sent data", serverSent.get());
Assert.assertTrue("Client connected", clientConnected.get());
Assert.assertTrue("Client received data", clientReceived.get());
Assertions.assertTrue(serverAccepted.get(), "Server accepted connection");
Assertions.assertTrue(serverSent.get(), "Server sent data");
Assertions.assertTrue(clientConnected.get(), "Client connected");
Assertions.assertTrue(clientReceived.get(), "Client received data");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment