GeezerVison is showing github.com — Application cannot get the newest records submitted by previous commits under async flush mode · Issue #1302 · ArcadeData/arcadedb
Application cannot get the newest records submitted by previous commits under async flush mode #1302DescriptionArcadeDB Version:
ArcadeDB Server v23.9.1 (build //)
OS and JDK Version:
Running on Linux 6.2.16-3-pve - OpenJDK 64-Bit Server VM 21
Expected behavior
Application read the newest record which is submitted by previous commits without concurrent commits under async flush mode.
Actual behavior
Application may read the old record without concurrent commits under async flush mode.
Steps to reproduce
Continuously insert records with large size (property values long string for example) with serveral commits using async flush mode and read them immediately
Additional description
This bug is due to the way modified pages are flushed into files under async flush mode. When asyncFlush transaction is committed, the modified pages are passed to the FlushThread and then recorded in readCache of PageManager, then FlushThread flush the pages into files. This usually works, until large amount of pages are modified continuously. In this case, the readCache may exceed the limit and drop the pages which not yet writen to files. If function loadPage tries to read the dropped page at the same time, it will not find the page in readCache, and will read the old page from the files.
This bug may produce several Exceptions, such as RecordNotFoundException if a page with new records not flushed yet. Moreover, the bug may cause ConcurrentModificationException even if two transactions are submitted by same thread, which is quite confusing. This happens when the latter transaction read a old page of former one to modify, and the new page has been flushed into files when the latter transaction commits. The database found that the version of modified page is different from the one of page in file, so it throws a ConcurrentModificationException.
This is the log when the ConcurrentModificationException is thrown:
2023-10-25 15:00:03.938 SEVER [PageManager] Reached max RAM for page cache. Freeing pages from cache (target=2152628224 current=4305256448 max=4294967296 threadId=41)
2023-10-25 15:00:31.163 SEVER [PageManager] Reached max RAM for page cache. Freeing pages from cache (target=2147483648 current=4294967296 max=4294967296 threadId=41)
2023-10-25 15:00:37.886 SEVER [PageManager] Reached max RAM for page cache. Freeing pages from cache (target=2147483648 current=4294967296 max=4294967296 threadId=41)
2023-10-25 15:00:58.637 SEVER [PageManager] Reached max RAM for page cache. Freeing pages from cache (target=2147483648 current=4294967296 max=4294967296 threadId=41)
2023-10-25 15:00:59.967 SEVER [PageManager] Concurrent modification on page PageId(78/416) in file '_stat_16_camera_number_0.78.65536.v0.bucket' (current v.66 <> database v.67). Please retry the operation (threadId=41)
2023-10-25 15:01:00.489 SEVER [PageManager] Reached max RAM for page cache. Freeing pages from cache (target=2147483648 current=4294967296 max=4294967296 threadId=41)
As for the fix of this bug, maybe check whether the cached page to remove from readCache is flushed into files is an option, or there may be some better choices. Activitylvca commented on Nov 6, 2023 Thanks @hjhk555 for the detailed explanation. The read cache contains only immutable pages, so it's fine if they are flushed during page eviction. A transaction should hold the immutable page when is modified to avoid this issue. This should be supported by REPEATABLE_READ isolation mode.
Questions:
- Are you using the embedded database or via remote?
- Have you already tried using
database.setTransactionIsolationLevel(REPEATABLE_READ)?
HarroYuz commented on Nov 7, 2023 Thank you for your reply.
I'm using embedded database, and I have tried using database.setTransactionIsolationLevel(REPEATABLE_READ) while this issue still occurred.
In my opinion, the reason of this issue is that the eviction of immutable pages happens before the pages are flushed. In the interval between page eviction and page flushed, the newest page is stored only in the queue of FlushThread of PageManager.
When TransactionContext tries to get the page to modify, it calls getPage, and if this page is not in TransactionContext, it calls PageManager.getImmutablePage, where the queue of FlushThread is not looked for.
This leads to the possibility that the TransactionContext failed to load the newest page. lvca commented on Nov 7, 2023 @hjhk555 it makes sense to me. Let me check those parts. lvca commented on Nov 7, 2023 I checked your analysis and it's correct. I general, the user code should be aware of concurrent modification exceptions and manage a retry/reload, but in the case of a single thread, it would be a little counter intuitive.
I've just pushed a fix but I'm struggling with writing a test case to reproduce the issue.
@hjhk555 any chance you can help me with writing such a test case?
This new method in PageManager reuses the page in the queue, ready to be flushed:
public CachedPage getCachedPageFromMutablePageInQueue(final PageId pageId) {
final Object[] content = queue.toArray();
for (int i = 0; i < content.length; i++) {
final List<MutablePage> pages = (List<MutablePage>) content[i];
if (pages != null) {
for (int j = 0; j < pages.size(); j++) {
final MutablePage page = pages.get(j);
if (page.getPageId().equals(pageId))
return new CachedPage(page, true);
}
}
}
return null;
}
It's not very efficient, but it's rare to have many pages in the flush queue, unless of poor write disk performance or large transactions. lvca commented on Nov 7, 2023 An alternative would be waiting for the page to be flushed and reloading it from the disk. HarroYuz commented on Nov 8, 2023 For the test case, The data I'm using contains sensitive information, and I haven't found a way to reproduce the issue with generated data, which is a pity.
And for the code, this issue still happens with the fix. In my point of view, the search order of queue should be opposite, because the newest pages are in the tail of queue. Moreover, the pages polled by function flushPagesFromQueueToDisk is removed from queue, which can lead to this issue since the page requested may among those removed pages.
Upon analysis above, I tried to fix by save the flushing pages in critical section. The modified code is like this:
private List<MutablePage> flushingPages;
private ReentrantLock flushingPagesLock = new ReentrantLock();
protected void flushPagesFromQueueToDisk(final long timeout) throws InterruptedException, IOException {
if (!queue.isEmpty()) {
flushingPagesLock.lock();
flushingPages = queue.poll(timeout, TimeUnit.MILLISECONDS);
flushingPagesLock.unlock();
if (flushingPages == null)
return;
if (flushingPages.isEmpty())
// EMPTY PAGES IS A SPECIAL CONTENT FOR SHUTDOWN
running = false;
else {
for (final MutablePage page : flushingPages)
try {
pageManager.flushPage(page);
} catch (final DatabaseMetadataException e) {
// FILE DELETED, CONTINUE WITH THE NEXT PAGES
LogManager.instance().log(this, Level.WARNING, "Error on flushing page '%s' to disk", e, page);
}
}
flushingPagesLock.lock();
flushingPages = null;
flushingPagesLock.unlock();
}
}
public CachedPage getCachedPageFromMutablePageInQueue(final PageId pageId) {
final Object[] content = queue.toArray();
for (int i = content.length - 1; i >= 0; i--) {
final List<MutablePage> pages = (List<MutablePage>) content[i];
if (pages != null) {
for (int j = 0; j < pages.size(); j++) {
final MutablePage page = pages.get(j);
if (page.getPageId().equals(pageId))
return new CachedPage(page, true);
}
}
}
CachedPage pageInFlushing = null;
flushingPagesLock.lock();
if (flushingPages != null) {
for (MutablePage page : flushingPages) {
if (page.getPageId().equals(pageId)) {
pageInFlushing = new CachedPage(page, true);
break;
}
}
}
flushingPagesLock.unlock();
return pageInFlushing;
}
After doing so, the issue does disapper. But I found that the efficiency of async massive inserts even become lower than which of the sync one before, so there may be a better way to deal with this issue. HarroYuz commented on Nov 8, 2023 Maybe keep the page's version when doing page eviction is a soulution, when page is reload from disk, the version of page can be verified, which should be larger or equal to the one stored(in case of multiple user threads). lvca commented on Nov 8, 2023 Okay, I'm going to remove my patch then because useless.
There is not an easy solution without adding locks, and I won't slow down the normal database operations for such a rare use case (so rare I can't find a way to reproduce it) anyway.
The solution should be that in your user code, you catch the ConcurrentModificationException and repeat the transaction when this happens. After all, this is why we have an MVCC, to catch issues like this and keep the database consistent.
If you use the transaction method, the transaction is automatically retried (up to 3 times, configurable) in case that case happens.
db.transaction( () -> {
final MutableVertex v = database.newVertex("Author");
v.set("name", "Jay");
v.set("surname", "Miner");
v.save();
});
lvca commented on Nov 14, 2023 Closing this issue because I cannot reproduce it and the provided solution (managing things in a transaction block with auto retry) solves this issue.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|