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
I'll start by exploring the repository structure and the relevant time-series files mentioned in the issue.
- Assistant
Let me look at the concurrent insert test and the key source files.
- Assistant
Good, the test exists. Let me read the key files.
- Assistant
Next I'll read the append path in TimeSeriesShard and TimeSeriesBucket.
- Assistant
Let me read the appendSamples methods in detail.
- Assistant
I need to use the correct syntax for open.
- Assistant
Let me read the appendSamples method specifically.
- Assistant
I need to understand nested transaction commit behavior and how pages are published.
- Assistant
Let me look at the begin and commit methods more carefully to understand nested transactions.
- Assistant
Checking how nested transaction commits interact with page visibility.
- Assistant
Let me look at commit of nested TX more carefully, and also how pages are read - specifically if nested TX commits, does the next append see the published page version?
- Assistant
Let me look at TimeSeriesBucket.appendSamples and getPage... Also check if there's a recent PR or commit related to this.
- Assistant
Let me look at getOrCreateActiveDataPage and how page versions work. Also look if nested transactions share anything with parent.
- Assistant
Interesting - so according to comments, nested IF commits independently and pages shouldn't be in outer dirty set. But the issue claims otherwise about durable publish being deferred.
- Assistant
I need to understand PageManager and how getPageToModify works. Perhaps the bug is that when nested TX commits, page is published, but outer TX from a concurrent transaction may have already gotten a ...
- Assistant
Let me look at the transaction method and also getPageToModify to understand versioning.
- Assistant
I'll check how SQL INSERT routes into the time series engine.
- Assistant
Looking at PageManager and getPageToModify for the versioning/copy logic.
- Assistant
Let me read getPageToModify and the page manager.
- Assistant
I'll try reproducing the issue with the concurrent insert test, first checking build setup.
- Assistant
Let me check git log for related commits and also look at any open fixing attempts. Also I should understand #4382 and HA nesting better.
- Assistant
I'll inspect the related concurrency and HA commits for context.
- Assistant
Let me look more carefully at how RaftReplicatedDatabase handles begin/commit - dir that's when nested and outer land on HA wrapper.
- Assistant
I'll check how tickets load PageManager mutex/version handling and DatabaseContext serialization across threads.
- Assistant
Let me read updatePageVersion and getMutablePage carefully.
- Assistant
Let me read updatePageVersion
- Assistant
I notice that when a page is put into the read cache after commit through
new CachedPage(page, true)for async flush - let me check if the MutablePage content is COPIED properly. - Assistant
I'll check DatabaseContext thread-local behavior and TestHelper setup.
- Assistant
Let me read full SaveElementStep for time series path.
- Assistant
Let me try building and running the test to reproduce. Also let me look at git blame/diff around the append samples music more carefully. And look at ContinuousAggregate schedule - that might do somet...I understand the issue you're describing—time series inserts under high concurrency are silently dropping samples due to a race in the per-page sample count read-modify-write. The appendLock serializes the in-memory append but the durable publish is deferred to the outer transaction, allowing stale reads on the same page.
- Assistant
I'll try building and running the regression test to reproduce the failure.
- Assistant
Need to set HOME.
- Assistant
I'll enable the concurrent insert test so we can reproduce the failure.
- Assistant
I'll run the enabled test now to reproduce the sample loss.
- Assistant
Reproduced: 120 samples lost. Next I'll inspect quality of nested commit vs outer TX pages.
- Assistant
So the outer txn shouldn't touch bucket pages—I'll search for issue #4382 and related publish deferral.
- Assistant
I'll add temporary diagnostics in appendSamples to see platform-level counts under the lock.
- Assistant
I'll inspect commit1stPhase for version checks and page locking.
- Assistant
There's a file lock order -
lockFilesInOrder. The appendLock is Java-level, but transaction manager also locks files. When nested commits, it releases file locks at the end of commit. - Assistant
I notice something few about NA. When nested TX commits phase1, it checks page versions. Then phase2 updates. File locks remain until after phase2?
- Assistant
Let me look at putPageInReadCache - is there a race where an older version PE can overwrite a newer one?
- Assistant
Let me read putPageInReadCache and getCachedPage
- Assistant
I'll check the default isolation level and whether SQL loads pages before the append.
- Assistant
Let me write a small diagnostic by patching TimeSeriesBucket temporarily to print sample counts. Actually, let me think differently. I see the core issue you're describing: the nested commit inside the lock isn't making the updated sample count visible to subsequent appends, even though the lock serializes the calls. That explains why the collision detector shows stale reads and why most inserts succeed but some overwrite slots.
Let me think about the transaction nesting here.
- Assistant
Look at DatabaseContext init - each worker thread needs DatabaseContext for the database. When transaction runs on another thread:
- Assistant
Let me check how checkDatabaseIsOpen initializes context for new threads.
- Assistant
Let me find the checkDatabaseIsOpen method body.
- Assistant
Let me open that.
- Assistant
I'll instrument briefly to observe whether nested commit publishes the updated sample count.
- Assistant
I'll track expected counts and detect drops instead of depending on pageSize.
- Assistant
Need to check if getPageSiz exists on PaginatedComponent.
- Assistant
getPageSize exists. Running the instrumented test next.
- Assistant
Header count resets mid-test (e.g. expected=52060 but before=0). Checking whether compaction is clearing the mutable bucket.
- Assistant
Interesting -
before=0after many samples may mean the peek itself is wrong. Checking BasePage offsets. - Assistant
Let me check if the flush thread can overwrite the cache with stale page content.
- Assistant
Interesting - eviction could force tainted reads. Looking at pageIndex.
- Assistant
Could clearDataPages run via compaction? Checking if maintenance starts automatically.
- Assistant
Compaction is scheduled on CREATE. Inspecting compact phases and races with append.
- Assistant
During Phase 1-2 of compaction (no lock) appends keep going—I'll carefully inspect Phase 4.
- Assistant
I'll disable maintenance/compaction and re-run to isolate whether the loss is from compaction or the RMW.
- Assistant
First compaction kicks in after 5 seconds — our ~19s test run would hit it multiple times.
- Assistant
I'll restore the original append path and short-circuit compact() to isolate the race.
- Assistant
I'll temporarily tweak the test to cancel maintenance so we can isolate the RMW bug.
- Assistant
There's a race: schedule is at fixed rate starting after 5s, gérer if cancel happens immediately after CREATE, before first run, we're good. CREATE schedules, we cancel immediately. Good.
- Assistant
Confirmed: loss comes from compaction, not the append RMW. Checking existing compaction tests and implementing the fix.on.
- Assistant
There's già a concurrent append/compact test - I'll read it.
- Assistant
Let me read the rest of the test.
- Assistant
I'll rewrite Phase 4 to hold the write lock for the residual pages so concurrent зе appends can't race.
- Assistant
Let me also read Phase 4 starting comment and Phase 4a start.
- Assistant
Replacing Phase 4a/4b/4c with a single write-locked Phase 4 so concurrent appends can't race residual page reads.
- Assistant
There's an extra closing brace and a typo to fix.
- Assistant
Removing the extra brace and fixing the typos.
- Assistant
I'll update the compact docsments and restore the concurrent insert test.
- Assistant
I'll update the compact javadoc around line 332 and the comment at 356, 375.
- Assistant
Updating comments that still mention 4c, and restoring the test.
- Assistant
Fixing wording in the test comments, then running the tests.
- Assistant
Running the concurrent insert and related compaction tests to verify the fix.
- Assistant
I'll re-run the concurrent insert test a few more times and a broader timeseries suite for stability.
- Assistant
I'll refine the Phase 4 docs to reflected that residual compress happens under the write lock.
- Assistant
Need to open the file at that line first maybe.
- Assistant
I see special unicode dash characters — let me use the exact text from the file.
- Assistant
Fix grammar "clear let" -> "clear lets"
- Assistant
Verifying the final diff is clean and only includes the intended changes.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.