{"record":{"id":"7b8aa4da7a379c12","repo":"MyCATApache/Mycat-Server","slug":"failed-to-create-a-temp-directory-under-rootdir","errorCode":null,"errorMessage":"Failed to create a temp directory (under ${rootDir}) after ${maxAttempts} attempts!","messagePattern":"Failed to create a temp directory \\(under (.+?)\\) after (.+?) attempts!","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/memory/unsafe/utils/JavaUtils.java","lineNumber":335,"sourceCode":"      unit = \"KB\";\n    } else {\n      value =   size;\n      unit = \"B\";\n    }\n\n    return value + unit;\n  }\n\n\n  public static File createDirectory(String rootDir, String blockmgr) throws IOException {\n\n    int attempts = 0;\n    int maxAttempts = MAX_DIR_CREATION_ATTEMPTS;\n    File dir = null;\n    while (dir == null) {\n      attempts += 1;\n      if (attempts > maxAttempts) {\n        throw new IOException(\"Failed to create a temp directory (under \" + rootDir + \") after \" +\n                maxAttempts + \" attempts!\");\n      }\n      try {\n        dir = new File(rootDir, blockmgr + \"-\" + UUID.randomUUID().toString());\n        if (dir.exists() || !dir.mkdirs()) {\n          dir = null;\n        }\n      } catch (Exception e) {\n        logger.error(e.getMessage());\n      }\n    }\n\n    return dir.getCanonicalFile();\n  }\n\n  /* Calculates 'x' modulo 'mod', takes to consideration sign of x,\n* i.e. if 'x' is negative, than 'x' % 'mod' is negative too\n* so function return (x % mod) + mod in that case.","sourceCodeStart":317,"sourceCodeEnd":353,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/memory/unsafe/utils/JavaUtils.java#L317-L353","documentation":"createDirectory attempts up to MAX_DIR_CREATION_ATTEMPTS to create a uniquely-named temp subdirectory (UUID-suffixed) under a root directory. If every mkdirs() call fails or collides, it throws IOException. This guards against indefinite loops when the filesystem is broken or exhausted.","triggerScenarios":"Calling JavaUtils.createDirectory(rootDir) when rootDir does not exist or is not writable, the filesystem is full, or mkdirs() repeatedly fails (e.g. root is a read-only mount or a file exists with the same path prefix).","commonSituations":"Deployments where the configured local scratch directory (e.g. /tmp or a block-manager dir) is missing or read-only; disk-full conditions on data nodes; running as a user without write permission on the parent directory.","solutions":["Ensure the root directory exists and is writable by the process user (mkdir -p and chown/chmod it).","Check disk space and mount state (df -h; mount) for the filesystem holding rootDir.","Fix any configured scratch/spill directories pointing at read-only or nonexistent paths.","Catch IOException and fall back to an alternative temp root (e.g. java.io.tmpdir) with a logged warning."],"exampleFix":"// before\nFile dir = JavaUtils.createDirectory(new File(\"/mnt/ro-scratch\"), \"blockmgr\");\n// after\nFile root = new File(\"/mnt/ro-scratch\");\nif (!root.canWrite()) root = new File(System.getProperty(\"java.io.tmpdir\"));\nFile dir = JavaUtils.createDirectory(root, \"blockmgr\");","handlingStrategy":"fallback","validationCode":"File root = new File(rootDir);\nif (!root.isDirectory() || !root.canWrite()) { throw new IllegalStateException(\"scratch dir unusable: \" + rootDir); }","typeGuard":null,"tryCatchPattern":"try { return JavaUtils.createDirectory(root, prefix); } catch (IOException e) { log.warn(\"temp dir creation failed under {}\", root, e); return JavaUtils.createDirectory(new File(System.getProperty(\"java.io.tmpdir\")), prefix); }","preventionTips":["Verify scratch directories exist and are writable at process startup","Monitor disk space on spill/scratch volumes","Run the process as a user with write access to configured dirs"],"tags":["filesystem","io","temp-directory"],"backgroundTag":"file-write-failed","analyzedSha":"65f8d8beb752f935752f2a0eec0ab017facab9ef","analyzedAt":"2026-09-11T00:12:21.696Z","contentChangedAt":"2026-09-11T00:12:21.696Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}