{"record":{"id":"6ea211bda719c2ae","repo":"apache/hadoop","slug":"error-creating","errorCode":null,"errorMessage":"Error creating {}","messagePattern":"Error creating (.+?)","errorType":"exception","errorClass":"MetricsException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/sink/FileSink.java","lineNumber":53,"sourceCode":"\n/**\n * A metrics sink that writes to a file\n */\n@InterfaceAudience.Public\n@InterfaceStability.Evolving\npublic class FileSink implements MetricsSink, Closeable {\n  private static final String FILENAME_KEY = \"filename\";\n  private PrintStream writer;\n\n  @Override\n  public void init(SubsetConfiguration conf) {\n    String filename = conf.getString(FILENAME_KEY);\n    try {\n      writer = filename == null ? System.out\n          : new PrintStream(Files.newOutputStream(Paths.get(filename)),\n                            true, \"UTF-8\");\n    } catch (Exception e) {\n      throw new MetricsException(\"Error creating \"+ filename, e);\n    }\n  }\n\n  @Override\n  public void putMetrics(MetricsRecord record) {\n    writer.print(record.timestamp());\n    writer.print(\" \");\n    writer.print(record.context());\n    writer.print(\".\");\n    writer.print(record.name());\n    String separator = \": \";\n    for (MetricsTag tag : record.tags()) {\n      writer.print(separator);\n      separator = \", \";\n      writer.print(tag.name());\n      writer.print(\"=\");\n      writer.print(tag.value());\n    }","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/sink/FileSink.java#L35-L71","documentation":"FileSink is the metrics2 file sink. On init() it opens an auto-flush UTF-8 PrintStream on the file named by the sink's filename property (falls back to System.out when no filename is configured). If Files.newOutputStream(Paths.get(filename)) throws — parent directory missing, permission denied, path is a directory, read-only filesystem — the sink wraps the cause in MetricsException(\"Error creating \" + filename) and sink initialization fails.","triggerScenarios":"hadoop-metrics2.properties sets <prefix>.sink.<instance>.class=org.apache.hadoop.metrics2.sink.FileSink and <prefix>.sink.<instance>.filename to a path whose parent directory does not exist or is not writable by the daemon user, or that names a directory, or lives on a read-only mount.","commonSituations":"Hadoop daemons (running as hdfs/yarn/nobody) pointed at /var/log/... directories owned by root; containers with read-only filesystems; SELinux denials; relative filenames resolving against an unexpected working directory.","solutions":["Create the parent directory and give the daemon user ownership: mkdir -p /var/log/app && chown <daemonuser> /var/log/app","Verify writability as the daemon user: sudo -u <daemonuser> touch <filename>","Point filename at a known-writable location such as the daemon's log directory","Omit the filename property entirely to send metrics to stdout"],"exampleFix":"# before (hadoop-metrics2.properties)\ndatanode.sink.file.class=org.apache.hadoop.metrics2.sink.FileSink\ndatanode.sink.file.filename=/var/log/hadoop/metrics.log  # /var/log/hadoop missing\n\n# after\ndatanode.sink.file.class=org.apache.hadoop.metrics2.sink.FileSink\ndatanode.sink.file.filename=/var/log/hadoop-yarn/containers/metrics.log  # pre-created, owned by 'yarn'","handlingStrategy":"validation","validationCode":"Path p = Paths.get(filename);\nPath parent = p.getParent() != null ? p.getParent() : Paths.get(\".\");\nif (!Files.isDirectory(parent) || !Files.isWritable(parent)) {\n  throw new IllegalStateException(\"FileSink target not writable: \" + parent\n      + \" (run as user \" + System.getProperty(\"user.name\") + \")\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  sink.init(subsetConf);\n} catch (MetricsException e) {\n  // e.getCause() holds the original IOException from Files.newOutputStream\n  LOG.error(\"FileSink init failed for {}: {}\", filename, e.getCause(), e);\n}","preventionTips":["Pre-create the metrics output directory in your deployment scripts and chown it to the daemon user","Smoke-test writability as the daemon user before starting: sudo -u <daemonuser> touch <file>","Prefer absolute paths under the daemon's own log directory; omit filename to fall back to stdout in tests"],"tags":["metrics2","file-sink","permissions","configuration"],"backgroundTag":"file-open-failed","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}