diff --git a/core/src/main/java/tech/ydb/core/impl/YdbDiscovery.java b/core/src/main/java/tech/ydb/core/impl/YdbDiscovery.java index 34347e519..4b53a12f0 100644 --- a/core/src/main/java/tech/ydb/core/impl/YdbDiscovery.java +++ b/core/src/main/java/tech/ydb/core/impl/YdbDiscovery.java @@ -182,7 +182,6 @@ private void runDiscovery() { private void handleThrowable(Throwable th) { readyLock.lock(); - try { lastException = th; scheduleNextTick(); @@ -193,16 +192,18 @@ private void handleThrowable(Throwable th) { } private void handleOk(String selfLocation, List endpoints) { - readyLock.lock(); + CompletableFuture handled = handler.handleEndpoints(endpoints, selfLocation); + readyLock.lock(); try { isStarted = true; lastException = null; - handler.handleEndpoints(endpoints, selfLocation).whenComplete((res, th) -> scheduleNextTick()); readyCondition.signalAll(); } finally { readyLock.unlock(); } + + handled.whenComplete((res, th) -> scheduleNextTick()); } private static String createAddress(EndpointInfo e) { diff --git a/core/src/test/java/tech/ydb/core/impl/YdbDiscoveryTest.java b/core/src/test/java/tech/ydb/core/impl/YdbDiscoveryTest.java index 756f11065..24d3ba2b4 100644 --- a/core/src/test/java/tech/ydb/core/impl/YdbDiscoveryTest.java +++ b/core/src/test/java/tech/ydb/core/impl/YdbDiscoveryTest.java @@ -6,6 +6,8 @@ import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import io.grpc.ConnectivityState; import io.grpc.ManagedChannel; @@ -178,6 +180,44 @@ public void waitWaitingTest() { discovery.stop(); } + @Test + public void readyOnlyAfterEndpointsAreHandledTest() throws InterruptedException { + Mockito.when(channel.newCall(Mockito.eq(DiscoveryServiceGrpc.getListEndpointsMethod()), Mockito.any())) + .thenReturn(MockedCall.discovery("self", new EndpointRecord("localhost", 12340))); + + CountDownLatch handlingStarted = new CountDownLatch(1); + CountDownLatch handlingAllowed = new CountDownLatch(1); + TestHandler handler = new TestHandler() { + @Override + public CompletableFuture handleEndpoints(List endpoints, String selfLocation) { + try { + handlingStarted.countDown(); + Assert.assertTrue(handlingAllowed.await(1, TimeUnit.SECONDS)); + return CompletableFuture.completedFuture(Boolean.TRUE); + } catch (InterruptedException ex) { + throw new AssertionError("interrupted", ex); + } + } + }; + + YdbDiscovery discovery = new YdbDiscovery(handler, scheduler, "/ready", Duration.ofMillis(100)); + discovery.start(); + + // the first discovery is blocked in the endpoints handling + CompletableFuture firstDiscovery = CompletableFuture.supplyAsync(scheduler::runNextTask); + Assert.assertTrue(handlingStarted.await(1, TimeUnit.SECONDS)); + + // endpoints are not applied yet, so discovery must not be ready and waiting must not be blocked + IllegalStateException ex = Assert.assertThrows(IllegalStateException.class, () -> discovery.waitReady(50)); + Assert.assertEquals("Discovery is not ready", ex.getMessage()); + + handlingAllowed.countDown(); + firstDiscovery.join(); + + discovery.waitReady(100); + discovery.stop(); + } + @Test public void failedDiscoveryTest() { Mockito.when(channel.newCall(Mockito.eq(DiscoveryServiceGrpc.getListEndpointsMethod()), Mockito.any())) diff --git a/core/src/test/java/tech/ydb/core/metrics/OpenTelemetryMeterTest.java b/core/src/test/java/tech/ydb/core/metrics/OpenTelemetryMeterTest.java index e9e2c8c51..17287ecf8 100644 --- a/core/src/test/java/tech/ydb/core/metrics/OpenTelemetryMeterTest.java +++ b/core/src/test/java/tech/ydb/core/metrics/OpenTelemetryMeterTest.java @@ -119,20 +119,20 @@ public void doubleGaugeReportsFractionalValuesAndCloses() { } @Test + @SuppressWarnings("deprecation") public void legacyGaugeCreationStillReportsValues() { meter.createLongGauge("ydb.test.legacy", null, null, m -> m.record(7)); - Assert.assertEquals(7L, singleLongPoint(single("ydb.test.legacy").getLongGaugeData().getPoints()).getValue()); } @Test + @SuppressWarnings("deprecation") public void registrationSupportsLegacyMeterOverride() { long[] observed = new long[1]; Meter legacy = new Meter() { @Override - public void createLongGauge( - String name, String unit, String description, Consumer callback) { - callback.accept((value, attrs) -> observed[0] = value); + public void createLongGauge(String name, String unit, String description, Consumer cb) { + cb.accept((value, attrs) -> observed[0] = value); } };