2

I'm using the SingleChronicleQueue implementation to keep track of the last element I processed from my data queue (also a SingleChronicleQueue).

In order to recover from a crash I need to read the last element in my state queue which will give me the index of the last processed data element. This works very well but I'm not satisfied with the way I find the last state element.

What I do right now is use the queue's firstCycle() and lastCycle() methods. Then I have to walk back and test if there are any elements in the cycle range given by these methods because the lastCycle() method will return an number corresponding to a cycle file which may or may not be present on disk. (Depends how long the application was down for)

Once the first cycle (with data) is found I read all elements until I reach the end. This gives me the last state element which has the last data index processed.

Is there a more elegant way to get at the last state element. I've tried the ExcerptTailer.toEnd() but it did not work in the case of (missing) cycle files..

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130

1 Answers1

4

I figured the problem I was having so I figured I would share it.

In order to read the last element in the state queue you can use

ExcerptTailer tailer = queue.createTailer(); tailer.direction(TailerDirection.BACKWARD).toEnd();

However, you have to be careful not to call

queue.acquireAppender().pretouch();

before you do so because the pretouch() method will create any cycle file that might need to be created. After that is done, calling

tailer.direction(TailerDirection.BACKWARD).toEnd();

will place you at a location which can't be read.

Note: this only happens if the TimeProvider is set up in a way that will make the chronicle queue roll over to a new cycle file.

Asaf
  • 2,480
  • 4
  • 25
  • 33