Skip to content

Commit

Permalink
Move factory methods to IOUringHandler (#14023)
Browse files Browse the repository at this point in the history
Motivation:

In all our other transport implementations we have the factory methods
in the IoHandler implementation itself. Let's be consistent

Modifications:

Move methods from IOUring to IOUringHandler.

Result:

More consistent API
  • Loading branch information
normanmaurer committed May 1, 2024
1 parent 1d86f61 commit f7eb1e7
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package io.netty5.channel.uring;

import io.netty5.channel.IoHandlerFactory;
import io.netty5.util.internal.PlatformDependent;
import io.netty5.util.internal.SystemPropertyUtil;
import org.slf4j.Logger;
Expand Down Expand Up @@ -82,30 +81,6 @@ public static Throwable unavailabilityCause() {
return UNAVAILABILITY_CAUSE;
}

public static IoHandlerFactory newFactory() {
ensureAvailability();
return () -> {
RingBuffer ringBuffer = Native.createRingBuffer();
return new IOUringHandler(ringBuffer);
};
}

public static IoHandlerFactory newFactory(int ringSize) {
ensureAvailability();
return () -> {
RingBuffer ringBuffer = Native.createRingBuffer(ringSize);
return new IOUringHandler(ringBuffer);
};
}

public static IoHandlerFactory newFactory(int ringSize, int kernelWorkerOffloadThreshold) {
ensureAvailability();
return () -> {
RingBuffer ringBuffer = Native.createRingBuffer(ringSize, kernelWorkerOffloadThreshold);
return new IOUringHandler(ringBuffer);
};
}

private IOUring() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.netty5.channel.IoExecutionContext;
import io.netty5.channel.IoHandle;
import io.netty5.channel.IoHandler;
import io.netty5.channel.IoHandlerFactory;
import io.netty5.channel.unix.FileDescriptor;
import io.netty5.util.collection.IntObjectHashMap;
import io.netty5.util.collection.IntObjectMap;
Expand All @@ -38,7 +39,7 @@
/**
* {@link IoHandler} which is implemented in terms of the Linux-specific {@code io_uring} API.
*/
final class IOUringHandler implements IoHandler, CompletionCallback {
public final class IOUringHandler implements IoHandler, CompletionCallback {
private static final Logger logger = LoggerFactory.getLogger(IOUringHandler.class);
private static final short RING_CLOSE = 1;

Expand Down Expand Up @@ -320,4 +321,28 @@ public void wakeup(boolean inEventLoop) {
public boolean isCompatible(Class<? extends IoHandle> handleType) {
return AbstractIOUringChannel.class.isAssignableFrom(handleType);
}

public static IoHandlerFactory newFactory() {
IOUring.ensureAvailability();
return () -> {
RingBuffer ringBuffer = Native.createRingBuffer();
return new IOUringHandler(ringBuffer);
};
}

public static IoHandlerFactory newFactory(int ringSize) {
IOUring.ensureAvailability();
return () -> {
RingBuffer ringBuffer = Native.createRingBuffer(ringSize);
return new IOUringHandler(ringBuffer);
};
}

public static IoHandlerFactory newFactory(int ringSize, int kernelWorkerOffloadThreshold) {
IOUring.ensureAvailability();
return () -> {
RingBuffer ringBuffer = Native.createRingBuffer(ringSize, kernelWorkerOffloadThreshold);
return new IOUringHandler(ringBuffer);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static void loadJNI() {

@Override
protected EventLoopGroup newGroup() {
return new MultithreadEventLoopGroup(2, IOUring.newFactory());
return new MultithreadEventLoopGroup(2, IOUringHandler.newFactory());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import io.netty5.channel.MultithreadEventLoopGroup;
import io.netty5.channel.ServerChannel;
import io.netty5.testsuite.transport.AbstractSingleThreadEventLoopTest;
import io.netty5.util.concurrent.Future;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
Expand All @@ -44,7 +43,7 @@ public static void loadJNI() {

@Override
protected IoHandlerFactory newIoHandlerFactory() {
return IOUring.newFactory();
return IOUringHandler.newFactory();
}

@Override
Expand Down Expand Up @@ -85,7 +84,7 @@ public void shutsDownGracefully() throws Exception {

@Test
public void testSchedule() throws Exception {
EventLoopGroup group = new MultithreadEventLoopGroup(1, IOUring.newFactory());
EventLoopGroup group = new MultithreadEventLoopGroup(1, IOUringHandler.newFactory());
try {
EventLoop loop = group.next();
loop.schedule(EMPTY_RUNNABLE, 1, TimeUnit.SECONDS).asStage().sync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void testRemoteAddressIpv6ServerAutoDetect() throws Exception {

private static void testRemoteAddress(InetAddress server, InetAddress client) throws Exception {
final Promise<SocketAddress> promise = ImmediateEventExecutor.INSTANCE.newPromise();
EventLoopGroup bossGroup = new MultithreadEventLoopGroup(1, IOUring.newFactory());
EventLoopGroup bossGroup = new MultithreadEventLoopGroup(1, IOUringHandler.newFactory());
Socket socket = new Socket();
try {
ServerBootstrap b = new ServerBootstrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public class IOUringSocketTestPermutation extends SocketTestPermutation {
static final IOUringSocketTestPermutation INSTANCE = new IOUringSocketTestPermutation();

static final EventLoopGroup IO_URING_BOSS_GROUP = new MultithreadEventLoopGroup(
BOSSES, new DefaultThreadFactory("testsuite-io_uring-boss", true), IOUring.newFactory());
BOSSES, new DefaultThreadFactory("testsuite-io_uring-boss", true), IOUringHandler.newFactory());
static final EventLoopGroup IO_URING_WORKER_GROUP = new MultithreadEventLoopGroup(
WORKERS, new DefaultThreadFactory("testsuite-io_uring-worker", true), IOUring.newFactory());
WORKERS, new DefaultThreadFactory("testsuite-io_uring-worker", true), IOUringHandler.newFactory());

private static final Logger logger = LoggerFactory.getLogger(IOUringSocketTestPermutation.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static void loadJNI() {

private static void ioUringTest() throws Exception {
Class<? extends ServerSocketChannel> clazz = IOUringServerSocketChannel.class;
final EventLoopGroup bossGroup = new MultithreadEventLoopGroup(1, IOUring.newFactory());
final EventLoopGroup bossGroup = new MultithreadEventLoopGroup(1, IOUringHandler.newFactory());

try {
ServerBootstrap b = new ServerBootstrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.netty5.channel.MultithreadEventLoopGroup;
import io.netty5.channel.socket.SocketChannel;
import io.netty5.channel.uring.IOUring;
import io.netty5.channel.uring.IOUringHandler;
import io.netty5.channel.uring.IOUringServerSocketChannel;
import io.netty5.handler.logging.LogLevel;
import io.netty5.handler.logging.LoggingHandler;
Expand All @@ -31,8 +32,8 @@ public class EchoIOUringServer {
private static final int PORT = Integer.parseInt(System.getProperty("port", "8080"));

public static void main(String []args) throws Exception {
EventLoopGroup bossGroup = new MultithreadEventLoopGroup(1, IOUring.newFactory());
EventLoopGroup workerGroup = new MultithreadEventLoopGroup(1, IOUring.newFactory());
EventLoopGroup bossGroup = new MultithreadEventLoopGroup(1, IOUringHandler.newFactory());
EventLoopGroup workerGroup = new MultithreadEventLoopGroup(1, IOUringHandler.newFactory());
final EchoIOUringServerHandler serverHandler = new EchoIOUringServerHandler();
try {
ServerBootstrap b = new ServerBootstrap();
Expand Down

0 comments on commit f7eb1e7

Please sign in to comment.