|
| 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