-
Notifications
You must be signed in to change notification settings - Fork 122
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
feat: add Relation.active, exclude inactive relations from Model.relations #1091
Conversation
…odel.relations. Still need to add unit tests covering the new functionality.
I found 25 Charms (in the 150+ Canonical charms list we have collated) that have relation-broken events. 4 of those 25 use Details are in the doc linked in the description. |
… treated as-if this was a relation-broken event.
Among the use cases this should address:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple of questions and some minor comments.
@tonyandrewmeyer Do you happen to have links to the code where these 3 charms do this? I'd be curious to see examples of that and how people are doing it now. |
Oh wait, I see you've linked to a couple of them in your follow-up comment above -- that's fine, thanks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thanks! Couple of very minor optional comments.
Co-authored-by: Ben Hoyt <[email protected]>
@tonyandrewmeyer @benhoyt I think I might be missing something—how do charms access the relations that are breaking if they are excluded from mysql-router relies on being able to access relations that are breaking so that it can properly clean up database users: https://github.com/canonical/mysql-router-k8s-operator/blob/d89b0f820cfc574b04e0b86b5412db9da0d7ff2e/src/relations/database_provides.py#L200-L208 More context: #940 (comment) |
If you're in the relation-broken event, then For the case above, you're basically doing the same thing when you raise the Am I missing something? (For what it's worth, I think that the Juju folk would say that relation-departed is probably where the clean-up ought to happen, rather than relation-broken). |
If a relation is breaking, we expect it to be in This change in ops means that the relation won't be in |
During relation-departed, we don't know if the relation is breaking or the unit is shutting down In the first case, we need to remove the user. In the second case, we need to not remove the user. More details: #940 (comment) |
Ah, that's the part I had missed, thanks! We (mostly I) reviewed a lot of I did run the tests (whichever ones a plain We did know that this is backwards incompatible, which is why we tried to find cases where it might break charms, not just assume it would be ok. Obviously we (again, mostly I) didn't manage to do that well enough (and particularly given that you had commented in the original ticket: sorry - I did read that ticket many many times but it's tough to follow). It would be easy enough to change the charm to work with the ops change - e.g. have if not event.relation.active:
relation.delete_user(shell=shell) In the If you want all the relations, even the broken one, then as long as you have the Note that the intention is that Juju also makes this change, and that ops was just 'leading the way'. @benhoyt is finished for the week, but I'll talk this over with him on Monday to decide whether this is too much of a change to make after all, and we should limit this to just the Thanks for bringing this up (again), especially before the 2.10 release! |
These would work, although it somewhat defeats the purpose of not needing to pass the |
What exactly does this mean? Will we no longer be able to access local (not remote) databags during relation broken events? |
It means that Juju would behave like current main ops does: relation-ids would not return the event that's broken in the relation-broken hook. Whether or not relation data is accessible is a separate concern. |
Sorry, that was a typo (fixed now) - I meant in a |
@tonyandrewmeyer does this potentially affect the keys in wondering if we need to update this code: https://github.com/canonical/mysql-router-k8s-operator/blob/ecb17ddf94af9f8b4bba498bae19e9dab9cdfd8f/src/lifecycle.py#L57 |
from testing, looks like keys are populated even if no relations exist on endpoint |
@tonyandrewmeyer does this work for collect status ops events or deferred ops events in the relation broken juju event? from a glance at the PR, it looks like we won't be able to access the relation data for the breaking relation during other ops events in the same juju event |
Workaround for breaking change in ops 2.10: canonical/operator#1091 (comment) Workaround may break during deferred ops events or collect status ops events (canonical/operator#1091 (comment))
Sorry, ran out of time for this today, but will check tomorrow (NZ time) morning. I also need to double check we're consistent now that the Juju fix has landed, so the same behaviour happens regardless of Juju version. |
Sorry, turns out that should have been "not NZ time" 😞.
It will still be there, the
If I'm understanding that correctly, I think it would still be fine.
If the broken event is deferred then the data will be gone, since Juju doesn't know anything about the deferring. If it's some previously deferred event, or the lifecycle/ops events like collect-status, then they should all see the same behaviour, because we set the broken relation ID based on the environment and that doesn't actually change for the other events. This does indeed mean that you can't get the data in e.g. a previously deferred event, because it's missing from It's not immediately obvious to me how to fix this - the model gets reused for the deferral, and I don't think we would want to change that, and the model knows nothing about what event(s) are being handled. Could you open an issue for this? If you can describe where it's causing issues that will help prioritise fixing it and provide context (it might be that someone else picks it up rather than me, although I can try to get to it soon). I still have to try out the new Juju behaviour to make sure we're consistent there: will try to get to that today still, but it might end up being Monday (Friday is a holiday here). |
Opened issue here: #1279 mysql-router-k8s is not currently affected, since it uses no deferred events, no custom events during relation-broken, and no ops collect status events—but it's certainly possible in the future that the charm or a lib that we depend on could add a deferred event or custom event during relation-broken |
This PR allows Charmers to easily check whether a relation is broken during a
relation-broken
event (and also framework events that share the same Juju context) without having to store state or pass theevent
object around. In addition, the broken relation is excluded from theModel.relations
list, to make it easy for charms to work on only the relations that will continue to exist after the relation-broken events complete (without having to do(rel for rel in self.model.relations if rel.active)
themselves.To do this:
relation-broken
events, set a new attributeactive
on the event'sRelation
object to False (and have this be True in all other cases).relation-broken
events,RelationMapping
__getitem__
excludes the event's relation.Builds on work from @ca-scribner in #958.
Additional background notes (limited to Canonical).
Fixes #940