This is an automated email from the ASF dual-hosted git repository.
yujun777 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new 248ba48b45a [fix](statistics) Bound pending statistics cache loads
(#70400)
248ba48b45a is described below
commit 248ba48b45ad307776730562d46d5db4e2352d58
Author: yujun <[email protected]>
AuthorDate: Sat Nov 21 23:05:49 2026 +0800
[fix](statistics) Bound pending statistics cache loads (#70400)
The FE loads missing column statistics asynchronously while planning
queries. Column, partition column and histogram caches share a
statistics fetch executor.
### What problem does this PR solve?
Issue Number: None
Related PR: None
PR Type: Bugfix
Problem Summary:
Distinct cache misses can enqueue unbounded statistics loads because
incomplete Caffeine entries have zero eviction weight and the executor
queue is effectively unbounded. Bound the shared loading queue and
degrade rejected lookups to unknown statistics. Separate cache
maintenance from SQL loading and handle rejected refresh and preheat
submissions.
### Details
- **Change map:** Three production files and a focused concurrency test
suite.
- **Frequency:** When distinct statistics cache misses are submitted
faster than the fetch workers can complete them.
- **Root cause:** Pending futures are excluded from size-based eviction
while the fetch executor accepts an effectively unbounded backlog.
- **Why existing tests missed it:** Cache tests cover normal loading and
eviction but do not saturate the loader queue.
- **Fix:** Add a bounded shared executor with immediate rejection,
preserve old values on rejected automatic refresh, invalidate the
previous value on rejected explicit refresh, and tolerate rejected
preheating during FE role transitions. Repeated hits after a rejected
automatic refresh keep using the previous statistics while that loader
waits one second before submitting another automatic refresh. Explicit
refresh submits directly and lets Caffeine conditionally invalidate the
old entry on rejection. Register a rejection counter and isolate cache
maintenance.
- **Other paths and branches:** Cover column, partition column and
histogram caches. The shared BasicAsyncCacheLoader and external
row-count cache retain their existing behavior. This change targets
master.
- **Default sizing:** The queue permits 100,000 pending loads to
accommodate bursts across wide tables. Operators can configure a larger
or smaller positive limit before restarting FE.
- **Behavior change:** Overloaded lookups may continue using unknown
statistics until capacity becomes available. Sessions forbidding unknown
column statistics can fail under these conditions.
### Testing
Locally passed StatisticsCacheOverloadTest (8 tests, including the
production-constructor saturation case in a separate run), CacheTest (8
tests), and StatisticsCacheTest (6 tests) through tools/fast-fe-ut.sh
and its run-fe-ut.sh fallback. Independently passed Checkstyle for
fe-common and fe-core and git diff --check. No production workload or
memory pressure benchmark was run.
### Release note
FE statistics cache loading now has a bounded pending queue controlled
by stats_fetch_queue_size (default 100000, effective after restarting
FE). When saturated, new lookups use unknown statistics instead of
accumulating an unbounded backlog.
---
.../main/java/org/apache/doris/common/Config.java | 4 +
.../doris/statistics/cache/StatisticsCache.java | 93 +++++--
.../statistics/cache/StatisticsCacheLoader.java | 90 +++++++
.../cache/StatisticsCacheOverloadTest.java | 286 +++++++++++++++++++++
4 files changed, 445 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 9cb44d25916..250a20692c6 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
@@ -2590,6 +2590,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/cache/StatisticsCache.java
b/fe/fe-core/src/main/java/org/apache/doris/statistics/cache/StatisticsCache.java
index f49a5500e97..423bb4ce0a8 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/statistics/cache/StatisticsCache.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/statistics/cache/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;
@@ -58,46 +61,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) {
@@ -130,6 +156,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);
}
@@ -159,6 +187,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);
}
@@ -207,6 +237,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);
}
@@ -243,16 +275,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/cache/StatisticsCacheLoader.java
b/fe/fe-core/src/main/java/org/apache/doris/statistics/cache/StatisticsCacheLoader.java
new file mode 100644
index 00000000000..69b2d6abecc
--- /dev/null
+++
b/fe/fe-core/src/main/java/org/apache/doris/statistics/cache/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.cache;
+
+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/cache/StatisticsCacheOverloadTest.java
b/fe/fe-core/src/test/java/org/apache/doris/statistics/cache/StatisticsCacheOverloadTest.java
new file mode 100644
index 00000000000..a408a84cc1a
--- /dev/null
+++
b/fe/fe-core/src/test/java/org/apache/doris/statistics/cache/StatisticsCacheOverloadTest.java
@@ -0,0 +1,286 @@
+// 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.cache;
+
+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 org.apache.doris.statistics.model.ColumnStatistic;
+import org.apache.doris.statistics.model.ColumnStatisticBuilder;
+import org.apache.doris.statistics.model.PartitionColumnStatistic;
+
+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]