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

Implement cascade deregistering #27

Merged
merged 1 commit into from
Nov 7, 2024

Conversation

avnik
Copy link
Contributor

@avnik avnik commented Nov 6, 2024

Description

Implement cascade service deregistering

Checklist

  • Summary of the proposed changes in the PR description
  • More detailed description in the commit message(s)
  • Commits are squashed into relevant entities - avoid a lot of minimal dev time commits in the PR
  • Contribution guidelines followed
  • Test procedure added to nixos/tests
  • Author has run nix flake check --accept-flake-config and it passes
  • All automatic Github Action checks pass - see actions
  • Author has added reviewers and removed PR draft status

Testing

cargo test for unit tests (one test added)
run-vm-test admin for integration test

Signed-off-by: Alexander V. Nikolaev <[email protected]>
@avnik avnik requested review from mbssrc and slakkala November 6, 2024 14:47
Copy link
Contributor

@slakkala slakkala left a comment

Choose a reason for hiding this comment

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

LGTM; added a comment for the hashmap handling, but there's currently not any clean solution.

}
None => error!("Problems due cascade deregistering {each} (via {name})"),
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Annoyingly there's a nightly feature providing .extract_if() that would provide exactly what we'd need here; I guess the closest would be to rebuild the hashmap:

                *state = state
                    .drain()
                    .filter_map(|(k, v)| {
                        if matches!(v.placement, Placement::Managed(ref within) if within == name) {
                            info!("Cascade deregistering {:#?}", v);
                            self.send_event(Event::UnitShutdown(v.into()));
                            None
                        } else {
                            Some((k, v))
                        }
                    })
                    .collect();

But I'm guessing the number of cascaded services should normally be rather small, so probably the current way is better. As a chaining over-user, I would write this as:

                for entry in state
                    .values()
                    .filter_map(|re| {
                        matches!(&re.placement, Placement::Managed(within) if within == name)
                            .then(|| re.name.clone())
                    })
                    .collect::<Vec<_>>()
                    .into_iter()
                    .filter_map(|re| state.remove(&re))
                {
                    info!("Cascade deregistering {entry:#?}");
                    self.send_event(Event::UnitShutdown(entry.into()))
                }

Third option; shortest, but involves cloning the whole entry:

                state.retain(|_, val| if matches!(&val.placement, Placement::Managed(within) if within == name) {
                    info!("Cascade deregistering {entry:#?}");
                    self.send_event(Event::UnitShutdown(val.clone().into()));
                    false
                } else {
                    true
                });

@mbssrc mbssrc merged commit aacb694 into tiiuae:main Nov 7, 2024
1 check passed
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.

3 participants