Repository navigation
Add retry metrics to SessionRetryContext #801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1115771
Add retry metrics and operation name to query SessionRetryContext
KirillKurdyukov eb73bc3
Add retry metrics and operation name to table SessionRetryContext
KirillKurdyukov 91300df
Pass Meter to SessionRetryContext through its builder
KirillKurdyukov 54fbbb0
Record retry metrics before completing the operation future
KirillKurdyukov 403f511
Report metrics usage from SessionRetryContext meter
KirillKurdyukov 1263e42
Ignore Meter failures when recording retry metrics
KirillKurdyukov 819d347
Record retry metrics when the operation is cancelled
KirillKurdyukov b93dddb
Revert "Record retry metrics when the operation is cancelled"
KirillKurdyukov 7df9209
Use plain fields in RetryMetrics.Operation
KirillKurdyukov 0330ece
Do not catch Meter exceptions in RetryMetrics
KirillKurdyukov b93123b
Record retry metrics only when finishing spans
KirillKurdyukov a797c8b
Remove finished flag from RetryMetrics.Operation
KirillKurdyukov 0a99e19
Rename finishSpans to finishObservability
KirillKurdyukov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
query/src/test/java/tech/ydb/query/tools/SessionRetryContextMetricsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| package tech.ydb.query.tools; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.ScheduledExecutorService; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import org.junit.Assert; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.mockito.ArgumentMatcher; | ||
|
|
||
| import tech.ydb.core.Result; | ||
| import tech.ydb.core.Status; | ||
| import tech.ydb.core.StatusCode; | ||
| import tech.ydb.core.metrics.Attr; | ||
| import tech.ydb.core.metrics.DoubleHistogram; | ||
| import tech.ydb.core.metrics.Meter; | ||
| import tech.ydb.core.tracing.Span; | ||
| import tech.ydb.core.tracing.SpanKind; | ||
| import tech.ydb.core.tracing.Tracer; | ||
| import tech.ydb.query.QueryClient; | ||
| import tech.ydb.query.QuerySession; | ||
|
|
||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.anyDouble; | ||
| import static org.mockito.ArgumentMatchers.anyLong; | ||
| import static org.mockito.ArgumentMatchers.anyString; | ||
| import static org.mockito.ArgumentMatchers.argThat; | ||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| public class SessionRetryContextMetricsTest { | ||
| private final QueryClient queryClient = mock(QueryClient.class); | ||
| private final ScheduledExecutorService scheduler = mock(ScheduledExecutorService.class); | ||
| private final Tracer tracer = mock(Tracer.class); | ||
| private final Meter meter = mock(Meter.class); | ||
| private final DoubleHistogram duration = mock(DoubleHistogram.class); | ||
| private final DoubleHistogram attempts = mock(DoubleHistogram.class); | ||
|
|
||
| private final ArgumentMatcher<Attr> myOp = a -> "operation.name".equals(a.getKey()) && "MyOp".equals(a.getValue()); | ||
|
|
||
| @Before | ||
| public void setup() { | ||
| when(queryClient.getTracer()).thenReturn(tracer); | ||
| when(queryClient.getScheduler()).thenReturn(scheduler); | ||
| when(queryClient.createSession(any())) | ||
| .thenReturn(CompletableFuture.completedFuture(Result.success(mock(QuerySession.class)))); | ||
| when(tracer.startSpan(anyString(), any())).thenReturn(Span.NOOP); | ||
| when(scheduler.schedule(any(Runnable.class), anyLong(), any())).thenAnswer(inv -> { | ||
| inv.<Runnable>getArgument(0).run(); | ||
| return null; | ||
| }); | ||
|
|
||
| when(meter.createHistogram(eq("ydb.client.retry.duration"), eq("s"), anyString(), | ||
| eq(Arrays.asList(0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1d, 2d, 5d, 10d, 30d)))).thenReturn(duration); | ||
| when(meter.createHistogram(eq("ydb.client.retry.attempts"), eq("{attempt}"), anyString(), | ||
| eq(Arrays.asList(1d, 2d, 3d, 4d, 5d, 7d, 10d, 20d)))).thenReturn(attempts); | ||
| } | ||
|
|
||
| private SessionRetryContext retryContext() { | ||
| return SessionRetryContext.create(queryClient).maxRetries(2).withMeter(meter).build(); | ||
| } | ||
|
|
||
| @Test | ||
| public void namedOperationSucceededOnFirstAttempt() { | ||
| Result<Integer> result = retryContext().supplyResult("MyOp", | ||
| session -> CompletableFuture.completedFuture(Result.success(42))).join(); | ||
|
|
||
| Assert.assertEquals(Integer.valueOf(42), result.getValue()); | ||
| verify(tracer).startSpan("MyOp", SpanKind.INTERNAL); | ||
| verify(tracer, never()).startSpan("ydb.RunWithRetry", SpanKind.INTERNAL); | ||
| verify(attempts).record(eq(1d), argThat(myOp)); | ||
| verify(duration).record(anyDouble(), argThat(myOp)); | ||
| verifyNoMoreInteractions(attempts, duration); | ||
| } | ||
|
|
||
| @Test | ||
| public void retriedOperationWithoutNameHasNoAttributes() { | ||
| AtomicInteger calls = new AtomicInteger(); | ||
| Status status = retryContext().supplyStatus(session -> CompletableFuture.completedFuture( | ||
| calls.getAndIncrement() == 0 ? Status.of(StatusCode.OVERLOADED) : Status.SUCCESS)).join(); | ||
|
|
||
| status.expectSuccess(); | ||
| verify(tracer).startSpan("ydb.RunWithRetry", SpanKind.INTERNAL); | ||
| verify(attempts).record(2d); | ||
| verify(duration).record(anyDouble()); | ||
| verifyNoMoreInteractions(attempts, duration); | ||
| } | ||
|
|
||
| @Test | ||
| public void nonRetryableFailureIsSingleAttempt() { | ||
| Status status = retryContext().supplyStatus("MyOp", | ||
| session -> CompletableFuture.completedFuture(Status.of(StatusCode.SCHEME_ERROR))).join(); | ||
|
|
||
| Assert.assertEquals(StatusCode.SCHEME_ERROR, status.getCode()); | ||
| verify(attempts).record(eq(1d), argThat(myOp)); | ||
| verify(duration).record(anyDouble(), argThat(myOp)); | ||
| verifyNoMoreInteractions(attempts, duration); | ||
| } | ||
|
|
||
| @Test | ||
| public void exhaustedSessionRetriesCountEveryAttempt() { | ||
| when(queryClient.createSession(any())) | ||
| .thenReturn(CompletableFuture.completedFuture(Result.fail(Status.of(StatusCode.OVERLOADED)))); | ||
|
|
||
| Status status = retryContext().supplyStatus("MyOp", session -> { | ||
| throw new AssertionError("session must not be acquired"); | ||
| }).join(); | ||
|
|
||
| Assert.assertEquals(StatusCode.OVERLOADED, status.getCode()); | ||
| verify(attempts).record(eq(3d), argThat(myOp)); | ||
| verify(duration).record(anyDouble(), argThat(myOp)); | ||
| verifyNoMoreInteractions(attempts, duration); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.