Skip to content

Commit

Permalink
Do not force refresh when write indexing buffer (#50769)
Browse files Browse the repository at this point in the history
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/
  • Loading branch information
dnhatn committed Jan 11, 2020
1 parent 981e4b8 commit f4aabdc
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1639,9 +1639,7 @@ final boolean refresh(String source, SearcherScope scope, boolean block) throws

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

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch 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.elasticsearch.indices;

import org.apache.logging.log4j.LogManager;
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.IndexService;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.codec.CodecService;
import org.elasticsearch.index.engine.EngineConfig;
import org.elasticsearch.index.engine.EngineFactory;
import org.elasticsearch.index.engine.InternalEngine;
import org.elasticsearch.index.refresh.RefreshStats;
import org.elasticsearch.index.shard.IndexShard;
import org.elasticsearch.plugins.EnginePlugin;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.test.ESSingleNodeTestCase;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
import static org.hamcrest.Matchers.greaterThan;

public class IndexingMemoryControllerIT extends ESSingleNodeTestCase {

@Override
protected Settings nodeSettings() {
return Settings.builder().put(super.nodeSettings())
// small indexing buffer so that we can trigger refresh after buffering 100 deletes
.put("indices.memory.index_buffer_size", "1kb").build();
}

@Override
protected Collection<Class<? extends Plugin>> getPlugins() {
final List<Class<? extends Plugin>> plugins = new ArrayList<>(super.getPlugins());
plugins.add(TestEnginePlugin.class);
return plugins;
}

public static class TestEnginePlugin extends Plugin implements EnginePlugin {

EngineConfig engineConfigWithLargerIndexingMemory(EngineConfig config) {
// We need to set a larger buffer for the IndexWriter; otherwise, it will flush before the IndexingMemoryController.
Settings settings = Settings.builder().put(config.getIndexSettings().getSettings())
.put("indices.memory.index_buffer_size", "10mb").build();
IndexSettings indexSettings = new IndexSettings(config.getIndexSettings().getIndexMetaData(), settings);
return new EngineConfig(config.getShardId(), config.getAllocationId(), config.getThreadPool(),
indexSettings, config.getWarmer(), config.getStore(), config.getMergePolicy(), config.getAnalyzer(),
config.getSimilarity(), new CodecService(null, LogManager.getLogger(IndexingMemoryControllerIT.class)),
config.getEventListener(), config.getQueryCache(),
config.getQueryCachingPolicy(), config.getTranslogConfig(), config.getFlushMergesAfter(),
config.getExternalRefreshListener(), config.getInternalRefreshListener(), config.getIndexSort(),
config.getCircuitBreakerService(), config.getGlobalCheckpointSupplier(), config.retentionLeasesSupplier(),
config.getPrimaryTermSupplier(), config.getTombstoneDocSupplier());
}

@Override
public Optional<EngineFactory> getEngineFactory(IndexSettings indexSettings) {
return Optional.of(config -> new InternalEngine(engineConfigWithLargerIndexingMemory(config)));
}
}

// #10312
public void testDeletesAloneCanTriggerRefresh() throws Exception {
IndexService indexService = createIndex("index", Settings.builder().put("index.number_of_shards", 1)
.put("index.number_of_replicas", 0).put("index.refresh_interval", -1).build());
IndexShard shard = indexService.getShard(0);
for (int i = 0; i < 100; i++) {
client().prepareIndex("index", "_doc").setId(Integer.toString(i)).setSource("field", "value").get();
}
// Force merge so we know all merges are done before we start deleting:
ForceMergeResponse r = client().admin().indices().prepareForceMerge().setMaxNumSegments(1).execute().actionGet();
assertNoFailures(r);
final RefreshStats refreshStats = shard.refreshStats();
for (int i = 0; i < 100; i++) {
client().prepareDelete("index", "_doc", Integer.toString(i)).get();
}
// need to assert busily as IndexingMemoryController refreshes in background
assertBusy(() -> assertThat(shard.refreshStats().getTotal(), greaterThan(refreshStats.getTotal() + 1)));
}
}
Loading

0 comments on commit f4aabdc

Please sign in to comment.