madrob commented on a change in pull request #2403: URL: https://github.com/apache/lucene-solr/pull/2403#discussion_r592489533
########## File path: solr/core/src/test/org/apache/solr/search/TestTaskManagement.java ########## @@ -0,0 +1,263 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.solr.search; + +import org.apache.lucene.util.BytesRef; +import org.apache.solr.client.solrj.SolrRequest; +import org.apache.solr.client.solrj.request.CollectionAdminRequest; +import org.apache.solr.client.solrj.request.QueryRequest; +import org.apache.solr.cloud.SolrCloudTestCase; +import org.apache.solr.common.SolrInputDocument; +import org.apache.solr.common.params.ModifiableSolrParams; +import org.apache.solr.common.util.ExecutorUtil; +import org.apache.solr.common.util.NamedList; +import org.eclipse.jetty.util.ConcurrentHashSet; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; + +public class TestTaskManagement extends SolrCloudTestCase { + private static final String COLLECTION_NAME = "collection1"; + + private ExecutorService executorService; + + @BeforeClass + public static void setupCluster() throws Exception { + initCore("solrconfig.xml", "schema11.xml"); + + configureCluster(4) + .addConfig("conf", configset("sql")) + .configure(); + } + + @AfterClass + public static void tearDownCluster() throws Exception { + shutdownCluster(); + } + + @Before + public void setup() throws Exception { + super.setUp(); + + CollectionAdminRequest.createCollection(COLLECTION_NAME, "conf", 2, 1) + .setPerReplicaState(SolrCloudTestCase.USE_PER_REPLICA_STATE) + .process(cluster.getSolrClient()); + cluster.waitForActiveCollection(COLLECTION_NAME, 2, 2); + cluster.getSolrClient().setDefaultCollection(COLLECTION_NAME); + + cluster.getSolrClient().setDefaultCollection("collection1"); + + executorService = ExecutorUtil.newMDCAwareCachedThreadPool("TestTaskManagement"); + + List<SolrInputDocument> docs = new ArrayList<>(); + for (int i = 0; i < 100; i++) { + SolrInputDocument doc = new SolrInputDocument(); + doc.addField("id", i); + doc.addField("foo1_s", Integer.toString(i)); + doc.addField("foo2_s", Boolean.toString(i % 2 == 0)); + doc.addField("foo4_s", new BytesRef(Boolean.toString(i % 2 == 0))); + + docs.add(doc); + } + + cluster.getSolrClient().add(docs); + cluster.getSolrClient().commit(); + } + + @After + public void tearDown() throws Exception { + CollectionAdminRequest.deleteCollection(COLLECTION_NAME).process(cluster.getSolrClient()); + executorService.shutdown(); + super.tearDown(); + } + + @Test + public void testNonExistentQuery() throws Exception { + ModifiableSolrParams params = new ModifiableSolrParams(); + + params.set("cancelUUID", "foobar"); + @SuppressWarnings({"rawtypes"}) + SolrRequest request = new QueryRequest(params); + request.setPath("/tasks/cancel"); + + NamedList<Object> queryResponse = null; + + queryResponse = cluster.getSolrClient().request(request); + + assertEquals("Query with queryID foobar not found", queryResponse.get("status")); + } + + @Test + public void testCancellationQuery() throws Exception { + ConcurrentHashSet<Integer> queryIdsSet = new ConcurrentHashSet<>(); Review comment: @atris did you see this comment? ########## File path: solr/core/src/java/org/apache/solr/core/CancellableQueryTracker.java ########## @@ -0,0 +1,106 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.solr.core; + +import org.apache.solr.client.solrj.util.Cancellable; +import org.apache.solr.search.CancellableCollector; +import org.apache.solr.request.SolrQueryRequest; + +import java.util.Iterator; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; + +import static org.apache.solr.common.params.CommonParams.QUERY_UUID; + +/** + * Tracks metadata for active queries and provides methods for access + */ +public class CancellableQueryTracker { + //TODO: This needs to become a time aware storage model + private final Map<String, Cancellable> activeCancellableQueries = new ConcurrentHashMap<>(); + private final Map<String, String> activeQueriesGenerated = new ConcurrentHashMap<>(); + + /** Generates a UUID for the given query or if the user provided a UUID + * for this query, uses that. + */ + public String generateQueryID(SolrQueryRequest req) { + String queryID; + String customQueryUUID = req.getParams().get(QUERY_UUID, null); + + if (customQueryUUID != null) { + queryID = customQueryUUID; + } else { + queryID = UUID.randomUUID().toString(); + } + + if (activeQueriesGenerated.containsKey(queryID)) { + if (customQueryUUID != null) { + throw new IllegalArgumentException("Duplicate query UUID given"); Review comment: So it's possible that when using a mix of generated and non-generated query IDs that we will get a conflict. Hmm... I would call this case out specifically in the docs, something like recommending that users either use all generated or all custom. ########## File path: solr/core/src/java/org/apache/solr/core/CancellableQueryTracker.java ########## @@ -0,0 +1,106 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.solr.core; + +import org.apache.solr.client.solrj.util.Cancellable; +import org.apache.solr.search.CancellableCollector; +import org.apache.solr.request.SolrQueryRequest; + +import java.util.Iterator; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; + +import static org.apache.solr.common.params.CommonParams.QUERY_UUID; + +/** + * Tracks metadata for active queries and provides methods for access + */ +public class CancellableQueryTracker { + //TODO: This needs to become a time aware storage model + private final Map<String, Cancellable> activeCancellableQueries = new ConcurrentHashMap<>(); + private final Map<String, String> activeQueriesGenerated = new ConcurrentHashMap<>(); + + /** Generates a UUID for the given query or if the user provided a UUID + * for this query, uses that. + */ + public String generateQueryID(SolrQueryRequest req) { + String queryID; + String customQueryUUID = req.getParams().get(QUERY_UUID, null); + + if (customQueryUUID != null) { + queryID = customQueryUUID; + } else { + queryID = UUID.randomUUID().toString(); Review comment: I mentioned this previously before the file moved - UUID.randomUUID is eventually slow because it eats system entropy. And it's also synchronized under the hood, getting the bytes, so not great for multi-threaded use. We can leave it in for now and replace with a different implementation later. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org