public MetricsBuffer renderToBuffer(Executor executor, List<PrometheusRawMetricsProvider> metricsProviders) {
        boolean cacheMetricsResponse = pulsar.getConfiguration().isMetricsBufferResponse();
        while (!closed && !Thread.currentThread().isInterrupted()) {
            long currentTimeSlot = cacheMetricsResponse ? calculateCurrentTimeSlot() : 0;
            MetricsBuffer currentMetricsBuffer = metricsBuffer;
            if (currentMetricsBuffer == null || currentMetricsBuffer.getBufferFuture().isCompletedExceptionally()
                    || (currentMetricsBuffer.getBufferFuture().isDone()
                    && (currentMetricsBuffer.getCreateTimeslot() != 0
                    && currentTimeSlot > currentMetricsBuffer.getCreateTimeslot()))) {
                MetricsBuffer newMetricsBuffer = new MetricsBuffer(currentTimeSlot);
                if (metricsBufferFieldUpdater.compareAndSet(this, currentMetricsBuffer, newMetricsBuffer)) {
                    if (currentMetricsBuffer != null) {
                        currentMetricsBuffer.release();
                    }
                    CompletableFuture<ResponseBuffer> bufferFuture = newMetricsBuffer.getBufferFuture();
                    executor.execute(() -> {
                        try {
                            bufferFuture.complete(new ResponseBuffer(generateMetrics(metricsProviders)));
                        } catch (Exception e) {
                            bufferFuture.completeExceptionally(e);
                        } finally {
                            if (currentTimeSlot == 0) {
                                // if the buffer is not cached, release it after the future is completed
                                metricsBufferFieldUpdater.compareAndSet(this, newMetricsBuffer, null);
                                newMetricsBuffer.release();
                            }
                        }
                    });
                    // no need to retain before returning since the new buffer starts with refCnt 2
                    return newMetricsBuffer;
                } else {
                    currentMetricsBuffer = metricsBuffer;
                }
            }
            // retain the buffer before returning
            // if the buffer is already released, retaining won't succeed, retry in that case
            if (currentMetricsBuffer != null && currentMetricsBuffer.retain()) {
                return currentMetricsBuffer;
            }
        }
        return null;
    }
@Test
    public void testReproducingBufferOverflowExceptionAndEOFExceptionBugsInGzipCompression()
            throws ExecutionException, InterruptedException, IOException {
        PulsarService pulsar = mock(PulsarService.class);
        ServiceConfiguration serviceConfiguration = new ServiceConfiguration();
        when(pulsar.getConfiguration()).thenReturn(serviceConfiguration);

        // generate a random byte buffer which is 8 bytes less than the minimum compress buffer size limit
        // this will trigger the BufferOverflowException bug in writing the gzip trailer
        // it will also trigger another bug in finishing the gzip compression stream when the compress buffer is full
        // which results in EOFException
        Random random = new Random();
        byte[] inputBytes = new byte[8192 - 8];
        random.nextBytes(inputBytes);
        ByteBuf byteBuf = Unpooled.wrappedBuffer(inputBytes);

        PrometheusMetricsGenerator generator =
                new PrometheusMetricsGenerator(pulsar, false, false, false, false, Clock.systemUTC()) {
                    // override the generateMetrics method to return the random byte buffer for gzip compression
                    // instead of the actual metrics
                    @Override
                    protected ByteBuf generateMetrics(List<PrometheusRawMetricsProvider> metricsProviders) {
                        return byteBuf;
                    }
                };

        PrometheusMetricsGenerator.MetricsBuffer metricsBuffer =
                generator.renderToBuffer(MoreExecutors.directExecutor(), Collections.emptyList());
        try {
            PrometheusMetricsGenerator.ResponseBuffer responseBuffer = metricsBuffer.getBufferFuture().get();

            ByteBuf compressed = responseBuffer.getCompressedBuffer(MoreExecutors.directExecutor()).get();
            byte[] compressedBytes = new byte[compressed.readableBytes()];
            compressed.readBytes(compressedBytes);
            try (GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(compressedBytes))) {
                byte[] uncompressedBytes = IOUtils.toByteArray(gzipInputStream);
                assertEquals(uncompressedBytes, inputBytes);
            }
        } finally {
            metricsBuffer.release();
        }
    }