From 2c939847b47629d00e22a48d2cc8739ff536121c Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Tue, 27 Aug 2013 10:45:09 +0200 Subject: [PATCH] Simplified percolate reduce logic and the percolator recovery test --- .../percolate/PercolateShardResponse.java | 11 +----- .../percolate/TransportPercolateAction.java | 20 ++++------ .../percolator/RecoveryPercolatorTests.java | 37 +++++++++++-------- 3 files changed, 30 insertions(+), 38 deletions(-) diff --git a/src/main/java/org/elasticsearch/action/percolate/PercolateShardResponse.java b/src/main/java/org/elasticsearch/action/percolate/PercolateShardResponse.java index 1bacebacaef..920724f4a00 100644 --- a/src/main/java/org/elasticsearch/action/percolate/PercolateShardResponse.java +++ b/src/main/java/org/elasticsearch/action/percolate/PercolateShardResponse.java @@ -67,15 +67,6 @@ public class PercolateShardResponse extends BroadcastShardOperationResponse { this.requestedSize = context.size; } - public PercolateShardResponse(BytesRef[] matches, long count, PercolateContext context, String index, int shardId) { - super(index, shardId); - this.matches = matches; - this.scores = new float[0]; - this.count = count; - this.percolatorTypeId = context.percolatorTypeId; - this.requestedSize = context.size; - } - public PercolateShardResponse(BytesRef[] matches, List> hls, long count, PercolateContext context, String index, int shardId) { super(index, shardId); this.matches = matches; @@ -127,7 +118,7 @@ public class PercolateShardResponse extends BroadcastShardOperationResponse { } public boolean isEmpty() { - return matches.length == 0 && count == 0; + return percolatorTypeId == 0x00; } @Override diff --git a/src/main/java/org/elasticsearch/action/percolate/TransportPercolateAction.java b/src/main/java/org/elasticsearch/action/percolate/TransportPercolateAction.java index d3397531cec..0a7174284bf 100644 --- a/src/main/java/org/elasticsearch/action/percolate/TransportPercolateAction.java +++ b/src/main/java/org/elasticsearch/action/percolate/TransportPercolateAction.java @@ -131,7 +131,6 @@ public class TransportPercolateAction extends TransportBroadcastOperationAction< List shardFailures = null; byte percolatorTypeId = 0x00; - int nonEmptyResponses = 0; for (int i = 0; i < shardsResponses.length(); i++) { Object shardResponse = shardsResponses.get(i); if (shardResponse == null) { @@ -144,21 +143,18 @@ public class TransportPercolateAction extends TransportBroadcastOperationAction< shardFailures.add(new DefaultShardOperationFailedException((BroadcastShardOperationFailedException) shardResponse)); } else { PercolateShardResponse percolateShardResponse = (PercolateShardResponse) shardResponse; - if (shardResults == null) { - shardResults = newArrayList(); - } - if (percolateShardResponse.percolatorTypeId() != 0x00) { - percolatorTypeId = percolateShardResponse.percolatorTypeId(); - } - if (!percolateShardResponse.isEmpty()) { - nonEmptyResponses++; - } - shardResults.add(percolateShardResponse); successfulShards++; + if (!percolateShardResponse.isEmpty()) { + if (shardResults == null) { + percolatorTypeId = percolateShardResponse.percolatorTypeId(); + shardResults = newArrayList(); + } + shardResults.add(percolateShardResponse); + } } } - if (shardResults == null || percolatorTypeId == 0x00 || nonEmptyResponses == 0) { + if (shardResults == null) { long tookInMillis = System.currentTimeMillis() - request.startTime; return new PercolateResponse(shardsResponses.length(), successfulShards, failedShards, shardFailures, tookInMillis); } else { diff --git a/src/test/java/org/elasticsearch/test/integration/percolator/RecoveryPercolatorTests.java b/src/test/java/org/elasticsearch/test/integration/percolator/RecoveryPercolatorTests.java index b26c5e7bbb2..c4f7a71477d 100644 --- a/src/test/java/org/elasticsearch/test/integration/percolator/RecoveryPercolatorTests.java +++ b/src/test/java/org/elasticsearch/test/integration/percolator/RecoveryPercolatorTests.java @@ -330,27 +330,32 @@ public class RecoveryPercolatorTests extends AbstractNodesTests { } } - String preference = "_prefer_node:" + (randomBoolean() ? node2Id : node3Id); + String preference; + if (node2Id == null && node3Id == null) { + preference = "_local"; + } else { + preference = "_prefer_node:" + (randomBoolean() ? node2Id : node3Id); + } if (multiPercolate) { MultiPercolateRequestBuilder builder = client .prepareMultiPercolate(); int numPercolateRequest = randomIntBetween(50, 100); - for (int i = 0; i < numPercolateRequest / 2; i++) { - builder.add( - client.preparePercolate() - .setPreference(preference) - .setIndices("test").setDocumentType("type") - .setPercolateDoc(docBuilder().setDoc(doc))); - } - - for (int i = numPercolateRequest / 2; i < numPercolateRequest; i++) { - builder.add( - client.preparePercolate() - .setPreference(preference) - .setGetRequest(Requests.getRequest("test").type("type").id("1")) - .setIndices("test").setDocumentType("type") - ); + for (int i = 0; i < numPercolateRequest; i++) { + if (randomBoolean()) { + builder.add( + client.preparePercolate() + .setPreference(preference) + .setGetRequest(Requests.getRequest("test").type("type").id("1")) + .setIndices("test").setDocumentType("type") + ); + } else { + builder.add( + client.preparePercolate() + .setPreference(preference) + .setIndices("test").setDocumentType("type") + .setPercolateDoc(docBuilder().setDoc(doc))); + } } MultiPercolateResponse response = builder.execute().actionGet();