From 596407961635d71c5d9310c0e89ea865cbf440d0 Mon Sep 17 00:00:00 2001 From: "eunjung.yoo" Date: Sun, 8 Sep 2024 02:07:15 +0900 Subject: [PATCH 1/3] Add pendingResponse to ServerMetrics --- .../server/GracefulShutdownSupport.java | 42 +++++++++++++------ .../com/linecorp/armeria/server/Server.java | 6 +-- .../armeria/server/ServerMetrics.java | 21 ++++++++++ .../server/GracefulShutdownSupportTest.java | 8 +++- 4 files changed, 59 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/com/linecorp/armeria/server/GracefulShutdownSupport.java b/core/src/main/java/com/linecorp/armeria/server/GracefulShutdownSupport.java index d2865092d80..4313ce344c7 100644 --- a/core/src/main/java/com/linecorp/armeria/server/GracefulShutdownSupport.java +++ b/core/src/main/java/com/linecorp/armeria/server/GracefulShutdownSupport.java @@ -19,7 +19,6 @@ import java.time.Duration; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.atomic.LongAdder; import com.google.common.base.Ticker; @@ -29,39 +28,48 @@ */ abstract class GracefulShutdownSupport { - static GracefulShutdownSupport create(Duration quietPeriod, Executor blockingTaskExecutor) { - return create(quietPeriod, blockingTaskExecutor, Ticker.systemTicker()); + private final ServerMetrics serverMetrics; + + static GracefulShutdownSupport create(Duration quietPeriod, Executor blockingTaskExecutor, + ServerMetrics serverMetrics) { + return create(quietPeriod, blockingTaskExecutor, Ticker.systemTicker(), serverMetrics); } - static GracefulShutdownSupport create(Duration quietPeriod, Executor blockingTaskExecutor, Ticker ticker) { - return new DefaultGracefulShutdownSupport(quietPeriod, blockingTaskExecutor, ticker); + static GracefulShutdownSupport create(Duration quietPeriod, Executor blockingTaskExecutor, Ticker ticker, + ServerMetrics serverMetrics) { + return new DefaultGracefulShutdownSupport(quietPeriod, blockingTaskExecutor, ticker, serverMetrics); } - static GracefulShutdownSupport createDisabled() { - return new DisabledGracefulShutdownSupport(); + static GracefulShutdownSupport createDisabled(ServerMetrics serverMetrics) { + return new DisabledGracefulShutdownSupport(serverMetrics); } - private final LongAdder pendingResponses = new LongAdder(); + /** + * Creates a new instance. + */ + private GracefulShutdownSupport(ServerMetrics serverMetrics) { + this.serverMetrics = serverMetrics; + } /** * Increases the number of pending responses. */ final void inc() { - pendingResponses.increment(); + serverMetrics.increasePendingResponse(); } /** * Decreases the number of pending responses. */ void dec() { - pendingResponses.decrement(); + serverMetrics.decreasePendingResponse(); } /** * Returns the number of pending responses. */ final long pendingResponses() { - return pendingResponses.sum(); + return serverMetrics.pendingResponses(); } /** @@ -78,6 +86,14 @@ private static final class DisabledGracefulShutdownSupport extends GracefulShutd private volatile boolean shuttingDown; + /** + * Creates a new instance. + * + */ + private DisabledGracefulShutdownSupport(ServerMetrics serverMetrics) { + super(serverMetrics); + } + @Override boolean isShuttingDown() { return shuttingDown; @@ -102,7 +118,9 @@ private static final class DefaultGracefulShutdownSupport extends GracefulShutdo private long lastResTimeNanos; private volatile long shutdownStartTimeNanos; - DefaultGracefulShutdownSupport(Duration quietPeriod, Executor blockingTaskExecutor, Ticker ticker) { + private DefaultGracefulShutdownSupport(Duration quietPeriod, Executor blockingTaskExecutor, Ticker ticker, + ServerMetrics serverMetrics) { + super(serverMetrics); quietPeriodNanos = quietPeriod.toNanos(); this.blockingTaskExecutor = blockingTaskExecutor; this.ticker = ticker; diff --git a/core/src/main/java/com/linecorp/armeria/server/Server.java b/core/src/main/java/com/linecorp/armeria/server/Server.java index 9beb62f4ab2..508a5095b77 100644 --- a/core/src/main/java/com/linecorp/armeria/server/Server.java +++ b/core/src/main/java/com/linecorp/armeria/server/Server.java @@ -502,11 +502,11 @@ private final class ServerStartStopSupport extends StartStopSupport doStart(@Nullable Void arg) { if (config().gracefulShutdownQuietPeriod().isZero()) { - gracefulShutdownSupport = GracefulShutdownSupport.createDisabled(); + gracefulShutdownSupport = GracefulShutdownSupport.createDisabled(config.serverMetrics()); } else { gracefulShutdownSupport = GracefulShutdownSupport.create(config().gracefulShutdownQuietPeriod(), - config().blockingTaskExecutor()); + config().blockingTaskExecutor(), config.serverMetrics()); } // Initialize the server sockets asynchronously. @@ -585,8 +585,6 @@ private void setupServerMetrics() { final GracefulShutdownSupport gracefulShutdownSupport = this.gracefulShutdownSupport; assert gracefulShutdownSupport != null; - meterRegistry.gauge("armeria.server.pending.responses", gracefulShutdownSupport, - GracefulShutdownSupport::pendingResponses); config.serverMetrics().bindTo(meterRegistry); } diff --git a/core/src/main/java/com/linecorp/armeria/server/ServerMetrics.java b/core/src/main/java/com/linecorp/armeria/server/ServerMetrics.java index 9a57bf65888..0d38ab2708a 100644 --- a/core/src/main/java/com/linecorp/armeria/server/ServerMetrics.java +++ b/core/src/main/java/com/linecorp/armeria/server/ServerMetrics.java @@ -39,6 +39,7 @@ public final class ServerMetrics implements MeterBinder { private final LongAdder activeHttp1WebSocketRequests = new LongAdder(); private final LongAdder activeHttp1Requests = new LongAdder(); private final LongAdder activeHttp2Requests = new LongAdder(); + private final LongAdder pendingResponse = new LongAdder(); /** * AtomicInteger is used to read the number of active connections frequently. @@ -98,6 +99,13 @@ public long activeHttp2Requests() { return activeHttp2Requests.longValue(); } + /** + * Returns the number of pending responses. + */ + public long pendingResponses() { + return pendingResponse.longValue(); + } + /** * Returns the number of open connections. */ @@ -153,6 +161,14 @@ void decreaseActiveConnections() { activeConnections.decrementAndGet(); } + void increasePendingResponse() { + pendingResponse.increment(); + } + + void decreasePendingResponse() { + pendingResponse.decrement(); + } + @Override public void bindTo(MeterRegistry meterRegistry) { meterRegistry.gauge("armeria.server.connections", activeConnections); @@ -174,6 +190,10 @@ public void bindTo(MeterRegistry meterRegistry) { meterRegistry.gauge(allRequestsMeterName, ImmutableList.of(Tag.of("protocol", "http1.websocket"), Tag.of("state", "active")), activeHttp1WebSocketRequests); + // pending responses + meterRegistry.gauge(allRequestsMeterName, + ImmutableList.of(Tag.of("protocol", "all"), Tag.of("state", "active")), + pendingResponse); } @Override @@ -185,6 +205,7 @@ public String toString() { .add("pendingHttp2Requests", pendingHttp2Requests) .add("activeHttp2Requests", activeHttp2Requests) .add("activeConnections", activeConnections) + .add("pendingResponse", pendingResponse) .toString(); } } diff --git a/core/src/test/java/com/linecorp/armeria/server/GracefulShutdownSupportTest.java b/core/src/test/java/com/linecorp/armeria/server/GracefulShutdownSupportTest.java index 57b66007d2b..04a7c1b3827 100644 --- a/core/src/test/java/com/linecorp/armeria/server/GracefulShutdownSupportTest.java +++ b/core/src/test/java/com/linecorp/armeria/server/GracefulShutdownSupportTest.java @@ -45,6 +45,8 @@ class GracefulShutdownSupportTest { @Mock private Ticker ticker; + private ServerMetrics serverMetrics; + private GracefulShutdownSupport support; private ThreadPoolExecutor executor; @@ -54,7 +56,9 @@ void setUp() { 0, 1, 1, TimeUnit.SECONDS, new LinkedTransferQueue<>(), ThreadFactories.newThreadFactory("graceful-shutdown-test", true)); - support = GracefulShutdownSupport.create(Duration.ofNanos(QUIET_PERIOD_NANOS), executor, ticker); + serverMetrics = new ServerMetrics(); + support = GracefulShutdownSupport.create(Duration.ofNanos(QUIET_PERIOD_NANOS), executor, ticker, + serverMetrics); } @AfterEach @@ -64,7 +68,7 @@ void tearDown() { @Test void testDisabled() { - final GracefulShutdownSupport support = GracefulShutdownSupport.createDisabled(); + final GracefulShutdownSupport support = GracefulShutdownSupport.createDisabled(serverMetrics); assertThat(support.isShuttingDown()).isFalse(); assertThat(support.completedQuietPeriod()).isTrue(); assertThat(support.isShuttingDown()).isTrue(); From 7417ccd6966ab8c3ee04ef2dd6f3dcf28629e84f Mon Sep 17 00:00:00 2001 From: jrhee17 Date: Thu, 19 Sep 2024 11:12:49 +0900 Subject: [PATCH 2/3] minor nits --- .../server/GracefulShutdownSupport.java | 6 +++--- .../linecorp/armeria/server/ServerMetrics.java | 18 ++++++++---------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/com/linecorp/armeria/server/GracefulShutdownSupport.java b/core/src/main/java/com/linecorp/armeria/server/GracefulShutdownSupport.java index 4313ce344c7..27f4c144ac4 100644 --- a/core/src/main/java/com/linecorp/armeria/server/GracefulShutdownSupport.java +++ b/core/src/main/java/com/linecorp/armeria/server/GracefulShutdownSupport.java @@ -55,14 +55,14 @@ private GracefulShutdownSupport(ServerMetrics serverMetrics) { * Increases the number of pending responses. */ final void inc() { - serverMetrics.increasePendingResponse(); + serverMetrics.increasePendingResponses(); } /** * Decreases the number of pending responses. */ void dec() { - serverMetrics.decreasePendingResponse(); + serverMetrics.decreasePendingResponses(); } /** @@ -119,7 +119,7 @@ private static final class DefaultGracefulShutdownSupport extends GracefulShutdo private volatile long shutdownStartTimeNanos; private DefaultGracefulShutdownSupport(Duration quietPeriod, Executor blockingTaskExecutor, Ticker ticker, - ServerMetrics serverMetrics) { + ServerMetrics serverMetrics) { super(serverMetrics); quietPeriodNanos = quietPeriod.toNanos(); this.blockingTaskExecutor = blockingTaskExecutor; diff --git a/core/src/main/java/com/linecorp/armeria/server/ServerMetrics.java b/core/src/main/java/com/linecorp/armeria/server/ServerMetrics.java index 0d38ab2708a..1946656503a 100644 --- a/core/src/main/java/com/linecorp/armeria/server/ServerMetrics.java +++ b/core/src/main/java/com/linecorp/armeria/server/ServerMetrics.java @@ -39,7 +39,7 @@ public final class ServerMetrics implements MeterBinder { private final LongAdder activeHttp1WebSocketRequests = new LongAdder(); private final LongAdder activeHttp1Requests = new LongAdder(); private final LongAdder activeHttp2Requests = new LongAdder(); - private final LongAdder pendingResponse = new LongAdder(); + private final LongAdder pendingResponses = new LongAdder(); /** * AtomicInteger is used to read the number of active connections frequently. @@ -103,7 +103,7 @@ public long activeHttp2Requests() { * Returns the number of pending responses. */ public long pendingResponses() { - return pendingResponse.longValue(); + return pendingResponses.longValue(); } /** @@ -161,12 +161,12 @@ void decreaseActiveConnections() { activeConnections.decrementAndGet(); } - void increasePendingResponse() { - pendingResponse.increment(); + void increasePendingResponses() { + pendingResponses.increment(); } - void decreasePendingResponse() { - pendingResponse.decrement(); + void decreasePendingResponses() { + pendingResponses.decrement(); } @Override @@ -191,9 +191,7 @@ public void bindTo(MeterRegistry meterRegistry) { ImmutableList.of(Tag.of("protocol", "http1.websocket"), Tag.of("state", "active")), activeHttp1WebSocketRequests); // pending responses - meterRegistry.gauge(allRequestsMeterName, - ImmutableList.of(Tag.of("protocol", "all"), Tag.of("state", "active")), - pendingResponse); + meterRegistry.gauge("armeria.server.pending.responses", pendingResponses); } @Override @@ -205,7 +203,7 @@ public String toString() { .add("pendingHttp2Requests", pendingHttp2Requests) .add("activeHttp2Requests", activeHttp2Requests) .add("activeConnections", activeConnections) - .add("pendingResponse", pendingResponse) + .add("pendingResponses", pendingResponses) .toString(); } } From ae2d48453120340e174dcb7c923697f418f9560f Mon Sep 17 00:00:00 2001 From: jrhee17 Date: Thu, 19 Sep 2024 12:34:11 +0900 Subject: [PATCH 3/3] lint --- .../com/linecorp/armeria/server/GracefulShutdownSupport.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/com/linecorp/armeria/server/GracefulShutdownSupport.java b/core/src/main/java/com/linecorp/armeria/server/GracefulShutdownSupport.java index 27f4c144ac4..8e61b48df92 100644 --- a/core/src/main/java/com/linecorp/armeria/server/GracefulShutdownSupport.java +++ b/core/src/main/java/com/linecorp/armeria/server/GracefulShutdownSupport.java @@ -118,8 +118,8 @@ private static final class DefaultGracefulShutdownSupport extends GracefulShutdo private long lastResTimeNanos; private volatile long shutdownStartTimeNanos; - private DefaultGracefulShutdownSupport(Duration quietPeriod, Executor blockingTaskExecutor, Ticker ticker, - ServerMetrics serverMetrics) { + private DefaultGracefulShutdownSupport(Duration quietPeriod, Executor blockingTaskExecutor, + Ticker ticker, ServerMetrics serverMetrics) { super(serverMetrics); quietPeriodNanos = quietPeriod.toNanos(); this.blockingTaskExecutor = blockingTaskExecutor;