Optimize API: Add onlyExpungeDeletes, flush and refresh parameters. Closes #15.

This commit is contained in:
kimchy 2010-02-15 23:55:11 +02:00
parent 9633108ad6
commit 66b86a7a03
6 changed files with 151 additions and 8 deletions

View File

@ -44,6 +44,12 @@ public class OptimizeRequest extends BroadcastOperationRequest {
private int maxNumSegments = -1;
private boolean onlyExpungeDeletes = false;
private boolean flush = false;
private boolean refresh = false;
/**
* Constructs an optimization request over one or more indices.
*
@ -101,15 +107,68 @@ public class OptimizeRequest extends BroadcastOperationRequest {
return this;
}
/**
* Should the optimization only expunge deletes from the index, without full optimization.
* Defaults to full optimization (<tt>false</tt>).
*/
public boolean onlyExpungeDeletes() {
return onlyExpungeDeletes;
}
/**
* Should the optimization only expunge deletes from the index, without full optimization.
* Defaults to full optimization (<tt>false</tt>).
*/
public OptimizeRequest onlyExpungeDeletes(boolean onlyExpungeDeletes) {
this.onlyExpungeDeletes = onlyExpungeDeletes;
return this;
}
/**
* Should flush be performed after the optimization. Defaults to <tt>false</tt>.
*/
public boolean flush() {
return flush;
}
/**
* Should flush be performed after the optimization. Defaults to <tt>false</tt>.
*/
public OptimizeRequest flush(boolean flush) {
this.flush = flush;
return this;
}
/**
* Should refresh be performed after the optimization. Defaults to <tt>false</tt>.
*/
public boolean refresh() {
return refresh;
}
/**
* Should refresh be performed after the optimization. Defaults to <tt>false</tt>.
*/
public OptimizeRequest refresh(boolean refresh) {
this.refresh = refresh;
return this;
}
public void readFrom(DataInput in) throws IOException, ClassNotFoundException {
super.readFrom(in);
waitForMerge = in.readBoolean();
maxNumSegments = in.readInt();
onlyExpungeDeletes = in.readBoolean();
flush = in.readBoolean();
refresh = in.readBoolean();
}
public void writeTo(DataOutput out) throws IOException {
super.writeTo(out);
out.writeBoolean(waitForMerge);
out.writeInt(maxNumSegments);
out.writeBoolean(onlyExpungeDeletes);
out.writeBoolean(flush);
out.writeBoolean(refresh);
}
}

View File

@ -34,6 +34,12 @@ public class ShardOptimizeRequest extends BroadcastShardOperationRequest {
private int maxNumSegments = -1;
private boolean onlyExpungeDeletes = false;
private boolean flush = false;
private boolean refresh = false;
ShardOptimizeRequest() {
}
@ -41,6 +47,9 @@ public class ShardOptimizeRequest extends BroadcastShardOperationRequest {
super(index, shardId);
waitForMerge = request.waitForMerge();
maxNumSegments = request.maxNumSegments();
onlyExpungeDeletes = request.onlyExpungeDeletes();
flush = request.flush();
refresh = request.refresh();
}
boolean waitForMerge() {
@ -51,15 +60,33 @@ public class ShardOptimizeRequest extends BroadcastShardOperationRequest {
return maxNumSegments;
}
public boolean onlyExpungeDeletes() {
return onlyExpungeDeletes;
}
public boolean flush() {
return flush;
}
public boolean refresh() {
return refresh;
}
@Override public void readFrom(DataInput in) throws IOException, ClassNotFoundException {
super.readFrom(in);
waitForMerge = in.readBoolean();
maxNumSegments = in.readInt();
onlyExpungeDeletes = in.readBoolean();
flush = in.readBoolean();
refresh = in.readBoolean();
}
@Override public void writeTo(DataOutput out) throws IOException {
super.writeTo(out);
out.writeBoolean(waitForMerge);
out.writeInt(maxNumSegments);
out.writeBoolean(onlyExpungeDeletes);
out.writeBoolean(flush);
out.writeBoolean(refresh);
}
}

View File

@ -86,7 +86,13 @@ public class TransportOptimizeAction extends TransportBroadcastOperationAction<O
@Override protected ShardOptimizeResponse shardOperation(ShardOptimizeRequest request) throws ElasticSearchException {
IndexShard indexShard = indicesService.indexServiceSafe(request.index()).shardSafe(request.shardId());
indexShard.optimize(new Engine.Optimize(request.waitForMerge(), request.maxNumSegments()));
indexShard.optimize(new Engine.Optimize()
.waitForMerge(request.waitForMerge())
.maxNumSegments(request.maxNumSegments())
.onlyExpungeDeletes(request.onlyExpungeDeletes())
.flush(request.flush())
.refresh(request.refresh())
);
return new ShardOptimizeResponse(request.index(), request.shardId());
}

View File

@ -51,6 +51,9 @@ public class HttpOptimizeAction extends BaseHttpServerHandler {
try {
optimizeRequest.waitForMerge(HttpActions.paramAsBoolean(request.param("waitForMerge"), true));
optimizeRequest.maxNumSegments(HttpActions.paramAsInt(request.param("maxNumSegments"), -1));
optimizeRequest.onlyExpungeDeletes(HttpActions.paramAsBoolean(request.param("onlyExpungeDeletes"), false));
optimizeRequest.flush(HttpActions.paramAsBoolean(request.param("flush"), false));
optimizeRequest.refresh(HttpActions.paramAsBoolean(request.param("refresh"), false));
// we just send back a response, no need to fork a listener
optimizeRequest.listenerThreaded(false);

View File

@ -162,24 +162,62 @@ public interface Engine extends IndexShardComponent {
}
static class Optimize {
private final boolean waitForMerge;
private final int maxNumSegments;
private boolean waitForMerge = true;
private int maxNumSegments = -1;
private boolean onlyExpungeDeletes = false;
private boolean flush = false;
private boolean refresh = false;
public Optimize(boolean waitForMerge, int maxNumSegments) {
this.waitForMerge = waitForMerge;
this.maxNumSegments = maxNumSegments;
public Optimize() {
}
public boolean waitForMerge() {
return waitForMerge;
}
public Optimize waitForMerge(boolean waitForMerge) {
this.waitForMerge = waitForMerge;
return this;
}
public int maxNumSegments() {
return maxNumSegments;
}
public Optimize maxNumSegments(int maxNumSegments) {
this.maxNumSegments = maxNumSegments;
return this;
}
public boolean onlyExpungeDeletes() {
return onlyExpungeDeletes;
}
public Optimize onlyExpungeDeletes(boolean onlyExpungeDeletes) {
this.onlyExpungeDeletes = onlyExpungeDeletes;
return this;
}
public boolean flush() {
return flush;
}
public Optimize flush(boolean flush) {
this.flush = flush;
return this;
}
public boolean refresh() {
return refresh;
}
public Optimize refresh(boolean refresh) {
this.refresh = refresh;
return this;
}
@Override public String toString() {
return "waitForMerge[" + waitForMerge + "], maxNumSegments[" + maxNumSegments + "]";
return "waitForMerge[" + waitForMerge + "], maxNumSegments[" + maxNumSegments + "], onlyExpungeDeletes[" + onlyExpungeDeletes + "], flush[" + flush + "], refresh[" + refresh + "]";
}
}

View File

@ -309,7 +309,11 @@ public class RobinEngine extends AbstractIndexShardComponent implements Engine,
}
}
}
indexWriter.optimize(maxNumberOfSegments, optimize.waitForMerge());
if (optimize.onlyExpungeDeletes()) {
indexWriter.expungeDeletes(optimize.waitForMerge());
} else {
indexWriter.optimize(maxNumberOfSegments, optimize.waitForMerge());
}
} catch (Exception e) {
throw new OptimizeFailedEngineException(shardId, e);
} finally {
@ -317,6 +321,12 @@ public class RobinEngine extends AbstractIndexShardComponent implements Engine,
optimizeMutex.set(false);
}
}
if (optimize.flush()) {
flush(new Flush());
}
if (optimize.refresh()) {
refresh(new Refresh(false));
}
}
@Override public void snapshot(SnapshotHandler snapshotHandler) throws EngineException {