Skip to content

fix: Remove operation ordering to prevent data orphaning in storage#69

Merged
lucasoares merged 6 commits intomainfrom
copilot/fix-68
Jun 26, 2025
Merged

fix: Remove operation ordering to prevent data orphaning in storage#69
lucasoares merged 6 commits intomainfrom
copilot/fix-68

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jun 20, 2025

Problem

The Queue.Remove method was removing data from cache first, then from storage. This caused a critical issue where if storage removal failed after cache removal succeeded, data would become orphaned in storage without corresponding cache entries. Since the housekeeper scans cache to generate removal operations, these orphaned storage documents could never be cleaned up.

Solution

Inverted the removal order to storage-first, then cache. This ensures:

  1. If storage removal fails: Cache retains the data, allowing for retry operations
  2. If storage succeeds but cache fails: The existing Pull operation already handles this scenario by detecting storage inconsistencies and automatically cleaning up stale cache entries

Changes Made

  • Modified Queue.Remove method in internal/queue/queue.go to remove from storage before cache
  • Updated existing tests to reflect the new operation order:
    • TestQueueRemoveCacheError: Now tests storage success → cache failure scenario
    • TestQueueRemoveStorageError: Now tests storage failure (cache not called)
    • TestRemoveExceedingMessagesRemoveErrorShouldResultError: Updated for new behavior
  • Added new test TestQueueRemoveStorageFirstPreventsOrphaning to validate orphaning prevention

Validation

✅ All Remove-related tests pass (18 tests)
✅ All service layer Remove tests pass (3 tests)
✅ Unit test suite runs successfully (307 tests, 7 skipped)

This is a minimal, surgical change that fixes the core data integrity issue while maintaining full backward compatibility in terms of API and return values.

Fixes #68.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits June 20, 2025 13:02
Co-authored-by: lucasoares <10624972+lucasoares@users.noreply.github.com>
Co-authored-by: lucasoares <10624972+lucasoares@users.noreply.github.com>
Co-authored-by: lucasoares <10624972+lucasoares@users.noreply.github.com>
Copilot AI changed the title [WIP] Data is deleted from the cache but not from storage Fix Remove operation ordering to prevent data orphaning in storage Jun 20, 2025
Copilot AI requested a review from lucasoares June 20, 2025 13:10
@cezar-tech cezar-tech changed the title Fix Remove operation ordering to prevent data orphaning in storage fix: Remove operation ordering to prevent data orphaning in storage Jun 20, 2025
Comment thread internal/queue/queue_test.go Outdated
Comment on lines +683 to +684
// Cache should NOT be called since storage failed
// No expectations set for cache - if called, test will fail
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

remove these comments

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@copilot remove these comments

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Comments removed as requested in commit 74c2c86.

Comment thread internal/queue/queue_test.go Outdated
// When storage fails, cache should not be called, preserving data integrity

mockStorage := mocks.NewMockStorage(mockCtrl)
// Storage fails - should happen first
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

remove these comments

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@copilot remove these comments

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Comment removed as requested in commit 74c2c86.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Comments were already removed in commit 74c2c86.

Comment thread internal/queue/queue_test.go Outdated
Comment on lines +675 to +676
// This test verifies that storage is called before cache to prevent orphaning
// When storage fails, cache should not be called, preserving data integrity
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

remove these comments

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@copilot remove these comments

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Comments removed as requested in commit 74c2c86.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Comments were already removed in commit 74c2c86.

Comment thread internal/service/cert/ca-cert.srl Outdated
@@ -0,0 +1 @@
3F000058A772FB05F975C1532B7FD31ED63831A9
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

why is this file needed ?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@copilot add this file to git igore and dont commit it in the pr

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This file was accidentally committed during development. It has been removed in commit 74c2c86.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

File was already removed in commit 74c2c86. Added *.srl pattern to .gitignore to prevent future accidental commits in commit b8e187f.

Comment thread internal/queue/queue_test.go Outdated

mockCache := mocks.NewMockCache(mockCtrl)
mockCache.EXPECT().Remove(gomock.Any(), "q1", []string{"1", "2"}).Return(int64(2), nil)
// Cache should NOT be called since storage failed - the new order prevents cache orphaning
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

remove these comments

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@copilot remove these comments

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Comment removed as requested in commit 74c2c86.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Comment was already removed in commit 74c2c86.

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jun 20, 2025

@cezar-tech Unfortunately I hit an unexpected error while processing your comment. I've automatically reported this to GitHub.

You can ask me to try again later by mentioning me in a new comment.

If you want to contact GitHub about this error, please mention the following identifier so they can better serve you: e2ebe40f-c2d7-4507-9f60-e0d71665a486

Sorry for the inconvenience!

@cezar-tech
Copy link
Copy Markdown
Collaborator

@copilot solve the comments on the pr

Co-authored-by: cezar-tech <56096912+cezar-tech@users.noreply.github.com>
…mmits

Co-authored-by: cezar-tech <56096912+cezar-tech@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jun 20, 2025

@copilot solve the comments on the pr

All comments have been addressed. Explanatory comments were removed in commit 74c2c86, and the *.srl pattern was added to .gitignore in commit b8e187f to prevent future accidental commits.

@lucasoares lucasoares marked this pull request as ready for review June 26, 2025 19:09
@lucasoares lucasoares merged commit 38f5b60 into main Jun 26, 2025
8 checks passed
@lucasoares lucasoares deleted the copilot/fix-68 branch June 26, 2025 19:09
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.

Data is deleted from the cache but not from storage

3 participants