From 4db443e7bd896a9a8453da613c4f5ef728e1ba69 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Fri, 20 Nov 2026 18:21:57 +0900 Subject: [PATCH 1/2] =?UTF-8?q?test(grpc):=20drain=EC=9D=84=20=EB=8B=A8?= =?UTF-8?q?=EC=96=B8=ED=95=98=EC=A7=80=20=EC=95=8A=EB=8A=94=20=EC=8B=9C?= =?UTF-8?q?=ED=97=98=EC=9D=B4=20=EC=A2=85=EB=A3=8C=20drain=202=EC=B4=88?= =?UTF-8?q?=EB=A5=BC=20=EA=B8=B0=EB=8B=A4=EB=A6=AC=EC=A7=80=20=EC=95=8A?= =?UTF-8?q?=EB=8A=94=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit gRPC 시험은 시험마다 ApplicationContextRunner로 서버를 띄우고 닫는다. 닫을 때 GrpcServerRunner.stop()이 health를 NOT_SERVING으로 바꾼 뒤 shutdown-drain-seconds만큼 잔다. 시험이 그 값을 주지 않아 운영 기본값 2초를 시험마다 기다린다. GrpcP1BoundaryWireTest는 시험 16개가 모두 2.1초 이상이고(합 38.6초), GrpcServerRunnerBootTest의 둘도 같다 (design-docs ci/05-backend-ci-speed-handoff.md 12.3절). drain을 단언하지 않는 시험의 context에 app.grpc.server.shutdown-drain-seconds=0을 준다. - GrpcP1BoundaryWireTest·GrpcRequiresPermissionWireTest· GrpcServerRunnerBootTest: 클래스가 함께 쓰는 runner에 둔다. - GrpcServerRunnerBootTest의 drain 시험은 자기 값(drain 6초, grace 8초)을 그대로 준다. 뒤에 준 값이 이긴다. - GrpcSafeActivationTest: listener를 띄우는 시험은 theVendorPlatformNamespaceBesideTheAdapterDoesNotFailItsStrictBinding 하나다. 그 시험에만 준다. drain 2초 > grace 1초로 기동 실패를 보는 시험은 그대로다. main 기본값(shutdownDrainSeconds = 2)과 단언은 바꾸지 않는다. 버린 안: 시험용 설정 파일 하나로 모든 시험에 drain 0을 주는 안. drain 시험이 기본값에서 출발하는지 시험 코드만 보고 알 수 없게 된다. 또 ApplicationContextRunner는 application.properties를 읽지 않아서, runner마다 설정 파일을 읽는 initializer를 더해야 한다. 위험: drain 대기가 없어지면 닫을 때 바로 shutdown()이 불리고, 진행 중인 호출은 awaitTermination이 grace 5초 동안 기다린다(전에는 drain 2초를 뺀 3초). 느린 handler(SLOW·GATED)가 다음 시험에 남지 않는 근거는 코드다. - 여러 시험이 나누는 static은 gate 하나다(GrpcP1BoundaryWireTest.java:91). 쓰는 시험마다 새 latch를 두고 단언 전에 연다. - 남은 handler는 stop()의 work.shutdownNow()가 interrupt하고, holdUntilReleased는 InterruptedException을 받아 false로 끝난다. - context는 ApplicationContextRunner.run() 안에서 동기로 닫힌다. JUnit 병렬 실행 설정은 없다. 순서 섞기 실행은 닫는 시점의 경합까지 보이지는 못한다. 검증(desktop, 이 트리 6f50d01b, 기준 0ecfb56f): - JUnit XML 시험 시간 합(초). --rerun --no-build-cache, nice 19, runner가 쉴 때 기준과 번갈아 2회. 이 desktop의 기준값은 38.6초가 아니라 34.3초다. GrpcP1BoundaryWireTest test 34.28/34.30 -> 2.27/2.26, 같은 클래스 lane grpcTransportQualificationTest 34.44/34.48 -> 2.35/2.46, GrpcServerRunnerBootTest 10.14/10.12 -> 6.11/6.11, GrpcRequiresPermissionWireTest 4.17/4.16 -> 0.18/0.18, GrpcSafeActivationTest 2.13/2.12 -> 0.13/0.14. - 시험 수·이름: test와 lane 합 115개가 네 실행 모두 같다. - 순서 섞기(method·class Random, seed 11·22·33, test): 셋 다 rc=0, 이름이 기준과 같다. - scripts/verify.sh leaf :adapter:inbound:grpc rc=0(84개, skip 0), pr --no-cache rc=0(7269개, skip 35), main rc=0(1980개, skip 19, grpcTransportQualificationTest 실행). --- .../adapter/inbound/grpc/GrpcP1BoundaryWireTest.java | 5 ++++- .../adapter/inbound/grpc/GrpcRequiresPermissionWireTest.java | 5 ++++- .../adapter/inbound/grpc/GrpcSafeActivationTest.java | 2 ++ .../adapter/inbound/grpc/GrpcServerRunnerBootTest.java | 5 ++++- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcP1BoundaryWireTest.java b/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcP1BoundaryWireTest.java index 3c36bdfb..065a8b20 100644 --- a/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcP1BoundaryWireTest.java +++ b/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcP1BoundaryWireTest.java @@ -104,7 +104,10 @@ class GrpcP1BoundaryWireTest { "app.grpc.server.port=0", "app.grpc.server.bind-address=127.0.0.1", "app.grpc.server.allow-insecure-local=true", - "app.grpc.server.reflection-enabled=false"); + "app.grpc.server.reflection-enabled=false", + // No test here asserts the drain, so closing the context does not wait the 2 s + // production default. In-flight calls still get the shutdown grace. + "app.grpc.server.shutdown-drain-seconds=0"); @Test void missingAuthenticationMetadataIsRejectedWithAStableContract() { diff --git a/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcRequiresPermissionWireTest.java b/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcRequiresPermissionWireTest.java index 83b97e32..8330d4e6 100644 --- a/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcRequiresPermissionWireTest.java +++ b/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcRequiresPermissionWireTest.java @@ -73,7 +73,10 @@ class GrpcRequiresPermissionWireTest { "app.grpc.server.port=0", "app.grpc.server.bind-address=127.0.0.1", "app.grpc.server.allow-insecure-local=true", - "app.grpc.server.reflection-enabled=false"); + "app.grpc.server.reflection-enabled=false", + // No test here asserts the drain, so closing the context does not wait the 2 s + // production default. In-flight calls still get the shutdown grace. + "app.grpc.server.shutdown-drain-seconds=0"); @Test @DisplayName( diff --git a/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcSafeActivationTest.java b/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcSafeActivationTest.java index c260ad9c..4d64f118 100644 --- a/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcSafeActivationTest.java +++ b/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcSafeActivationTest.java @@ -183,6 +183,8 @@ class GrpcSafeActivationTest { .withPropertyValues( "app.grpc.server.port=0", "app.grpc.server.allow-insecure-local=true", + // The only test here that starts a listener: closing it does not wait the 2 s drain. + "app.grpc.server.shutdown-drain-seconds=0", "app.grpc.platform.enabled=true") .run(context -> assertThat(context).hasNotFailed()); } diff --git a/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcServerRunnerBootTest.java b/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcServerRunnerBootTest.java index 28c3d28d..232d93c8 100644 --- a/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcServerRunnerBootTest.java +++ b/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcServerRunnerBootTest.java @@ -44,7 +44,10 @@ class GrpcServerRunnerBootTest { "app.grpc.server.port=0", "app.grpc.server.bind-address=127.0.0.1", "app.grpc.server.allow-insecure-local=true", - "app.grpc.server.reflection-enabled=true"); + "app.grpc.server.reflection-enabled=true", + // Closing the context does not wait the 2 s production drain. The drain test below + // sets its own value, which overrides this one. + "app.grpc.server.shutdown-drain-seconds=0"); @Test void skeletonServerStartsAndServesHealthAndReflectionWithNoFeatures() { -- 2.54.0 From 120093a0058f4ef1687b14b3409af13ad2c727e8 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Fri, 20 Nov 2026 19:16:14 +0900 Subject: [PATCH 2/2] =?UTF-8?q?test(grpc):=20drain=20=EC=8B=9C=ED=97=98?= =?UTF-8?q?=EC=9D=B4=20drain=20=EB=8C=80=EA=B8=B0=20=EB=8F=99=EC=95=88=20l?= =?UTF-8?q?istener=EA=B0=80=20=EC=97=B4=EB=A0=A4=20=EC=9E=88=EC=9D=8C?= =?UTF-8?q?=EC=9D=84=20=EB=8B=A8=EC=96=B8=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GrpcServerRunnerBootTest의 drain 시험은 이름이 「drain 대기 동안 listener가 살아 있고 health는 NOT_SERVING을 보고한다」인데, SERVING이 아닌 첫 응답이 NOT_SERVING인지만 본다. stop()은 health를 NOT_SERVING으로 바꾼 뒤 drain만큼 자고 shutdown()한다. drain이 0이어도 두 줄 사이에 온 응답 하나가 NOT_SERVING이면 시험이 통과한다. 시험에서 drain 6초 줄을 지운 변이가 0.05초 만에 통과했다. SERVING이 아닌 첫 응답부터 2초 동안 health를 계속 묻고, 모든 답이 NOT_SERVING인지 본다. listener가 닫히면 답은 UNAVAILABLE 같은 gRPC 상태 코드로 기록되어 단언이 그 값을 보여 준다. 찾기와 지켜보기를 한 helper로 묶어 when 하나, then 하나로 둔다. - 지켜보는 시간 2초는 drain 6초의 절반 이하다. 첫 NOT_SERVING은 drain이 시작된 뒤에만 오고, 2초에 마지막 탐침의 deadline 2초를 더해도 drain 안에 2초가 남는다. 부하로 탐침이 늦어도 그 2초가 받는다. 버린 안: listener 포트에 TCP 연결이 되는지 보는 안. health 응답이 load balancer가 실제로 읽는 신호이고, 연결만으로는 NOT_SERVING을 못 본다. 위험: 시험 시간은 drain 6초가 정하므로 늘지 않는다. 지켜보는 2초 안에 부하로 탐침 하나가 deadline 2초를 넘기면 DEADLINE_EXCEEDED로 실패한다. 첫 탐침에도 원래 있던 위험이다. 검증(desktop, 이 트리 66d57abd): - GrpcServerRunnerBootTest만 --rerun --no-build-cache, nice 19로 돌렸다. - 고친 시험 그대로 5회: 모두 통과. drain 시험 6.05~6.08초. - 변이 (a) 시험의 shutdown-drain-seconds=6 줄을 지움: 실패(2.08초). 첫 답 NOT_SERVING 뒤 2초 동안 답 40개가 모두 UNAVAILABLE이고, AssertJ가 그 40개를 unexpected로 보여 준다. - 변이 (b) main GrpcServerRunner.stop()의 drain sleep을 지움: 같은 실패(2.09초, UNAVAILABLE 40개). - 앞 커밋 트리 6f50d01b의 시험 + 변이 (b): 통과(0.047초). 고치기 전에는 drain이 없어도 몰랐다. - 변이 뒤 worktree가 이 트리로 돌아온 것을 git write-tree로 봤다. - scripts/verify.sh leaf :adapter:inbound:grpc rc=0(84개, skip 0), pr --no-cache rc=0(7269개, skip 35), main rc=0(1980개, skip 19, grpcTransportQualificationTest 실행). --- .../grpc/GrpcServerRunnerBootTest.java | 48 ++++++++++++++----- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcServerRunnerBootTest.java b/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcServerRunnerBootTest.java index 232d93c8..31aef859 100644 --- a/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcServerRunnerBootTest.java +++ b/src/adapter/inbound/grpc/src/test/java/dev/caskeleton/adapter/inbound/grpc/GrpcServerRunnerBootTest.java @@ -6,6 +6,7 @@ import io.grpc.BindableService; import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; import io.grpc.ServerServiceDefinition; +import io.grpc.StatusRuntimeException; import io.grpc.health.v1.HealthCheckRequest; import io.grpc.health.v1.HealthCheckResponse; import io.grpc.health.v1.HealthCheckResponse.ServingStatus; @@ -36,6 +37,9 @@ import org.springframework.context.annotation.Configuration; */ class GrpcServerRunnerBootTest { + // At most half the 6 s drain: this hold plus one 2 s probe leaves 2 s of slack under load. + private static final Duration HOLD = Duration.ofSeconds(2); + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withUserConfiguration(GrpcServerConfig.class) @@ -124,10 +128,12 @@ class GrpcServerRunnerBootTest { Thread stopping = Thread.ofVirtual().start(runner::stop); try { // when - ServingStatus duringDrain = firstStatusOtherThanServing(channel); + List duringDrain = answersFromTheFirstOtherThanServing(channel); // then - assertThat(duringDrain).isEqualTo(ServingStatus.NOT_SERVING); + assertThat(duringDrain) + .as("health answers from the first one other than SERVING, for %s more", HOLD) + .containsOnly(ServingStatus.NOT_SERVING.name()); } finally { stopping.join(Duration.ofSeconds(20)); channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS); @@ -136,23 +142,41 @@ class GrpcServerRunnerBootTest { } /** - * Polls health until it stops saying SERVING. A server that shut its listener the moment shutdown - * began answers this with UNAVAILABLE instead, which is what a load balancer cannot act on. + * Polls health until it stops saying SERVING, then keeps asking for {@link #HOLD} and returns + * every answer from that first one on: the serving status while the listener is up, the gRPC + * status code once it is gone. A server that shut its listener the moment shutdown began answers + * UNAVAILABLE here, which is what a load balancer cannot act on. A health that still says SERVING + * after four seconds is the whole answer: shutdown never reported itself. */ - private static ServingStatus firstStatusOtherThanServing(ManagedChannel channel) + private static List answersFromTheFirstOtherThanServing(ManagedChannel channel) throws InterruptedException { long until = System.nanoTime() + TimeUnit.SECONDS.toNanos(4); - while (System.nanoTime() < until) { - ServingStatus status = health(channel); - if (status != ServingStatus.SERVING) { - return status; - } + String first = answer(channel); + while (first.equals(ServingStatus.SERVING.name()) && System.nanoTime() < until) { Thread.sleep(10); + first = answer(channel); } - return ServingStatus.SERVING; + List answers = new ArrayList<>(List.of(first)); + if (first.equals(ServingStatus.SERVING.name())) { + return answers; + } + long holdUntil = System.nanoTime() + HOLD.toNanos(); + while (System.nanoTime() < holdUntil) { + Thread.sleep(50); + answers.add(answer(channel)); + } + return answers; } - // Two seconds per probe: the four-second window plus one probe still ends inside the 6 s drain. + private static String answer(ManagedChannel channel) { + try { + return health(channel).name(); + } catch (StatusRuntimeException e) { + return e.getStatus().getCode().name(); + } + } + + // Two seconds per probe: the 4 s search or the hold, each plus one probe, ends inside the drain. private static ServingStatus health(ManagedChannel channel) { return HealthGrpc.newBlockingStub(channel) .withDeadlineAfter(2, TimeUnit.SECONDS) -- 2.54.0