Apache Solr open-source search software
SOLR-15363 Add CompletableFuture#runAsync to forbidden-api (#86)
Mike Drobe843d8203da2
diff --git a/gradle/validation/forbidden-apis.gradle b/gradle/validation/forbidden-apis.gradleindex 3d6840697bc..bbee655fc89 100644--- a/gradle/validation/forbidden-apis.gradle+++ b/gradle/validation/forbidden-apis.gradle@@ -1,4 +1,4 @@- /*+/** 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.diff --git a/gradle/validation/forbidden-apis/defaults.all.txt b/gradle/validation/forbidden-apis/defaults.all.txtindex 6c6e0296cc5..51643a096f9 100644--- a/gradle/validation/forbidden-apis/defaults.all.txt+++ b/gradle/validation/forbidden-apis/defaults.all.txt@@ -22,6 +22,25 @@ java.util.concurrent.Executors#newScheduledThreadPool(int)java.util.concurrent.Executors#defaultThreadFactory()java.util.concurrent.Executors#privilegedThreadFactory()+@defaultMessage Spawns threads without MDC logging context; use ExecutorUtil.newMDCAwareFixedThreadPool instead+java.util.concurrent.Executors#newFixedThreadPool(int,java.util.concurrent.ThreadFactory)++@defaultMessage Spawns threads without MDC logging context; use ExecutorUtil.newMDCAwareSingleThreadExecutor instead+java.util.concurrent.Executors#newSingleThreadExecutor(java.util.concurrent.ThreadFactory)++@defaultMessage Spawns threads without MDC logging context; use ExecutorUtil.newMDCAwareCachedThreadPool instead+java.util.concurrent.Executors#newCachedThreadPool(java.util.concurrent.ThreadFactory)++@defaultMessage Use ExecutorUtil.MDCAwareThreadPoolExecutor instead of ThreadPoolExecutor+java.util.concurrent.ThreadPoolExecutor#<init>(int,int,long,java.util.concurrent.TimeUnit,java.util.concurrent.BlockingQueue,java.util.concurrent.ThreadFactory,java.util.concurrent.RejectedExecutionHandler)+java.util.concurrent.ThreadPoolExecutor#<init>(int,int,long,java.util.concurrent.TimeUnit,java.util.concurrent.BlockingQueue)+java.util.concurrent.ThreadPoolExecutor#<init>(int,int,long,java.util.concurrent.TimeUnit,java.util.concurrent.BlockingQueue,java.util.concurrent.ThreadFactory)+java.util.concurrent.ThreadPoolExecutor#<init>(int,int,long,java.util.concurrent.TimeUnit,java.util.concurrent.BlockingQueue,java.util.concurrent.RejectedExecutionHandler)++@defaultMessage Must specify an explicit executor to run async tasks so that we have named threads+java.util.concurrent.CompletableFuture#runAsync(java.lang.Runnable)+java.util.concurrent.CompletableFuture#supplyAsync(java.util.function.Supplier)+@defaultMessage Properties files should be read/written with Reader/Writer, using UTF-8 charset. This allows reading older files with unicode escapes, too.java.util.Properties#load(java.io.InputStream)java.util.Properties#save(java.io.OutputStream,java.lang.String)@@ -66,3 +85,9 @@ java.io.ObjectOutputStream@defaultMessage Don't set a dictionary on a Deflater using a method that takes an offset or ByteBuffer (JDK-8252739)java.util.zip.Deflater#setDictionary(byte[],int,int)java.util.zip.Deflater#setDictionary(java.nio.ByteBuffer)++@defaultMessage Use RTimer/TimeOut/System.nanoTime for time comparisons, and `new Date()` output/debugging/stats of timestamps. If for some miscellaneous reason, you absolutely need to use this, use a SuppressForbidden.+java.lang.System#currentTimeMillis()++@defaultMessage Use slf4j classes instead+java.util.logging.**diff --git a/gradle/validation/forbidden-apis/defaults.lucene.txt b/gradle/validation/forbidden-apis/defaults.lucene.txtdeleted file mode 100644index 0cc4edda6bd..00000000000--- a/gradle/validation/forbidden-apis/defaults.lucene.txt+++ /dev/null@@ -1,49 +0,0 @@-# 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.--@defaultMessage Use NIO.2 instead-java.io.File-java.io.FileInputStream-java.io.FileOutputStream-java.io.PrintStream#<init>(java.lang.String,java.lang.String)-java.io.PrintWriter#<init>(java.lang.String,java.lang.String)-java.util.Formatter#<init>(java.lang.String,java.lang.String,java.util.Locale)-java.io.RandomAccessFile-java.nio.file.Path#toFile()-java.util.jar.JarFile-java.util.zip.ZipFile-@defaultMessage Prefer using ArrayUtil as Arrays#copyOfRange fills zeros for bad bounds-java.util.Arrays#copyOfRange(byte[],int,int)-java.util.Arrays#copyOfRange(char[],int,int)-java.util.Arrays#copyOfRange(short[],int,int)-java.util.Arrays#copyOfRange(int[],int,int)-java.util.Arrays#copyOfRange(long[],int,int)-java.util.Arrays#copyOfRange(float[],int,int)-java.util.Arrays#copyOfRange(double[],int,int)-java.util.Arrays#copyOfRange(boolean[],int,int)-java.util.Arrays#copyOfRange(java.lang.Object[],int,int)-java.util.Arrays#copyOfRange(java.lang.Object[],int,int,java.lang.Class)--@defaultMessage Prefer using ArrayUtil as Arrays#copyOf fills zeros for bad bounds-java.util.Arrays#copyOf(byte[],int)-java.util.Arrays#copyOf(char[],int)-java.util.Arrays#copyOf(short[],int)-java.util.Arrays#copyOf(int[],int)-java.util.Arrays#copyOf(long[],int)-java.util.Arrays#copyOf(float[],int)-java.util.Arrays#copyOf(double[],int)-java.util.Arrays#copyOf(boolean[],int)-java.util.Arrays#copyOf(java.lang.Object[],int)-java.util.Arrays#copyOf(java.lang.Object[],int,java.lang.Class)diff --git a/gradle/validation/forbidden-apis/defaults.solr.txt b/gradle/validation/forbidden-apis/defaults.solr.txtdeleted file mode 100644index 50c69ac0227..00000000000--- a/gradle/validation/forbidden-apis/defaults.solr.txt+++ /dev/null@@ -1,35 +0,0 @@-# 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.--@defaultMessage Spawns threads without MDC logging context; use ExecutorUtil.newMDCAwareFixedThreadPool instead-java.util.concurrent.Executors#newFixedThreadPool(int,java.util.concurrent.ThreadFactory)--@defaultMessage Spawns threads without MDC logging context; use ExecutorUtil.newMDCAwareSingleThreadExecutor instead-java.util.concurrent.Executors#newSingleThreadExecutor(java.util.concurrent.ThreadFactory)--@defaultMessage Spawns threads without MDC logging context; use ExecutorUtil.newMDCAwareCachedThreadPool instead-java.util.concurrent.Executors#newCachedThreadPool(java.util.concurrent.ThreadFactory)--@defaultMessage Use ExecutorUtil.MDCAwareThreadPoolExecutor instead of ThreadPoolExecutor-java.util.concurrent.ThreadPoolExecutor#<init>(int,int,long,java.util.concurrent.TimeUnit,java.util.concurrent.BlockingQueue,java.util.concurrent.ThreadFactory,java.util.concurrent.RejectedExecutionHandler)-java.util.concurrent.ThreadPoolExecutor#<init>(int,int,long,java.util.concurrent.TimeUnit,java.util.concurrent.BlockingQueue)-java.util.concurrent.ThreadPoolExecutor#<init>(int,int,long,java.util.concurrent.TimeUnit,java.util.concurrent.BlockingQueue,java.util.concurrent.ThreadFactory)-java.util.concurrent.ThreadPoolExecutor#<init>(int,int,long,java.util.concurrent.TimeUnit,java.util.concurrent.BlockingQueue,java.util.concurrent.RejectedExecutionHandler)--@defaultMessage Use RTimer/TimeOut/System.nanoTime for time comparisons, and `new Date()` output/debugging/stats of timestamps. If for some miscellaneous reason, you absolutely need to use this, use a SuppressForbidden.-java.lang.System#currentTimeMillis()--@defaultMessage Use slf4j classes instead-java.util.logging.**diff --git a/solr/core/src/test/org/apache/solr/search/TestTaskManagement.java b/solr/core/src/test/org/apache/solr/search/TestTaskManagement.javaindex 0666a5be9a5..ac0a3b09363 100644--- a/solr/core/src/test/org/apache/solr/search/TestTaskManagement.java+++ b/solr/core/src/test/org/apache/solr/search/TestTaskManagement.java@@ -18,7 +18,9 @@ package org.apache.solr.search;import org.apache.lucene.util.BytesRef;import org.apache.solr.client.solrj.SolrRequest;+import org.apache.solr.client.solrj.SolrServerException;import org.apache.solr.client.solrj.request.CollectionAdminRequest;+import org.apache.solr.client.solrj.request.GenericSolrRequest;import org.apache.solr.client.solrj.request.QueryRequest;import org.apache.solr.cloud.SolrCloudTestCase;import org.apache.solr.common.SolrInputDocument;@@ -26,11 +28,14 @@ import org.apache.solr.common.params.ModifiableSolrParams;import org.apache.solr.common.util.ExecutorUtil;import org.apache.solr.common.util.NamedList;import org.junit.After;-import org.junit.AfterClass;import org.junit.Before;import org.junit.BeforeClass;import org.junit.Test;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;+import java.io.IOException;+import java.lang.invoke.MethodHandles;import java.util.ArrayList;import java.util.HashSet;import java.util.Iterator;@@ -41,28 +46,26 @@ import java.util.concurrent.CompletableFuture;import java.util.concurrent.CompletionException;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.ExecutorService;+import java.util.concurrent.TimeUnit;public class TestTaskManagement extends SolrCloudTestCase {+ private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());+private static final String COLLECTION_NAME = "collection1";- private ExecutorService executorService;+ private ExecutorService queryExecutor;+ private ExecutorService cancelExecutor;@BeforeClasspublic 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 {+ @Override+ public void setUp() throws Exception {super.setUp();CollectionAdminRequest.createCollection(COLLECTION_NAME, "conf", 2, 1)@@ -71,7 +74,8 @@ public class TestTaskManagement extends SolrCloudTestCase {cluster.waitForActiveCollection(COLLECTION_NAME, 2, 2);cluster.getSolrClient().setDefaultCollection(COLLECTION_NAME);- executorService = ExecutorUtil.newMDCAwareCachedThreadPool("TestTaskManagement");+ queryExecutor = ExecutorUtil.newMDCAwareCachedThreadPool("TestTaskManagement-Query");+ cancelExecutor = ExecutorUtil.newMDCAwareCachedThreadPool("TestTaskManagement-Cancel");List<SolrInputDocument> docs = new ArrayList<>();for (int i = 0; i < 100; i++) {@@ -89,76 +93,69 @@ public class TestTaskManagement extends SolrCloudTestCase {}@After+ @Overridepublic void tearDown() throws Exception {+ queryExecutor.shutdown();+ cancelExecutor.shutdown();++ queryExecutor.awaitTermination(5, TimeUnit.SECONDS);CollectionAdminRequest.deleteCollection(COLLECTION_NAME).process(cluster.getSolrClient());- executorService.shutdown();+super.tearDown();}@Testpublic void testNonExistentQuery() throws Exception {ModifiableSolrParams params = new ModifiableSolrParams();-params.set("queryUUID", "foobar");- @SuppressWarnings({"rawtypes"})- SolrRequest request = new QueryRequest(params);- request.setPath("/tasks/cancel");- NamedList<Object> queryResponse;-- queryResponse = cluster.getSolrClient().request(request);+ GenericSolrRequest request = new GenericSolrRequest(SolrRequest.METHOD.GET, "/tasks/cancel", params);+ NamedList<Object> queryResponse = cluster.getSolrClient().request(request);assertEquals("Query with queryID foobar not found", queryResponse.get("status"));assertEquals(404, queryResponse.get("responseCode"));}@Test- public void testCancellationQuery() {+ public void testCancellationQuery() throws IOException, SolrServerException {Set<Integer> queryIdsSet = ConcurrentHashMap.newKeySet();Set<Integer> notFoundIdsSet = ConcurrentHashMap.newKeySet();List<CompletableFuture<Void>> queryFutures = new ArrayList<>();for (int i = 0; i < 100; i++) {- CompletableFuture<Void> future = executeQueryAsync(Integer.toString(i));-- queryFutures.add(future);+ queryFutures.add(executeQueryAsync(Integer.toString(i)));}- List<CompletableFuture<Void>> futures = new ArrayList<>();+ List<CompletableFuture<Void>> cancelFutures = new ArrayList<>();- for (int i = 0; i < 100; i++) {- CompletableFuture<Void> future = cancelQuery(Integer.toString(i), 4000, queryIdsSet, notFoundIdsSet);+ NamedList<String> tasks = listTasks();- futures.add(future);+ for (int i = 0; i < 100; i++) {+ cancelFutures.add(cancelQuery(Integer.toString(i), 0, queryIdsSet, notFoundIdsSet));}- futures.forEach(CompletableFuture::join);-+ cancelFutures.forEach(CompletableFuture::join);queryFutures.forEach(CompletableFuture::join);+ // There is a very small window where we can successfully cancel the query because QueryComponent will+ // aggressively deregister, and even if we use DelayingSearchComponent these queries are not around+ // assertFalse("Should have canceled at least one query", queryIdsSet.isEmpty());+ if (log.isInfoEnabled()) {+ log.info("Cancelled {} queries", queryIdsSet.size());+ }+assertEquals("Total query count did not match the expected value",queryIdsSet.size() + notFoundIdsSet.size(), 100);}@Testpublic void testListCancellableQueries() throws Exception {- ModifiableSolrParams params = new ModifiableSolrParams();-- @SuppressWarnings({"rawtypes"})- SolrRequest request = new QueryRequest(params);- request.setPath("/tasks/list");-for (int i = 0; i < 50; i++) {executeQueryAsync(Integer.toString(i));}- NamedList<Object> queryResponse;-- queryResponse = cluster.getSolrClient().request(request);-- @SuppressWarnings({"unchecked"})- NamedList<String> result = (NamedList<String>) queryResponse.get("taskList");+ NamedList<String> result = listTasks();Iterator<Map.Entry<String, String>> iterator = result.iterator();@@ -177,17 +174,19 @@ public class TestTaskManagement extends SolrCloudTestCase {}}+ @SuppressWarnings("unchecked")+ private NamedList<String> listTasks() throws SolrServerException, IOException {+ NamedList<Object> response = cluster.getSolrClient().request(+ new GenericSolrRequest(SolrRequest.METHOD.GET, "/tasks/list", null));+ return (NamedList<String>) response.get("taskList");+ }+@Testpublic void testCheckSpecificQueryStatus() throws Exception {ModifiableSolrParams params = new ModifiableSolrParams();-params.set("taskUUID", "25");- @SuppressWarnings({"rawtypes"})- SolrRequest request = new QueryRequest(params);-- request.setPath("/tasks/list");-+ GenericSolrRequest request = new GenericSolrRequest(SolrRequest.METHOD.GET, "/tasks/list", params);NamedList<Object> queryResponse = cluster.getSolrClient().request(request);@SuppressWarnings({"unchecked"})@@ -202,8 +201,7 @@ public class TestTaskManagement extends SolrCloudTestCase {ModifiableSolrParams params = new ModifiableSolrParams();params.set("queryUUID", queryID);- @SuppressWarnings({"rawtypes"})- SolrRequest request = new QueryRequest(params);+ SolrRequest<?> request = new QueryRequest(params);request.setPath("/tasks/cancel");// Wait for some time to let the query start@@ -231,7 +229,7 @@ public class TestTaskManagement extends SolrCloudTestCase {Thread.currentThread().interrupt();throw new CompletionException(e);}- }, executorService);+ }, cancelExecutor);}public void executeQuery(String queryId) throws Exception {@@ -244,8 +242,7 @@ public class TestTaskManagement extends SolrCloudTestCase {params.set("queryUUID", queryId);}- @SuppressWarnings({"rawtypes"})- SolrRequest request = new QueryRequest(params);+ SolrRequest<?> request = new QueryRequest(params);cluster.getSolrClient().request(request);}@@ -257,6 +254,6 @@ public class TestTaskManagement extends SolrCloudTestCase {} catch (Exception e) {throw new CompletionException(e);}- });+ }, queryExecutor);}}