You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by re...@apache.org on 2025/03/04 09:56:09 UTC

(jackrabbit-oak) branch OAK-11500 created (now 4b8a67c6c2)

This is an automated email from the ASF dual-hosted git repository.

reschke pushed a change to branch OAK-11500
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git


      at 4b8a67c6c2 Revert "OAK-11500: fix change in MongoDocumentStore"

This branch includes the following new commits:

     new c6c9ccf9d1  OAK-11500: Remove usage of Guava io.Closeable  (#2129)
     new 91ce7801d5 OAK-11500: fix change in MongoDocumentStore
     new 4b8a67c6c2 Revert "OAK-11500: fix change in MongoDocumentStore"

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(jackrabbit-oak) 01/03: OAK-11500: Remove usage of Guava io.Closeable (#2129)

Posted by re...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

reschke pushed a commit to branch OAK-11500
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git

commit c6c9ccf9d1db5032a80b5cc8900e6e5915a3d78d
Author: Julian Reschke <re...@apache.org>
AuthorDate: Tue Mar 4 07:41:51 2025 +0100

     OAK-11500: Remove usage of Guava io.Closeable  (#2129)
    
    * OAK-11500: Remove usage of Guava io.Closeable
    
    * OAK-11500: Remove usage of Guava io.Closeable
---
 .../blob/AbstractSharedCachingDataStore.java       |  3 +-
 .../jackrabbit/oak/plugins/blob/FileCache.java     |  9 +----
 .../plugins/blob/MarkSweepGarbageCollector.java    | 25 +++++++++-----
 .../plugins/blob/datastore/DataStoreBlobStore.java | 24 ++++++-------
 .../oak/plugins/blob/datastore/FSBackend.java      | 10 +++---
 .../plugins/blob/datastore/OakFileDataStore.java   | 10 +++---
 .../oak/plugins/blob/UploadStagingCacheTest.java   | 11 ++----
 .../datastore/BlobIdTrackerClusterSharedTest.java  |  5 ++-
 .../apache/jackrabbit/oak/commons/FileIOUtils.java | 25 +++-----------
 .../ActiveDeletedBlobCollectorFactory.java         |  7 ++--
 .../jackrabbit/oak/run/DataStoreCheckCommand.java  | 17 ++--------
 .../jackrabbit/oak/run/DataStoreCommand.java       |  9 +++--
 .../oak/segment/DefaultSegmentWriter.java          | 10 ++++--
 .../plugins/document/mongo/MongoDocumentStore.java |  3 +-
 .../oak/plugins/document/MongoBlobGCTest.java      | 39 +++++++---------------
 15 files changed, 82 insertions(+), 125 deletions(-)

diff --git a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/AbstractSharedCachingDataStore.java b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/AbstractSharedCachingDataStore.java
index 5bb8a907ca..dcd57525b4 100644
--- a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/AbstractSharedCachingDataStore.java
+++ b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/AbstractSharedCachingDataStore.java
@@ -58,7 +58,6 @@ import org.slf4j.LoggerFactory;
 
 import org.apache.jackrabbit.guava.common.base.Stopwatch;
 import org.apache.jackrabbit.guava.common.collect.Iterators;
-import org.apache.jackrabbit.guava.common.io.Closeables;
 import org.apache.jackrabbit.guava.common.util.concurrent.ListeningExecutorService;
 
 /**
@@ -335,7 +334,7 @@ public abstract class AbstractSharedCachingDataStore extends AbstractDataStore
                         copyInputStreamToFile(in, tmpFile);
                         return new LazyFileInputStream(tmpFile);
                     } finally {
-                        Closeables.close(in, false);
+                        in.close();
                     }
                 } else {
                     return new FileInputStream(cached);
diff --git a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/FileCache.java b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/FileCache.java
index 88db2ebd5f..847d41d0f1 100644
--- a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/FileCache.java
+++ b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/FileCache.java
@@ -49,7 +49,6 @@ import org.slf4j.LoggerFactory;
 
 import org.apache.jackrabbit.guava.common.base.Stopwatch;
 import org.apache.jackrabbit.guava.common.cache.AbstractCache;
-import org.apache.jackrabbit.guava.common.io.Closeables;
 
 /**
  */
@@ -110,18 +109,12 @@ public class FileCache extends AbstractCache<String, File> implements Closeable
                 if (cachedFile.exists()) {
                     return cachedFile;
                 } else {
-                    InputStream is = null;
-                    boolean threw = true;
                     long startNanos = System.nanoTime();
-                    try {
-                        is = loader.load(key);
+                    try (InputStream is = loader.load(key))  {
                         copyInputStreamToFile(is, cachedFile);
-                        threw = false;
                     } catch (Exception e) {
                         LOG.warn("Error reading object for id [{}] from backend", key, e);
                         throw e;
-                    } finally {
-                        Closeables.close(is, threw);
                     }
                     if (LOG.isDebugEnabled()) {
                         LOG.debug("Loaded file: {} in {}", key, (System.nanoTime() - startNanos) / 1_000_000);
diff --git a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/MarkSweepGarbageCollector.java b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/MarkSweepGarbageCollector.java
index 578377ded5..5fe7f6bea0 100644
--- a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/MarkSweepGarbageCollector.java
+++ b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/MarkSweepGarbageCollector.java
@@ -58,7 +58,6 @@ import org.apache.commons.collections4.ListValuedMap;
 import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
 import org.apache.jackrabbit.guava.common.base.Stopwatch;
 import org.apache.jackrabbit.guava.common.collect.Iterators;
-import org.apache.jackrabbit.guava.common.io.Closeables;
 import org.apache.jackrabbit.guava.common.util.concurrent.ListenableFutureTask;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.LineIterator;
@@ -303,14 +302,10 @@ public class MarkSweepGarbageCollector implements BlobGarbageCollector {
                             stat.setStartTime(markers.get(uniqueSessionId).getLastModified());
                         }
 
-                        LineNumberReader reader = null;
-                        try {
-                            reader = new LineNumberReader(new InputStreamReader(refRec.getStream()));
+                        try (LineNumberReader reader = new LineNumberReader(new InputStreamReader(refRec.getStream()))) {
                             while (reader.readLine() != null) {
                             }
                             stat.setNumLines(reader.getLineNumber());
-                        } finally {
-                            Closeables.close(reader, true);
                         }
                     }
                 }
@@ -380,7 +375,13 @@ public class MarkSweepGarbageCollector implements BlobGarbageCollector {
         } finally {
             statsCollector.updateDuration(sw.elapsed(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS);
             if (!LOG.isTraceEnabled() && !traceOutput) {
-                Closeables.close(fs, threw);
+                try {
+                    fs.close();
+                } catch (IOException ioe) {
+                    if (!threw) {
+                        throw ioe;
+                    }
+                }
             }
         }
     }
@@ -769,7 +770,13 @@ public class MarkSweepGarbageCollector implements BlobGarbageCollector {
             }
         } finally {
             if (!traceOutput && (!LOG.isTraceEnabled() && candidates == 0)) {
-                Closeables.close(fs, threw);
+                try {
+                    fs.close();
+                } catch (IOException ioe) {
+                    if (!threw) {
+                        throw ioe;
+                    }
+                }
             }
             sw.stop();
             consistencyStatsCollector.updateDuration(sw.elapsed(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS);
@@ -1091,7 +1098,7 @@ public class MarkSweepGarbageCollector implements BlobGarbageCollector {
             } finally {
                 if (idsIter instanceof Closeable) {
                     try {
-                        Closeables.close((Closeable) idsIter, false);
+                        ((Closeable)idsIter).close();
                     } catch (Exception e) {
                         LOG.debug("Error closing iterator");
                     }
diff --git a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/DataStoreBlobStore.java b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/DataStoreBlobStore.java
index 240e519ca6..24aa6b0b65 100644
--- a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/DataStoreBlobStore.java
+++ b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/DataStoreBlobStore.java
@@ -79,7 +79,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import org.apache.jackrabbit.guava.common.collect.Iterators;
-import org.apache.jackrabbit.guava.common.io.Closeables;
 
 /**
  * BlobStore wrapper for DataStore. Wraps Jackrabbit 2 DataStore and expose them as BlobStores
@@ -341,9 +340,15 @@ public class DataStoreBlobStore
             stats.uploadFailed();
             throw new IOException(e);
         } finally {
-            //DataStore does not closes the stream internally
+            //DataStore does not close the stream internally
             //So close the stream explicitly
-            Closeables.close(stream, threw);
+            try {
+                stream.close();
+            } catch (IOException ioe) {
+                if (!threw) {
+                    throw ioe;
+                }
+            }
         }
     }
 
@@ -364,15 +369,10 @@ public class DataStoreBlobStore
         //This is inefficient as repeated calls for same blobId would involve opening new Stream
         //instead clients should directly access the stream from DataRecord by special casing for
         //BlobStore which implements DataStore
-        InputStream stream = getInputStream(encodedBlobId);
-        boolean threw = true;
-        try {
+        try (InputStream stream = getInputStream(encodedBlobId)) {
             IOUtils.skipFully(stream, pos);
             int readCount = stream.read(buff, off, length);
-            threw = false;
             return readCount;
-        } finally {
-            Closeables.close(stream, threw);
         }
     }
 
@@ -439,13 +439,9 @@ public class DataStoreBlobStore
                     @Override
                     public byte[] call() throws Exception {
                         boolean threw = true;
-                        InputStream stream = getStream(blobId.blobId);
-                        try {
+                        try (InputStream stream = getStream(blobId.blobId)) {
                             byte[] result = IOUtils.toByteArray(stream);
-                            threw = false;
                             return result;
-                        } finally {
-                            Closeables.close(stream, threw);
                         }
                     }
                 });
diff --git a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/FSBackend.java b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/FSBackend.java
index 7822b115b4..6ebb1fecd5 100644
--- a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/FSBackend.java
+++ b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/FSBackend.java
@@ -30,7 +30,6 @@ import java.util.List;
 import java.util.Properties;
 
 import org.apache.commons.lang3.StringUtils;
-import org.apache.jackrabbit.guava.common.io.Closeables;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.io.filefilter.FileFilterUtils;
@@ -189,12 +188,13 @@ public class FSBackend extends AbstractSharedBackend {
 
         try {
             File file = new File(fsPathDir, name);
-            FileOutputStream os = new FileOutputStream(file);
-            try {
+            try (FileOutputStream os = new FileOutputStream(file)) {
                 IOUtils.copyLarge(input, os);
             } finally {
-                Closeables.close(os, true);
-                Closeables.close(input, true);
+                try {
+                    input.close();
+                } catch (IOException swallowed) {
+                }
             }
         } catch (IOException e) {
             LOG.error("Exception while adding metadata record with name {}, {}",
diff --git a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/OakFileDataStore.java b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/OakFileDataStore.java
index 7ad09d04c1..1cddd3713c 100644
--- a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/OakFileDataStore.java
+++ b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/OakFileDataStore.java
@@ -33,7 +33,6 @@ import java.util.List;
 import java.util.Set;
 
 import org.apache.commons.lang3.StringUtils;
-import org.apache.jackrabbit.guava.common.io.Closeables;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.io.filefilter.FileFilterUtils;
@@ -139,12 +138,13 @@ public class OakFileDataStore extends FileDataStore implements SharedDataStore {
 
         try {
             File file = new File(getPath(), name);
-            FileOutputStream os = new FileOutputStream(file);
-            try {
+            try (FileOutputStream os = new FileOutputStream(file)) {
                 IOUtils.copyLarge(input, os);
             } finally {
-                Closeables.close(os, true);
-                Closeables.close(input, true);
+                try {
+                    input.close();
+                } catch (IOException swallowed) {
+                }
             }
         } catch (IOException e) {
             LOG.error("Exception while adding metadata record with name {}, {}",
diff --git a/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/UploadStagingCacheTest.java b/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/UploadStagingCacheTest.java
index 77ffbf5b21..82748f1e81 100644
--- a/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/UploadStagingCacheTest.java
+++ b/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/UploadStagingCacheTest.java
@@ -42,7 +42,6 @@ import java.util.concurrent.atomic.AtomicInteger;
 import ch.qos.logback.classic.Level;
 
 import org.apache.jackrabbit.guava.common.collect.Iterators;
-import org.apache.jackrabbit.guava.common.io.Closeables;
 import org.apache.jackrabbit.guava.common.io.Closer;
 import org.apache.jackrabbit.guava.common.util.concurrent.Futures;
 import org.apache.jackrabbit.guava.common.util.concurrent.ListenableFuture;
@@ -693,7 +692,7 @@ public class UploadStagingCacheTest extends AbstractDataStoreCacheTest {
         // Create pre-upgrade load
         File home = folder.newFolder();
         File pendingUploadsFile = new File(home, DataStoreCacheUpgradeUtils.UPLOAD_MAP);
-        createGibberishLoad(home, pendingUploadsFile);
+        createGibberishLoad(pendingUploadsFile);
 
         LogCustomizer lc = LogCustomizer.forLogger(DataStoreCacheUpgradeUtils.class.getName())
             .filter(Level.WARN)
@@ -717,13 +716,9 @@ public class UploadStagingCacheTest extends AbstractDataStoreCacheTest {
     }
 
 
-    private void createGibberishLoad(File home, File pendingUploadFile) throws IOException {
-        BufferedWriter writer = null;
-        try {
-            writer = new BufferedWriter(new FileWriter(pendingUploadFile, StandardCharsets.UTF_8));
+    private void createGibberishLoad(File pendingUploadFile) throws IOException {
+        try (BufferedWriter writer = new BufferedWriter(new FileWriter(pendingUploadFile, StandardCharsets.UTF_8))) {
             FileIOUtils.writeAsLine(writer, "jerhgiuheirghoeoorqehgsjlwjpfkkwpkf", false);
-        } finally {
-            Closeables.close(writer, true);
         }
     }
 
diff --git a/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/datastore/BlobIdTrackerClusterSharedTest.java b/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/datastore/BlobIdTrackerClusterSharedTest.java
index 28bc17d3af..4ac3dac862 100644
--- a/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/datastore/BlobIdTrackerClusterSharedTest.java
+++ b/oak-blob-plugins/src/test/java/org/apache/jackrabbit/oak/plugins/blob/datastore/BlobIdTrackerClusterSharedTest.java
@@ -41,7 +41,6 @@ import org.junit.rules.TemporaryFolder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import static org.apache.jackrabbit.guava.common.io.Closeables.close;
 import static java.lang.String.valueOf;
 import static java.util.UUID.randomUUID;
 import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
@@ -224,11 +223,11 @@ public class BlobIdTrackerClusterSharedTest {
         Set<String> retrieved = new HashSet<>();
         Iterator<String> iter = tracker.get();
         log.info("retrieving blob ids");
-        while(iter.hasNext()) {
+        while (iter.hasNext()) {
             retrieved.add(iter.next());
         }
         if (iter instanceof Closeable) {
-            close((Closeable)iter, true);
+            ((Closeable)iter).close();
         }
         return retrieved;
     }
diff --git a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/FileIOUtils.java b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/FileIOUtils.java
index b4a9e6a3c0..6f9a120d5a 100644
--- a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/FileIOUtils.java
+++ b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/FileIOUtils.java
@@ -20,7 +20,6 @@ import static java.io.File.createTempFile;
 import static java.nio.charset.StandardCharsets.UTF_8;
 import static org.apache.commons.io.FileUtils.forceDelete;
 import static org.apache.commons.io.IOUtils.copyLarge;
-import static org.apache.jackrabbit.guava.common.io.Closeables.close;
 import static org.apache.jackrabbit.oak.commons.sort.EscapeUtils.escapeLineBreak;
 import static org.apache.jackrabbit.oak.commons.sort.EscapeUtils.unescapeLineBreaks;
 import static org.apache.jackrabbit.oak.commons.sort.ExternalSort.mergeSortedFiles;
@@ -140,25 +139,20 @@ public final class FileIOUtils {
      * @throws IOException
      */
     public static void append(List<File> files, File appendTo, boolean delete) throws IOException {
-        OutputStream appendStream = null;
-        boolean threw = true;
 
-        try {
-            appendStream = new BufferedOutputStream(new FileOutputStream(appendTo, true));
+        try (OutputStream appendStream = new BufferedOutputStream(new FileOutputStream(appendTo, true))) {
 
             for (File f : files) {
                 try (InputStream iStream = new FileInputStream(f)) {
                     copyLarge(iStream, appendStream);
                 }
             }
-            threw = false;
         } finally {
             if (delete) {
                 for (File f : files) {
                     f.delete();
                 }
             }
-            close(appendStream, threw);
         }
     }
 
@@ -227,11 +221,9 @@ public final class FileIOUtils {
      */
     public static int writeStrings(Iterator<String> iterator, File f, boolean escape,
         @NotNull Function<String, String> transformer, @Nullable Logger logger, @Nullable String message) throws IOException {
-        BufferedWriter writer = new BufferedWriter(new FileWriter(f, UTF_8));
-        boolean threw = true;
 
         int count = 0;
-        try {
+        try (BufferedWriter writer = new BufferedWriter(new FileWriter(f, UTF_8))) {
             while (iterator.hasNext()) {
                 writeAsLine(writer, transformer.apply(iterator.next()), escape);
                 count++;
@@ -241,9 +233,6 @@ public final class FileIOUtils {
                     }
                 }
             }
-            threw = false;
-        } finally {
-            close(writer, threw);
         }
         return count;
     }
@@ -257,13 +246,10 @@ public final class FileIOUtils {
      * @throws IOException
      */
     public static Set<String> readStringsAsSet(InputStream stream, boolean unescape) throws IOException {
-        BufferedReader reader = null;
         Set<String> set = new HashSet<>();
-        boolean threw = true;
 
-        try {
-            reader = new BufferedReader(new InputStreamReader(stream, UTF_8));
-            String line  = null;
+        try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream, UTF_8))) {
+            String line;
             while ((line = reader.readLine()) != null) {
                 if (unescape) {
                     set.add(unescapeLineBreaks(line));
@@ -271,9 +257,6 @@ public final class FileIOUtils {
                     set.add(line);
                 }
             }
-            threw = false;
-        } finally {
-            close(reader, threw);
         }
         return set;
     }
diff --git a/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/directory/ActiveDeletedBlobCollectorFactory.java b/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/directory/ActiveDeletedBlobCollectorFactory.java
index 92ee7e34cc..323c2056aa 100644
--- a/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/directory/ActiveDeletedBlobCollectorFactory.java
+++ b/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/directory/ActiveDeletedBlobCollectorFactory.java
@@ -38,7 +38,6 @@ import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 
-import org.apache.jackrabbit.guava.common.io.Closeables;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.LineIterator;
 import org.apache.commons.io.filefilter.IOFileFilter;
@@ -324,7 +323,11 @@ public class ActiveDeletedBlobCollectorFactory {
             long startBlobTrackerSyncTime = clock.getTime();
             // Synchronize deleted blob ids with the blob id tracker
             try {
-                Closeables.close(idTempDeleteWriter, true);
+                try {
+                    idTempDeleteWriter.close();
+                } catch (IOException ex) {
+                    LOG.warn("IOException thrown while closing idTempDeleteWriter", ex);
+                }
 
                 if (blobIdsTracked && numBlobsDeleted > 0) {
                     BlobTracker tracker = ((BlobTrackingStore) blobStore).getTracker();
diff --git a/oak-run/src/main/java/org/apache/jackrabbit/oak/run/DataStoreCheckCommand.java b/oak-run/src/main/java/org/apache/jackrabbit/oak/run/DataStoreCheckCommand.java
index 389b9bb351..1d55c0c768 100644
--- a/oak-run/src/main/java/org/apache/jackrabbit/oak/run/DataStoreCheckCommand.java
+++ b/oak-run/src/main/java/org/apache/jackrabbit/oak/run/DataStoreCheckCommand.java
@@ -17,7 +17,6 @@
 package org.apache.jackrabbit.oak.run;
 
 import static org.apache.jackrabbit.guava.common.base.Stopwatch.createStarted;
-import static org.apache.jackrabbit.guava.common.io.Closeables.close;
 import static java.io.File.createTempFile;
 import static java.util.Arrays.asList;
 import static org.apache.commons.io.FileUtils.forceDelete;
@@ -47,7 +46,6 @@ import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.jackrabbit.guava.common.base.Splitter;
 import org.apache.jackrabbit.guava.common.base.Stopwatch;
-import org.apache.jackrabbit.guava.common.io.Closeables;
 import org.apache.jackrabbit.guava.common.io.Closer;
 import com.mongodb.MongoClient;
 import com.mongodb.MongoClientURI;
@@ -426,10 +424,8 @@ public class DataStoreCheckCommand implements Command {
 
     private static void retrieveBlobReferences(GarbageCollectableBlobStore blobStore, BlobReferenceRetriever marker,
         File marked, String dsType, boolean isVerbose) throws IOException {
-        final BufferedWriter writer = new BufferedWriter(new FileWriter(marked, StandardCharsets.UTF_8));
         final AtomicInteger count = new AtomicInteger();
-        boolean threw = true;
-        try {
+        try (BufferedWriter writer = new BufferedWriter(new FileWriter(marked, StandardCharsets.UTF_8))) {
             final GarbageCollectableBlobStore finalBlobStore = blobStore;
 
             System.out.println("Starting dump of blob references");
@@ -464,9 +460,6 @@ public class DataStoreCheckCommand implements Command {
 
             System.out.println(count.get() + " blob references found");
             System.out.println("Finished in " + watch.elapsed(TimeUnit.SECONDS) + " seconds");
-            threw = false;
-        } finally {
-            close(writer, threw);
         }
     }
 
@@ -543,14 +536,11 @@ public class DataStoreCheckCommand implements Command {
         }
 
         public void traverse(String ... paths) throws IOException {
-            BufferedWriter writer = null;
             final AtomicInteger count = new AtomicInteger();
-            boolean threw = true;
             System.out.println("Starting dump of blob references by traversing");
             Stopwatch watch = createStarted();
 
-            try {
-                writer = new BufferedWriter(new FileWriter(references, StandardCharsets.UTF_8));
+            try (BufferedWriter writer = new BufferedWriter(new FileWriter(references, StandardCharsets.UTF_8))) {
                 if (paths.length == 0) {
                     traverseChildren(nodeStore.getRoot(), "/", writer, count);
                 } else {
@@ -570,9 +560,6 @@ public class DataStoreCheckCommand implements Command {
 
                 System.out.println(count.get() + " blob references found");
                 System.out.println("Finished in " + watch.elapsed(TimeUnit.SECONDS) + " seconds");
-                threw = false;
-            } finally {
-                Closeables.close(writer, threw);
             }
         }
 
diff --git a/oak-run/src/main/java/org/apache/jackrabbit/oak/run/DataStoreCommand.java b/oak-run/src/main/java/org/apache/jackrabbit/oak/run/DataStoreCommand.java
index 1ead8f7cc7..225e6589a5 100644
--- a/oak-run/src/main/java/org/apache/jackrabbit/oak/run/DataStoreCommand.java
+++ b/oak-run/src/main/java/org/apache/jackrabbit/oak/run/DataStoreCommand.java
@@ -41,7 +41,6 @@ import java.util.stream.StreamSupport;
 
 import org.apache.jackrabbit.guava.common.base.Splitter;
 import org.apache.jackrabbit.guava.common.base.Stopwatch;
-import org.apache.jackrabbit.guava.common.io.Closeables;
 import org.apache.jackrabbit.guava.common.io.Closer;
 import joptsimple.OptionParser;
 import org.apache.commons.io.FileUtils;
@@ -239,7 +238,13 @@ public class DataStoreCommand implements Command {
 
                     FileUtils.copyFile(referencesTemp, references);
                 } finally {
-                    Closeables.close(writer, threw);
+                    try {
+                        writer.close();
+                    } catch (IOException ex) {
+                        if (!threw) {
+                            throw ex;
+                        }
+                    }
                 }
             } else if (dataStoreOpts.dumpIds()) {
                 log.info("Initiating dump of data store IDs");
diff --git a/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/DefaultSegmentWriter.java b/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/DefaultSegmentWriter.java
index 259e16dcf1..0b070810ae 100644
--- a/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/DefaultSegmentWriter.java
+++ b/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/DefaultSegmentWriter.java
@@ -55,7 +55,6 @@ import java.util.Map;
 import javax.jcr.PropertyType;
 
 import org.apache.commons.io.IOUtils;
-import org.apache.jackrabbit.guava.common.io.Closeables;
 import org.apache.jackrabbit.oak.api.Blob;
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.api.Type;
@@ -645,7 +644,14 @@ public class DefaultSegmentWriter implements SegmentWriter {
                 threw = false;
                 return id;
             } finally {
-                Closeables.close(stream, threw);
+                try {
+                    stream.close();
+                } catch (IOException ex) {
+                    if (!threw) {
+                        throw ex;
+                    }
+                    LOG.warn("IOException thrown while closing stream", ex);
+                }
             }
         }
 
diff --git a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
index a91470d0ed..8bd3a48e12 100644
--- a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
+++ b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
@@ -44,7 +44,6 @@ import java.util.stream.StreamSupport;
 import org.apache.jackrabbit.guava.common.base.Stopwatch;
 import org.apache.jackrabbit.guava.common.collect.Iterables;
 import org.apache.jackrabbit.guava.common.collect.Iterators;
-import org.apache.jackrabbit.guava.common.io.Closeables;
 import org.apache.jackrabbit.guava.common.util.concurrent.AtomicDouble;
 import com.mongodb.Block;
 import com.mongodb.DBObject;
@@ -2030,7 +2029,7 @@ public class MongoDocumentStore implements DocumentStore {
             clusterNodesConnection.close();
         }
         try {
-            Closeables.close(throttlingMetricsUpdater, false);
+            throttlingMetricsUpdater.close();
         } catch (IOException e) {
             LOG.warn("Error occurred while closing throttlingMetricsUpdater", e);
         }
diff --git a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoBlobGCTest.java b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoBlobGCTest.java
index 8c5b97018e..aa4a58ff6f 100644
--- a/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoBlobGCTest.java
+++ b/oak-store-document/src/test/java/org/apache/jackrabbit/oak/plugins/document/MongoBlobGCTest.java
@@ -36,7 +36,6 @@ import ch.qos.logback.classic.Level;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.jackrabbit.guava.common.base.Splitter;
 import org.apache.jackrabbit.guava.common.base.Stopwatch;
-import org.apache.jackrabbit.guava.common.io.Closeables;
 import com.mongodb.BasicDBObject;
 import com.mongodb.ReadPreference;
 import com.mongodb.client.MongoCollection;
@@ -398,26 +397,18 @@ public class MongoBlobGCTest extends AbstractMongoConnectionTest {
     }
 
     private static void assertBlobReferences(Set<String> expected, String rootFolder) throws IOException {
-        InputStream is = null;
-        try {
-            is = new FileInputStream(getMarkedFile(rootFolder));
+        try (InputStream is = new FileInputStream(getMarkedFile(rootFolder))) {
             Set<String> records = FileIOUtils.readStringsAsSet(is, true);
             assertEquals(expected, records);
-        } finally {
-            Closeables.close(is, false);
         }
     }
 
     private static void assertBlobReferenceRecords(int expected, String rootFolder) throws IOException {
-        InputStream is = null;
-        try {
-            is = new FileInputStream(getMarkedFile(rootFolder));
+        try (InputStream is = new FileInputStream(getMarkedFile(rootFolder))) {
             Set<String> records = FileIOUtils.readStringsAsSet(is, true);
             for (String rec : records) {
                 assertEquals(expected, Splitter.on(",").omitEmptyStrings().splitToList(rec).size());
             }
-        } finally {
-            Closeables.close(is, false);
         }
     }
 
@@ -509,42 +500,36 @@ public class MongoBlobGCTest extends AbstractMongoConnectionTest {
             this.maxLastModifiedInterval = maxLastModifiedInterval;
             this.additionalBlobs = new HashSet<>();
         }
-        
+
         @Override
         protected void markAndSweep(boolean markOnly, boolean forceBlobRetrieve) throws Exception {
-            boolean threw = true;
-            GarbageCollectorFileState fs = new GarbageCollectorFileState(root);
-            try {
+
+            try (GarbageCollectorFileState fs = new GarbageCollectorFileState(root)) {
                 Stopwatch sw = Stopwatch.createStarted();
                 LOG.info("Starting Test Blob garbage collection");
-                
+
                 // Sleep a little more than the max interval to get over the interval for valid blobs
                 Thread.sleep(maxLastModifiedInterval + 1000);
                 LOG.info("Slept {} to make blobs old", maxLastModifiedInterval + 1000);
-                
+
                 long markStart = System.currentTimeMillis();
                 mark(fs);
                 LOG.info("Mark finished");
-                
+
                 additionalBlobs = createAdditional();
-    
+
                 if (!markOnly) {
                     Thread.sleep(maxLastModifiedInterval + 100);
                     LOG.info("Slept {} to make additional blobs old", maxLastModifiedInterval + 100);
-    
+
                     long deleteCount = sweep(fs, markStart, forceBlobRetrieve);
-                    threw = false;
-            
+
                     LOG.info("Blob garbage collection completed in {}. Number of blobs deleted [{}]", sw.toString(),
                         deleteCount, maxLastModifiedInterval);
                 }
-            } finally {
-                if (!LOG.isTraceEnabled()) {
-                    Closeables.close(fs, threw);
-                }
             }
         }
-    
+
         public HashSet<String> createAdditional() throws Exception {
             HashSet<String> blobSet = new HashSet<String>();
             DocumentNodeStore s = mk.getNodeStore();


(jackrabbit-oak) 03/03: Revert "OAK-11500: fix change in MongoDocumentStore"

Posted by re...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

reschke pushed a commit to branch OAK-11500
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git

commit 4b8a67c6c2e6a3934674cfef0c71a15ca8acd603
Author: Julian Reschke <re...@apache.org>
AuthorDate: Tue Mar 4 10:54:29 2025 +0100

    Revert "OAK-11500: fix change in MongoDocumentStore"
    
    This reverts commit 91ce7801d57c81a71c1c5dc10ca2adbd6d641970.
---
 .../oak/plugins/blob/AbstractSharedCachingDataStore.java       | 10 +++++++---
 .../oak/plugins/document/mongo/MongoDocumentStore.java         |  4 +---
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/AbstractSharedCachingDataStore.java b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/AbstractSharedCachingDataStore.java
index d70f847b43..dcd57525b4 100644
--- a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/AbstractSharedCachingDataStore.java
+++ b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/AbstractSharedCachingDataStore.java
@@ -326,11 +326,15 @@ public abstract class AbstractSharedCachingDataStore extends AbstractDataStore
             try {
                 // If cache configured to 0 will return null
                 if (cached == null || !cached.exists()) {
-                    TransientFileFactory fileFactory = TransientFileFactory.getInstance();
-                    File tmpFile = fileFactory.createTransientFile("temp0cache", null, temp);
-                    try (InputStream in = backend.getRecord(getIdentifier()).getStream()) {
+                    InputStream in = null;
+                    try {
+                        TransientFileFactory fileFactory = TransientFileFactory.getInstance();
+                        File tmpFile = fileFactory.createTransientFile("temp0cache", null, temp);
+                        in = backend.getRecord(getIdentifier()).getStream();
                         copyInputStreamToFile(in, tmpFile);
                         return new LazyFileInputStream(tmpFile);
+                    } finally {
+                        in.close();
                     }
                 } else {
                     return new FileInputStream(cached);
diff --git a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
index 37781ba318..8bd3a48e12 100644
--- a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
+++ b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
@@ -41,7 +41,6 @@ import java.util.function.Function;
 import java.util.stream.Collectors;
 import java.util.stream.StreamSupport;
 
-import org.apache.commons.io.IOUtils;
 import org.apache.jackrabbit.guava.common.base.Stopwatch;
 import org.apache.jackrabbit.guava.common.collect.Iterables;
 import org.apache.jackrabbit.guava.common.collect.Iterators;
@@ -2029,9 +2028,8 @@ public class MongoDocumentStore implements DocumentStore {
         if (clusterNodesConnection != connection) {
             clusterNodesConnection.close();
         }
-
         try {
-            IOUtils.close(throttlingMetricsUpdater);
+            throttlingMetricsUpdater.close();
         } catch (IOException e) {
             LOG.warn("Error occurred while closing throttlingMetricsUpdater", e);
         }


(jackrabbit-oak) 02/03: OAK-11500: fix change in MongoDocumentStore

Posted by re...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

reschke pushed a commit to branch OAK-11500
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git

commit 91ce7801d57c81a71c1c5dc10ca2adbd6d641970
Author: Julian Reschke <re...@apache.org>
AuthorDate: Tue Mar 4 10:53:44 2025 +0100

    OAK-11500: fix change in MongoDocumentStore
---
 .../oak/plugins/blob/AbstractSharedCachingDataStore.java       | 10 +++-------
 .../oak/plugins/document/mongo/MongoDocumentStore.java         |  4 +++-
 2 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/AbstractSharedCachingDataStore.java b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/AbstractSharedCachingDataStore.java
index dcd57525b4..d70f847b43 100644
--- a/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/AbstractSharedCachingDataStore.java
+++ b/oak-blob-plugins/src/main/java/org/apache/jackrabbit/oak/plugins/blob/AbstractSharedCachingDataStore.java
@@ -326,15 +326,11 @@ public abstract class AbstractSharedCachingDataStore extends AbstractDataStore
             try {
                 // If cache configured to 0 will return null
                 if (cached == null || !cached.exists()) {
-                    InputStream in = null;
-                    try {
-                        TransientFileFactory fileFactory = TransientFileFactory.getInstance();
-                        File tmpFile = fileFactory.createTransientFile("temp0cache", null, temp);
-                        in = backend.getRecord(getIdentifier()).getStream();
+                    TransientFileFactory fileFactory = TransientFileFactory.getInstance();
+                    File tmpFile = fileFactory.createTransientFile("temp0cache", null, temp);
+                    try (InputStream in = backend.getRecord(getIdentifier()).getStream()) {
                         copyInputStreamToFile(in, tmpFile);
                         return new LazyFileInputStream(tmpFile);
-                    } finally {
-                        in.close();
                     }
                 } else {
                     return new FileInputStream(cached);
diff --git a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
index 8bd3a48e12..37781ba318 100644
--- a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
+++ b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java
@@ -41,6 +41,7 @@ import java.util.function.Function;
 import java.util.stream.Collectors;
 import java.util.stream.StreamSupport;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.jackrabbit.guava.common.base.Stopwatch;
 import org.apache.jackrabbit.guava.common.collect.Iterables;
 import org.apache.jackrabbit.guava.common.collect.Iterators;
@@ -2028,8 +2029,9 @@ public class MongoDocumentStore implements DocumentStore {
         if (clusterNodesConnection != connection) {
             clusterNodesConnection.close();
         }
+
         try {
-            throttlingMetricsUpdater.close();
+            IOUtils.close(throttlingMetricsUpdater);
         } catch (IOException e) {
             LOG.warn("Error occurred while closing throttlingMetricsUpdater", e);
         }