Search in sources :

Example 6 with Exchanger

use of java.util.concurrent.Exchanger in project parseq by linkedin.

the class AbstractBenchmark method doRunBenchmark.

protected void doRunBenchmark(final Engine engine, BenchmarkConfig config) throws Exception {
    final int N = N(config);
    final int warmUpN = warmUpN(config);
    final Histogram planHistogram = createHistogram();
    final Histogram taskHistogram = createHistogram();
    LOG.info("Number of cores: " + Runtime.getRuntime().availableProcessors());
    LOG.info("Configuration: " + config);
    Task<?> probe = createPlan();
    engine.run(probe);
    probe.await();
    final int numberOfTasks = probe.getTrace().getTraceMap().size();
    LOG.info("Number of tasks per plan: " + numberOfTasks);
    final Exchanger<Optional<Task<?>>> exchanger = new Exchanger<>();
    Thread histogramCollector = new Thread(() -> {
        try {
            Optional<Task<?>> t = exchanger.exchange(Optional.empty());
            while (t.isPresent()) {
                Task<?> task = t.get();
                task.await();
                recordCompletionTimes(planHistogram, taskHistogram, task);
                t = exchanger.exchange(Optional.empty());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
    histogramCollector.start();
    Task<?> t = null;
    LOG.info("Warming up using " + warmUpN + " plan execution");
    System.out.print("Progress[");
    Stepper warmUpPercentage = new Stepper(0.1, warmUpN);
    for (int i = 0; i < warmUpN; i++) {
        t = createPlan();
        config.runTask(engine, t);
        warmUpPercentage.isNewStep(i).ifPresent(pct -> {
            System.out.print(".");
        });
    }
    System.out.println(".]");
    grabCPUTimesBeforeTest();
    LOG.info("Starting test of " + N + " plan executions");
    System.out.print("Progress[");
    Stepper percentage = new Stepper(0.1, N);
    Stepper sampler = new Stepper(1 / (N * config.sampleRate), N);
    long start = System.nanoTime();
    for (int i = 0; i < N; i++) {
        t = createPlan();
        config.runTask(engine, t);
        final Task<?> task = t;
        sampler.isNewStep(i).ifPresent(s -> {
            try {
                exchanger.exchange(Optional.of(task));
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        percentage.isNewStep(i).ifPresent(pct -> {
            System.out.print(".");
        });
    }
    long end = System.nanoTime();
    System.out.println(".]");
    grabCPUTimesAfterTest();
    exchanger.exchange(Optional.empty());
    histogramCollector.join();
    config.wrapUp();
    LOG.info("----------------------------------------------------------------");
    LOG.info("Histogram of task execution times on parseq threads in \u00B5s:");
    taskHistogram.outputPercentileDistribution(System.out, 1000.0);
    LOG.info(BENCHMARK_TEST_RESULTS_LOG_PREFIX + "Histogram of task execution times on parseq threads in \u00B5s: " + _histogramSerializer.serialize(taskHistogram));
    LOG.info("----------------------------------------------------------------");
    LOG.info("Histogram of plan completion times in \u00B5s:");
    planHistogram.outputPercentileDistribution(System.out, 1000.0);
    LOG.info(BENCHMARK_TEST_RESULTS_LOG_PREFIX + "Histogram of plan completion times in \u00B5s: " + _histogramSerializer.serialize(planHistogram));
    LOG.info("----------------------------------------------------------------");
    LOG.info("Throughput: " + String.format("%.3f", (N / ((double) (end - start) / 1000000000))) + " plans/s, " + String.format("%.3f", ((N * numberOfTasks) / ((double) (end - start) / 1000000000))) + " tasks/s");
}
Also used : Histogram(org.HdrHistogram.Histogram) Optional(java.util.Optional) Exchanger(java.util.concurrent.Exchanger)

Example 7 with Exchanger

use of java.util.concurrent.Exchanger in project kafka by apache.

the class KafkaProducerTest method testTopicExpiryInMetadata.

@Test
public void testTopicExpiryInMetadata() throws InterruptedException {
    Map<String, Object> configs = new HashMap<>();
    configs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9999");
    configs.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, "30000");
    long refreshBackoffMs = 500L;
    long metadataExpireMs = 60000L;
    long metadataIdleMs = 60000L;
    final Time time = new MockTime();
    final ProducerMetadata metadata = new ProducerMetadata(refreshBackoffMs, metadataExpireMs, metadataIdleMs, new LogContext(), new ClusterResourceListeners(), time);
    final String topic = "topic";
    try (KafkaProducer<String, String> producer = kafkaProducer(configs, new StringSerializer(), new StringSerializer(), metadata, new MockClient(time, metadata), null, time)) {
        Exchanger<Void> exchanger = new Exchanger<>();
        Thread t = new Thread(() -> {
            try {
                // 1
                exchanger.exchange(null);
                while (!metadata.updateRequested()) Thread.sleep(100);
                MetadataResponse updateResponse = RequestTestUtils.metadataUpdateWith(1, singletonMap(topic, 1));
                metadata.updateWithCurrentRequestVersion(updateResponse, false, time.milliseconds());
                // 2
                exchanger.exchange(null);
                time.sleep(120 * 1000L);
                // Update the metadata again, but it should be expired at this point.
                updateResponse = RequestTestUtils.metadataUpdateWith(1, singletonMap(topic, 1));
                metadata.updateWithCurrentRequestVersion(updateResponse, false, time.milliseconds());
                // 3
                exchanger.exchange(null);
                while (!metadata.updateRequested()) Thread.sleep(100);
                time.sleep(30 * 1000L);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
        t.start();
        // 1
        exchanger.exchange(null);
        assertNotNull(producer.partitionsFor(topic));
        // 2
        exchanger.exchange(null);
        // 3
        exchanger.exchange(null);
        assertThrows(TimeoutException.class, () -> producer.partitionsFor(topic));
        t.join();
    }
}
Also used : ProducerMetadata(org.apache.kafka.clients.producer.internals.ProducerMetadata) ClusterResourceListeners(org.apache.kafka.common.internals.ClusterResourceListeners) HashMap(java.util.HashMap) Exchanger(java.util.concurrent.Exchanger) LogContext(org.apache.kafka.common.utils.LogContext) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) KafkaException(org.apache.kafka.common.KafkaException) InvalidTopicException(org.apache.kafka.common.errors.InvalidTopicException) InterruptException(org.apache.kafka.common.errors.InterruptException) ExecutionException(java.util.concurrent.ExecutionException) RecordTooLargeException(org.apache.kafka.common.errors.RecordTooLargeException) TimeoutException(org.apache.kafka.common.errors.TimeoutException) ConfigException(org.apache.kafka.common.config.ConfigException) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) MockTime(org.apache.kafka.common.utils.MockTime) MockClient(org.apache.kafka.clients.MockClient) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 8 with Exchanger

use of java.util.concurrent.Exchanger in project netty by netty.

the class FlowControlHandlerTest method testFlowAutoReadOff.

/**
 * The {@link FlowControlHandler} will pass down messages one by one
 * if auto reading is off and the user is calling {@code read()} on
 * their own.
 */
@Test
public void testFlowAutoReadOff() throws Exception {
    final Exchanger<Channel> peerRef = new Exchanger<Channel>();
    final CountDownLatch msgRcvLatch1 = new CountDownLatch(1);
    final CountDownLatch msgRcvLatch2 = new CountDownLatch(2);
    final CountDownLatch msgRcvLatch3 = new CountDownLatch(3);
    ChannelInboundHandlerAdapter handler = new ChannelDuplexHandler() {

        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            ctx.fireChannelActive();
            peerRef.exchange(ctx.channel(), 1L, SECONDS);
        }

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) {
            msgRcvLatch1.countDown();
            msgRcvLatch2.countDown();
            msgRcvLatch3.countDown();
        }
    };
    FlowControlHandler flow = new FlowControlHandler();
    Channel server = newServer(false, flow, handler);
    Channel client = newClient(server.localAddress());
    try {
        // The client connection on the server side
        Channel peer = peerRef.exchange(null, 1L, SECONDS);
        // Write the message
        client.writeAndFlush(newOneMessage()).syncUninterruptibly();
        // channelRead(1)
        peer.read();
        assertTrue(msgRcvLatch1.await(1L, SECONDS));
        // channelRead(2)
        peer.read();
        assertTrue(msgRcvLatch2.await(1L, SECONDS));
        // channelRead(3)
        peer.read();
        assertTrue(msgRcvLatch3.await(1L, SECONDS));
        assertTrue(flow.isQueueEmpty());
    } finally {
        client.close();
        server.close();
    }
}
Also used : Exchanger(java.util.concurrent.Exchanger) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 9 with Exchanger

use of java.util.concurrent.Exchanger in project netty by netty.

the class FlowControlHandlerTest method testAutoReadingOff.

/**
 * This test demonstrates the default behavior if auto reading
 * is turned off from the get-go and you're calling read() in
 * the hope that only one message will be returned.
 *
 * NOTE: This test waits for the client to disconnect which is
 * interpreted as the signal that all {@code byte}s have been
 * transferred to the server.
 */
@Test
public void testAutoReadingOff() throws Exception {
    final Exchanger<Channel> peerRef = new Exchanger<Channel>();
    final CountDownLatch latch = new CountDownLatch(3);
    ChannelInboundHandlerAdapter handler = new ChannelInboundHandlerAdapter() {

        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            peerRef.exchange(ctx.channel(), 1L, SECONDS);
            ctx.fireChannelActive();
        }

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) {
            ReferenceCountUtil.release(msg);
            latch.countDown();
        }
    };
    Channel server = newServer(false, handler);
    Channel client = newClient(server.localAddress());
    try {
        // The client connection on the server side
        Channel peer = peerRef.exchange(null, 1L, SECONDS);
        // Write the message
        client.writeAndFlush(newOneMessage()).syncUninterruptibly();
        // Read the message
        peer.read();
        // We received all three messages but hoped that only one
        // message was read because auto reading was off and we
        // invoked the read() method only once.
        assertTrue(latch.await(1L, SECONDS));
    } finally {
        client.close();
        server.close();
    }
}
Also used : Exchanger(java.util.concurrent.Exchanger) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 10 with Exchanger

use of java.util.concurrent.Exchanger in project jetty.project by eclipse.

the class ConnectorTimeoutTest method testMaxIdleWithRequest11NoClientClose.

@Test(timeout = 60000)
public void testMaxIdleWithRequest11NoClientClose() throws Exception {
    final Exchanger<EndPoint> exchanger = new Exchanger<>();
    configureServer(new EchoHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            try {
                exchanger.exchange(baseRequest.getHttpChannel().getEndPoint());
            } catch (Exception e) {
                e.printStackTrace();
            }
            super.handle(target, baseRequest, request, response);
        }
    });
    Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort());
    client.setSoTimeout(10000);
    Assert.assertFalse(client.isClosed());
    OutputStream os = client.getOutputStream();
    InputStream is = client.getInputStream();
    String content = "Wibble";
    byte[] contentB = content.getBytes("utf-8");
    os.write(("POST /echo HTTP/1.1\r\n" + "host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "content-type: text/plain; charset=utf-8\r\n" + "content-length: " + contentB.length + "\r\n" + "connection: close\r\n" + "\r\n").getBytes("utf-8"));
    os.write(contentB);
    os.flush();
    // Get the server side endpoint
    EndPoint endPoint = exchanger.exchange(null, 10, TimeUnit.SECONDS);
    // read the response
    IO.toString(is);
    // check client reads EOF
    Assert.assertEquals(-1, is.read());
    TimeUnit.MILLISECONDS.sleep(3 * MAX_IDLE_TIME);
    // further writes will get broken pipe or similar
    try {
        for (int i = 0; i < 1000; i++) {
            os.write(("GET / HTTP/1.0\r\n" + "host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "connection: keep-alive\r\n" + "\r\n").getBytes("utf-8"));
            os.flush();
        }
        Assert.fail("half close should have timed out");
    } catch (SocketException e) {
    // expected
    }
    // check the server side is closed
    Assert.assertFalse(endPoint.isOpen());
}
Also used : SocketException(java.net.SocketException) Exchanger(java.util.concurrent.Exchanger) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) EndPoint(org.eclipse.jetty.io.EndPoint) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) SocketException(java.net.SocketException) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException) EndPoint(org.eclipse.jetty.io.EndPoint) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Socket(java.net.Socket) Test(org.junit.Test)

Aggregations

Exchanger (java.util.concurrent.Exchanger)16 Test (org.junit.Test)9 CountDownLatch (java.util.concurrent.CountDownLatch)7 IOException (java.io.IOException)6 InputStream (java.io.InputStream)5 OutputStream (java.io.OutputStream)5 Socket (java.net.Socket)5 Test (org.junit.jupiter.api.Test)5 Channel (io.netty.channel.Channel)4 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)4 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)4 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)4 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)4 ServletException (javax.servlet.ServletException)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 SocketException (java.net.SocketException)3 HashMap (java.util.HashMap)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3