Skip to content

Commit cfafb52

Browse files
committed
Optimize API: Allow to optimize index/indices. Closes #9.
1 parent 7631b13 commit cfafb52

37 files changed

+829
-78
lines changed

modules/benchmark/micro/src/main/java/org/elasticsearch/benchmark/index/engine/SimpleEngineBenchmark.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public SimpleEngineBenchmark build() {
154154
.add(field("content", contentItem)).build();
155155
engine.index(new Engine.Index(new Term("_id", sId), doc, Lucene.STANDARD_ANALYZER, "type", sId, "{ ... }"));
156156
}
157-
engine.refresh(true);
157+
engine.refresh(new Engine.Refresh(true));
158158
stopWatch.stop();
159159
System.out.println("Warmup of [" + contentItems.length + "] content items, took " + stopWatch.totalTime());
160160

@@ -192,7 +192,7 @@ public void run() throws Exception {
192192

193193
scheduledExecutorService.shutdown();
194194

195-
engine.refresh(true);
195+
engine.refresh(new Engine.Refresh(true));
196196
stopWatch = new StopWatch();
197197
stopWatch.start();
198198
Engine.Searcher searcher = engine.searcher();
@@ -212,7 +212,7 @@ private class Flusher implements Runnable {
212212

213213
@Override public void run() {
214214
stopWatch.start("" + ++id);
215-
engine.flush();
215+
engine.flush(new Engine.Flush());
216216
stopWatch.stop();
217217
}
218218
}
@@ -224,7 +224,7 @@ private class Refresher implements Runnable {
224224
@Override public synchronized void run() {
225225
stopWatch.start("" + ++id);
226226
int lastId = idGenerator.get();
227-
engine.refresh(true);
227+
engine.refresh(new Engine.Refresh(true));
228228
lastRefreshedId = lastId;
229229
stopWatch.stop();
230230
}

modules/elasticsearch/src/main/java/org/elasticsearch/action/TransportActionModule.java

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.elasticsearch.action.admin.indices.gateway.snapshot.TransportIndexGatewaySnapshotAction;
3535
import org.elasticsearch.action.admin.indices.gateway.snapshot.TransportShardGatewaySnapshotAction;
3636
import org.elasticsearch.action.admin.indices.mapping.create.TransportCreateMappingAction;
37+
import org.elasticsearch.action.admin.indices.optimize.TransportOptimizeAction;
3738
import org.elasticsearch.action.admin.indices.refresh.TransportRefreshAction;
3839
import org.elasticsearch.action.admin.indices.status.TransportIndicesStatusAction;
3940
import org.elasticsearch.action.count.TransportCountAction;
@@ -74,6 +75,7 @@ public class TransportActionModule extends AbstractModule {
7475

7576
bind(TransportRefreshAction.class).asEagerSingleton();
7677
bind(TransportFlushAction.class).asEagerSingleton();
78+
bind(TransportOptimizeAction.class).asEagerSingleton();
7779

7880
bind(TransportIndexAction.class).asEagerSingleton();
7981
bind(TransportGetAction.class).asEagerSingleton();

modules/elasticsearch/src/main/java/org/elasticsearch/action/TransportActions.java

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public static class Indices {
4545
public static final String DELETE = "indices/deleteIndex";
4646
public static final String FLUSH = "indices/flush";
4747
public static final String REFRESH = "indices/refresh";
48+
public static final String OPTIMIZE = "indices/optimize";
4849
public static final String STATUS = "indices/status";
4950

5051
public static class Gateway {

modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/cluster/ping/broadcast/BroadcastPingRequest.java

-4
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ public class BroadcastPingRequest extends BroadcastOperationRequest {
3434
BroadcastPingRequest() {
3535
}
3636

37-
public BroadcastPingRequest(String index) {
38-
super(new String[]{index}, null);
39-
}
40-
4137
public BroadcastPingRequest(String... indices) {
4238
super(indices, null);
4339
}

modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/cluster/ping/replication/ReplicationPingRequest.java

-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@
2727
*/
2828
public class ReplicationPingRequest extends IndicesReplicationOperationRequest {
2929

30-
public ReplicationPingRequest(String index) {
31-
this(new String[]{index});
32-
}
33-
3430
public ReplicationPingRequest(String... indices) {
3531
this.indices = indices;
3632
}

modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/flush/FlushRequest.java

-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ public class FlushRequest extends BroadcastOperationRequest {
3131

3232
}
3333

34-
public FlushRequest(String index) {
35-
this(new String[]{index});
36-
}
37-
3834
public FlushRequest(String... indices) {
3935
super(indices, null);
4036
// we want to do the refresh in parallel on local shards...

modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/flush/TransportFlushAction.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.elasticsearch.cluster.ClusterState;
2828
import org.elasticsearch.cluster.routing.GroupShardsIterator;
2929
import org.elasticsearch.cluster.routing.ShardRouting;
30+
import org.elasticsearch.index.engine.Engine;
3031
import org.elasticsearch.index.shard.IndexShard;
3132
import org.elasticsearch.indices.IndicesService;
3233
import org.elasticsearch.threadpool.ThreadPool;
@@ -84,7 +85,7 @@ public class TransportFlushAction extends TransportBroadcastOperationAction<Flus
8485

8586
@Override protected ShardFlushResponse shardOperation(ShardFlushRequest request) throws ElasticSearchException {
8687
IndexShard indexShard = indicesService.indexServiceSafe(request.index()).shardSafe(request.shardId());
87-
indexShard.flush();
88+
indexShard.flush(new Engine.Flush());
8889
return new ShardFlushResponse(request.index(), request.shardId());
8990
}
9091

modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/gateway/snapshot/GatewaySnapshotRequest.java

-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@
2727
*/
2828
public class GatewaySnapshotRequest extends IndicesReplicationOperationRequest {
2929

30-
public GatewaySnapshotRequest(String index) {
31-
this(new String[]{index});
32-
}
33-
3430
public GatewaySnapshotRequest(String... indices) {
3531
this.indices = indices;
3632
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Licensed to Elastic Search and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. Elastic Search licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. 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+
20+
package org.elasticsearch.action.admin.indices.optimize;
21+
22+
import org.elasticsearch.action.support.broadcast.BroadcastOperationRequest;
23+
import org.elasticsearch.action.support.broadcast.BroadcastOperationThreading;
24+
25+
import java.io.DataInput;
26+
import java.io.DataOutput;
27+
import java.io.IOException;
28+
29+
/**
30+
* A request to optimize one or more indices. In order to optimize on all the indices, pass an empty array or
31+
* <tt>null</tt> for the indices.
32+
*
33+
* <p>{@link #waitForMerge(boolean)} allows to control if the call will block until the optimize completes and
34+
* defaults to <tt>true</tt>.
35+
*
36+
* <p>{@link #maxNumSegments(int)} allows to control the number of segments to optimize down to. By default, will
37+
* cause the optimize process to optimize down to half the configured number of segments.
38+
*
39+
* @author kimchy (Shay Banon)
40+
*/
41+
public class OptimizeRequest extends BroadcastOperationRequest {
42+
43+
private boolean waitForMerge = true;
44+
45+
private int maxNumSegments = -1;
46+
47+
/**
48+
* Constructs an optimization request over one or more indices.
49+
*
50+
* @param indices The indices to optimize, no indices passed means all indices will be optimized.
51+
*/
52+
public OptimizeRequest(String... indices) {
53+
super(indices, null);
54+
// we want to do the optimize in parallel on local shards...
55+
operationThreading(BroadcastOperationThreading.THREAD_PER_SHARD);
56+
}
57+
58+
OptimizeRequest() {
59+
60+
}
61+
62+
@Override public OptimizeRequest listenerThreaded(boolean threadedListener) {
63+
super.listenerThreaded(threadedListener);
64+
return this;
65+
}
66+
67+
@Override public OptimizeRequest operationThreading(BroadcastOperationThreading operationThreading) {
68+
super.operationThreading(operationThreading);
69+
return this;
70+
}
71+
72+
/**
73+
* Should the call block until the optimize completes. Defaults to <tt>true</tt>.
74+
*/
75+
public boolean waitForMerge() {
76+
return waitForMerge;
77+
}
78+
79+
/**
80+
* Should the call block until the optimize completes. Defaults to <tt>true</tt>.
81+
*/
82+
public OptimizeRequest waitForMerge(boolean waitForMerge) {
83+
this.waitForMerge = waitForMerge;
84+
return this;
85+
}
86+
87+
/**
88+
* Will optimize the index down to <= maxNumSegments. By default, will cause the optimize
89+
* process to optimize down to half the configured number of segments.
90+
*/
91+
public int maxNumSegments() {
92+
return maxNumSegments;
93+
}
94+
95+
/**
96+
* Will optimize the index down to <= maxNumSegments. By default, will cause the optimize
97+
* process to optimize down to half the configured number of segments.
98+
*/
99+
public OptimizeRequest maxNumSegments(int maxNumSegments) {
100+
this.maxNumSegments = maxNumSegments;
101+
return this;
102+
}
103+
104+
public void readFrom(DataInput in) throws IOException, ClassNotFoundException {
105+
super.readFrom(in);
106+
waitForMerge = in.readBoolean();
107+
maxNumSegments = in.readInt();
108+
}
109+
110+
public void writeTo(DataOutput out) throws IOException {
111+
super.writeTo(out);
112+
out.writeBoolean(waitForMerge);
113+
out.writeInt(maxNumSegments);
114+
}
115+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to Elastic Search and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. Elastic Search licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. 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+
20+
package org.elasticsearch.action.admin.indices.optimize;
21+
22+
import org.elasticsearch.action.support.broadcast.BroadcastOperationResponse;
23+
24+
import java.io.DataInput;
25+
import java.io.DataOutput;
26+
import java.io.IOException;
27+
28+
/**
29+
* @author kimchy (Shay Banon)
30+
*/
31+
public class OptimizeResponse extends BroadcastOperationResponse {
32+
33+
OptimizeResponse() {
34+
35+
}
36+
37+
OptimizeResponse(int successfulShards, int failedShards) {
38+
super(successfulShards, failedShards);
39+
}
40+
41+
@Override public void readFrom(DataInput in) throws IOException, ClassNotFoundException {
42+
super.readFrom(in);
43+
}
44+
45+
@Override public void writeTo(DataOutput out) throws IOException {
46+
super.writeTo(out);
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Licensed to Elastic Search and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. Elastic Search licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. 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+
20+
package org.elasticsearch.action.admin.indices.optimize;
21+
22+
import org.elasticsearch.action.support.broadcast.BroadcastShardOperationRequest;
23+
24+
import java.io.DataInput;
25+
import java.io.DataOutput;
26+
import java.io.IOException;
27+
28+
/**
29+
* @author kimchy (Shay Banon)
30+
*/
31+
public class ShardOptimizeRequest extends BroadcastShardOperationRequest {
32+
33+
private boolean waitForMerge = true;
34+
35+
private int maxNumSegments = -1;
36+
37+
ShardOptimizeRequest() {
38+
}
39+
40+
public ShardOptimizeRequest(String index, int shardId, OptimizeRequest request) {
41+
super(index, shardId);
42+
waitForMerge = request.waitForMerge();
43+
maxNumSegments = request.maxNumSegments();
44+
}
45+
46+
boolean waitForMerge() {
47+
return waitForMerge;
48+
}
49+
50+
int maxNumSegments() {
51+
return maxNumSegments;
52+
}
53+
54+
@Override public void readFrom(DataInput in) throws IOException, ClassNotFoundException {
55+
super.readFrom(in);
56+
waitForMerge = in.readBoolean();
57+
maxNumSegments = in.readInt();
58+
}
59+
60+
@Override public void writeTo(DataOutput out) throws IOException {
61+
super.writeTo(out);
62+
out.writeBoolean(waitForMerge);
63+
out.writeInt(maxNumSegments);
64+
}
65+
}

0 commit comments

Comments
 (0)