apache/hadoop · warning · MetricsException
Already connected to Graphite
Error message
Already connected to Graphite
What it means
The inner Graphite class's connect() throws MetricsException("Already connected to Graphite") when invoked while isConnected() is true (socket and writer already established). Internal call sites guard with isConnected() — write() only calls connect() when disconnected — so this surfaces almost exclusively from user or test code that manages the connection lifecycle directly and calls connect() on an already-connected instance.
Source
Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/sink/GraphiteSink.java:145
}
public static class Graphite {
private final static int MAX_CONNECTION_FAILURES = 5;
private String serverHost;
private int serverPort;
private Writer writer = null;
private Socket socket = null;
private int connectionFailures = 0;
public Graphite(String serverHost, int serverPort) {
this.serverHost = serverHost;
this.serverPort = serverPort;
}
public void connect() {
if (isConnected()) {
throw new MetricsException("Already connected to Graphite");
}
if (tooManyConnectionFailures()) {
// return silently (there was ERROR in logs when we reached limit for the first time)
return;
}
try {
// Open a connection to Graphite server.
socket = new Socket(serverHost, serverPort);
writer = new OutputStreamWriter(socket.getOutputStream(),
StandardCharsets.UTF_8);
} catch (Exception e) {
connectionFailures++;
if (tooManyConnectionFailures()) {
// first time when connection limit reached, report to logs
LOG.error("Too many connection failures, would not try to connect again.");
}
throw new MetricsException("Error creating connection, " +
serverHost + ":" + serverPort, e);View on GitHub (pinned to 2add963021)
Solutions
- Guard the call: if (!graphite.isConnected()) { graphite.connect(); }
- Drop manual connect() entirely — write() connects lazily when needed
- Call close() before intentionally reconnecting
Example fix
// before
graphite.connect(); // later in the same session, still connected
graphite.connect(); // MetricsException: Already connected to Graphite
// after
if (!graphite.isConnected()) {
graphite.connect();
} Defensive patterns
Strategy: validation
Validate before calling
if (graphite.isConnected()) {
LOG.debug("Graphite already connected; skipping connect()");
} else {
graphite.connect();
} Try / catch
try {
graphite.connect();
} catch (MetricsException e) {
if (e.getMessage() != null && e.getMessage().contains("Already connected")) {
LOG.debug("connect() was redundant; connection already up", e);
} else {
throw e;
}
} Prevention
- Never call connect() manually before write() — write() connects lazily and guards with isConnected()
- In tests, close() the Graphite instance in @After to avoid carrying a connection between cases
- Treat connect() as an internal lifecycle method; the public contract is write()/flush()/close()
When it happens
Trigger: Calling graphite.connect() twice without an intervening close(); a test fixture that connects in setUp() without closing between tests; custom wrapper code calling connect() manually before writes that would lazily connect anyway.
Common situations: Unit tests of GraphiteSink reusing a connected Graphite instance; application code embedding the Graphite inner class and double-connecting during reconfiguration.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- Error closing connection to Graphite
- Error closing connection to Graphite.
- Error creating connection, {}:{}
- write (b[{}], {}, {})
- Not implemented by the ${getClass().getSimpleName()} FileSys
AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22).
Data as JSON: /api/errors/6f8861dbd5f313b1.
Report an issue: GitHub.