{"record":{"id":"a00378e3da27e089","repo":"apache/hadoop","slug":"keypath-already-exists","errorCode":null,"errorMessage":"${keyPath} already exists","messagePattern":"(.+?) already exists","errorType":"exception","errorClass":"FileAlreadyExistsException","httpStatus":null,"severity":"error","filePath":"hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-hs/src/main/java/org/apache/hadoop/mapreduce/v2/hs/HistoryServerFileSystemStateStoreService.java","lineNumber":185,"sourceCode":"  @Override\n  public void removeToken(MRDelegationTokenIdentifier tokenId)\n      throws IOException {\n    if (LOG.isDebugEnabled()) {\n      LOG.debug(\"Removing token \" + tokenId.getSequenceNumber());\n    }\n    deleteFile(getTokenPath(tokenId));\n  }\n\n  @Override\n  public void storeTokenMasterKey(DelegationKey key) throws IOException {\n    if (LOG.isDebugEnabled()) {\n      LOG.debug(\"Storing master key \" + key.getKeyId());\n    }\n\n    Path keyPath = new Path(tokenKeysStatePath,\n        TOKEN_MASTER_KEY_FILE_PREFIX + key.getKeyId());\n    if (fs.exists(keyPath)) {\n      throw new FileAlreadyExistsException(keyPath + \" already exists\");\n    }\n\n    ByteArrayOutputStream memStream = new ByteArrayOutputStream();\n    DataOutputStream dataStream = new DataOutputStream(memStream);\n    try {\n      key.write(dataStream);\n      dataStream.close();\n      dataStream = null;\n    } finally {\n      IOUtils.cleanupWithLogger(LOG, dataStream);\n    }\n\n    createNewFile(keyPath, memStream.toByteArray());\n  }\n\n  @Override\n  public void removeTokenMasterKey(DelegationKey key)\n      throws IOException {","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-hs/src/main/java/org/apache/hadoop/mapreduce/v2/hs/HistoryServerFileSystemStateStoreService.java#L167-L203","documentation":"The filesystem state store persists each delegation master key as its own file (TOKEN_MASTER_KEY_FILE_PREFIX + keyId) under the token keys directory. storeTokenMasterKey throws FileAlreadyExistsException when that file already exists, meaning the store already contains a master key with the same keyId. Like the duplicate-token error, it signals stale state in the recovery store or a second writer rather than an intrinsic Hadoop fault.","triggerScenarios":"JHS restarted against a store that already contains a master key with the same key id; key id sequence reset while old key files remain; two JHS instances sharing one store URI both storing new master keys.","commonSituations":"Recovery directory reused across reinstalls; store restored from a snapshot; duplicated store location in HA-like deployments; experimentation leftover files in the keys directory.","solutions":["Confirm a single JHS instance owns the store location.","If master-key state can be discarded, stop JHS and clear the token keys area of the recovery store.","Otherwise remove only the conflicting key file (TOKEN_MASTER_KEY_<keyId>) and restart JHS.","Ensure the secret manager always starts from the same recovery store so key ids keep advancing instead of resetting."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  store.storeTokenMasterKey(key);\n} catch (org.apache.hadoop.fs.FileAlreadyExistsException e) {\n  // key file already in store: stale state or second writer; clean or skip\n}","preventionTips":["One writer per store location.","Clear the keys directory when the secret-manager key sequence is intentionally reset.","Never reuse a store snapshot across parallel JHS instances."],"tags":["mapreduce","job-history-server","delegation-key","state-store","recovery"],"backgroundTag":"file-already-exists","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}