This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch branch-4.2
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-4.2 by this push:
     new a031b52381a branch-4.2: [fix](statistics) Bound pending statistics 
cache loads #70400 (#70888)
a031b52381a is described below

commit a031b52381ac65be4e0ee79c5fadde01fb57ff9c
Author: yujun <[email protected]>
AuthorDate: Mon Nov 30 11:29:24 2026 +0800

    branch-4.2: [fix](statistics) Bound pending statistics cache loads #70400 
(#70888)
    
    cherry-pick: #70400
---
 .../main/java/org/apache/doris/common/Config.java  |   4 +
 .../apache/doris/statistics/StatisticsCache.java   |  93 +++++--
 .../doris/statistics/StatisticsCacheLoader.java    |  90 +++++++
 .../statistics/StatisticsCacheOverloadTest.java    | 283 +++++++++++++++++++++
 4 files changed, 442 insertions(+), 28 deletions(-)

diff --git a/fe/fe-common/src/main/java/org/apache/doris/common/Config.java 
b/fe/fe-common/src/main/java/org/apache/doris/common/Config.java
index 8a047a27084..1321ef0e917 100644
--- a/fe/fe-common/src/main/java/org/apache/doris/common/Config.java
+++ b/fe/fe-common/src/main/java/org/apache/doris/common/Config.java
@@ -2839,6 +2839,10 @@ public class Config extends ConfigBase {
     @ConfField
     public static long stats_cache_size = 50_0000;
 
+    @ConfField(mutable = false, description = "Maximum queued statistics cache 
loads. Must be positive. "
+            + "When full, new lookups use unknown statistics. Takes effect 
after restarting FE.")
+    public static int stats_fetch_queue_size = 100000;
+
     /**
      * This config used for ranger cache data mask/row policy
      */
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/statistics/StatisticsCache.java 
b/fe/fe-core/src/main/java/org/apache/doris/statistics/StatisticsCache.java
index 4e7ea91f945..113c53a6e23 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/statistics/StatisticsCache.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/statistics/StatisticsCache.java
@@ -26,6 +26,9 @@ import org.apache.doris.common.Config;
 import org.apache.doris.common.FeConstants;
 import org.apache.doris.common.ThreadPoolManager;
 import org.apache.doris.datasource.CatalogIf;
+import org.apache.doris.metric.LongCounterMetric;
+import org.apache.doris.metric.Metric;
+import org.apache.doris.metric.MetricRepo;
 import org.apache.doris.nereids.trees.plans.algebra.OlapScan;
 import org.apache.doris.persist.gson.GsonUtils;
 import org.apache.doris.qe.ConnectContext;
@@ -50,46 +53,69 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Optional;
 import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.RejectedExecutionException;
 import java.util.concurrent.ThreadPoolExecutor;
 
 public class StatisticsCache {
 
     private static final Logger LOG = 
LogManager.getLogger(StatisticsCache.class);
 
-    /**
-     * Use a standalone thread pool to avoid interference between this and any 
other jdk function
-     * that use the thread of ForkJoinPool#common in the system.
-     */
-    private final ThreadPoolExecutor threadPool
-            = ThreadPoolManager.newDaemonFixedThreadPool(
-            10, Integer.MAX_VALUE, "STATS_FETCH", true);
+    private final ThreadPoolExecutor threadPool;
+    private final LongCounterMetric rejectedLoads = new LongCounterMetric(
+            "stats_cache_load_rejected", Metric.MetricUnit.REQUESTS,
+            "Statistics cache loads rejected due to saturation");
 
     private final ColumnStatisticsCacheLoader columnStatisticCacheLoader = new 
ColumnStatisticsCacheLoader();
     private final HistogramCacheLoader histogramCacheLoader = new 
HistogramCacheLoader();
     private final PartitionColumnStatisticCacheLoader 
partitionColumnStatisticCacheLoader
             = new PartitionColumnStatisticCacheLoader();
 
-    private final AsyncLoadingCache<StatisticsCacheKey, 
Optional<ColumnStatistic>> columnStatisticsCache =
-            Caffeine.newBuilder()
-                    .maximumSize(Config.stats_cache_size)
-                    
.refreshAfterWrite(Duration.ofHours(StatisticConstants.STATISTICS_CACHE_REFRESH_INTERVAL))
-                    .executor(threadPool)
-                    .buildAsync(columnStatisticCacheLoader);
-
-    private final AsyncLoadingCache<StatisticsCacheKey, Optional<Histogram>> 
histogramCache =
-            Caffeine.newBuilder()
-                    .maximumSize(Config.stats_cache_size)
-                    
.refreshAfterWrite(Duration.ofHours(StatisticConstants.STATISTICS_CACHE_REFRESH_INTERVAL))
-                    .executor(threadPool)
-                    .buildAsync(histogramCacheLoader);
-
+    private final AsyncLoadingCache<StatisticsCacheKey, 
Optional<ColumnStatistic>> columnStatisticsCache;
+    private final AsyncLoadingCache<StatisticsCacheKey, Optional<Histogram>> 
histogramCache;
     private final AsyncLoadingCache<PartitionColumnStatisticCacheKey, 
Optional<PartitionColumnStatistic>>
-            partitionColumnStatisticCache =
-            Caffeine.newBuilder()
+            partitionColumnStatisticCache;
+
+    public StatisticsCache() {
+        this(ThreadPoolManager.newDaemonFixedThreadPool(10, 
Config.stats_fetch_queue_size,
+                        "STATS_FETCH", true, new 
ThreadPoolExecutor.AbortPolicy()),
+                ThreadPoolManager.newDaemonFixedThreadPool(1, 16,
+                        "STATS_CACHE_MAINTENANCE", true, new 
ThreadPoolExecutor.AbortPolicy()));
+        MetricRepo.DORIS_METRIC_REGISTER.addMetrics(rejectedLoads);
+    }
+
+    @VisibleForTesting
+    StatisticsCache(ThreadPoolExecutor threadPool, ThreadPoolExecutor 
maintenancePool) {
+        this.threadPool = threadPool;
+        columnStatisticsCache = Caffeine.newBuilder()
+                .maximumSize(Config.stats_cache_size)
+                
.refreshAfterWrite(Duration.ofHours(StatisticConstants.STATISTICS_CACHE_REFRESH_INTERVAL))
+                .executor(maintenancePool)
+                .buildAsync(new 
StatisticsCacheLoader<>(columnStatisticCacheLoader, this::executeLoad));
+        histogramCache = Caffeine.newBuilder()
+                .maximumSize(Config.stats_cache_size)
+                
.refreshAfterWrite(Duration.ofHours(StatisticConstants.STATISTICS_CACHE_REFRESH_INTERVAL))
+                .executor(maintenancePool)
+                .buildAsync(new StatisticsCacheLoader<>(histogramCacheLoader, 
this::executeLoad));
+        partitionColumnStatisticCache = Caffeine.newBuilder()
                 .maximumSize(Config.stats_cache_size)
                 
.refreshAfterWrite(Duration.ofHours(StatisticConstants.STATISTICS_CACHE_REFRESH_INTERVAL))
-                .executor(threadPool)
-                .buildAsync(partitionColumnStatisticCacheLoader);
+                .executor(maintenancePool)
+                .buildAsync(new 
StatisticsCacheLoader<>(partitionColumnStatisticCacheLoader, 
this::executeLoad));
+    }
+
+    private void executeLoad(Runnable task) {
+        try {
+            threadPool.execute(task);
+        } catch (RejectedExecutionException e) {
+            rejectedLoads.increase(1L);
+            throw e;
+        }
+    }
+
+    @VisibleForTesting
+    long getRejectedLoadCount() {
+        return rejectedLoads.getValue();
+    }
 
     public ColumnStatistic getColumnStatistics(
             long catalogId, long dbId, long tblId, long idxId, String colName, 
ConnectContext ctx) {
@@ -122,6 +148,8 @@ public class StatisticsCache {
                 }
                 return columnStatistic.get();
             }
+        } catch (RejectedExecutionException e) {
+            // Queue saturation is expected: this lookup uses unknown 
statistics.
         } catch (Exception e) {
             LOG.warn("Unexpected exception while returning ColumnStatistic", 
e);
         }
@@ -151,6 +179,8 @@ public class StatisticsCache {
             if (f.isDone()) {
                 return f.get().orElse(PartitionColumnStatistic.UNKNOWN);
             }
+        } catch (RejectedExecutionException e) {
+            // Queue saturation is expected: this lookup uses unknown 
statistics.
         } catch (Exception e) {
             LOG.warn("Unexpected exception while returning ColumnStatistic", 
e);
         }
@@ -199,6 +229,8 @@ public class StatisticsCache {
             if (f.isDone()) {
                 return f.get();
             }
+        } catch (RejectedExecutionException e) {
+            // Queue saturation is expected: this lookup uses unknown 
statistics.
         } catch (Exception e) {
             LOG.warn("Unexpected exception while returning Histogram", e);
         }
@@ -235,16 +267,21 @@ public class StatisticsCache {
     }
 
     public void refreshColStatsSync(long ctlId, long dbId, long tblId, long 
idxId, String colName) {
-        columnStatisticsCache.synchronous().refresh(new 
StatisticsCacheKey(ctlId, dbId, tblId, idxId, colName));
+        StatisticsCacheLoader.refresh(columnStatisticsCache,
+                new StatisticsCacheKey(ctlId, dbId, tblId, idxId, colName));
     }
 
     public void refreshHistogramSync(long ctlId, long dbId, long tblId, long 
idxId, String colName) {
-        histogramCache.synchronous().refresh(new StatisticsCacheKey(ctlId, 
dbId, tblId, idxId, colName));
+        StatisticsCacheLoader.refresh(histogramCache, new 
StatisticsCacheKey(ctlId, dbId, tblId, idxId, colName));
     }
 
     public void preHeat() {
         if (!FeConstants.disablePreHeat) {
-            threadPool.submit(this::doPreHeat);
+            try {
+                executeLoad(this::doPreHeat);
+            } catch (RejectedExecutionException e) {
+                // Preheating is best effort, including during FE role 
transitions.
+            }
         }
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/statistics/StatisticsCacheLoader.java
 
b/fe/fe-core/src/main/java/org/apache/doris/statistics/StatisticsCacheLoader.java
new file mode 100644
index 00000000000..cd56f559f3d
--- /dev/null
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/statistics/StatisticsCacheLoader.java
@@ -0,0 +1,90 @@
+// 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.doris.statistics;
+
+import com.github.benmanes.caffeine.cache.AsyncCacheLoader;
+import com.github.benmanes.caffeine.cache.AsyncLoadingCache;
+import com.github.benmanes.caffeine.cache.Ticker;
+import com.google.common.annotations.VisibleForTesting;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Executor;
+import java.util.concurrent.RejectedExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
+
+/** Keeps statistics SQL off the cache maintenance executor. */
+final class StatisticsCacheLoader<K, V> implements AsyncCacheLoader<K, V> {
+    @VisibleForTesting
+    static final long REFRESH_RETRY_DELAY_NANOS = TimeUnit.SECONDS.toNanos(1);
+    private static final ThreadLocal<Boolean> EXPLICIT_REFRESH = new 
ThreadLocal<>();
+
+    private final BasicAsyncCacheLoader<K, V> delegate;
+    private final Executor executor;
+    private final Ticker ticker;
+    private final AtomicLong retryAfter;
+    private final CompletableFuture<V> skippedRefresh = new 
CompletableFuture<>();
+
+    StatisticsCacheLoader(BasicAsyncCacheLoader<K, V> delegate, Executor 
executor) {
+        this(delegate, executor, Ticker.systemTicker());
+    }
+
+    @VisibleForTesting
+    StatisticsCacheLoader(BasicAsyncCacheLoader<K, V> delegate, Executor 
executor, Ticker ticker) {
+        this.delegate = delegate;
+        this.executor = executor;
+        this.ticker = ticker;
+        retryAfter = new AtomicLong(ticker.read());
+        skippedRefresh.cancel(false);
+    }
+
+    static <K, V> void refresh(AsyncLoadingCache<K, V> cache, K key) {
+        // Caffeine invokes asyncReload synchronously while submitting an 
explicit refresh.
+        EXPLICIT_REFRESH.set(true);
+        try {
+            cache.synchronous().refresh(key);
+        } catch (RejectedExecutionException e) {
+            // A cold load was rejected before publishing a future.
+        } finally {
+            EXPLICIT_REFRESH.remove();
+        }
+    }
+
+    @Override
+    public CompletableFuture<V> asyncLoad(K key, Executor ignored) {
+        // Throw synchronously on rejection, so Caffeine does not publish a 
pending entry.
+        return delegate.asyncLoad(key, executor);
+    }
+
+    @Override
+    public CompletableFuture<V> asyncReload(K key, V oldValue, Executor 
ignored) {
+        boolean explicit = Boolean.TRUE.equals(EXPLICIT_REFRESH.get());
+        if (!explicit && retryAfter.get() - ticker.read() > 0) {
+            return skippedRefresh;
+        }
+        try {
+            return asyncLoad(key, ignored);
+        } catch (RejectedExecutionException e) {
+            long nextRetry = ticker.read() + REFRESH_RETRY_DELAY_NANOS;
+            retryAfter.accumulateAndGet(nextRetry, (current, next) -> next - 
current > 0 ? next : current);
+            // A null reload removes only the old entry Caffeine captured for 
an explicit refresh.
+            // An automatic refresh cancellation preserves the old value 
during backoff.
+            return explicit ? CompletableFuture.completedFuture(null) : 
skippedRefresh;
+        }
+    }
+}
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/statistics/StatisticsCacheOverloadTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/statistics/StatisticsCacheOverloadTest.java
new file mode 100644
index 00000000000..41c1abb0015
--- /dev/null
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/statistics/StatisticsCacheOverloadTest.java
@@ -0,0 +1,283 @@
+// 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.doris.statistics;
+
+import org.apache.doris.common.Config;
+import org.apache.doris.common.FeConstants;
+import org.apache.doris.common.ThreadPoolManager;
+import org.apache.doris.common.jmockit.Deencapsulation;
+import org.apache.doris.metric.Metric;
+import org.apache.doris.metric.MetricRepo;
+
+import com.github.benmanes.caffeine.cache.AsyncLoadingCache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+
+import java.lang.reflect.Field;
+import java.time.Duration;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.RejectedExecutionException;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
+
+@Timeout(30)
+class StatisticsCacheOverloadTest {
+    private final ThreadPoolExecutor loads = pool();
+    private final ThreadPoolExecutor maintenance = new ThreadPoolExecutor(1, 
1, 0, TimeUnit.SECONDS,
+            new LinkedBlockingQueue<>(16), new 
ThreadPoolExecutor.AbortPolicy());
+    private final CountDownLatch release = new CountDownLatch(1);
+    private final CountDownLatch drained = new CountDownLatch(1);
+
+    private static ThreadPoolExecutor pool() {
+        return new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS,
+                new LinkedBlockingQueue<>(1), new 
ThreadPoolExecutor.AbortPolicy());
+    }
+
+    @AfterEach
+    void tearDown() throws InterruptedException {
+        release.countDown();
+        loads.shutdownNow();
+        maintenance.shutdownNow();
+        Assertions.assertTrue(loads.awaitTermination(10, TimeUnit.SECONDS));
+        Assertions.assertTrue(maintenance.awaitTermination(10, 
TimeUnit.SECONDS));
+    }
+
+    private void saturate() throws InterruptedException {
+        blockWorkers(loads, 1);
+        loads.execute(drained::countDown);
+    }
+
+    private void blockWorkers(ThreadPoolExecutor pool, int workers) throws 
InterruptedException {
+        CountDownLatch started = new CountDownLatch(workers);
+        for (int i = 0; i < workers; i++) {
+            pool.execute(() -> {
+                started.countDown();
+                try {
+                    release.await();
+                } catch (InterruptedException e) {
+                    Thread.currentThread().interrupt();
+                }
+            });
+        }
+        Assertions.assertTrue(started.await(10, TimeUnit.SECONDS));
+    }
+
+    private StatisticsCacheLoader<Integer, Integer> loader() {
+        return new StatisticsCacheLoader<>(integerLoader(), loads);
+    }
+
+    private static BasicAsyncCacheLoader<Integer, Integer> integerLoader() {
+        return new BasicAsyncCacheLoader<Integer, Integer>() {
+            @Override
+            protected Integer doLoad(Integer key) {
+                return key;
+            }
+        };
+    }
+
+    @Test
+    void rejectedColdLoadsLeaveNoPendingEntriesAndCanRecover() throws 
Exception {
+        saturate();
+        AsyncLoadingCache<Integer, Integer> cache = Caffeine.newBuilder()
+                .maximumSize(1).executor(maintenance).buildAsync(loader());
+        for (int i = 0; i < 1000; i++) {
+            final int key = i;
+            Assertions.assertThrows(RejectedExecutionException.class, () -> 
cache.get(key));
+        }
+        Assertions.assertTrue(cache.asMap().isEmpty());
+        Assertions.assertEquals(1, loads.getQueue().size());
+        release.countDown();
+        Assertions.assertTrue(drained.await(10, TimeUnit.SECONDS));
+        Assertions.assertEquals(7, cache.get(7).get(10, TimeUnit.SECONDS));
+    }
+
+    @Test
+    void pendingKeysAreDeduplicatedAndShareTheExecutorLimit() throws Exception 
{
+        blockWorkers(loads, 1);
+        AsyncLoadingCache<Integer, Integer> first = Caffeine.newBuilder()
+                .maximumSize(1).executor(maintenance).buildAsync(loader());
+        AsyncLoadingCache<Integer, Integer> second = Caffeine.newBuilder()
+                .maximumSize(1).executor(maintenance).buildAsync(loader());
+        CompletableFuture<Integer> pending = first.get(1);
+        Assertions.assertSame(pending, first.get(1));
+        Assertions.assertThrows(RejectedExecutionException.class, () -> 
second.get(2));
+        Assertions.assertTrue(second.asMap().isEmpty());
+        release.countDown();
+        Assertions.assertEquals(1, pending.get(10, TimeUnit.SECONDS));
+    }
+
+    @Test
+    void automaticRefreshRetainsOldValueAndDoesNotBlockMaintenance() throws 
Exception {
+        saturate();
+        AtomicLong clock = new AtomicLong();
+        AsyncLoadingCache<Integer, Integer> cache = Caffeine.newBuilder()
+                
.maximumSize(1).ticker(clock::get).refreshAfterWrite(Duration.ofSeconds(1))
+                .executor(maintenance).buildAsync(loader());
+        cache.put(7, CompletableFuture.completedFuture(70));
+        clock.set(TimeUnit.SECONDS.toNanos(2));
+        Assertions.assertEquals(70, cache.get(7).get(10, TimeUnit.SECONDS));
+        Assertions.assertEquals(70, cache.getIfPresent(7).join());
+        CountDownLatch maintained = new CountDownLatch(1);
+        // A maintenance task can run while SQL loading remains saturated.
+        maintenance.execute(maintained::countDown);
+        Assertions.assertTrue(maintained.await(10, TimeUnit.SECONDS));
+    }
+
+    @Test
+    void automaticRefreshBacksOffAndRecoversWhenQueueDrains() throws Exception 
{
+        saturate();
+        AtomicLong clock = new AtomicLong();
+        AtomicInteger attempts = new AtomicInteger();
+        StatisticsCacheLoader<Integer, Integer> loader = new 
StatisticsCacheLoader<>(integerLoader(), task -> {
+            attempts.incrementAndGet();
+            loads.execute(task);
+        }, clock::get);
+        AsyncLoadingCache<Integer, Integer> cache = Caffeine.newBuilder()
+                
.maximumSize(1).ticker(clock::get).refreshAfterWrite(Duration.ofSeconds(1))
+                .executor(Runnable::run).buildAsync(loader);
+        cache.put(7, CompletableFuture.completedFuture(70));
+        clock.set(TimeUnit.SECONDS.toNanos(2));
+
+        for (int i = 0; i < 1000; i++) {
+            Assertions.assertEquals(70, cache.get(7).join());
+        }
+        Assertions.assertEquals(1, attempts.get());
+
+        release.countDown();
+        Assertions.assertTrue(drained.await(10, TimeUnit.SECONDS));
+        Assertions.assertEquals(70, cache.get(7).join());
+        Assertions.assertEquals(1, attempts.get());
+
+        clock.addAndGet(StatisticsCacheLoader.REFRESH_RETRY_DELAY_NANOS);
+        cache.get(7);
+        // The single worker publishes the refreshed value before starting the 
next task.
+        CountDownLatch refreshed = new CountDownLatch(1);
+        loads.getQueue().put(refreshed::countDown);
+        Assertions.assertTrue(refreshed.await(10, TimeUnit.SECONDS));
+        Assertions.assertEquals(2, attempts.get());
+        Assertions.assertEquals(7, 
cache.synchronous().policy().getIfPresentQuietly(7));
+    }
+
+    @Test
+    void 
expiredEntrySubmitsOnlyOneExplicitRefreshAndIsInvalidatedOnRejection() throws 
Exception {
+        saturate();
+        AtomicLong clock = new AtomicLong();
+        AtomicInteger attempts = new AtomicInteger();
+        StatisticsCacheLoader<Integer, Integer> loader = new 
StatisticsCacheLoader<>(integerLoader(), task -> {
+            attempts.incrementAndGet();
+            loads.execute(task);
+        }, clock::get);
+        AsyncLoadingCache<Integer, Integer> cache = Caffeine.newBuilder()
+                
.maximumSize(1).ticker(clock::get).refreshAfterWrite(Duration.ofSeconds(1))
+                .executor(Runnable::run).buildAsync(loader);
+        cache.put(7, CompletableFuture.completedFuture(70));
+        clock.set(TimeUnit.SECONDS.toNanos(2));
+
+        StatisticsCacheLoader.refresh(cache, 7);
+
+        Assertions.assertEquals(1, attempts.get());
+        Assertions.assertTrue(cache.asMap().isEmpty());
+    }
+
+    @Test
+    void productionQueueLimitRejectsPromptly() throws Exception {
+        String metricName = "stats_cache_load_rejected";
+        List<Metric> previousMetrics = 
MetricRepo.DORIS_METRIC_REGISTER.getMetricsByName(metricName);
+        Field registryField = 
ThreadPoolManager.class.getDeclaredField("nameToThreadPoolMap");
+        registryField.setAccessible(true);
+        @SuppressWarnings("unchecked")
+        Map<String, ThreadPoolExecutor> registeredPools = (Map<String, 
ThreadPoolExecutor>) registryField.get(null);
+        ThreadPoolExecutor previousLoads = registeredPools.get("STATS_FETCH");
+        ThreadPoolExecutor previousMaintenance = 
registeredPools.get("STATS_CACHE_MAINTENANCE");
+        int previousQueueSize = Config.stats_fetch_queue_size;
+        ThreadPoolExecutor productionLoads = null;
+        ThreadPoolExecutor productionMaintenance = null;
+        try {
+            Config.stats_fetch_queue_size = 1;
+            StatisticsCache cache = new StatisticsCache();
+            productionLoads = Deencapsulation.getField(cache, "threadPool");
+            productionMaintenance = 
registeredPools.get("STATS_CACHE_MAINTENANCE");
+            blockWorkers(productionLoads, 10);
+            productionLoads.execute(drained::countDown);
+            Assertions.assertEquals(0, 
productionLoads.getQueue().remainingCapacity());
+            Assertions.assertTimeoutPreemptively(Duration.ofSeconds(2), () ->
+                    Assertions.assertSame(ColumnStatistic.UNKNOWN,
+                            cache.getColumnStatistics(1, 1, 1, -1, "c", 
null)));
+            Assertions.assertEquals(1, cache.getRejectedLoadCount());
+            Assertions.assertEquals(1, productionLoads.getQueue().size());
+        } finally {
+            Config.stats_fetch_queue_size = previousQueueSize;
+            release.countDown();
+            if (productionLoads != null) {
+                productionLoads.shutdownNow();
+                registeredPools.computeIfPresent("STATS_FETCH", (name, pool) ->
+                        pool.isShutdown() ? previousLoads : pool);
+                Assertions.assertTrue(productionLoads.awaitTermination(10, 
TimeUnit.SECONDS));
+            }
+            if (productionMaintenance != null && productionMaintenance != 
previousMaintenance) {
+                productionMaintenance.shutdownNow();
+                registeredPools.computeIfPresent("STATS_CACHE_MAINTENANCE", 
(name, pool) ->
+                        pool.isShutdown() ? previousMaintenance : pool);
+                
Assertions.assertTrue(productionMaintenance.awaitTermination(10, 
TimeUnit.SECONDS));
+            }
+            MetricRepo.DORIS_METRIC_REGISTER.removeMetrics(metricName);
+            
previousMetrics.forEach(MetricRepo.DORIS_METRIC_REGISTER::addMetrics);
+        }
+    }
+
+    @Test
+    void statisticsLookupsAndPreheatDegradeWithoutPropagatingRejection() 
throws Exception {
+        saturate();
+        StatisticsCache cache = new StatisticsCache(loads, maintenance);
+        Assertions.assertSame(ColumnStatistic.UNKNOWN,
+                cache.getColumnStatistics(1, 1, 1, -1, "c", null));
+        Assertions.assertSame(PartitionColumnStatistic.UNKNOWN,
+                cache.getPartitionColumnStatistics(1, 1, 1, -1, "p", "c", 
null));
+        Assertions.assertNull(cache.getHistogram(1, 1, 1, "c"));
+        Assertions.assertDoesNotThrow(() -> cache.refreshHistogramSync(1, 1, 
1, -1, "c"));
+        boolean previous = FeConstants.disablePreHeat;
+        try {
+            FeConstants.disablePreHeat = false;
+            Assertions.assertDoesNotThrow(cache::preHeat);
+        } finally {
+            FeConstants.disablePreHeat = previous;
+        }
+        Assertions.assertEquals(5, cache.getRejectedLoadCount());
+    }
+
+    @Test
+    void rejectedExplicitRefreshInvalidatesYoungOldValue() throws Exception {
+        saturate();
+        StatisticsCache cache = new StatisticsCache(loads, maintenance);
+        ColumnStatistic old = new ColumnStatisticBuilder(10).setNdv(5).build();
+        cache.updateColStatsCache(1, 1, 1, -1, "c", old);
+        Assertions.assertSame(old, cache.getColumnStatistics(1, 1, 1, -1, "c", 
null));
+        Assertions.assertDoesNotThrow(() -> cache.refreshColStatsSync(1, 1, 1, 
-1, "c"));
+        Assertions.assertSame(ColumnStatistic.UNKNOWN,
+                cache.getColumnStatistics(1, 1, 1, -1, "c", null));
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to