在裁缝检索到最近编写的摘录后,编年史队列是否会阻塞恐惧?

Does a ChronicleQueue block on reads after a tailer has retrieved the most recently written excerpt?

我也开始关注 ChronicleQueue。

来自文档:

Reading the queue follows the same pattern as writing, except there is a possibility there is not a message when you attempt to read it

我想了解没有消息时会发生什么。 LinkedBlockingQueue 的美妙之处在于,当一个线程将项目放入队列(例如,从流中)而另一个线程正在使用项目(是的,在元素到达时删除元素)时,它只需很少的 CPU 负载即可工作并将它们分组处理)。我想利用映射文件来获取流中原始项目的历史记录和 ChronicleQueue 的性能,但似乎要复制 LinkedBlockingQueue 的行为,我的代码必须跟踪摘录索引以了解摘录内容已被处理——当它们到达时(被放入队列中),或者通过一些在实际处理摘录内容的代码中使用重播的阴谋。 也就是说,我怀疑我在理解 appender/tailer 行为时遗漏了一些基本知识。 任何见解将不胜感激。

Chronicle Queue 专为低延迟、单线程环境而设计,因此它从不阻塞任何操作。

因此,通常从队列中读取的模式涉及忙转(或各种pausers,最简单的是Thread.yield()),例如:

while (true) {
    try (DocumentContext dc = tailer.readingDocument()) {
        // false here means there's no data to read, so we just keep waiting
        if (dc.isPresent()) {
             Bytes bytes = dc.wire().bytes();
            // read from bytes
        }
    }
}

除非您想稍后 return 到队列中的某些项目,否则您不需要为索引或任何此类问题操心。

注意:忙转与阻塞相反,它将消耗 100% 的 CPU,这是对低延迟解决方案的有意权衡。