Skip to content

Commit

Permalink
[ENGINE] Force optimize was not passed to shard request
Browse files Browse the repository at this point in the history
The force flag to trigger optimiz calls of a single segment for upgrading
etc. was never passed on to the shard request.

Closes #7404
  • Loading branch information
s1monw committed Aug 22, 2014
1 parent e78694a commit fdf1998
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class ShardOptimizeRequest extends BroadcastShardOperationRequest {
maxNumSegments = request.maxNumSegments();
onlyExpungeDeletes = request.onlyExpungeDeletes();
flush = request.flush();
force = request.force();
}

boolean waitForMerge() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.elasticsearch.action.admin.indices.segments.IndicesSegmentResponse;
import org.elasticsearch.action.admin.indices.segments.ShardSegments;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.util.BloomFilter;
import org.elasticsearch.index.codec.CodecService;
Expand All @@ -35,6 +36,9 @@
import org.junit.Test;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutionException;

public class InternalEngineIntegrationTest extends ElasticsearchIntegrationTest {

Expand Down Expand Up @@ -130,6 +134,33 @@ public void testSetIndexCompoundOnFlush() {
assertTotalCompoundSegments(2, 3, "test");
}

public void testForceOptimize() throws ExecutionException, InterruptedException {
client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.builder().put("number_of_replicas", 0).put("number_of_shards", 1)).get();
final int numDocs = randomIntBetween(10, 100);
IndexRequestBuilder[] builders = new IndexRequestBuilder[numDocs];
for (int i = 0; i < builders.length; i++) {
builders[i] = client().prepareIndex("test", "type").setSource("field", "value");
}
indexRandom(true, builders);
ensureGreen();
flushAndRefresh();
client().admin().indices().prepareOptimize("test").setMaxNumSegments(1).setWaitForMerge(true).get();
IndexSegments firstSegments = client().admin().indices().prepareSegments("test").get().getIndices().get("test");
client().admin().indices().prepareOptimize("test").setMaxNumSegments(1).setWaitForMerge(true).get();
IndexSegments secondsSegments = client().admin().indices().prepareSegments("test").get().getIndices().get("test");

assertThat(segments(firstSegments), Matchers.containsInAnyOrder(segments(secondsSegments).toArray()));
assertThat(segments(firstSegments).size(), Matchers.equalTo(1));
assertThat(segments(secondsSegments), Matchers.containsInAnyOrder(segments(firstSegments).toArray()));
assertThat(segments(secondsSegments).size(), Matchers.equalTo(1));
client().admin().indices().prepareOptimize("test").setMaxNumSegments(1).setWaitForMerge(true).setForce(true).get();
IndexSegments thirdSegments = client().admin().indices().prepareSegments("test").get().getIndices().get("test");
assertThat(segments(firstSegments).size(), Matchers.equalTo(1));
assertThat(segments(thirdSegments).size(), Matchers.equalTo(1));
assertThat(segments(firstSegments), Matchers.not(Matchers.containsInAnyOrder(segments(thirdSegments).toArray())));
assertThat(segments(thirdSegments), Matchers.not(Matchers.containsInAnyOrder(segments(firstSegments).toArray())));
}

private void assertTotalCompoundSegments(int i, int t, String index) {
IndicesSegmentResponse indicesSegmentResponse = client().admin().indices().prepareSegments(index).get();
IndexSegments indexSegments = indicesSegmentResponse.getIndices().get(index);
Expand All @@ -150,7 +181,15 @@ private void assertTotalCompoundSegments(int i, int t, String index) {
}
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;
}
}

0 comments on commit fdf1998

Please sign in to comment.