/*
* 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.index.engine;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse;
import org.elasticsearch.action.admin.indices.segments.IndexSegments;
import org.elasticsearch.action.admin.indices.segments.IndexShardSegments;
import org.elasticsearch.action.admin.indices.segments.IndicesSegmentResponse;
import org.elasticsearch.action.admin.indices.segments.ShardSegments;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.hamcrest.ElasticsearchAssertions;
import org.hamcrest.Matchers;
import org.junit.Test;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
public class InternalEngineIT extends ESIntegTestCase {
@Test
public void testSetBogusMaxMergeThreads() throws InterruptedException {
client().admin().indices().prepareCreate("test").get();
ensureGreen();
client().admin().indices().prepareUpdateSettings("test")
.setSettings(Collections.singletonMap("index.merge.scheduler.max_thread_count",
(Object) Integer.valueOf(Integer.MAX_VALUE))).get();
client().prepareIndex("test", "foo").setSource("field", "foo").get();
final CountDownLatch latch = new CountDownLatch(1);
client().admin().indices().prepareForceMerge("test").execute(new ActionListener<ForceMergeResponse>() {
@Override
public void onResponse(ForceMergeResponse forceMergeResponse) {
latch.countDown();
}
@Override
public void onFailure(Throwable e) {
latch.countDown();
throw new AssertionError(e);
}
});
int numDocs = randomIntBetween(10, 100);
for (int i = 0; i < numDocs; i++) {
client().prepareIndex("test", "foo").setSource("field", "foo").get();
}
latch.await();
client().admin().indices().prepareRefresh("test").get();
ElasticsearchAssertions.assertHitCount(client().prepareSearch("test").get(), numDocs + 1);
}
@Test
public void testSetIndexCompoundOnFlush() {
client().admin().indices().prepareCreate("test").setSettings(Settings.builder().put("number_of_replicas", 0).put("number_of_shards", 1)).get();
ensureGreen();
client().prepareIndex("test", "foo").setSource("field", "foo").get();
refresh();
assertTotalCompoundSegments(1, 1, "test");
client().admin().indices().prepareUpdateSettings("test")
.setSettings(Settings.builder().put(EngineConfig.INDEX_COMPOUND_ON_FLUSH, false)).get();
client().prepareIndex("test", "foo").setSource("field", "foo").get();
refresh();
assertTotalCompoundSegments(1, 2, "test");
client().admin().indices().prepareUpdateSettings("test")
.setSettings(Settings.builder().put(EngineConfig.INDEX_COMPOUND_ON_FLUSH, true)).get();
client().prepareIndex("test", "foo").setSource("field", "foo").get();
refresh();
assertTotalCompoundSegments(2, 3, "test");
}
private void assertTotalCompoundSegments(int i, int t, String index) {
IndicesSegmentResponse indicesSegmentResponse = client().admin().indices().prepareSegments(index).get();
assertNotNull("indices segments response should contain indices", indicesSegmentResponse.getIndices());
IndexSegments indexSegments = indicesSegmentResponse.getIndices().get(index);
assertNotNull(indexSegments);
assertNotNull(indexSegments.getShards());
Collection<IndexShardSegments> values = indexSegments.getShards().values();
int compounds = 0;
int total = 0;
for (IndexShardSegments indexShardSegments : values) {
for (ShardSegments s : indexShardSegments) {
for (Segment segment : s) {
if (segment.isSearch() && segment.getNumDocs() > 0) {
if (segment.isCompound()) {
compounds++;
}
total++;
}
}
}
}
assertThat(compounds, Matchers.equalTo(i));
assertThat(total, Matchers.equalTo(t));
}
private Set<Segment> segments(IndexSegments segments) {
Set<Segment> segmentSet = new HashSet<>();
for (IndexShardSegments s : segments) {
for (ShardSegments shardSegments : s) {
segmentSet.addAll(shardSegments.getSegments());
}
}
return segmentSet;
}
}