diff --git a/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java b/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java index bd646909440c6c136bb457791a703d4dbda764b6..9c0c45ef97b41c465bff9f2637304e6dec56ebeb 100644 --- a/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java +++ b/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java @@ -50,10 +50,10 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; /** * SLUBStoragePlugin @@ -117,6 +117,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { if (null != blocksizeStr) { try { this.blocksize = Integer.parseInt(blocksizeStr); + log.info("Set blocksize to " + this.blocksize); } catch (NumberFormatException e) { log.error("Could not convert BLOCK_SIZE string to int, " + e.getMessage()); } @@ -172,14 +173,18 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { private void copyStreamFast(InputStream is, OutputStream os) throws IOException{ if ((is instanceof FileInputStream) && (os instanceof FileOutputStream) ) { + log.info("copyStreamFast, transferFrom() will be used"); /* use very fast java.nio.channels.FileChannel.transferFrom() function if both are Filestreams */ FileInputStream fis = (FileInputStream) is; FileOutputStream fos = (FileOutputStream) os; FileChannel sourceChannel = fis.getChannel(); FileChannel destChannel = fos.getChannel(); destChannel.transferFrom(sourceChannel, 0, sourceChannel.size()); + sourceChannel.close(); + destChannel.close(); } else { /* otherwise use blockwise copy loop */ + log.info("copyStreamFast, blockwise copy will be used"); int blocksize = getBlockSize(); assert (blocksize > 0); byte[] buffer = new byte[blocksize]; @@ -233,12 +238,12 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { } return res; */ - boolean res = Boolean.TRUE; + boolean res = true; try { for (Fixity f : fixities) { res &= checkSpecificFixity(absolutePath, f); } - } catch (Exception e) { + } catch (NoSuchAlgorithmException e) { return false; } return res; @@ -314,6 +319,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { String oldValue = fixity.getValue(); //log.debug("SLUBStoragePlugin.checkFixityByBuiltin() getAlgorithm (2)=" + algorithm); //log.debug("SLUBStoragePlugin.checkFixityByBuiltin() oldvalue=" + oldValue); + log.info("SLUBStoragePlugin.checkFixityByBuiltin(), begin checksum calc for pathname='" + absolute_storedEntityIdentifier.filestring() + "'" + " algorithm=" + algorithm); long starttime = System.currentTimeMillis(); String storedEntityIdentifier = relativePath(absolute_storedEntityIdentifier); //log.debug("SLUBStoragePlugin.checkFixityByBuiltin() calculated storedEntityIdentifier=" + storedEntityIdentifier); @@ -327,7 +333,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { boolean result = fixity.getResult(); is.close(); long endtime = System.currentTimeMillis(); - log.info("SLUBStoragePlugin.checkFixityByBuiltin() pathname='" + absolute_storedEntityIdentifier.filestring() + "'" + log.info("SLUBStoragePlugin.checkFixityByBuiltin(), end checksum calc for pathname='" + absolute_storedEntityIdentifier.filestring() + "'" + " algorithm=" + algorithm + " oldvalue=" + oldValue + " newvalue=" + newValue @@ -371,8 +377,9 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { log.error("SLUBStoragePlugin.checkFixityByPlugin() fallback table has no plugin name entry for algorithm '" + algorithm + "'"); } } + String oldValue = fixity.getValue(); - log.info("SLUBStoragePlugin.checkFixityByPlugin() oldvalue=" + oldValue); + log.info("SLUBStoragePlugin.checkFixityByPlugin(), begin checksum calc for pathname='" + absolute_storedEntityIdentifier.filestring() + "'"+ " plugin=" + pluginname); long starttime = System.currentTimeMillis(); String newValue = "should not occur"; try { @@ -387,7 +394,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { fixity.setResult((oldValue == null) || (oldValue.equals(newValue))); boolean result = fixity.getResult(); long endtime = System.currentTimeMillis(); - log.info("SLUBStoragePlugin.checkFixityByPlugin() pathname='" + absolute_storedEntityIdentifier.filestring() + "'" + log.info("SLUBStoragePlugin.checkFixityByPlugin(), end checksum calc for pathname='" + absolute_storedEntityIdentifier.filestring() + "'" + " plugin=" + pluginname + " oldvalue=" + oldValue + " newvalue=" + newValue @@ -461,7 +468,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { contractAssertIsRelativePath(storedEntityIdentifier); var absolute_filename = getFullFilePath(storedEntityIdentifier); var absolute_path = Paths.get(absolute_filename); - var is = new FileInputStream( absolute_path.toString() ); + var is = java.nio.file.Files.newInputStream( absolute_path ); return new BufferedInputStream( is , getBlockSize()); } @@ -478,7 +485,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { log.info("SLUBStoragePlugin.retrieveEntitybyRange() with storedEntityIdentifier='" + storedEntityIdentifier + "' start=" + start + " end=" + end); /* try-with-ressource block, no close if failed needed */ var absolute_filename = getFullFilePath(storedEntityIdentifier); - log.info("SLUBStoragePlugin.retrieveEntitybyRange() with absolute filename=" + absolute_filename); + log.info("SLUBStoragePlugin.retrieveEntitybyRange() with absolute filename=" + absolute_filename + ", begin…"); try (RandomAccessFile file = new RandomAccessFile(absolute_filename, "r")) { long starttime = System.currentTimeMillis(); file.seek(start); @@ -535,18 +542,18 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { is.close(); } copyStream(storedEntityMetadata, absoluteDestFilePath); - //log.debug("SLUBStoragePlugin.storeEntity() try copy (copyStream(storedEntityMetadata, '" + absolute_destFilePath + "')) was successfull"); + //log.debug("SLUBStoragePlugin.storeEntity() try copy (copyStream(storedEntityMetadata, '" + absolute_destFilePath + "')) was successful"); } else { log.info("SLUBStoragePlugin.storeEntity() Cannot handle source path: " + storedEntityMetadata.getCurrentFilePath()); if (is == null) { log.warn("SLUBStoragePlugin.storeEntity() InputStream is null"); return null; } - FileOutputStream output = new FileOutputStream(absoluteDestFilePath.filestring()); + OutputStream output = Files.newOutputStream(Path.of(absoluteDestFilePath.filestring())); long starttime = System.currentTimeMillis(); copyStreamFast(is, output); long endtime = System.currentTimeMillis(); - log.info("SLUBStoragePlugin.storeEntity() try copy (copyStreamFast(is, '" + absoluteDestFilePath.filestring() + "') was successfull (" + throughput(starttime, endtime, absoluteDestFilePath) + ")"); + log.info("SLUBStoragePlugin.storeEntity() copy (copyStreamFast(is, '" + absoluteDestFilePath.filestring() + "') was successful (" + throughput(starttime, endtime, absoluteDestFilePath) + ")"); } if (!checkFixityAbs(storedEntityMetadata.getFixities(), absoluteDestFilePath)) { log.warn("SLUBStoragePlugin.storeEntity() called checkFixity(fixities, '"+absoluteDestFilePath+"') failed"); @@ -555,19 +562,15 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { } else { log.info("SLUBStoragePlugin.storeEntity(), copyfile NOT needed for path=" + absoluteDestFilePath.filestring()); } - String storedEntityIdentifier = relativeFromStoreEntityMetadata(storedEntityMetadata); - //log.debug("SLUBStoragePlugin.storeEntity() storedEntityIdentifier (2)='" + storedEntityIdentifier + "'"); - return storedEntityIdentifier; + return relativeFromStoreEntityMetadata(storedEntityMetadata); } private AbsolutePath generateAbsoluteDestPath(StoredEntityMetaData storedEntityMetadata) throws IOException { - AbsolutePath absoluteDestFilePath; String relativeDirectoryPath = getStreamRelativePath(storedEntityMetadata); String fileName = createFileName(storedEntityMetadata); File destFile = getStreamDirectory(relativeDirectoryPath, fileName); log.info("generateAbsoluteDestPath, relativeDir=" + relativeDirectoryPath + " filename=" + fileName + " absolute=" + destFile.getAbsolutePath()); - absoluteDestFilePath = new AbsolutePath(destFile.getAbsolutePath()); - return absoluteDestFilePath; + return new AbsolutePath(destFile.getAbsolutePath()); } /** @@ -578,23 +581,25 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { */ private void copyFile(String source, AbsolutePath destination) throws IOException { long starttime = System.currentTimeMillis(); - FileOutputStream output = new FileOutputStream(destination.filestring()); + log.info("SLUBStoragePlugin.copyFile (" + source + ", " + destination.filestring() + "), begin copying…"); + OutputStream output = java.nio.file.Files.newOutputStream(Path.of(destination.filestring())); InputStream input; if (source.toLowerCase().trim().startsWith("http")) { + log.info("SLUBStoragePlugin.copyFile (\" + source + \", \" + destination.filestring() + \"), using getUrlContent()\");"); input = FileTransferUtil.getUrlContent(source); } else { /* simpleFilecopy, see https://www.digitalocean.com/community/tutorials/java-copy-file */ - input = new FileInputStream(source); + log.info("SLUBStoragePlugin.copyFile (\" + source + \", \" + destination.filestring() + \"), using FileInputStream()\");"); + input = java.nio.file.Files.newInputStream(Path.of(source)); } copyStreamFast(input, output); input.close(); output.close(); long endtime = System.currentTimeMillis(); - log.info("SLUBStoragePlugin.copyFile (" + source + ", " + destination.filestring() + ") was successfull (" + throughput(starttime, endtime, destination) + ")"); + log.info("SLUBStoragePlugin.copyFile (" + source + ", " + destination.filestring() + "), …finished copying was successful (" + throughput(starttime, endtime, destination) + ")"); } protected void copyStream(StoredEntityMetaData storedEntityMetadata, AbsolutePath absoluteDestPath) throws IOException { - log.info("SLUBStoragePlugin.copyStream()"); String filesHandlingMethod = getFilesHandlingMethod(); String srcPath = storedEntityMetadata.getCurrentFilePath(); //log.debug("SLUBStoragePlugin.copyStream() destPath='" + destPath + "'"); @@ -605,31 +610,29 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { //String iePid = storedEntityMetadata.getIePid(); //log.debug("SLUBStoragePlugin.copyStream() iePid='" + iePid + "'"); String attr = "('"+srcPath+"','" + absoluteDestPath.filestring() + "')"; + log.info("SLUBStoragePlugin.copyStream(), begin… " + attr); if ("copy".equalsIgnoreCase(filesHandlingMethod)) { // FileUtil.copyFile(srcPath, destPath); copyFile(srcPath, absoluteDestPath); saveAbsoluteDestPathsTmpFile(storedEntityMetadata, absoluteDestPath); - log.info("SLUBStoragePlugin.copyStream(), copy"+attr + " was successful"); } else if ("move".equalsIgnoreCase(filesHandlingMethod)) { File canonicalSrcFile = getCanonicalFile(srcPath); assert canonicalSrcFile != null; FileUtil.moveFile(canonicalSrcFile, new File(absoluteDestPath.filestring())); saveAbsoluteDestPathsTmpFile(storedEntityMetadata, absoluteDestPath); - log.info("SLUBStoragePlugin.copyStream(), move"+attr+" was successful"); } else if ("soft_link".equalsIgnoreCase(filesHandlingMethod)) { softLink(srcPath, absoluteDestPath.filestring()); - log.info("SLUBStoragePlugin.copyStream(), softlink"+attr+" was successful"); } else if ("hard_link".equalsIgnoreCase(filesHandlingMethod)) { hardLink(srcPath, absoluteDestPath.filestring()); - log.info("SLUBStoragePlugin.copyStream(), hardlink"+attr+" was successful"); } else { String msg = "SLUBStoragePlugin.copyStream(), unknown filehandling method detected: " + filesHandlingMethod; log.fatal(msg); throw new IOException(msg); } + log.info("SLUBStoragePlugin.copyStream(), …finished via " + filesHandlingMethod + attr+" was successful"); } /** copied from NFS Storage Plugin, enhanced with debugging info, {@inheritDoc} */ @@ -660,9 +663,6 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { return -1; } - - - /** copied from NFS Storage Plugin, enhanced with debugging info, {@inheritDoc} */ private File getCanonicalFile(String srcPath) { @@ -691,7 +691,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { private Map<String, String> getStoreEntityIdentifier(StoredEntityMetaData storedEntityMetadata, AbsolutePath absoluteDestFilePath) throws IOException { log.info("SLUBStoragePlugin.getStoreEntityIdentifier() destFilePath='" + absoluteDestFilePath.filestring() +"'"); //log.debug("destFilePath (2)='" + destFilePath + "'"); - Map<String, String> paths = new HashMap<>(); + Map<String, String> paths = new ConcurrentHashMap<>(); paths.put("destFilePath", absoluteDestFilePath.filestring()); String relativePath = relativeFromStoreEntityMetadata(storedEntityMetadata); contractAssertIsRelativePath(relativePath); @@ -711,7 +711,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { File newDir = new File(getDirRoot() + File.separator + path); //log.debug("SLUBStoragePlugin.getStreamDirectory newDir.getAbsolutePath()=" + newDir.getAbsolutePath()); boolean arecreated = newDir.mkdirs(); - //log.debug("SLUBStoragePlugin.getStreamDirectory newDir.mkdirs(), directories are created:" + arecreated); + log.debug("SLUBStoragePlugin.getStreamDirectory newDir.mkdirs(), directories are created:" + arecreated); return new File(newDir.getAbsolutePath() + File.separator + fileName); } /** prepare right path @@ -809,7 +809,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { private void saveAbsoluteDestPathsTmpFile(StoredEntityMetaData storedEntityMetadata, AbsolutePath absolutePath) { log.info("SLUBStoragePlugin.saveAbsoluteDestPathsTmpFile()"); String tmpFilePath = getTempStorageDirectory(false) + "destPath"; - log.info("SLUBStoragePlugin.saveAbsoluteDestPathsTmpFile tmpFilePath='" + tmpFilePath + "'"); + log.info("SLUBStoragePlugin.saveAbsoluteDestPathsTmpFile tmpFilePath='" + tmpFilePath + "', begin…"); File destPathDir = new File(getTempStorageDirectory(false) + "destPath" + File.separator); //log.debug("SLUBStoragePlugin.saveDestPathsTmpFile destPathDir='" + destPathDir + "'"); if (!destPathDir.exists()) { @@ -819,6 +819,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { } } StorageUtil.saveDestPathToTmpFile(storedEntityMetadata.getIePid(), tmpFilePath, storedEntityMetadata.getEntityPid(), absolutePath.filestring()); + log.info("SLUBStoragePlugin.saveAbsoluteDestPathsTmpFile tmpFilePath='" + tmpFilePath + "', …finished"); } /** @@ -828,11 +829,12 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { */ private AbsolutePath loadAbsoluteDestPathsTmpFile(StoredEntityMetaData storedEntityMetadata) throws IOException { String tmpFilePath = getTempStorageDirectory(false) + "destPath"; - log.info("SLUBStoragePlugin.loadAbsoluteDestPathsTmpFile tmpFilePath='" + tmpFilePath + "'"); + log.info("SLUBStoragePlugin.loadAbsoluteDestPathsTmpFile tmpFilePath='" + tmpFilePath + "', begin…"); String absolutePath = StorageUtil.readDestPathFromTmpFile(storedEntityMetadata.getIePid(), tmpFilePath, storedEntityMetadata.getEntityPid()); if (null == absolutePath) { throw new IOException("no destpath read from tmpfilePath '"+tmpFilePath+"' and IEPID="+storedEntityMetadata.getIePid() + " and PID="+storedEntityMetadata.getEntityPid() + ", got null"); } + log.info("SLUBStoragePlugin.loadAbsoluteDestPathsTmpFile tmpFilePath='" + tmpFilePath + "', …finished"); return new AbsolutePath(absolutePath); } }