Skip to content

Commit 3294ec4

Browse files
zymaplhotari
authored andcommitted
[improve] Use single buffer for metrics when noUnsafe use (apache#23612)
Co-authored-by: Lari Hotari <[email protected]> (cherry picked from commit 9b79d70) (cherry picked from commit aee413b)
1 parent 65effdf commit 3294ec4

File tree

3 files changed

+69
-9
lines changed

3 files changed

+69
-9
lines changed

build/run_unit_group.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ alias echo='{ [[ $- =~ .*x.* ]] && trace_enabled=1 || trace_enabled=0; set +x; }
7777
# Test Groups -- start --
7878
function test_group_broker_group_1() {
7979
mvn_test -pl pulsar-broker -Dgroups='broker' -DtestReuseFork=true
80+
# run tests in broker-isolated group individually (instead of with -Dgroups=broker-isolated) to avoid scanning all test classes
81+
mvn_test -pl pulsar-broker -Dtest=org.apache.pulsar.broker.stats.prometheus.PrometheusMetricsGeneratorWithNoUnsafeTest -DtestForkCount=1 -DtestReuseFork=false
8082
}
8183

8284
function test_group_broker_group_2() {

pulsar-broker/src/main/java/org/apache/pulsar/broker/stats/prometheus/PrometheusMetricsGenerator.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.netty.buffer.CompositeByteBuf;
2626
import io.netty.buffer.PooledByteBufAllocator;
2727
import io.netty.buffer.Unpooled;
28+
import io.netty.util.internal.PlatformDependent;
2829
import io.prometheus.client.Collector;
2930
import java.io.BufferedOutputStream;
3031
import java.io.IOException;
@@ -363,19 +364,24 @@ protected ByteBuf generateMetrics(List<PrometheusRawMetricsProvider> metricsProv
363364
}
364365
}
365366

366-
private ByteBuf allocateMultipartCompositeDirectBuffer() {
367+
ByteBuf allocateMultipartCompositeDirectBuffer() {
367368
// use composite buffer with pre-allocated buffers to ensure that the pooled allocator can be used
368369
// for allocating the buffers
369370
ByteBufAllocator byteBufAllocator = PulsarByteBufAllocator.DEFAULT;
370-
int chunkSize = resolveChunkSize(byteBufAllocator);
371-
CompositeByteBuf buf = byteBufAllocator.compositeDirectBuffer(
371+
ByteBuf buf;
372+
if (PlatformDependent.hasUnsafe()) {
373+
int chunkSize = resolveChunkSize(byteBufAllocator);
374+
buf = byteBufAllocator.compositeDirectBuffer(
372375
Math.max(MINIMUM_FOR_MAX_COMPONENTS, (initialBufferSize / chunkSize) + 1));
373-
int totalLen = 0;
374-
while (totalLen < initialBufferSize) {
375-
totalLen += chunkSize;
376-
// increase the capacity in increments of chunkSize to preallocate the buffers
377-
// in the composite buffer
378-
buf.capacity(totalLen);
376+
int totalLen = 0;
377+
while (totalLen < initialBufferSize) {
378+
totalLen += chunkSize;
379+
// increase the capacity in increments of chunkSize to preallocate the buffers
380+
// in the composite buffer
381+
buf.capacity(totalLen);
382+
}
383+
} else {
384+
buf = byteBufAllocator.directBuffer(initialBufferSize);
379385
}
380386
return buf;
381387
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.pulsar.broker.stats.prometheus;
20+
21+
import static org.testng.Assert.assertFalse;
22+
import io.netty.buffer.ByteBuf;
23+
import io.netty.util.internal.PlatformDependent;
24+
import java.time.Clock;
25+
import lombok.Cleanup;
26+
import org.apache.pulsar.common.util.SimpleTextOutputStream;
27+
import org.testng.annotations.BeforeClass;
28+
import org.testng.annotations.Test;
29+
30+
@Test(groups = "broker-isolated")
31+
public class PrometheusMetricsGeneratorWithNoUnsafeTest {
32+
33+
@BeforeClass
34+
static void setup() {
35+
System.setProperty("io.netty.noUnsafe", "true");
36+
}
37+
38+
@Test
39+
public void testWriteStringWithNoUnsafe() {
40+
assertFalse(PlatformDependent.hasUnsafe());
41+
@Cleanup
42+
PrometheusMetricsGenerator generator = new PrometheusMetricsGenerator(null, false, false, false, false,
43+
Clock.systemUTC());
44+
@Cleanup("release")
45+
ByteBuf buf = generator.allocateMultipartCompositeDirectBuffer();
46+
for (int i = 0; i < 2; i++) {
47+
buf.writeBytes(new byte[1024 * 1024]);
48+
}
49+
SimpleTextOutputStream outputStream = new SimpleTextOutputStream(buf);
50+
outputStream.write("test");
51+
}
52+
}

0 commit comments

Comments
 (0)