Kevin Doran created NIFI-16667:
----------------------------------
Summary: Start Management Server before Application Server to fail
fast on bind failures
Key: NIFI-16667
URL: https://issues.apache.org/jira/browse/NIFI-16667
Project: Apache NiFi
Issue Type: Improvement
Components: Core Framework
Reporter: Kevin Doran
Assignee: Kevin Doran
h3. Description
{{Application.startServer()}} starts the Application Server (Jetty, flow
synchronization, components) before it starts the Management Server:
{code:java}
// Start Application Server before Management Server
applicationServer.start();
managementServer.start();
{code}
(nifi-runtime {{Application.java}} lines 130-132)
If the Management Server cannot bind its address,
{{StandardManagementServer.start()}} throws {{UncheckedIOException: Management
Server start failed}}. {{Application.run()}} catches the failure, logs {{Start
Server failed}}, and calls {{applicationServer.stop()}} (lines 79-81). By then
the full flow has already been loaded and started, so the stop has to tear down
a running application.
We have seen this in a containerized deployment where the management address is
bound to {{0.0.0.0:52020}} so that orchestrator health probes can reach
{{/health/cluster}}:
* The default management port 52020 is inside the default Linux ephemeral port
range (32768-60999).
* The Management Server binds last, after flow synchronization has started
components. Those components open outbound connections (for example, client
libraries probing cloud metadata endpoints and calling remote APIs) in the
seconds before the bind.
* About 0.08% of starts fail with {{java.net.BindException: Address already in
use}} on 52020. The next start succeeds.
* In these deployments the JVM stayed alive after {{applicationServer.stop()}}.
With no Management Server listening, health probes failed until the
orchestrator killed the process about 10 minutes later. We have not identified
which threads keep the JVM alive; locally, a flow of standard processors shuts
down and exits cleanly, so it appears to depend on the extensions in the flow.
The bind failure is detected late, after the most expensive and least
predictable part of startup has run.
h3. Steps to reproduce
# Hold the management port with an established client socket before NiFi
starts. Run this in the same network namespace as NiFi, with the management
address set to {{0.0.0.0:52020}}:
{code:python}
import socket
server = socket.socket(); server.bind(("127.0.0.1", 19999)); server.listen(1)
client = socket.socket(); client.bind(("127.0.0.1", 52020));
client.connect(("127.0.0.1", 19999))
server.accept()
input()
{code}
# Start NiFi with a flow containing running processors.
# Observe that Jetty starts and the flow is synchronized and started. Only then
does {{Start Server failed}} appear, caused by {{BindException: Address already
in use}}, and the application is stopped.
h3. Proposed change
Start the Management Server first, and stop it if the Application Server fails
to start:
{code:java}
// Start Management Server before Application Server so that a bind failure
stops startup before any components run
managementServer.start();
try {
applicationServer.start();
} catch (final RuntimeException e) {
managementServer.stop();
throw e;
}
{code}
With this order:
* A bind failure happens immediately after NAR unpacking, before Jetty starts
or any flow component is created, so there is nothing to tear down.
* The Management Server binds before components open outbound connections,
which closes most of the window for an ephemeral-port collision.
* Before the Application Server is ready, {{/health/cluster}} returns 503
({{Cluster Status: UNKNOWN}}), because {{getClusterDetailsFactory()}} is null.
Probes keep reporting not-ready until the application is up, as they do today
(today the probe gets connection refused instead).
Local verification of this change, hot-patched into a 2.x build:
* With the port held, startup fails about 13 seconds in, right after NAR
unpacking, and the process exits within a second. With the current order it
fails after Jetty and the flow have started.
* With the port free, startup time is unchanged (37.5 s vs 38.0 s).
{{/health/cluster}} returns 503 from bind until the application is up.
* Only standalone mode was tested. In clustered mode, {{CONNECTING}} maps to
200 in {{HealthClusterHttpHandler}}; I'd like confirmation that the cluster
details factory cannot report CONNECTING before the Application Server is
serving.
h3. Related observations (possibly separate issues)
* When startup fails this way, {{main}} returns normally and the process exits
with status 0, so orchestrators see a clean exit rather than a failure.
* The shutdown hook is registered only after both servers start, so a failure
during startup skips {{ShutdownCommand}}.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)