NIFI-10702 Clear server list on connection error in SMB processors

This closes #6620.

Signed-off-by: Peter Turcsanyi <turcsanyi@apache.org>
This commit is contained in:
Gabor Kulik 2022-11-04 15:23:43 +01:00 committed by Peter Turcsanyi
parent 9c21e26e63
commit 1bd4169558
7 changed files with 22 additions and 5 deletions

View File

@ -53,6 +53,10 @@
<groupId>com.hierynomus</groupId>
<artifactId>smbj</artifactId>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk18on</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>

View File

@ -545,6 +545,7 @@ public class GetSmbFile extends AbstractProcessor {
} catch (Exception e) {
logger.error("Could not establish smb connection because of error {}", new Object[]{e});
context.yield();
smbClient.getServerList().unregister(hostname);
}
}
}

View File

@ -401,6 +401,7 @@ public class PutSmbFile extends AbstractProcessor {
} catch (Exception e) {
session.transfer(flowFiles, REL_FAILURE);
logger.error("Could not establish smb connection because of error {}", new Object[]{e});
smbClient.getServerList().unregister(hostname);
}
}
}

View File

@ -21,6 +21,7 @@ import com.hierynomus.mssmb2.SMB2ShareAccess;
import com.hierynomus.smbj.SMBClient;
import com.hierynomus.smbj.auth.AuthenticationContext;
import com.hierynomus.smbj.connection.Connection;
import com.hierynomus.smbj.server.ServerList;
import com.hierynomus.smbj.session.Session;
import com.hierynomus.smbj.share.DiskEntry;
import com.hierynomus.smbj.share.DiskShare;
@ -59,6 +60,7 @@ public class PutSmbFileTest {
private DiskShare diskShare;
private DiskEntry diskEntry;
private File smbfile;
private ServerList serverList;
private ByteArrayOutputStream baOutputStream;
private final static String HOSTNAME = "smbhostname";
@ -80,9 +82,12 @@ public class PutSmbFileTest {
diskShare = mock(DiskShare.class);
diskEntry = mock(DiskEntry.class);
smbfile = mock(File.class);
serverList = mock(ServerList.class);
baOutputStream = new ByteArrayOutputStream();
when(smbClient.connect(any(String.class))).thenReturn(connection);
when(smbClient.getServerList()).thenReturn(serverList);
when(connection.authenticate(any(AuthenticationContext.class))).thenReturn(session);
when(session.connectShare(SHARE)).thenReturn(diskShare);
when(diskShare.openFile(

View File

@ -49,6 +49,10 @@
<groupId>com.hierynomus</groupId>
<artifactId>smbj</artifactId>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk18on</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>

View File

@ -121,14 +121,16 @@ public class SmbjClientProviderService extends AbstractControllerService impleme
@Override
public SmbClientService getClient() throws IOException {
Connection connection = smbClient.connect(hostname, port);
Connection connection = null;
try {
connection = smbClient.connect(hostname, port);
return connectToShare(connection);
} catch (IOException e) {
getLogger().debug("Closing stale connection and trying to create a new one for share " + getServiceLocation());
closeConnection(connection);
unregisterHost();
connection = smbClient.connect(hostname, port);
return connectToShare(connection);
@ -160,6 +162,10 @@ public class SmbjClientProviderService extends AbstractControllerService impleme
return new SmbjClientService(session, (DiskShare) share, getServiceLocation());
}
private void unregisterHost() {
smbClient.getServerList().unregister(hostname);
}
private void closeConnection(Connection connection) {
try {
if (connection != null) {

View File

@ -47,10 +47,6 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk18on</artifactId>
</dependency>
<dependency>
<groupId>net.engio</groupId>
<artifactId>mbassador</artifactId>