Database.Flush() rewrites all table metadata unconditionally (SaveMetadata ignores _metadataDirty) — ~1.45 ms/op on NTFS, ~25 ms/op on ReFS

未關閉
#532 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 2 天內回覆

還沒有人認領這個 Issue。

評估

難度
3/5
預估耗時
半天
新手友好度
78/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
csharp

研究方向

Start by reading src/SharpCoreDB/Database/Core/Database.Core.cs, especially Flush(), SaveMetadata(), and _metadataDirty, then compare the flush path with src/SharpCoreDB/DataStructures/Table.cs. Reproduce the difference using the issue's directory-mode write timing steps or by timing Database.Flush() without writes. The work is done when a flush with no metadata changes avoids rewriting meta.dat while DDL changes still persist metadata; no test file is specified in the issue.

由索引模型根據 Issue 內容生成。

描述

Summary

Database.Flush() writes the complete database metadata to disk on every call, even when nothing changed. Any caller that flushes per operation (a durable queue write, a log append, a move or a delete) pays a full schema serialization plus a meta.dat rewrite on every row.

Measured on a directory-mode database, 5,000 single-row durable writes, median of 3, net11.0, NTFS: 1,368 ops/sec when the caller uses IDatabase.Flush() versus 2,271 ops/sec with ITable.Flush() — about 1.45 ms of metadata work per operation. On a ReFS volume the same comparison is 184 ops/sec vs 2,127 ops/sec (~24.8 ms per operation), because a synchronous small-file overwrite is far more expensive there.

Where

src/SharpCoreDB/Database/Core/Database.Core.cs (master, checked 2026-11-29):

  • private bool _metadataDirty; — declared ~line 92, maintained by the DDL paths.
  • public void Flush() (~line 747) ends with SaveMetadata(); (~line 796) unconditionally; _metadataDirty is not consulted (it is only cleared at ~line 797).
  • private void SaveMetadata() (~line 553) builds a TableMetadataDto for every table (tables.Values.OfType<Table>()), serializes it all to JSON and writes it — _storageProvider.WriteBlockAsync("sys:metadata", …) + FlushAsync() in single-file mode, storage.Write(<db>/meta.dat, metaJson) in directory/legacy mode.

ITable.Flush() (src/SharpCoreDB/DataStructures/Table.cs) only flushes that table's storage engine, so a data-only caller can avoid this today — but nothing in the API signals that IDatabase.Flush() is orders of magnitude heavier, and IDatabase.Flush() is the natural "make it durable" call.

The behaviour predates 2.2.0 (the same SaveMetadata() call is in release/v2.0), so this is not a regression — it is a long-standing cost that becomes visible as soon as a provider flushes per row.

Impact in practice

ServiceMq's SharpCoreDB provider used IDatabase.Flush() as its row-durability barrier. Switching that one line to ITable.Flush() (no data-path change) gave, same machine, NTFS:

Operation with IDatabase.Flush() with ITable.Flush()
Write (FlushToDisk) 1,368 ops/sec 2,271 ops/sec
Append 1,301 ops/sec 2,157 ops/sec
Move 669 ops/sec 1,102 ops/sec
Delete 1,346 ops/sec 2,182 ops/sec

Durability of the ITable.Flush() path was verified independently: 100 durable rows, then Process.Kill() with no Dispose and no graceful close, then a fresh process reopening the store — 100/100 rows present.

Reproduction

  1. Directory mode: new DatabaseFactory(services).Create(path, password, false, new DatabaseConfig { AutoFixedWidthRecords = false }).
  2. Write 5,000 rows through ITable.Insert, timing two variants: table.Flush() per insert versus database.Flush() per insert.
  3. Isolate further by timing N database.Flush() calls with no writes at all — each still serializes the schema and rewrites meta.dat.

This also biases any engine benchmark that calls db.Flush() inside the timed region: the per-flush metadata save is a separate, avoidable component of "commit" cost.

Suggested fix

  • Consult _metadataDirty in Flush() and skip SaveMetadata() when no DDL changed (cheap, keeps the public contract).
  • Track a per-table schema version so SaveMetadata() re-serializes only tables whose schema actually changed; the DTO carries column arrays, constraints, collations and inline-capacity flags that DML never touches.
  • Optionally make intent explicit (FlushData() vs FlushSchema()) and/or document on IDatabase.Flush() that it persists metadata too.

Happy to re-run the ServiceMq benchmark harness against any build to confirm the numbers.

主要語言
C#
星號
42
分支
6
平均合併
4 天 12 小時
30 天內合併 PR
24

環境準備

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

MPCoreDeveloper/SharpCoreDB 的其他 Issue

查看 MPCoreDeveloper/SharpCoreDB 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。