Race condition in OnHeapHnswGraph
Viliam ġurina <[email protected]> Mon, 12 Jan 2026 12:38:32 +0100
| Newsgroups | gmane.comp.jakarta.lucene.user |
|---|---|
| Message-ID | <CAO=iB8KTK2-Ti-EdxBod-2aM1J_wzX5rhibLHQsLwiP4Lmrukg@mail.gmail.com> |
--000000000000acd47b06482f52fc Content-Type: text/plain; charset="UTF-8" Hi all, I'm looking at `OnHeapHnswGraph` code and noticed that the volatile field `graphRamBytesUsed` is modified in `addNode` in a racy way: long bytesUsed = graphRamBytesUsed; graphRamBytesUsed = bytesUsed + l; This is equivalent to `graphRamBytesUsed += l`. This code is susceptible to lost update due to non-atomic read-modify-write operation. I guess the it's not really a problem, because this code is in fact single-threaded when documents are added to the index. It might be concurrent during merging, but then `ramBytesUsed()` isn't called, and it's a wasted work. If the above assumption is correct, then this field should not be volatile to improve performance. If it's not, it should be replaced with `AtomicLong` or `LongAdder`. Viliam --000000000000acd47b06482f52fc--