Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truncating queue files #374

Closed
rupinder10 opened this issue Aug 16, 2017 · 6 comments
Closed

Truncating queue files #374

rupinder10 opened this issue Aug 16, 2017 · 6 comments

Comments

@rupinder10
Copy link

I have a situation where the rolling daily files see periods of high activity. So some files are large and some relatively very small. I would like to reclaim the wasted space in the files that dont use it. Is there a way to do this in the StoreListener so that when we release a file we can truncate its length to needed size only ?

This is on Windows so rapidly growing files cause issues with the team that manages the file system.

@TerryW9
Copy link

TerryW9 commented Aug 17, 2017

Hi Rupinder,

Thank you for the question.
We will try to get back to you with an answer as soon as possible.

Best Regards,
Terry

@epickrram
Copy link
Contributor

Hi Rupinder,
there is no supported API for retrieving the actual data length of a queue file.
If you really needed to, you could implement a StoreFileListener to perform the truncate operation like so:

@Override
public void onReleased(final int cycle, final File file) {
    try (SingleChronicleQueue copy = queueBuilder(queueDirectory, wireType).build()) {
        final WireStore wireStore = copy.
                storeForCycle(cycle, 0, false);
        final long lastWritePositionInStore = wireStore.writePosition();
        final int header = wireStore.bytes().readVolatileInt(lastWritePositionInStore);
        final int length = Wires.lengthOf(header);
        final long endOfLastRecord = lastWritePositionInStore + length + Wires.SPB_HEADER_SIZE;
        System.out.printf("Last write at %d, entry length %d, truncating to %d%n",
                lastWritePositionInStore, length, endOfLastRecord);

        final RandomAccessFile raf = new RandomAccessFile(file, "rw");
        raf.setLength(endOfLastRecord);
        raf.close();
    } catch (FileNotFoundException e) {
        // ignore
    } catch (IOException e) {
        // ignore
    }
}

However, please note that this is not a supported API, and relies on the internal format of the queue-file header. This code works with the current version of chronicle-queue, but may break in future versions.

If you decide to use such a method, you should carry out extensive testing to make sure that you are not accidentally truncating data from the end of the queue files. A safer way to solve the problem is to acquire more disk space.

@peter-lawrey
Copy link
Member

peter-lawrey commented Aug 17, 2017 via email

@rupinder10
Copy link
Author

Peter,

Your points are very valid. I was debating whether to do it or not and this certainly gives me some arguments to justify not doing it.

Another fallow up question then is about the block size. What is the impact of selecting a small block size when the files have to grow ? And what if the files are written using a certain block size but read with a different one. Does that cause issues ? I ask because, in some cases, files are copied around systems and the reader may not use the same block size. One way would be to wrap it in another API that does not allow them to change the block size.

@peter-lawrey
Copy link
Member

peter-lawrey commented Aug 17, 2017 via email

@dpisklov
Copy link
Contributor

Another thing to note, queue files are zipped fairly well, especially the empty sections at the end. So one can implement a cron job to zip the files from old roll cycles if space is an issue.
I'll close the issue for now, @rupinder10 feel free to reopen if you feel there's more needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants