Apache Lucene open-source search software
SOLR-6026: Also check work-queue while processing a REQUESTSTATUS Collection API Call
Anshum Gupta22174ac432a3
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 2765c3c1741..1f54aafc3b2 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -179,6 +179,9 @@ Other Changes* SOLR-6116: Refactor DocRouter.getDocRouter to accept routerName as a String. (shalin)+* SOLR-6026: REQUESTSTATUS Collection API now also checks for submitted tasks which are+ yet to begin execution.+Optimizations----------------------diff --git a/solr/core/src/java/org/apache/solr/cloud/DistributedQueue.java b/solr/core/src/java/org/apache/solr/cloud/DistributedQueue.javaindex 3fb82794adf..a073da497a3 100644--- a/solr/core/src/java/org/apache/solr/cloud/DistributedQueue.java+++ b/solr/core/src/java/org/apache/solr/cloud/DistributedQueue.java@@ -22,6 +22,7 @@ import org.apache.solr.common.SolrException;import org.apache.solr.common.SolrException.ErrorCode;import org.apache.solr.common.cloud.SolrZkClient;import org.apache.solr.common.cloud.ZkCmdExecutor;+import org.apache.solr.common.cloud.ZkNodeProps;import org.apache.solr.util.stats.TimerContext;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.KeeperException;@@ -118,7 +119,39 @@ public class DistributedQueue {return orderedChildren;}-+++ /**+ * Returns true if the queue contains a task with the specified async id.+ */+ public boolean containsTaskWithRequestId(String requestId)+ throws KeeperException, InterruptedException {++ List<String> childNames = null;+ try {+ childNames = zookeeper.getChildren(dir, null, true);+ } catch (KeeperException.NoNodeException e) {+ throw e;+ }++ for (String childName : childNames) {+ if (childName != null) {+ try {+ ZkNodeProps message = ZkNodeProps.load(zookeeper.getData(dir + "/" + childName, null, null, true));+ if (message.containsKey(OverseerCollectionProcessor.ASYNC)) {+ LOG.info(">>>> {}", message.get(OverseerCollectionProcessor.ASYNC));+ if(message.get(OverseerCollectionProcessor.ASYNC).equals(requestId)) return true;+ }+ } catch (KeeperException.NoNodeException e) {+ // Another client removed the node first, try next+ }+ }+ }++ return false;+ }++/*** Return the head of the queue without modifying the queue.*diff --git a/solr/core/src/java/org/apache/solr/handler/admin/CollectionsHandler.java b/solr/core/src/java/org/apache/solr/handler/admin/CollectionsHandler.javaindex 3f57aacd1f4..380435bcfb5 100644--- a/solr/core/src/java/org/apache/solr/handler/admin/CollectionsHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/admin/CollectionsHandler.java@@ -51,6 +51,7 @@ import org.apache.solr.client.solrj.SolrServerException;import org.apache.solr.client.solrj.impl.HttpSolrServer;import org.apache.solr.client.solrj.request.CoreAdminRequest;import org.apache.solr.client.solrj.request.CoreAdminRequest.RequestSyncShard;+import org.apache.solr.cloud.DistributedQueue;import org.apache.solr.cloud.DistributedQueue.QueueEvent;import org.apache.solr.cloud.Overseer;import org.apache.solr.cloud.OverseerCollectionProcessor;@@ -291,6 +292,11 @@ public class CollectionsHandler extends RequestHandlerBase {} else if (coreContainer.getZkController().getOverseerRunningMap().contains(requestId)) {SimpleOrderedMap success = new SimpleOrderedMap();success.add("state", "running");+ success.add("msg", "found " + requestId + " in running tasks");+ results.add("status", success);+ } else if(overseerCollectionQueueContains(requestId)){+ SimpleOrderedMap success = new SimpleOrderedMap();+ success.add("state", "submitted");success.add("msg", "found " + requestId + " in submitted tasks");results.add("status", success);} else {@@ -305,6 +311,11 @@ public class CollectionsHandler extends RequestHandlerBase {}}+ private boolean overseerCollectionQueueContains(String asyncId) throws KeeperException, InterruptedException {+ DistributedQueue collectionQueue = coreContainer.getZkController().getOverseerCollectionQueue();+ return collectionQueue.containsTaskWithRequestId(asyncId);+ }+private void handleResponse(String operation, ZkNodeProps m,SolrQueryResponse rsp) throws KeeperException, InterruptedException {handleResponse(operation, m, rsp, DEFAULT_ZK_TIMEOUT);@@ -326,13 +337,13 @@ public class CollectionsHandler extends RequestHandlerBase {if (coreContainer.getZkController().getOverseerCompletedMap().contains(asyncId) ||coreContainer.getZkController().getOverseerFailureMap().contains(asyncId) ||- coreContainer.getZkController().getOverseerRunningMap().contains(asyncId)) {+ coreContainer.getZkController().getOverseerRunningMap().contains(asyncId) ||+ overseerCollectionQueueContains(asyncId)) {r.add("error", "Task with the same requestid already exists.");} else {coreContainer.getZkController().getOverseerCollectionQueue().offer(ZkStateReader.toJSON(m));-}r.add(CoreAdminParams.REQUESTID, (String) m.get(ASYNC));SolrResponse response = new OverseerSolrResponse(r);diff --git a/solr/core/src/test/org/apache/solr/cloud/AsyncMigrateRouteKeyTest.java b/solr/core/src/test/org/apache/solr/cloud/AsyncMigrateRouteKeyTest.javaindex 6b9a7090397..c26df681505 100644--- a/solr/core/src/test/org/apache/solr/cloud/AsyncMigrateRouteKeyTest.java+++ b/solr/core/src/test/org/apache/solr/cloud/AsyncMigrateRouteKeyTest.java@@ -55,7 +55,7 @@ public class AsyncMigrateRouteKeyTest extends MigrateRouteKeyTest {params.set(OverseerCollectionProcessor.REQUESTID, asyncId);// This task takes long enough to run. Also check for the current state of the task to be running.message = sendStatusRequestWithRetry(params, 2);- assertEquals("found " + asyncId + " in submitted tasks", message);+ assertEquals("found " + asyncId + " in running tasks", message);// Now wait until the task actually completes successfully/fails.message = sendStatusRequestWithRetry(params, 20);assertEquals("Task " + asyncId + " not found in completed tasks.",diff --git a/solr/core/src/test/org/apache/solr/cloud/MultiThreadedOCPTest.java b/solr/core/src/test/org/apache/solr/cloud/MultiThreadedOCPTest.javaindex f09e6752291..309e91dbb71 100644--- a/solr/core/src/test/org/apache/solr/cloud/MultiThreadedOCPTest.java+++ b/solr/core/src/test/org/apache/solr/cloud/MultiThreadedOCPTest.java@@ -64,6 +64,7 @@ public class MultiThreadedOCPTest extends AbstractFullDistribZkTestBase {testParallelCollectionAPICalls();testTaskExclusivity();+ testDeduplicationOfSubmittedTasks();testLongAndShortRunningParallelApiCalls();}@@ -139,6 +140,25 @@ public class MultiThreadedOCPTest extends AbstractFullDistribZkTestBase {}}+ private void testDeduplicationOfSubmittedTasks() throws IOException, SolrServerException {+ SolrServer server = createNewSolrServer("", getBaseUrl((HttpSolrServer) clients.get(0)));+ CollectionAdminRequest.createCollection("ocptest_shardsplit2", 4, "conf1", server, "3000");++ CollectionAdminRequest.splitShard("ocptest_shardsplit2", SHARD1, server, "3001");+ CollectionAdminRequest.splitShard("ocptest_shardsplit2", SHARD2, server, "3002");++ // Now submit another task with the same id. At this time, hopefully the previous 2002 should still be in the queue.+ CollectionAdminResponse response = CollectionAdminRequest.splitShard("ocptest_shardsplit2", SHARD1, server, "3002");+ NamedList r = response.getResponse();+ assertEquals("Duplicate request was supposed to exist but wasn't found. De-duplication of submitted task failed.",+ "Task with the same requestid already exists.", r.get("error"));++ for (int i=3001;i<=3002;i++) {+ String state = getRequestStateAfterCompletion(i + "", 30, server);+ assertTrue("Task " + i + " did not complete, final state: " + state,state.equals("completed"));+ }+ }+private void testLongAndShortRunningParallelApiCalls() throws InterruptedException, IOException, SolrServerException {Thread indexThread = new Thread() {@@ -158,17 +178,14 @@ public class MultiThreadedOCPTest extends AbstractFullDistribZkTestBase {indexThread.start();try {- Thread.sleep(5000);SolrServer server = createNewSolrServer("", getBaseUrl((HttpSolrServer) clients.get(0)));CollectionAdminRequest.splitShard("collection1", SHARD1, server, "2000");String state = getRequestState("2000", server);- while (!state.equals("running")) {+ while (state.equals("submitted")) {state = getRequestState("2000", server);- if (state.equals("completed") || state.equals("failed"))- break;- Thread.sleep(100);+ Thread.sleep(10);}assertTrue("SplitShard task [2000] was supposed to be in [running] but isn't. It is [" + state + "]", state.equals("running"));