Skip to content

Commit 0510af8

Browse files
authored
Do not force refresh when write indexing buffer (#50769)
Today we periodically check the indexing buffer memory every 5 seconds or after we have used 1/30 of the configured memory. If the total used memory is over the threshold, then we refresh the "largest" shards. If refreshing takes longer these intervals (i.e., 5s or 1/30 buffer), then we continue to enqueue refreshes to these shards. This leads to two issues: - The refresh thread pool can be exhausted and other shards can't refresh - Execute too many refreshes for the "largest" shards With this change, we only refresh the largest shards if they are not refreshing. Here we rely on the periodic check to trigger another refresh if needed. We can harden this by making the ongoing refresh triggers the memory check when it's completed. I opted out this option in this PR for simplicity. See: https://discuss.elastic.co/t/write-queue-continue-to-rise/213652/
1 parent 5d289e3 commit 0510af8

File tree

3 files changed

+213
-124
lines changed

3 files changed

+213
-124
lines changed

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -1612,9 +1612,7 @@ final boolean refresh(String source, SearcherScope scope, boolean block) throws
16121612

16131613
@Override
16141614
public void writeIndexingBuffer() throws EngineException {
1615-
// we obtain a read lock here, since we don't want a flush to happen while we are writing
1616-
// since it flushes the index as well (though, in terms of concurrency, we are allowed to do it)
1617-
refresh("write indexing buffer", SearcherScope.INTERNAL, true);
1615+
refresh("write indexing buffer", SearcherScope.INTERNAL, false);
16181616
}
16191617

16201618
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.indices;
20+
21+
import org.apache.logging.log4j.LogManager;
22+
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse;
23+
import org.elasticsearch.common.settings.Settings;
24+
import org.elasticsearch.index.IndexService;
25+
import org.elasticsearch.index.IndexSettings;
26+
import org.elasticsearch.index.codec.CodecService;
27+
import org.elasticsearch.index.engine.EngineConfig;
28+
import org.elasticsearch.index.engine.EngineFactory;
29+
import org.elasticsearch.index.engine.InternalEngine;
30+
import org.elasticsearch.index.refresh.RefreshStats;
31+
import org.elasticsearch.index.shard.IndexShard;
32+
import org.elasticsearch.plugins.EnginePlugin;
33+
import org.elasticsearch.plugins.Plugin;
34+
import org.elasticsearch.test.ESSingleNodeTestCase;
35+
36+
import java.util.ArrayList;
37+
import java.util.Collection;
38+
import java.util.List;
39+
import java.util.Optional;
40+
41+
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
42+
import static org.hamcrest.Matchers.greaterThan;
43+
44+
public class IndexingMemoryControllerIT extends ESSingleNodeTestCase {
45+
46+
@Override
47+
protected Settings nodeSettings() {
48+
return Settings.builder().put(super.nodeSettings())
49+
// small indexing buffer so that we can trigger refresh after buffering 100 deletes
50+
.put("indices.memory.index_buffer_size", "1kb").build();
51+
}
52+
53+
@Override
54+
protected Collection<Class<? extends Plugin>> getPlugins() {
55+
final List<Class<? extends Plugin>> plugins = new ArrayList<>(super.getPlugins());
56+
plugins.add(TestEnginePlugin.class);
57+
return plugins;
58+
}
59+
60+
public static class TestEnginePlugin extends Plugin implements EnginePlugin {
61+
62+
EngineConfig engineConfigWithLargerIndexingMemory(EngineConfig config) {
63+
// We need to set a larger buffer for the IndexWriter; otherwise, it will flush before the IndexingMemoryController.
64+
Settings settings = Settings.builder().put(config.getIndexSettings().getSettings())
65+
.put("indices.memory.index_buffer_size", "10mb").build();
66+
IndexSettings indexSettings = new IndexSettings(config.getIndexSettings().getIndexMetaData(), settings);
67+
return new EngineConfig(config.getShardId(), config.getAllocationId(), config.getThreadPool(),
68+
indexSettings, config.getWarmer(), config.getStore(), config.getMergePolicy(), config.getAnalyzer(),
69+
config.getSimilarity(), new CodecService(null, LogManager.getLogger(IndexingMemoryControllerIT.class)),
70+
config.getEventListener(), config.getQueryCache(),
71+
config.getQueryCachingPolicy(), config.getTranslogConfig(), config.getFlushMergesAfter(),
72+
config.getExternalRefreshListener(), config.getInternalRefreshListener(), config.getIndexSort(),
73+
config.getCircuitBreakerService(), config.getGlobalCheckpointSupplier(), config.retentionLeasesSupplier(),
74+
config.getPrimaryTermSupplier(), config.getTombstoneDocSupplier());
75+
}
76+
77+
@Override
78+
public Optional<EngineFactory> getEngineFactory(IndexSettings indexSettings) {
79+
return Optional.of(config -> new InternalEngine(engineConfigWithLargerIndexingMemory(config)));
80+
}
81+
}
82+
83+
// #10312
84+
public void testDeletesAloneCanTriggerRefresh() throws Exception {
85+
IndexService indexService = createIndex("index", Settings.builder().put("index.number_of_shards", 1)
86+
.put("index.number_of_replicas", 0).put("index.refresh_interval", -1).build());
87+
IndexShard shard = indexService.getShard(0);
88+
for (int i = 0; i < 100; i++) {
89+
client().prepareIndex("index").setId(Integer.toString(i)).setSource("field", "value").get();
90+
}
91+
// Force merge so we know all merges are done before we start deleting:
92+
ForceMergeResponse r = client().admin().indices().prepareForceMerge().setMaxNumSegments(1).execute().actionGet();
93+
assertNoFailures(r);
94+
final RefreshStats refreshStats = shard.refreshStats();
95+
for (int i = 0; i < 100; i++) {
96+
client().prepareDelete("index", Integer.toString(i)).get();
97+
}
98+
// need to assert busily as IndexingMemoryController refreshes in background
99+
assertBusy(() -> assertThat(shard.refreshStats().getTotal(), greaterThan(refreshStats.getTotal() + 1)));
100+
}
101+
}

0 commit comments

Comments
 (0)