{"record":{"id":"76d5beea67ffff37","repo":"apache/hadoop","slug":"existing-policy-currentpolicy-getname-c","errorCode":null,"errorMessage":"\"Existing policy \" + currentPolicy.getName() + \" cannot be changed after file creation.\"","messagePattern":"\"Existing policy \" \\+ currentPolicy\\.getName\\(\\) \\+ \" cannot be changed after file creation\\.\"","errorType":"exception","errorClass":"HadoopIllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSDirAttrOp.java","lineNumber":458,"sourceCode":"          + iip.getPath());\n    }\n    final int snapshotId = iip.getLatestSnapshotId();\n    if (inode.isFile()) {\n      FSDirectory.LOG.debug(\"DIR* FSDirAAr.unprotectedSetStoragePolicy for \" +\n              \"File.\");\n      if (policyId != HdfsConstants.BLOCK_STORAGE_POLICY_ID_UNSPECIFIED) {\n        BlockStoragePolicy newPolicy = bm.getStoragePolicy(policyId);\n        if (newPolicy.isCopyOnCreateFile()) {\n          throw new HadoopIllegalArgumentException(\"Policy \" + newPolicy\n              + \" cannot be set after file creation.\");\n        }\n      }\n\n      BlockStoragePolicy currentPolicy =\n          bm.getStoragePolicy(inode.getLocalStoragePolicyID());\n\n      if (currentPolicy != null && currentPolicy.isCopyOnCreateFile()) {\n        throw new HadoopIllegalArgumentException(\n            \"Existing policy \" + currentPolicy.getName() +\n                \" cannot be changed after file creation.\");\n      }\n      inode.asFile().setStoragePolicyID(policyId, snapshotId);\n    } else if (inode.isDirectory()) {\n      FSDirectory.LOG.debug(\"DIR* FSDirAAr.unprotectedSetStoragePolicy for \" +\n              \"Directory.\");\n      setDirStoragePolicy(fsd, iip, policyId);\n    } else {\n      throw new FileNotFoundException(iip.getPath()\n          + \" is not a file or directory\");\n    }\n  }\n\n  private static void setDirStoragePolicy(\n      FSDirectory fsd, INodesInPath iip, byte policyId) throws IOException {\n    INode inode = FSDirectory.resolveLastINode(iip);\n    List<XAttr> existingXAttrs = XAttrStorage.readINodeXAttrs(inode);","sourceCodeStart":440,"sourceCodeEnd":476,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSDirAttrOp.java#L440-L476","documentation":"Mirror of the copy-on-create check: if the file already carries a copy-on-create policy (LAZY_PERSIST in the default suite), changing it to any other policy after creation is rejected - placement decisions were made when the blocks were written and cannot be switched retroactively. The message names the existing policy that is locked in.","triggerScenarios":"setStoragePolicy(file, otherPolicy) where the file's current policy is LAZY_PERSIST: e.g. 'hdfs storagepolicies -setStoragePolicy -policy HOT <lazy-persist-file>' when RAM_DISK fills and ops try to migrate files to disk-backed storage.","commonSituations":"Migrating lazy-persist temp data when memory pressure hits; lifecycle scripts walking trees and 'normalizing' all policies to HOT/WARM/COLD without checking the current one.","solutions":["Rewrite the file: create a new file under a directory with the desired policy (or default policy), copy the content, then replace the original.","Prevent it earlier: scope LAZY_PERSIST to throwaway scratch data you never intend to migrate.","For directories carrying the policy, 'hdfs storagepolicies -unsetStoragePolicy <dir>' stops inheritance for future children (existing files still need rewriting)."],"exampleFix":"# before: rejected, existing LAZY_PERSIST policy is locked in\nhdfs storagepolicies -setStoragePolicy -policy HOT /scratch/part-0\n\n# after: rewrite via copy under a HOT directory, then swap\nhdfs dfs -mkdir -p /staging\nhdfs storagepolicies -setStoragePolicy -policy HOT /staging\nhdfs dfs -cp /scratch/part-0 /staging/part-0\nhdfs dfs -mv /staging/part-0 /scratch/part-0","handlingStrategy":"validation","validationCode":"BlockStoragePolicy current = ((DistributedFileSystem) fs).getStoragePolicy(path);\nif (current != null && current.isCopyOnCreateFile()) {\n  // locked in at creation: must rewrite into a new file with the desired policy\n  Path tmp = new Path(path.getParent(), path.getName() + \".migrate\");\n  FileUtil.copy(fs, path, fs, tmp, false, true, fs.getConf());\n  fs.setStoragePolicy(tmp, \"HOT\");\n  fs.delete(path, false);\n  fs.rename(tmp, path);\n} else {\n  fs.setStoragePolicy(path, \"HOT\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  fs.setStoragePolicy(path, \"HOT\");\n} catch (HadoopIllegalArgumentException e) {\n  if (e.getMessage().contains(\"cannot be changed after file creation\")) {\n    // existing copy-on-create policy: rewrite the file instead of retrying\n  }\n}","preventionTips":["Check getStoragePolicy(path).isCopyOnCreateFile() before policy migrations.","Keep LAZY_PERSIST scoped to throwaway data so migrations are never needed."],"tags":["hdfs","storage-policy","lazy-persist","copy-on-create"],"backgroundTag":"copy-on-create-policy","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}