无法将文件保存到磁盘“设备上没有剩余空间”

时间:2019-11-08 07:35:11

标签: java randomaccessfile

我有一个Java程序,可将文件写入Linux VM上的目录。写入490万个文件后,它失败并显示以下错误:

java.io.FileNotFoundException: /home/user/test/6BA30639CA0A2772AA0217312B3E847E2399E9A25F50F9960D6A670F4F2533EF.blob.lock (No space left on device)
        at java.io.RandomAccessFile.open0(Native Method)
        at java.io.RandomAccessFile.open(RandomAccessFile.java:316)
        at java.io.RandomAccessFile.<init>(RandomAccessFile.java:243)
        at java.io.RandomAccessFile.<init>(RandomAccessFile.java:124)
        at com.xyz.azure.AsyncADLSHashFileUploader.SaveToLocalStore(AsyncADLSHashFileUploader.java:231)

我的逻辑是:

public String SaveToLocalStore(byte[] bytes, String subfolder) throws Exception {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(bytes);
        byte[] sha256 = md.digest();
        String sha256str = bytesToHex(sha256);

        Path tmpDir = Paths.get(LocalRootDirectory.toString(), subfolder);
        Path tmpFile = Paths.get(tmpDir.toString(), sha256str + ".tmp");
        Path localFile = Paths.get(LocalRootDirectory.toString(), subfolder, sha256str + ".blob");
        String dstFile = getDestFileFromSrcFile(localFile.toString());

        //noinspection ResultOfMethodCallIgnored
        tmpDir.toFile().mkdirs();

        //We can make a safe assumption that if the .blob file is present, that means that it has been fully written to
        if (!Files.exists(localFile)) {

        try (
                    RandomAccessFile randomAccessFile = new RandomAccessFile(localFile + ".lock", "rw");
                    FileChannel fc = randomAccessFile.getChannel();
                    FileLock fileLock = fc.tryLock()) {
                //if other thread/process is already handling this file... no point of us doing anything
                if (fileLock != null) {
                    //local file is already there, no need to write it again
                    try (FileOutputStream fos = new FileOutputStream(tmpFile.toString())) {
                        fos.write(bytes);
                        fos.flush();
                        fos.close();
                        Files.move(tmpFile, localFile, REPLACE_EXISTING);

                        if(statisticsEnabled) {
                            synchronized (StatsLock) {
                                StatsSavedHashes.add(localFile.toString());
                            }
                        }

                    } catch (Exception e) {
                        LOGGER.error("Failed to create local temp file: " + tmpFile + ": " + e, e);
                        //cleanup temp if it exists
                        Files.deleteIfExists(tmpFile);
                        throw e;
                    }
                }
            } catch (OverlappingFileLockException e) {
                //other thread is handling it already, so just carry one as if(fileLock == null) was at play
                return dstFile;
            }
            catch (Exception e) {
                LOGGER.error("Error while saving local file: " + localFile + ": " + e, e);
                throw e;
            }
        }
          return dstFile;
}

我可以看到有80%以上的磁盘空间可用。 我还检查了文件系统上可用的inode。

我不知道问题出在哪里

有人可以在这方面帮助我吗?

0 个答案:

没有答案