lhotari commented on code in PR #24833: URL: https://github.com/apache/pulsar/pull/24833#discussion_r2429735799
########## pulsar-common/src/main/java/org/apache/pulsar/common/semaphore/AsyncSemaphoreImpl.java: ########## @@ -0,0 +1,288 @@ +/* + * 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.common.semaphore; + +import io.netty.util.concurrent.DefaultThreadFactory; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.AtomicLongFieldUpdater; +import java.util.function.BooleanSupplier; +import java.util.function.LongConsumer; +import org.apache.pulsar.common.util.Runnables; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Implementation of AsyncSemaphore with timeout and queue size limits. + */ +public class AsyncSemaphoreImpl implements AsyncSemaphore, AutoCloseable { + private static final Logger log = LoggerFactory.getLogger(AsyncSemaphoreImpl.class); + + private final AtomicLong availablePermits; + private final ConcurrentLinkedQueue<PendingRequest> queue = new ConcurrentLinkedQueue<>(); + private final long maxPermits; + private final int maxQueueSize; + private final long timeoutMillis; + private final ScheduledExecutorService executor; + private final boolean shutdownExecutor; + private final LongConsumer queueLatencyRecorder; + private final AtomicBoolean closed = new AtomicBoolean(false); + private final Runnable processQueueRunnable = Runnables.catchingAndLoggingThrowables(this::internalProcessQueue); + + public AsyncSemaphoreImpl(long maxPermits, int maxQueueSize, long timeoutMillis) { + this(maxPermits, maxQueueSize, timeoutMillis, createExecutor(), true, null); + } + + public AsyncSemaphoreImpl(long maxPermits, int maxQueueSize, long timeoutMillis, + ScheduledExecutorService executor, LongConsumer queueLatencyRecorder) { + this(maxPermits, maxQueueSize, timeoutMillis, executor, false, queueLatencyRecorder); + } + + AsyncSemaphoreImpl(long maxPermits, int maxQueueSize, long timeoutMillis, ScheduledExecutorService executor, + boolean shutdownExecutor, LongConsumer queueLatencyRecorder) { + this.availablePermits = new AtomicLong(maxPermits); + this.maxPermits = maxPermits; + this.maxQueueSize = maxQueueSize; + this.timeoutMillis = timeoutMillis; + this.executor = executor; + this.shutdownExecutor = shutdownExecutor; + this.queueLatencyRecorder = queueLatencyRecorder; + } + + private static ScheduledExecutorService createExecutor() { + return Executors.newSingleThreadScheduledExecutor( + new DefaultThreadFactory("async-semaphore-executor")); + } + + @Override + public CompletableFuture<AsyncSemaphorePermit> acquire(long permits, BooleanSupplier isCancelled) { + return internalAcquire(permits, permits, isCancelled); + } + + private CompletableFuture<AsyncSemaphorePermit> internalAcquire(long permits, long acquirePermits, + BooleanSupplier isCancelled) { + if (permits < 0) { + throw new IllegalArgumentException("Invalid permits value: " + permits); + } + + CompletableFuture<AsyncSemaphorePermit> future = new CompletableFuture<>(); + + if (closed.get()) { + future.completeExceptionally(new PermitAcquireAlreadyClosedException("Semaphore is closed")); + return future; + } + + if (queue.size() >= maxQueueSize) { Review Comment: I'll replace the queue implementation with ArrayBlockingQueue and use `offer` to add to the queue. (it returns false if the queue size limit has been reached) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
