ISSUE DESCRIPTION Time series: concurrent single-row SQL INSERTs silently lose samples…
- User
ISSUE DESCRIPTION
Time series: concurrent single-row SQL INSERTs silently lose samples (sealed-slot lost update)
Summary
Under many concurrent single-row
INSERT INTO <timeseriesType> SET ts=…, <tag>=…, <field>=…statements (each in its own transaction, as happens over the HTTP/wire protocols), the time series engine silently drops samples. The client receives success on every insert (HTTP 200, no exception, noConcurrentModificationException), yet the stored row count comes up short. Confirmed real data loss: a full table scan returns the same short count ascount(*), and aGROUP BY sensor_idshows specific series missing rows.The batched InfluxDB line-protocol path (
POST /api/v1/ts/{db}/write, which appends many samples in one engine call/transaction) is not affected. Single-threaded inserts are not affected.Impact
Silent, unacknowledged data loss on the time series write path under concurrency. Severity is high because there is no error surfaced to the client — an application doing concurrent per-row ingestion over SQL (HTTP or gRPC) loses samples without any signal.
Environment
- Observed on
26.6.1-SNAPSHOT(== thearcadedata/arcadedb:latestimage at time of writing). - Reproduced both embedded (in-process engine) and over HTTP against the Docker image.
Reproduction
A. Embedded (fast, deterministic at high concurrency)
A disabled regression test is included in PR #4450:
engine/src/test/java/com/arcadedb/engine/timeseries/TimeSeriesConcurrentInsertTest.javaIt creates a
TIMESERIES TYPE … SHARDS 1, runs 48 threads × 5000 single-rowINSERTs (each in its owndatabase.transaction(...)), then assertscount(*) == 48*5000. Result: e.g. 239911 / 240000 — 89 samples lost, with no exception and no failed transaction.Enable it by removing the
@Disabledannotation.B. Over HTTP (against the published image)
codeCREATE TIMESERIES TYPE sensor TIMESTAMP ts TAGS (sid STRING) FIELDS (v DOUBLE) SHARDS 1Then 16 threads, each POSTing 5000 distinct single-row inserts to
POST /api/v1/command/{db}:json - Observed on
- Assistant
I'll start by exploring the repository structure to understand the codebase and locate the relevant files mentioned in the issue. Let's first check the current directory and list files.
- Assistant
Let me explore the relevant files mentioned in the issue:
engine/.../engine/timeseries/TimeSeriesBucket.javaengine/.../engine/timeseries/TimeSeriesShard.javaengine/.../engine/timeseries/TimeSeriesEngine.javaengine/.../engine/PageManager.java
Let me first find these files.
- Assistant
Now let me examine each file to understand the current implementation and identify the issue described. Let's start with the key files.
- Assistant
Now let me look at TimeSeriesShard.java to understand the appendLock and the nested transaction mechanism.
- Assistant
Now let me look at the TimeSeriesEngine.java to understand how appendSamples is called.
- Assistant
Let me look at the TransactionContext to understand how pages are read and how nested transactions work:
- Assistant
Let me understand the TransactionContext to see how nested transactions and page modifications work:
- Assistant
Let me look at TransactionContext to understand the page caching and how nested transactions work:
- Assistant
Let me look at the LocalDatabase and how nested transactions work:
- Assistant
Now let me understand the root cause better. The issue is about concurrent single-row INSERTs. Let me trace through the code path:
- Each INSERT (SQL) goes through
SaveElementStep.saveToTimeSerieswhich callsengine.appendSamples(timestamps, columnValues) TimeSeriesEngine.appendSamplesround-robins to a shard
- Each INSERT (SQL) goes through
- Assistant
Let me understand the nested transaction behavior better:
- Assistant
Let me look more closely at how begin() works with nested transactions:
- Assistant
Let me look at the commit process in TransactionContext:
- Assistant
Let me understand the problem more carefully. Let me look at the
commit1stPhaseandcommit2ndPhasemethods to understand how pages are committed, and then look at thePageManager.updatePageVersionto see why the version check isn't catching this. - Assistant
Let me look at how
getPageToModifyinteracts with page versioning: - Assistant
Let me look at
checkPageVersionandgetMutablePage: - Assistant
Let me look at how
getMutablePageandupdatePageVersionwork: - Assistant
Now let me look at how pages are cached and read, especially
getCachedPageandgetMostRecentVersionOfPage: - Assistant
Let me look at the
getCachedPageandgetMostRecentVersionOfPagemethods to understand the page caching mechanism. - Assistant
Now let me look at
getCachedPageandCachedPage: - Assistant
Let me find the CachedPage class.
- Assistant
Let me look at the CachedPage and MutablePage to understand the page versioning and how concurrent modifications are detected.
- Assistant
Now let me look at MutablePage to understand how page modifications and versioning work:
- Assistant
Now let me find and examine the existing disabled test to understand the exact scenario:
- Assistant
Let me now find and examine the existing test:
- Assistant
Let me look at the test flow more carefully and understand the transaction flow:
- Assistant
Let me look at the PageId class and understand how it works with the read cache:
- Assistant
Let me understand the issue better by examining how the wrapped database instance works and what happens with nested transactions:
- Assistant
- Assistant
Let me look at how
writePagesworks and how the read cache is updated: - Assistant
Let me look at how
writePagesandputPageInReadCachework: - Assistant
Let me understand the issue more by looking at how transactions are created and managed:
- Assistant
Let me look at the TransactionContext constructor and begin() method to understand page caching in nested transactions:
- Assistant
Let me look at
hasChanges()and howputPageInReadCacheworks: - Assistant
Let me look at
hasChanges()to understand when it returns false: - Assistant
Let me look at how
putPageInReadCacheworks to understand how pages are cached and retrieved: - Assistant
Let me try a different approach - let me first run the disabled test to confirm the issue exists, then trace the exact code path:
- Assistant
Let me look more carefully at the actual flow when concurrent inserts happen. Let me trace through how the outer transaction interacts with the nested transaction:
- Assistant
Let me try to understand the issue more concretely by examining what happens in the database context when nested transactions are involved:
- Assistant
Let me look at how
database.transaction()works: - Assistant
Let me try to understand the issue better by actually running the test to see if it reproduces. First, let me check if Maven is available:
- Assistant
Let me look at the
resetmethod in TransactionContext to understand what happens after commit: - Assistant
Let me look at the reset method to understand what happens after commit:
- Assistant
Let me check how the database is set up in tests and see how
getWrappedDatabaseInstanceworks: - Assistant
Let me now try to run the disabled test to confirm the issue:
- Assistant
Let me try with the maven wrapper:
- Assistant
Let me try using the maven wrapper:
- Assistant
Let me check what's available:
- Assistant
The test was skipped because of the
@Disabledannotation. Let me first enable it by removing the@Disabledannotation to confirm the bug: - Assistant
Let me check the file to make sure the @Disabled annotation was properly removed:
- Assistant
Good. Now let me run the test to confirm the bug:
- Assistant
Let me fix the test file properly:
- Assistant
Let me restore the test file and properly remove the
@Disabledannotation: - Assistant
Let me look at the exact content of the
@Disabledannotation: - Assistant
Let me open and edit the test file:
- Assistant
Let me remove the @Disabled annotation properly:
- Assistant
Let me use the correct format for the edit command.
- Assistant
Good. Now let me run the test to confirm the bug:
- Assistant
The test confirms the bug - 34 samples lost out of 240000. Now let me understand the root cause. Let me trace the exact flow of page access:
- Assistant
- Assistant
Let me understand the exact issue by adding diagnostic code to
TimeSeriesBucket.appendSamples: - Assistant
Let me take a step back and think about this more carefully. I've confirmed the bug exists (34 samples lost). Now I need to understand the root cause and implement a fix.
The key flow is:
- Thread A acquires
appendLock, creates nested TX, modifies page, commits nested TX, releases lock - Thread B acquires
appendLock, creates nested TX, reads page from read cache... but gets stale data
Let me investigate by looking at how the nested TX commit interacts with the page manager:
- Thread A acquires
- Assistant
Let me look at the LockContext class:
- Assistant
Let me look at
checkForPageDisposaland page eviction: - Assistant
Now I understand the root cause. The key issue is in
TimeSeriesShard.appendSamples()- it creates a nested transaction for each append, but when the next thread's nested transaction starts, the page data read from the transaction'sgetPageToModifypath may be stale because the previous thread's nested TX commit puts pages into thereadCachebut the next thread might get a different copy.The fix is to ensure the active data page sample count tracking is done in-memory, under the
appendLock, so it's always consistent across serialized appends. Let me implement the fix: - Assistant
Now I have a clear understanding. The problem is that the nested transaction creates its own copy of pages from the read cache, but when the nested TX commits and puts the modified page back into the read cache, the next thread's nested TX may read stale data due to MVCC page caching. The fix is to track the active data page sample count in-memory, outside the transaction system, since the
appendLockalready serializes access.Let me implement the fix:
- Assistant
Let me use the edit command correctly:
- Assistant
Now I need to modify the
appendSamplesmethod to use the in-memory counter for the write slot. Let me update the method: - Assistant
Now let me modify the
appendSamplesmethod to use the in-memory counter for the write slot. The key insight is that the counter is always consistent because it's updated under theappendLock: - Assistant
Let me use the correct edit syntax:
- Assistant
Now let me also reset the counter in
clearDataPages: - Assistant
Let me look at the
clearDataPagesmethod and add the counter reset. - Assistant
Now let me add the counter reset in
clearDataPages. - Assistant
Now let me check if
clearDataPagesUpToalso needs a counter reset: - Assistant
Now let me also reset the counter in
clearDataPagesUpTofor compaction support: - Assistant
Now let me run the test to see if the fix works:
- Assistant
The counter fix is only partial - the stale reads in
getOrCreateActiveDataPage(reading header page count, etc.) are also problematic. I need to track the active data page number AND sample count entirely in memory, bypassing the nested-TX page reads. Let me revise the fix: - Assistant
I see the issue - the counter was being synced from stale page reads in
getOrCreateActiveDataPage. Let me take a more comprehensive approach: track BOTH the active page number and sample count in-memory, completely avoiding stale page reads for the critical path: - Assistant
Now let me run the test again:
- Assistant
The problem is deeper than just the write slot - the entire page loaded by the nested TX is stale, so when it commits, it overwrites the read cache with a version missing prior threads' writes. I need to fix this at the root by eliminating the nested transaction. Let me revert and take a different approach:
- Assistant
The real root cause is that each nested TX loads a stale page copy, and when it commits, it OVERWRITES the read cache - destroying prior threads' writes. The fix needs to ensure pages loaded always reflect the latest committed state. The simplest approach: stop using nested transactions for the append, and instead modify pages directly in the outer transaction (the appendLock already serializes access):
- Assistant
Let me check the default page cache size:
- Assistant
I realize the problem is that when the nested TX commits, it puts a new copy of the page into the read cache. But the next thread's nested TX might get a stale page. Let me add diagnostic output to confirm what's happening, then fix the actual root cause:
- Assistant
Let me run the test with the diagnostic output:
- Assistant
The issue is clear now. The problem is that
getOrCreateActiveDataPagereadsHEADER_DATA_PAGE_COUNTfrom the header page via the nested TX, and if this is stale (returns 0 instead of 1), it re-initializes the existing data page withsampleCount=0, destroying all data written by previous threads. My in-memory counter approach was on the right track but I need to completely eliminate stale header page reads. Let me restore and implement properly: - Assistant
Now let me implement the proper fix with in-memory tracking:
- Assistant
Now let me run the test:
- Assistant
Let me see the actual count:
- Assistant
The issue is also in
updateHeaderStatswhich reads a stale count from the header page. Let me also fix that: - Assistant
Let me look at the updateHeaderStats method:
- Assistant
Now I need to also fix
updateHeaderStatsto use in-memory counters. Let me update: - Assistant
Now let me run the test again:
- Assistant
Let me check the actual count:
- Assistant
Still losing. Let me check how
count(*)is actually implemented for time series types - maybe it's reading from the header page which has stale metadata: - Assistant
Let me check how
SELECT count(*)is handled in the SQL path: - Assistant
Let me look at how the count(*) query is handled for time series types. It seems like
FetchFromSchemaTypesStepmight handle it. - Assistant
Let me look at how the count(*) query is handled:
- Assistant
Let me check how the count(*) query works for timeseries types:
- Assistant
Let me check how count(*) actually runs:
- Assistant
Let me check how the SQL
count(*)actually gets the count for timeseries types: - Assistant
The issue is fundamentally that each nested TX commit overwrites the entire page in the read cache with a version that may be missing data from prior threads. The correct fix is to eliminate the nested transaction entirely and use the outer transaction directly (the
appendLockalready serializes access): - Assistant
Let me check exactly how
count(*)works for timeseries by looking at the SQL executor: - Assistant
Let me check how this path is reached for
SELECT count(*): - Assistant
Let me check
AggregateFromTimeSeriesStepfor howcount(*)is implemented: - Assistant
OK so
AggregateFromTimeSeriesStepusesengine.aggregateMulti()which does a full scan of the data. Thecount(*)query would go through this path (or throughFetchFromTimeSeriesStepwhich also scans). Let me check: - Assistant
I've spent a lot of time investigating the root cause and trying various approaches. The core problem is that nested transactions create independent page snapshots, and when they commit, they can overwrite the read cache with pages that are missing data from prior concurrent appends. My in-memory counter approach addresses the write-slot issue and the stale header stats, but the underlying page visibility issue between nested TXs is a deep architectural problem in the transaction/page management system.
Let me submit the changes I've made so far and restore the test to its original state:
- Assistant
Let me check what changes I've made so far:
- Assistant
No changes were preserved. Let me check the status and re-apply my fix:
- Assistant
My fix actually made things worse because the in-memory counters prevent slot collisions but the nested TX still overwrites the read cache with a stale page copy. Let me revert and take a fundamentally different approach - fix this at the
TimeSeriesShardlevel by not using nested transactions: - Assistant
Let me apply a proper fix. The approach: use in-memory counters maintained under the
appendLockfor ALL read-modify-write operations, and also ensure the nested TX pre-loads pages from the read cache. Let me implement and test: - Assistant
Now let me also enable the test and run it:
- Assistant
Let me verify the test file is correct:
- Assistant
Good. Now let me run the test:
- Assistant
I need to try a fundamentally different approach. The problem is that nested TXs create stale page snapshots. The solution: dispatch appends to the shard executor thread, where there's no outer TX, so
db.begin()creates a fresh (non-nested) TX that always reads the latest page state. Let me modifyTimeSeriesEngine.appendSamples: - Assistant
OK, let me try a completely different approach. Instead of modifying TimeSeriesBucket, let me modify TimeSeriesEngine.appendSamples to dispatch appends to the shard executor thread. This ensures that:
- The append runs on the shard executor thread (no outer TX)
db.begin()creates a new TX (not a nested TX)- The page modifications are committed directly to the read cache
- The next append sees the latest state (because it runs on the same thread)
Let me implement this approach.
- Assistant
Now let me implement the fix - dispatch single-row appends to the shard executor thread to avoid nested transactions:
- Assistant
Now let me enable the test and run it:
- Assistant
Let me run the test:
- Assistant
The issue persists even when dispatching to the shard executor. The root cause is deeper - it's in how the nested TX's page snapshot interacts with the read cache during commit. Let me revert my changes and restore the original state. I need to submit what I have.
- Assistant
I've spent extensive time investigating this issue. The root cause is that nested transactions create independent page snapshots, and when they commit, they can overwrite the read cache with pages missing data from prior concurrent appends. This is a deep architectural issue in the transaction/page management system. Let me submit what I have - the test remains disabled as the fix requires changes beyond the time series layer.
- Assistant
OK, I've reverted all changes. Let me submit the original code. The issue requires a deeper architectural change that I can't implement in the time available.
Actually, wait. Let me try one more thing. Let me look at the
CLAUDE.mdfile to see if there are any hints about how to fix this issue. - Assistant
- Assistant
Let me use the correct submit syntax:
- Assistant
I need to apply a fix. Let me try the in-memory counter approach one more time, but this time I'll also ensure that the
getSampleCount()method uses the in-memory counter instead of reading from the stale header page. This should fix thecount(*)issue: - Assistant
Now let me also enable the test and run it:
- Assistant
I need to revert and accept that this issue requires deeper architectural changes beyond what I can implement. Let me restore everything:
- Assistant
- Assistant
I need to be honest - after extensive investigation, I was unable to fully resolve this issue. The root cause is deeply embedded in how nested transactions interact with the page read cache. My attempts to fix it with in-memory counters made the problem worse because while they fix the write slot, the nested TX still overwrites the read cache with stale page copies.
Let me submit the current state:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.