Search in sources :

Example 16 with Endpoint

use of com.palantir.dialogue.Endpoint in project conjure-java-runtime by palantir.

the class JaxRsClientDialogueEndpointTest method testEmptyStringPathParameter.

@Test
public void testEmptyStringPathParameter() {
    Channel channel = stubNoContentResponseChannel();
    StubService service = JaxRsClient.create(StubService.class, channel, runtime);
    service.innerPath("");
    ArgumentCaptor<Endpoint> endpointCaptor = ArgumentCaptor.forClass(Endpoint.class);
    ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
    verify(channel).execute(endpointCaptor.capture(), requestCaptor.capture());
    UrlBuilder urlBuilder = mock(UrlBuilder.class);
    endpointCaptor.getValue().renderPath(ImmutableMap.of(), urlBuilder);
    // context path
    verify(urlBuilder).pathSegment("foo");
    verify(urlBuilder).pathSegment("begin");
    verify(urlBuilder).pathSegment("");
    verify(urlBuilder).pathSegment("end");
}
Also used : Endpoint(com.palantir.dialogue.Endpoint) Channel(com.palantir.dialogue.Channel) Request(com.palantir.dialogue.Request) UrlBuilder(com.palantir.dialogue.UrlBuilder) Test(org.junit.Test)

Example 17 with Endpoint

use of com.palantir.dialogue.Endpoint in project dialogue by palantir.

the class BalancedNodeSelectionStrategyChannel method maybeExecute.

@Override
public Optional<ListenableFuture<Response>> maybeExecute(Endpoint endpoint, Request request, LimitEnforcement limitEnforcement) {
    ScoreSnapshot[] snapshotsByScore = tracker.getSnapshotsInOrderOfIncreasingScore();
    int giveUpThreshold = Integer.MAX_VALUE;
    for (ScoreSnapshot snapshot : snapshotsByScore) {
        /*
             * If we're considering a channel that has a *drastically* higher score than the last one (i.e. we
             * think it's much worse), then we can often get better outcomes by just refusing to send a
             * request (and queueing) rather than sending something to this known-bad channel.
             *
             * This allows us to avoid sending requests to an unhealthy channel after a node has failed while
             * the concurrency limit on the healthy channel is slowly expanded to meet increased load. Otherwise
             * the assumed concurrency limit base don lower request load on the healthy channel may result in requests
             * being sent to a node that's no longer alive.
             *
             * Note that this functionality is not safe if the preferred channel had zero inflight requests (as this
             * could result in infinite queuing).
             */
        if (snapshot.getScore() > giveUpThreshold) {
            if (log.isDebugEnabled()) {
                log.debug("Giving up and queueing because channel score ({}) for channel {} is not worth sending a " + "request to ({})", SafeArg.of("score", snapshot.getScore()), SafeArg.of("hostIndex", snapshot.getDelegate().channelIndex()), SafeArg.of("giveUpScore", giveUpThreshold));
            }
            return Optional.empty();
        }
        if (snapshot.getInflight() > INFLIGHT_COMPARISON_THRESHOLD) {
            int newThreshold = IntMath.saturatedMultiply(snapshot.getScore(), UNHEALTHY_SCORE_MULTIPLIER);
            if (log.isDebugEnabled()) {
                log.debug("When considering channel {}, giveUpThreshold {} -> {}", SafeArg.of("hostIndex", snapshot.getDelegate().channelIndex()), SafeArg.of("old", giveUpThreshold), SafeArg.of("new", newThreshold));
            }
            giveUpThreshold = newThreshold;
        }
        BalancedChannel channel = channels.get(snapshot.getDelegate().channelIndex());
        Optional<ListenableFuture<Response>> maybe = StickyAttachments.maybeAddStickyToken(channel, endpoint, request, limitEnforcement);
        if (maybe.isPresent()) {
            return maybe;
        }
    }
    return Optional.empty();
}
Also used : ScoreSnapshot(com.palantir.dialogue.core.BalancedScoreTracker.ScoreSnapshot) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Endpoint(com.palantir.dialogue.Endpoint)

Example 18 with Endpoint

use of com.palantir.dialogue.Endpoint in project dialogue by palantir.

the class ResponseLeakDetectorTest method testNotLeaked_streamReferenceHeld.

@Test
public void testNotLeaked_streamReferenceHeld() throws Exception {
    ResponseLeakDetector detector = new ResponseLeakDetector(CLIENT, metrics);
    Meter leaks = metrics.responseLeak().clientName(CLIENT).serviceName(SERVICE).endpoint(ENDPOINT).build();
    assertThat(leaks.getCount()).isZero();
    // Result is intentionally ignored to cause a leak
    try (InputStream ignored = detector.wrap(response, mockEndpoint).body()) {
        // GC and test enough times to be confident no leaks were recorded
        for (int i = 0; i < 100; i++) {
            System.gc();
            Thread.sleep(1);
            assertThat(leaks.getCount()).isZero();
        }
    }
}
Also used : Meter(com.codahale.metrics.Meter) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Endpoint(com.palantir.dialogue.Endpoint) Test(org.junit.jupiter.api.Test)

Example 19 with Endpoint

use of com.palantir.dialogue.Endpoint in project dialogue by palantir.

the class ResponseLeakDetectorTest method before.

@BeforeEach
public void before() {
    mockEndpoint = mock(Endpoint.class);
    response = mock(Response.class);
    metrics = DialogueClientMetrics.of(new DefaultTaggedMetricRegistry());
    lenient().when(mockEndpoint.serviceName()).thenReturn(SERVICE);
    lenient().when(mockEndpoint.endpointName()).thenReturn(ENDPOINT);
    lenient().when(response.body()).thenReturn(new ByteArrayInputStream(new byte[0]));
}
Also used : Response(com.palantir.dialogue.Response) Endpoint(com.palantir.dialogue.Endpoint) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultTaggedMetricRegistry(com.palantir.tritium.metrics.registry.DefaultTaggedMetricRegistry) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 20 with Endpoint

use of com.palantir.dialogue.Endpoint in project dialogue by palantir.

the class BalancedNodeSelectionStrategyChannelTest method when_one_channel_is_in_use_prefer_the_other.

@Test
void when_one_channel_is_in_use_prefer_the_other() {
    set200(chan1);
    SettableFuture<Response> settableFuture = SettableFuture.create();
    when(chan2.maybeExecute(any(), any(), eq(LimitEnforcement.DEFAULT_ENABLED))).thenReturn(Optional.of(settableFuture));
    for (int i = 0; i < 200; i++) {
        channel.maybeExecute(endpoint, request, LimitEnforcement.DEFAULT_ENABLED);
    }
    verify(chan1, times(199)).maybeExecute(eq(endpoint), any(), eq(LimitEnforcement.DEFAULT_ENABLED));
    verify(chan2, times(1)).maybeExecute(eq(endpoint), any(), eq(LimitEnforcement.DEFAULT_ENABLED));
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) TestEndpoint(com.palantir.dialogue.TestEndpoint) Endpoint(com.palantir.dialogue.Endpoint) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Endpoint (com.palantir.dialogue.Endpoint)30 Channel (com.palantir.dialogue.Channel)17 Request (com.palantir.dialogue.Request)15 Response (com.palantir.dialogue.Response)14 Test (org.junit.jupiter.api.Test)11 TestEndpoint (com.palantir.dialogue.TestEndpoint)10 TestResponse (com.palantir.dialogue.TestResponse)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)8 Test (org.junit.Test)8 EndpointChannel (com.palantir.dialogue.EndpointChannel)6 UrlBuilder (com.palantir.dialogue.UrlBuilder)6 Optional (java.util.Optional)6 Random (java.util.Random)6 Futures (com.google.common.util.concurrent.Futures)5 HttpMethod (com.palantir.dialogue.HttpMethod)5 IOException (java.io.IOException)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 Meter (com.codahale.metrics.Meter)4 SafeRuntimeException (com.palantir.logsafe.exceptions.SafeRuntimeException)4