Skip to content

Commit 7582ddc

Browse files
committed
NIFI-16611 Add connect timeout to load balancing client sockets
- Connect load balancing client sockets with a timeout instead of blocking for the OS TCP connect timeout against unreachable peers. - Add nifi.cluster.load.balance.connection.timeout with a default of 5 sec.
1 parent f7e7387 commit 7582ddc

10 files changed

Lines changed: 26 additions & 11 deletions

File tree

‎nifi-commons/nifi-properties/src/main/java/org/apache/nifi/util/NiFiProperties.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ public class NiFiProperties extends ApplicationProperties {
264264
public static final String LOAD_BALANCE_CONNECTIONS_PER_NODE = "nifi.cluster.load.balance.connections.per.node";
265265
public static final String LOAD_BALANCE_MAX_THREAD_COUNT = "nifi.cluster.load.balance.max.thread.count";
266266
public static final String LOAD_BALANCE_COMMS_TIMEOUT = "nifi.cluster.load.balance.comms.timeout";
267+
public static final String LOAD_BALANCE_CONNECTION_TIMEOUT = "nifi.cluster.load.balance.connection.timeout";
267268

268269
// zookeeper properties
269270
public static final String ZOOKEEPER_CONNECT_STRING = "nifi.zookeeper.connect.string";
@@ -434,6 +435,7 @@ public class NiFiProperties extends ApplicationProperties {
434435
public static final int DEFAULT_LOAD_BALANCE_CONNECTIONS_PER_NODE = 4;
435436
public static final int DEFAULT_LOAD_BALANCE_MAX_THREAD_COUNT = 8;
436437
public static final String DEFAULT_LOAD_BALANCE_COMMS_TIMEOUT = "30 sec";
438+
public static final String DEFAULT_LOAD_BALANCE_CONNECTION_TIMEOUT = "5 sec";
437439

438440
// state management defaults
439441
public static final String DEFAULT_STATE_MANAGEMENT_CONFIG_FILE = "conf/state-management.xml";

‎nifi-docs/src/main/asciidoc/administration-guide.adoc‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3772,6 +3772,7 @@ Subject Alternative Names included on the server certificate configured in the a
37723772

37733773
*NOTE:* Increasing this value will allow additional threads to be used for communicating with other nodes in the cluster and writing the data to the Content and FlowFile Repositories. However, if this property is set to a value greater than the number of nodes in the cluster multiplied by the number of connections per node (`nifi.cluster.load.balance.connections.per.node`), then no further benefit will be gained and resources will be wasted.
37743774
|`nifi.cluster.load.balance.comms.timeout`|When communicating with another node, if this amount of time elapses without making any progress when reading from or writing to a socket, then a TimeoutException will be thrown. This will then result in the data either being retried or sent to another node in the cluster, depending on the configured Load Balancing Strategy. The default value is `30 sec`.
3775+
|`nifi.cluster.load.balance.connection.timeout`|The maximum amount of time to wait when establishing a connection to another node in the cluster for load balancing. If the connection cannot be established within this time, the connection attempt fails. Keeping this value low limits how long data transfers to other nodes are delayed when a node is unreachable, for example after it has been restarted with a new network address. The default value is `5 sec`.
37753776
|====
37763777

37773778
=== ZooKeeper Properties

‎nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/FlowController.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -884,8 +884,10 @@ private FlowController(
884884
numThreads, loadBalanceProtocol, eventReporter, timeoutMillis);
885885

886886
final int connectionsPerNode = nifiProperties.getIntegerProperty(NiFiProperties.LOAD_BALANCE_CONNECTIONS_PER_NODE, NiFiProperties.DEFAULT_LOAD_BALANCE_CONNECTIONS_PER_NODE);
887-
final NioAsyncLoadBalanceClientFactory asyncClientFactory = new NioAsyncLoadBalanceClientFactory(sslContext, timeoutMillis, new ContentRepositoryFlowFileAccess(contentRepository),
888-
eventReporter, new StandardLoadBalanceFlowFileCodec(), clusterCoordinator);
887+
final String connectTimeoutPeriod = nifiProperties.getProperty(NiFiProperties.LOAD_BALANCE_CONNECTION_TIMEOUT, NiFiProperties.DEFAULT_LOAD_BALANCE_CONNECTION_TIMEOUT);
888+
final int connectTimeoutMillis = (int) FormatUtils.getTimeDuration(connectTimeoutPeriod, TimeUnit.MILLISECONDS);
889+
final NioAsyncLoadBalanceClientFactory asyncClientFactory = new NioAsyncLoadBalanceClientFactory(sslContext, timeoutMillis, connectTimeoutMillis,
890+
new ContentRepositoryFlowFileAccess(contentRepository), eventReporter, new StandardLoadBalanceFlowFileCodec(), clusterCoordinator);
889891
loadBalanceClientRegistry = new NioAsyncLoadBalanceClientRegistry(asyncClientFactory, connectionsPerNode);
890892

891893
final int loadBalanceClientThreadCount = nifiProperties.getIntegerProperty(NiFiProperties.LOAD_BALANCE_MAX_THREAD_COUNT, NiFiProperties.DEFAULT_LOAD_BALANCE_MAX_THREAD_COUNT);

‎nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/queue/clustered/client/async/nio/NioAsyncLoadBalanceClient.java‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public class NioAsyncLoadBalanceClient implements AsyncLoadBalanceClient {
6363

6464
private final NodeIdentifier nodeIdentifier;
6565
private final int timeoutMillis;
66+
private final int connectTimeoutMillis;
6667
private final FlowFileContentAccess flowFileContentAccess;
6768
private final LoadBalanceFlowFileCodec flowFileCodec;
6869
private final EventReporter eventReporter;
@@ -88,10 +89,12 @@ public class NioAsyncLoadBalanceClient implements AsyncLoadBalanceClient {
8889
private final Lock loadBalanceSessionLock = new ReentrantLock();
8990
private LoadBalanceSession loadBalanceSession = null;
9091

91-
public NioAsyncLoadBalanceClient(final NodeIdentifier nodeIdentifier, final SSLContext sslContext, final int timeoutMillis, final FlowFileContentAccess flowFileContentAccess,
92-
final LoadBalanceFlowFileCodec flowFileCodec, final EventReporter eventReporter, final ClusterCoordinator clusterCoordinator) {
92+
public NioAsyncLoadBalanceClient(final NodeIdentifier nodeIdentifier, final SSLContext sslContext, final int timeoutMillis, final int connectTimeoutMillis,
93+
final FlowFileContentAccess flowFileContentAccess, final LoadBalanceFlowFileCodec flowFileCodec, final EventReporter eventReporter,
94+
final ClusterCoordinator clusterCoordinator) {
9395
this.nodeIdentifier = nodeIdentifier;
9496
this.timeoutMillis = timeoutMillis;
97+
this.connectTimeoutMillis = connectTimeoutMillis;
9598
this.flowFileContentAccess = flowFileContentAccess;
9699
this.flowFileCodec = flowFileCodec;
97100
this.eventReporter = eventReporter;
@@ -486,7 +489,9 @@ private SocketChannel createChannel() throws IOException {
486489
final Socket socket = socketChannel.socket();
487490
socket.setSoTimeout(timeoutMillis);
488491

489-
socket.connect(new InetSocketAddress(nodeIdentifier.getLoadBalanceAddress(), nodeIdentifier.getLoadBalancePort()));
492+
// Bound the connect: without a timeout, connecting to a peer that silently drops packets (e.g. the old address of a
493+
// restarted node) blocks for the OS TCP connect timeout, stalling every other Client served by the same task thread
494+
socket.connect(new InetSocketAddress(nodeIdentifier.getLoadBalanceAddress(), nodeIdentifier.getLoadBalancePort()), connectTimeoutMillis);
490495
socket.setSoTimeout(timeoutMillis);
491496

492497
return socketChannel;

‎nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/queue/clustered/client/async/nio/NioAsyncLoadBalanceClientFactory.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,18 @@
2929
public class NioAsyncLoadBalanceClientFactory implements AsyncLoadBalanceClientFactory {
3030
private final SSLContext sslContext;
3131
private final int timeoutMillis;
32+
private final int connectTimeoutMillis;
3233
private final FlowFileContentAccess flowFileContentAccess;
3334
private final EventReporter eventReporter;
3435
private final LoadBalanceFlowFileCodec flowFileCodec;
3536
private final ClusterCoordinator clusterCoordinator;
3637

37-
public NioAsyncLoadBalanceClientFactory(final SSLContext sslContext, final int timeoutMillis, final FlowFileContentAccess flowFileContentAccess, final EventReporter eventReporter,
38+
public NioAsyncLoadBalanceClientFactory(final SSLContext sslContext, final int timeoutMillis, final int connectTimeoutMillis,
39+
final FlowFileContentAccess flowFileContentAccess, final EventReporter eventReporter,
3840
final LoadBalanceFlowFileCodec loadBalanceFlowFileCodec, final ClusterCoordinator clusterCoordinator) {
3941
this.sslContext = sslContext;
4042
this.timeoutMillis = timeoutMillis;
43+
this.connectTimeoutMillis = connectTimeoutMillis;
4144
this.flowFileContentAccess = flowFileContentAccess;
4245
this.eventReporter = eventReporter;
4346
this.flowFileCodec = loadBalanceFlowFileCodec;
@@ -46,6 +49,6 @@ public NioAsyncLoadBalanceClientFactory(final SSLContext sslContext, final int t
4649

4750
@Override
4851
public NioAsyncLoadBalanceClient createClient(final NodeIdentifier nodeIdentifier) {
49-
return new NioAsyncLoadBalanceClient(nodeIdentifier, sslContext, timeoutMillis, flowFileContentAccess, flowFileCodec, eventReporter, clusterCoordinator);
52+
return new NioAsyncLoadBalanceClient(nodeIdentifier, sslContext, timeoutMillis, connectTimeoutMillis, flowFileContentAccess, flowFileCodec, eventReporter, clusterCoordinator);
5053
}
5154
}

‎nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/controller/queue/clustered/LoadBalancedQueueIT.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ private ContentClaim createContentClaim(final byte[] bytes) {
220220

221221
private NioAsyncLoadBalanceClientFactory createClientFactory(final SSLContext sslContext) {
222222
final FlowFileContentAccess flowFileContentAccess = flowFile -> clientContentRepo.read(flowFile.getContentClaim());
223-
return new NioAsyncLoadBalanceClientFactory(sslContext, 30000, flowFileContentAccess, eventReporter, new StandardLoadBalanceFlowFileCodec(), clusterCoordinator);
223+
return new NioAsyncLoadBalanceClientFactory(sslContext, 30000, 5000, flowFileContentAccess, eventReporter, new StandardLoadBalanceFlowFileCodec(), clusterCoordinator);
224224
}
225225

226226
@Test

‎nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/controller/queue/clustered/client/async/nio/TestNioAsyncLoadBalanceClient.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void testUnregisterOfInFlightTransactionClosesChannel() throws Exception {
8181

8282
final FlowFileContentAccess contentAccess = ff -> new ByteArrayInputStream(new byte[0]);
8383

84-
final NioAsyncLoadBalanceClient client = new NioAsyncLoadBalanceClient(nodeId, null, 30000, contentAccess,
84+
final NioAsyncLoadBalanceClient client = new NioAsyncLoadBalanceClient(nodeId, null, 30000, 5000, contentAccess,
8585
new StandardLoadBalanceFlowFileCodec(), EventReporter.NO_OP, clusterCoordinator);
8686

8787
final String connectionId = "unit-test-connection";

‎nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/controller/queue/clustered/client/async/nio/TestNioAsyncLoadBalanceClientRegistry.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public boolean isRebalanceOnFailure() {
6767
void setUp() {
6868
createdClients = new ArrayList<>();
6969

70-
final NioAsyncLoadBalanceClientFactory factory = new NioAsyncLoadBalanceClientFactory(null, 30000, null, null, null, null) {
70+
final NioAsyncLoadBalanceClientFactory factory = new NioAsyncLoadBalanceClientFactory(null, 30000, 5000, null, null, null, null) {
7171
@Override
7272
public NioAsyncLoadBalanceClient createClient(final NodeIdentifier nodeIdentifier) {
7373
final StubAsyncLoadBalanceClient client = new StubAsyncLoadBalanceClient(nodeIdentifier);
@@ -165,7 +165,7 @@ private static class StubAsyncLoadBalanceClient extends NioAsyncLoadBalanceClien
165165
boolean stopped = false;
166166

167167
StubAsyncLoadBalanceClient(final NodeIdentifier nodeIdentifier) {
168-
super(nodeIdentifier, null, 30000, null, null, null, null);
168+
super(nodeIdentifier, null, 30000, 5000, null, null, null, null);
169169
}
170170

171171
@Override

‎nifi-framework-bundle/nifi-framework/nifi-resources/pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@
221221
<nifi.cluster.load.balance.connections.per.node>1</nifi.cluster.load.balance.connections.per.node>
222222
<nifi.cluster.load.balance.max.thread.count>8</nifi.cluster.load.balance.max.thread.count>
223223
<nifi.cluster.load.balance.comms.timeout>30 sec</nifi.cluster.load.balance.comms.timeout>
224+
<nifi.cluster.load.balance.connection.timeout>5 sec</nifi.cluster.load.balance.connection.timeout>
224225

225226
<!-- nifi.properties: zookeeper properties -->
226227
<nifi.zookeeper.connect.string />

‎nifi-framework-bundle/nifi-framework/nifi-resources/src/main/resources/conf/nifi.properties‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ nifi.cluster.load.balance.port=${nifi.cluster.load.balance.port}
285285
nifi.cluster.load.balance.connections.per.node=${nifi.cluster.load.balance.connections.per.node}
286286
nifi.cluster.load.balance.max.thread.count=${nifi.cluster.load.balance.max.thread.count}
287287
nifi.cluster.load.balance.comms.timeout=${nifi.cluster.load.balance.comms.timeout}
288+
nifi.cluster.load.balance.connection.timeout=${nifi.cluster.load.balance.connection.timeout}
288289

289290
# zookeeper properties, used for cluster management #
290291
nifi.zookeeper.connect.string=${nifi.zookeeper.connect.string}

0 commit comments

Comments
 (0)