This is an automated email from the ASF dual-hosted git repository.
joewitt pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi.git
The following commit(s) were added to refs/heads/main by this push:
new 172afac NIFI-8633: This closes #5104. When reading a Content/Resource
Claim from FileSystemRepository, avoid the unnecessary Files.exists call and
instead just create a FileInputStream, catching FileNotFoundException
172afac is described below
commit 172afac6ab32350caf6f882e6e7c18e86a5c8c82
Author: Mark Payne <[email protected]>
AuthorDate: Wed May 26 12:46:27 2021 -0400
NIFI-8633: This closes #5104. When reading a Content/Resource Claim from
FileSystemRepository, avoid the unnecessary Files.exists call and instead just
create a FileInputStream, catching FileNotFoundException
Signed-off-by: Joe Witt <[email protected]>
---
.../repository/FileSystemRepository.java | 62 +++++++++++++++++-----
1 file changed, 50 insertions(+), 12 deletions(-)
diff --git
a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/repository/FileSystemRepository.java
b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/repository/FileSystemRepository.java
index 71e7d6c..c6b0586 100644
---
a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/repository/FileSystemRepository.java
+++
b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/repository/FileSystemRepository.java
@@ -16,7 +16,6 @@
*/
package org.apache.nifi.controller.repository;
-import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.nifi.controller.repository.claim.ContentClaim;
import org.apache.nifi.controller.repository.claim.ResourceClaim;
@@ -36,9 +35,11 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
+import java.io.Closeable;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
+import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -552,11 +553,6 @@ public class FileSystemRepository implements
ContentRepository {
return
containerPath.resolve(resourceClaim.getSection()).resolve(resourceClaim.getId());
}
- public Path getPath(final ResourceClaim resourceClaim, final boolean
verifyExists) throws ContentNotFoundException {
- final ContentClaim contentClaim = new
StandardContentClaim(resourceClaim, 0L);
- return getPath(contentClaim, verifyExists);
- }
-
public Path getPath(final ContentClaim claim, final boolean verifyExists)
throws ContentNotFoundException {
final ResourceClaim resourceClaim = claim.getResourceClaim();
final Path containerPath =
containers.get(resourceClaim.getContainer());
@@ -582,6 +578,35 @@ public class FileSystemRepository implements
ContentRepository {
return resolvedPath;
}
+ private InputStream getInputStream(final ResourceClaim resourceClaim) {
+ final ContentClaim contentClaim = new
StandardContentClaim(resourceClaim, 0L);
+ return getInputStream(contentClaim);
+ }
+
+ private InputStream getInputStream(final ContentClaim claim) {
+ final ResourceClaim resourceClaim = claim.getResourceClaim();
+ final Path containerPath =
containers.get(resourceClaim.getContainer());
+ if (containerPath == null) {
+ throw new ContentNotFoundException(claim);
+ }
+
+ // Create the Path that points to the data
+ final Path resolvedPath =
containerPath.resolve(resourceClaim.getSection()).resolve(resourceClaim.getId());
+
+ try {
+ return new FileInputStream(resolvedPath.toFile());
+ } catch (final FileNotFoundException fnfe) {
+ // If this occurs, we will also check the archive directory.
+ }
+
+ final Path archivePath = getArchivePath(resourceClaim);
+ try {
+ return new FileInputStream(archivePath.toFile());
+ } catch (final FileNotFoundException fnfe) {
+ throw new ContentNotFoundException(claim, fnfe);
+ }
+ }
+
@Override
public ContentClaim create(final boolean lossTolerant) throws IOException {
ResourceClaim resourceClaim;
@@ -885,9 +910,7 @@ public class FileSystemRepository implements
ContentRepository {
return new ByteArrayInputStream(new byte[0]);
}
- final Path path = getPath(claim, true);
- final FileInputStream fis = new FileInputStream(path.toFile());
- return fis;
+ return getInputStream(claim);
}
@Override
@@ -895,12 +918,15 @@ public class FileSystemRepository implements
ContentRepository {
if (claim == null) {
return new ByteArrayInputStream(new byte[0]);
}
- final Path path = getPath(claim, true);
- final FileInputStream fis = new FileInputStream(path.toFile());
+
+ final InputStream fis = getInputStream(claim);
if (claim.getOffset() > 0L) {
try {
StreamUtils.skip(fis, claim.getOffset());
} catch (final EOFException eof) {
+ closeQuietly(fis);
+
+ final Path path = getPath(claim, false);
final long resourceClaimBytes;
try {
resourceClaimBytes = Files.size(path);
@@ -911,7 +937,7 @@ public class FileSystemRepository implements
ContentRepository {
throw new ContentNotFoundException(claim, "Content Claim has
an offset of " + claim.getOffset() + " but Resource Claim " + path + " is only
" + resourceClaimBytes + " bytes");
} catch (final IOException ioe) {
- IOUtils.closeQuietly(fis);
+ closeQuietly(fis);
throw ioe;
}
}
@@ -931,6 +957,18 @@ public class FileSystemRepository implements
ContentRepository {
}
}
+ private void closeQuietly(final Closeable closeable) {
+ if (closeable == null) {
+ return;
+ }
+
+ try {
+ closeable.close();
+ } catch (final IOException ioe) {
+ LOG.warn("Failed to close {}", closeable, ioe);
+ }
+ }
+
@Override
public OutputStream write(final ContentClaim claim) throws IOException {
return write(claim, false);