0

CQ: 4.5.27.

I have created 2 chronicle for one for write (single thread) and other reader (N threads) reading same file using single chronicleReader.

SingleChronicleQueue chronicleWriter = chronicleFactory.createChronicle("Writer", path, RollCycles.MINUTELY);

SingleChronicleQueue chronicleReader  = chronicleFactory.createChronicle("Reader", path, RollCycles.MINUTELY);


public class ChronicleFactory {

ChronicleProcessorImpl chronicleProcessor = new ChronicleProcessorImpl();
public SingleChronicleQueue createChronicle(String instance, String persistenceDir, RollCycles rollCycles) {
    SingleChronicleQueue chronicle = null;
    try {
        chronicle = SingleChronicleQueueBuilder.binary(persistenceDir).rollCycle(rollCycles).storeFileListener(new StoreFileListener() {
            @Override
            public void onReleased(int i, File file) {
                System.out.println(Thread.currentThread().getName() + " onReleased called for file: " + file.getAbsolutePath());
                //chronicleProcessor.onRead(file);
            }
            @Override
            public void onAcquired(int cycle, File file) {
                System.out.println( Thread.currentThread().getName() +  " onAcquired called for file: " + file.getAbsolutePath());
            }
        }).build();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return chronicle;
}

}

When and how many times StoreFileListener onRelease gets called for Reader (N thread with their own tailer)? In beginning I am registering number of interested readers and once I receive equal number of release signal I mark file for deletion. But I have seen onAcquired and onReleased occurs multiple times on the same file for Reader.

What I understood onReleased should only happen once when a chronicle file is read completely.

Can I use same chronicleReader for multiple reads? And how many release events will I get if I have multiple reader on same chronicle? Is it also possible a file is released by reader but still a writer is writing on the same file?

AmbGup
  • 731
  • 1
  • 9
  • 23

1 Answers1

0

It is possible for a file to be acquired/released multiple times.

ExcerptTailers cannot be shared between multiple threads safely.

If you wish to implement a safe file-deletion mechanism, you would be better off tracking ExcerptTailer.cycle() in each reader thread. When all readers' tailer cycle advances to N+1, then it should be safe to remove the file corresponding to cycle N. You can retrieve the file for a cycle by calling:

queue.storeForCycle(N, 0, false).file();

Mark Price
  • 296
  • 1
  • 3
  • Keeping cycle count may be a good idea but I will prefer implementation to be thread safe and managed by hft libraries. My other post of this topic has got few more details and I want to make use of StoreFileListener https://stackoverflow.com/questions/48078740/chronicle-queue-storefilelistener-multiple-onacquired-and-onreleased – AmbGup Jan 09 '18 at 10:11
  • How do i get the actual filename to delete? – Sathish Kumar Feb 18 '18 at 13:25