Skip to content

Commit c32cf06

Browse files
authored
Improve default value for refresh_interval (#96867)
Relates ES-6303
1 parent 17930ba commit c32cf06

File tree

4 files changed

+62
-43
lines changed

4 files changed

+62
-43
lines changed

server/src/main/java/org/elasticsearch/index/IndexSettings.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.function.Consumer;
3737
import java.util.function.Function;
3838

39+
import static org.elasticsearch.cluster.routing.allocation.ExistingShardsAllocator.EXISTING_SHARDS_ALLOCATOR_SETTING;
3940
import static org.elasticsearch.index.mapper.MapperService.INDEX_MAPPING_DEPTH_LIMIT_SETTING;
4041
import static org.elasticsearch.index.mapper.MapperService.INDEX_MAPPING_DIMENSION_FIELDS_LIMIT_SETTING;
4142
import static org.elasticsearch.index.mapper.MapperService.INDEX_MAPPING_FIELD_NAME_LENGTH_LIMIT_SETTING;
@@ -128,6 +129,7 @@ public final class IndexSettings {
128129
Property.Dynamic,
129130
Property.IndexScope
130131
);
132+
131133
/**
132134
* Index setting describing the maximum value of from + size on an individual inner hit definition or
133135
* top hits aggregation. The default maximum of 100 is defensive for the reason that the number of inner hit responses
@@ -237,6 +239,7 @@ public final class IndexSettings {
237239
Property.Dynamic,
238240
Property.IndexScope
239241
);
242+
240243
/**
241244
* Index setting describing the maximum size of the rescore window. Defaults to {@link #MAX_RESULT_WINDOW_SETTING}
242245
* because they both do the same thing: control the size of the heap of hits.
@@ -248,20 +251,6 @@ public final class IndexSettings {
248251
Property.Dynamic,
249252
Property.IndexScope
250253
);
251-
public static final TimeValue DEFAULT_REFRESH_INTERVAL = new TimeValue(1, TimeUnit.SECONDS);
252-
public static final Setting<TimeValue> NODE_DEFAULT_REFRESH_INTERVAL_SETTING = Setting.timeSetting(
253-
"node._internal.default_refresh_interval",
254-
DEFAULT_REFRESH_INTERVAL,
255-
new TimeValue(-1, TimeUnit.MILLISECONDS),
256-
Property.NodeScope
257-
);
258-
public static final Setting<TimeValue> INDEX_REFRESH_INTERVAL_SETTING = Setting.timeSetting(
259-
"index.refresh_interval",
260-
NODE_DEFAULT_REFRESH_INTERVAL_SETTING,
261-
new TimeValue(-1, TimeUnit.MILLISECONDS),
262-
Property.Dynamic,
263-
Property.IndexScope
264-
);
265254
/**
266255
* Only intended for stateless.
267256
*/
@@ -271,6 +260,22 @@ public final class IndexSettings {
271260
Property.Final,
272261
Property.IndexScope
273262
);
263+
264+
public static final TimeValue DEFAULT_REFRESH_INTERVAL = new TimeValue(1, TimeUnit.SECONDS);
265+
public static final Setting<TimeValue> NODE_DEFAULT_REFRESH_INTERVAL_SETTING = Setting.timeSetting(
266+
"node._internal.default_refresh_interval",
267+
DEFAULT_REFRESH_INTERVAL,
268+
TimeValue.MINUS_ONE,
269+
Property.NodeScope
270+
); // TODO: remove setting
271+
public static TimeValue STATELESS_DEFAULT_REFRESH_INTERVAL = TimeValue.timeValueSeconds(5); // TODO: settle on right value
272+
public static final Setting<TimeValue> INDEX_REFRESH_INTERVAL_SETTING = Setting.timeSetting("index.refresh_interval", (settings) -> {
273+
if (EXISTING_SHARDS_ALLOCATOR_SETTING.get(settings).equals("stateless") && INDEX_FAST_REFRESH_SETTING.get(settings) == false) {
274+
return STATELESS_DEFAULT_REFRESH_INTERVAL;
275+
}
276+
return DEFAULT_REFRESH_INTERVAL;
277+
}, TimeValue.MINUS_ONE, Property.Dynamic, Property.IndexScope);
278+
274279
public static final Setting<ByteSizeValue> INDEX_TRANSLOG_FLUSH_THRESHOLD_SIZE_SETTING = Setting.byteSizeSetting(
275280
"index.translog.flush_threshold_size",
276281
/*
@@ -315,6 +320,7 @@ public final class IndexSettings {
315320
Property.Dynamic,
316321
Property.IndexScope
317322
);
323+
318324
/**
319325
* The maximum size of a translog generation. This is independent of the maximum size of
320326
* translog operations that have not been flushed.

server/src/main/java/org/elasticsearch/index/engine/Engine.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,13 @@ public enum SearcherScope {
755755
*/
756756
public abstract boolean isTranslogSyncNeeded();
757757

758+
/**
759+
* Whether search idleness may be allowed to be considered for skipping a scheduled refresh.
760+
*/
761+
public boolean allowSearchIdleOptimization() {
762+
return true;
763+
}
764+
758765
/**
759766
* Ensures that the location has been written to the underlying storage.
760767
*/

server/src/main/java/org/elasticsearch/index/shard/IndexShard.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3749,24 +3749,24 @@ ReplicationTracker getReplicationTracker() {
37493749
public void scheduledRefresh(ActionListener<Boolean> listener) {
37503750
verifyNotClosed();
37513751
boolean listenerNeedsRefresh = refreshListeners.refreshNeeded();
3752-
if (isReadAllowed() && (listenerNeedsRefresh || getEngine().refreshNeeded())) {
3752+
final Engine engine = getEngine();
3753+
if (isReadAllowed() && (listenerNeedsRefresh || engine.refreshNeeded())) {
37533754
if (listenerNeedsRefresh == false // if we have a listener that is waiting for a refresh we need to force it
3755+
&& engine.allowSearchIdleOptimization()
37543756
&& isSearchIdle()
37553757
&& indexSettings.isExplicitRefresh() == false
37563758
&& active.get()) { // it must be active otherwise we might not free up segment memory once the shard became inactive
37573759
// lets skip this refresh since we are search idle and
37583760
// don't necessarily need to refresh. the next searcher access will register a refreshListener and that will
37593761
// cause the next schedule to refresh.
3760-
final Engine engine = getEngine();
37613762
engine.maybePruneDeletes(); // try to prune the deletes in the engine if we accumulated some
37623763
setRefreshPending(engine);
37633764
ActionListener.completeWith(listener, () -> false);
37643765
} else {
37653766
logger.trace("refresh with source [schedule]");
3766-
getEngine().maybeRefresh("schedule", listener.map(Engine.RefreshResult::refreshed));
3767+
engine.maybeRefresh("schedule", listener.map(Engine.RefreshResult::refreshed));
37673768
}
37683769
}
3769-
final Engine engine = getEngine();
37703770
engine.maybePruneDeletes(); // try to prune the deletes in the engine if we accumulated some
37713771
ActionListener.completeWith(listener, () -> false);
37723772
}

server/src/test/java/org/elasticsearch/index/IndexSettingsTests.java

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@
3131
import java.util.concurrent.atomic.AtomicInteger;
3232
import java.util.function.Function;
3333

34-
import static org.elasticsearch.index.IndexSettings.NODE_DEFAULT_REFRESH_INTERVAL_SETTING;
34+
import static org.elasticsearch.cluster.node.DiscoveryNode.STATELESS_ENABLED_SETTING_NAME;
35+
import static org.elasticsearch.cluster.routing.allocation.ExistingShardsAllocator.EXISTING_SHARDS_ALLOCATOR_SETTING;
36+
import static org.elasticsearch.index.IndexSettings.DEFAULT_REFRESH_INTERVAL;
37+
import static org.elasticsearch.index.IndexSettings.INDEX_FAST_REFRESH_SETTING;
38+
import static org.elasticsearch.index.IndexSettings.STATELESS_DEFAULT_REFRESH_INTERVAL;
3539
import static org.elasticsearch.index.IndexSettings.TIME_SERIES_END_TIME;
3640
import static org.elasticsearch.index.IndexSettings.TIME_SERIES_START_TIME;
3741
import static org.hamcrest.CoreMatchers.equalTo;
@@ -318,36 +322,38 @@ public void testRefreshInterval() {
318322
);
319323
}
320324

321-
public void testNodeDefaultRefreshInterval() {
322-
String defaultRefreshInterval = getRandomTimeString();
325+
public void testDefaultRefreshInterval() {
323326
IndexMetadata metadata = newIndexMeta(
324327
"index",
325328
Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build()
326329
);
327-
IndexSettings settings = new IndexSettings(
328-
metadata,
329-
Settings.builder().put(NODE_DEFAULT_REFRESH_INTERVAL_SETTING.getKey(), defaultRefreshInterval).build()
330-
);
331-
assertEquals(
332-
TimeValue.parseTimeValue(
333-
defaultRefreshInterval,
334-
new TimeValue(1, TimeUnit.DAYS),
335-
IndexSettings.INDEX_REFRESH_INTERVAL_SETTING.getKey()
336-
),
337-
settings.getRefreshInterval()
338-
);
339-
String newRefreshInterval = getRandomTimeString();
340-
settings.updateIndexMetadata(
341-
newIndexMeta("index", Settings.builder().put(IndexSettings.INDEX_REFRESH_INTERVAL_SETTING.getKey(), newRefreshInterval).build())
330+
IndexSettings settings = new IndexSettings(metadata, Settings.EMPTY);
331+
assertEquals(DEFAULT_REFRESH_INTERVAL, settings.getRefreshInterval());
332+
}
333+
334+
public void testStatelessDefaultRefreshInterval() {
335+
IndexMetadata metadata = newIndexMeta(
336+
"index",
337+
Settings.builder()
338+
.put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)
339+
.put(EXISTING_SHARDS_ALLOCATOR_SETTING.getKey(), "stateless")
340+
.build()
342341
);
343-
assertEquals(
344-
TimeValue.parseTimeValue(
345-
newRefreshInterval,
346-
new TimeValue(1, TimeUnit.DAYS),
347-
IndexSettings.INDEX_REFRESH_INTERVAL_SETTING.getKey()
348-
),
349-
settings.getRefreshInterval()
342+
IndexSettings settings = new IndexSettings(metadata, Settings.EMPTY);
343+
assertEquals(STATELESS_DEFAULT_REFRESH_INTERVAL, settings.getRefreshInterval());
344+
}
345+
346+
public void testStatelessFastRefreshDefaultRefreshInterval() {
347+
IndexMetadata metadata = newIndexMeta(
348+
"index",
349+
Settings.builder()
350+
.put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)
351+
.put(EXISTING_SHARDS_ALLOCATOR_SETTING.getKey(), "stateless")
352+
.put(INDEX_FAST_REFRESH_SETTING.getKey(), true)
353+
.build()
350354
);
355+
IndexSettings settings = new IndexSettings(metadata, Settings.builder().put(STATELESS_ENABLED_SETTING_NAME, true).build());
356+
assertEquals(DEFAULT_REFRESH_INTERVAL, settings.getRefreshInterval());
351357
}
352358

353359
private String getRandomTimeString() {

0 commit comments

Comments
 (0)