{"record":{"id":"6f8861dbd5f313b1","repo":"apache/hadoop","slug":"already-connected-to-graphite","errorCode":null,"errorMessage":"Already connected to Graphite","messagePattern":"Already connected to Graphite","errorType":"exception","errorClass":"MetricsException","httpStatus":null,"severity":"warning","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/sink/GraphiteSink.java","lineNumber":145,"sourceCode":"  }\n\n  public static class Graphite {\n    private final static int MAX_CONNECTION_FAILURES = 5;\n\n    private String serverHost;\n    private int serverPort;\n    private Writer writer = null;\n    private Socket socket = null;\n    private int connectionFailures = 0;\n\n    public Graphite(String serverHost, int serverPort) {\n      this.serverHost = serverHost;\n      this.serverPort = serverPort;\n    }\n\n    public void connect() {\n      if (isConnected()) {\n        throw new MetricsException(\"Already connected to Graphite\");\n      }\n      if (tooManyConnectionFailures()) {\n        // return silently (there was ERROR in logs when we reached limit for the first time)\n        return;\n      }\n      try {\n          // Open a connection to Graphite server.\n        socket = new Socket(serverHost, serverPort);\n        writer = new OutputStreamWriter(socket.getOutputStream(),\n                StandardCharsets.UTF_8);\n      } catch (Exception e) {\n        connectionFailures++;\n        if (tooManyConnectionFailures()) {\n          // first time when connection limit reached, report to logs\n          LOG.error(\"Too many connection failures, would not try to connect again.\");\n        }\n        throw new MetricsException(\"Error creating connection, \" +\n            serverHost + \":\" + serverPort, e);","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/sink/GraphiteSink.java#L127-L163","documentation":"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.","triggerScenarios":"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.","commonSituations":"Unit tests of GraphiteSink reusing a connected Graphite instance; application code embedding the Graphite inner class and double-connecting during reconfiguration.","solutions":["Guard the call: if (!graphite.isConnected()) { graphite.connect(); }","Drop manual connect() entirely — write() connects lazily when needed","Call close() before intentionally reconnecting"],"exampleFix":"// before\ngraphite.connect();   // later in the same session, still connected\ngraphite.connect();   // MetricsException: Already connected to Graphite\n\n// after\nif (!graphite.isConnected()) {\n  graphite.connect();\n}","handlingStrategy":"validation","validationCode":"if (graphite.isConnected()) {\n  LOG.debug(\"Graphite already connected; skipping connect()\");\n} else {\n  graphite.connect();\n}","typeGuard":null,"tryCatchPattern":"try {\n  graphite.connect();\n} catch (MetricsException e) {\n  if (e.getMessage() != null && e.getMessage().contains(\"Already connected\")) {\n    LOG.debug(\"connect() was redundant; connection already up\", e);\n  } else {\n    throw e;\n  }\n}","preventionTips":["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()"],"tags":["graphite","metrics2","state","api-misuse"],"backgroundTag":"invalid-state-transition","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}