Capture and set start time in Delete By Query operations
This is important for queries/filters that use `now` in date based queries/filters Closes #5540
This commit is contained in:
parent
ab3e22d17c
commit
196e3c3602
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
package org.elasticsearch.action.deletebyquery;
|
package org.elasticsearch.action.deletebyquery;
|
||||||
|
|
||||||
|
import org.elasticsearch.Version;
|
||||||
import org.elasticsearch.action.ActionRequestValidationException;
|
import org.elasticsearch.action.ActionRequestValidationException;
|
||||||
import org.elasticsearch.action.support.replication.IndexReplicationOperationRequest;
|
import org.elasticsearch.action.support.replication.IndexReplicationOperationRequest;
|
||||||
import org.elasticsearch.common.Nullable;
|
import org.elasticsearch.common.Nullable;
|
||||||
|
@ -45,8 +46,11 @@ public class IndexDeleteByQueryRequest extends IndexReplicationOperationRequest<
|
||||||
private Set<String> routing;
|
private Set<String> routing;
|
||||||
@Nullable
|
@Nullable
|
||||||
private String[] filteringAliases;
|
private String[] filteringAliases;
|
||||||
|
private long nowInMillis;
|
||||||
|
|
||||||
IndexDeleteByQueryRequest(DeleteByQueryRequest request, String index, @Nullable Set<String> routing, @Nullable String[] filteringAliases) {
|
IndexDeleteByQueryRequest(DeleteByQueryRequest request, String index, @Nullable Set<String> routing, @Nullable String[] filteringAliases,
|
||||||
|
long nowInMillis
|
||||||
|
) {
|
||||||
this.index = index;
|
this.index = index;
|
||||||
this.timeout = request.timeout();
|
this.timeout = request.timeout();
|
||||||
this.source = request.source();
|
this.source = request.source();
|
||||||
|
@ -55,6 +59,7 @@ public class IndexDeleteByQueryRequest extends IndexReplicationOperationRequest<
|
||||||
this.consistencyLevel = request.consistencyLevel();
|
this.consistencyLevel = request.consistencyLevel();
|
||||||
this.routing = routing;
|
this.routing = routing;
|
||||||
this.filteringAliases = filteringAliases;
|
this.filteringAliases = filteringAliases;
|
||||||
|
this.nowInMillis = nowInMillis;
|
||||||
}
|
}
|
||||||
|
|
||||||
IndexDeleteByQueryRequest() {
|
IndexDeleteByQueryRequest() {
|
||||||
|
@ -85,6 +90,10 @@ public class IndexDeleteByQueryRequest extends IndexReplicationOperationRequest<
|
||||||
return filteringAliases;
|
return filteringAliases;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
long nowInMillis() {
|
||||||
|
return nowInMillis;
|
||||||
|
}
|
||||||
|
|
||||||
public IndexDeleteByQueryRequest timeout(TimeValue timeout) {
|
public IndexDeleteByQueryRequest timeout(TimeValue timeout) {
|
||||||
this.timeout = timeout;
|
this.timeout = timeout;
|
||||||
return this;
|
return this;
|
||||||
|
@ -114,6 +123,11 @@ public class IndexDeleteByQueryRequest extends IndexReplicationOperationRequest<
|
||||||
filteringAliases[i] = in.readString();
|
filteringAliases[i] = in.readString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (in.getVersion().onOrAfter(Version.V_1_2_0)) {
|
||||||
|
nowInMillis = in.readVLong();
|
||||||
|
} else {
|
||||||
|
nowInMillis = System.currentTimeMillis();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeTo(StreamOutput out) throws IOException {
|
public void writeTo(StreamOutput out) throws IOException {
|
||||||
|
@ -139,5 +153,8 @@ public class IndexDeleteByQueryRequest extends IndexReplicationOperationRequest<
|
||||||
} else {
|
} else {
|
||||||
out.writeVInt(0);
|
out.writeVInt(0);
|
||||||
}
|
}
|
||||||
|
if (out.getVersion().onOrAfter(Version.V_1_2_0)) {
|
||||||
|
out.writeVLong(nowInMillis);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
package org.elasticsearch.action.deletebyquery;
|
package org.elasticsearch.action.deletebyquery;
|
||||||
|
|
||||||
|
import org.elasticsearch.Version;
|
||||||
import org.elasticsearch.action.ActionRequestValidationException;
|
import org.elasticsearch.action.ActionRequestValidationException;
|
||||||
import org.elasticsearch.action.support.replication.ShardReplicationOperationRequest;
|
import org.elasticsearch.action.support.replication.ShardReplicationOperationRequest;
|
||||||
import org.elasticsearch.common.Nullable;
|
import org.elasticsearch.common.Nullable;
|
||||||
|
@ -47,6 +48,7 @@ public class ShardDeleteByQueryRequest extends ShardReplicationOperationRequest<
|
||||||
private Set<String> routing;
|
private Set<String> routing;
|
||||||
@Nullable
|
@Nullable
|
||||||
private String[] filteringAliases;
|
private String[] filteringAliases;
|
||||||
|
private long nowInMillis;
|
||||||
|
|
||||||
ShardDeleteByQueryRequest(IndexDeleteByQueryRequest request, int shardId) {
|
ShardDeleteByQueryRequest(IndexDeleteByQueryRequest request, int shardId) {
|
||||||
super(request);
|
super(request);
|
||||||
|
@ -59,6 +61,7 @@ public class ShardDeleteByQueryRequest extends ShardReplicationOperationRequest<
|
||||||
timeout = request.timeout();
|
timeout = request.timeout();
|
||||||
this.routing = request.routing();
|
this.routing = request.routing();
|
||||||
filteringAliases = request.filteringAliases();
|
filteringAliases = request.filteringAliases();
|
||||||
|
nowInMillis = request.nowInMillis();
|
||||||
}
|
}
|
||||||
|
|
||||||
ShardDeleteByQueryRequest() {
|
ShardDeleteByQueryRequest() {
|
||||||
|
@ -93,6 +96,10 @@ public class ShardDeleteByQueryRequest extends ShardReplicationOperationRequest<
|
||||||
return filteringAliases;
|
return filteringAliases;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
long nowInMillis() {
|
||||||
|
return nowInMillis;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void readFrom(StreamInput in) throws IOException {
|
public void readFrom(StreamInput in) throws IOException {
|
||||||
super.readFrom(in);
|
super.readFrom(in);
|
||||||
|
@ -113,6 +120,12 @@ public class ShardDeleteByQueryRequest extends ShardReplicationOperationRequest<
|
||||||
filteringAliases[i] = in.readString();
|
filteringAliases[i] = in.readString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (in.getVersion().onOrAfter(Version.V_1_2_0)) {
|
||||||
|
nowInMillis = in.readVLong();
|
||||||
|
} else {
|
||||||
|
nowInMillis = System.currentTimeMillis();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -137,6 +150,9 @@ public class ShardDeleteByQueryRequest extends ShardReplicationOperationRequest<
|
||||||
} else {
|
} else {
|
||||||
out.writeVInt(0);
|
out.writeVInt(0);
|
||||||
}
|
}
|
||||||
|
if (out.getVersion().onOrAfter(Version.V_1_2_0)) {
|
||||||
|
out.writeVLong(nowInMillis);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -100,8 +100,8 @@ public class TransportDeleteByQueryAction extends TransportIndicesReplicationOpe
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected IndexDeleteByQueryRequest newIndexRequestInstance(DeleteByQueryRequest request, String index, Set<String> routing) {
|
protected IndexDeleteByQueryRequest newIndexRequestInstance(DeleteByQueryRequest request, String index, Set<String> routing, long startTimeInMillis) {
|
||||||
String[] filteringAliases = clusterService.state().metaData().filteringAliases(index, request.indices());
|
String[] filteringAliases = clusterService.state().metaData().filteringAliases(index, request.indices());
|
||||||
return new IndexDeleteByQueryRequest(request, index, routing, filteringAliases);
|
return new IndexDeleteByQueryRequest(request, index, routing, filteringAliases, startTimeInMillis);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -115,7 +115,7 @@ public class TransportShardDeleteByQueryAction extends TransportShardReplication
|
||||||
IndexService indexService = indicesService.indexServiceSafe(shardRequest.request.index());
|
IndexService indexService = indicesService.indexServiceSafe(shardRequest.request.index());
|
||||||
IndexShard indexShard = indexService.shardSafe(shardRequest.shardId);
|
IndexShard indexShard = indexService.shardSafe(shardRequest.shardId);
|
||||||
|
|
||||||
SearchContext.setCurrent(new DefaultSearchContext(0, new ShardSearchRequest().types(request.types()), null,
|
SearchContext.setCurrent(new DefaultSearchContext(0, new ShardSearchRequest().types(request.types()).nowInMillis(request.nowInMillis()), null,
|
||||||
indexShard.acquireSearcher("delete_by_query"), indexService, indexShard, scriptService, cacheRecycler,
|
indexShard.acquireSearcher("delete_by_query"), indexService, indexShard, scriptService, cacheRecycler,
|
||||||
pageCacheRecycler, bigArrays));
|
pageCacheRecycler, bigArrays));
|
||||||
try {
|
try {
|
||||||
|
@ -138,7 +138,7 @@ public class TransportShardDeleteByQueryAction extends TransportShardReplication
|
||||||
IndexService indexService = indicesService.indexServiceSafe(shardRequest.request.index());
|
IndexService indexService = indicesService.indexServiceSafe(shardRequest.request.index());
|
||||||
IndexShard indexShard = indexService.shardSafe(shardRequest.shardId);
|
IndexShard indexShard = indexService.shardSafe(shardRequest.shardId);
|
||||||
|
|
||||||
SearchContext.setCurrent(new DefaultSearchContext(0, new ShardSearchRequest().types(request.types()), null,
|
SearchContext.setCurrent(new DefaultSearchContext(0, new ShardSearchRequest().types(request.types()).nowInMillis(request.nowInMillis()), null,
|
||||||
indexShard.acquireSearcher("delete_by_query", IndexShard.Mode.WRITE), indexService, indexShard, scriptService,
|
indexShard.acquireSearcher("delete_by_query", IndexShard.Mode.WRITE), indexService, indexShard, scriptService,
|
||||||
cacheRecycler, pageCacheRecycler, bigArrays));
|
cacheRecycler, pageCacheRecycler, bigArrays));
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -84,6 +84,7 @@ public abstract class TransportIndicesReplicationOperationAction<Request extends
|
||||||
final AtomicInteger indexCounter = new AtomicInteger();
|
final AtomicInteger indexCounter = new AtomicInteger();
|
||||||
final AtomicInteger completionCounter = new AtomicInteger(concreteIndices.length);
|
final AtomicInteger completionCounter = new AtomicInteger(concreteIndices.length);
|
||||||
final AtomicReferenceArray<Object> indexResponses = new AtomicReferenceArray<Object>(concreteIndices.length);
|
final AtomicReferenceArray<Object> indexResponses = new AtomicReferenceArray<Object>(concreteIndices.length);
|
||||||
|
final long startTimeInMillis = System.currentTimeMillis();
|
||||||
|
|
||||||
Map<String, Set<String>> routingMap = resolveRouting(clusterState, request);
|
Map<String, Set<String>> routingMap = resolveRouting(clusterState, request);
|
||||||
if (concreteIndices == null || concreteIndices.length == 0) {
|
if (concreteIndices == null || concreteIndices.length == 0) {
|
||||||
|
@ -94,7 +95,7 @@ public abstract class TransportIndicesReplicationOperationAction<Request extends
|
||||||
if (routingMap != null) {
|
if (routingMap != null) {
|
||||||
routing = routingMap.get(index);
|
routing = routingMap.get(index);
|
||||||
}
|
}
|
||||||
IndexRequest indexRequest = newIndexRequestInstance(request, index, routing);
|
IndexRequest indexRequest = newIndexRequestInstance(request, index, routing, startTimeInMillis);
|
||||||
// no threading needed, all is done on the index replication one
|
// no threading needed, all is done on the index replication one
|
||||||
indexRequest.listenerThreaded(false);
|
indexRequest.listenerThreaded(false);
|
||||||
indexAction.execute(indexRequest, new ActionListener<IndexResponse>() {
|
indexAction.execute(indexRequest, new ActionListener<IndexResponse>() {
|
||||||
|
@ -127,7 +128,7 @@ public abstract class TransportIndicesReplicationOperationAction<Request extends
|
||||||
|
|
||||||
protected abstract String transportAction();
|
protected abstract String transportAction();
|
||||||
|
|
||||||
protected abstract IndexRequest newIndexRequestInstance(Request request, String index, Set<String> routing);
|
protected abstract IndexRequest newIndexRequestInstance(Request request, String index, Set<String> routing, long startTimeInMillis);
|
||||||
|
|
||||||
protected abstract boolean accumulateExceptions();
|
protected abstract boolean accumulateExceptions();
|
||||||
|
|
||||||
|
|
|
@ -142,4 +142,16 @@ public class DeleteByQueryTests extends ElasticsearchIntegrationTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDateMath() throws Exception {
|
||||||
|
index("test", "type", "1", "d", "2013-01-01");
|
||||||
|
ensureGreen();
|
||||||
|
refresh();
|
||||||
|
assertHitCount(client().prepareCount("test").get(), 1);
|
||||||
|
client().prepareDeleteByQuery("test").setQuery(QueryBuilders.rangeQuery("d").to("now-1h")).get();
|
||||||
|
refresh();
|
||||||
|
assertHitCount(client().prepareCount("test").get(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue