{"record":{"id":"aff77161787f150c","repo":"apache/hadoop","slug":"metrics-are-read-only","errorCode":null,"errorMessage":"Metrics are read-only.","messagePattern":"Metrics are read-only\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/impl/MetricsSourceAdapter.java","lineNumber":124,"sourceCode":"      throws AttributeNotFoundException, MBeanException, ReflectionException {\n    updateJmxCache();\n    synchronized(this) {\n      Attribute a = attrCache.get(attribute);\n      if (a == null) {\n        throw new AttributeNotFoundException(attribute +\" not found\");\n      }\n      if (LOG.isDebugEnabled()) {\n        LOG.debug(attribute +\": \"+ a);\n      }\n      return a.getValue();\n    }\n  }\n\n  @Override\n  public void setAttribute(Attribute attribute)\n      throws AttributeNotFoundException, InvalidAttributeValueException,\n             MBeanException, ReflectionException {\n    throw new UnsupportedOperationException(\"Metrics are read-only.\");\n  }\n\n  @Override\n  public AttributeList getAttributes(String[] attributes) {\n    updateJmxCache();\n    synchronized(this) {\n      AttributeList ret = new AttributeList();\n      for (String key : attributes) {\n        Attribute attr = attrCache.get(key);\n        if (LOG.isDebugEnabled()) {\n          LOG.debug(key +\": \"+ attr);\n        }\n        ret.add(attr);\n      }\n      return ret;\n    }\n  }\n","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/impl/MetricsSourceAdapter.java#L106-L142","documentation":"MetricsSourceAdapter implements the JMX DynamicMBean contract, but metrics2 MBeans are read-only views: setAttribute(Attribute) unconditionally throws UnsupportedOperationException('Metrics are read-only.'). No code path mutates a metric through JMX; metrics are produced only by the instrumented component.","triggerScenarios":"Calling MBeanServerConnection.setAttribute, JConsole's set-attribute dialog, or any management client that attempts to write a value on a Hadoop metrics MBean ('Hadoop:service=...,name=...').","commonSituations":"Ops tooling that assumes all JMX attributes are writable; generic JMX dashboards that offer an edit box for every attribute; attempts to 'pin' a gauge at runtime by writing it through JMX.","solutions":["Remove the write attempt and change the underlying component/config instead of the MBean","If runtime writes are required, expose your own custom MBean rather than a metrics2 source","Guard the client: check MBeanAttributeInfo.isWritable() (always false here) before calling setAttribute"],"exampleFix":"// before\nmbsc.setAttribute(objectName, new Attribute(\"NumOps\", 5));\n\n// after\nMBeanAttributeInfo ai = Arrays.stream(mbsc.getMBeanInfo(objectName).getAttributes())\n    .filter(a -> a.getName().equals(\"NumOps\")).findFirst().orElse(null);\nif (ai != null && ai.isWritable()) {  // false for metrics2 beans\n  mbsc.setAttribute(objectName, new Attribute(\"NumOps\", 5));\n}","handlingStrategy":"validation","validationCode":"MBeanAttributeInfo ai = Arrays.stream(mbsc.getMBeanInfo(objectName).getAttributes())\n    .filter(a -> a.getName().equals(attr)).findFirst().orElse(null);\nif (ai != null && ai.isWritable()) {  // always false for metrics2 beans\n  mbsc.setAttribute(objectName, new Attribute(attr, value));\n}","typeGuard":null,"tryCatchPattern":"try {\n  mbsc.setAttribute(objectName, new Attribute(attr, value));\n} catch (UnsupportedOperationException e) {\n  // metrics2 MBeans are read-only by design; configure the component instead\n}","preventionTips":["Treat all Hadoop metrics2 MBeans as read-only views","Check MBeanAttributeInfo.isWritable() before any setAttribute call","Route intended changes through component configuration, not JMX writes"],"tags":["jmx","metrics2","hadoop","read-only","unsupported-operation"],"backgroundTag":"jmx-mbean-read-only","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}