通过并行线程尝试将文件放在sftp上,经常从sftp服务器获取连接重置错误

问题描述 投票:-1回答:1

我有一个多线程代码,有22个并行运行的线程,并试图将文件放在sftp服务器上。

但是我的日志中间歇性地出现连接重置错误,因此很少有记录失败。

在初步分析中,我发现sftp服务器的大小为t2.small,CPU利用率为92%。

考虑到这一点,因为我之所以将服务器更改为c5n.xlarge,现在错误的频率降低了,但是,即使最大CPU利用率达到63%,我也会得到它。

我无法在/ var / log / secure的sftp服务器日志中找到任何不同的内容。

下面是用于放置文件的代码段,每个线程创建一个新会话并关闭它。

JSch ssh = new JSch();
            // ssh.setKnownHosts("/path/of/known_hosts/file");
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            // Use key authentication if it is set, else use password auth
            if (mpServerDetails.get(SftpFile.SFTP_USERKEY) != null
                    && mpServerDetails.get(SftpFile.SFTP_USERKEY) != "") {
                    File userKeyFile = new File(mpServerDetails.get(SftpFile.SFTP_USERKEY).toString());
                if (userKeyFile == null || !userKeyFile.exists()) {
                    throw new NonRetriableException(
                            "Key file " + mpServerDetails.get(SftpFile.SFTP_USERKEY).toString() + "not found.");
                }
                ssh.addIdentity(userKeyFile.getAbsolutePath());
                session = ssh.getSession(mpServerDetails.get(SftpFile.SFTP_USERNAME).toString(),
                        mpServerDetails.get(SftpFile.SFTP_HOSTNAME).toString());
            } else if (mpServerDetails.get(SftpFile.SFTP_PASSWORD) != null) {
                session = ssh.getSession(mpServerDetails.get(SftpFile.SFTP_USERNAME).toString(),
                        mpServerDetails.get(SftpFile.SFTP_HOSTNAME).toString());
                session.setPassword(mpServerDetails.get(SftpFile.SFTP_PASSWORD).toString());
            }
            session.setConfig(config);
            session.connect();
            if (session != null && !session.isConnected()) {
                logger.warn("**session is not connected going to connect the sftp session ** {} ", session.getHost());
                session.connect();
            }
            channel = (ChannelSftp) session.openChannel("sftp");
            if (channel != null && !channel.isConnected()) {
                logger.warn("**channel is not connected going to connect the sftp channel ** {} ",
                        channel.getSession().isConnected());
                channel.connect();
            }
            channel.put(file.getAbsolutePath(), dest.getConfig().get(TransporterFileConstants.SFTP_DIRECTORY).toString()
                    + File.separatorChar + dest.getFileName(), new SystemOutProgressMonitor());

        }
        catch (NonRetriableException e) {
            throw new NonRetriableException(e);
        }
        catch (Exception e) {
            logger.error(
                    "Error occured while uploading file having name " + dest.getFileName() + " from remote directory:"
                            + dest.getConfig().get(TransporterFileConstants.SFTP_DIRECTORY).toString(),
                    e);
            logger.error("SFTP Exception : ", e);
            throw new RetriableException(e);
        }
        finally {
            if (null != channel && channel.isConnected()) {
                try {
                    channel.disconnect();
                }
                catch (Throwable e) {
                    logger.error("Error while disconnecting channel : ", e);
                }
            }
            if (null != session) {
                try {
                    session.disconnect();
                }
                catch (Throwable e) {
                    logger.error("Error while returning object to sftp pool : ", e);
                }
            }
        }

有人可以帮我理解为什么我会得到这个例外吗?

SFTP服务器配置是

MaxSessions 50
Capacity - 25 GB
4 core server with 10 GB Ram

一段错误消息

com.jcraft.jsch.JSchException: Session.connect: java.net.SocketException: Connection reset
    at com.jcraft.jsch.Session.connect(Session.java:558) ~[honeybee-engine.jar:na]

如果这种情况继续存在,我的数据处理将不一致。

java jsch sshd
1个回答
0
投票
MaxSessions 50

SSH服务器MaxSessions参数限制可以通过单个SSH连接运行的“会话”数。您只通过每个连接运行一个会话 - SFTP会话,因此MaxSessions限制与您不是特别相关。

您的问题可能与MaxStartups setting

MaxStartups 指定SSH守护程序的最大并发未经身份验证的连接数。在身份验证成功或LoginGraceTime到期以进行连接之前,将删除其他连接。默认值为10:30:100 ....

基本上,如果有太多客户端连接到服务器但尚未进行身份验证,则服务器将删除其中一些连接。如果您的应用程序同时打开了太多与服务器的连接,则服务器可能会丢弃其中一些连接。这里的解决方案是调整MaxStartups的值,或者更改您的应用程序不要一次打开这么多连接。

还有一个名为listen backlog的操作系统限制。基本上,操作系统只能保留一定数量的挂起TCP连接。如果有足够的连接尝试同时进入,并且ssh服务器进程在接受它们时速度不够快,则操作系统将丢弃一些连接请求。 SSH服务器请求128个连接的积压,但操作系统可能会将积压限制在较低的值。如果您的SSH服务器足够忙,您可能会遇到此限制。

© www.soinside.com 2019 - 2024. All rights reserved.