Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #11432 - Review number of acceptor threads. #11436

Merged
merged 2 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public AbstractConnector(

int cores = ProcessorUtils.availableProcessors();
if (acceptors < 0)
acceptors = Math.max(1, Math.min(4, cores / 8));
acceptors = 1;
if (acceptors > cores)
LOG.warn("Acceptors should be <= availableProcessors: {} ", this);
_acceptors = new Thread[acceptors];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class LocalConnector extends AbstractConnector

public LocalConnector(Server server, Executor executor, Scheduler scheduler, ByteBufferPool bufferPool, int acceptors, ConnectionFactory... factories)
{
super(server, executor, scheduler, bufferPool, acceptors, factories);
super(server, executor, scheduler, bufferPool, Math.max(1, acceptors), factories);
setIdleTimeout(30000);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,17 @@ public void setAcceptedSendBufferSize(int acceptedSendBufferSize)
protected void doStart() throws Exception
{
getBeans(SelectorManager.SelectorManagerListener.class).forEach(selectorManager::addEventListener);

serverChannel = open();
addBean(serverChannel);

super.doStart();

if (getAcceptors() == 0)
{
serverChannel.configureBlocking(false);
acceptor.set(selectorManager.acceptor(serverChannel));
}
}

@Override
Expand Down Expand Up @@ -200,25 +208,6 @@ public Object getTransport()
}

private ServerSocketChannel open() throws IOException
{
ServerSocketChannel serverChannel = openServerSocketChannel();
if (getAcceptors() == 0)
{
serverChannel.configureBlocking(false);
acceptor.set(selectorManager.acceptor(serverChannel));
}
return serverChannel;
}

private void close() throws IOException
{
ServerSocketChannel serverChannel = this.serverChannel;
this.serverChannel = null;
IO.close(serverChannel);
Files.deleteIfExists(getUnixDomainPath());
}

private ServerSocketChannel openServerSocketChannel() throws IOException
{
ServerSocketChannel serverChannel = null;
if (isInheritChannel())
Expand All @@ -234,6 +223,14 @@ private ServerSocketChannel openServerSocketChannel() throws IOException
return serverChannel;
}

private void close() throws IOException
{
ServerSocketChannel serverChannel = this.serverChannel;
this.serverChannel = null;
IO.close(serverChannel);
Files.deleteIfExists(getUnixDomainPath());
}

private ServerSocketChannel bindServerSocketChannel() throws IOException
{
Path unixDomainPath = getUnixDomainPath();
Expand Down
9 changes: 4 additions & 5 deletions tests/jetty-jmh/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-slf4j-impl</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
Expand All @@ -51,11 +55,6 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-slf4j-impl</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package org.eclipse.jetty.server.jmh;

import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.LongAdder;

import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.HttpTester;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.Callback;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.openjdk.jmh.runner.options.TimeValue;

@State(Scope.Benchmark)
public class ServerConnectorAcceptBenchmark
{
public static void main(String[] args) throws Exception
{
Options opt = new OptionsBuilder()
.include(ServerConnectorAcceptBenchmark.class.getSimpleName())
.warmupIterations(10)
.warmupTime(TimeValue.milliseconds(500))
.measurementIterations(10)
.measurementTime(TimeValue.milliseconds(500))
.forks(1)
.threads(20)
.build();
new Runner(opt).run();
}

// @Param({"0", "1", "2", "4"})
@Param({"4", "2", "1", "0"})
public int acceptors;

final LongAdder count = new LongAdder();

Server server;
ServerConnector connector;

@Setup
public void prepare() throws Exception
{
server = new Server();
connector = new ServerConnector(server, acceptors, -1);
server.addConnector(connector);
server.setHandler(new Handler.Abstract()
{
@Override
public boolean handle(Request request, Response response, Callback callback) throws Exception
{
callback.succeeded();
return true;
}
});
server.start();
}

@TearDown
public void dispose() throws Exception
{
System.err.println("count = " + count.sum());
server.stop();
}

@Benchmark
@BenchmarkMode({Mode.Throughput})
public void accept() throws Exception
{
count.increment();
try (SocketChannel channel = SocketChannel.open(new InetSocketAddress("localhost", connector.getLocalPort())))
{
channel.write(StandardCharsets.US_ASCII.encode("GET / HTTP/1.0\r\n\r\n"));
HttpTester.Response response = HttpTester.parseResponse(HttpTester.from(channel));
if (response.getStatus() != HttpStatus.OK_200)
System.err.println("x = " + response);
}
catch (Throwable x)
{
System.err.println("x = " + x);
}
}
}
Loading