Skip to content

fix: metadata only serialized on schema change (#532) + document at-rest encryption default (#531) - #533

Open
MPCoreDeveloper wants to merge 2 commits into
masterfrom
fix/532-metadata-flush-cost
Open

MPCoreDeveloper wants to merge 2 commits into
masterfrom
fix/532-metadata-flush-cost

Conversation

@MPCoreDeveloper

Copy link
Copy Markdown
Owner

Summary

Fixes the metadata-flush cost in issue #532 and documents the at-rest-encryption default flip in issue #531, with regression tests for both. Issue #171 is dispositioned as out of scope for this repository.

#532 — Database.Flush() no longer re-serializes every table's metadata on a data-only flush

Database.Flush() called SaveMetadata() unconditionally, so any caller that flushed per operation (a durable queue write, a log append, a move or a delete) paid a full TableMetadataDto build + JSON serialization + meta.dat rewrite on every row — ~1.45 ms/op on NTFS and ~24.8 ms/op on ReFS per the reporter.

Key finding: the issue's suggested fix ("consult _metadataDirty") is insufficient, because _metadataDirty is also set by INSERT/UPDATE/DELETE — it tracks pending data for SELECT visibility, not schema. The fix introduces a dedicated _schemaDirty signal and gates SaveMetadata() in Flush() on it. The DDL path already persists the schema eagerly through ForceSave() (the batch dispatcher and Dispose save it directly), so a data-only flush now skips the metadata work while the data flush (WAL batch buffer, tables, storage provider, storage engine) and its durability are unchanged.

Files: Database.Core.cs (_schemaDirty, MetadataSaveCount, Flush() gate, SaveMetadata() clear), Database.Execution.cs (4 DDL branches), Database.Batch.cs (batch schema branch).

#531 — documented the 2.2.0 EnableAtRestRecordEncryption default flip

  • Property XML doc: records the false (2.0.0.x) → true (2.2.0) flip, the ~40% (~1.66×) point-read cost, and the EnableBatchEncryption interaction (a different layer).
  • docs/CHANGELOG.md: 2.2.0 Changed entry for the flip.
  • DatabaseConfigDefaultsTests pins the secure default so an accidental flip-back fails the build.
  • The default stays true (constitution Principle I); flipping it is an owner decision.

#171 — out of scope

The graphical UI belongs to MPCoreDeveloper/SCDMS (constitution Principle V). No code; dispositioned in the spec.

Tests

  • DatabaseFlushMetadataTests (4 cases) — 2 failed before (MetadataSaveCount 1→2 on one flush, 1→21 over 20 flushes), 4 pass after.
  • DatabaseConfigDefaultsTests (2 cases) — pass.
  • Regression canaries + new tests: 63/63.
  • Wider core suite (Category!=Debug/Manual/Performance): 3091 tests / 0 failed / 22 skipped.
  • Build: 0 errors.

Spec-kit

specs/001-metadata-flush-cost/{spec,plan,tasks}.md.

Issue closure

#532 closes only after the reporter's own ServiceMq harness is re-run against a fixed build (.clinerules/30-issue-closure.md); the fix is on this branch, not yet released. #531's documentation asks are answered; its ask 3 (workload-dependent default) is an explicit owner decision. Merging into master is an owner action.

Database.Flush() called SaveMetadata() unconditionally, so any caller that
flushed per operation (a durable queue write, a log append, a move or a
delete) paid a full TableMetadataDto build + JSON serialization + meta.dat
rewrite on every row -- measured ~1.45 ms/op on NTFS and ~24.8 ms/op on ReFS.

Introduce a dedicated schema-dirty signal (_schemaDirty), separate from the
existing data-dirty flag (_metadataDirty, set by INSERT/UPDATE/DELETE for
SELECT visibility), and call SaveMetadata() from Flush() only when the schema
actually changed. The DDL path already persists the schema eagerly through
ForceSave() (and the batch dispatcher / Dispose save it directly), so a
data-only flush now skips the metadata work while the data flush (WAL batch
buffer, tables, storage provider, storage engine) and its durability are
unchanged.

Also document the 2.2.0 EnableAtRestRecordEncryption default flip (false in
2.0.0.x, true in 2.2.0; ~40% point-read cost; EnableBatchEncryption is a
different layer) on the property and in the changelog, and pin the secure
default with DatabaseConfigDefaultsTests so an accidental flip back fails the
build (#531).

Tests:
- DatabaseFlushMetadataTests (4 cases) -- 2 failed before (MetadataSaveCount
  1->2 on one flush, 1->21 over 20 flushes) and pass after.
- DatabaseConfigDefaultsTests (2 cases).
Wider core suite: 3091 tests / 0 failed / 22 skipped; canaries + new tests
63/63. Build: 0 errors.

Refs #532, #531. #532 closes only after the reporter's ServiceMq harness is
re-run against the fixed build (see .clinerules/30-issue-closure.md).
@codecov

codecov Bot commented Nov 29, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 44.44444% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/SharpCoreDB/Database/Core/Database.Core.cs 50.00% 1 Missing and 1 partial ⚠️
...arpCoreDB/Database/Execution/Database.Execution.cs 50.00% 2 Missing ⚠️
...c/SharpCoreDB/Database/Execution/Database.Batch.cs 0.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

The comparative harness flushes once per batch/phase, so it never exercises
the per-row IDatabase.Flush() pattern issue #532 is about. Add a focused
--flush-per-row mode: N single-row ITable.Insert writes, each followed by
either IDatabase.Flush() or ITable.Flush(), interleaved and reported as a
median on the same table and machine.

Measured (AppendOnly, at-rest records on, 5,000 rows/arm, 2 reps):
  before the fix: IDatabase.Flush 3,109 ops/s vs ITable.Flush 5,063 ops/s (1.63x gap)
  after the fix:  IDatabase.Flush 5,301 ops/s vs ITable.Flush 5,281 ops/s (1.00x gap)

Refs #532.
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant