Skip to content

Apache Lucene open-source search software

SOLR-8357: UpdateLog.RecentUpdates implements Closeable

Alan Woodward30f941068af5
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 438e6633498..b745e640ba1 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -215,6 +215,8 @@ Other Changes
* SOLR-8333: Several API tweaks so that public APIs were no longer refering to private classes
(ehatcher, Shawn Heisey, hossman)
+* SOLR-8357: UpdateLog.RecentUpdates now implements Closeable (Alan Woodward)
+
================== 5.4.0 ==================
Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release
diff --git a/solr/core/src/java/org/apache/solr/cloud/ElectionContext.java b/solr/core/src/java/org/apache/solr/cloud/ElectionContext.java
index a81b4d65ce8..26d78e54ea5 100644
--- a/solr/core/src/java/org/apache/solr/cloud/ElectionContext.java
+++ b/solr/core/src/java/org/apache/solr/cloud/ElectionContext.java
@@ -356,11 +356,8 @@ final class ShardLeaderElectionContext extends ShardLeaderElectionContextBase {
boolean hasRecentUpdates = false;
if (ulog != null) {
// TODO: we could optimize this if necessary
- UpdateLog.RecentUpdates recentUpdates = ulog.getRecentUpdates();
- try {
+ try (UpdateLog.RecentUpdates recentUpdates = ulog.getRecentUpdates()) {
hasRecentUpdates = !recentUpdates.getVersions(1).isEmpty();
- } finally {
- recentUpdates.close();
}
}
diff --git a/solr/core/src/java/org/apache/solr/cloud/RecoveryStrategy.java b/solr/core/src/java/org/apache/solr/cloud/RecoveryStrategy.java
index 507a8330119..b57038df27f 100644
--- a/solr/core/src/java/org/apache/solr/cloud/RecoveryStrategy.java
+++ b/solr/core/src/java/org/apache/solr/cloud/RecoveryStrategy.java
@@ -254,17 +254,11 @@ public class RecoveryStrategy extends Thread implements ClosableThread {
boolean firstTime = true;
List<Long> recentVersions;
- UpdateLog.RecentUpdates recentUpdates = null;
- try {
- recentUpdates = ulog.getRecentUpdates();
+ try (UpdateLog.RecentUpdates recentUpdates = ulog.getRecentUpdates()) {
recentVersions = recentUpdates.getVersions(ulog.getNumRecordsToKeep());
} catch (Exception e) {
SolrException.log(log, "Corrupt tlog - ignoring.", e);
recentVersions = new ArrayList<>(0);
- } finally {
- if (recentUpdates != null) {
- recentUpdates.close();
- }
}
List<Long> startingVersions = ulog.getStartingVersions();
diff --git a/solr/core/src/java/org/apache/solr/handler/CdcrRequestHandler.java b/solr/core/src/java/org/apache/solr/handler/CdcrRequestHandler.java
index e7c27b20d28..a9fef59057e 100644
--- a/solr/core/src/java/org/apache/solr/handler/CdcrRequestHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/CdcrRequestHandler.java
@@ -17,6 +17,18 @@ package org.apache.solr.handler;
* limitations under the License.
*/
+import java.io.IOException;
+import java.lang.invoke.MethodHandles;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
@@ -45,14 +57,6 @@ import org.apache.solr.util.plugin.SolrCoreAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.io.IOException;
-import java.lang.invoke.MethodHandles;
-import java.util.*;
-import java.util.concurrent.Callable;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Future;
-
/**
* <p>
* This request handler implements the CDCR API and is responsible of the execution of the
@@ -406,11 +410,11 @@ public class CdcrRequestHandler extends RequestHandlerBase implements SolrCoreAw
}
UpdateLog ulog = core.getUpdateHandler().getUpdateLog();
- UpdateLog.RecentUpdates recentUpdates = ulog.getRecentUpdates();
- List<Long> versions = recentUpdates.getVersions(1);
- long lastVersion = versions.isEmpty() ? -1 : Math.abs(versions.get(0));
- rsp.add(CdcrParams.CHECKPOINT, lastVersion);
- recentUpdates.close();
+ try (UpdateLog.RecentUpdates recentUpdates = ulog.getRecentUpdates()) {
+ List<Long> versions = recentUpdates.getVersions(1);
+ long lastVersion = versions.isEmpty() ? -1 : Math.abs(versions.get(0));
+ rsp.add(CdcrParams.CHECKPOINT, lastVersion);
+ }
}
private void handleEnableBufferAction(SolrQueryRequest req, SolrQueryResponse rsp) {
diff --git a/solr/core/src/java/org/apache/solr/handler/component/RealTimeGetComponent.java b/solr/core/src/java/org/apache/solr/handler/component/RealTimeGetComponent.java
index 8fe0de114b7..9af3f0f495d 100644
--- a/solr/core/src/java/org/apache/solr/handler/component/RealTimeGetComponent.java
+++ b/solr/core/src/java/org/apache/solr/handler/component/RealTimeGetComponent.java
@@ -547,11 +547,8 @@ public class RealTimeGetComponent extends SearchComponent
UpdateLog ulog = req.getCore().getUpdateHandler().getUpdateLog();
if (ulog == null) return;
- UpdateLog.RecentUpdates recentUpdates = ulog.getRecentUpdates();
- try {
+ try (UpdateLog.RecentUpdates recentUpdates = ulog.getRecentUpdates()) {
rb.rsp.add("versions", recentUpdates.getVersions(nVersions));
- } finally {
- recentUpdates.close(); // cache this somehow?
}
}
@@ -604,8 +601,7 @@ public class RealTimeGetComponent extends SearchComponent
long minVersion = Long.MAX_VALUE;
// TODO: get this from cache instead of rebuilding?
- UpdateLog.RecentUpdates recentUpdates = ulog.getRecentUpdates();
- try {
+ try (UpdateLog.RecentUpdates recentUpdates = ulog.getRecentUpdates()) {
for (String versionStr : versions) {
long version = Long.parseLong(versionStr);
try {
@@ -615,7 +611,7 @@ public class RealTimeGetComponent extends SearchComponent
if (version > 0) {
minVersion = Math.min(minVersion, version);
}
-
+
// TODO: do any kind of validation here?
updates.add(o);
@@ -626,12 +622,10 @@ public class RealTimeGetComponent extends SearchComponent
// Must return all delete-by-query commands that occur after the first add requested
// since they may apply.
- updates.addAll( recentUpdates.getDeleteByQuery(minVersion));
+ updates.addAll(recentUpdates.getDeleteByQuery(minVersion));
rb.rsp.add("updates", updates);
- } finally {
- recentUpdates.close(); // cache this somehow?
}
}
diff --git a/solr/core/src/java/org/apache/solr/update/HdfsUpdateLog.java b/solr/core/src/java/org/apache/solr/update/HdfsUpdateLog.java
index 114e55ce076..ff972dbf499 100644
--- a/solr/core/src/java/org/apache/solr/update/HdfsUpdateLog.java
+++ b/solr/core/src/java/org/apache/solr/update/HdfsUpdateLog.java
@@ -240,18 +240,17 @@ public class HdfsUpdateLog extends UpdateLog {
// TODO: these startingVersions assume that we successfully recover from all
// non-complete tlogs.
- HdfsUpdateLog.RecentUpdates startingUpdates = getRecentUpdates();
- try {
+ try (RecentUpdates startingUpdates = getRecentUpdates()) {
startingVersions = startingUpdates.getVersions(getNumRecordsToKeep());
startingOperation = startingUpdates.getLatestOperation();
-
+
// populate recent deletes list (since we can't get that info from the
// index)
for (int i = startingUpdates.deleteList.size() - 1; i >= 0; i--) {
DeleteUpdate du = startingUpdates.deleteList.get(i);
oldDeletes.put(new BytesRef(du.id), new LogPtr(-1, du.version));
}
-
+
// populate recent deleteByQuery commands
for (int i = startingUpdates.deleteByQueryList.size() - 1; i >= 0; i--) {
Update update = startingUpdates.deleteByQueryList.get(i);
@@ -260,9 +259,7 @@ public class HdfsUpdateLog extends UpdateLog {
String q = (String) dbq.get(2);
trackDeleteByQuery(q, version);
}
-
- } finally {
- startingUpdates.close();
+
}
}
diff --git a/solr/core/src/java/org/apache/solr/update/PeerSync.java b/solr/core/src/java/org/apache/solr/update/PeerSync.java
index 5161c2e2d1e..2154a1715d5 100644
--- a/solr/core/src/java/org/apache/solr/update/PeerSync.java
+++ b/solr/core/src/java/org/apache/solr/update/PeerSync.java
@@ -17,9 +17,6 @@
package org.apache.solr.update;
-import static org.apache.solr.update.processor.DistributedUpdateProcessor.DistribPhase.FROMLEADER;
-import static org.apache.solr.update.processor.DistributingUpdateProcessorFactory.DISTRIB_UPDATE_PARAM;
-
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.net.ConnectException;
@@ -56,6 +53,9 @@ import org.apache.solr.update.processor.UpdateRequestProcessorChain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import static org.apache.solr.update.processor.DistributedUpdateProcessor.DistribPhase.FROMLEADER;
+import static org.apache.solr.update.processor.DistributingUpdateProcessorFactory.DISTRIB_UPDATE_PARAM;
+
/** @lucene.experimental */
public class PeerSync {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
@@ -70,7 +70,6 @@ public class PeerSync {
private HttpShardHandlerFactory shardHandlerFactory;
private ShardHandler shardHandler;
- private UpdateLog.RecentUpdates recentUpdates;
private List<Long> startingVersions;
private List<Long> ourUpdates;
@@ -203,12 +202,9 @@ public class PeerSync {
for (String replica : replicas) {
requestVersions(replica);
}
-
- recentUpdates = ulog.getRecentUpdates();
- try {
+
+ try (UpdateLog.RecentUpdates recentUpdates = ulog.getRecentUpdates()) {
ourUpdates = recentUpdates.getVersions(nUpdates);
- } finally {
- recentUpdates.close();
}
Collections.sort(ourUpdates, absComparator);
diff --git a/solr/core/src/java/org/apache/solr/update/UpdateLog.java b/solr/core/src/java/org/apache/solr/update/UpdateLog.java
index 36fde60752a..cadeb722a60 100644
--- a/solr/core/src/java/org/apache/solr/update/UpdateLog.java
+++ b/solr/core/src/java/org/apache/solr/update/UpdateLog.java
@@ -17,6 +17,7 @@
package org.apache.solr.update;
+import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
@@ -312,19 +313,18 @@ public class UpdateLog implements PluginInfoInitialized {
}
// TODO: these startingVersions assume that we successfully recover from all non-complete tlogs.
- UpdateLog.RecentUpdates startingUpdates = getRecentUpdates();
- try {
+ try (RecentUpdates startingUpdates = getRecentUpdates()) {
startingVersions = startingUpdates.getVersions(numRecordsToKeep);
startingOperation = startingUpdates.getLatestOperation();
// populate recent deletes list (since we can't get that info from the index)
- for (int i=startingUpdates.deleteList.size()-1; i>=0; i--) {
+ for (int i = startingUpdates.deleteList.size() - 1; i >= 0; i--) {
DeleteUpdate du = startingUpdates.deleteList.get(i);
- oldDeletes.put(new BytesRef(du.id), new LogPtr(-1,du.version));
+ oldDeletes.put(new BytesRef(du.id), new LogPtr(-1, du.version));
}
// populate recent deleteByQuery commands
- for (int i=startingUpdates.deleteByQueryList.size()-1; i>=0; i--) {
+ for (int i = startingUpdates.deleteByQueryList.size() - 1; i >= 0; i--) {
Update update = startingUpdates.deleteByQueryList.get(i);
List<Object> dbq = (List<Object>) update.log.lookup(update.pointer);
long version = (Long) dbq.get(1);
@@ -332,8 +332,6 @@ public class UpdateLog implements PluginInfoInitialized {
trackDeleteByQuery(q, version);
}
- } finally {
- startingUpdates.close();
}
}
@@ -931,16 +929,32 @@ public class UpdateLog implements PluginInfoInitialized {
}
}
- public class RecentUpdates {
- Deque<TransactionLog> logList; // newest first
+ public class RecentUpdates implements Closeable {
+
+ final Deque<TransactionLog> logList; // newest first
List<List<Update>> updateList;
HashMap<Long, Update> updates;
List<Update> deleteByQueryList;
List<DeleteUpdate> deleteList;
int latestOperation;
+ public RecentUpdates(Deque<TransactionLog> logList) {
+ this.logList = logList;
+ boolean success = false;
+ try {
+ update();
+ success = true;
+ } finally {
+ // defensive: if some unknown exception is thrown,
+ // make sure we close so that the tlogs are decref'd
+ if (!success) {
+ close();
+ }
+ }
+ }
+
public List<Long> getVersions(int n) {
- List<Long> ret = new ArrayList(n);
+ List<Long> ret = new ArrayList<>(n);
for (List<Update> singleList : updateList) {
for (Update ptr : singleList) {
@@ -1055,6 +1069,7 @@ public class UpdateLog implements PluginInfoInitialized {
}
+ @Override
public void close() {
for (TransactionLog log : logList) {
log.decref();
@@ -1091,23 +1106,8 @@ public class UpdateLog implements PluginInfoInitialized {
// TODO: what if I hand out a list of updates, then do an update, then hand out another list (and
// one of the updates I originally handed out fell off the list). Over-request?
+ return new RecentUpdates(logList);
- boolean success = false;
- RecentUpdates recentUpdates = null;
- try {
- recentUpdates = new RecentUpdates();
- recentUpdates.logList = logList;
- recentUpdates.update();
- success = true;
- } finally {
- // defensive: if some unknown exception is thrown,
- // make sure we close so that the tlogs are decref'd
- if (!success && recentUpdates != null) {
- recentUpdates.close();
- }
- }
-
- return recentUpdates;
}
public void bufferUpdates() {
@@ -1575,9 +1575,7 @@ public class UpdateLog implements PluginInfoInitialized {
Long highestVersion = null;
final RTimer timer = new RTimer();
- RecentUpdates recentUpdates = null;
- try {
- recentUpdates = getRecentUpdates();
+ try (RecentUpdates recentUpdates = getRecentUpdates()) {
long maxVersionFromRecent = recentUpdates.getMaxRecentVersion();
long maxVersionFromIndex = versions.getMaxVersionFromIndex(newSearcher);
@@ -1593,9 +1591,6 @@ public class UpdateLog implements PluginInfoInitialized {
highestVersion = maxVersion;
} catch (IOException ioExc) {
log.warn("Failed to determine the max value of the version field due to: "+ioExc, ioExc);
- } finally {
- if (recentUpdates != null)
- recentUpdates.close();
}
log.info("Took {}ms to seed version buckets with highest version {}",
diff --git a/solr/core/src/test/org/apache/solr/search/TestRTGBase.java b/solr/core/src/test/org/apache/solr/search/TestRTGBase.java
index ef6eb1dafa4..13ecb32e9a7 100644
--- a/solr/core/src/test/org/apache/solr/search/TestRTGBase.java
+++ b/solr/core/src/test/org/apache/solr/search/TestRTGBase.java
@@ -17,9 +17,16 @@
package org.apache.solr.search;
-import org.apache.lucene.index.PostingsEnum;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+import java.util.concurrent.ConcurrentHashMap;
+
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.MultiFields;
+import org.apache.lucene.index.PostingsEnum;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.Terms;
import org.apache.lucene.index.TermsEnum;
@@ -28,13 +35,6 @@ import org.apache.lucene.util.BytesRef;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.update.UpdateLog;
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Random;
-import java.util.concurrent.ConcurrentHashMap;
-
import static org.apache.solr.update.processor.DistributedUpdateProcessor.DistribPhase;
import static org.apache.solr.update.processor.DistributingUpdateProcessorFactory.DISTRIB_UPDATE_PARAM;
@@ -112,14 +112,9 @@ public class TestRTGBase extends SolrTestCaseJ4 {
protected List<Long> getLatestVersions() {
- List<Long> recentVersions;
- UpdateLog.RecentUpdates startingRecentUpdates = h.getCore().getUpdateHandler().getUpdateLog().getRecentUpdates();
- try {
- recentVersions = startingRecentUpdates.getVersions(100);
- } finally {
- startingRecentUpdates.close();
+ try (UpdateLog.RecentUpdates startingRecentUpdates = h.getCore().getUpdateHandler().getUpdateLog().getRecentUpdates()) {
+ return startingRecentUpdates.getVersions(100);
}
- return recentVersions;
}