Skip to content

Commit e5aa85f

Browse files
guan46lhotari
authored andcommitted
[improve][ml] Use lock-free queue in InflightReadsLimiter since there's no concurrent access (#23962)
(cherry picked from commit 38a41e0)
1 parent 657d5ee commit e5aa85f

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

Diff for: managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/InflightReadsLimiter.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.opentelemetry.api.OpenTelemetry;
2323
import io.opentelemetry.api.metrics.ObservableLongCounter;
2424
import io.prometheus.client.Gauge;
25+
import java.util.ArrayDeque;
2526
import java.util.Optional;
2627
import java.util.Queue;
2728
import java.util.concurrent.ScheduledExecutorService;
@@ -31,7 +32,6 @@
3132
import org.apache.pulsar.opentelemetry.Constants;
3233
import org.apache.pulsar.opentelemetry.OpenTelemetryAttributes.InflightReadLimiterUtilization;
3334
import org.apache.pulsar.opentelemetry.annotations.PulsarDeprecatedMetric;
34-
import org.jctools.queues.SpscArrayQueue;
3535

3636
@Slf4j
3737
public class InflightReadsLimiter implements AutoCloseable {
@@ -51,6 +51,7 @@ public class InflightReadsLimiter implements AutoCloseable {
5151
public static final String INFLIGHT_READS_LIMITER_USAGE_METRIC_NAME =
5252
"pulsar.broker.managed_ledger.inflight.read.usage";
5353
private final ObservableLongCounter inflightReadsUsageCounter;
54+
private final int maxReadsInFlightAcquireQueueSize;
5455

5556
@PulsarDeprecatedMetric(newMetricName = INFLIGHT_READS_LIMITER_USAGE_METRIC_NAME)
5657
@Deprecated
@@ -82,9 +83,10 @@ public InflightReadsLimiter(long maxReadsInFlightSize, int maxReadsInFlightAcqui
8283
this.remainingBytes = maxReadsInFlightSize;
8384
this.acquireTimeoutMillis = acquireTimeoutMillis;
8485
this.timeOutExecutor = timeOutExecutor;
86+
this.maxReadsInFlightAcquireQueueSize = maxReadsInFlightAcquireQueueSize;
8587
if (maxReadsInFlightSize > 0) {
8688
enabled = true;
87-
this.queuedHandles = new SpscArrayQueue<>(maxReadsInFlightAcquireQueueSize);
89+
this.queuedHandles = new ArrayDeque<>();
8890
} else {
8991
enabled = false;
9092
this.queuedHandles = null;
@@ -174,13 +176,14 @@ private synchronized Optional<Handle> internalAcquire(long permits, Consumer<Han
174176
updateMetrics();
175177
return Optional.of(new Handle(maxReadsInFlightSize, handle.creationTime, true));
176178
} else {
177-
if (queuedHandles.offer(new QueuedHandle(handle, callback))) {
178-
scheduleTimeOutCheck(acquireTimeoutMillis);
179-
return Optional.empty();
180-
} else {
179+
if (queuedHandles.size() >= maxReadsInFlightAcquireQueueSize) {
181180
log.warn("Failed to queue handle for acquiring permits: {}, creationTime: {}, remainingBytes:{}",
182181
permits, handle.creationTime, remainingBytes);
183182
return Optional.of(new Handle(0, handle.creationTime, false));
183+
} else {
184+
queuedHandles.offer(new QueuedHandle(handle, callback));
185+
scheduleTimeOutCheck(acquireTimeoutMillis);
186+
return Optional.empty();
184187
}
185188
}
186189
}

0 commit comments

Comments
 (0)