Copilot commented on code in PR #808:
URL: https://github.com/apache/datasketches-java/pull/808#discussion_r4632426123
##########
src/main/java/org/apache/datasketches/req/ReqSketch.java:
##########
@@ -554,26 +559,35 @@ private static void checkK(final int k) {
}
}
+ //Each compaction moves items to the next level only after the space for
them has been allocated,
+ //so a failed allocation never loses items. If an error interrupts
compression, the counters are
+ //recomputed from the compactors, so the sketch stays consistent and the
next update compresses again.
private void compress() {
if (reqDebug != null) { reqDebug.emitStartCompress(); }
- for (int h = 0; h < compactors.size(); h++) {
- final ReqCompactor c = compactors.get(h);
- final int compRetItems = c.getBuffer().getCount();
- final int compNomCap = c.getNomCapacity();
-
- if (compRetItems >= compNomCap) {
- if ((h + 1) >= getNumLevels()) { //at the top?
- if (reqDebug != null) { reqDebug.emitMustAddCompactor(); }
- grow(); //add a level, increases maxNomSize
+ try {
+ for (int h = 0; h < compactors.size(); h++) {
+ final ReqCompactor c = compactors.get(h);
+ final int compRetItems = c.getBuffer().getCount();
+ final int compNomCap = c.getNomCapacity();
+
+ if (compRetItems >= compNomCap) {
+ if ((h + 1) >= getNumLevels()) { //at the top?
+ if (reqDebug != null) { reqDebug.emitMustAddCompactor(); }
+ grow(); //add a level, increases maxNomSize
+ }
+ c.compact(cReturn, rand, compactors.get(h + 1).getBuffer());
+ retItems += cReturn.deltaRetItems;
+ maxNomSize += cReturn.deltaNomSize;
+ //we specifically decided not to do lazy compression.
}
- final FloatBuffer promoted = c.compact(cReturn, rand);
- compactors.get(h + 1).getBuffer().mergeSortIn(promoted);
- retItems += cReturn.deltaRetItems;
- maxNomSize += cReturn.deltaNomSize;
- //we specifically decided not to do lazy compression.
}
+ } catch (final RuntimeException | Error e) {
+ retItems = computeTotalRetainedItems();
+ maxNomSize = computeMaxNomSize();
Review Comment:
Both recount helpers use enhanced-for loops over `compactors`, which can
allocate an iterator. If an `OutOfMemoryError` occurs after promotion and
trimming, another allocation failure during recounting can leave the counters
inconsistent. Calculate both totals with indexed access so recovery does not
require another allocation.
##########
src/main/java/org/apache/datasketches/req/ReqSketch.java:
##########
@@ -469,15 +474,15 @@ public String toString() {
public void update(final float item) {
if (Float.isNaN(item)) { return; }
ensureCapacity(); //first use allocates, before anything changes
+ final FloatBuffer fbuf = compactors.get(0).getBuffer();
+ fbuf.append(item); //may grow the buffer; nothing else has changed yet
Review Comment:
If a sorted view is cached and `emitStartCompress()` throws, `update()` has
already committed the item and incremented N, but the hook runs outside the new
`try/finally`, leaving queries backed by the old view. Invalidate `reqSV`
immediately after a successful append so failures before entering the
compression try block cannot leave stale query results.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]