Skip to content

Commit

Permalink
Consider same port
Browse files Browse the repository at this point in the history
  • Loading branch information
minwoox committed Feb 24, 2025
1 parent 0aae5c1 commit e2ca3d5
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ final class DefaultServerConfig implements ServerConfig {

@Nullable
private final Mapping<String, SslContext> sslContexts;
private final ServerMetrics serverMetrics = new ServerMetrics();
private final ServerMetrics serverMetrics;

@Nullable
private String strVal;
Expand Down Expand Up @@ -266,6 +266,7 @@ final class DefaultServerConfig implements ServerConfig {
this.absoluteUriTransformer = castAbsoluteUriTransformer;
this.unloggedExceptionsReportIntervalMillis = unloggedExceptionsReportIntervalMillis;
this.shutdownSupports = ImmutableList.copyOf(requireNonNull(shutdownSupports, "shutdownSupports"));
serverMetrics = new ServerMetrics(meterRegistry);
}

private static Int2ObjectMap<Mapping<String, VirtualHost>> buildDomainAndPortMapping(
Expand Down
28 changes: 23 additions & 5 deletions core/src/main/java/com/linecorp/armeria/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,25 @@ private ChannelFuture doStart(ServerPort port) {
final GracefulShutdownSupport gracefulShutdownSupport = this.gracefulShutdownSupport;
assert gracefulShutdownSupport != null;

final ServerPortMetric serverPortMetric = new ServerPortMetric();
ServerPortMetric serverPortMetric = null;
lock.lock();
try {
for (ServerPort serverPort : activePorts.values()) {
final InetSocketAddress localAddress = serverPort.localAddress();
if (!(localAddress instanceof DomainSocketAddress) &&
localAddress.getPort() == port.localAddress().getPort()) {
// Because we use the port number for aggregating metrics, use the previous
// serverPortMetric.
serverPortMetric = serverPort.serverPortMetric();
break;
}
}
} finally {
lock.unlock();
}
if (serverPortMetric == null) {
serverPortMetric = new ServerPortMetric();
}
b.group(bossGroup, config.workerGroup());
b.handler(connectionLimitingHandler.newChildHandler(serverPortMetric));
b.childHandler(new HttpServerPipelineConfigurator(config, port, gracefulShutdownSupport,
Expand Down Expand Up @@ -837,6 +855,9 @@ public void operationComplete(ChannelFuture f) {
logger.warn("Unexpected local address type: {}", localAddress.getClass().getName());
return;
}
final ServerPortMetric serverPortMetric = ch.attr(SERVER_PORT_METRIC).get();
assert serverPortMetric != null;
actualPort.setServerPortMetric(serverPortMetric);

// Update the boss thread so its name contains the actual port.
Thread.currentThread().setName(bossThreadName(actualPort));
Expand All @@ -849,10 +870,7 @@ public void operationComplete(ChannelFuture f) {
lock.unlock();
}

final ServerPortMetric serverPortMetric = ch.attr(SERVER_PORT_METRIC).get();
assert serverPortMetric != null;
serverPortMetric.bindTo(config.meterRegistry(), actualPort);
config.serverMetrics().addServerPortMetric(serverPortMetric);
config.serverMetrics().addServerPort(actualPort);

if (logger.isInfoEnabled()) {
if (isLocalPort(actualPort)) {
Expand Down
21 changes: 15 additions & 6 deletions core/src/main/java/com/linecorp/armeria/server/ServerMetrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@

package com.linecorp.armeria.server;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

import com.google.common.base.MoreObjects;
import com.google.common.primitives.Ints;

import com.linecorp.armeria.common.annotation.UnstableApi;

import io.micrometer.core.instrument.MeterRegistry;

/**
* A class that holds metrics related server.
*/
Expand All @@ -33,12 +35,19 @@ public final class ServerMetrics {
static final String ALL_REQUESTS_METER_NAME = "armeria.server.all.requests";
static final String ALL_CONNECTIONS_METER_NAME = "armeria.server.connections";

private final List<ServerPortMetric> serverPortMetrics = new CopyOnWriteArrayList<>();
private final Set<ServerPortMetric> serverPortMetrics = new CopyOnWriteArraySet<>();
private final MeterRegistry meterRegistry;

ServerMetrics() {}
ServerMetrics(MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
}

void addServerPortMetric(ServerPortMetric serverPortMetric) {
serverPortMetrics.add(serverPortMetric);
void addServerPort(ServerPort serverPort) {
final ServerPortMetric serverPortMetric = serverPort.serverPortMetric();
assert serverPortMetric != null;
if (serverPortMetrics.add(serverPortMetric)) {
serverPortMetric.bindTo(meterRegistry, serverPort);
}
}

/**
Expand Down
12 changes: 12 additions & 0 deletions core/src/main/java/com/linecorp/armeria/server/ServerPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ static long nextPortGroup() {
private final Set<SessionProtocol> protocols;
private final long portGroup;
private int hashCode;
@Nullable
private ServerPortMetric serverPortMetric;

@Nullable
private String strVal;
Expand Down Expand Up @@ -226,6 +228,16 @@ long portGroup() {
return portGroup;
}

@Nullable
ServerPortMetric serverPortMetric() {
return serverPortMetric;
}

void setServerPortMetric(ServerPortMetric serverPortMetric) {
this.serverPortMetric = serverPortMetric;
}

// Do not take account into serverPortMetric for equality and hashCode.
@Override
public int hashCode() {
int hashCode = this.hashCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ void bindTo(MeterRegistry meterRegistry, ServerPort actualPort) {
this, ServerPortMetric::activeConnections);
}

// Use reference equality for comparison.

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
Expand Down

0 comments on commit e2ca3d5

Please sign in to comment.