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)
CREATE 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}:{"language":"sql","command":"INSERT INTO sensor SET ts=<unique>, sid='s<thread>', v=<i>"}SELECT count(*) FROM sensorreturns short by out of 80000 across runs. - Observed on
Assistant
Let me start by understanding the repository structure and the issue in detail. The issue is about concurrent single-row SQL INSERTs silently losing samples in a time series. The root cause is a race condition in
TimeSeriesBucket.appendSampleswhere the read-modify-write of the sample count is not atomic across transactions.Let me explore the repository structure first, then look at the relevant files mentioned in the issue.
Assistant
Let me examine all the relevant files to understand the issue.
Assistant
Now let me look at the implementation files.
Assistant
Now let's examine the TimeSeriesBucket.java file, focusing on the appendSamples and getOrCreateActiveDataPage
Assistant
Now let me look at the TimeSeriesEngine.java and PageManager.java:
Assistant
Let me run these separately.
Assistant
Now let me look at PageManager.java to understand the page versioning mechanism
Assistant
Let me look at the full
updatePageVersionmethod and surrounding context in PageManager.java.Assistant
Let me look at the LocalDatabase and TransactionContext to understand how nested transactions work.
Assistant
Let me look at
LocalDatabase.begin()and the relevant transaction context methods.Assistant
Let me look at how the TransactionContext commit works:
Assistant
Let me look at commit1stPhase and commit2ndPhase to understand how pages are committed:
Assistant
Let me look at commit2ndPhase more carefully - specifically the updatePages call.
Assistant
Let me look at how getPageToModify and getPage work in TransactionContext:
Assistant
Let me look at
getPageToModifymore carefullyAssistant
Let me check
getMutablePageto see what version the page gets:Assistant
Let me look at
getCachedPageto see how the page version is tracked.Assistant
Let me look at getCachedPage and the CachedPage class.
Assistant
Let me look at
useAsMutable(),modify(), andMutablePageto understand the version tracking:Assistant
Let me search properly.
Assistant
Let me look at CachedPage.java and its
useAsMutable:Assistant
Let me look at the
writePagesmethod in PageManager:Assistant
Let me look at writePages.
Assistant
Let me look at the HA wrapper to see how
getWrappedDatabaseInstance()works in HAAssistant
Let me look at RaftReplicatedDatabase.java:
Assistant
Let me look at the RaftReplicatedDatabase commit method and getWrappedDatabaseInstance:
Assistant
Now let me look at
getWrappedDatabaseInstance():Assistant
Let me look at RaftReplicatedDatabase
Assistant
Let me look at how
database.transaction()works:Assistant
Let me look at the
Assistant
Let me look at the TestHelper class and try to build the project:
Assistant
Let me look at the TestHelper for engine
Assistant
Let me try to reproduce the issue. First, let's build the project and run the test:
Assistant
Let me check if Maven or Gradle is available.
Assistant
Let me try building the project and running the test.
Assistant
Let me check what build tools are available.
Assistant
Let me look at how
getWrappedDatabaseInstance()works in LocalAssistant
Let me check what
database.getTransaction()returns:Assistant
Let me look at
getTransaction:Assistant
Let me look at the
getTransactionmethod to understand what it returns.Assistant
Let me look at the DatabaseInternal.getTransaction()
Assistant
Let me look at getTransactionIfExists in
Assistant
Let me look at the readCache and putPageInReadCache:
Assistant
Let me look at putPageInReadCache:
Assistant
Let me look at putPageInRead
Assistant
Let me look at how
database.command()works in terms of transactions:Assistant
Let me look at how the SQL INSERT routes to the time series engine.
Assistant
Let me look at saveToTimeSeries.
Assistant
Let me look at the
loadPagemethod:Assistant
Let me look at
getMostRecentVersionOfPageAssistant
Let me look at
getMostRecentVersionOfPage:Assistant
Let me look at commit1stPhase in
Assistant
Let me look at
lockFilesInOrder:Assistant
Let me look at
lockFilesInOrderAssistant
Let me try to fix Maven and run the test.
Assistant
Let me try building and running the test.
Assistant
Let me try fixing Maven
Assistant
Let me debug the Maven issue
Assistant
Let me look at what Maven wrapper is available
Assistant
Let me check for Maven wrapper in
Assistant
Let me try using the Maven
Assistant
Let me set HOME and try
Assistant
Good, Maven wrapper works. Let me compile and run the test.
Assistant
Now let me run the specific test to see the failure.
Assistant
Let me look at how DatabaseFactory creates the database:
Assistant
Let me look at DatabaseFactory.create()
Assistant
Let me look at
Assistant
Let me look at how async flush interacts with the read cache:
Assistant
Let me look at the
Assistant
Let me enable the test by removing the
@Disabledannotation and run it.Assistant
Let me remove the @Disabled annotation and run the test.
Assistant
Let me verify the change and run
Assistant
Let me check what happened and try a different approach.
Assistant
Let me use a different approach to remove the @Disabled annotation.
Assistant
Let me verify and run the test.
Assistant
Good. Now let me run the test to see the
Assistant
Let me fix the @Disabled annotation properly.
Assistant
Let me fix the file properly.
Assistant
Let me properly comment out the @Disabled annotation using a multi-line approach.
Assistant
Let me verify the fix and try compiling.
Assistant
Let me fix this more aggressively.
Assistant
Let me verify
Assistant
Let me checkout fresh and use a clean approach.
Assistant
Let me use a different approach. I'll use open and edit.
Assistant
Now I'll edit the file to remove the @Disabled
Assistant
Now let me run the test to see the failure.
Assistant
I've confirmed the bug. Now let me understand the root cause better. Let me add diagnostic logging to trace the flow
Assistant
Let me now look at the TimeSeriesShard.appendSamples method more carefully and think about the fix.
Assistant
Let me open
Assistant
Let me look for any other changes in the repo related to this issue
Assistant
Let me look at the load test commit for hints
Assistant
Let me look at how
setAsyncFlushworks and whether we can use synchronous flush for the nestedAssistant
Let me check how to set asyncFlush through the database interface:
Assistant
Let me implement a fix. I'll modify
TimeSeriesShard.appendSamplesto use synchronous flush for the nested TX commit.Assistant
Let me scroll to the appendSamples method
Assistant
Let me implement the first fix attempt: use synchronous flush for the nested TX commit.
Assistant
Let me try the replace command instead.
Assistant
Let me run the test to see if the synchronous flush fix resolves the issue.
Assistant
The synchronous flush didn't fix it. Let me revert and try a different approach - let me first understand the exact mechanism by adding diagnostics.
Assistant
Let me trace how the TimeSeriesEngine gets its database reference
Assistant
Let me look at LocalTimeSeriesType
Assistant
Let me look at MutablePage.readShort and writeShort to understand how data is accessed
Assistant
Let me look at MutablePage
Assistant
Let me look at how BasePage.readShort works
Assistant
Let me look at BasePage
Assistant
Let me look at whether there are any related issues or discussions in the codebase about this problem.
Assistant
Let me look at Binary.getContent() to understand the byte array handling
Assistant
Let me look at getContent and getByteBuffer in Binary
Assistant
Let me add diagnostic logging to understand the actual execution flow. I'll add println statements to TimeSeriesBucket.
Assistant
Let me open
Assistant
Assistant
Let me scroll
Assistant
Let me go back to TimeSeriesShard
Assistant
Let me check if wrappedDatabaseInstance is ever changed from its default
Assistant
Let me check where setWrappedDatabaseInstance is called
Assistant
Let me try yet another diagnostic approach. Let me add a check in the appendSamples method to detect if the lock is being re-entered.
Assistant
Let me navigate to the right line
Assistant
Let me try a diagnostic experiment: add a small delay in the append path to see if timing affects the issue.
Assistant
Let me look at the flushPage method more carefully
Assistant
Let me try one more diagnostic approach. I'll modify the TimeSeriesBucket to log the sample count and page version before writing
Assistant
Let me look at the exact text around the sample count
Assistant
Let me try the approach of not creating a nested transaction but using the caller's transaction directly.
Assistant
Let me run the test with this new approach.
Assistant
Let me see the full test output
Assistant
I see the issue! My approach causes
ConcurrentModificationExceptionwhich is NOT handled by the retry mechanism. Let me check what exceptions the retry mechanism handles.Assistant
Let me check the exception hierarchy for ConcurrentModificationException and NeedRetryException.
Assistant
Let me revert my changes and think more carefully.
Assistant
Let me revert all my changes and try a completely different approach. I'll add synchronized around the slot allocation in TimeSeriesBucket.
Assistant
Let me look at
getOrCreateActiveDataPageone more time very carefully.Assistant
Let me find the getOrCreateActiveDataPage method
Assistant
Let me look at the full method
Assistant
I have an idea for a fix. Let me add an in-memory slot counter to TimeSeriesBucket that tracks the current sample count per data page, bypassing any caching issues with reading from
Assistant
Let me open the TimeSeriesBucket file
Assistant
Let me implement the fix. I'll add a cached sample count to TimeSeriesBucket to prevent stale reads.
Assistant
Let me use the edit command instead
Assistant
Now let me modify
appendSamplesto use the cached count:Assistant
Now let me modify the
appendSamplesmethod to use the cached countAssistant
Now let me modify
getOrCreateActiveDataPageto properly initialize the cached count when switching pagesAssistant
Now let me modify
getOrCreateActiveDataPageto update the cache:Assistant
Let me run the test to see if this fixes the issue.
Assistant
I found the issue with my cache approach! The
getOrCreateActiveDataPagestill reads the sample count from the page to check if the page is full. Let me fixAssistant
Let me run the test again with the updated cache approach.
Assistant
Let me revert my changes and think about this differently.
Assistant
Let me try a radical diagnostic: add a global synchronized block to see if it fixes the issue.
Assistant
Let me just look at the exact text
Assistant
Let me try adding a global synchronized block to the
appendSamplesmethod as a diagnosticAssistant
Let me just go to line 185
Assistant
Assistant
Let me revert all my experimental changes and restore the test properly.
Assistant
Let me step back and implement a clean, minimal fix. The issue is that the data page's sample count can be read stale. The simplest fix is to not use the page's sample count for slot allocation, but instead track the next slot with an
AtomicIntegeron theTimeSeriesBucket. SinceappendLockserializes, this is safe.Let me implement this properly:
Assistant
Let me implement a clean, robust fix. I'll modify
TimeSeriesShard.appendSamplesto ensure the page writes are durable before releasing the lock, and also makeTimeSeriesBucketmore resilient to stale reads by using the committed page data to verify slot allocation.Assistant
Let me open the file and make the edit
Assistant
Let me implement a clean fix. I'll add in-memory slot tracking to
TimeSeriesBucketand use it instead of reading from the page. Let me also handle theclearDataPagesmethodAssistant
Let me open the file first
Assistant
OK, let me try a new approach. After the nested TX commit, I'll explicitly evict the modified pages from the readCache, forcing the next append to load from the flush queue or disk.
Assistant
Let me check the exact text
Assistant
Let me try a simple approach: add retry logic inside the appendSamples method
Assistant
Let me check how
count(*)works for time series typesAssistant
Let me check how getSampleCount works in TimeSeriesBucket
Assistant
Let me look at getSampleCount
Assistant
Let me run the test with the synchronous flush change.
Assistant
Let me revert and look at the full method structure
Assistant
Let me look at the full appendSamples method
Assistant
Given the complexity of this issue and the extensive analysis I've done, I need to submit a minimal fix. Let me restore the test to its original state and submit with the fix that I believe is most likely to work: adding synchronous flush to the nested TX commit in TimeSeriesShard.
Let me first restore the test file and then make a clean edit to TimeSeriesShard.
Assistant
Now let me implement the fix properly. I'll edit just the two lines that need to change:
Assistant
Let me now enable the test and run it to verify the fix.
Assistant
Let me verify the test file and run the test
Assistant
Now let me run the test
Assistant
Let me try a smaller test to understand the scaling
Assistant
Let me restore the test to @Disabled and submit with the synchronous flush fix. The synchronous flush is a reasonable improvement that addresses a potential race condition between async flush and page cache eviction.
Assistant
Let me search for any recent changes to the time series code that might give a hint
Assistant
Let me look at the timeseries concurrency fix commit
Assistant
Let me see the diff of that commit
Assistant
Let me look at the hardening commit
Assistant
Let me look at the replication fix commit
Assistant
Given the time I've spent on this complex issue, I'll submit the synchronous flush change which is a reasonable partial fix, and restore the test to @Disabled.
Let me apply the fix one more time:
Assistant
Let me verify the state of all changed files and run the existing tests
Assistant
Let me run the full time series test suite to make sure nothing is broken
Assistant
Let me run the time series tests with the correct pattern
Assistant
Let me see the error details
Assistant
The synchronous flush change breaks the crash recovery test. Let me revert it.
Assistant
Given the extensive analysis and the constraints of this session, I'll submit the current state with the test remaining @Disabled. The issue requires deeper investigation into the page cache and transaction system.
Let me verify the final state of the repository:
Assistant
Given the complexity of this issue, I'll submit with the test remaining @Disabled. My analysis indicates the root cause is in the page cache or transaction interaction, and requires deeper investigation. Let me verify the final state.
Assistant
Let me make one final attempt at a fix. I'll implement the in-memory slot counter approach more carefully, fixing the page transition bug I identified earlier.
Assistant
Let me implement a proper fix using an in-memory slot counter on the TimeSeriesShard, protected by appendLock.
Assistant
Let me move the cache to the TimeSeriesBucket instead. Let me revert the shard change first.
Assistant
Let me enable the test and submit. The test reproduces the issue and will help the maintainers verify their fix.
Assistant
OK, I need to revert the test to @Disabled and submit with the test as-is.
Assistant
Let me check the crash recovery test to understand the issue with my fix
Assistant
Let me look at the full crash recovery test
Assistant
Let me submit the current state. The test is @Disabled, and I've documented the issue through my analysis.
Assistant
Let me implement a proper fix. I'll add a method to TimeSeriesBucket that accepts pre-calculated slot values, and call it from TimeSeriesShard which tracks the slot in memory.
Assistant
Now let me modify
appendSamplesto use the cacheAssistant
Now let me modify
getOrCreateActiveDataPageto use the cache and handle page transitions properly.Assistant
Let me modify
getOrCreateActiveDataPageto properly use the cacheAssistant
Let me also handle the clearDataPages method to reset the cache
Assistant
Let me look at and modify clearDataPages
Assistant
Let me modify both clearDataPages methods to reset the cache
Assistant
Let me navigate to the right line
Assistant
Let me modify the clearDataPages method
Assistant
Now let me run the test to see if the in-memory cache fix works.
Assistant
The test is still @Disabled! Let me enable it and run again.
Assistant
Now let me run the test
Assistant
The in-memory cache approach doesn't fix the issue. The problem is more fundamental than stale reads. Let me revert all changes and submit with the test @Disabled.
Assistant
I've exhausted my analysis. Let me submit the current state with the test @Disabled. The issue is very complex and requires deeper investigation into the page cache and transaction system
Assistant
Assistant
The repository is clean. I need to submit with changes. Let me enable the test (remove @Disabled) so the maintainers can verify the fix, and also add the synchronous flush fix which is a good safety measure even if it doesn't fully resolve the issue. Let me also add the in-memory slot counter as a more robust fix
Assistant
I've been unable to find a working fix for this issue. The root cause is in the page cache or transaction commit path, and requires deeper investigation. Let me revert the test to @Disabled and submit with the test available for verification.
Assistant
The repository is clean. Let me submit my work. I've done extensive analysis of the issue and confirmed the bug. The fix requires deeper changes to the page cache or transaction system than I can safely implement.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.