From d47c8e679f0a46926072bd0247d7ad7f32d822a6 Mon Sep 17 00:00:00 2001 From: GWphua Date: Tue, 21 Oct 2025 15:24:07 +0800 Subject: [PATCH 01/21] Add segment load on startup by period --- .../segment/loading/SegmentLoaderConfig.java | 23 +++++++++ .../loading/SegmentLocalCacheManager.java | 8 +++- .../HistoricalStartupCacheLoadStrategy.java | 27 +++++++++++ ...oricalStartupCacheLoadStrategyFactory.java | 43 +++++++++++++++++ .../startup/LoadAllEagerlyStrategy.java | 41 ++++++++++++++++ .../startup/LoadAllLazilyStrategy.java | 41 ++++++++++++++++ .../startup/LoadEagerlyBeforePeriod.java | 48 +++++++++++++++++++ .../server/metrics/SegmentStatsMonitor.java | 3 +- .../metrics/SegmentStatsMonitorTest.java | 5 +- 9 files changed, 234 insertions(+), 5 deletions(-) create mode 100644 server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategy.java create mode 100644 server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactory.java create mode 100644 server/src/main/java/org/apache/druid/server/coordination/startup/LoadAllEagerlyStrategy.java create mode 100644 server/src/main/java/org/apache/druid/server/coordination/startup/LoadAllLazilyStrategy.java create mode 100644 server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java diff --git a/server/src/main/java/org/apache/druid/segment/loading/SegmentLoaderConfig.java b/server/src/main/java/org/apache/druid/segment/loading/SegmentLoaderConfig.java index e4822f0661b7..f5a56828118b 100644 --- a/server/src/main/java/org/apache/druid/segment/loading/SegmentLoaderConfig.java +++ b/server/src/main/java/org/apache/druid/segment/loading/SegmentLoaderConfig.java @@ -22,7 +22,10 @@ import com.fasterxml.jackson.annotation.JacksonInject; import com.fasterxml.jackson.annotation.JsonProperty; import com.google.common.collect.Lists; +import org.apache.druid.server.coordination.startup.LoadAllEagerlyStrategy; +import org.apache.druid.server.coordination.startup.LoadAllLazilyStrategy; import org.apache.druid.utils.RuntimeInfo; +import org.joda.time.Period; import java.io.File; import java.util.Collections; @@ -41,9 +44,16 @@ public class SegmentLoaderConfig @JsonProperty private List locations = Collections.emptyList(); + @Deprecated @JsonProperty("lazyLoadOnStart") private boolean lazyLoadOnStart = false; + @JsonProperty("startupLoadStrategy") + private String startupLoadStrategy = null; + + @JsonProperty("startupLoadPeriod") + private Period startupLoadPeriod = new Period("P7D"); + @JsonProperty("deleteOnRemove") private boolean deleteOnRemove = true; @@ -89,6 +99,19 @@ public boolean isLazyLoadOnStart() return lazyLoadOnStart; } + public String getStartupCacheLoadStrategy() + { + return startupLoadStrategy == null + ? isLazyLoadOnStart() + ? LoadAllLazilyStrategy.STRATEGY_NAME + : LoadAllEagerlyStrategy.STRATEGY_NAME + : startupLoadStrategy; + } + + public Period getStartupLoadPeriod() { + return startupLoadPeriod; + } + public boolean isDeleteOnRemove() { return deleteOnRemove; diff --git a/server/src/main/java/org/apache/druid/segment/loading/SegmentLocalCacheManager.java b/server/src/main/java/org/apache/druid/segment/loading/SegmentLocalCacheManager.java index 104304dce6a4..5a1e4235d5fb 100644 --- a/server/src/main/java/org/apache/druid/segment/loading/SegmentLocalCacheManager.java +++ b/server/src/main/java/org/apache/druid/segment/loading/SegmentLocalCacheManager.java @@ -42,6 +42,8 @@ import org.apache.druid.segment.ReferenceCountedSegmentProvider; import org.apache.druid.segment.Segment; import org.apache.druid.segment.SegmentLazyLoadFailCallback; +import org.apache.druid.server.coordination.startup.HistoricalStartupCacheLoadStrategy; +import org.apache.druid.server.coordination.startup.HistoricalStartupCacheLoadStrategyFactory; import org.apache.druid.timeline.DataSegment; import org.apache.druid.timeline.SegmentId; import org.apache.druid.utils.CloseableUtils; @@ -77,6 +79,7 @@ public class SegmentLocalCacheManager implements SegmentCacheManager private final SegmentLoaderConfig config; private final ObjectMapper jsonMapper; + private final HistoricalStartupCacheLoadStrategy loadStrategy; private final List locations; @@ -128,6 +131,8 @@ public SegmentLocalCacheManager( this.strategy = strategy; this.indexIO = indexIO; + this.loadStrategy = HistoricalStartupCacheLoadStrategyFactory.factorize(config); + log.info("Using storage location strategy[%s].", this.strategy.getClass().getSimpleName()); if (config.isVirtualStorage()) { @@ -853,7 +858,8 @@ public void mount(StorageLocation mountLocation) throws SegmentLoadingException final SegmentizerFactory factory = getSegmentFactory(storageDir); @SuppressWarnings("ObjectEquality") - final boolean lazy = config.isLazyLoadOnStart() && lazyLoadCallback != SegmentLazyLoadFailCallback.NOOP; + final boolean lazy = loadStrategy.shouldLoadLazily(dataSegment) + && lazyLoadCallback != SegmentLazyLoadFailCallback.NOOP; final Segment segment = factory.factorize(dataSegment, storageDir, lazy, lazyLoadCallback); // wipe load callback after calling lazyLoadCallback = SegmentLazyLoadFailCallback.NOOP; diff --git a/server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategy.java b/server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategy.java new file mode 100644 index 000000000000..fa3940e40cb2 --- /dev/null +++ b/server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategy.java @@ -0,0 +1,27 @@ +/* + * 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.druid.server.coordination.startup; + +import org.apache.druid.timeline.DataSegment; + +public interface HistoricalStartupCacheLoadStrategy +{ + boolean shouldLoadLazily(DataSegment segment); +} diff --git a/server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactory.java b/server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactory.java new file mode 100644 index 000000000000..3579285f3322 --- /dev/null +++ b/server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactory.java @@ -0,0 +1,43 @@ +/* + * 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.druid.server.coordination.startup; + +import org.apache.druid.error.DruidException; +import org.apache.druid.segment.loading.SegmentLoaderConfig; + +public class HistoricalStartupCacheLoadStrategyFactory +{ + public static HistoricalStartupCacheLoadStrategy factorize(SegmentLoaderConfig config) + { + String strategyName = config.getStartupCacheLoadStrategy(); + switch (strategyName) { + case LoadAllLazilyStrategy.STRATEGY_NAME: + return new LoadAllLazilyStrategy(); + case LoadAllEagerlyStrategy.STRATEGY_NAME: + return new LoadAllEagerlyStrategy(); + case LoadEagerlyBeforePeriod.STRATEGY_NAME: + return new LoadEagerlyBeforePeriod(config.getStartupLoadPeriod()); + default: + throw DruidException.forPersona(DruidException.Persona.OPERATOR) + .ofCategory(DruidException.Category.UNSUPPORTED) + .build("Unknown configured Historical Startup Loading Strategy[%s]", strategyName); + } + } +} diff --git a/server/src/main/java/org/apache/druid/server/coordination/startup/LoadAllEagerlyStrategy.java b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadAllEagerlyStrategy.java new file mode 100644 index 000000000000..652729f2f4a9 --- /dev/null +++ b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadAllEagerlyStrategy.java @@ -0,0 +1,41 @@ +/* + * 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.druid.server.coordination.startup; + +import org.apache.druid.java.util.common.logger.Logger; +import org.apache.druid.timeline.DataSegment; + +public class LoadAllEagerlyStrategy implements HistoricalStartupCacheLoadStrategy +{ + private static final Logger log = new Logger(LoadAllEagerlyStrategy.class); + + public static final String STRATEGY_NAME = "loadAllEagerly"; + + public LoadAllEagerlyStrategy() + { + log.info("Using [%s] strategy", STRATEGY_NAME); + } + + @Override + public boolean shouldLoadLazily(DataSegment segment) + { + return false; + } +} diff --git a/server/src/main/java/org/apache/druid/server/coordination/startup/LoadAllLazilyStrategy.java b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadAllLazilyStrategy.java new file mode 100644 index 000000000000..5994f400a1e5 --- /dev/null +++ b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadAllLazilyStrategy.java @@ -0,0 +1,41 @@ +/* + * 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.druid.server.coordination.startup; + +import org.apache.druid.java.util.common.logger.Logger; +import org.apache.druid.timeline.DataSegment; + +public class LoadAllLazilyStrategy implements HistoricalStartupCacheLoadStrategy +{ + private static final Logger log = new Logger(LoadAllLazilyStrategy.class); + + public static final String STRATEGY_NAME = "loadAllLazily"; + + public LoadAllLazilyStrategy() + { + log.info("Using [%s] strategy", STRATEGY_NAME); + } + + @Override + public boolean shouldLoadLazily(DataSegment segment) + { + return true; + } +} diff --git a/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java new file mode 100644 index 000000000000..337e4e328988 --- /dev/null +++ b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java @@ -0,0 +1,48 @@ +/* + * 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.druid.server.coordination.startup; + +import org.apache.druid.java.util.common.DateTimes; +import org.apache.druid.java.util.common.logger.Logger; +import org.apache.druid.timeline.DataSegment; +import org.joda.time.DateTime; +import org.joda.time.Interval; +import org.joda.time.Period; + +public class LoadEagerlyBeforePeriod implements HistoricalStartupCacheLoadStrategy +{ + private static final Logger log = new Logger(LoadEagerlyBeforePeriod.class); + public static final String STRATEGY_NAME = "loadEagerlyBeforePeriod"; + private final Interval eagerLoadingWindow; + + public LoadEagerlyBeforePeriod(Period eagerLoadingPeriod) + { + DateTime now = DateTimes.nowUtc(); + this.eagerLoadingWindow = new Interval(now.minus(eagerLoadingPeriod), now); + + log.info("Using [%s] strategy with Interval[%s]", STRATEGY_NAME, eagerLoadingWindow); + } + + @Override + public boolean shouldLoadLazily(DataSegment segment) + { + return !segment.getInterval().overlaps(eagerLoadingWindow); + } +} diff --git a/server/src/main/java/org/apache/druid/server/metrics/SegmentStatsMonitor.java b/server/src/main/java/org/apache/druid/server/metrics/SegmentStatsMonitor.java index 4afc745ae4fe..353b19176a60 100644 --- a/server/src/main/java/org/apache/druid/server/metrics/SegmentStatsMonitor.java +++ b/server/src/main/java/org/apache/druid/server/metrics/SegmentStatsMonitor.java @@ -33,6 +33,7 @@ import org.apache.druid.query.DruidMetrics; import org.apache.druid.segment.loading.SegmentLoaderConfig; import org.apache.druid.server.coordination.SegmentLoadDropHandler; +import org.apache.druid.server.coordination.startup.LoadAllLazilyStrategy; import java.util.Map; @@ -63,7 +64,7 @@ public SegmentStatsMonitor( SegmentLoaderConfig segmentLoaderConfig ) { - if (segmentLoaderConfig.isLazyLoadOnStart()) { + if (segmentLoaderConfig.getStartupCacheLoadStrategy().equals(LoadAllLazilyStrategy.STRATEGY_NAME)) { // log message ensures there is an error displayed at startup if this fails as the exception isn't logged. log.error("Monitor doesn't support working with lazy loading on start"); // throw this exception it kill the process at startup diff --git a/server/src/test/java/org/apache/druid/server/metrics/SegmentStatsMonitorTest.java b/server/src/test/java/org/apache/druid/server/metrics/SegmentStatsMonitorTest.java index 373b1c1dd2d6..6296d3c888a9 100644 --- a/server/src/test/java/org/apache/druid/server/metrics/SegmentStatsMonitorTest.java +++ b/server/src/test/java/org/apache/druid/server/metrics/SegmentStatsMonitorTest.java @@ -28,6 +28,7 @@ import org.apache.druid.query.DruidMetrics; import org.apache.druid.segment.loading.SegmentLoaderConfig; import org.apache.druid.server.coordination.SegmentLoadDropHandler; +import org.apache.druid.server.coordination.startup.LoadAllLazilyStrategy; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -73,9 +74,7 @@ public void setUp() public void testLazyLoadOnStartThrowsException() { SegmentLoaderConfig segmentLoaderConfig = Mockito.mock(SegmentLoaderConfig.class); - Mockito.when(segmentLoaderConfig.isLazyLoadOnStart()).thenReturn(true); - - //should throw an exception here + Mockito.when(segmentLoaderConfig.getStartupCacheLoadStrategy()).thenReturn(LoadAllLazilyStrategy.STRATEGY_NAME); new SegmentStatsMonitor(druidServerConfig, segmentLoadDropMgr, segmentLoaderConfig); } From 34081eecea5baaf84d20cc3f607eacb2eeacd2b5 Mon Sep 17 00:00:00 2001 From: GWphua Date: Tue, 21 Oct 2025 17:19:56 +0800 Subject: [PATCH 02/21] Change test file --- .../druid/segment/loading/SegmentLoaderConfig.java | 10 +++------- .../segment/loading/SegmentLocalCacheManagerTest.java | 5 +++-- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/server/src/main/java/org/apache/druid/segment/loading/SegmentLoaderConfig.java b/server/src/main/java/org/apache/druid/segment/loading/SegmentLoaderConfig.java index f5a56828118b..d599ff82e516 100644 --- a/server/src/main/java/org/apache/druid/segment/loading/SegmentLoaderConfig.java +++ b/server/src/main/java/org/apache/druid/segment/loading/SegmentLoaderConfig.java @@ -94,21 +94,17 @@ public List getLocations() return locations; } - public boolean isLazyLoadOnStart() - { - return lazyLoadOnStart; - } - public String getStartupCacheLoadStrategy() { return startupLoadStrategy == null - ? isLazyLoadOnStart() + ? lazyLoadOnStart ? LoadAllLazilyStrategy.STRATEGY_NAME : LoadAllEagerlyStrategy.STRATEGY_NAME : startupLoadStrategy; } - public Period getStartupLoadPeriod() { + public Period getStartupLoadPeriod() + { return startupLoadPeriod; } diff --git a/server/src/test/java/org/apache/druid/segment/loading/SegmentLocalCacheManagerTest.java b/server/src/test/java/org/apache/druid/segment/loading/SegmentLocalCacheManagerTest.java index 04eafa41ed71..7dcb4d936e0a 100644 --- a/server/src/test/java/org/apache/druid/segment/loading/SegmentLocalCacheManagerTest.java +++ b/server/src/test/java/org/apache/druid/segment/loading/SegmentLocalCacheManagerTest.java @@ -44,6 +44,7 @@ import org.apache.druid.segment.TestIndex; import org.apache.druid.segment.TestSegmentUtils; import org.apache.druid.segment.column.ColumnConfig; +import org.apache.druid.server.coordination.startup.LoadAllLazilyStrategy; import org.apache.druid.server.metrics.NoopServiceEmitter; import org.apache.druid.testing.InitializedNullHandlingTest; import org.apache.druid.timeline.DataSegment; @@ -847,9 +848,9 @@ public void testGetBootstrapSegmentLazy() throws SegmentLoadingException final SegmentLoaderConfig loaderConfig = new SegmentLoaderConfig() { @Override - public boolean isLazyLoadOnStart() + public String getStartupCacheLoadStrategy() { - return true; + return LoadAllLazilyStrategy.STRATEGY_NAME; } @Override From 88407fc3ccf03b4c615fedeeb11d2f2b9d7a0eb2 Mon Sep 17 00:00:00 2001 From: GWphua Date: Wed, 22 Oct 2025 17:21:03 +0800 Subject: [PATCH 03/21] Unit test for Strategy Factory --- ...alStartupCacheLoadStrategyFactoryTest.java | 106 ++++++++++++++++++ .../startup/LoadEagerlyBeforePeriodTest.java | 4 + 2 files changed, 110 insertions(+) create mode 100644 server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java create mode 100644 server/src/test/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriodTest.java diff --git a/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java b/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java new file mode 100644 index 000000000000..82c9da14c894 --- /dev/null +++ b/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java @@ -0,0 +1,106 @@ +/* + * 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.druid.server.coordination.startup; + +import org.apache.druid.segment.loading.SegmentLoaderConfig; + +import org.joda.time.Period; +import org.junit.Assert; +import org.junit.Test; + +public class HistoricalStartupCacheLoadStrategyFactoryTest +{ + @Test + public void testConstructionOfEagerLoadingBeforePeriodFromConfig() { + final SegmentLoaderConfig config = new SegmentLoaderConfig() + { + @Override + public String getStartupCacheLoadStrategy() + { + return LoadEagerlyBeforePeriod.STRATEGY_NAME; + } + + @Override + public Period getStartupLoadPeriod() + { + return Period.days(7); + } + }; + + HistoricalStartupCacheLoadStrategy strategy = HistoricalStartupCacheLoadStrategyFactory.factorize(config); + Assert.assertEquals(LoadEagerlyBeforePeriod.class, strategy.getClass()); + Assert.assertEquals( + config.getStartupLoadPeriod().toStandardDuration(), + ((LoadEagerlyBeforePeriod) strategy).getEagerLoadingWindow().toDuration() + ); + } + + @Test + public void testConstructionOfLazyLoadingFromConfig() { + final SegmentLoaderConfig config = new SegmentLoaderConfig() + { + @Override + public String getStartupCacheLoadStrategy() + { + return LoadAllLazilyStrategy.STRATEGY_NAME; + } + + // This config is not applicable for lazy / eager. + @Override + public Period getStartupLoadPeriod() + { + return Period.days(7); + } + }; + + HistoricalStartupCacheLoadStrategy strategy = HistoricalStartupCacheLoadStrategyFactory.factorize(config); + Assert.assertEquals(LoadAllLazilyStrategy.class, strategy.getClass()); + } + + @Test + public void testConstructionOfEagerLoadingFromConfig() { + final SegmentLoaderConfig config = new SegmentLoaderConfig() + { + @Override + public String getStartupCacheLoadStrategy() + { + return LoadAllEagerlyStrategy.STRATEGY_NAME; + } + + // This config is not applicable for lazy / eager. + @Override + public Period getStartupLoadPeriod() + { + return Period.days(7); + } + }; + + HistoricalStartupCacheLoadStrategy strategy = HistoricalStartupCacheLoadStrategyFactory.factorize(config); + Assert.assertEquals(LoadAllEagerlyStrategy.class, strategy.getClass()); + } + + @Test + public void testDefaultConstruction() { + final SegmentLoaderConfig config = new SegmentLoaderConfig() {}; + + HistoricalStartupCacheLoadStrategy strategy = HistoricalStartupCacheLoadStrategyFactory.factorize(config); + Assert.assertEquals(LoadAllEagerlyStrategy.class, strategy.getClass()); + } +} diff --git a/server/src/test/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriodTest.java b/server/src/test/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriodTest.java new file mode 100644 index 000000000000..2f00d15eab17 --- /dev/null +++ b/server/src/test/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriodTest.java @@ -0,0 +1,4 @@ +import static org.junit.jupiter.api.Assertions.*; +class LoadEagerlyBeforePeriodTest { + +} \ No newline at end of file From 9978f73922fa63a8cf4bdca6825b28a718e1f6e0 Mon Sep 17 00:00:00 2001 From: GWphua Date: Wed, 22 Oct 2025 17:21:42 +0800 Subject: [PATCH 04/21] Load eagerly test --- .../startup/LoadEagerlyBeforePeriod.java | 8 +++ .../startup/LoadEagerlyBeforePeriodTest.java | 62 +++++++++++++++++-- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java index 337e4e328988..47b4680facb9 100644 --- a/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java +++ b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java @@ -19,6 +19,7 @@ package org.apache.druid.server.coordination.startup; +import com.google.common.annotations.VisibleForTesting; import org.apache.druid.java.util.common.DateTimes; import org.apache.druid.java.util.common.logger.Logger; import org.apache.druid.timeline.DataSegment; @@ -30,6 +31,7 @@ public class LoadEagerlyBeforePeriod implements HistoricalStartupCacheLoadStrate { private static final Logger log = new Logger(LoadEagerlyBeforePeriod.class); public static final String STRATEGY_NAME = "loadEagerlyBeforePeriod"; + private final Interval eagerLoadingWindow; public LoadEagerlyBeforePeriod(Period eagerLoadingPeriod) @@ -40,6 +42,12 @@ public LoadEagerlyBeforePeriod(Period eagerLoadingPeriod) log.info("Using [%s] strategy with Interval[%s]", STRATEGY_NAME, eagerLoadingWindow); } + @VisibleForTesting + public Interval getEagerLoadingWindow() + { + return this.eagerLoadingWindow; + } + @Override public boolean shouldLoadLazily(DataSegment segment) { diff --git a/server/src/test/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriodTest.java b/server/src/test/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriodTest.java index 2f00d15eab17..e0ff479ef2e6 100644 --- a/server/src/test/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriodTest.java +++ b/server/src/test/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriodTest.java @@ -1,4 +1,58 @@ -import static org.junit.jupiter.api.Assertions.*; -class LoadEagerlyBeforePeriodTest { - -} \ No newline at end of file +/* + * 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.druid.server.coordination.startup; + +import org.apache.druid.java.util.common.Intervals; +import org.apache.druid.segment.TestSegmentUtils; +import org.apache.druid.timeline.DataSegment; +import org.joda.time.DateTime; +import org.joda.time.Interval; +import org.joda.time.Period; +import org.junit.Assert; +import org.junit.Test; + +public class LoadEagerlyBeforePeriodTest { + @Test + public void testLoadEagerlyForSegments() + { + DateTime now = DateTime.now(); + LoadEagerlyBeforePeriod strategy = new LoadEagerlyBeforePeriod(Period.days(7)); + + final DataSegment withinRange = TestSegmentUtils.makeSegment("foo2", "v1", new Interval(now.minusDays(2), now.minusDays(1))); + final DataSegment infiniteRange = TestSegmentUtils.makeSegment("foo1", "v1", Intervals.ETERNITY); + final DataSegment overlappingRange = TestSegmentUtils.makeSegment("foo3", "v1", new Interval(now.minusDays(8), now.minusDays(6))); + + Assert.assertFalse(strategy.shouldLoadLazily(withinRange)); + Assert.assertFalse(strategy.shouldLoadLazily(infiniteRange)); + Assert.assertFalse(strategy.shouldLoadLazily(overlappingRange)); + } + + @Test + public void testLoadLazilyForSegments() + { + LoadEagerlyBeforePeriod strategy = new LoadEagerlyBeforePeriod(Period.days(1)); + DateTime now = DateTime.now(); + final DataSegment laterThanEnd = TestSegmentUtils.makeSegment("foo2", "v1", new Interval(now.plusDays(1), now.plusDays(2))); + final DataSegment earlierThanStart = TestSegmentUtils.makeSegment("foo3", "v1", new Interval(now.minusDays(8), now.minusDays(7))); + + Assert.assertTrue(strategy.shouldLoadLazily(laterThanEnd)); + Assert.assertTrue(strategy.shouldLoadLazily(earlierThanStart)); + } +} From e15b8be96c29ec78dd81b5f5e8272d4471b16cf1 Mon Sep 17 00:00:00 2001 From: GWphua Date: Wed, 22 Oct 2025 17:25:24 +0800 Subject: [PATCH 05/21] Test unknown strategy --- ...alStartupCacheLoadStrategyFactoryTest.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java b/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java index 82c9da14c894..c6d343bf5727 100644 --- a/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java +++ b/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java @@ -19,6 +19,7 @@ package org.apache.druid.server.coordination.startup; +import org.apache.druid.error.DruidException; import org.apache.druid.segment.loading.SegmentLoaderConfig; import org.joda.time.Period; @@ -103,4 +104,23 @@ public void testDefaultConstruction() { HistoricalStartupCacheLoadStrategy strategy = HistoricalStartupCacheLoadStrategyFactory.factorize(config); Assert.assertEquals(LoadAllEagerlyStrategy.class, strategy.getClass()); } + + @Test + public void testConstructionOfInvalidConfig() { + String unknownStrategy = "some unknown strategy"; + final SegmentLoaderConfig config = new SegmentLoaderConfig() + { + @Override + public String getStartupCacheLoadStrategy() + { + return unknownStrategy; + } + }; + + Assert.assertThrows( + "Unknown configured Historical Startup Loading Strategy[" + unknownStrategy + "]", + DruidException.class, + () -> HistoricalStartupCacheLoadStrategyFactory.factorize(config) + ); + } } From 5064bccf1692db72768fee72409e2ab212267c01 Mon Sep 17 00:00:00 2001 From: GWphua Date: Wed, 22 Oct 2025 17:59:14 +0800 Subject: [PATCH 06/21] Documentation for load eagerly before period --- docs/configuration/index.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/docs/configuration/index.md b/docs/configuration/index.md index 8aa5e8184689..04d86ce9c6b6 100644 --- a/docs/configuration/index.md +++ b/docs/configuration/index.md @@ -1585,7 +1585,9 @@ These Historical configurations can be defined in the `historical/runtime.proper |`druid.segmentCache.announceIntervalMillis`|How frequently to announce segments while segments are loading from cache. Set this value to zero to wait for all segments to be loaded before announcing.|5000 (5 seconds)| |`druid.segmentCache.numLoadingThreads`|How many segments to drop or load concurrently from deep storage. Note that the work of loading segments involves downloading segments from deep storage, decompressing them and loading them to a memory mapped location. So the work is not all I/O Bound. Depending on CPU and network load, one could possibly increase this config to a higher value.|max(1,Number of cores / 6)| |`druid.segmentCache.numBootstrapThreads`|How many segments to load concurrently during historical startup.|`druid.segmentCache.numLoadingThreads`| -|`druid.segmentCache.lazyLoadOnStart`|Whether or not to load segment columns metadata lazily during historical startup. When set to true, Historical startup time will be dramatically improved by deferring segment loading until the first time that segment takes part in a query, which will incur this cost instead.|false| +|`druid.segmentCache.lazyLoadOnStart`|_DEPRECATED_ Use `druid.segmentCache.startupLoadStrategy` instead. Whether or not to load segment columns metadata lazily during historical startup. When set to true, Historical startup time will be dramatically improved by deferring segment loading until the first time that segment takes part in a query, which will incur this cost instead.|false| +|`druid.segmentCache.startupLoadStrategy`|Selects the segment column metadata loading strategy during historical startup. Possible values are `loadAllEagerly`, `loadAllLazily`, and `loadEagerlyBeforePeriod`. More details on each strategy below.|`loadAllEagerly`| +|`druid.segmentCache.startupLoadPeriod`| Used only when startup load strategy `loadEagerlyBeforePeriod` is configured. Suppose timestamp `t` is when the Historical started up. Any segment metadata with interval that does not overlap with the interval of `[t - startupLoadPeriod, t]` will be lazily loaded.|`P7D`| |`druid.segmentCache.numThreadsToLoadSegmentsIntoPageCacheOnDownload`|Number of threads to asynchronously read segment index files into null output stream on each new segment download after the Historical service finishes bootstrapping. Recommended to set to 1 or 2 or leave unspecified to disable. See also `druid.segmentCache.numThreadsToLoadSegmentsIntoPageCacheOnBootstrap`|0| |`druid.segmentCache.numThreadsToLoadSegmentsIntoPageCacheOnBootstrap`|Number of threads to asynchronously read segment index files into null output stream during Historical service bootstrap. This thread pool is terminated after Historical service finishes bootstrapping. Recommended to set to half of available cores. If left unspecified, `druid.segmentCache.numThreadsToLoadSegmentsIntoPageCacheOnDownload` will be used. If both configs are unspecified, this feature is disabled. Preemptively loading segments into page cache helps in the sense that later when a segment is queried, it's already in page cache and only a minor page fault needs to be triggered instead of a more costly major page fault to make the query latency more consistent. Note that loading segment into page cache just does a blind loading of segment index files and will evict any existing segments from page cache at the discretion of operating system when the total segment size on local disk is larger than the page cache usable in the RAM, which roughly equals to total available RAM in the host - druid process memory including both heap and direct memory allocated - memory used by other non druid processes on the host, so it is the user's responsibility to ensure the host has enough RAM to host all the segments to avoid random evictions to fully leverage this feature.|`druid.segmentCache.numThreadsToLoadSegmentsIntoPageCacheOnDownload`| @@ -1602,6 +1604,14 @@ In `druid.segmentCache.locationSelector.strategy`, one of `leastBytesUsed`, `rou Note that if `druid.segmentCache.numLoadingThreads` > 1, multiple threads can download different segments at the same time. In this case, with the `leastBytesUsed` strategy or `mostAvailableSize` strategy, Historicals may select a sub-optimal storage location because each decision is based on a snapshot of the storage location status of when a segment is requested to download. +In `druid.segmentCache.startupLoadStrategy`, one of `loadAllEagerly`, `loadAllLazily`, or `loadEagerlyBeforePeriod` could be specified to represent the strategy to load segments when starting the Historical service. + +|Strategy|Description| +|--------|-----------| +|`loadAllEagerly`|The default startup strategy. The Historical service will load all segment column metadata immediately during the initial startup process.| +|`loadAllLazily`|To significantly improve historical system startup time, segments are not loaded during the initial startup sequence. Instead, the loading cost is deferred, and will be incurred the first time a segment is referenced by a query.| +|`loadEagerlyBeforePeriod`|Provides a balance between fast startup and query performance. The Historical service will eagerly load column metadata only for segments that fall within the most recent period defined by `druid.segmentCache.startupLoadPeriod`. Segments outside this recent period will be loaded on-demand when first queried.| + #### Historical query configs ##### Concurrent requests From f0ced70bbffd3fcf85e02127b72e9c7a027ea094 Mon Sep 17 00:00:00 2001 From: GWphua Date: Wed, 22 Oct 2025 18:02:09 +0800 Subject: [PATCH 07/21] Checkstyle --- ...icalStartupCacheLoadStrategyFactoryTest.java | 17 +++++++++++------ .../startup/LoadEagerlyBeforePeriodTest.java | 3 ++- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java b/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java index c6d343bf5727..a6e3ce0a74aa 100644 --- a/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java +++ b/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java @@ -29,7 +29,8 @@ public class HistoricalStartupCacheLoadStrategyFactoryTest { @Test - public void testConstructionOfEagerLoadingBeforePeriodFromConfig() { + public void testConstructionOfEagerLoadingBeforePeriodFromConfig() + { final SegmentLoaderConfig config = new SegmentLoaderConfig() { @Override @@ -54,7 +55,8 @@ public Period getStartupLoadPeriod() } @Test - public void testConstructionOfLazyLoadingFromConfig() { + public void testConstructionOfLazyLoadingFromConfig() + { final SegmentLoaderConfig config = new SegmentLoaderConfig() { @Override @@ -76,7 +78,8 @@ public Period getStartupLoadPeriod() } @Test - public void testConstructionOfEagerLoadingFromConfig() { + public void testConstructionOfEagerLoadingFromConfig() + { final SegmentLoaderConfig config = new SegmentLoaderConfig() { @Override @@ -98,15 +101,17 @@ public Period getStartupLoadPeriod() } @Test - public void testDefaultConstruction() { - final SegmentLoaderConfig config = new SegmentLoaderConfig() {}; + public void testDefaultConstruction() + { + final SegmentLoaderConfig config = new SegmentLoaderConfig(); HistoricalStartupCacheLoadStrategy strategy = HistoricalStartupCacheLoadStrategyFactory.factorize(config); Assert.assertEquals(LoadAllEagerlyStrategy.class, strategy.getClass()); } @Test - public void testConstructionOfInvalidConfig() { + public void testConstructionOfInvalidConfig() + { String unknownStrategy = "some unknown strategy"; final SegmentLoaderConfig config = new SegmentLoaderConfig() { diff --git a/server/src/test/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriodTest.java b/server/src/test/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriodTest.java index e0ff479ef2e6..08b1540a4f2e 100644 --- a/server/src/test/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriodTest.java +++ b/server/src/test/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriodTest.java @@ -28,7 +28,8 @@ import org.junit.Assert; import org.junit.Test; -public class LoadEagerlyBeforePeriodTest { +public class LoadEagerlyBeforePeriodTest +{ @Test public void testLoadEagerlyForSegments() { From f325cf1fdf8aae85390c655e020d70cd723a7da2 Mon Sep 17 00:00:00 2001 From: GWphua Date: Wed, 22 Oct 2025 18:39:57 +0800 Subject: [PATCH 08/21] Forbidden API --- .../coordination/startup/LoadEagerlyBeforePeriodTest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/server/src/test/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriodTest.java b/server/src/test/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriodTest.java index 08b1540a4f2e..9f981618e07b 100644 --- a/server/src/test/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriodTest.java +++ b/server/src/test/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriodTest.java @@ -19,6 +19,7 @@ package org.apache.druid.server.coordination.startup; +import org.apache.druid.java.util.common.DateTimes; import org.apache.druid.java.util.common.Intervals; import org.apache.druid.segment.TestSegmentUtils; import org.apache.druid.timeline.DataSegment; @@ -33,7 +34,7 @@ public class LoadEagerlyBeforePeriodTest @Test public void testLoadEagerlyForSegments() { - DateTime now = DateTime.now(); + DateTime now = DateTimes.nowUtc(); LoadEagerlyBeforePeriod strategy = new LoadEagerlyBeforePeriod(Period.days(7)); final DataSegment withinRange = TestSegmentUtils.makeSegment("foo2", "v1", new Interval(now.minusDays(2), now.minusDays(1))); @@ -49,7 +50,7 @@ public void testLoadEagerlyForSegments() public void testLoadLazilyForSegments() { LoadEagerlyBeforePeriod strategy = new LoadEagerlyBeforePeriod(Period.days(1)); - DateTime now = DateTime.now(); + DateTime now = DateTimes.nowUtc(); final DataSegment laterThanEnd = TestSegmentUtils.makeSegment("foo2", "v1", new Interval(now.plusDays(1), now.plusDays(2))); final DataSegment earlierThanStart = TestSegmentUtils.makeSegment("foo3", "v1", new Interval(now.minusDays(8), now.minusDays(7))); From ae15550e8e4919a2e4c565bb0236810340702449 Mon Sep 17 00:00:00 2001 From: GWphua Date: Wed, 22 Oct 2025 18:51:03 +0800 Subject: [PATCH 09/21] checkstyle --- .../startup/HistoricalStartupCacheLoadStrategyFactoryTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java b/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java index a6e3ce0a74aa..cd0cc2a189f8 100644 --- a/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java +++ b/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java @@ -21,7 +21,6 @@ import org.apache.druid.error.DruidException; import org.apache.druid.segment.loading.SegmentLoaderConfig; - import org.joda.time.Period; import org.junit.Assert; import org.junit.Test; From 05509cd3ccd88952c12a89fe6ca502c7ade5c53a Mon Sep 17 00:00:00 2001 From: GWphua Date: Thu, 23 Oct 2025 10:38:24 +0800 Subject: [PATCH 10/21] Setup test for deprecated method --- .../segment/loading/SegmentLoaderConfig.java | 16 ++++++++++++++- ...alStartupCacheLoadStrategyFactoryTest.java | 20 +++++++++++++++++++ .../startup/LoadEagerlyBeforePeriodTest.java | 8 ++++---- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/server/src/main/java/org/apache/druid/segment/loading/SegmentLoaderConfig.java b/server/src/main/java/org/apache/druid/segment/loading/SegmentLoaderConfig.java index d599ff82e516..960ee4c3cea3 100644 --- a/server/src/main/java/org/apache/druid/segment/loading/SegmentLoaderConfig.java +++ b/server/src/main/java/org/apache/druid/segment/loading/SegmentLoaderConfig.java @@ -44,6 +44,9 @@ public class SegmentLoaderConfig @JsonProperty private List locations = Collections.emptyList(); + /** + * @deprecated Use {@link #startupLoadStrategy} instead. + */ @Deprecated @JsonProperty("lazyLoadOnStart") private boolean lazyLoadOnStart = false; @@ -94,10 +97,21 @@ public List getLocations() return locations; } + /** + * @deprecated Use {@link #getStartupCacheLoadStrategy()} instead. + * Removal of this method in the future will requires a change in {@link #getStartupCacheLoadStrategy()} + * to default to {@link LoadAllEagerlyStrategy#STRATEGY_NAME} when {@link #startupLoadStrategy} is null. + */ + @Deprecated + public boolean isLazyLoadOnStart() + { + return lazyLoadOnStart; + } + public String getStartupCacheLoadStrategy() { return startupLoadStrategy == null - ? lazyLoadOnStart + ? isLazyLoadOnStart() ? LoadAllLazilyStrategy.STRATEGY_NAME : LoadAllEagerlyStrategy.STRATEGY_NAME : startupLoadStrategy; diff --git a/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java b/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java index cd0cc2a189f8..5ca7af645c42 100644 --- a/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java +++ b/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java @@ -108,6 +108,26 @@ public void testDefaultConstruction() Assert.assertEquals(LoadAllEagerlyStrategy.class, strategy.getClass()); } + /** + * Test to be removed when {@link SegmentLoaderConfig#isLazyLoadOnStart()} is removed. + */ + @Test + @SuppressWarnings("deprecation") + public void testConfigWhenLazyLoadSetToTrue() + { + final SegmentLoaderConfig config = new SegmentLoaderConfig() + { + @Override + public boolean isLazyLoadOnStart() + { + return true; + } + }; + + HistoricalStartupCacheLoadStrategy strategy = HistoricalStartupCacheLoadStrategyFactory.factorize(config); + Assert.assertEquals(LoadAllLazilyStrategy.class, strategy.getClass()); + } + @Test public void testConstructionOfInvalidConfig() { diff --git a/server/src/test/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriodTest.java b/server/src/test/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriodTest.java index 9f981618e07b..0000bd2e62b8 100644 --- a/server/src/test/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriodTest.java +++ b/server/src/test/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriodTest.java @@ -51,10 +51,10 @@ public void testLoadLazilyForSegments() { LoadEagerlyBeforePeriod strategy = new LoadEagerlyBeforePeriod(Period.days(1)); DateTime now = DateTimes.nowUtc(); - final DataSegment laterThanEnd = TestSegmentUtils.makeSegment("foo2", "v1", new Interval(now.plusDays(1), now.plusDays(2))); - final DataSegment earlierThanStart = TestSegmentUtils.makeSegment("foo3", "v1", new Interval(now.minusDays(8), now.minusDays(7))); + final DataSegment segmentInFuture = TestSegmentUtils.makeSegment("foo2", "v1", new Interval(now.plusDays(1), now.plusDays(2))); + final DataSegment segmentTooLate = TestSegmentUtils.makeSegment("foo3", "v1", new Interval(now.minusDays(8), now.minusDays(7))); - Assert.assertTrue(strategy.shouldLoadLazily(laterThanEnd)); - Assert.assertTrue(strategy.shouldLoadLazily(earlierThanStart)); + Assert.assertTrue(strategy.shouldLoadLazily(segmentInFuture)); + Assert.assertTrue(strategy.shouldLoadLazily(segmentTooLate)); } } From dc8c7d71f0127dcba9d59d304ae9b9ccd27d8c15 Mon Sep 17 00:00:00 2001 From: Virushade Date: Fri, 24 Oct 2025 15:16:45 +0800 Subject: [PATCH 11/21] Update docs/configuration/index.md Co-authored-by: Frank Chen --- docs/configuration/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/configuration/index.md b/docs/configuration/index.md index 04d86ce9c6b6..2261da01ca6e 100644 --- a/docs/configuration/index.md +++ b/docs/configuration/index.md @@ -1587,7 +1587,7 @@ These Historical configurations can be defined in the `historical/runtime.proper |`druid.segmentCache.numBootstrapThreads`|How many segments to load concurrently during historical startup.|`druid.segmentCache.numLoadingThreads`| |`druid.segmentCache.lazyLoadOnStart`|_DEPRECATED_ Use `druid.segmentCache.startupLoadStrategy` instead. Whether or not to load segment columns metadata lazily during historical startup. When set to true, Historical startup time will be dramatically improved by deferring segment loading until the first time that segment takes part in a query, which will incur this cost instead.|false| |`druid.segmentCache.startupLoadStrategy`|Selects the segment column metadata loading strategy during historical startup. Possible values are `loadAllEagerly`, `loadAllLazily`, and `loadEagerlyBeforePeriod`. More details on each strategy below.|`loadAllEagerly`| -|`druid.segmentCache.startupLoadPeriod`| Used only when startup load strategy `loadEagerlyBeforePeriod` is configured. Suppose timestamp `t` is when the Historical started up. Any segment metadata with interval that does not overlap with the interval of `[t - startupLoadPeriod, t]` will be lazily loaded.|`P7D`| +|`druid.segmentCache.startupLoadPeriod`| Used only when startup load strategy `loadEagerlyBeforePeriod` is configured. Suppose timestamp `t` is when the Historical started up. Any segment metadata with interval that falls in the interval of `[t - startupLoadPeriod, t]` will be loaded.|`P7D`| |`druid.segmentCache.numThreadsToLoadSegmentsIntoPageCacheOnDownload`|Number of threads to asynchronously read segment index files into null output stream on each new segment download after the Historical service finishes bootstrapping. Recommended to set to 1 or 2 or leave unspecified to disable. See also `druid.segmentCache.numThreadsToLoadSegmentsIntoPageCacheOnBootstrap`|0| |`druid.segmentCache.numThreadsToLoadSegmentsIntoPageCacheOnBootstrap`|Number of threads to asynchronously read segment index files into null output stream during Historical service bootstrap. This thread pool is terminated after Historical service finishes bootstrapping. Recommended to set to half of available cores. If left unspecified, `druid.segmentCache.numThreadsToLoadSegmentsIntoPageCacheOnDownload` will be used. If both configs are unspecified, this feature is disabled. Preemptively loading segments into page cache helps in the sense that later when a segment is queried, it's already in page cache and only a minor page fault needs to be triggered instead of a more costly major page fault to make the query latency more consistent. Note that loading segment into page cache just does a blind loading of segment index files and will evict any existing segments from page cache at the discretion of operating system when the total segment size on local disk is larger than the page cache usable in the RAM, which roughly equals to total available RAM in the host - druid process memory including both heap and direct memory allocated - memory used by other non druid processes on the host, so it is the user's responsibility to ensure the host has enough RAM to host all the segments to avoid random evictions to fully leverage this feature.|`druid.segmentCache.numThreadsToLoadSegmentsIntoPageCacheOnDownload`| From 8351de103e9d06a63e2f0f3d399def8dd7b91733 Mon Sep 17 00:00:00 2001 From: GWphua Date: Fri, 24 Oct 2025 15:16:24 +0800 Subject: [PATCH 12/21] Resolve nitpick --- .../coordination/startup/LoadEagerlyBeforePeriod.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java index 47b4680facb9..c41b0b0d9c0e 100644 --- a/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java +++ b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java @@ -32,25 +32,25 @@ public class LoadEagerlyBeforePeriod implements HistoricalStartupCacheLoadStrate private static final Logger log = new Logger(LoadEagerlyBeforePeriod.class); public static final String STRATEGY_NAME = "loadEagerlyBeforePeriod"; - private final Interval eagerLoadingWindow; + private final Interval eagerLoadingInterval; public LoadEagerlyBeforePeriod(Period eagerLoadingPeriod) { DateTime now = DateTimes.nowUtc(); - this.eagerLoadingWindow = new Interval(now.minus(eagerLoadingPeriod), now); + this.eagerLoadingInterval = new Interval(now.minus(eagerLoadingPeriod), now); - log.info("Using [%s] strategy with Interval[%s]", STRATEGY_NAME, eagerLoadingWindow); + log.info("Using [%s] strategy with Interval[%s]", STRATEGY_NAME, eagerLoadingInterval); } @VisibleForTesting public Interval getEagerLoadingWindow() { - return this.eagerLoadingWindow; + return this.eagerLoadingInterval; } @Override public boolean shouldLoadLazily(DataSegment segment) { - return !segment.getInterval().overlaps(eagerLoadingWindow); + return !segment.getInterval().overlaps(eagerLoadingInterval); } } From 019a3bd575170922a052649293994ab84937ca01 Mon Sep 17 00:00:00 2001 From: GWphua Date: Fri, 24 Oct 2025 16:02:39 +0800 Subject: [PATCH 13/21] Javadocs --- .../segment/loading/SegmentLocalCacheManager.java | 4 ++-- .../startup/HistoricalStartupCacheLoadStrategy.java | 12 ++++++++++++ .../druid/server/metrics/SegmentStatsMonitor.java | 3 +-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/server/src/main/java/org/apache/druid/segment/loading/SegmentLocalCacheManager.java b/server/src/main/java/org/apache/druid/segment/loading/SegmentLocalCacheManager.java index 5a1e4235d5fb..3efc72c654e1 100644 --- a/server/src/main/java/org/apache/druid/segment/loading/SegmentLocalCacheManager.java +++ b/server/src/main/java/org/apache/druid/segment/loading/SegmentLocalCacheManager.java @@ -858,8 +858,8 @@ public void mount(StorageLocation mountLocation) throws SegmentLoadingException final SegmentizerFactory factory = getSegmentFactory(storageDir); @SuppressWarnings("ObjectEquality") - final boolean lazy = loadStrategy.shouldLoadLazily(dataSegment) - && lazyLoadCallback != SegmentLazyLoadFailCallback.NOOP; + final boolean lazy = lazyLoadCallback != SegmentLazyLoadFailCallback.NOOP + && loadStrategy.shouldLoadLazily(dataSegment); final Segment segment = factory.factorize(dataSegment, storageDir, lazy, lazyLoadCallback); // wipe load callback after calling lazyLoadCallback = SegmentLazyLoadFailCallback.NOOP; diff --git a/server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategy.java b/server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategy.java index fa3940e40cb2..7b6758cbb0a1 100644 --- a/server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategy.java +++ b/server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategy.java @@ -21,7 +21,19 @@ import org.apache.druid.timeline.DataSegment; +/** + * Strategy for determining whether segments should be loaded lazily or eagerly during + * Historical process startup. A lazily loaded segment defers the loading of the segment + * until it is first accessed, which can help to lower Historical startup time, but + * it can also impact query latency due to the time it takes to first access a segment. + */ public interface HistoricalStartupCacheLoadStrategy { + /** + * Indicates whether the provided segment should be loaded lazily during Historical startup. + * + * @param segment the segment being evaluated + * @return {@code true} if the segment should be loaded lazily, {@code false} if it should be loaded eagerly. + */ boolean shouldLoadLazily(DataSegment segment); } diff --git a/server/src/main/java/org/apache/druid/server/metrics/SegmentStatsMonitor.java b/server/src/main/java/org/apache/druid/server/metrics/SegmentStatsMonitor.java index 353b19176a60..1743ca98a1fd 100644 --- a/server/src/main/java/org/apache/druid/server/metrics/SegmentStatsMonitor.java +++ b/server/src/main/java/org/apache/druid/server/metrics/SegmentStatsMonitor.java @@ -64,7 +64,7 @@ public SegmentStatsMonitor( SegmentLoaderConfig segmentLoaderConfig ) { - if (segmentLoaderConfig.getStartupCacheLoadStrategy().equals(LoadAllLazilyStrategy.STRATEGY_NAME)) { + if (LoadAllLazilyStrategy.STRATEGY_NAME.equals(segmentLoaderConfig.getStartupCacheLoadStrategy())) { // log message ensures there is an error displayed at startup if this fails as the exception isn't logged. log.error("Monitor doesn't support working with lazy loading on start"); // throw this exception it kill the process at startup @@ -72,7 +72,6 @@ public SegmentStatsMonitor( } this.serverConfig = serverConfig; this.segmentLoadDropHandler = segmentLoadDropHandler; - } @Override From 84efec72dcf245718b065a1b991359b80e0f9890 Mon Sep 17 00:00:00 2001 From: GWphua Date: Fri, 24 Oct 2025 17:06:47 +0800 Subject: [PATCH 14/21] JsonTypeInfo --- .../startup/HistoricalStartupCacheLoadStrategy.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategy.java b/server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategy.java index 7b6758cbb0a1..25fedb16f87b 100644 --- a/server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategy.java +++ b/server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategy.java @@ -19,14 +19,21 @@ package org.apache.druid.server.coordination.startup; +import com.fasterxml.jackson.annotation.JsonSubTypes; +import com.fasterxml.jackson.annotation.JsonTypeInfo; import org.apache.druid.timeline.DataSegment; /** * Strategy for determining whether segments should be loaded lazily or eagerly during - * Historical process startup. A lazily loaded segment defers the loading of the segment - * until it is first accessed, which can help to lower Historical startup time, but - * it can also impact query latency due to the time it takes to first access a segment. + * Historical process startup. Lazy loading can help to lower Historical startup time at the + * expense of query latency, by deferring the loading process to the first access of that segment. */ +@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type") +@JsonSubTypes(value = { + @JsonSubTypes.Type(name = LoadAllEagerlyStrategy.STRATEGY_NAME, value = LoadAllEagerlyStrategy.class) + @JsonSubTypes.Type(name = LoadAllLazilyStrategy.STRATEGY_NAME, value = LoadAllLazilyStrategy.class) + @JsonSubTypes.Type(name = LoadEagerlyBeforePeriod.STRATEGY_NAME, value = LoadEagerlyBeforePeriod.class) +}) public interface HistoricalStartupCacheLoadStrategy { /** From 288223de811273313e2d8b7e99bb76189d9d0677 Mon Sep 17 00:00:00 2001 From: GWphua Date: Fri, 24 Oct 2025 17:55:03 +0800 Subject: [PATCH 15/21] Use JSON config... TODO: Change Factory class. --- .../segment/loading/SegmentLoaderConfig.java | 9 ++++----- .../HistoricalStartupCacheLoadStrategy.java | 4 ++-- ...HistoricalStartupCacheLoadStrategyFactory.java | 15 +-------------- .../startup/LoadEagerlyBeforePeriod.java | 8 +++++++- .../druid/server/metrics/SegmentStatsMonitor.java | 2 +- .../loading/SegmentLocalCacheManagerTest.java | 6 +++--- ...oricalStartupCacheLoadStrategyFactoryTest.java | 8 ++++---- .../server/metrics/SegmentStatsMonitorTest.java | 2 +- 8 files changed, 23 insertions(+), 31 deletions(-) diff --git a/server/src/main/java/org/apache/druid/segment/loading/SegmentLoaderConfig.java b/server/src/main/java/org/apache/druid/segment/loading/SegmentLoaderConfig.java index 960ee4c3cea3..00d770ee6890 100644 --- a/server/src/main/java/org/apache/druid/segment/loading/SegmentLoaderConfig.java +++ b/server/src/main/java/org/apache/druid/segment/loading/SegmentLoaderConfig.java @@ -22,6 +22,7 @@ import com.fasterxml.jackson.annotation.JacksonInject; import com.fasterxml.jackson.annotation.JsonProperty; import com.google.common.collect.Lists; +import org.apache.druid.server.coordination.startup.HistoricalStartupCacheLoadStrategy; import org.apache.druid.server.coordination.startup.LoadAllEagerlyStrategy; import org.apache.druid.server.coordination.startup.LoadAllLazilyStrategy; import org.apache.druid.utils.RuntimeInfo; @@ -52,7 +53,7 @@ public class SegmentLoaderConfig private boolean lazyLoadOnStart = false; @JsonProperty("startupLoadStrategy") - private String startupLoadStrategy = null; + private HistoricalStartupCacheLoadStrategy startupLoadStrategy = null; @JsonProperty("startupLoadPeriod") private Period startupLoadPeriod = new Period("P7D"); @@ -108,12 +109,10 @@ public boolean isLazyLoadOnStart() return lazyLoadOnStart; } - public String getStartupCacheLoadStrategy() + public HistoricalStartupCacheLoadStrategy getStartupCacheLoadStrategy() { return startupLoadStrategy == null - ? isLazyLoadOnStart() - ? LoadAllLazilyStrategy.STRATEGY_NAME - : LoadAllEagerlyStrategy.STRATEGY_NAME + ? isLazyLoadOnStart() ? new LoadAllLazilyStrategy() : new LoadAllEagerlyStrategy() : startupLoadStrategy; } diff --git a/server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategy.java b/server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategy.java index 25fedb16f87b..dfa804ede691 100644 --- a/server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategy.java +++ b/server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategy.java @@ -30,8 +30,8 @@ */ @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type") @JsonSubTypes(value = { - @JsonSubTypes.Type(name = LoadAllEagerlyStrategy.STRATEGY_NAME, value = LoadAllEagerlyStrategy.class) - @JsonSubTypes.Type(name = LoadAllLazilyStrategy.STRATEGY_NAME, value = LoadAllLazilyStrategy.class) + @JsonSubTypes.Type(name = LoadAllEagerlyStrategy.STRATEGY_NAME, value = LoadAllEagerlyStrategy.class), + @JsonSubTypes.Type(name = LoadAllLazilyStrategy.STRATEGY_NAME, value = LoadAllLazilyStrategy.class), @JsonSubTypes.Type(name = LoadEagerlyBeforePeriod.STRATEGY_NAME, value = LoadEagerlyBeforePeriod.class) }) public interface HistoricalStartupCacheLoadStrategy diff --git a/server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactory.java b/server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactory.java index 3579285f3322..808db2fd635e 100644 --- a/server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactory.java +++ b/server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactory.java @@ -19,25 +19,12 @@ package org.apache.druid.server.coordination.startup; -import org.apache.druid.error.DruidException; import org.apache.druid.segment.loading.SegmentLoaderConfig; public class HistoricalStartupCacheLoadStrategyFactory { public static HistoricalStartupCacheLoadStrategy factorize(SegmentLoaderConfig config) { - String strategyName = config.getStartupCacheLoadStrategy(); - switch (strategyName) { - case LoadAllLazilyStrategy.STRATEGY_NAME: - return new LoadAllLazilyStrategy(); - case LoadAllEagerlyStrategy.STRATEGY_NAME: - return new LoadAllEagerlyStrategy(); - case LoadEagerlyBeforePeriod.STRATEGY_NAME: - return new LoadEagerlyBeforePeriod(config.getStartupLoadPeriod()); - default: - throw DruidException.forPersona(DruidException.Persona.OPERATOR) - .ofCategory(DruidException.Category.UNSUPPORTED) - .build("Unknown configured Historical Startup Loading Strategy[%s]", strategyName); - } + return config.getStartupCacheLoadStrategy(); } } diff --git a/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java index c41b0b0d9c0e..a34b1a707b26 100644 --- a/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java +++ b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java @@ -19,6 +19,8 @@ package org.apache.druid.server.coordination.startup; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; import com.google.common.annotations.VisibleForTesting; import org.apache.druid.java.util.common.DateTimes; import org.apache.druid.java.util.common.logger.Logger; @@ -34,7 +36,11 @@ public class LoadEagerlyBeforePeriod implements HistoricalStartupCacheLoadStrate private final Interval eagerLoadingInterval; - public LoadEagerlyBeforePeriod(Period eagerLoadingPeriod) + @VisibleForTesting + @JsonCreator + public LoadEagerlyBeforePeriod( + @JsonProperty("period") Period eagerLoadingPeriod + ) { DateTime now = DateTimes.nowUtc(); this.eagerLoadingInterval = new Interval(now.minus(eagerLoadingPeriod), now); diff --git a/server/src/main/java/org/apache/druid/server/metrics/SegmentStatsMonitor.java b/server/src/main/java/org/apache/druid/server/metrics/SegmentStatsMonitor.java index 1743ca98a1fd..5be0a499b0f0 100644 --- a/server/src/main/java/org/apache/druid/server/metrics/SegmentStatsMonitor.java +++ b/server/src/main/java/org/apache/druid/server/metrics/SegmentStatsMonitor.java @@ -64,7 +64,7 @@ public SegmentStatsMonitor( SegmentLoaderConfig segmentLoaderConfig ) { - if (LoadAllLazilyStrategy.STRATEGY_NAME.equals(segmentLoaderConfig.getStartupCacheLoadStrategy())) { + if (segmentLoaderConfig.getStartupCacheLoadStrategy() instanceof LoadAllLazilyStrategy) { // log message ensures there is an error displayed at startup if this fails as the exception isn't logged. log.error("Monitor doesn't support working with lazy loading on start"); // throw this exception it kill the process at startup diff --git a/server/src/test/java/org/apache/druid/segment/loading/SegmentLocalCacheManagerTest.java b/server/src/test/java/org/apache/druid/segment/loading/SegmentLocalCacheManagerTest.java index 7dcb4d936e0a..76e374a839ba 100644 --- a/server/src/test/java/org/apache/druid/segment/loading/SegmentLocalCacheManagerTest.java +++ b/server/src/test/java/org/apache/druid/segment/loading/SegmentLocalCacheManagerTest.java @@ -44,7 +44,7 @@ import org.apache.druid.segment.TestIndex; import org.apache.druid.segment.TestSegmentUtils; import org.apache.druid.segment.column.ColumnConfig; -import org.apache.druid.server.coordination.startup.LoadAllLazilyStrategy; +import org.apache.druid.server.coordination.startup.HistoricalStartupCacheLoadStrategy; import org.apache.druid.server.metrics.NoopServiceEmitter; import org.apache.druid.testing.InitializedNullHandlingTest; import org.apache.druid.timeline.DataSegment; @@ -848,9 +848,9 @@ public void testGetBootstrapSegmentLazy() throws SegmentLoadingException final SegmentLoaderConfig loaderConfig = new SegmentLoaderConfig() { @Override - public String getStartupCacheLoadStrategy() + public HistoricalStartupCacheLoadStrategy getStartupCacheLoadStrategy() { - return LoadAllLazilyStrategy.STRATEGY_NAME; + return LoadAllLazilyStrategy; } @Override diff --git a/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java b/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java index 5ca7af645c42..1031738c99b5 100644 --- a/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java +++ b/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java @@ -33,7 +33,7 @@ public void testConstructionOfEagerLoadingBeforePeriodFromConfig() final SegmentLoaderConfig config = new SegmentLoaderConfig() { @Override - public String getStartupCacheLoadStrategy() + public HistoricalStartupCacheLoadStrategy getStartupCacheLoadStrategy() { return LoadEagerlyBeforePeriod.STRATEGY_NAME; } @@ -59,7 +59,7 @@ public void testConstructionOfLazyLoadingFromConfig() final SegmentLoaderConfig config = new SegmentLoaderConfig() { @Override - public String getStartupCacheLoadStrategy() + public HistoricalStartupCacheLoadStrategy getStartupCacheLoadStrategy() { return LoadAllLazilyStrategy.STRATEGY_NAME; } @@ -82,7 +82,7 @@ public void testConstructionOfEagerLoadingFromConfig() final SegmentLoaderConfig config = new SegmentLoaderConfig() { @Override - public String getStartupCacheLoadStrategy() + public HistoricalStartupCacheLoadStrategy getStartupCacheLoadStrategy() { return LoadAllEagerlyStrategy.STRATEGY_NAME; } @@ -135,7 +135,7 @@ public void testConstructionOfInvalidConfig() final SegmentLoaderConfig config = new SegmentLoaderConfig() { @Override - public String getStartupCacheLoadStrategy() + public HistoricalStartupCacheLoadStrategy getStartupCacheLoadStrategy() { return unknownStrategy; } diff --git a/server/src/test/java/org/apache/druid/server/metrics/SegmentStatsMonitorTest.java b/server/src/test/java/org/apache/druid/server/metrics/SegmentStatsMonitorTest.java index 6296d3c888a9..5e05c84675a7 100644 --- a/server/src/test/java/org/apache/druid/server/metrics/SegmentStatsMonitorTest.java +++ b/server/src/test/java/org/apache/druid/server/metrics/SegmentStatsMonitorTest.java @@ -74,7 +74,7 @@ public void setUp() public void testLazyLoadOnStartThrowsException() { SegmentLoaderConfig segmentLoaderConfig = Mockito.mock(SegmentLoaderConfig.class); - Mockito.when(segmentLoaderConfig.getStartupCacheLoadStrategy()).thenReturn(LoadAllLazilyStrategy.STRATEGY_NAME); + Mockito.when(segmentLoaderConfig.getStartupCacheLoadStrategy()).thenReturn(new LoadAllLazilyStrategy()); new SegmentStatsMonitor(druidServerConfig, segmentLoadDropMgr, segmentLoaderConfig); } From 430e21639861c1d3e918ec6af3ca5da76981dfac Mon Sep 17 00:00:00 2001 From: GWphua Date: Fri, 24 Oct 2025 18:04:28 +0800 Subject: [PATCH 16/21] Add test to JSON serialize correctly --- .../loading/SegmentLocalCacheManagerTest.java | 3 +- ...alStartupCacheLoadStrategyFactoryTest.java | 150 ------------------ ...istoricalStartupCacheLoadStrategyTest.java | 57 +++++++ 3 files changed, 59 insertions(+), 151 deletions(-) delete mode 100644 server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java create mode 100644 server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyTest.java diff --git a/server/src/test/java/org/apache/druid/segment/loading/SegmentLocalCacheManagerTest.java b/server/src/test/java/org/apache/druid/segment/loading/SegmentLocalCacheManagerTest.java index 76e374a839ba..0a92d102577a 100644 --- a/server/src/test/java/org/apache/druid/segment/loading/SegmentLocalCacheManagerTest.java +++ b/server/src/test/java/org/apache/druid/segment/loading/SegmentLocalCacheManagerTest.java @@ -45,6 +45,7 @@ import org.apache.druid.segment.TestSegmentUtils; import org.apache.druid.segment.column.ColumnConfig; import org.apache.druid.server.coordination.startup.HistoricalStartupCacheLoadStrategy; +import org.apache.druid.server.coordination.startup.LoadAllLazilyStrategy; import org.apache.druid.server.metrics.NoopServiceEmitter; import org.apache.druid.testing.InitializedNullHandlingTest; import org.apache.druid.timeline.DataSegment; @@ -850,7 +851,7 @@ public void testGetBootstrapSegmentLazy() throws SegmentLoadingException @Override public HistoricalStartupCacheLoadStrategy getStartupCacheLoadStrategy() { - return LoadAllLazilyStrategy; + return new LoadAllLazilyStrategy(); } @Override diff --git a/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java b/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java deleted file mode 100644 index 1031738c99b5..000000000000 --- a/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactoryTest.java +++ /dev/null @@ -1,150 +0,0 @@ -/* - * 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.druid.server.coordination.startup; - -import org.apache.druid.error.DruidException; -import org.apache.druid.segment.loading.SegmentLoaderConfig; -import org.joda.time.Period; -import org.junit.Assert; -import org.junit.Test; - -public class HistoricalStartupCacheLoadStrategyFactoryTest -{ - @Test - public void testConstructionOfEagerLoadingBeforePeriodFromConfig() - { - final SegmentLoaderConfig config = new SegmentLoaderConfig() - { - @Override - public HistoricalStartupCacheLoadStrategy getStartupCacheLoadStrategy() - { - return LoadEagerlyBeforePeriod.STRATEGY_NAME; - } - - @Override - public Period getStartupLoadPeriod() - { - return Period.days(7); - } - }; - - HistoricalStartupCacheLoadStrategy strategy = HistoricalStartupCacheLoadStrategyFactory.factorize(config); - Assert.assertEquals(LoadEagerlyBeforePeriod.class, strategy.getClass()); - Assert.assertEquals( - config.getStartupLoadPeriod().toStandardDuration(), - ((LoadEagerlyBeforePeriod) strategy).getEagerLoadingWindow().toDuration() - ); - } - - @Test - public void testConstructionOfLazyLoadingFromConfig() - { - final SegmentLoaderConfig config = new SegmentLoaderConfig() - { - @Override - public HistoricalStartupCacheLoadStrategy getStartupCacheLoadStrategy() - { - return LoadAllLazilyStrategy.STRATEGY_NAME; - } - - // This config is not applicable for lazy / eager. - @Override - public Period getStartupLoadPeriod() - { - return Period.days(7); - } - }; - - HistoricalStartupCacheLoadStrategy strategy = HistoricalStartupCacheLoadStrategyFactory.factorize(config); - Assert.assertEquals(LoadAllLazilyStrategy.class, strategy.getClass()); - } - - @Test - public void testConstructionOfEagerLoadingFromConfig() - { - final SegmentLoaderConfig config = new SegmentLoaderConfig() - { - @Override - public HistoricalStartupCacheLoadStrategy getStartupCacheLoadStrategy() - { - return LoadAllEagerlyStrategy.STRATEGY_NAME; - } - - // This config is not applicable for lazy / eager. - @Override - public Period getStartupLoadPeriod() - { - return Period.days(7); - } - }; - - HistoricalStartupCacheLoadStrategy strategy = HistoricalStartupCacheLoadStrategyFactory.factorize(config); - Assert.assertEquals(LoadAllEagerlyStrategy.class, strategy.getClass()); - } - - @Test - public void testDefaultConstruction() - { - final SegmentLoaderConfig config = new SegmentLoaderConfig(); - - HistoricalStartupCacheLoadStrategy strategy = HistoricalStartupCacheLoadStrategyFactory.factorize(config); - Assert.assertEquals(LoadAllEagerlyStrategy.class, strategy.getClass()); - } - - /** - * Test to be removed when {@link SegmentLoaderConfig#isLazyLoadOnStart()} is removed. - */ - @Test - @SuppressWarnings("deprecation") - public void testConfigWhenLazyLoadSetToTrue() - { - final SegmentLoaderConfig config = new SegmentLoaderConfig() - { - @Override - public boolean isLazyLoadOnStart() - { - return true; - } - }; - - HistoricalStartupCacheLoadStrategy strategy = HistoricalStartupCacheLoadStrategyFactory.factorize(config); - Assert.assertEquals(LoadAllLazilyStrategy.class, strategy.getClass()); - } - - @Test - public void testConstructionOfInvalidConfig() - { - String unknownStrategy = "some unknown strategy"; - final SegmentLoaderConfig config = new SegmentLoaderConfig() - { - @Override - public HistoricalStartupCacheLoadStrategy getStartupCacheLoadStrategy() - { - return unknownStrategy; - } - }; - - Assert.assertThrows( - "Unknown configured Historical Startup Loading Strategy[" + unknownStrategy + "]", - DruidException.class, - () -> HistoricalStartupCacheLoadStrategyFactory.factorize(config) - ); - } -} diff --git a/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyTest.java b/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyTest.java new file mode 100644 index 000000000000..d3f48f5390bd --- /dev/null +++ b/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyTest.java @@ -0,0 +1,57 @@ +/* + * 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.druid.server.coordination.startup; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.druid.segment.TestHelper; +import org.junit.Assert; +import org.junit.Test; + +public class HistoricalStartupCacheLoadStrategyTest +{ + private final ObjectMapper mapper = TestHelper.JSON_MAPPER; + + @Test + public void testDeserializeLoadAllEagerly() throws Exception + { + final String json = "{\"type\":\"" + LoadAllEagerlyStrategy.STRATEGY_NAME + "\"}"; + final HistoricalStartupCacheLoadStrategy obj = + mapper.readValue(json, HistoricalStartupCacheLoadStrategy.class); + Assert.assertTrue(obj instanceof LoadAllEagerlyStrategy); + } + + @Test + public void testDeserializeLoadAllLazily() throws Exception + { + final String json = "{\"type\":\"" + LoadAllLazilyStrategy.STRATEGY_NAME + "\"}"; + final HistoricalStartupCacheLoadStrategy obj = + mapper.readValue(json, HistoricalStartupCacheLoadStrategy.class); + Assert.assertTrue(obj instanceof LoadAllLazilyStrategy); + } + + @Test + public void testDeserializeLoadEagerlyBeforePeriod() throws Exception + { + final String json = "{\"type\":\"" + LoadEagerlyBeforePeriod.STRATEGY_NAME + "\",\"period\":\"P3D\"}"; + final HistoricalStartupCacheLoadStrategy obj = + mapper.readValue(json, HistoricalStartupCacheLoadStrategy.class); + Assert.assertTrue(obj instanceof LoadEagerlyBeforePeriod); + } +} From a0fd32ef60e44922b2be3cc41be83d33862ba5e5 Mon Sep 17 00:00:00 2001 From: GWphua Date: Fri, 24 Oct 2025 18:11:32 +0800 Subject: [PATCH 17/21] Load strategy test --- .../loading/SegmentLocalCacheManager.java | 4 +-- ...oricalStartupCacheLoadStrategyFactory.java | 30 ------------------- .../startup/LoadEagerlyBeforePeriod.java | 2 +- ...istoricalStartupCacheLoadStrategyTest.java | 15 ++++++---- 4 files changed, 11 insertions(+), 40 deletions(-) delete mode 100644 server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactory.java diff --git a/server/src/main/java/org/apache/druid/segment/loading/SegmentLocalCacheManager.java b/server/src/main/java/org/apache/druid/segment/loading/SegmentLocalCacheManager.java index 3efc72c654e1..5ed0176e1397 100644 --- a/server/src/main/java/org/apache/druid/segment/loading/SegmentLocalCacheManager.java +++ b/server/src/main/java/org/apache/druid/segment/loading/SegmentLocalCacheManager.java @@ -43,7 +43,6 @@ import org.apache.druid.segment.Segment; import org.apache.druid.segment.SegmentLazyLoadFailCallback; import org.apache.druid.server.coordination.startup.HistoricalStartupCacheLoadStrategy; -import org.apache.druid.server.coordination.startup.HistoricalStartupCacheLoadStrategyFactory; import org.apache.druid.timeline.DataSegment; import org.apache.druid.timeline.SegmentId; import org.apache.druid.utils.CloseableUtils; @@ -130,8 +129,7 @@ public SegmentLocalCacheManager( this.locations = locations; this.strategy = strategy; this.indexIO = indexIO; - - this.loadStrategy = HistoricalStartupCacheLoadStrategyFactory.factorize(config); + this.loadStrategy = config.getStartupCacheLoadStrategy(); log.info("Using storage location strategy[%s].", this.strategy.getClass().getSimpleName()); diff --git a/server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactory.java b/server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactory.java deleted file mode 100644 index 808db2fd635e..000000000000 --- a/server/src/main/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyFactory.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * 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.druid.server.coordination.startup; - -import org.apache.druid.segment.loading.SegmentLoaderConfig; - -public class HistoricalStartupCacheLoadStrategyFactory -{ - public static HistoricalStartupCacheLoadStrategy factorize(SegmentLoaderConfig config) - { - return config.getStartupCacheLoadStrategy(); - } -} diff --git a/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java index a34b1a707b26..fae2ea8b5533 100644 --- a/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java +++ b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java @@ -49,7 +49,7 @@ public LoadEagerlyBeforePeriod( } @VisibleForTesting - public Interval getEagerLoadingWindow() + public Interval getEagerLoadingInterval() { return this.eagerLoadingInterval; } diff --git a/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyTest.java b/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyTest.java index d3f48f5390bd..0d81cc62a7cd 100644 --- a/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyTest.java +++ b/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyTest.java @@ -21,6 +21,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.druid.segment.TestHelper; +import org.joda.time.Period; import org.junit.Assert; import org.junit.Test; @@ -32,26 +33,28 @@ public class HistoricalStartupCacheLoadStrategyTest public void testDeserializeLoadAllEagerly() throws Exception { final String json = "{\"type\":\"" + LoadAllEagerlyStrategy.STRATEGY_NAME + "\"}"; - final HistoricalStartupCacheLoadStrategy obj = + final HistoricalStartupCacheLoadStrategy historicalStartupLoadingStrategy = mapper.readValue(json, HistoricalStartupCacheLoadStrategy.class); - Assert.assertTrue(obj instanceof LoadAllEagerlyStrategy); + Assert.assertTrue(historicalStartupLoadingStrategy instanceof LoadAllEagerlyStrategy); } @Test public void testDeserializeLoadAllLazily() throws Exception { final String json = "{\"type\":\"" + LoadAllLazilyStrategy.STRATEGY_NAME + "\"}"; - final HistoricalStartupCacheLoadStrategy obj = + final HistoricalStartupCacheLoadStrategy historicalStartupLoadingStrategy = mapper.readValue(json, HistoricalStartupCacheLoadStrategy.class); - Assert.assertTrue(obj instanceof LoadAllLazilyStrategy); + Assert.assertTrue(historicalStartupLoadingStrategy instanceof LoadAllLazilyStrategy); } @Test public void testDeserializeLoadEagerlyBeforePeriod() throws Exception { final String json = "{\"type\":\"" + LoadEagerlyBeforePeriod.STRATEGY_NAME + "\",\"period\":\"P3D\"}"; - final HistoricalStartupCacheLoadStrategy obj = + final HistoricalStartupCacheLoadStrategy historicalStartupLoadingStrategy = mapper.readValue(json, HistoricalStartupCacheLoadStrategy.class); - Assert.assertTrue(obj instanceof LoadEagerlyBeforePeriod); + Assert.assertTrue(historicalStartupLoadingStrategy instanceof LoadEagerlyBeforePeriod); + LoadEagerlyBeforePeriod loadEagerlyBeforePeriodStrategy = (LoadEagerlyBeforePeriod) historicalStartupLoadingStrategy; + Assert.assertEquals(Period.days(3).toStandardDuration(), loadEagerlyBeforePeriodStrategy.getEagerLoadingInterval().toDuration()); } } From 6c641dca3e5bab87ea8ebf97119c324aa7e0a516 Mon Sep 17 00:00:00 2001 From: GWphua Date: Fri, 24 Oct 2025 18:32:22 +0800 Subject: [PATCH 18/21] Add String method to all --- .../druid/segment/loading/SegmentLoaderConfig.java | 10 +--------- .../coordination/startup/LoadAllEagerlyStrategy.java | 8 ++++++++ .../coordination/startup/LoadAllLazilyStrategy.java | 8 ++++++++ .../startup/LoadEagerlyBeforePeriod.java | 8 ++++++++ .../HistoricalStartupCacheLoadStrategyTest.java | 12 ++++++++++-- 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/server/src/main/java/org/apache/druid/segment/loading/SegmentLoaderConfig.java b/server/src/main/java/org/apache/druid/segment/loading/SegmentLoaderConfig.java index 00d770ee6890..bce04cdc760b 100644 --- a/server/src/main/java/org/apache/druid/segment/loading/SegmentLoaderConfig.java +++ b/server/src/main/java/org/apache/druid/segment/loading/SegmentLoaderConfig.java @@ -26,7 +26,6 @@ import org.apache.druid.server.coordination.startup.LoadAllEagerlyStrategy; import org.apache.druid.server.coordination.startup.LoadAllLazilyStrategy; import org.apache.druid.utils.RuntimeInfo; -import org.joda.time.Period; import java.io.File; import java.util.Collections; @@ -55,9 +54,6 @@ public class SegmentLoaderConfig @JsonProperty("startupLoadStrategy") private HistoricalStartupCacheLoadStrategy startupLoadStrategy = null; - @JsonProperty("startupLoadPeriod") - private Period startupLoadPeriod = new Period("P7D"); - @JsonProperty("deleteOnRemove") private boolean deleteOnRemove = true; @@ -116,11 +112,6 @@ public HistoricalStartupCacheLoadStrategy getStartupCacheLoadStrategy() : startupLoadStrategy; } - public Period getStartupLoadPeriod() - { - return startupLoadPeriod; - } - public boolean isDeleteOnRemove() { return deleteOnRemove; @@ -216,6 +207,7 @@ public String toString() return "SegmentLoaderConfig{" + "locations=" + locations + ", lazyLoadOnStart=" + lazyLoadOnStart + + ", startupLoadStrategy=" + startupLoadStrategy + ", deleteOnRemove=" + deleteOnRemove + ", dropSegmentDelayMillis=" + dropSegmentDelayMillis + ", announceIntervalMillis=" + announceIntervalMillis + diff --git a/server/src/main/java/org/apache/druid/server/coordination/startup/LoadAllEagerlyStrategy.java b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadAllEagerlyStrategy.java index 652729f2f4a9..78a6573716bc 100644 --- a/server/src/main/java/org/apache/druid/server/coordination/startup/LoadAllEagerlyStrategy.java +++ b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadAllEagerlyStrategy.java @@ -22,6 +22,8 @@ import org.apache.druid.java.util.common.logger.Logger; import org.apache.druid.timeline.DataSegment; +import java.util.Locale; + public class LoadAllEagerlyStrategy implements HistoricalStartupCacheLoadStrategy { private static final Logger log = new Logger(LoadAllEagerlyStrategy.class); @@ -38,4 +40,10 @@ public boolean shouldLoadLazily(DataSegment segment) { return false; } + + @Override + public String toString() + { + return String.format(Locale.ROOT, "{type=%s}", STRATEGY_NAME); + } } diff --git a/server/src/main/java/org/apache/druid/server/coordination/startup/LoadAllLazilyStrategy.java b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadAllLazilyStrategy.java index 5994f400a1e5..ac861dbfe8b8 100644 --- a/server/src/main/java/org/apache/druid/server/coordination/startup/LoadAllLazilyStrategy.java +++ b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadAllLazilyStrategy.java @@ -22,6 +22,8 @@ import org.apache.druid.java.util.common.logger.Logger; import org.apache.druid.timeline.DataSegment; +import java.util.Locale; + public class LoadAllLazilyStrategy implements HistoricalStartupCacheLoadStrategy { private static final Logger log = new Logger(LoadAllLazilyStrategy.class); @@ -38,4 +40,10 @@ public boolean shouldLoadLazily(DataSegment segment) { return true; } + + @Override + public String toString() + { + return String.format(Locale.ROOT, "{type=%s}", STRATEGY_NAME); + } } diff --git a/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java index fae2ea8b5533..45c526c112c6 100644 --- a/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java +++ b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java @@ -29,6 +29,8 @@ import org.joda.time.Interval; import org.joda.time.Period; +import java.util.Locale; + public class LoadEagerlyBeforePeriod implements HistoricalStartupCacheLoadStrategy { private static final Logger log = new Logger(LoadEagerlyBeforePeriod.class); @@ -59,4 +61,10 @@ public boolean shouldLoadLazily(DataSegment segment) { return !segment.getInterval().overlaps(eagerLoadingInterval); } + + @Override + public String toString() + { + return String.format(Locale.ROOT, "{type=%s,interval=%s}", STRATEGY_NAME, getEagerLoadingInterval()); + } } diff --git a/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyTest.java b/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyTest.java index 0d81cc62a7cd..dc2dd02ebd93 100644 --- a/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyTest.java +++ b/server/src/test/java/org/apache/druid/server/coordination/startup/HistoricalStartupCacheLoadStrategyTest.java @@ -36,6 +36,9 @@ public void testDeserializeLoadAllEagerly() throws Exception final HistoricalStartupCacheLoadStrategy historicalStartupLoadingStrategy = mapper.readValue(json, HistoricalStartupCacheLoadStrategy.class); Assert.assertTrue(historicalStartupLoadingStrategy instanceof LoadAllEagerlyStrategy); + + LoadAllEagerlyStrategy strategy = (LoadAllEagerlyStrategy) historicalStartupLoadingStrategy; + Assert.assertEquals("{type=" + LoadAllEagerlyStrategy.STRATEGY_NAME + "}", strategy.toString()); } @Test @@ -45,6 +48,8 @@ public void testDeserializeLoadAllLazily() throws Exception final HistoricalStartupCacheLoadStrategy historicalStartupLoadingStrategy = mapper.readValue(json, HistoricalStartupCacheLoadStrategy.class); Assert.assertTrue(historicalStartupLoadingStrategy instanceof LoadAllLazilyStrategy); + LoadAllLazilyStrategy strategy = (LoadAllLazilyStrategy) historicalStartupLoadingStrategy; + Assert.assertEquals("{type=" + LoadAllLazilyStrategy.STRATEGY_NAME + "}", strategy.toString()); } @Test @@ -53,8 +58,11 @@ public void testDeserializeLoadEagerlyBeforePeriod() throws Exception final String json = "{\"type\":\"" + LoadEagerlyBeforePeriod.STRATEGY_NAME + "\",\"period\":\"P3D\"}"; final HistoricalStartupCacheLoadStrategy historicalStartupLoadingStrategy = mapper.readValue(json, HistoricalStartupCacheLoadStrategy.class); + Assert.assertTrue(historicalStartupLoadingStrategy instanceof LoadEagerlyBeforePeriod); - LoadEagerlyBeforePeriod loadEagerlyBeforePeriodStrategy = (LoadEagerlyBeforePeriod) historicalStartupLoadingStrategy; - Assert.assertEquals(Period.days(3).toStandardDuration(), loadEagerlyBeforePeriodStrategy.getEagerLoadingInterval().toDuration()); + LoadEagerlyBeforePeriod strategy = (LoadEagerlyBeforePeriod) historicalStartupLoadingStrategy; + + Assert.assertEquals(Period.days(3).toStandardDuration(), strategy.getEagerLoadingInterval().toDuration()); + Assert.assertEquals("{type=" + LoadEagerlyBeforePeriod.STRATEGY_NAME + ",interval=" + strategy.getEagerLoadingInterval() + "}", strategy.toString()); } } From 4978b215abeee0be92e066038d0b81e89800a0e6 Mon Sep 17 00:00:00 2001 From: GWphua Date: Fri, 24 Oct 2025 18:34:58 +0800 Subject: [PATCH 19/21] Documentation update --- docs/configuration/index.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/configuration/index.md b/docs/configuration/index.md index 2261da01ca6e..b3fa878533a4 100644 --- a/docs/configuration/index.md +++ b/docs/configuration/index.md @@ -1586,8 +1586,8 @@ These Historical configurations can be defined in the `historical/runtime.proper |`druid.segmentCache.numLoadingThreads`|How many segments to drop or load concurrently from deep storage. Note that the work of loading segments involves downloading segments from deep storage, decompressing them and loading them to a memory mapped location. So the work is not all I/O Bound. Depending on CPU and network load, one could possibly increase this config to a higher value.|max(1,Number of cores / 6)| |`druid.segmentCache.numBootstrapThreads`|How many segments to load concurrently during historical startup.|`druid.segmentCache.numLoadingThreads`| |`druid.segmentCache.lazyLoadOnStart`|_DEPRECATED_ Use `druid.segmentCache.startupLoadStrategy` instead. Whether or not to load segment columns metadata lazily during historical startup. When set to true, Historical startup time will be dramatically improved by deferring segment loading until the first time that segment takes part in a query, which will incur this cost instead.|false| -|`druid.segmentCache.startupLoadStrategy`|Selects the segment column metadata loading strategy during historical startup. Possible values are `loadAllEagerly`, `loadAllLazily`, and `loadEagerlyBeforePeriod`. More details on each strategy below.|`loadAllEagerly`| -|`druid.segmentCache.startupLoadPeriod`| Used only when startup load strategy `loadEagerlyBeforePeriod` is configured. Suppose timestamp `t` is when the Historical started up. Any segment metadata with interval that falls in the interval of `[t - startupLoadPeriod, t]` will be loaded.|`P7D`| +|`druid.segmentCache.startupLoadStrategy.type`|Selects the segment column metadata loading strategy during historical startup. Possible values are `loadAllEagerly`, `loadAllLazily`, and `loadEagerlyBeforePeriod`. More details on each strategy below.|`loadAllEagerly`| +|`druid.segmentCache.startupLoadStrategy.period`| Used only when startup load strategy `loadEagerlyBeforePeriod` is configured. Suppose timestamp `t` is when the Historical started up. Any segment metadata with interval that falls in the interval of `[t - startupLoadPeriod, t]` will be loaded.|`P7D`| |`druid.segmentCache.numThreadsToLoadSegmentsIntoPageCacheOnDownload`|Number of threads to asynchronously read segment index files into null output stream on each new segment download after the Historical service finishes bootstrapping. Recommended to set to 1 or 2 or leave unspecified to disable. See also `druid.segmentCache.numThreadsToLoadSegmentsIntoPageCacheOnBootstrap`|0| |`druid.segmentCache.numThreadsToLoadSegmentsIntoPageCacheOnBootstrap`|Number of threads to asynchronously read segment index files into null output stream during Historical service bootstrap. This thread pool is terminated after Historical service finishes bootstrapping. Recommended to set to half of available cores. If left unspecified, `druid.segmentCache.numThreadsToLoadSegmentsIntoPageCacheOnDownload` will be used. If both configs are unspecified, this feature is disabled. Preemptively loading segments into page cache helps in the sense that later when a segment is queried, it's already in page cache and only a minor page fault needs to be triggered instead of a more costly major page fault to make the query latency more consistent. Note that loading segment into page cache just does a blind loading of segment index files and will evict any existing segments from page cache at the discretion of operating system when the total segment size on local disk is larger than the page cache usable in the RAM, which roughly equals to total available RAM in the host - druid process memory including both heap and direct memory allocated - memory used by other non druid processes on the host, so it is the user's responsibility to ensure the host has enough RAM to host all the segments to avoid random evictions to fully leverage this feature.|`druid.segmentCache.numThreadsToLoadSegmentsIntoPageCacheOnDownload`| From aa11aa0c05727494d4523c544ee4890bbd223db5 Mon Sep 17 00:00:00 2001 From: GWphua Date: Fri, 24 Oct 2025 18:39:50 +0800 Subject: [PATCH 20/21] Javadocs for all classes added --- .../server/coordination/startup/LoadAllEagerlyStrategy.java | 6 ++++++ .../server/coordination/startup/LoadAllLazilyStrategy.java | 6 ++++++ .../coordination/startup/LoadEagerlyBeforePeriod.java | 6 ++++++ 3 files changed, 18 insertions(+) diff --git a/server/src/main/java/org/apache/druid/server/coordination/startup/LoadAllEagerlyStrategy.java b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadAllEagerlyStrategy.java index 78a6573716bc..c06973eb7a34 100644 --- a/server/src/main/java/org/apache/druid/server/coordination/startup/LoadAllEagerlyStrategy.java +++ b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadAllEagerlyStrategy.java @@ -24,6 +24,12 @@ import java.util.Locale; +/** + * Eagerly loads column metadata for all segments at Historical startup. + *

+ * Optimizes for predictable first-query latency at the cost of longer startup time and higher I/O during bootstrap. + * {@link #shouldLoadLazily(DataSegment)} always returns {@code false}. + */ public class LoadAllEagerlyStrategy implements HistoricalStartupCacheLoadStrategy { private static final Logger log = new Logger(LoadAllEagerlyStrategy.class); diff --git a/server/src/main/java/org/apache/druid/server/coordination/startup/LoadAllLazilyStrategy.java b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadAllLazilyStrategy.java index ac861dbfe8b8..60f4d7af4aa2 100644 --- a/server/src/main/java/org/apache/druid/server/coordination/startup/LoadAllLazilyStrategy.java +++ b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadAllLazilyStrategy.java @@ -24,6 +24,12 @@ import java.util.Locale; +/** + * Defers column metadata loading for all segments until first access. + *

+ * Minimizes Historical startup time and I/O during bootstrap at the expense of increased latency on the first + * query that touches a segment. {@link #shouldLoadLazily(DataSegment)} always returns {@code true}. + */ public class LoadAllLazilyStrategy implements HistoricalStartupCacheLoadStrategy { private static final Logger log = new Logger(LoadAllLazilyStrategy.class); diff --git a/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java index 45c526c112c6..cb30dde5e933 100644 --- a/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java +++ b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java @@ -31,6 +31,12 @@ import java.util.Locale; +/** + * Eagerly loads column metadata for segments whose intervals overlap a recent sliding window; all others load lazily. + *

+ * Balances bootstrap time and first-query performance by eagerly loading only "hot" segments. The window is + * computed as {@code [now - period, now]} at Historical startup. + */ public class LoadEagerlyBeforePeriod implements HistoricalStartupCacheLoadStrategy { private static final Logger log = new Logger(LoadEagerlyBeforePeriod.class); From 7e420d84bcea970de613e280f853943fc4b80b44 Mon Sep 17 00:00:00 2001 From: GWphua Date: Mon, 27 Oct 2025 10:28:00 +0800 Subject: [PATCH 21/21] Set startupLoadPeriod property to null by default --- docs/configuration/index.md | 2 +- .../coordination/startup/LoadEagerlyBeforePeriod.java | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/docs/configuration/index.md b/docs/configuration/index.md index b3fa878533a4..c7cc3bf48d50 100644 --- a/docs/configuration/index.md +++ b/docs/configuration/index.md @@ -1587,7 +1587,7 @@ These Historical configurations can be defined in the `historical/runtime.proper |`druid.segmentCache.numBootstrapThreads`|How many segments to load concurrently during historical startup.|`druid.segmentCache.numLoadingThreads`| |`druid.segmentCache.lazyLoadOnStart`|_DEPRECATED_ Use `druid.segmentCache.startupLoadStrategy` instead. Whether or not to load segment columns metadata lazily during historical startup. When set to true, Historical startup time will be dramatically improved by deferring segment loading until the first time that segment takes part in a query, which will incur this cost instead.|false| |`druid.segmentCache.startupLoadStrategy.type`|Selects the segment column metadata loading strategy during historical startup. Possible values are `loadAllEagerly`, `loadAllLazily`, and `loadEagerlyBeforePeriod`. More details on each strategy below.|`loadAllEagerly`| -|`druid.segmentCache.startupLoadStrategy.period`| Used only when startup load strategy `loadEagerlyBeforePeriod` is configured. Suppose timestamp `t` is when the Historical started up. Any segment metadata with interval that falls in the interval of `[t - startupLoadPeriod, t]` will be loaded.|`P7D`| +|`druid.segmentCache.startupLoadStrategy.period`|This property is used only when the startup load strategy is set to `loadEagerlyBeforePeriod`. Suppose `t` is the timestamp when the Historical started up. Any segment metadata with an interval that falls within the range `[t - startupLoadPeriod, t]` will be loaded. For example, setting this property to `P7D` causes Historical services to eagerly load data segments covering intervals spanning 7 days or less.|`null`| |`druid.segmentCache.numThreadsToLoadSegmentsIntoPageCacheOnDownload`|Number of threads to asynchronously read segment index files into null output stream on each new segment download after the Historical service finishes bootstrapping. Recommended to set to 1 or 2 or leave unspecified to disable. See also `druid.segmentCache.numThreadsToLoadSegmentsIntoPageCacheOnBootstrap`|0| |`druid.segmentCache.numThreadsToLoadSegmentsIntoPageCacheOnBootstrap`|Number of threads to asynchronously read segment index files into null output stream during Historical service bootstrap. This thread pool is terminated after Historical service finishes bootstrapping. Recommended to set to half of available cores. If left unspecified, `druid.segmentCache.numThreadsToLoadSegmentsIntoPageCacheOnDownload` will be used. If both configs are unspecified, this feature is disabled. Preemptively loading segments into page cache helps in the sense that later when a segment is queried, it's already in page cache and only a minor page fault needs to be triggered instead of a more costly major page fault to make the query latency more consistent. Note that loading segment into page cache just does a blind loading of segment index files and will evict any existing segments from page cache at the discretion of operating system when the total segment size on local disk is larger than the page cache usable in the RAM, which roughly equals to total available RAM in the host - druid process memory including both heap and direct memory allocated - memory used by other non druid processes on the host, so it is the user's responsibility to ensure the host has enough RAM to host all the segments to avoid random evictions to fully leverage this feature.|`druid.segmentCache.numThreadsToLoadSegmentsIntoPageCacheOnDownload`| diff --git a/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java index cb30dde5e933..61c1c0fe56f5 100644 --- a/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java +++ b/server/src/main/java/org/apache/druid/server/coordination/startup/LoadEagerlyBeforePeriod.java @@ -22,6 +22,7 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.google.common.annotations.VisibleForTesting; +import org.apache.druid.error.DruidException; import org.apache.druid.java.util.common.DateTimes; import org.apache.druid.java.util.common.logger.Logger; import org.apache.druid.timeline.DataSegment; @@ -50,6 +51,16 @@ public LoadEagerlyBeforePeriod( @JsonProperty("period") Period eagerLoadingPeriod ) { + if (eagerLoadingPeriod == null) { + throw DruidException + .forPersona(DruidException.Persona.OPERATOR) + .ofCategory(DruidException.Category.INVALID_INPUT) + .build( + "druid.segmentCache.startupLoadStrategy.period must be configured for Historical startup strategy[%s].", + STRATEGY_NAME + ); + } + DateTime now = DateTimes.nowUtc(); this.eagerLoadingInterval = new Interval(now.minus(eagerLoadingPeriod), now);