question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using StoreFileListener for removing CQ files - dealing with multiple onAcquired/onReleased events

See original GitHub issue

We use CQ version 5.17.30, setting up a single CQ instance like this:

SingleChronicleQueueBuilder.single(queueDir)
                .rollCycle(RollCycles.valueOf(rollCycle))
                .storeFileListener(new QueueFileListener())
                .build();

and for writing to the queue: fileQueue.acquireAppender().writeText().

We see many onAcquired / onReleased calls: 2020-05-17 20:42:42,740 DEBUG h.l.a.i.QueueFileListener - Acquired file 20200517-18.cq4 … work … 2020-05-17 20:42:43,073 DEBUG h.l.a.i.QueueFileListener - Released file 20200517-18.cq4

And, presumably, when using multiple threads: 2020-05-17 20:42:44,926 DEBUG h.l.a.i.QueueFileListener - Acquired file 20200517-18.cq4 2020-05-17 20:42:44,926 DEBUG h.l.a.i.QueueFileListener - Acquired file 20200517-18.cq4 2020-05-17 20:42:44,927 DEBUG h.l.a.i.QueueFileListener - Acquired file 20200517-18.cq4 … work … 2020-05-17 20:42:45,091 DEBUG h.l.a.i.QueueFileListener - Released file 20200517-18.cq4 2020-05-17 20:42:45,093 DEBUG h.l.a.i.QueueFileListener - Released file 20200517-18.cq4 2020-05-17 20:42:45,094 DEBUG h.l.a.i.QueueFileListener - Released file 20200517-18.cq4

Questions on this behavior and the correct way to delete files:

  1. Based on this Stack Overflow question and your answer to it: can you confirm that this is expected behavior?

  2. onAcquired / onReleased are used to maintain a ConcurrentHashMap with pointers to files that can (eventually) be deleted (in a separate thread).

    @Override
    public void onAcquired(int cycle, File file) {

        if (file != null) {
            releasedFiles.remove(file);
            logger.debug(format("Acquired file %s", file.getAbsolutePath()));
        }
    }

    @Override
    public void onReleased(int cycle, File file) {

        if (file != null) {
            releasedFiles.put(file, new ReleasedFileData(cycle));
            logger.debug(format("Released file %s", file.getAbsolutePath()));
        }
    }

In ReleasedFileData we track the cycle and the current timestamp:

    public ReleasedFileData(int cycle) {
        this.cycle = cycle;
        this.releasedAt = LocalDateTime.now();
    }

and determine if the file can be deleted using the following logic:

    public boolean fileCanBeDeleted(int currentCycle) {

        LocalDateTime earlier = LocalDateTime.now().minusMinutes(10);
        return cycle < currentCycle && releasedAt.isBefore(earlier);
    }

Are we overly cautious, e.g. would return cycle < currentCycle be sufficient?

Thanks and kudos on the great product!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sjoerd-michelscommented, May 19, 2020

Thanks for your fast feedback and for confirming that this is the expected behavior. Perhaps you could update the documentation, because it means that files cannot be deleted based on the onReleased event only. I would also appreciate your feedback on the “time interval safety check”, for now we are keeping that in place.

0reactions
dpisklovcommented, May 20, 2020

@sjoerd-michels you’re welcome, although in the future we would appreciate if you raise questions on stackoverflow (we monitor it closely), we are trying to keep github for the real code issues.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chronicle Queue: StoreFileListener multiple onAcquired and ...
Below is simple implementation for Writer and Reader with StoreFileListner. In Reader I am getting multiple onAcquired and onReleased events.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found