wu-sheng commented on code in PR #13703: URL: https://github.com/apache/skywalking/pull/13703#discussion_r2809268270
########## oap-server/server-library/library-batch-queue/src/main/java/org/apache/skywalking/oap/server/library/batchqueue/BatchQueueManager.java: ########## @@ -0,0 +1,179 @@ +/* + * 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.skywalking.oap.server.library.batchqueue; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.atomic.AtomicInteger; +import lombok.extern.slf4j.Slf4j; + +/** + * Global registry for batch queues and shared schedulers. + * Thread-safe. Queues are created by name and shared across modules. + * + * <p>Shared schedulers are created lazily on first queue reference — no separate + * setup step needed. This eliminates startup ordering dependencies. + * + * <p>Shared schedulers are reference-counted: each queue that uses a shared scheduler + * increments the count on creation and decrements on shutdown. When the count reaches + * zero, the scheduler is shut down automatically. + * + * <p>Internal maps: + * <pre> + * QUEUES: queueName -> BatchQueue instance + * SHARED_SCHEDULERS: schedulerName -> ScheduledExecutorService + * SHARED_SCHEDULER_POLICIES: schedulerName -> ThreadPolicy (first-wins) + * SHARED_SCHEDULER_REF_COUNTS: schedulerName -> AtomicInteger (reference count) + * </pre> + */ +@Slf4j +public class BatchQueueManager { + /** + * queueName -> BatchQueue instance. Each queue has a unique name. + */ + private static final ConcurrentHashMap<String, BatchQueue<?>> QUEUES = new ConcurrentHashMap<>(); + /** + * schedulerName -> ScheduledExecutorService. Multiple queues can share one scheduler by + * referencing the same scheduler name in their config. + */ + private static final ConcurrentHashMap<String, ScheduledExecutorService> SHARED_SCHEDULERS = + new ConcurrentHashMap<>(); + /** + * schedulerName -> ThreadPolicy. Tracks the first-wins policy for each shared scheduler + * to detect mismatched configs. + */ + private static final ConcurrentHashMap<String, ThreadPolicy> SHARED_SCHEDULER_POLICIES = + new ConcurrentHashMap<>(); + /** + * schedulerName -> reference count. Incremented when a queue acquires the scheduler, + * decremented when a queue releases it. Scheduler is shut down when count reaches 0. + */ + private static final ConcurrentHashMap<String, AtomicInteger> SHARED_SCHEDULER_REF_COUNTS = + new ConcurrentHashMap<>(); + + /** + * Get or create a shared scheduler and increment its reference count. + * Called internally by BatchQueue constructor. + * First call creates the scheduler; subsequent calls reuse it. + * If ThreadPolicy differs from the first creator, logs a warning (first one wins). + */ + static ScheduledExecutorService getOrCreateSharedScheduler(final String name, + final ThreadPolicy threads) { + SHARED_SCHEDULER_POLICIES.compute(name, (k, existing) -> { + if (existing != null) { + if (!existing.toString().equals(threads.toString())) { + log.warn("Shared scheduler [{}]: ThreadPolicy mismatch. " + + "Existing={}, requested={}. Using existing.", + name, existing, threads); + } + return existing; + } + return threads; Review Comment: Fixed. Added equals()/hashCode() to ThreadPolicy and replaced toString() comparison with equals() (commit 3187b53b67). -- 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]
