Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor state store #4253

Conversation

AndersonQ
Copy link
Member

@AndersonQ AndersonQ commented Feb 14, 2024

What does this PR do?

It refactors the state store to:

  • Have an interface that better reflects what it actually does
    • methods do not return a slice of action when only 1 action is involved
    • action queue only accepts ScheduledAction
  • Add a Version field to the state store state
  • Test for saving 2 actions in the queue uses 2 upgrade actions as it's the only scheduled action so far.

Why is it important?

So the state store API reflects its actual usage and purpose.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • [ ] I have made corresponding changes to the documentation
  • [ ] I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works
  • [ ] I have added an entry in ./changelog/fragments using the changelog tool
  • [ ] I have added an integration test or an E2E test

Related issues

Questions to ask yourself

  • How are we going to support this in production?
  • How are we going to measure its adoption?
  • How are we going to debug this?
  • What are the metrics I should take care of?
  • ...

@AndersonQ AndersonQ changed the title WIP - refactor state store Refactor state store Feb 14, 2024
@AndersonQ AndersonQ changed the title Refactor state store [WIP] Refactor state store Feb 14, 2024
Comment on lines 47 to 50
type state struct {
ActionSerializer actionSerializer `json:"action,omitempty"`
AckToken string `json:"ack_token,omitempty"`
Queue fleetapi.Actions `json:"action_queue,omitempty"`
Copy link
Member Author

Choose a reason for hiding this comment

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

Add a field to version the store

Copy link
Contributor

mergify bot commented Feb 15, 2024

This pull request is now in conflicts. Could you fix it? 🙏
To fixup this pull request, you can check out it locally. See documentation: https://help.github.com/articles/checking-out-pull-requests-locally/

git fetch upstream
git checkout -b 3912-refactor-state-store upstream/3912-refactor-state-store
git merge upstream/bugfix/3912-borken-stat-store
git push upstream 3912-refactor-state-store

@AndersonQ AndersonQ force-pushed the 3912-refactor-state-store branch from b6811f3 to 708f615 Compare February 19, 2024 11:22
@AndersonQ AndersonQ marked this pull request as ready for review February 19, 2024 14:57
@AndersonQ AndersonQ requested a review from a team as a code owner February 19, 2024 14:57
@AndersonQ AndersonQ requested review from blakerouse and pchila and removed request for a team February 19, 2024 14:57
@elasticmachine
Copy link
Contributor

Pinging @elastic/elastic-agent (Team:Elastic-Agent)

@AndersonQ AndersonQ changed the title [WIP] Refactor state store Refactor state store Feb 19, 2024
Copy link
Contributor

mergify bot commented Feb 23, 2024

This pull request is now in conflicts. Could you fix it? 🙏
To fixup this pull request, you can check out it locally. See documentation: https://help.github.com/articles/checking-out-pull-requests-locally/

git fetch upstream
git checkout -b 3912-refactor-state-store upstream/3912-refactor-state-store
git merge upstream/bugfix/3912-borken-stat-store
git push upstream 3912-refactor-state-store

@AndersonQ AndersonQ force-pushed the 3912-refactor-state-store branch from 5eb5e3b to cc98d01 Compare February 23, 2024 06:14
Copy link
Member

@pchila pchila left a comment

Choose a reason for hiding this comment

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

Code looks fine except for some typos or nitpicks...

I wonder if we should go deeper with the refactor and have an ordered queue of actions with some retention mechanism (keep all the received but not yet run/unacked actions, keep at least 2 PolicyChangeActions, keep the UnEnroll action) in order to feed the dispatcher the necessary actions.

I realize that it is a big change but I think it would give us more flexibility and reliability as we have one more checkpoint between receiving an action from fleet and trying to run it.

Thoughts ?

internal/pkg/agent/cmd/run.go Outdated Show resolved Hide resolved
internal/pkg/agent/storage/store/state_store.go Outdated Show resolved Hide resolved
internal/pkg/agent/storage/store/state_store.go Outdated Show resolved Hide resolved
internal/pkg/agent/storage/store/state_store_test.go Outdated Show resolved Hide resolved
internal/pkg/agent/storage/store/state_store_test.go Outdated Show resolved Hide resolved
internal/pkg/agent/storage/store/state_store_test.go Outdated Show resolved Hide resolved
Copy link

Quality Gate passed Quality Gate passed

The SonarQube Quality Gate passed, but some issues were introduced.

2 New issues
0 Security Hotspots
68.3% 68.3% Coverage on New Code
0.0% 0.0% Duplication on New Code

See analysis details on SonarQube

@AndersonQ AndersonQ requested a review from pchila March 6, 2024 13:29
}
if st.Version != Version {
return nil, fmt.Errorf(
"invalid state store version, got %q isntead of %s",
Copy link
Member

@pchila pchila Mar 6, 2024

Choose a reason for hiding this comment

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

Suggested change
"invalid state store version, got %q isntead of %s",
"invalid state store version, got %q instead of %q",

@AndersonQ AndersonQ merged commit a7b7bca into elastic:bugfix/3912-borken-stat-store Mar 7, 2024
6 of 9 checks passed
@AndersonQ AndersonQ deleted the 3912-refactor-state-store branch March 12, 2024 06:10
@AndersonQ AndersonQ mentioned this pull request Apr 2, 2024
4 tasks
AndersonQ added a commit that referenced this pull request Jun 27, 2024
Fix action store -> state store migration tests (#4235)
* use golden file for store migration test
* remove withFile function
* migrate take in a storage.Store instead of the storage's path. It's needed so we can set the encrypted store vault's path

refactor state store (#4253)
It modifies the state store API to match the current needs.

update action model to match fleet-server schema (#4240)
* simplify fleetapi.Actions.UnmarshalJSON
* add test to ensure the state store is correctly loaded from disk
* skip state store migration tests, they will be fixes on a follow-up PR as part of #3912

add migrations for action and state stores (#4305)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants