Skip to content

perf: use singleflight for more db queries to better handle many similar parallel requests#3023

Open
maxmanuylov wants to merge 1 commit into
Permify:masterfrom
JetBrains:fix-concurrency
Open

perf: use singleflight for more db queries to better handle many similar parallel requests#3023
maxmanuylov wants to merge 1 commit into
Permify:masterfrom
JetBrains:fix-concurrency

Conversation

@maxmanuylov

@maxmanuylov maxmanuylov commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

So far many similar parallel requests lead to a lot of DB calls fetching the same tuples and competing for DB pool.

This PR uses singleflight for such requests as well to improve the performance.

Summary by CodeRabbit

  • New Features

    • Improved response handling for repeated data lookups, especially when multiple identical requests arrive at the same time.
  • Bug Fixes

    • Reduced duplicate work for common read operations, which can improve performance and consistency under load.
    • Kept results isolated between different tenants, filters, and pagination requests.
    • Ensured query results can be reused safely without affecting other requests.

@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ βœ…
Posted by the CLA Assistant Lite bot.

@coderabbitai

coderabbitai Bot commented Jul 1, 2026

Copy link
Copy Markdown

Review Change Stack

πŸ“ Walkthrough

Walkthrough

DataReader in the singleflight proxy is extended with separate singleflight groups for QueryRelationships, QuerySingleAttribute, and QueryAttributes, adding key-builder and iterator-drain helpers. HeadSnapshot now uses a renamed field. Test mocks and suites are expanded to cover the new deduplication behavior.

Changes

Singleflight-backed query deduplication

Layer / File(s) Summary
DataReader struct and singleflight-wrapped query methods
internal/storage/proxies/singleflight/data_reader.go
Adds headSnapshotGroup, queryRelationshipsGroup, querySingleAttrGroup, and queryAttributesGroup fields; converts QueryRelationships, QuerySingleAttribute, and QueryAttributes to build dedup keys, execute via singleflight, drain iterators into slices, and return new iterators; HeadSnapshot uses the renamed headSnapshotGroup; adds fmt/strings imports and internal key-builder/drain helpers.
Mock DataReader query implementations and counters
internal/storage/proxies/singleflight/data_reader_test.go
MockDataReader gains per-method counter maps and a mutex, new QueryRelationships/QuerySingleAttribute/QueryAttributes implementations with call counting, a shared getCallCount helper, and ErrorMockDataReader returns errors for the new query methods; adds anypb import.
Test suite updates for query dedup coverage
internal/storage/proxies/singleflight/data_reader_test.go
Removes old delegation-only tests, updates HeadSnapshot assertions to use GetCallCount, and adds tests for concurrent dedup, tenant/filter isolation, sequential non-dedup, error propagation, empty results, and iterator independence for the new query methods.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Possibly related PRs

  • Permify/permify#2649: Expands the same data_reader_test.go Singleflight test coverage, including ErrorMockDataReader error propagation and call-count assertions for DataReader methods.
πŸš₯ Pre-merge checks | βœ… 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 44.44% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
βœ… Passed checks (4 passed)
Check name Status Explanation
Description Check βœ… Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check βœ… Passed The title clearly summarizes the main change: expanding singleflight deduplication for parallel database queries.
Linked Issues check βœ… Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check βœ… Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
πŸ§ͺ Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❀️ Share

Comment @coderabbitai help to get the list of available commands.

@maxmanuylov

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Jul 1, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

πŸ€– Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/storage/proxies/singleflight/data_reader_test.go`:
- Around line 385-390: The test goroutine in data_reader_test.go is calling
Ginkgo assertions inside go func() without recovery, which can make failures
escape the spec. Add defer GinkgoRecover() at the start of each goroutine that
uses Expect, including the QueryRelationships helper in the singleflight data
reader tests, so assertion failures are handled by Ginkgo instead of crashing or
flaking the suite.

In `@internal/storage/proxies/singleflight/data_reader.go`:
- Around line 33-43: Clone the materialized protobuf results before returning
from the singleflight paths in data_reader.go: the tuples returned by the
QueryRelationships flow and the attribute objects returned by
QuerySingleAttribute and QueryAttributes should be deep-copied after
draining/materializing and before constructing the final iterator or response.
Use the existing helpers around queryRelationshipsGroup and any attribute-query
singleflight logic to ensure each caller gets its own copies, preventing shared
protobuf pointers from being mutated across duplicate requests.
πŸͺ„ Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
βš™οΈ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 74726ccc-8d17-4eb3-bb5e-08eedb224cb7

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between aa3a7c6 and e783304.

πŸ“’ Files selected for processing (2)
  • internal/storage/proxies/singleflight/data_reader.go
  • internal/storage/proxies/singleflight/data_reader_test.go

Comment thread internal/storage/proxies/singleflight/data_reader_test.go
Comment thread internal/storage/proxies/singleflight/data_reader.go
@codecov

codecov Bot commented Jul 1, 2026

Copy link
Copy Markdown

Codecov Report

βœ… All modified and coverable lines are covered by tests.
βœ… Project coverage is 74.87%. Comparing base (aa3a7c6) to head (e783304).

❌ Your project check has failed because the head coverage (74.87%) is below the target coverage (75.00%). You can increase the head coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3023      +/-   ##
==========================================
+ Coverage   74.67%   74.87%   +0.20%     
==========================================
  Files          83       83              
  Lines        9212     9281      +69     
==========================================
+ Hits         6878     6948      +70     
  Misses       1798     1798              
+ Partials      536      535       -1     

β˜” View full report in Codecov by Harness.
πŸ“’ Have feedback on the report? Share it here.

πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • πŸ“¦ JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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