From 18fb9463de4828e736fa75e0e8f06c013133513b Mon Sep 17 00:00:00 2001 From: Noble Paul Date: Tue, 5 Apr 2016 22:45:36 +0530 Subject: [PATCH] SOLR-8750: replace anonymous inner class for callable, Runnable etc --- .../solr/analytics/expression/Expression.java | 14 +-- .../java/org/apache/solr/hadoop/GoLive.java | 27 ++--- .../solr/hadoop/MapReduceIndexerTool.java | 7 +- .../solr/hadoop/ZooKeeperInspector.java | 9 +- .../solr/SolrMorphlineZkAvroTest.java | 14 +-- .../OverseerAutoReplicaFailoverThread.java | 8 +- .../apache/solr/core/ConfigSetService.java | 9 +- .../org/apache/solr/core/CoreContainer.java | 35 +++--- .../solr/core/HdfsDirectoryFactory.java | 21 +--- .../java/org/apache/solr/core/SolrCore.java | 83 +++++-------- .../java/org/apache/solr/core/SolrCores.java | 25 ++-- .../solr/handler/CdcrReplicatorScheduler.java | 19 ++- .../solr/handler/MoreLikeThisHandler.java | 12 +- .../handler/component/HttpShardHandler.java | 109 +++++++++--------- .../PerSegmentSingleValuedFaceting.java | 9 +- .../org/apache/solr/request/SimpleFacets.java | 35 +++--- .../solr/schema/AbstractSpatialFieldType.java | 7 +- .../solr/update/SolrCmdDistributor.java | 11 +- .../solr/cloud/BasicDistributedZkTest.java | 78 ++++++------- .../org/apache/solr/search/TestRecovery.java | 7 +- .../apache/solr/search/TestRecoveryHdfs.java | 13 +-- .../solr/update/AddBlockUpdateTest.java | 20 +--- .../org/apache/solr/update/TestUpdate.java | 16 +-- .../client/solrj/impl/CloudSolrClient.java | 7 +- .../client/solrj/impl/HttpSolrClient.java | 7 +- .../solrj/impl/Krb5HttpClientBuilder.java | 52 +++------ .../solrj/impl/SolrHttpClientBuilder.java | 8 +- .../client/solrj/io/comp/FieldComparator.java | 42 +++---- .../solr/cloud/MiniSolrCloudCluster.java | 14 +-- 29 files changed, 269 insertions(+), 449 deletions(-) diff --git a/solr/contrib/analytics/src/java/org/apache/solr/analytics/expression/Expression.java b/solr/contrib/analytics/src/java/org/apache/solr/analytics/expression/Expression.java index 4c9bd585d95..ba26d9adfaf 100644 --- a/solr/contrib/analytics/src/java/org/apache/solr/analytics/expression/Expression.java +++ b/solr/contrib/analytics/src/java/org/apache/solr/analytics/expression/Expression.java @@ -28,15 +28,11 @@ public abstract class Expression { public abstract Comparable getValue(); public Comparator comparator(final FacetSortDirection direction) { - return new Comparator(){ - @SuppressWarnings("unchecked") - @Override - public int compare(Expression a, Expression b) { - if( direction == FacetSortDirection.ASCENDING ){ - return a.getValue().compareTo(b.getValue()); - } else { - return b.getValue().compareTo(a.getValue()); - } + return (a, b) -> { + if( direction == FacetSortDirection.ASCENDING ){ + return a.getValue().compareTo(b.getValue()); + } else { + return b.getValue().compareTo(a.getValue()); } }; } diff --git a/solr/contrib/map-reduce/src/java/org/apache/solr/hadoop/GoLive.java b/solr/contrib/map-reduce/src/java/org/apache/solr/hadoop/GoLive.java index 103e3ee239e..bd48c643623 100644 --- a/solr/contrib/map-reduce/src/java/org/apache/solr/hadoop/GoLive.java +++ b/solr/contrib/map-reduce/src/java/org/apache/solr/hadoop/GoLive.java @@ -87,22 +87,19 @@ class GoLive { baseUrl = baseUrl.substring(0, lastPathIndex); final String mergeUrl = baseUrl; - Callable task = new Callable() { - @Override - public Request call() { - Request req = new Request(); - LOG.info("Live merge " + dir.getPath() + " into " + mergeUrl); - try (final HttpSolrClient client = new HttpSolrClient(mergeUrl)) { - CoreAdminRequest.MergeIndexes mergeRequest = new CoreAdminRequest.MergeIndexes(); - mergeRequest.setCoreName(name); - mergeRequest.setIndexDirs(Arrays.asList(dir.getPath().toString() + "/data/index")); - mergeRequest.process(client); - req.success = true; - } catch (SolrServerException | IOException e) { - req.e = e; - } - return req; + Callable task = () -> { + Request req = new Request(); + LOG.info("Live merge " + dir.getPath() + " into " + mergeUrl); + try (final HttpSolrClient client = new HttpSolrClient(mergeUrl)) { + CoreAdminRequest.MergeIndexes mergeRequest = new CoreAdminRequest.MergeIndexes(); + mergeRequest.setCoreName(name); + mergeRequest.setIndexDirs(Arrays.asList(dir.getPath().toString() + "/data/index")); + mergeRequest.process(client); + req.success = true; + } catch (SolrServerException | IOException e) { + req.e = e; } + return req; }; pending.add(completionService.submit(task)); } diff --git a/solr/contrib/map-reduce/src/java/org/apache/solr/hadoop/MapReduceIndexerTool.java b/solr/contrib/map-reduce/src/java/org/apache/solr/hadoop/MapReduceIndexerTool.java index a2430e7b1c1..23d04c6200a 100644 --- a/solr/contrib/map-reduce/src/java/org/apache/solr/hadoop/MapReduceIndexerTool.java +++ b/solr/contrib/map-reduce/src/java/org/apache/solr/hadoop/MapReduceIndexerTool.java @@ -1188,12 +1188,7 @@ public class MapReduceIndexerTool extends Configured implements Tool { } // use alphanumeric sort (rather than lexicographical sort) to properly handle more than 99999 shards - Arrays.sort(dirs, new Comparator() { - @Override - public int compare(FileStatus f1, FileStatus f2) { - return new AlphaNumericComparator().compare(f1.getPath().getName(), f2.getPath().getName()); - } - }); + Arrays.sort(dirs, (f1, f2) -> new AlphaNumericComparator().compare(f1.getPath().getName(), f2.getPath().getName())); return dirs; } diff --git a/solr/contrib/map-reduce/src/java/org/apache/solr/hadoop/ZooKeeperInspector.java b/solr/contrib/map-reduce/src/java/org/apache/solr/hadoop/ZooKeeperInspector.java index 3672e0b7c53..9b86dcda97a 100644 --- a/solr/contrib/map-reduce/src/java/org/apache/solr/hadoop/ZooKeeperInspector.java +++ b/solr/contrib/map-reduce/src/java/org/apache/solr/hadoop/ZooKeeperInspector.java @@ -114,12 +114,9 @@ final class ZooKeeperInspector { public List getSortedSlices(Collection slices) { List sorted = new ArrayList(slices); - Collections.sort(sorted, new Comparator() { - @Override - public int compare(Slice slice1, Slice slice2) { - Comparator c = new AlphaNumericComparator(); - return c.compare(slice1.getName(), slice2.getName()); - } + Collections.sort(sorted, (slice1, slice2) -> { + Comparator c = new AlphaNumericComparator(); + return c.compare(slice1.getName(), slice2.getName()); }); LOG.trace("Sorted slices: {}", sorted); return sorted; diff --git a/solr/contrib/morphlines-core/src/test/org/apache/solr/morphlines/solr/SolrMorphlineZkAvroTest.java b/solr/contrib/morphlines-core/src/test/org/apache/solr/morphlines/solr/SolrMorphlineZkAvroTest.java index 0eb9e400469..1c30a84e107 100644 --- a/solr/contrib/morphlines-core/src/test/org/apache/solr/morphlines/solr/SolrMorphlineZkAvroTest.java +++ b/solr/contrib/morphlines-core/src/test/org/apache/solr/morphlines/solr/SolrMorphlineZkAvroTest.java @@ -79,12 +79,7 @@ public class SolrMorphlineZkAvroTest extends AbstractSolrMorphlineZkTestBase { assertEquals(2104, collector.getRecords().size()); assertEquals(collector.getRecords().size(), rsp.getResults().size()); - Collections.sort(collector.getRecords(), new Comparator() { - @Override - public int compare(Record r1, Record r2) { - return r1.get("id").toString().compareTo(r2.get("id").toString()); - } - }); + Collections.sort(collector.getRecords(), (r1, r2) -> r1.get("id").toString().compareTo(r2.get("id").toString())); // fetch test input data and sort like solr result set List records = new ArrayList(); @@ -94,12 +89,7 @@ public class SolrMorphlineZkAvroTest extends AbstractSolrMorphlineZkTestBase { records.add(expected); } assertEquals(collector.getRecords().size(), records.size()); - Collections.sort(records, new Comparator() { - @Override - public int compare(GenericData.Record r1, GenericData.Record r2) { - return r1.get("id").toString().compareTo(r2.get("id").toString()); - } - }); + Collections.sort(records, (r1, r2) -> r1.get("id").toString().compareTo(r2.get("id").toString())); Object lastId = null; for (int i = 0; i < records.size(); i++) { diff --git a/solr/core/src/java/org/apache/solr/cloud/OverseerAutoReplicaFailoverThread.java b/solr/core/src/java/org/apache/solr/cloud/OverseerAutoReplicaFailoverThread.java index d6aa3926229..a9f8bdd0997 100644 --- a/solr/core/src/java/org/apache/solr/cloud/OverseerAutoReplicaFailoverThread.java +++ b/solr/core/src/java/org/apache/solr/cloud/OverseerAutoReplicaFailoverThread.java @@ -248,13 +248,7 @@ public class OverseerAutoReplicaFailoverThread implements Runnable, Closeable { log.debug("submit call to {}", createUrl); MDC.put("OverseerAutoReplicaFailoverThread.createUrl", createUrl); try { - updateExecutor.submit(new Callable() { - - @Override - public Boolean call() { - return createSolrCore(collection, createUrl, dataDir, ulogDir, coreNodeName, coreName); - } - }); + updateExecutor.submit(() -> createSolrCore(collection, createUrl, dataDir, ulogDir, coreNodeName, coreName)); } finally { MDC.remove("OverseerAutoReplicaFailoverThread.createUrl"); } diff --git a/solr/core/src/java/org/apache/solr/core/ConfigSetService.java b/solr/core/src/java/org/apache/solr/core/ConfigSetService.java index ac0cabd8827..3e5c8f9e7b7 100644 --- a/solr/core/src/java/org/apache/solr/core/ConfigSetService.java +++ b/solr/core/src/java/org/apache/solr/core/ConfigSetService.java @@ -204,12 +204,9 @@ public abstract class ConfigSetService { if (Files.exists(schemaFile)) { try { String cachedName = cacheName(schemaFile); - return schemaCache.get(cachedName, new Callable() { - @Override - public IndexSchema call() throws Exception { - logger.info("Creating new index schema for core {}", cd.getName()); - return IndexSchemaFactory.buildIndexSchema(cd.getSchemaName(), solrConfig); - } + return schemaCache.get(cachedName, () -> { + logger.info("Creating new index schema for core {}", cd.getName()); + return IndexSchemaFactory.buildIndexSchema(cd.getSchemaName(), solrConfig); }); } catch (ExecutionException e) { throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, diff --git a/solr/core/src/java/org/apache/solr/core/CoreContainer.java b/solr/core/src/java/org/apache/solr/core/CoreContainer.java index 4d57ad1d054..e1ea6fb8bdd 100644 --- a/solr/core/src/java/org/apache/solr/core/CoreContainer.java +++ b/solr/core/src/java/org/apache/solr/core/CoreContainer.java @@ -480,28 +480,25 @@ public class CoreContainer { solrCores.markCoreAsLoading(cd); } if (cd.isLoadOnStartup()) { - futures.add(coreLoadExecutor.submit(new Callable() { - @Override - public SolrCore call() throws Exception { - SolrCore core; - try { - if (zkSys.getZkController() != null) { - zkSys.getZkController().throwErrorIfReplicaReplaced(cd); - } + futures.add(coreLoadExecutor.submit(() -> { + SolrCore core; + try { + if (zkSys.getZkController() != null) { + zkSys.getZkController().throwErrorIfReplicaReplaced(cd); + } - core = create(cd, false); - } finally { - if (asyncSolrCoreLoad) { - solrCores.markCoreAsNotLoading(cd); - } + core = create(cd, false); + } finally { + if (asyncSolrCoreLoad) { + solrCores.markCoreAsNotLoading(cd); } - try { - zkSys.registerInZk(core, true); - } catch (RuntimeException e) { - SolrException.log(log, "Error registering SolrCore", e); - } - return core; } + try { + zkSys.registerInZk(core, true); + } catch (RuntimeException e) { + SolrException.log(log, "Error registering SolrCore", e); + } + return core; })); } } diff --git a/solr/core/src/java/org/apache/solr/core/HdfsDirectoryFactory.java b/solr/core/src/java/org/apache/solr/core/HdfsDirectoryFactory.java index 9d6797a4fb8..77d7bd9b5f1 100644 --- a/solr/core/src/java/org/apache/solr/core/HdfsDirectoryFactory.java +++ b/solr/core/src/java/org/apache/solr/core/HdfsDirectoryFactory.java @@ -318,12 +318,7 @@ public class HdfsDirectoryFactory extends CachingDirectoryFactory implements Sol FileSystem fileSystem = null; try { // no need to close the fs, the cache will do it - fileSystem = tmpFsCache.get(path, new Callable() { - @Override - public FileSystem call() throws IOException { - return FileSystem.get(hdfsDirPath.toUri(), conf); - } - }); + fileSystem = tmpFsCache.get(path, () -> FileSystem.get(hdfsDirPath.toUri(), conf)); } catch (ExecutionException e) { throw new RuntimeException(e); } @@ -351,12 +346,7 @@ public class HdfsDirectoryFactory extends CachingDirectoryFactory implements Sol try { // no need to close the fs, the cache will do it - fileSystem = tmpFsCache.get(cacheValue.path, new Callable() { - @Override - public FileSystem call() throws IOException { - return FileSystem.get(new Path(cacheValue.path).toUri(), conf); - } - }); + fileSystem = tmpFsCache.get(cacheValue.path, () -> FileSystem.get(new Path(cacheValue.path).toUri(), conf)); } catch (ExecutionException e) { throw new RuntimeException(e); } @@ -487,12 +477,7 @@ public class HdfsDirectoryFactory extends CachingDirectoryFactory implements Sol final Configuration conf = getConf(); FileSystem fileSystem = null; try { - fileSystem = tmpFsCache.get(dataDir, new Callable() { - @Override - public FileSystem call() throws IOException { - return FileSystem.get(dataDirPath.toUri(), conf); - } - }); + fileSystem = tmpFsCache.get(dataDir, () -> FileSystem.get(dataDirPath.toUri(), conf)); } catch (ExecutionException e) { throw new RuntimeException(e); } diff --git a/solr/core/src/java/org/apache/solr/core/SolrCore.java b/solr/core/src/java/org/apache/solr/core/SolrCore.java index e511de054c8..3e10efeb6bd 100644 --- a/solr/core/src/java/org/apache/solr/core/SolrCore.java +++ b/solr/core/src/java/org/apache/solr/core/SolrCore.java @@ -735,12 +735,9 @@ public final class SolrCore implements SolrInfoMBean, Closeable { // cause the executor to stall so firstSearcher events won't fire // until after inform() has been called for all components. // searchExecutor must be single-threaded for this to work - searcherExecutor.submit(new Callable() { - @Override - public Void call() throws Exception { - latch.await(); - return null; - } + searcherExecutor.submit(() -> { + latch.await(); + return null; }); this.updateHandler = initUpdateHandler(updateHandler); @@ -854,14 +851,7 @@ public final class SolrCore implements SolrInfoMBean, Closeable { if (iwRef != null) { final IndexWriter iw = iwRef.get(); final SolrCore core = this; - newReaderCreator = new Callable() { - // this is used during a core reload - - @Override - public DirectoryReader call() throws Exception { - return indexReaderFactory.newReader(iw, core); - } - }; + newReaderCreator = () -> indexReaderFactory.newReader(iw, core); } } @@ -1779,57 +1769,48 @@ public final class SolrCore implements SolrInfoMBean, Closeable { // warm the new searcher based on the current searcher. // should this go before the other event handlers or after? if (currSearcher != null) { - future = searcherExecutor.submit(new Callable() { - @Override - public Object call() throws Exception { - try { - newSearcher.warm(currSearcher); - } catch (Throwable e) { - SolrException.log(log, e); - if (e instanceof Error) { - throw (Error) e; - } + future = searcherExecutor.submit(() -> { + try { + newSearcher.warm(currSearcher); + } catch (Throwable e) { + SolrException.log(log, e); + if (e instanceof Error) { + throw (Error) e; } - return null; } + return null; }); } if (currSearcher == null) { - future = searcherExecutor.submit(new Callable() { - @Override - public Object call() throws Exception { - try { - for (SolrEventListener listener : firstSearcherListeners) { - listener.newSearcher(newSearcher, null); - } - } catch (Throwable e) { - SolrException.log(log, null, e); - if (e instanceof Error) { - throw (Error) e; - } + future = searcherExecutor.submit(() -> { + try { + for (SolrEventListener listener : firstSearcherListeners) { + listener.newSearcher(newSearcher, null); + } + } catch (Throwable e) { + SolrException.log(log, null, e); + if (e instanceof Error) { + throw (Error) e; } - return null; } + return null; }); } if (currSearcher != null) { - future = searcherExecutor.submit(new Callable() { - @Override - public Object call() throws Exception { - try { - for (SolrEventListener listener : newSearcherListeners) { - listener.newSearcher(newSearcher, currSearcher); - } - } catch (Throwable e) { - SolrException.log(log, null, e); - if (e instanceof Error) { - throw (Error) e; - } + future = searcherExecutor.submit(() -> { + try { + for (SolrEventListener listener : newSearcherListeners) { + listener.newSearcher(newSearcher, currSearcher); + } + } catch (Throwable e) { + SolrException.log(log, null, e); + if (e instanceof Error) { + throw (Error) e; } - return null; } + return null; }); } diff --git a/solr/core/src/java/org/apache/solr/core/SolrCores.java b/solr/core/src/java/org/apache/solr/core/SolrCores.java index 86fab0d8223..af287b7b0fb 100644 --- a/solr/core/src/java/org/apache/solr/core/SolrCores.java +++ b/solr/core/src/java/org/apache/solr/core/SolrCores.java @@ -127,22 +127,19 @@ class SolrCores { new DefaultSolrThreadFactory("coreCloseExecutor")); try { for (SolrCore core : coreList) { - coreCloseExecutor.submit(new Callable() { - @Override - public SolrCore call() throws Exception { - MDCLoggingContext.setCore(core); - try { - core.close(); - } catch (Throwable e) { - SolrException.log(log, "Error shutting down core", e); - if (e instanceof Error) { - throw (Error) e; - } - } finally { - MDCLoggingContext.clear(); + coreCloseExecutor.submit(() -> { + MDCLoggingContext.setCore(core); + try { + core.close(); + } catch (Throwable e) { + SolrException.log(log, "Error shutting down core", e); + if (e instanceof Error) { + throw (Error) e; } - return core; + } finally { + MDCLoggingContext.clear(); } + return core; }); } } finally { diff --git a/solr/core/src/java/org/apache/solr/handler/CdcrReplicatorScheduler.java b/solr/core/src/java/org/apache/solr/handler/CdcrReplicatorScheduler.java index 7217abb9188..bb817e5df2a 100644 --- a/solr/core/src/java/org/apache/solr/handler/CdcrReplicatorScheduler.java +++ b/solr/core/src/java/org/apache/solr/handler/CdcrReplicatorScheduler.java @@ -73,19 +73,14 @@ class CdcrReplicatorScheduler { for (int i = 0; i < nCandidates; i++) { // a thread that poll one state from the queue, execute the replication task, and push back // the state in the queue when the task is completed - replicatorsPool.execute(new Runnable() { - - @Override - public void run() { - CdcrReplicatorState state = statesQueue.poll(); - assert state != null; // Should never happen - try { - new CdcrReplicator(state, batchSize).run(); - } finally { - statesQueue.offer(state); - } + replicatorsPool.execute(() -> { + CdcrReplicatorState state = statesQueue.poll(); + assert state != null; // Should never happen + try { + new CdcrReplicator(state, batchSize).run(); + } finally { + statesQueue.offer(state); } - }); } diff --git a/solr/core/src/java/org/apache/solr/handler/MoreLikeThisHandler.java b/solr/core/src/java/org/apache/solr/handler/MoreLikeThisHandler.java index 89b0180127b..a4d9ea081c9 100644 --- a/solr/core/src/java/org/apache/solr/handler/MoreLikeThisHandler.java +++ b/solr/core/src/java/org/apache/solr/handler/MoreLikeThisHandler.java @@ -284,17 +284,7 @@ public class MoreLikeThisHandler extends RequestHandlerBase { public Term term; public float boost; - - public static Comparator BOOST_ORDER = new Comparator() { - @Override - public int compare(InterestingTerm t1, InterestingTerm t2) { - float d = t1.boost - t2.boost; - if( d == 0 ) { - return 0; - } - return (d>0)?1:-1; - } - }; + } /** diff --git a/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandler.java b/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandler.java index 09dc1b862e1..4ae013c5681 100644 --- a/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandler.java +++ b/solr/core/src/java/org/apache/solr/handler/component/HttpShardHandler.java @@ -160,64 +160,61 @@ public class HttpShardHandler extends ShardHandler { // do this outside of the callable for thread safety reasons final List urls = getURLs(shard, preferredHostAddress); - Callable task = new Callable() { - @Override - public ShardResponse call() throws Exception { + Callable task = () -> { - ShardResponse srsp = new ShardResponse(); - if (sreq.nodeName != null) { - srsp.setNodeName(sreq.nodeName); - } - srsp.setShardRequest(sreq); - srsp.setShard(shard); - SimpleSolrResponse ssr = new SimpleSolrResponse(); - srsp.setSolrResponse(ssr); - long startTime = System.nanoTime(); - - try { - params.remove(CommonParams.WT); // use default (currently javabin) - params.remove(CommonParams.VERSION); - - QueryRequest req = makeQueryRequest(sreq, params, shard); - req.setMethod(SolrRequest.METHOD.POST); - - // no need to set the response parser as binary is the default - // req.setResponseParser(new BinaryResponseParser()); - - // if there are no shards available for a slice, urls.size()==0 - if (urls.size()==0) { - // TODO: what's the right error code here? We should use the same thing when - // all of the servers for a shard are down. - throw new SolrException(SolrException.ErrorCode.SERVICE_UNAVAILABLE, "no servers hosting shard: " + shard); - } - - if (urls.size() <= 1) { - String url = urls.get(0); - srsp.setShardAddress(url); - try (SolrClient client = new HttpSolrClient(url, httpClient)) { - ssr.nl = client.request(req); - } - } else { - LBHttpSolrClient.Rsp rsp = httpShardHandlerFactory.makeLoadBalancedRequest(req, urls); - ssr.nl = rsp.getResponse(); - srsp.setShardAddress(rsp.getServer()); - } - } - catch( ConnectException cex ) { - srsp.setException(cex); //???? - } catch (Exception th) { - srsp.setException(th); - if (th instanceof SolrException) { - srsp.setResponseCode(((SolrException)th).code()); - } else { - srsp.setResponseCode(-1); - } - } - - ssr.elapsedTime = TimeUnit.MILLISECONDS.convert(System.nanoTime() - startTime, TimeUnit.NANOSECONDS); - - return transfomResponse(sreq, srsp, shard); + ShardResponse srsp = new ShardResponse(); + if (sreq.nodeName != null) { + srsp.setNodeName(sreq.nodeName); } + srsp.setShardRequest(sreq); + srsp.setShard(shard); + SimpleSolrResponse ssr = new SimpleSolrResponse(); + srsp.setSolrResponse(ssr); + long startTime = System.nanoTime(); + + try { + params.remove(CommonParams.WT); // use default (currently javabin) + params.remove(CommonParams.VERSION); + + QueryRequest req = makeQueryRequest(sreq, params, shard); + req.setMethod(SolrRequest.METHOD.POST); + + // no need to set the response parser as binary is the default + // req.setResponseParser(new BinaryResponseParser()); + + // if there are no shards available for a slice, urls.size()==0 + if (urls.size()==0) { + // TODO: what's the right error code here? We should use the same thing when + // all of the servers for a shard are down. + throw new SolrException(SolrException.ErrorCode.SERVICE_UNAVAILABLE, "no servers hosting shard: " + shard); + } + + if (urls.size() <= 1) { + String url = urls.get(0); + srsp.setShardAddress(url); + try (SolrClient client = new HttpSolrClient(url, httpClient)) { + ssr.nl = client.request(req); + } + } else { + LBHttpSolrClient.Rsp rsp = httpShardHandlerFactory.makeLoadBalancedRequest(req, urls); + ssr.nl = rsp.getResponse(); + srsp.setShardAddress(rsp.getServer()); + } + } + catch( ConnectException cex ) { + srsp.setException(cex); //???? + } catch (Exception th) { + srsp.setException(th); + if (th instanceof SolrException) { + srsp.setResponseCode(((SolrException)th).code()); + } else { + srsp.setResponseCode(-1); + } + } + + ssr.elapsedTime = TimeUnit.MILLISECONDS.convert(System.nanoTime() - startTime, TimeUnit.NANOSECONDS); + + return transfomResponse(sreq, srsp, shard); }; try { diff --git a/solr/core/src/java/org/apache/solr/request/PerSegmentSingleValuedFaceting.java b/solr/core/src/java/org/apache/solr/request/PerSegmentSingleValuedFaceting.java index 62a41468a7a..2be8d1ec389 100644 --- a/solr/core/src/java/org/apache/solr/request/PerSegmentSingleValuedFaceting.java +++ b/solr/core/src/java/org/apache/solr/request/PerSegmentSingleValuedFaceting.java @@ -103,12 +103,9 @@ class PerSegmentSingleValuedFaceting { for (final LeafReaderContext leave : leaves) { final SegFacet segFacet = new SegFacet(leave); - Callable task = new Callable() { - @Override - public SegFacet call() throws Exception { - segFacet.countTerms(); - return segFacet; - } + Callable task = () -> { + segFacet.countTerms(); + return segFacet; }; // TODO: if limiting threads, submit by largest segment first? diff --git a/solr/core/src/java/org/apache/solr/request/SimpleFacets.java b/solr/core/src/java/org/apache/solr/request/SimpleFacets.java index a9ce11dcfde..3dd340328ff 100644 --- a/solr/core/src/java/org/apache/solr/request/SimpleFacets.java +++ b/solr/core/src/java/org/apache/solr/request/SimpleFacets.java @@ -662,26 +662,23 @@ public class SimpleFacets { final String termList = localParams == null ? null : localParams.get(CommonParams.TERMS); final String key = parsed.key; final String facetValue = parsed.facetValue; - Callable callable = new Callable() { - @Override - public NamedList call() throws Exception { - try { - NamedList result = new SimpleOrderedMap<>(); - if(termList != null) { - List terms = StrUtils.splitSmart(termList, ",", true); - result.add(key, getListedTermCounts(facetValue, parsed, terms)); - } else { - result.add(key, getTermCounts(facetValue, parsed)); - } - return result; - } catch (SolrException se) { - throw se; - } catch (Exception e) { - throw new SolrException(ErrorCode.SERVER_ERROR, - "Exception during facet.field: " + facetValue, e); - } finally { - semaphore.release(); + Callable callable = () -> { + try { + NamedList result = new SimpleOrderedMap<>(); + if(termList != null) { + List terms = StrUtils.splitSmart(termList, ",", true); + result.add(key, getListedTermCounts(facetValue, parsed, terms)); + } else { + result.add(key, getTermCounts(facetValue, parsed)); } + return result; + } catch (SolrException se) { + throw se; + } catch (Exception e) { + throw new SolrException(ErrorCode.SERVER_ERROR, + "Exception during facet.field: " + facetValue, e); + } finally { + semaphore.release(); } }; diff --git a/solr/core/src/java/org/apache/solr/schema/AbstractSpatialFieldType.java b/solr/core/src/java/org/apache/solr/schema/AbstractSpatialFieldType.java index 7addb20254d..39d45c22a08 100644 --- a/solr/core/src/java/org/apache/solr/schema/AbstractSpatialFieldType.java +++ b/solr/core/src/java/org/apache/solr/schema/AbstractSpatialFieldType.java @@ -407,12 +407,7 @@ public abstract class AbstractSpatialFieldType extend */ public T getStrategy(final String fieldName) { try { - return fieldStrategyCache.get(fieldName, new Callable() { - @Override - public T call() throws Exception { - return newSpatialStrategy(fieldName); - } - }); + return fieldStrategyCache.get(fieldName, () -> newSpatialStrategy(fieldName)); } catch (ExecutionException e) { throw Throwables.propagate(e.getCause()); } diff --git a/solr/core/src/java/org/apache/solr/update/SolrCmdDistributor.java b/solr/core/src/java/org/apache/solr/update/SolrCmdDistributor.java index a99952dbd57..9a196b7ca40 100644 --- a/solr/core/src/java/org/apache/solr/update/SolrCmdDistributor.java +++ b/solr/core/src/java/org/apache/solr/update/SolrCmdDistributor.java @@ -279,14 +279,9 @@ public class SolrCmdDistributor { // a commit using ConncurrentUpdateSolrServer is not async, // so we make it async to prevent commits from happening // serially across multiple nodes - pending.add(completionService.submit(new Callable() { - - @Override - public Object call() throws Exception { - doRequest(req); - return null; - } - + pending.add(completionService.submit(() -> { + doRequest(req); + return null; })); } else { doRequest(req); diff --git a/solr/core/src/test/org/apache/solr/cloud/BasicDistributedZkTest.java b/solr/core/src/test/org/apache/solr/cloud/BasicDistributedZkTest.java index 6eee7dbb947..b17f9b03f9b 100644 --- a/solr/core/src/test/org/apache/solr/cloud/BasicDistributedZkTest.java +++ b/solr/core/src/test/org/apache/solr/cloud/BasicDistributedZkTest.java @@ -960,35 +960,32 @@ public class BasicDistributedZkTest extends AbstractFullDistribZkTestBase { private void createSolrCore(final String collection, List collectionClients, final String baseUrl, final int num, final String shardId) { - Callable call = new Callable() { - @Override - public Object call() { - try (HttpSolrClient client = new HttpSolrClient(baseUrl)) { - // client.setConnectionTimeout(15000); - Create createCmd = new Create(); - createCmd.setRoles("none"); - createCmd.setCoreName(collection + num); - createCmd.setCollection(collection); - - if (random().nextBoolean()) { - // sometimes we use an explicit core node name - createCmd.setCoreNodeName("anode" + nodeCounter.incrementAndGet()); - } - - if (shardId == null) { - createCmd.setNumShards(2); - } - createCmd.setDataDir(getDataDir(createTempDir(collection).toFile().getAbsolutePath())); - if (shardId != null) { - createCmd.setShardId(shardId); - } - client.request(createCmd); - } catch (Exception e) { - e.printStackTrace(); - //fail + Callable call = () -> { + try (HttpSolrClient client = new HttpSolrClient(baseUrl)) { + // client.setConnectionTimeout(15000); + Create createCmd = new Create(); + createCmd.setRoles("none"); + createCmd.setCoreName(collection + num); + createCmd.setCollection(collection); + + if (random().nextBoolean()) { + // sometimes we use an explicit core node name + createCmd.setCoreNodeName("anode" + nodeCounter.incrementAndGet()); } - return null; + + if (shardId == null) { + createCmd.setNumShards(2); + } + createCmd.setDataDir(getDataDir(createTempDir(collection).toFile().getAbsolutePath())); + if (shardId != null) { + createCmd.setShardId(shardId); + } + client.request(createCmd); + } catch (Exception e) { + e.printStackTrace(); + //fail } + return null; }; pending.add(completionService.submit(call)); @@ -1091,23 +1088,20 @@ public class BasicDistributedZkTest extends AbstractFullDistribZkTestBase { ((HttpSolrClient) client).getBaseURL().length() - DEFAULT_COLLECTION.length() -1); final int frozeUnique = unique; - Callable call = new Callable() { - @Override - public Object call() { + Callable call = () -> { - try (HttpSolrClient client = new HttpSolrClient(baseUrl)) { - client.setConnectionTimeout(15000); - client.setSoTimeout(60000); - Create createCmd = new Create(); - createCmd.setCoreName(collection); - createCmd.setDataDir(getDataDir(createTempDir(collection).toFile().getAbsolutePath())); - client.request(createCmd); - } catch (Exception e) { - e.printStackTrace(); - //fails - } - return null; + try (HttpSolrClient client1 = new HttpSolrClient(baseUrl)) { + client1.setConnectionTimeout(15000); + client1.setSoTimeout(60000); + Create createCmd = new Create(); + createCmd.setCoreName(collection); + createCmd.setDataDir(getDataDir(createTempDir(collection).toFile().getAbsolutePath())); + client1.request(createCmd); + } catch (Exception e) { + e.printStackTrace(); + //fails } + return null; }; collectionClients.add(createNewSolrClient(collection, baseUrl)); diff --git a/solr/core/src/test/org/apache/solr/search/TestRecovery.java b/solr/core/src/test/org/apache/solr/search/TestRecovery.java index 307492f53b6..6439498dfeb 100644 --- a/solr/core/src/test/org/apache/solr/search/TestRecovery.java +++ b/solr/core/src/test/org/apache/solr/search/TestRecovery.java @@ -197,12 +197,7 @@ public class TestRecovery extends SolrTestCaseJ4 { } }; - UpdateLog.testing_logReplayFinishHook = new Runnable() { - @Override - public void run() { - logReplayFinish.release(); - } - }; + UpdateLog.testing_logReplayFinishHook = logReplayFinish::release; SolrQueryRequest req = req(); diff --git a/solr/core/src/test/org/apache/solr/search/TestRecoveryHdfs.java b/solr/core/src/test/org/apache/solr/search/TestRecoveryHdfs.java index d0d61be5bcf..7de7a2bcff4 100644 --- a/solr/core/src/test/org/apache/solr/search/TestRecoveryHdfs.java +++ b/solr/core/src/test/org/apache/solr/search/TestRecoveryHdfs.java @@ -161,7 +161,7 @@ public class TestRecoveryHdfs extends SolrTestCaseJ4 { } }; - UpdateLog.testing_logReplayFinishHook = () -> logReplayFinish.release(); + UpdateLog.testing_logReplayFinishHook = logReplayFinish::release; clearIndex(); @@ -257,7 +257,7 @@ public class TestRecoveryHdfs extends SolrTestCaseJ4 { } }; - UpdateLog.testing_logReplayFinishHook = () -> logReplayFinish.release(); + UpdateLog.testing_logReplayFinishHook = logReplayFinish::release; SolrQueryRequest req = req(); @@ -412,7 +412,7 @@ public class TestRecoveryHdfs extends SolrTestCaseJ4 { } }; - UpdateLog.testing_logReplayFinishHook = () -> logReplayFinish.release(); + UpdateLog.testing_logReplayFinishHook = logReplayFinish::release; SolrQueryRequest req = req(); @@ -722,12 +722,7 @@ public class TestRecoveryHdfs extends SolrTestCaseJ4 { } }; - UpdateLog.testing_logReplayFinishHook = new Runnable() { - @Override - public void run() { - logReplayFinish.release(); - } - }; + UpdateLog.testing_logReplayFinishHook = logReplayFinish::release; clearIndex(); diff --git a/solr/core/src/test/org/apache/solr/update/AddBlockUpdateTest.java b/solr/core/src/test/org/apache/solr/update/AddBlockUpdateTest.java index 3be8c2eba06..557e6b23032 100644 --- a/solr/core/src/test/org/apache/solr/update/AddBlockUpdateTest.java +++ b/solr/core/src/test/org/apache/solr/update/AddBlockUpdateTest.java @@ -575,22 +575,14 @@ public class AddBlockUpdateTest extends SolrTestCaseJ4 { for (Document block : blocks) { final String msg = block.asXML(); if (msg.length() > 0) { - rez.add(new Callable() { - @Override - public Void call() { - assertBlockU(msg); - return null; - } - + rez.add(() -> { + assertBlockU(msg); + return null; }); if (rarely()) { - rez.add(new Callable() { - @Override - public Void call() { - assertBlockU(commit()); - return null; - } - + rez.add(() -> { + assertBlockU(commit()); + return null; }); } } diff --git a/solr/core/src/test/org/apache/solr/update/TestUpdate.java b/solr/core/src/test/org/apache/solr/update/TestUpdate.java index 13a24797e83..6efa5bbaed3 100644 --- a/solr/core/src/test/org/apache/solr/update/TestUpdate.java +++ b/solr/core/src/test/org/apache/solr/update/TestUpdate.java @@ -37,20 +37,12 @@ public class TestUpdate extends SolrTestCaseJ4 { // Test both by running the same test with and without commits // do without commits - doUpdateTest(new Callable() { - @Override - public Object call() throws Exception { - return null; - } - }); + doUpdateTest(() -> null); // do with commits - doUpdateTest(new Callable() { - @Override - public Object call() throws Exception { - assertU(commit("softCommit","false")); - return null; - } + doUpdateTest(() -> { + assertU(commit("softCommit","false")); + return null; }); diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java index 43a4c183391..aefda459ffa 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java @@ -603,12 +603,7 @@ public class CloudSolrClient extends SolrClient { final LBHttpSolrClient.Req lbRequest = entry.getValue(); try { MDC.put("CloudSolrClient.url", url); - responseFutures.put(url, threadPool.submit(new Callable>() { - @Override - public NamedList call() throws Exception { - return lbClient.request(lbRequest).getResponse(); - } - })); + responseFutures.put(url, threadPool.submit(() -> lbClient.request(lbRequest).getResponse())); } finally { MDC.remove("CloudSolrClient.url"); } diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java index 29a56a65194..18038c2d0c6 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java @@ -285,12 +285,7 @@ public class HttpSolrClient extends SolrClient { ExecutorService pool = ExecutorUtil.newMDCAwareFixedThreadPool(1, new SolrjNamedThreadFactory("httpUriRequest")); try { MDC.put("HttpSolrClient.url", baseUrl); - mrr.future = pool.submit(new Callable>(){ - - @Override - public NamedList call() throws Exception { - return executeMethod(method, processor); - }}); + mrr.future = pool.submit(() -> executeMethod(method, processor)); } finally { pool.shutdown(); diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/Krb5HttpClientBuilder.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/Krb5HttpClientBuilder.java index 6c7c64f2cd1..f9cf513a758 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/Krb5HttpClientBuilder.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/Krb5HttpClientBuilder.java @@ -96,14 +96,11 @@ public class Krb5HttpClientBuilder { javax.security.auth.login.Configuration.setConfiguration(jaasConfig); //Enable only SPNEGO authentication scheme. - builder.setAuthSchemeRegistryProvider(new AuthSchemeRegistryProvider() { - @Override - public Lookup getAuthSchemeRegistry() { - Lookup authProviders = RegistryBuilder.create() - .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true, false)) - .build(); - return authProviders; - } + builder.setAuthSchemeRegistryProvider(() -> { + Lookup authProviders = RegistryBuilder.create() + .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true, false)) + .build(); + return authProviders; }); // Get the credentials from the JAAS configuration rather than here Credentials useJaasCreds = new Credentials() { @@ -117,26 +114,19 @@ public class Krb5HttpClientBuilder { HttpClientUtil.setCookiePolicy(SolrPortAwareCookieSpecFactory.POLICY_NAME); - builder.setCookieSpecRegistryProvider(new CookieSpecRegistryProvider() { - @Override - public Lookup getCookieSpecRegistry() { - SolrPortAwareCookieSpecFactory cookieFactory = new SolrPortAwareCookieSpecFactory(); + builder.setCookieSpecRegistryProvider(() -> { + SolrPortAwareCookieSpecFactory cookieFactory = new SolrPortAwareCookieSpecFactory(); - Lookup cookieRegistry = RegistryBuilder. create() - .register(SolrPortAwareCookieSpecFactory.POLICY_NAME, cookieFactory).build(); + Lookup cookieRegistry = RegistryBuilder. create() + .register(SolrPortAwareCookieSpecFactory.POLICY_NAME, cookieFactory).build(); - return cookieRegistry; - } + return cookieRegistry; }); - builder.setDefaultCredentialsProvider(new CredentialsProviderProvider() { - - @Override - public CredentialsProvider getCredentialsProvider() { - CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); - credentialsProvider.setCredentials(AuthScope.ANY, useJaasCreds); - return credentialsProvider; - } + builder.setDefaultCredentialsProvider(() -> { + CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + credentialsProvider.setCredentials(AuthScope.ANY, useJaasCreds); + return credentialsProvider; }); HttpClientUtil.addRequestInterceptor(bufferedEntityInterceptor); } @@ -146,15 +136,11 @@ public class Krb5HttpClientBuilder { } // Set a buffered entity based request interceptor - private HttpRequestInterceptor bufferedEntityInterceptor = new HttpRequestInterceptor() { - @Override - public void process(HttpRequest request, HttpContext context) throws HttpException, - IOException { - if(request instanceof HttpEntityEnclosingRequest) { - HttpEntityEnclosingRequest enclosingRequest = ((HttpEntityEnclosingRequest) request); - HttpEntity requestEntity = enclosingRequest.getEntity(); - enclosingRequest.setEntity(new BufferedHttpEntity(requestEntity)); - } + private HttpRequestInterceptor bufferedEntityInterceptor = (request, context) -> { + if(request instanceof HttpEntityEnclosingRequest) { + HttpEntityEnclosingRequest enclosingRequest = ((HttpEntityEnclosingRequest) request); + HttpEntity requestEntity = enclosingRequest.getEntity(); + enclosingRequest.setEntity(new BufferedHttpEntity(requestEntity)); } }; diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/SolrHttpClientBuilder.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/SolrHttpClientBuilder.java index 98217f81595..7b49ea18149 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/SolrHttpClientBuilder.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/SolrHttpClientBuilder.java @@ -35,19 +35,19 @@ public class SolrHttpClientBuilder { } public interface HttpRequestInterceptorProvider { - public HttpRequestInterceptor getHttpRequestInterceptor(); + HttpRequestInterceptor getHttpRequestInterceptor(); } public interface CredentialsProviderProvider { - public CredentialsProvider getCredentialsProvider(); + CredentialsProvider getCredentialsProvider(); } public interface AuthSchemeRegistryProvider { - public Lookup getAuthSchemeRegistry(); + Lookup getAuthSchemeRegistry(); } public interface CookieSpecRegistryProvider { - public Lookup getCookieSpecRegistry(); + Lookup getCookieSpecRegistry(); } private CookieSpecRegistryProvider cookieSpecRegistryProvider; diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/comp/FieldComparator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/comp/FieldComparator.java index c3a22891b19..10c7c294e6e 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/comp/FieldComparator.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/comp/FieldComparator.java @@ -97,34 +97,28 @@ public class FieldComparator implements StreamComparator { */ private void assignComparator(){ if(ComparatorOrder.DESCENDING == order){ - comparator = new ComparatorLambda() { - @Override - public int compare(Tuple leftTuple, Tuple rightTuple) { - Comparable leftComp = (Comparable)leftTuple.get(leftFieldName); - Comparable rightComp = (Comparable)rightTuple.get(rightFieldName); - - if(leftComp == rightComp){ return 0; } // if both null then they are equal. if both are same ref then are equal - if(null == leftComp){ return 1; } - if(null == rightComp){ return -1; } - - return rightComp.compareTo(leftComp); - } + comparator = (leftTuple, rightTuple) -> { + Comparable leftComp = (Comparable)leftTuple.get(leftFieldName); + Comparable rightComp = (Comparable)rightTuple.get(rightFieldName); + + if(leftComp == rightComp){ return 0; } // if both null then they are equal. if both are same ref then are equal + if(null == leftComp){ return 1; } + if(null == rightComp){ return -1; } + + return rightComp.compareTo(leftComp); }; } else{ // See above for black magic reasoning. - comparator = new ComparatorLambda() { - @Override - public int compare(Tuple leftTuple, Tuple rightTuple) { - Comparable leftComp = (Comparable)leftTuple.get(leftFieldName); - Comparable rightComp = (Comparable)rightTuple.get(rightFieldName); - - if(leftComp == rightComp){ return 0; } // if both null then they are equal. if both are same ref then are equal - if(null == leftComp){ return -1; } - if(null == rightComp){ return 1; } - - return leftComp.compareTo(rightComp); - } + comparator = (leftTuple, rightTuple) -> { + Comparable leftComp = (Comparable)leftTuple.get(leftFieldName); + Comparable rightComp = (Comparable)rightTuple.get(rightFieldName); + + if(leftComp == rightComp){ return 0; } // if both null then they are equal. if both are same ref then are equal + if(null == leftComp){ return -1; } + if(null == rightComp){ return 1; } + + return leftComp.compareTo(rightComp); }; } } diff --git a/solr/test-framework/src/java/org/apache/solr/cloud/MiniSolrCloudCluster.java b/solr/test-framework/src/java/org/apache/solr/cloud/MiniSolrCloudCluster.java index 002304173cc..118f7bef621 100644 --- a/solr/test-framework/src/java/org/apache/solr/cloud/MiniSolrCloudCluster.java +++ b/solr/test-framework/src/java/org/apache/solr/cloud/MiniSolrCloudCluster.java @@ -206,12 +206,7 @@ public class MiniSolrCloudCluster { List> startups = new ArrayList<>(numServers); for (int i = 0; i < numServers; ++i) { - startups.add(new Callable() { - @Override - public JettySolrRunner call() throws Exception { - return startJettySolrRunner(newNodeName(), jettyConfig.context, jettyConfig); - } - }); + startups.add(() -> startJettySolrRunner(newNodeName(), jettyConfig.context, jettyConfig)); } Collection> futures = executor.invokeAll(startups); @@ -424,12 +419,7 @@ public class MiniSolrCloudCluster { solrClient.close(); List> shutdowns = new ArrayList<>(jettys.size()); for (final JettySolrRunner jetty : jettys) { - shutdowns.add(new Callable() { - @Override - public JettySolrRunner call() throws Exception { - return stopJettySolrRunner(jetty); - } - }); + shutdowns.add(() -> stopJettySolrRunner(jetty)); } jettys.clear(); Collection> futures = executor.invokeAll(shutdowns);