wernerdv commented on code in PR #13554:
URL: https://github.com/apache/ignite/pull/13554#discussion_r4004730086
##########
modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/IgniteCacheDatabaseSharedManager.java:
##########
@@ -1216,24 +1270,170 @@ public void ensureFreeSpaceForInsert(DataRegion
region, int dataRowSize) throws
boolean oomThreshold = (memorySize / pageMem.systemPageSize()) <
((double)dataRowSize / pageMem.pageSize() + nonEmptyPages * (8.0 *
1.5 / pageMem.pageSize() + 1) + 256 /*one page per bucket*/);
- if (oomThreshold) {
- IgniteOutOfMemoryException oom = new
IgniteOutOfMemoryException("Out of memory in data region [" +
- "name=" + regCfg.getName() +
- ", initSize=" + U.readableSize(regCfg.getInitialSize(), false)
+
- ", maxSize=" + U.readableSize(regCfg.getMaxSize(), false) +
- ", persistenceEnabled=" + regCfg.isPersistenceEnabled() + "]
Try the following:" + U.nl() +
- " ^-- Increase maximum off-heap memory size
(DataRegionConfiguration.maxSize)" + U.nl() +
- " ^-- Enable Ignite persistence
(DataRegionConfiguration.persistenceEnabled)" + U.nl() +
- " ^-- Enable eviction or expiration policies"
- );
+ if (oomThreshold)
+ throw outOfMemory(regCfg);
+ }
+
+ /**
+ * Size-aware reserve for an eviction-enabled non-persistent region. Runs
eviction until the free list holds
+ * enough real empty pages to accommodate the row, or throws {@link
IgniteOutOfMemoryException} if the goal is
+ * unreachable / no progress can be made. Progress is measured against the
number of empty pages in the free list
+ * (the only resource a subsequent fragmented write can reliably consume
once the region is effectively full); the
+ * region's spare capacity (headroom) is only trusted in the fast path
while the region is below the eviction
+ * threshold.
+ *
+ * @param region Data region.
+ * @param regCfg Data region configuration.
+ * @param dataRowSize Size of data row to be inserted.
+ * @throws IgniteOutOfMemoryException If the target cannot be reached (row
too large for the region or eviction
+ * makes no progress).
+ * @throws IgniteCheckedException If failed to evict data pages.
+ */
+ private void ensureFreeSpaceForEviction(DataRegion region,
DataRegionConfiguration regCfg, int dataRowSize)
+ throws IgniteOutOfMemoryException, IgniteCheckedException {
+ PageMemory pageMem = region.pageMemory();
+
+ long pageSize = pageMem.pageSize();
+
+ // Maximum payload bytes that a single data page can hold for a
fragmented row.
+ long pagePayload = pageSize -
AbstractDataPageIO.MIN_DATA_PAGE_OVERHEAD;
+
+ // A row that fits into the steady-state empty-pages pool is satisfied
by normal threshold eviction, so the
+ // fast path is a single comparison (no page computation, free-list
lookup or page-memory reads on the hot
+ // small-put path).
+ long maxFastRowBytes = regCfg.getEmptyPagesPoolSize() * pagePayload;
+
+ if (dataRowSize <= maxFastRowBytes)
+ return;
+
+ CacheFreeList freeList = freeListMap.get(regCfg.getName());
+
+ if (freeList == null)
+ return;
+
+ long totalPages = regCfg.getMaxSize() / pageMem.systemPageSize();
- if (cctx.kernalContext() != null)
- cctx.kernalContext().failure().process(new
FailureContext(FailureType.CRITICAL_ERROR, oom));
+ // Pages the row will actually occupy once written, and which the free
list must hand out on demand during
+ // the fragmented write.
+ long requiredPages = (dataRowSize + pagePayload - 1) / pagePayload;
- throw oom;
+ // The row fundamentally cannot fit into the whole region.
+ if (requiredPages > totalPages)
+ throw outOfMemory(regCfg);
+
+ // The reserve must guarantee that the free list holds `requiredPages`
REAL empty pages, not merely that the
+ // region has apparent headroom. Apparent headroom (totalPages -
loadedPages) is shared and non-exclusive:
+ // concurrent inserts can both count on it and then both run out of
pages mid-write (TOCTOU / raw OOM), since
+ // a fresh allocation cannot grow the region beyond capacity. Once the
region is effectively full, real empty
+ // pages already in the free list are the only resource the fragmented
write can reliably consume, so the loop
+ // below accumulates them. (Headroom is trusted in the fast path only
while the region is below the eviction
+ // threshold, i.e. where contention cannot exhaust the slack.)
+ long emptyPages = freeList.emptyDataPages();
+
+ long headroom = totalPages - pageMem.loadedPages();
+
+ // The region is "under pressure" once loaded pages reach the eviction
threshold; below it a fresh allocation
+ // can safely grow the region, so a row that fits into the combined
spare space is satisfied without eviction
+ // (which would otherwise destroy evictable, e.g. short-TTL, entries
just to accumulate empty pages the slack
+ // could have absorbed).
+ long pagesThreshold = (long)(totalPages *
regCfg.getEvictionThreshold());
+
+ boolean underPressure = pageMem.loadedPages() >= pagesThreshold;
+
+ // Fast path: the row is satisfiable without eviction when (a) the
free list already holds enough real empty
+ // pages, or (b) the region is not under pressure and has enough spare
space to grow into.
+ if (emptyPages >= requiredPages || (!underPressure && emptyPages +
headroom >= requiredPages))
Review Comment:
Not a bug.
The gate reuses evictionThreshold only as a regime boundary, not as "when to
start eviction" (that's evictionRequired(), which stops on emptyPages >=
poolSize). (1) loadedPages() counts every allocated page including empty reuse
pages, so on a filling region it routinely exceeds the threshold — no last 10%
is unusable; (2) below the threshold headroom is trusted and the region grows,
at/above it only real empty pages are counted.
Renamed to evictionRegime; no behavior change.
--
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]