{"record":{"id":"fde696b11c7f7a86","repo":"apache/hadoop","slug":"adddirective-you-cannot-specify-an-id-for-this-op","errorCode":null,"errorMessage":"addDirective: you cannot specify an ID for this operation.","messagePattern":"addDirective: you cannot specify an ID for this operation\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSNDNCacheOp.java","lineNumber":41,"sourceCode":"import org.apache.hadoop.hdfs.protocol.CacheDirectiveInfo;\nimport org.apache.hadoop.hdfs.protocol.CachePoolEntry;\nimport org.apache.hadoop.hdfs.protocol.CachePoolInfo;\nimport org.apache.hadoop.security.AccessControlException;\n\nimport java.io.IOException;\nimport java.util.EnumSet;\n\nclass FSNDNCacheOp {\n  static CacheDirectiveInfo addCacheDirective(\n      FSNamesystem fsn, CacheManager cacheManager,\n      CacheDirectiveInfo directive, EnumSet<CacheFlag> flags,\n      boolean logRetryCache)\n      throws IOException {\n\n    final FSPermissionChecker pc = getFsPermissionChecker(fsn);\n\n    if (directive.getId() != null) {\n      throw new IOException(\"addDirective: you cannot specify an ID \" +\n          \"for this operation.\");\n    }\n    CacheDirectiveInfo effectiveDirective =\n        cacheManager.addDirective(directive, pc, flags);\n    fsn.getEditLog().logAddCacheDirectiveInfo(effectiveDirective,\n        logRetryCache);\n    return effectiveDirective;\n  }\n\n  static void modifyCacheDirective(\n      FSNamesystem fsn, CacheManager cacheManager, CacheDirectiveInfo directive,\n      EnumSet<CacheFlag> flags, boolean logRetryCache) throws IOException {\n    final FSPermissionChecker pc = getFsPermissionChecker(fsn);\n\n    cacheManager.modifyDirective(directive, pc, flags);\n    fsn.getEditLog().logModifyCacheDirectiveInfo(directive, logRetryCache);\n  }\n","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSNDNCacheOp.java#L23-L59","documentation":"Server-side wrapper for the add-cache-directive RPC: CacheManager assigns directive IDs itself, so the caller-supplied CacheDirectiveInfo must not carry one. Passing an info with a non-null id is treated as client misuse and rejected before any state change or edit-log write.","triggerScenarios":"DFSClient addCacheDirective (DistributedFileSystem.addCacheDirective or 'hdfs dfsadmin -addDirective') with a CacheDirectiveInfo built via setId(...), or a Builder reused from a listCacheDirectives/modify flow that already carries an id.","commonSituations":"Copy-pasting a modify-directive path into an add path; reusing builder objects populated from existing directives; UI/tools copying a listed directive for re-creation.","solutions":["Build the CacheDirectiveInfo for adds with only path/pool/replication/expiration — never setId","If the info came from an existing directive, create a fresh Builder and copy only the mutable fields","Use the returned id from addCacheDirective to track the new directive afterwards"],"exampleFix":"// before\nCacheDirectiveInfo info = new CacheDirectiveInfo.Builder()\n    .setId(37L)                      // rejected: IDs are assigned by the NameNode\n    .setPath(new Path(\"/data\"))\n    .build();\ndfs.addCacheDirective(info);\n\n// after\nCacheDirectiveInfo info = new CacheDirectiveInfo.Builder()\n    .setPath(new Path(\"/data\"))\n    .setReplication((short) 1)\n    .build();\nlong id = dfs.addCacheDirective(info);   // ID assigned server-side and returned","handlingStrategy":"type-guard","validationCode":"if (directive.getId() != null) {\n  directive = new CacheDirectiveInfo.Builder(directive)\n      .setId(null)   // IDs are assigned by the NameNode on add\n      .build();\n}\nlong id = dfs.addCacheDirective(directive);","typeGuard":"static boolean isAddableCacheDirective(CacheDirectiveInfo info) {\n  return info != null && info.getId() == null;\n}","tryCatchPattern":null,"preventionTips":["Always build fresh CacheDirectiveInfo.Builder objects for add operations","Reserve setId for modify/delete paths where the ID identifies an existing directive","Use the long returned by addCacheDirective instead of pre-assigning IDs client-side"],"tags":["hadoop","hdfs","namenode","cache-directive","api-misuse"],"backgroundTag":"invalid-argument","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}