Chronicle queue named appender to get the most recent roll cycle file for iteration returns earliest file

huangapple go评论84阅读模式
英文:

Chronicle queue named appender to get the most recent roll cycle file for iteration returns earliest file

问题

我正在尝试实现一个命名的追踪器,以从先前中断的任务继续进行。但在继续执行任务之间,我可能想要检查最近的滚动周期的内容。然后,我尝试测试是否可以获取最近的滚动周期进行迭代(命名的追踪器会从中断的地方继续,所以我认为它应该迭代最近的文件),这是输出:

Chronicle queue named appender to get the most recent roll cycle file for iteration returns earliest file

以下是代码:

public static void main(String[] args) {
    ChronicleQueue QUEUE = SingleChronicleQueueBuilder.single("./chronicle/roll").rollCycle(RollCycles.MINUTELY).build();
    ExcerptAppender APPENDER = QUEUE.acquireAppender();
    ExcerptTailer TAILER = QUEUE.createTailer("a");

    //this reads all roll cycles starting from first and carries on to next rollcycle.
    //busy spinner that spins non-stop trying to read from queue
    while(true){
        System.out.println(TAILER.cycle());
        if (TAILER.readDocument(w -> w.read("packet").marshallable(
                m -> {
                    System.out.println(m.read("moldUdpHeader").text());
                }
        ))){ ; } else { TAILER.close(); break; }
        //breaks out from spinner if nothing to be read.
        //a named tailer could be used to pick up from where is left off.

    }

    //by calling TAILER.approximateExcerptsInCycle(TAILER.cycle()) inside the for-loops condition statement,
    //it winds the index back to start of the cycle, so TAILER will constantly only read first item
    ExcerptTailer tailer = QUEUE.createTailer("a");
    tailer.moveToCycle(tailer.cycle());
    System.out.println(tailer.cycle());
    long excerpts = tailer.approximateExcerptsInCycle(tailer.cycle());  //gets the number of records for earliest roll cycle available
    System.out.println(excerpts);
    for (int i=0; i< excerpts; i++){
        System.out.println(tailer.cycle());
        tailer.readDocument(w -> w.read("packet").marshallable(
                m -> {
                    System.println(m.read("moldUdpHeader").text());
                }
        ));
    }
    tailer.close();
}

输出的解释:

我运行了 while 循环,所以它走到了追踪器的末尾,没有进一步迭代的内容。因此,它打印了滚动周期,但没有打印任何文档,然后中断。

然后,我尝试使用.moveToCycle(TAILER.cycle()); 移动到滚动周期的起始位置,它打印并显示我仍然在最近的(最后的滚动周期 27927733),我打印了追踪器内的摘要数量(6)并进入 for 循环。在 for 循环内,它输出来自第一个编年史滚动周期文件(27927664)中最早文档的值。尽管第一行在打印输出之前打印了 27927733,但输出也是来自第一个滚动周期。

Chronicle Queue POM:

<!-- https://mvnrepository.com/artifact/net.openhft/chronicle-queue -->
<dependency>
    <groupId>net.openhft</groupId>
    <artifactId>chronicle-queue</artifactId>
    <version>5.24ea7</version>
</dependency>

<details>
<summary>英文:</summary>
I am trying to implement a named tailer to pickup where a task was previously left off. But in between continuing from a task, I may want to check the contents of the most recent roll cycle. I then tried to test if I can get the most recent roll cycle to iterate over (named tailers pick up where left off so I think it should iterate most recent file) and this is the output:
[![output][1]][1]
The code is as follows:
public static void main(String[] args) {
ChronicleQueue QUEUE = SingleChronicleQueueBuilder.single(&quot;./chronicle/roll&quot;).rollCycle(RollCycles.MINUTELY).build();
ExcerptAppender APPENDER = QUEUE.acquireAppender();
ExcerptTailer TAILER = QUEUE.createTailer(&quot;a&quot;);
//this reads all roll cycles starting from first and carries on to next rollcycle.
//busy spinner that spins non-stop trying to read from queue
while(true){
System.out.println(TAILER.cycle());
if (TAILER.readDocument(w -&gt; w.read(&quot;packet&quot;).marshallable(
m -&gt; {
System.out.println(m.read(&quot;moldUdpHeader&quot;).text());
}
))){ ; } else { TAILER.close(); break; }
//breaks out from spinner if nothing to be read.
//a named tailer could be used to pick up from where is left off.
}
//by calling TAILER.approximateExcerptsInCycle(TAILER.cycle()) inside the for-loops condition statement,
//it winds the index back to start of the cycle, so TAILER will constantly only read first item
ExcerptTailer tailer = QUEUE.createTailer(&quot;a&quot;);
tailer.moveToCycle(tailer.cycle());
System.out.println(tailer.cycle());
long excerpts = tailer.approximateExcerptsInCycle(tailer.cycle());  //gets the number of records for earliest roll cycle available
System.out.println(excerpts);
for (int i=0; i&lt; excerpts; i++){
System.out.println(tailer.cycle());
tailer.readDocument(w -&gt; w.read(&quot;packet&quot;).marshallable(
m -&gt; {
System.out.println(m.read(&quot;moldUdpHeader&quot;).text());
}
));
}
tailer.close();
Explanation of output:
I ran the while loop, so it went to the end of the tailer and there is nothing to further iterate. So it prints the cycle, but does not print any documents then breaks.
I then try to move to start of the cycle with `.moveToCycle(TAILER.cycle());`, it prints and shows that I am still on most recent (last cycle 27927733), I print the number of excerpts (6) inside the tailer and enter the for loop. Inside the for loop, it outputs values from the earliest documents from the first chronicle roll cycle file (27927664). Although the first line prints 27927733 before printing output, the output is also from the first roll cycle.
Chronicle Queue POM:
&lt;!-- https://mvnrepository.com/artifact/net.openhft/chronicle-queue --&gt;
&lt;dependency&gt;
&lt;groupId&gt;net.openhft&lt;/groupId&gt;
&lt;artifactId&gt;chronicle-queue&lt;/artifactId&gt;
&lt;version&gt;5.24ea7&lt;/version&gt;
&lt;/dependency&gt;
[1]: https://i.stack.imgur.com/IEdU7.png
</details>
# 答案1
**得分**: 1
奇怪的行为应该进行调查。然而,我建议为每个目的创建一个尾部,以简化其功能。这样做很可能可以解决这个问题。我还建议您不需要任何静态字段。
<details>
<summary>英文:</summary>
That odd behaviour should be investigated.
However, I suggest creating a tailer for each purpose to simplify what it does. Doing so is likely to work around this issue.  I also suggest you don&#39;t need any static fields.
</details>
# 答案2
**得分**: 1
我查看了[这个Stack Overflow问题](https://stackoverflow.com/a/44793911/16034206)并从不同的角度解决了这个问题。现在,我创建了一个新的tailer(没有命名),将其移到末尾,循环获取近似的摘要(来自上面的代码),然后将其移动到最后一个可用滚动周期的第一个索引(近似*),然后进行迭代。
这可能不总是获得滚动周期中的第一项,因为它是一个近似值。请仍然注意上述问题中提到的奇怪行为。
```java
tailer.toEnd();
long lastIndex = tailer.index();
long excerpts = tailer.approximateExcerptsInCycle(tailer.cycle());
tailer.moveToIndex(lastIndex - excerpts);
英文:

I have checked out this SO question and approached the problem from a different perspective. Now, I create a new tailer (not named) move to end, get the approximate excerpts in cycle (from code above), move to the first index (approximate*) of the last available roll cycle, then iterate.

This may not always get the first item in the roll cycle, as it is an approximate. Please still do note the weird behaviour mentioned in above question.

    tailer.toEnd();
long lastIndex = tailer.index();
long excerpts = tailer.approximateExcerptsInCycle(tailer.cycle());
tailer.moveToIndex(lastIndex - excerpts);

huangapple
  • 本文由 发表于 2023年2月6日 14:54:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358187.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定