From d9f375a6f5c84d0a802336dc44241ce149949f2b Mon Sep 17 00:00:00 2001 From: Bryce Anderson Date: Wed, 18 Dec 2024 08:41:08 -0700 Subject: [PATCH] loadbalancer-experimental: include reason DefaultHost is unhealthy (#3143) Motivation: We store the reason that a host enters the unhealthy/healthchecking state but we never use it. Modifications: - Include that info when we're creating the DefaultHost.toString(). - Add a useful .toString() for XdsHealthIndicator --- .../java/io/servicetalk/loadbalancer/DefaultHost.java | 9 ++++++--- .../servicetalk/loadbalancer/XdsHealthIndicator.java | 10 ++++++++++ .../io/servicetalk/loadbalancer/DefaultHostTest.java | 4 ++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/DefaultHost.java b/servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/DefaultHost.java index 91712b21a7..5940dc92f0 100644 --- a/servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/DefaultHost.java +++ b/servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/DefaultHost.java @@ -456,10 +456,13 @@ public int score() { @Override public String toString() { final ConnState connState = this.connState; - return "Host{" + + String stateString = connState.isUnhealthy() ? State.UNHEALTHY + "(" + connState.healthCheck + ")" : + connState.state.toString(); + return "DefaultHost{" + "lbDescription=" + lbDescription + ", address=" + address + - ", state=" + connState.state + + ", state=" + stateString + + ", healthIndicator=" + healthIndicator + ", #connections=" + connState.connections.size() + '}'; } @@ -514,7 +517,7 @@ public void schedule(final Throwable originalCause) { @Override public String toString() { - return "UNHEALTHY(" + lastError + ')'; + return "HealthCheck(" + lastError + ')'; } } diff --git a/servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/XdsHealthIndicator.java b/servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/XdsHealthIndicator.java index 2c8015c62c..a657773fdd 100644 --- a/servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/XdsHealthIndicator.java +++ b/servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/XdsHealthIndicator.java @@ -257,6 +257,16 @@ public final void cancel() { sequentialExecutor.execute(this::sequentialCancel); } + @Override + public String toString() { + return "XdsHealthIndicator{" + + ", consecutive5xx=" + consecutive5xx.get() + + ", successes=" + successes.get() + + ", failures=" + failures.get() + + ", evictedUntilNanos=" + evictedUntilNanos + + '}'; + } + void sequentialCancel() { assert sequentialExecutor.isCurrentThreadDraining(); if (cancelled) { diff --git a/servicetalk-loadbalancer-experimental/src/test/java/io/servicetalk/loadbalancer/DefaultHostTest.java b/servicetalk-loadbalancer-experimental/src/test/java/io/servicetalk/loadbalancer/DefaultHostTest.java index 0f4150d328..14d8daf288 100644 --- a/servicetalk-loadbalancer-experimental/src/test/java/io/servicetalk/loadbalancer/DefaultHostTest.java +++ b/servicetalk-loadbalancer-experimental/src/test/java/io/servicetalk/loadbalancer/DefaultHostTest.java @@ -35,6 +35,7 @@ import static io.servicetalk.loadbalancer.HealthCheckConfig.DEFAULT_HEALTH_CHECK_FAILED_CONNECTIONS_THRESHOLD; import static io.servicetalk.loadbalancer.UnhealthyHostConnectionFactory.UNHEALTHY_HOST_EXCEPTION; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -182,6 +183,9 @@ void hostStatus() throws Exception { assertThrows(ExecutionException.class, () -> host.newConnection(any(), false, null).toFuture().get()); } + assertThat(host.toString(), containsString("UNHEALTHY")); + assertThat(host.toString(), containsString("DeliberateException")); + verify(mockHostObserver, times(1)).onHostMarkedUnhealthy(UNHEALTHY_HOST_EXCEPTION); assertThat(host.isHealthy(), is(false)); assertThat(host.canMakeNewConnections(), is(true));