Skip to content

Commit

Permalink
[improve] Use single buffer for metrics when noUnsafe use (apache#23612)
Browse files Browse the repository at this point in the history
Co-authored-by: Lari Hotari <[email protected]>
(cherry picked from commit 9b79d70)
(cherry picked from commit aee413b)
  • Loading branch information
zymap authored and srinath-ctds committed Nov 28, 2024
1 parent 65effdf commit 3294ec4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
2 changes: 2 additions & 0 deletions build/run_unit_group.sh
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ alias echo='{ [[ $- =~ .*x.* ]] && trace_enabled=1 || trace_enabled=0; set +x; }
# Test Groups -- start --
function test_group_broker_group_1() {
mvn_test -pl pulsar-broker -Dgroups='broker' -DtestReuseFork=true
# run tests in broker-isolated group individually (instead of with -Dgroups=broker-isolated) to avoid scanning all test classes
mvn_test -pl pulsar-broker -Dtest=org.apache.pulsar.broker.stats.prometheus.PrometheusMetricsGeneratorWithNoUnsafeTest -DtestForkCount=1 -DtestReuseFork=false
}

function test_group_broker_group_2() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.buffer.Unpooled;
import io.netty.util.internal.PlatformDependent;
import io.prometheus.client.Collector;
import java.io.BufferedOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -363,19 +364,24 @@ protected ByteBuf generateMetrics(List<PrometheusRawMetricsProvider> metricsProv
}
}

private ByteBuf allocateMultipartCompositeDirectBuffer() {
ByteBuf allocateMultipartCompositeDirectBuffer() {
// use composite buffer with pre-allocated buffers to ensure that the pooled allocator can be used
// for allocating the buffers
ByteBufAllocator byteBufAllocator = PulsarByteBufAllocator.DEFAULT;
int chunkSize = resolveChunkSize(byteBufAllocator);
CompositeByteBuf buf = byteBufAllocator.compositeDirectBuffer(
ByteBuf buf;
if (PlatformDependent.hasUnsafe()) {
int chunkSize = resolveChunkSize(byteBufAllocator);
buf = byteBufAllocator.compositeDirectBuffer(
Math.max(MINIMUM_FOR_MAX_COMPONENTS, (initialBufferSize / chunkSize) + 1));
int totalLen = 0;
while (totalLen < initialBufferSize) {
totalLen += chunkSize;
// increase the capacity in increments of chunkSize to preallocate the buffers
// in the composite buffer
buf.capacity(totalLen);
int totalLen = 0;
while (totalLen < initialBufferSize) {
totalLen += chunkSize;
// increase the capacity in increments of chunkSize to preallocate the buffers
// in the composite buffer
buf.capacity(totalLen);
}
} else {
buf = byteBufAllocator.directBuffer(initialBufferSize);
}
return buf;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.pulsar.broker.stats.prometheus;

import static org.testng.Assert.assertFalse;
import io.netty.buffer.ByteBuf;
import io.netty.util.internal.PlatformDependent;
import java.time.Clock;
import lombok.Cleanup;
import org.apache.pulsar.common.util.SimpleTextOutputStream;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

@Test(groups = "broker-isolated")
public class PrometheusMetricsGeneratorWithNoUnsafeTest {

@BeforeClass
static void setup() {
System.setProperty("io.netty.noUnsafe", "true");
}

@Test
public void testWriteStringWithNoUnsafe() {
assertFalse(PlatformDependent.hasUnsafe());
@Cleanup
PrometheusMetricsGenerator generator = new PrometheusMetricsGenerator(null, false, false, false, false,
Clock.systemUTC());
@Cleanup("release")
ByteBuf buf = generator.allocateMultipartCompositeDirectBuffer();
for (int i = 0; i < 2; i++) {
buf.writeBytes(new byte[1024 * 1024]);
}
SimpleTextOutputStream outputStream = new SimpleTextOutputStream(buf);
outputStream.write("test");
}
}

0 comments on commit 3294ec4

Please sign in to comment.