{"record":{"id":"1b56a97700230075","repo":"apache/hadoop","slug":"a-valid-writer-is-required-for-constructing-a-repl","errorCode":null,"errorMessage":"A valid writer is required for constructing a Replica from block {}","messagePattern":"A valid writer is required for constructing a Replica from block (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/ReplicaBuilder.java","lineNumber":240,"sourceCode":"          return new ReplicaBeingWritten(blockId, genStamp, volume,\n              directoryUsed, bytesToReserve);\n        }\n      }\n    }\n  }\n\n  private LocalReplicaInPipeline buildTemporaryReplica()\n      throws IllegalArgumentException {\n    if (null != fromReplica &&\n        fromReplica.getState() == ReplicaState.TEMPORARY) {\n      return new LocalReplicaInPipeline((LocalReplicaInPipeline) fromReplica);\n    } else if (null != fromReplica) {\n      throw new IllegalArgumentException(\"Incompatible fromReplica \"\n          + \"state: \" + fromReplica.getState());\n    } else {\n      if (null != block) {\n        if (null == writer) {\n          throw new IllegalArgumentException(\"A valid writer is \"\n              + \"required for constructing a Replica from block \"\n              + block.getBlockId());\n        }\n        return new LocalReplicaInPipeline(block, volume, directoryUsed,\n            writer);\n      } else {\n        if (length != -1) {\n          return new LocalReplicaInPipeline(blockId, length, genStamp,\n              volume, directoryUsed, writer, bytesToReserve);\n        } else {\n          return new LocalReplicaInPipeline(blockId, genStamp, volume,\n              directoryUsed, bytesToReserve);\n        }\n      }\n    }\n  }\n\n  private LocalReplica buildFinalizedReplica() throws IllegalArgumentException {","sourceCodeStart":222,"sourceCodeEnd":258,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/ReplicaBuilder.java#L222-L258","documentation":"When ReplicaBuilder constructs a LocalReplicaInPipeline (TEMPORARY) from a Block via setBlock, it requires a writer thread because in-pipeline replicas track the writing thread. A null writer (no setWriterThread call) triggers IllegalArgumentException 'A valid writer is required for constructing a Replica from block'.","triggerScenarios":"new ReplicaBuilder(ReplicaState.TEMPORARY).setBlock(block).buildLocalReplicaInPipeline() with no .setWriterThread(...) - the block branch of buildTemporaryReplica() finds writer == null and throws.","commonSituations":"Staging replicas for replication tests or DataNode restart flows where code was written for finalized replicas (which need no writer) and reused for TEMPORARY; builder chains where setWriterThread was accidentally dropped during refactoring.","solutions":["Call .setWriterThread(Thread.currentThread()) before build when using setBlock.","Or use the field-based constructors path (setBlockId/setGenerationStamp/.../setWriterThread) - writer is still mandatory for in-pipeline replicas.","Add a builder wrapper that asserts writer != null when block != null."],"exampleFix":"// before\nLocalReplicaInPipeline r = new ReplicaBuilder(ReplicaState.TEMPORARY)\n    .setBlock(block).buildLocalReplicaInPipeline();\n\n// after\nLocalReplicaInPipeline r = new ReplicaBuilder(ReplicaState.TEMPORARY)\n    .setBlock(block)\n    .setWriterThread(Thread.currentThread())\n    .buildLocalReplicaInPipeline();","handlingStrategy":"validation","validationCode":"LocalReplicaInPipeline r = new ReplicaBuilder(ReplicaState.TEMPORARY)\n    .setBlock(block)\n    .setWriterThread(Thread.currentThread())  // required with setBlock\n    .buildLocalReplicaInPipeline();","typeGuard":null,"tryCatchPattern":"try {\n  r = builder.buildLocalReplicaInPipeline();\n} catch (IllegalArgumentException e) {\n  throw new IllegalArgumentException(\"TEMPORARY construction failed (missing writer): \"\n      + e.getMessage(), e);\n}","preventionTips":["Remember: any in-pipeline replica (RBW or TEMPORARY) built from a Block needs setWriterThread.","Centralize replica construction in one helper that sets the writer once.","Assert writer != null in tests before build."],"tags":["hdfs","datanode","replica","builder","temporary-replica","required-parameter"],"backgroundTag":"missing-builder-parameter","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}