Skip to content

[WIP] refactor datastore struct #641

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

Closed
wants to merge 1 commit into from

Conversation

nirrozenbaum
Copy link
Contributor

instead added ctx to one function that required it which is more aligned with best practices.

instead added ctx to one function that required it which is more aligned with best practices.

Signed-off-by: Nir Rozenbaum <[email protected]>
@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Apr 2, 2025
@k8s-ci-robot k8s-ci-robot requested review from ahg-g and kfswain April 2, 2025 12:21
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: nirrozenbaum
Once this PR has been reviewed and has the lgtm label, please assign sergeykanzhelev for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link

netlify bot commented Apr 2, 2025

Deploy Preview for gateway-api-inference-extension ready!

Name Link
🔨 Latest commit 4bb3be3
🔍 Latest deploy log https://app.netlify.com/sites/gateway-api-inference-extension/deploys/67ed2bdb531509000887d092
😎 Deploy Preview https://deploy-preview-641--gateway-api-inference-extension.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Apr 2, 2025
@ahg-g
Copy link
Contributor

ahg-g commented Apr 2, 2025

The existing logic is intentional. We need the parent context because it is used to start a metrics probing goroutine that should continue to live beyond the life of the reconcile event that triggered its creation.

/hold

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Apr 2, 2025
@nirrozenbaum
Copy link
Contributor Author

nirrozenbaum commented Apr 2, 2025

can you point me to the code you’re referring to?
reconcile event also gets context (in controller-runtime) and that can be used in a new go routine.
in general, best practice for context usage is to usually propagate the context between functions rather than saving in struct.

@ahg-g
Copy link
Contributor

ahg-g commented Apr 6, 2025

My point is that the context that is created as part of the pod reconcile even is short lived, its lifespan is related to that of the event. The metrics probing thread on the other hand is supposed to live longer than the pod reconcile event that triggered the creation of the metrics probing thread.

@nirrozenbaum
Copy link
Contributor Author

nirrozenbaum commented Apr 7, 2025

My point is that the context that is created as part of the pod reconcile even is short lived, it is live span is related to that of the event.

I agree. I was looking to see the specific code you're referring to in order to make sure I'm not missing something.

The metrics probing thread on the other hand is supposed to live longer than the pod reconcile event that triggered the creation of the metrics probing thread.

as far as I know, it's not recommended to store context in a struct. there are other ways in go to do what you're describing here. for example, datastore may use a stop channel and upon receiving a signal on that channel stop all its go routines (that are managed by the datastore struct, each may have a context that can be cancelled or a stop channel).
we can also use sync.WaitGroup to wait until all routines are stopped (internally in the datastore).

bottom line, best practices recommend not to save context in a struct but to propagate it in function calls. for the use case described above I think stop channel fits more.

if the above sounds reasonable to you, I can implement it, LMKWYT.

@ahg-g
Copy link
Contributor

ahg-g commented Apr 7, 2025

Sounds good, @liu-cong perhaps can help review your proposal.

@liu-cong
Copy link
Contributor

liu-cong commented Apr 7, 2025

@nirrozenbaum , yeah a stop channel sounds good to me. The key code path is line #239 in datastore.go: pm = ds.pmf.NewPodMetrics(ds.parentCtx, pod, ds), which creates a new PodMetrics object which behind the scenes starts a goroutine to refresh metrics. We need to make sure the goroutine is gracefully handled when receiving the SIGINT or SIGTERM (this is how the parentCtx is used today).

I can review it once you refactor this. Thanks!

@liu-cong
Copy link
Contributor

liu-cong commented Apr 7, 2025

/assign

@nirrozenbaum nirrozenbaum changed the title removed ctx from datastore struct [WIP] refactor datastore struct Apr 8, 2025
@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Apr 8, 2025
@kfswain
Copy link
Collaborator

kfswain commented Apr 8, 2025

if the above sounds reasonable to you, I can implement it, LMKWYT.

+1 sounds great

@k8s-ci-robot
Copy link
Contributor

PR needs rebase.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Apr 9, 2025
@kfswain
Copy link
Collaborator

kfswain commented Apr 23, 2025

are we going to use this PR for the restructuring?

@nirrozenbaum
Copy link
Contributor Author

main changed a lot and I'm busy in this cycle with other PRs, will create a new PR probably early next week.
can be closed for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants