Skip to content

perf: async batched edit-log writer with unified flush path#134

Merged
XuPeng-SH merged 1 commit intomatrixorigin:mainfrom
XuPeng-SH:optimize_2
Mar 25, 2026
Merged

perf: async batched edit-log writer with unified flush path#134
XuPeng-SH merged 1 commit intomatrixorigin:mainfrom
XuPeng-SH:optimize_2

Conversation

@XuPeng-SH
Copy link
Collaborator

What type of PR is this?

  • feat (new feature)
  • fix (bug fix)
  • docs (documentation)
  • style (formatting, no code change)
  • refactor (code change that neither fixes a bug nor adds a feature)
  • perf (performance improvement)
  • test (adding or updating tests)
  • chore (maintenance, tooling)
  • build / ci (build or CI changes)

Which issue(s) this PR fixes

Fixes #

What this PR does / why we need it

Replace synchronous per-operation sql.log_edit() calls with an async EditLogBuffer that collects entries via a bounded channel (4096) and flushes as multi-row INSERTs every 2s or when 64 entries accumulate.

Key changes:

  • EditLogFlusher trait abstracts flush target (SqlMemoryStore in prod, InMemoryFlusher in unit tests) so the full batching path is testable
  • Bounded channel with try_send for backpressure, retry-once on flush failure, oneshot-based flush() for deterministic test assertions
  • All edit-log write paths unified: store/correct/purge/store_batch/ purge_batch/purge_by_topic in MemoryService, governance quarantine/ cleanup in MCP tools + API routes, and GovernanceStore::log_edit via SqlMemoryStore.edit_log_tx sender propagation to background pools
  • MemoryService::new() returns Self (clean API); new_with_test_entries() for tests that need to assert on edit-log writes
  • Comprehensive e2e test suite (18 tests) verifying all 9 columns of mem_edit_log against the real database for every mutation path

Additional improvements:

  • Deduplicate shutdown_signal() into memoria-service (shared by CLI/MCP/API)
  • Graceful shutdown: signal handling + edit-log drain + flusher drain
  • Background flusher tasks (AccessCounter, LastUsed, ToolUsage, CallLog) now have shutdown signals via watch channel + JoinHandle await
  • Graph + entity link cleanup on correct/purge (hot path + governance fallback)
  • cleanup_orphan_graph_data() surfaces partial failures instead of swallowing
  • Batch optimizations: N+1 → batch entity/graph queries, tokio::join for hybrid search, get_stats_batch combines access_count + feedback
  • Moka caches: active_table (5s), cooldown, node_count (2min)
  • find_near_duplicate: single query with app-layer same-type preference
  • Test SQL injection hardening: parameterized queries + enum whitelist

@XuPeng-SH XuPeng-SH force-pushed the optimize_2 branch 2 times, most recently from 6c7e40a to 403ccce Compare March 25, 2026 05:36
Replace synchronous per-operation sql.log_edit() calls with an async
EditLogBuffer that collects entries via a bounded channel (4096) and
flushes as multi-row INSERTs every 2s or when 64 entries accumulate.

Key changes:
- EditLogFlusher trait abstracts flush target (SqlMemoryStore in prod,
  InMemoryFlusher in unit tests) so the full batching path is testable
- Bounded channel with try_send for backpressure, retry-once on flush
  failure, oneshot-based flush() for deterministic test assertions
- All edit-log write paths unified: store/correct/purge/store_batch/
  purge_batch/purge_by_topic in MemoryService, governance quarantine/
  cleanup in MCP tools + API routes, and GovernanceStore::log_edit via
  SqlMemoryStore.edit_log_tx sender propagation to background pools
- MemoryService::new() returns Self (clean API); new_with_test_entries()
  for tests that need to assert on edit-log writes
- Comprehensive e2e test suite (18 tests) verifying all 9 columns of
  mem_edit_log against the real database for every mutation path

Additional improvements:
- Deduplicate shutdown_signal() into memoria-service (shared by CLI/MCP/API)
- Graceful shutdown: signal handling + edit-log drain + flusher drain
- Background flusher tasks (AccessCounter, LastUsed, ToolUsage, CallLog)
  now have shutdown signals via watch channel + JoinHandle await
- Graph + entity link cleanup on correct/purge (hot path + governance fallback)
- cleanup_orphan_graph_data() surfaces partial failures instead of swallowing
- Batch optimizations: N+1 → batch entity/graph queries, tokio::join for
  hybrid search, get_stats_batch combines access_count + feedback
- Moka caches: active_table (5s), cooldown, node_count (2min)
- find_near_duplicate: single query with app-layer same-type preference
- Test SQL injection hardening: parameterized queries + enum whitelist
@XuPeng-SH XuPeng-SH merged commit e3718c2 into matrixorigin:main Mar 25, 2026
4 checks passed
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