GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — Add configurable file transfer client with SCP/SFTP support by diegolovison · Pull Request #784 · Hyperfoil/Hyperfoil Conversation
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Files | scp with array (Baseline) | sftp |
|---|---|---|
| 1 | 304 ms | 15 ms (20.27x faster) |
| 10 | 540 ms | 50 ms (10.80x faster) |
| 100 | 3442 ms | 296 ms (11.63x faster) |
| 1000 | 33474 ms | 2760 ms (12.13x faster) |
Reproducer:
mvn test -Dtest=FileTransferPerformanceTest -Dio.hyperfoil.file.transfer.client=scp
mvn test -Dtest=FileTransferPerformanceTest -Dio.hyperfoil.file.transfer.client=sftp
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Nice work on the performance improvement - the SCP vs SFTP numbers are compelling, especially for multi-file transfers.
A few suggestions on the design and two bugs I spotted. The main design suggestion is to make FileTransfer an abstract class instead of using delegation through IFileTransfer. This eliminates the interface (whose I prefix is not idiomatic Java), removes the delegation boilerplate, and lets the factory logic live in a static method with a proper return type.
I also noticed a bug in the SFTP single-file upload path and a test bug with missing cleanup between iterations - details in the inline comments.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| public class FileTransfer implements AutoCloseable { | ||
|
|
||
| private static final Logger log = LogManager.getLogger(FileTransfer.class); | ||
|
|
||
| private IFileTransfer client; | ||
|
|
||
| FileTransfer(ClientSession session) throws Exception { | ||
| String fileTransferClient = Properties.get(Properties.FILE_TRANSFER_CLIENT, "scp"); | ||
| log.info("Using {} protocol for file transfer", fileTransferClient); | ||
| if ("scp".equals(fileTransferClient)) { | ||
| this.client = new Scp(session); | ||
| } else if ("sftp".equals(fileTransferClient)) { | ||
| this.client = new Sftp(session); | ||
| } else { | ||
| throw new IllegalArgumentException("Invalid file transfer client. You provided: " + fileTransferClient); | ||
| } | ||
| } | ||
|
|
||
| public void upload(String local, String remote) throws IOException { | ||
| this.upload(List.of(local), remote); | ||
| } | ||
|
|
||
| public void upload(List<String> local, String remote) throws IOException { | ||
| this.client.upload(local, remote); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| this.client.close(); | ||
| } | ||
|
|
||
| private interface IFileTransfer { | ||
|
|
||
| void upload(List<String> local, String remote) throws IOException; | ||
|
|
||
| void close() throws IOException; | ||
| } | ||
|
|
||
| private static class Scp implements IFileTransfer { | ||
|
|
||
| private ScpClient scpClient; | ||
|
|
||
| Scp(ClientSession session) { | ||
| this.scpClient = ScpClientCreator.instance().createScpClient(session); | ||
| } | ||
|
|
||
| @Override | ||
| public void upload(List<String> local, String remote) throws IOException { | ||
| if (!local.isEmpty()) { | ||
| this.scpClient.upload(local.toArray(String[]::new), remote, ScpClient.Option.PreserveAttributes); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
|
|
||
| } | ||
| } | ||
|
|
||
| private static class Sftp implements IFileTransfer { | ||
|
|
||
| private SftpClient sftpClient; | ||
|
|
||
| Sftp(ClientSession session) throws Exception { | ||
| try { | ||
| this.sftpClient = SftpClientFactory.instance().createSftpClient(session); | ||
| } catch (IOException e) { | ||
| throw new Exception("Problem while creating the Sftp instance", e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void upload(List<String> local, String remote) throws IOException { | ||
| for (String l : local) { | ||
| try (InputStream in = new FileInputStream(l); | ||
| OutputStream out = sftpClient.write(remote + "/" + new File(l).getName())) { | ||
| byte[] buffer = new byte[8192]; | ||
| int len; | ||
| while ((len = in.read(buffer)) != -1) { | ||
| out.write(buffer, 0, len); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| this.sftpClient.close(); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: refactor FileTransfer to be an abstract class with a static factory method, eliminating the IFileTransfer interface and the delegation layer.
Benefits:
IFileTransfer (and its non-idiomatic I prefix)Scp and Sftp extend FileTransfer directly - no wrappingIOException instead of broad Exceptionfinalpublic abstract class FileTransfer implements AutoCloseable { private static final Logger log = LogManager.getLogger(FileTransfer.class); public static FileTransfer create(ClientSession session) throws IOException { String protocol = Properties.get(Properties.FILE_TRANSFER_CLIENT, "scp"); log.info("Using {} protocol for file transfer", protocol); return switch (protocol) { case "scp" -> new Scp(session); case "sftp" -> new Sftp(session); default -> throw new IllegalArgumentException( "Invalid file transfer client. You provided: " + protocol + ". Supported: scp, sftp"); }; } public void upload(String local, String remote) throws IOException { upload(List.of(local), remote); } public abstract void upload(List<String> local, String remote) throws IOException; @Override public abstract void close() throws IOException; static class Scp extends FileTransfer { private final ScpClient scpClient; Scp(ClientSession session) { this.scpClient = ScpClientCreator.instance().createScpClient(session); } @Override public void upload(List<String> local, String remote) throws IOException { if (!local.isEmpty()) { this.scpClient.upload(local.toArray(String[]::new), remote, ScpClient.Option.PreserveAttributes); } } @Override public void close() { // ScpClient has no close method } } static class Sftp extends FileTransfer { private final SftpClient sftpClient; Sftp(ClientSession session) throws IOException { this.sftpClient = SftpClientFactory.instance().createSftpClient(session); } @Override public void upload(List<String> local, String remote) throws IOException { for (String l : local) { String target = remote + "/" + new File(l).getName(); try (InputStream in = new FileInputStream(l); OutputStream out = sftpClient.write(target)) { byte[] buffer = new byte[32768]; int len; while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } } } } @Override public void close() throws IOException { this.sftpClient.close(); } } }
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| try { | ||
| this.fileTransfer = new FileTransfer(session); | ||
| } catch (Exception e) { | ||
| exceptionHandler.accept(new DeploymentException("Failed to initialize file transfer", e)); | ||
| return; |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
With the factory method approach, this becomes:
try { this.fileTransfer = FileTransfer.create(session); } catch (IOException e) { exceptionHandler.accept(new DeploymentException("Failed to initialize file transfer", e)); return; }
Narrowing the catch to IOException instead of Exception.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| @@ -205,7 +215,7 @@ public void close() throws IOException { | |||
| String filename = log4jConfigurationFile.substring(log4jConfigurationFile.lastIndexOf(File.separatorChar) + 1); | |||
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Bug: This passes targetFile which is already a full path including the filename (e.g. /dir/agentlib/log4j2.xml). But FileTransfer.upload(String, String) delegates to upload(List.of(local), remote), and the SFTP implementation does remote + "/" + new File(l).getName() - resulting in /dir/agentlib/log4j2.xml/log4j2.xml.
The SCP path works because the underlying ScpClient.upload(String[], String, Option...) handles single-file-to-file-path correctly.
You need either:
upload method for single-file-to-exact-path (no filename appending), orremote is a directory or a file pathOption 1 is simpler - add a boolean parameter or a separate method name like uploadTo for the exact-path case.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| * mvn test -Dtest=FileTransferPerformanceTest -Dio.hyperfoil.file.transfer.client=scp | ||
| * mvn test -Dtest=FileTransferPerformanceTest -Dio.hyperfoil.file.transfer.client=sftp |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Javadoc references the wrong class name.
Should be FileTransferTest instead of FileTransferPerformanceTest.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| for (int fileCount : fileCounts) { | ||
| log.info("--- Testing with {} file(s) ---", fileCount); | ||
|
|
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Bug: The remoteDir is not cleaned between iterations. After the 1-file iteration uploads 1 file, the 10-file iteration will find 11 files in remoteDir, causing assertEquals(files.size(), uploadedFileCount) to fail.
Add cleanup at the start of each iteration:
// Clean remote directory from previous iteration try (var files = Files.list(remoteDir)) { files.forEach(p -> { try { Files.deleteIfExists(p); } catch (IOException e) { log.warn("Failed to clean up {}", p, e); } }); }
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| } | ||
|
|
||
| private long testFileTransfer(List<String> files) throws Exception { | ||
| try (FileTransfer fileTransfer = new FileTransfer(session)) { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
With the refactor this becomes FileTransfer.create(session).
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@diegolovison this is still valid or #780 is enough and we can close it? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@franz1981 Yes. This PR is valid. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@stalep I decided to keep the inner interface and inner implementation to reduce the surface of the change. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I think this looks good and can be merged as it. I personally do not like the IFileTransfer, but I guess that's a more personal take. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.