{"record":{"id":"e253e7965497da05","repo":"apache/hadoop","slug":"blocksize-option-is-set-multiple-times","errorCode":null,"errorMessage":"BlockSize option is set multiple times","messagePattern":"BlockSize option is set multiple times","errorType":"validation","errorClass":"HadoopIllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/AbstractFileSystem.java","lineNumber":548,"sourceCode":"  public final FSDataOutputStream create(final Path f,\n      final EnumSet<CreateFlag> createFlag, Options.CreateOpts... opts)\n      throws AccessControlException, FileAlreadyExistsException,\n      FileNotFoundException, ParentNotDirectoryException,\n      UnsupportedFileSystemException, UnresolvedLinkException, IOException {\n    checkPath(f);\n    int bufferSize = -1;\n    short replication = -1;\n    long blockSize = -1;\n    int bytesPerChecksum = -1;\n    ChecksumOpt checksumOpt = null;\n    FsPermission permission = null;\n    Progressable progress = null;\n    Boolean createParent = null;\n \n    for (CreateOpts iOpt : opts) {\n      if (CreateOpts.BlockSize.class.isInstance(iOpt)) {\n        if (blockSize != -1) {\n          throw new HadoopIllegalArgumentException(\n              \"BlockSize option is set multiple times\");\n        }\n        blockSize = ((CreateOpts.BlockSize) iOpt).getValue();\n      } else if (CreateOpts.BufferSize.class.isInstance(iOpt)) {\n        if (bufferSize != -1) {\n          throw new HadoopIllegalArgumentException(\n              \"BufferSize option is set multiple times\");\n        }\n        bufferSize = ((CreateOpts.BufferSize) iOpt).getValue();\n      } else if (CreateOpts.ReplicationFactor.class.isInstance(iOpt)) {\n        if (replication != -1) {\n          throw new HadoopIllegalArgumentException(\n              \"ReplicationFactor option is set multiple times\");\n        }\n        replication = ((CreateOpts.ReplicationFactor) iOpt).getValue();\n      } else if (CreateOpts.BytesPerChecksum.class.isInstance(iOpt)) {\n        if (bytesPerChecksum != -1) {\n          throw new HadoopIllegalArgumentException(","sourceCodeStart":530,"sourceCodeEnd":566,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/AbstractFileSystem.java#L530-L566","documentation":"AbstractFileSystem.create scans the CreateOpts varargs once; the second CreateOpts.BlockSize it encounters throws HadoopIllegalArgumentException before any I/O happens. The API accepts each option type at most once so the resolved value is unambiguous.","triggerScenarios":"Passing CreateOpts.blockSize(...) twice in one create() call — classically a wrapper that appends its default blockSize after forwarding the caller's options array verbatim (Arrays.copyOf + append).","commonSituations":"Helper/utility create methods that add defaults on top of user opts; merging config-derived opts with caller opts by concatenation; refactors that append an option instead of replacing it.","solutions":["Remove the duplicate at the call site so blockSize is passed exactly once","Use CreateOpts.setOpt(CreateOpts.blockSize(n), opts), which replaces an existing occurrence instead of appending","Run opts through a dedupe that keeps one entry per option class before calling create"],"exampleFix":"// before\nopts = Arrays.copyOf(opts, opts.length + 1);\nopts[opts.length - 1] = CreateOpts.blockSize(bs); // caller may already have passed one\nfc.create(f, flag, opts);\n\n// after\nopts = CreateOpts.setOpt(CreateOpts.blockSize(bs), opts); // replaces, never duplicates\nfc.create(f, flag, opts);","handlingStrategy":"validation","validationCode":"static Options.CreateOpts[] dedupe(Options.CreateOpts... opts) {\n  Map<Class<?>, Options.CreateOpts> m = new LinkedHashMap<>();\n  for (Options.CreateOpts o : opts) m.put(o.getClass(), o); // last occurrence wins\n  return m.values().toArray(new Options.CreateOpts[0]);\n}\n// fc.create(f, flag, dedupe(opts));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["In wrappers, always use CreateOpts.setOpt(opt, opts) which replaces instead of appending","Never append a default option to an array that may already contain it","Unit-test option-merging helpers with inputs that already carry every option"],"tags":["hadoop","file-create","options","varargs","validation"],"backgroundTag":"duplicate-create-option","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}