Search in sources :

Example 6 with Endpoint

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

the class JaxRsClientDialogueEndpointTest method testPostWithBody.

@Test
public void testPostWithBody() {
    Channel channel = stubNoContentResponseChannel();
    StubService service = JaxRsClient.create(StubService.class, channel, runtime);
    service.post("Hello, World!");
    ArgumentCaptor<Endpoint> endpointCaptor = ArgumentCaptor.forClass(Endpoint.class);
    ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
    verify(channel).execute(endpointCaptor.capture(), requestCaptor.capture());
    Endpoint endpoint = endpointCaptor.getValue();
    assertThat(endpoint.serviceName()).isEqualTo("StubService");
    assertThat(endpoint.endpointName()).isEqualTo("post");
    assertThat(endpoint.httpMethod()).isEqualTo(HttpMethod.POST);
    Request request = requestCaptor.getValue();
    assertThat(request.body()).isPresent();
    assertThat(request.body().get().contentType()).isEqualTo("text/plain");
    assertThat(request.headerParams().asMap()).containsExactly(new AbstractMap.SimpleImmutableEntry<>("Accept", ImmutableList.of("application/json")), new AbstractMap.SimpleImmutableEntry<>("Content-Length", ImmutableList.of("13")));
}
Also used : AbstractMap(java.util.AbstractMap) Endpoint(com.palantir.dialogue.Endpoint) Channel(com.palantir.dialogue.Channel) Request(com.palantir.dialogue.Request) Test(org.junit.Test)

Example 7 with Endpoint

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

the class SimulationServer method execute.

@Override
public ListenableFuture<Response> execute(Endpoint endpoint, Request request) {
    Meter perEndpointRequests = MetricNames.requestMeter(simulation.taggedMetrics(), serverName, endpoint);
    activeRequests.inc();
    perEndpointRequests.mark();
    simulation.metricsReporter().report();
    for (ServerHandler handler : handlers) {
        long beforeNanos = simulation.clock().read();
        Optional<ListenableFuture<Response>> maybeResp = handler.maybeExecute(this, endpoint, request);
        if (!maybeResp.isPresent()) {
            continue;
        }
        ListenableFuture<Response> resp = maybeResp.get();
        DialogueFutures.addDirectCallback(resp, DialogueFutures.onSuccess(_ignored -> globalResponses.inc()));
        resp.addListener(() -> {
            activeRequests.dec();
            globalServerTimeNanos.inc(simulation.clock().read() - beforeNanos);
        }, DialogueFutures.safeDirectExecutor());
        if (log.isDebugEnabled()) {
            DialogueFutures.addDirectCallback(resp, DialogueFutures.onSuccess(result -> {
                log.debug("time={} server={} status={} id={}", Duration.ofNanos(simulation.clock().read()), serverName, result.code(), request != null ? request.headerParams().get(Benchmark.REQUEST_ID_HEADER) : null);
            }));
        }
        return resp;
    }
    log.error("No handler available for request {}", request);
    activeRequests.dec();
    return Futures.immediateFailedFuture(new SafeRuntimeException("No handler"));
}
Also used : TestResponse(com.palantir.dialogue.TestResponse) Response(com.palantir.dialogue.Response) Logger(org.slf4j.Logger) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListMultimap(com.google.common.collect.ListMultimap) SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) Predicate(java.util.function.Predicate) LoggerFactory(org.slf4j.LoggerFactory) Channel(com.palantir.dialogue.Channel) Function(java.util.function.Function) TimeUnit(java.util.concurrent.TimeUnit) Meter(com.codahale.metrics.Meter) TestResponse(com.palantir.dialogue.TestResponse) Futures(com.google.common.util.concurrent.Futures) DialogueFutures(com.palantir.dialogue.futures.DialogueFutures) ImmutableList(com.google.common.collect.ImmutableList) Duration(java.time.Duration) Counter(com.codahale.metrics.Counter) Endpoint(com.palantir.dialogue.Endpoint) Optional(java.util.Optional) ResponseAttachments(com.palantir.dialogue.ResponseAttachments) Request(com.palantir.dialogue.Request) Response(com.palantir.dialogue.Response) Preconditions(com.palantir.logsafe.Preconditions) InputStream(java.io.InputStream) SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) Meter(com.codahale.metrics.Meter) ListenableFuture(com.google.common.util.concurrent.ListenableFuture)

Example 8 with Endpoint

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

the class SimulationTest method one_endpoint_dies_on_each_server.

@SimulationCase
public void one_endpoint_dies_on_each_server(Strategy strategy) {
    Endpoint endpoint1 = SimulationUtils.endpoint("e1", HttpMethod.POST);
    Endpoint endpoint2 = SimulationUtils.endpoint("e2", HttpMethod.POST);
    servers = servers(SimulationServer.builder().serverName("server_where_e1_breaks").simulation(simulation).handler(endpoint1, h -> h.response(200).responseTime(Duration.ofMillis(600))).handler(endpoint2, h -> h.response(200).responseTime(Duration.ofMillis(600))).until(Duration.ofSeconds(3), "e1 breaks").handler(endpoint1, h -> h.response(500).responseTime(Duration.ofMillis(600))).handler(endpoint2, h -> h.response(200).responseTime(Duration.ofMillis(600))).build(), SimulationServer.builder().serverName("server_where_e2_breaks").simulation(simulation).handler(endpoint1, h -> h.response(200).responseTime(Duration.ofMillis(600))).handler(endpoint2, h -> h.response(200).responseTime(Duration.ofMillis(600))).until(Duration.ofSeconds(3), "e2 breaks").handler(endpoint1, h -> h.response(200).responseTime(Duration.ofMillis(600))).handler(endpoint2, h -> h.response(500).responseTime(Duration.ofMillis(600))).build());
    st = strategy;
    result = Benchmark.builder().simulation(simulation).requestsPerSecond(250).sendUntil(Duration.ofSeconds(10)).abortAfter(Duration.ofMinutes(1)).clients(10, _i -> strategy.getChannel(simulation, servers)).endpoints(endpoint1, endpoint2).abortAfter(Duration.ofMinutes(10)).run();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Inherited(java.lang.annotation.Inherited) LoggerFactory(org.slf4j.LoggerFactory) Random(java.util.Random) AfterAll(org.junit.jupiter.api.AfterAll) Duration(java.time.Duration) Map(java.util.Map) Tracers(com.palantir.tracing.Tracers) Path(java.nio.file.Path) ExecutionMode(org.junit.jupiter.api.parallel.ExecutionMode) StandardOpenOption(java.nio.file.StandardOpenOption) Channel(com.palantir.dialogue.Channel) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) TestInfo(org.junit.jupiter.api.TestInfo) TestResponse(com.palantir.dialogue.TestResponse) List(java.util.List) Stream(java.util.stream.Stream) Optional(java.util.Optional) Response(com.palantir.dialogue.Response) IntStream(java.util.stream.IntStream) Tracer(com.palantir.tracing.Tracer) Stopwatch(com.google.common.base.Stopwatch) EnumSource(org.junit.jupiter.params.provider.EnumSource) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Retention(java.lang.annotation.Retention) ArrayList(java.util.ArrayList) Meter(com.codahale.metrics.Meter) ScheduledRequest(com.palantir.dialogue.core.Benchmark.ScheduledRequest) Suppliers(com.google.common.base.Suppliers) Endpoint(com.palantir.dialogue.Endpoint) DEFAULT_ENDPOINT(com.palantir.dialogue.core.Benchmark.DEFAULT_ENDPOINT) Logger(org.slf4j.Logger) Files(java.nio.file.Files) XYChart(org.knowm.xchart.XYChart) Observability(com.palantir.tracing.Observability) IOException(java.io.IOException) HttpMethod(com.palantir.dialogue.HttpMethod) TimeUnit(java.util.concurrent.TimeUnit) EndpointChannel(com.palantir.dialogue.EndpointChannel) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ChronoUnit(java.time.temporal.ChronoUnit) Paths(java.nio.file.Paths) Comparator(java.util.Comparator) Execution(org.junit.jupiter.api.parallel.Execution) RetentionPolicy(java.lang.annotation.RetentionPolicy) Endpoint(com.palantir.dialogue.Endpoint)

Example 9 with Endpoint

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

the class Benchmark method endpoints.

public Benchmark endpoints(Endpoint... endpoints) {
    Preconditions.checkNotNull(clients, "Must call client or clients first");
    Preconditions.checkNotNull(requestStream, "Must call sendUntil or numRequests first");
    Preconditions.checkNotNull(simulation, "Must call .simulation() first");
    endpointChannels = new ArrayList<>();
    Arrays.stream(clients).forEach(client -> Arrays.stream(endpoints).forEach(endpoint -> addEndpointChannel(client.name(), endpoint, client.channel())));
    Random pseudoRandom = new Random(21876781263L);
    int count = endpointChannels.size();
    endpointChannelChooser = () -> pseudoRandom.nextInt(count);
    return this;
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Stopwatch(com.google.common.base.Stopwatch) LoggerFactory(org.slf4j.LoggerFactory) Random(java.util.Random) SettableFuture(com.google.common.util.concurrent.SettableFuture) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) DialogueFutures(com.palantir.dialogue.futures.DialogueFutures) SafeArg(com.palantir.logsafe.SafeArg) Value(org.immutables.value.Value) Duration(java.time.Duration) Map(java.util.Map) DefaultConjureRuntime(com.palantir.conjure.java.dialogue.serde.DefaultConjureRuntime) Endpoint(com.palantir.dialogue.Endpoint) Request(com.palantir.dialogue.Request) IntSupplier(java.util.function.IntSupplier) IntFunction(java.util.function.IntFunction) LongStream(java.util.stream.LongStream) Logger(org.slf4j.Logger) KeyedStream(com.palantir.common.streams.KeyedStream) Clients(com.palantir.dialogue.Clients) Streams(com.google.common.collect.Streams) Channel(com.palantir.dialogue.Channel) HttpMethod(com.palantir.dialogue.HttpMethod) Collectors(java.util.stream.Collectors) Snapshot(com.codahale.metrics.Snapshot) FutureCallback(com.google.common.util.concurrent.FutureCallback) TimeUnit(java.util.concurrent.TimeUnit) Futures(com.google.common.util.concurrent.Futures) EndpointChannel(com.palantir.dialogue.EndpointChannel) List(java.util.List) Stream(java.util.stream.Stream) TreeMap(java.util.TreeMap) Optional(java.util.Optional) BaseStream(java.util.stream.BaseStream) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Comparator(java.util.Comparator) Response(com.palantir.dialogue.Response) Preconditions(com.palantir.logsafe.Preconditions) Random(java.util.Random) Endpoint(com.palantir.dialogue.Endpoint)

Example 10 with Endpoint

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

the class BalancedNodeSelectionStrategyChannelTest method constant_4xxs_do_eventually_move_the_needle_but_we_go_back_to_fair_distribution.

@Test
void constant_4xxs_do_eventually_move_the_needle_but_we_go_back_to_fair_distribution() {
    when(chan1.maybeExecute(any(), any(), eq(LimitEnforcement.DEFAULT_ENABLED))).thenReturn(http(400));
    when(chan2.maybeExecute(any(), any(), eq(LimitEnforcement.DEFAULT_ENABLED))).thenReturn(http(200));
    for (int i = 0; i < 11; i++) {
        rttChannel.maybeExecute(endpoint, request, LimitEnforcement.DEFAULT_ENABLED);
        assertThat(rttChannel.getScoresForTesting()).describedAs("%s %s: Scores not affected yet %s", i, Duration.ofNanos(clock.read()), rttChannel).containsExactly(0, 0);
        incrementClockBy(Duration.ofMillis(50));
    }
    rttChannel.maybeExecute(endpoint, request, LimitEnforcement.DEFAULT_ENABLED);
    assertThat(rttChannel.getScoresForTesting()).describedAs("%s: Constant 4xxs did move the needle %s", Duration.ofNanos(clock.read()), rttChannel).containsExactly(1, 0);
    incrementClockBy(Duration.ofSeconds(5));
    assertThat(rttChannel.getScoresForTesting()).describedAs("%s: We quickly forget about 4xxs and go back to fair shuffling %s", Duration.ofNanos(clock.read()), rttChannel).containsExactly(0, 0);
}
Also used : 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