Search in sources :

Example 6 with ConfirmationHandler

use of com.rabbitmq.stream.ConfirmationHandler in project rabbitmq-stream-java-client by rabbitmq.

the class StreamPerfTest method call.

@Override
public Integer call() throws Exception {
    maybeDisplayVersion();
    maybeDisplayEnvironmentVariablesHelp();
    overridePropertiesWithEnvironmentVariables();
    Codec codec = createCodec(this.codecClass);
    ByteBufAllocator byteBufAllocator = ByteBufAllocator.DEFAULT;
    CompositeMeterRegistry meterRegistry = new CompositeMeterRegistry();
    String metricsPrefix = "rabbitmq.stream";
    this.metricsCollector = new MicrometerMetricsCollector(meterRegistry, metricsPrefix);
    Counter producerConfirm = meterRegistry.counter(metricsPrefix + ".producer_confirmed");
    Supplier<String> memoryReportSupplier;
    if (this.memoryReport) {
        long physicalMemory = Utils.physicalMemory();
        String physicalMemoryReport = physicalMemory == 0 ? "" : format(", physical memory %s (%d bytes)", Utils.formatByte(physicalMemory), physicalMemory);
        this.out.println(format("Max memory %s (%d bytes), max direct memory %s (%d bytes)%s", Utils.formatByte(Runtime.getRuntime().maxMemory()), Runtime.getRuntime().maxMemory(), Utils.formatByte(PlatformDependent.maxDirectMemory()), PlatformDependent.maxDirectMemory(), physicalMemoryReport));
        if (byteBufAllocator instanceof ByteBufAllocatorMetricProvider) {
            ByteBufAllocatorMetric allocatorMetric = ((ByteBufAllocatorMetricProvider) byteBufAllocator).metric();
            memoryReportSupplier = () -> {
                long usedHeapMemory = allocatorMetric.usedHeapMemory();
                long usedDirectMemory = allocatorMetric.usedDirectMemory();
                return format("Used heap memory %s (%d bytes), used direct memory %s (%d bytes)", Utils.formatByte(usedHeapMemory), usedHeapMemory, Utils.formatByte(usedDirectMemory), usedDirectMemory);
            };
        } else {
            memoryReportSupplier = () -> "";
        }
    } else {
        memoryReportSupplier = () -> "";
    }
    this.performanceMetrics = new DefaultPerformanceMetrics(meterRegistry, metricsPrefix, this.summaryFile, this.includeByteRates, this.confirmLatency, memoryReportSupplier, this.out);
    // we need to store a long in it
    this.messageSize = this.messageSize < 8 ? 8 : this.messageSize;
    ShutdownService shutdownService = new ShutdownService();
    try {
        MonitoringContext monitoringContext = new MonitoringContext(this.monitoringPort, meterRegistry);
        this.monitorings.forEach(m -> m.configure(monitoringContext));
        monitoringContext.start();
        shutdownService.wrap(closeStep("Closing monitoring context", monitoringContext::close));
        Runtime.getRuntime().addShutdownHook(new Thread(() -> shutdownService.close()));
        // FIXME add confirm latency
        ScheduledExecutorService envExecutor = Executors.newScheduledThreadPool(Math.max(Runtime.getRuntime().availableProcessors(), this.producers), new NamedThreadFactory("stream-perf-test-env-"));
        shutdownService.wrap(closeStep("Closing environment executor", () -> envExecutor.shutdownNow()));
        boolean tls = isTls(this.uris);
        AddressResolver addrResolver;
        if (loadBalancer) {
            int defaultPort = tls ? Client.DEFAULT_TLS_PORT : Client.DEFAULT_PORT;
            List<Address> addresses = this.uris.stream().map(uri -> {
                try {
                    return new URI(uri);
                } catch (URISyntaxException e) {
                    throw new IllegalArgumentException("Error while parsing URI " + uri + ": " + e.getMessage());
                }
            }).map(uriItem -> new Address(uriItem.getHost() == null ? "localhost" : uriItem.getHost(), uriItem.getPort() == -1 ? defaultPort : uriItem.getPort())).collect(Collectors.toList());
            AtomicInteger connectionAttemptCount = new AtomicInteger(0);
            addrResolver = address -> addresses.get(connectionAttemptCount.getAndIncrement() % addresses.size());
        } else {
            if (this.addressResolver == null) {
                addrResolver = address -> address;
            } else {
                // should happen only in tests
                addrResolver = this.addressResolver;
            }
        }
        EnvironmentBuilder environmentBuilder = Environment.builder().id("stream-perf-test").uris(this.uris).addressResolver(addrResolver).scheduledExecutorService(envExecutor).metricsCollector(metricsCollector).byteBufAllocator(byteBufAllocator).codec(codec).maxProducersByConnection(this.producersByConnection).maxTrackingConsumersByConnection(this.trackingConsumersByConnection).maxConsumersByConnection(this.consumersByConnection).rpcTimeout(Duration.ofSeconds(this.rpcTimeout));
        ChannelCustomizer channelCustomizer = channel -> {
        };
        if (tls) {
            TlsConfiguration tlsConfiguration = environmentBuilder.tls();
            tlsConfiguration = tlsConfiguration.sslContext(SslContextBuilder.forClient().trustManager(Utils.TRUST_EVERYTHING_TRUST_MANAGER).build());
            environmentBuilder = tlsConfiguration.environmentBuilder();
            if (!this.sniServerNames.isEmpty()) {
                channelCustomizer = channelCustomizer.andThen(ch -> {
                    SslHandler sslHandler = ch.pipeline().get(SslHandler.class);
                    if (sslHandler != null) {
                        SSLParameters sslParameters = sslHandler.engine().getSSLParameters();
                        sslParameters.setServerNames(this.sniServerNames);
                        sslHandler.engine().setSSLParameters(sslParameters);
                    }
                });
            }
        }
        Environment environment = environmentBuilder.channelCustomizer(channelCustomizer).build();
        shutdownService.wrap(closeStep("Closing environment(s)", () -> environment.close()));
        streams = Utils.streams(this.streamCount, this.streams);
        for (String stream : streams) {
            StreamCreator streamCreator = environment.streamCreator().stream(stream).maxLengthBytes(this.maxLengthBytes).maxSegmentSizeBytes(this.maxSegmentSize).leaderLocator(this.leaderLocator);
            if (this.maxAge != null) {
                streamCreator.maxAge(this.maxAge);
            }
            try {
                streamCreator.create();
            } catch (StreamException e) {
                if (e.getCode() == Constants.RESPONSE_CODE_PRECONDITION_FAILED) {
                    String message = String.format("Warning: stream '%s' already exists, but with different properties than " + "max-length-bytes=%s, stream-max-segment-size-bytes=%s, queue-leader-locator=%s", stream, this.maxLengthBytes, this.maxSegmentSize, this.leaderLocator);
                    if (this.maxAge != null) {
                        message += String.format(", max-age=%s", this.maxAge);
                    }
                    this.out.println(message);
                } else {
                    throw e;
                }
            }
        }
        if (this.deleteStreams) {
            shutdownService.wrap(closeStep("Deleting stream(s)", () -> {
                for (String stream : streams) {
                    LOGGER.debug("Deleting {}", stream);
                    try {
                        environment.deleteStream(stream);
                        LOGGER.debug("Deleted {}", stream);
                    } catch (Exception e) {
                        LOGGER.warn("Could not delete stream {}: {}", stream, e.getMessage());
                    }
                }
            }));
        }
        List<Producer> producers = Collections.synchronizedList(new ArrayList<>(this.producers));
        List<Runnable> producerRunnables = IntStream.range(0, this.producers).mapToObj(i -> {
            Runnable rateLimiterCallback;
            if (this.rate > 0) {
                RateLimiter rateLimiter = RateLimiter.create(this.rate);
                rateLimiterCallback = () -> rateLimiter.acquire(1);
            } else {
                rateLimiterCallback = () -> {
                };
            }
            String stream = stream(this.streams, i);
            ProducerBuilder producerBuilder = environment.producerBuilder();
            String producerName = this.producerNameStrategy.apply(stream, i + 1);
            if (producerName != null && !producerName.trim().isEmpty()) {
                producerBuilder = producerBuilder.name(producerName).confirmTimeout(Duration.ZERO);
            }
            Producer producer = producerBuilder.subEntrySize(this.subEntrySize).batchSize(this.batchSize).compression(this.compression == Compression.NONE ? null : this.compression).maxUnconfirmedMessages(this.confirms).stream(stream).build();
            AtomicLong messageCount = new AtomicLong(0);
            ConfirmationHandler confirmationHandler;
            if (this.confirmLatency) {
                final PerformanceMetrics metrics = this.performanceMetrics;
                final int divisor = Utils.downSamplingDivisor(this.rate);
                confirmationHandler = confirmationStatus -> {
                    if (confirmationStatus.isConfirmed()) {
                        producerConfirm.increment();
                        // this should not affect the metric much
                        if (messageCount.incrementAndGet() % divisor == 0) {
                            try {
                                long time = Utils.readLong(confirmationStatus.getMessage().getBodyAsBinary());
                                // see below why we use current time to measure latency
                                metrics.confirmLatency(System.currentTimeMillis() - time, TimeUnit.MILLISECONDS);
                            } catch (Exception e) {
                            // not able to read the body, something wrong?
                            }
                        }
                    }
                };
            } else {
                confirmationHandler = confirmationStatus -> {
                    if (confirmationStatus.isConfirmed()) {
                        producerConfirm.increment();
                    }
                };
            }
            producers.add(producer);
            return (Runnable) () -> {
                final int msgSize = this.messageSize;
                try {
                    while (true && !Thread.currentThread().isInterrupted()) {
                        rateLimiterCallback.run();
                        // Using current time for interoperability with other tools
                        // and also across different processes.
                        // This is good enough to measure duration/latency this way
                        // in a performance tool.
                        long creationTime = System.currentTimeMillis();
                        byte[] payload = new byte[msgSize];
                        Utils.writeLong(payload, creationTime);
                        producer.send(producer.messageBuilder().addData(payload).build(), confirmationHandler);
                    }
                } catch (Exception e) {
                    if (e instanceof InterruptedException || (e.getCause() != null && e.getCause() instanceof InterruptedException)) {
                        LOGGER.info("Publisher #{} thread interrupted", i, e);
                    } else {
                        LOGGER.warn("Publisher #{} crashed", i, e);
                    }
                }
            };
        }).collect(Collectors.toList());
        List<Consumer> consumers = Collections.synchronizedList(IntStream.range(0, this.consumers).mapToObj(i -> {
            final PerformanceMetrics metrics = this.performanceMetrics;
            AtomicLong messageCount = new AtomicLong(0);
            String stream = stream(streams, i);
            ConsumerBuilder consumerBuilder = environment.consumerBuilder();
            consumerBuilder = consumerBuilder.stream(stream).offset(this.offset);
            if (this.storeEvery > 0) {
                String consumerName = this.consumerNameStrategy.apply(stream, i + 1);
                consumerBuilder = consumerBuilder.name(consumerName).autoTrackingStrategy().messageCountBeforeStorage(this.storeEvery).builder();
            }
            // we assume the publishing rate is the same order as the consuming rate
            // we actually don't want to downsample for low rates
            final int divisor = Utils.downSamplingDivisor(this.rate);
            consumerBuilder = consumerBuilder.messageHandler((context, message) -> {
                // this should not affect the metric much
                if (messageCount.incrementAndGet() % 100 == divisor) {
                    try {
                        long time = Utils.readLong(message.getBodyAsBinary());
                        // see above why we use current time to measure latency
                        metrics.latency(System.currentTimeMillis() - time, TimeUnit.MILLISECONDS);
                    } catch (Exception e) {
                    // not able to read the body, maybe not a message from the
                    // tool
                    }
                    metrics.offset(context.offset());
                }
            });
            Consumer consumer = consumerBuilder.build();
            return consumer;
        }).collect(Collectors.toList()));
        shutdownService.wrap(closeStep("Closing consumers", () -> {
            for (Consumer consumer : consumers) {
                consumer.close();
            }
        }));
        ExecutorService executorService;
        if (this.producers > 0) {
            executorService = Executors.newFixedThreadPool(this.producers, new NamedThreadFactory("stream-perf-test-publishers-"));
            for (Runnable producer : producerRunnables) {
                this.out.println("Starting producer");
                executorService.submit(producer);
            }
        } else {
            executorService = null;
        }
        shutdownService.wrap(closeStep("Closing producers", () -> {
            for (Producer p : producers) {
                p.close();
            }
        }));
        shutdownService.wrap(closeStep("Closing producers executor service", () -> {
            if (executorService != null) {
                executorService.shutdownNow();
            }
        }));
        String metricsHeader = "Arguments: " + String.join(" ", arguments);
        this.performanceMetrics.start(metricsHeader);
        shutdownService.wrap(closeStep("Closing metrics", () -> this.performanceMetrics.close()));
        CountDownLatch latch = new CountDownLatch(1);
        Runtime.getRuntime().addShutdownHook(new Thread(() -> latch.countDown()));
        try {
            latch.await();
        } catch (InterruptedException e) {
        // moving on to the closing sequence
        }
    } finally {
        shutdownService.close();
    }
    return 0;
}
Also used : Arrays(java.util.Arrays) ByteBufAllocatorMetricProvider(io.netty.buffer.ByteBufAllocatorMetricProvider) SNIServerName(javax.net.ssl.SNIServerName) AddressResolver(com.rabbitmq.stream.AddressResolver) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) URISyntaxException(java.net.URISyntaxException) BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) Codec(com.rabbitmq.stream.Codec) QpidProtonCodec(com.rabbitmq.stream.codec.QpidProtonCodec) SimpleCodec(com.rabbitmq.stream.codec.SimpleCodec) StreamException(com.rabbitmq.stream.StreamException) PlatformDependent(io.netty.util.internal.PlatformDependent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) ConsumerBuilder(com.rabbitmq.stream.ConsumerBuilder) CloseCallback(com.rabbitmq.stream.perf.ShutdownService.CloseCallback) Duration(java.time.Duration) Map(java.util.Map) URI(java.net.URI) CommandLine(picocli.CommandLine) Counter(io.micrometer.core.instrument.Counter) PrintWriter(java.io.PrintWriter) ENVIRONMENT_VARIABLE_PREFIX(com.rabbitmq.stream.perf.Utils.ENVIRONMENT_VARIABLE_PREFIX) Collection(java.util.Collection) OPTION_TO_ENVIRONMENT_VARIABLE(com.rabbitmq.stream.perf.Utils.OPTION_TO_ENVIRONMENT_VARIABLE) Compression(com.rabbitmq.stream.compression.Compression) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Executors(java.util.concurrent.Executors) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) StreamCreator(com.rabbitmq.stream.StreamCreator) SslHandler(io.netty.handler.ssl.SslHandler) ProducerBuilder(com.rabbitmq.stream.ProducerBuilder) Constants(com.rabbitmq.stream.Constants) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) LeaderLocator(com.rabbitmq.stream.StreamCreator.LeaderLocator) ENVIRONMENT_VARIABLE_LOOKUP(com.rabbitmq.stream.perf.Utils.ENVIRONMENT_VARIABLE_LOOKUP) IntStream(java.util.stream.IntStream) ByteCapacity(com.rabbitmq.stream.ByteCapacity) NamedThreadFactory(com.rabbitmq.stream.perf.Utils.NamedThreadFactory) CompositeMeterRegistry(io.micrometer.core.instrument.composite.CompositeMeterRegistry) SSLParameters(javax.net.ssl.SSLParameters) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) Address(com.rabbitmq.stream.Address) RateLimiter(com.google.common.util.concurrent.RateLimiter) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) Charset(java.nio.charset.Charset) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ChannelCustomizer(com.rabbitmq.stream.ChannelCustomizer) ExecutorService(java.util.concurrent.ExecutorService) PrintStream(java.io.PrintStream) Logger(org.slf4j.Logger) MetricsCollector(com.rabbitmq.stream.metrics.MetricsCollector) Environment(com.rabbitmq.stream.Environment) IOException(java.io.IOException) Consumer(com.rabbitmq.stream.Consumer) Producer(com.rabbitmq.stream.Producer) EnvironmentBuilder(com.rabbitmq.stream.EnvironmentBuilder) MicrometerMetricsCollector(com.rabbitmq.stream.metrics.MicrometerMetricsCollector) ByteBufAllocatorMetric(io.netty.buffer.ByteBufAllocatorMetric) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) Client(com.rabbitmq.stream.impl.Client) SslContextBuilder(io.netty.handler.ssl.SslContextBuilder) CommandSpec(picocli.CommandLine.Model.CommandSpec) TlsConfiguration(com.rabbitmq.stream.EnvironmentBuilder.TlsConfiguration) Collections(java.util.Collections) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) Address(com.rabbitmq.stream.Address) MicrometerMetricsCollector(com.rabbitmq.stream.metrics.MicrometerMetricsCollector) ByteBufAllocatorMetric(io.netty.buffer.ByteBufAllocatorMetric) EnvironmentBuilder(com.rabbitmq.stream.EnvironmentBuilder) Codec(com.rabbitmq.stream.Codec) QpidProtonCodec(com.rabbitmq.stream.codec.QpidProtonCodec) SimpleCodec(com.rabbitmq.stream.codec.SimpleCodec) Counter(io.micrometer.core.instrument.Counter) Consumer(com.rabbitmq.stream.Consumer) ConsumerBuilder(com.rabbitmq.stream.ConsumerBuilder) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) NamedThreadFactory(com.rabbitmq.stream.perf.Utils.NamedThreadFactory) SslHandler(io.netty.handler.ssl.SslHandler) RateLimiter(com.google.common.util.concurrent.RateLimiter) ProducerBuilder(com.rabbitmq.stream.ProducerBuilder) Producer(com.rabbitmq.stream.Producer) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Environment(com.rabbitmq.stream.Environment) TlsConfiguration(com.rabbitmq.stream.EnvironmentBuilder.TlsConfiguration) ChannelCustomizer(com.rabbitmq.stream.ChannelCustomizer) URISyntaxException(java.net.URISyntaxException) StreamCreator(com.rabbitmq.stream.StreamCreator) URI(java.net.URI) StreamException(com.rabbitmq.stream.StreamException) ByteBufAllocatorMetricProvider(io.netty.buffer.ByteBufAllocatorMetricProvider) SSLParameters(javax.net.ssl.SSLParameters) CompositeMeterRegistry(io.micrometer.core.instrument.composite.CompositeMeterRegistry) AddressResolver(com.rabbitmq.stream.AddressResolver) CountDownLatch(java.util.concurrent.CountDownLatch) URISyntaxException(java.net.URISyntaxException) StreamException(com.rabbitmq.stream.StreamException) IOException(java.io.IOException) AtomicLong(java.util.concurrent.atomic.AtomicLong) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService)

Example 7 with ConfirmationHandler

use of com.rabbitmq.stream.ConfirmationHandler in project rabbitmq-stream-java-client by rabbitmq.

the class AlarmsTest method publishingShouldBeBlockedWhenDiskAlarmIsOn.

@Test
void publishingShouldBeBlockedWhenDiskAlarmIsOn() throws Exception {
    int messageCount = 50_000;
    AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(messageCount));
    ConfirmationHandler confirmationHandler = confirmationStatus -> latch.get().countDown();
    Producer producer = env.producerBuilder().stream(stream).build();
    range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().build(), confirmationHandler));
    assertThat(latchAssert(latch.get())).completes();
    try (AutoCloseable alarm = diskAlarm()) {
        latch.set(new CountDownLatch(messageCount));
        new Thread(() -> range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().build(), confirmationHandler))).start();
        assertThat(latchAssert(latch.get())).doesNotComplete(Duration.ofSeconds(5));
    }
    assertThat(latchAssert(latch.get())).completes();
    producer.close();
    latch.set(new CountDownLatch(messageCount * 2));
    Consumer consumer = env.consumerBuilder().stream(stream).offset(OffsetSpecification.first()).messageHandler((context, message) -> latch.get().countDown()).build();
    assertThat(latchAssert(latch.get())).completes();
    consumer.close();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Host.memoryAlarm(com.rabbitmq.stream.Host.memoryAlarm) IntStream.range(java.util.stream.IntStream.range) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) AtomicReference(java.util.concurrent.atomic.AtomicReference) Host.diskAlarm(com.rabbitmq.stream.Host.diskAlarm) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) AfterAll(org.junit.jupiter.api.AfterAll) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) StreamException(com.rabbitmq.stream.StreamException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) EventLoopGroup(io.netty.channel.EventLoopGroup) Environment(com.rabbitmq.stream.Environment) Consumer(com.rabbitmq.stream.Consumer) Producer(com.rabbitmq.stream.Producer) EnvironmentBuilder(com.rabbitmq.stream.EnvironmentBuilder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) AfterEach(org.junit.jupiter.api.AfterEach) TestUtils.responseCode(com.rabbitmq.stream.impl.TestUtils.responseCode) Constants(com.rabbitmq.stream.Constants) SECONDS(java.util.concurrent.TimeUnit.SECONDS) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) TestUtils.localhost(com.rabbitmq.stream.impl.TestUtils.localhost) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) Producer(com.rabbitmq.stream.Producer) Consumer(com.rabbitmq.stream.Consumer) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 8 with ConfirmationHandler

use of com.rabbitmq.stream.ConfirmationHandler in project rabbitmq-stream-java-client by rabbitmq.

the class AlarmsTest method diskAlarmShouldNotPreventConsumption.

@Test
void diskAlarmShouldNotPreventConsumption() throws Exception {
    int messageCount = 50_000;
    AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(messageCount));
    ConfirmationHandler confirmationHandler = confirmationStatus -> latch.get().countDown();
    Producer producer = env.producerBuilder().stream(stream).build();
    range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().build(), confirmationHandler));
    assertThat(latchAssert(latch)).completes();
    producer.close();
    try (AutoCloseable alarm = diskAlarm()) {
        latch.set(new CountDownLatch(messageCount));
        Consumer consumer = env.consumerBuilder().stream(stream).offset(OffsetSpecification.first()).messageHandler((context, message) -> latch.get().countDown()).build();
        assertThat(latchAssert(latch)).completes();
        consumer.close();
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Host.memoryAlarm(com.rabbitmq.stream.Host.memoryAlarm) IntStream.range(java.util.stream.IntStream.range) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) AtomicReference(java.util.concurrent.atomic.AtomicReference) Host.diskAlarm(com.rabbitmq.stream.Host.diskAlarm) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) AfterAll(org.junit.jupiter.api.AfterAll) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) StreamException(com.rabbitmq.stream.StreamException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) EventLoopGroup(io.netty.channel.EventLoopGroup) Environment(com.rabbitmq.stream.Environment) Consumer(com.rabbitmq.stream.Consumer) Producer(com.rabbitmq.stream.Producer) EnvironmentBuilder(com.rabbitmq.stream.EnvironmentBuilder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) AfterEach(org.junit.jupiter.api.AfterEach) TestUtils.responseCode(com.rabbitmq.stream.impl.TestUtils.responseCode) Constants(com.rabbitmq.stream.Constants) SECONDS(java.util.concurrent.TimeUnit.SECONDS) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) TestUtils.localhost(com.rabbitmq.stream.impl.TestUtils.localhost) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) Producer(com.rabbitmq.stream.Producer) Consumer(com.rabbitmq.stream.Consumer) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 9 with ConfirmationHandler

use of com.rabbitmq.stream.ConfirmationHandler in project rabbitmq-stream-java-client by rabbitmq.

the class AlarmsTest method publishingWhenMemoryAlarmIsOnShouldWork.

@Test
void publishingWhenMemoryAlarmIsOnShouldWork() throws Exception {
    int messageCount = 50_000;
    AtomicReference<CountDownLatch> latch = new AtomicReference<>(new CountDownLatch(messageCount));
    ConfirmationHandler confirmationHandler = confirmationStatus -> latch.get().countDown();
    Producer producer = env.producerBuilder().stream(stream).build();
    range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().build(), confirmationHandler));
    assertThat(latchAssert(latch.get())).completes();
    try (AutoCloseable alarm = memoryAlarm()) {
        latch.set(new CountDownLatch(messageCount));
        new Thread(() -> range(0, messageCount).forEach(i -> producer.send(producer.messageBuilder().build(), confirmationHandler))).start();
        assertThat(latchAssert(latch.get())).completes();
    }
    assertThat(latchAssert(latch.get())).completes();
    producer.close();
    latch.set(new CountDownLatch(messageCount * 2));
    Consumer consumer = env.consumerBuilder().stream(stream).offset(OffsetSpecification.first()).messageHandler((context, message) -> latch.get().countDown()).build();
    assertThat(latchAssert(latch.get())).completes();
    consumer.close();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Host.memoryAlarm(com.rabbitmq.stream.Host.memoryAlarm) IntStream.range(java.util.stream.IntStream.range) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) AtomicReference(java.util.concurrent.atomic.AtomicReference) Host.diskAlarm(com.rabbitmq.stream.Host.diskAlarm) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) AfterAll(org.junit.jupiter.api.AfterAll) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) StreamException(com.rabbitmq.stream.StreamException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) EventLoopGroup(io.netty.channel.EventLoopGroup) Environment(com.rabbitmq.stream.Environment) Consumer(com.rabbitmq.stream.Consumer) Producer(com.rabbitmq.stream.Producer) EnvironmentBuilder(com.rabbitmq.stream.EnvironmentBuilder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) AfterEach(org.junit.jupiter.api.AfterEach) TestUtils.responseCode(com.rabbitmq.stream.impl.TestUtils.responseCode) Constants(com.rabbitmq.stream.Constants) SECONDS(java.util.concurrent.TimeUnit.SECONDS) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) TestUtils.localhost(com.rabbitmq.stream.impl.TestUtils.localhost) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) Producer(com.rabbitmq.stream.Producer) Consumer(com.rabbitmq.stream.Consumer) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 10 with ConfirmationHandler

use of com.rabbitmq.stream.ConfirmationHandler in project rabbitmq-stream-java-client by rabbitmq.

the class StreamEnvironmentTest method createPublishConsumeDelete.

@ParameterizedTest
@ValueSource(booleans = { false, true })
void createPublishConsumeDelete(boolean lazyInit, TestInfo info) {
    try (Environment env = environmentBuilder.lazyInitialization(lazyInit).build()) {
        String s = streamName(info);
        env.streamCreator().stream(s).create();
        int messageCount = 50_000;
        CountDownLatch confirmLatch = new CountDownLatch(messageCount);
        CountDownLatch consumeLatch = new CountDownLatch(messageCount);
        Producer producer = env.producerBuilder().stream(s).build();
        ConfirmationHandler confirmationHandler = confirmationStatus -> confirmLatch.countDown();
        IntStream.range(0, messageCount).forEach(i -> {
            Message message = producer.messageBuilder().addData("".getBytes(StandardCharsets.UTF_8)).build();
            producer.send(message, confirmationHandler);
        });
        latchAssert(confirmLatch).completes();
        Consumer consumer = env.consumerBuilder().stream(s).offset(OffsetSpecification.first()).messageHandler((context, message) -> consumeLatch.countDown()).build();
        latchAssert(consumeLatch).completes();
        producer.close();
        consumer.close();
        env.deleteStream(s);
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) SNIHostName(javax.net.ssl.SNIHostName) Message(com.rabbitmq.stream.Message) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) TestUtils.streamName(com.rabbitmq.stream.impl.TestUtils.streamName) AuthenticationFailureException(com.rabbitmq.stream.AuthenticationFailureException) AfterAll(org.junit.jupiter.api.AfterAll) StreamException(com.rabbitmq.stream.StreamException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) ConsumerBuilder(com.rabbitmq.stream.ConsumerBuilder) Duration(java.time.Duration) Map(java.util.Map) Host(com.rabbitmq.stream.Host) TestUtils.waitAtMost(com.rabbitmq.stream.impl.TestUtils.waitAtMost) Collection(java.util.Collection) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) StandardCharsets(java.nio.charset.StandardCharsets) TestInfo(org.junit.jupiter.api.TestInfo) BackOffDelayPolicy(com.rabbitmq.stream.BackOffDelayPolicy) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) StreamCreator(com.rabbitmq.stream.StreamCreator) SslHandler(io.netty.handler.ssl.SslHandler) ProducerBuilder(com.rabbitmq.stream.ProducerBuilder) Constants(com.rabbitmq.stream.Constants) OffsetSpecification(com.rabbitmq.stream.OffsetSpecification) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) IntStream(java.util.stream.IntStream) SSLParameters(javax.net.ssl.SSLParameters) StreamMetadata(com.rabbitmq.stream.impl.Client.StreamMetadata) TestUtils.latchAssert(com.rabbitmq.stream.impl.TestUtils.latchAssert) Address(com.rabbitmq.stream.Address) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) EnvironmentInfo(com.rabbitmq.stream.impl.MonitoringTestUtils.EnvironmentInfo) ConnectException(java.net.ConnectException) ChannelCustomizer(com.rabbitmq.stream.ChannelCustomizer) ValueSource(org.junit.jupiter.params.provider.ValueSource) EventLoopGroup(io.netty.channel.EventLoopGroup) Environment(com.rabbitmq.stream.Environment) Consumer(com.rabbitmq.stream.Consumer) Producer(com.rabbitmq.stream.Producer) EnvironmentBuilder(com.rabbitmq.stream.EnvironmentBuilder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) DisabledIfTlsNotEnabled(com.rabbitmq.stream.impl.TestUtils.DisabledIfTlsNotEnabled) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) TestUtils.localhost(com.rabbitmq.stream.impl.TestUtils.localhost) TestUtils.localhostTls(com.rabbitmq.stream.impl.TestUtils.localhostTls) Producer(com.rabbitmq.stream.Producer) ConfirmationHandler(com.rabbitmq.stream.ConfirmationHandler) Message(com.rabbitmq.stream.Message) Consumer(com.rabbitmq.stream.Consumer) Environment(com.rabbitmq.stream.Environment) CountDownLatch(java.util.concurrent.CountDownLatch) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

ConfirmationHandler (com.rabbitmq.stream.ConfirmationHandler)13 CountDownLatch (java.util.concurrent.CountDownLatch)13 Environment (com.rabbitmq.stream.Environment)12 OffsetSpecification (com.rabbitmq.stream.OffsetSpecification)12 Producer (com.rabbitmq.stream.Producer)12 StreamException (com.rabbitmq.stream.StreamException)12 Duration (java.time.Duration)12 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)12 BeforeEach (org.junit.jupiter.api.BeforeEach)12 Constants (com.rabbitmq.stream.Constants)11 EnvironmentBuilder (com.rabbitmq.stream.EnvironmentBuilder)11 TestUtils.latchAssert (com.rabbitmq.stream.impl.TestUtils.latchAssert)11 AfterEach (org.junit.jupiter.api.AfterEach)11 Test (org.junit.jupiter.api.Test)11 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)11 TestUtils.localhost (com.rabbitmq.stream.impl.TestUtils.localhost)10 EventLoopGroup (io.netty.channel.EventLoopGroup)10 IntStream (java.util.stream.IntStream)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 Host (com.rabbitmq.stream.Host)8