-
Notifications
You must be signed in to change notification settings - Fork 503
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(lxdprofiles): add DDL and state layer for adding and retrieving LXD profiles #18140
Conversation
profiles This small patch adds the DDL (creation of the new lxd_profile table) and the state layer to add and retrieve all lxd profiles for a given machine.
domain/machine/state/state.go
Outdated
setLXDProfileStmt, err := st.Prepare(` | ||
INSERT INTO lxd_profile (*) | ||
VALUES ($lxdProfile.*) | ||
ON CONFLICT DO NOTHING`, lxdProfile{}) |
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.
I think the better approach is to ask which profiles haven't been inserted, and then insert the ones that are missing. This would work in our favor if there are profiles that have already been applied. You'd do nothing. Also, the number of inserts will be less.
Always prefer a read over a write. The fewer writes we have, the less contention we have, which means we're more likely to go brrrrrrrrrrrrr!
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.
Mostly LGTM, I have request on TU.
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.
Couple of changes please.
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.
Few more suggestion ;)
domain/machine/state/state_test.go
Outdated
c.Assert(err, jc.ErrorIsNil) | ||
profiles = append(profiles, profile) | ||
} | ||
c.Check(profiles, gc.HasLen, 2) |
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.
todo: remove hasLen check, I think it is redundant with the SameContent check (which already do it)
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.
Fixed in 82a3a30.
domain/machine/state/state_test.go
Outdated
|
||
profiles, err := s.state.LXDProfiles(context.Background(), "deadbeef") | ||
c.Assert(err, jc.ErrorIsNil) | ||
c.Check(profiles, gc.HasLen, 2) |
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.
todo: remove hasLen check, I think it is redundant with the SameContent check (which already do it)
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.
Fixed in 82a3a30.
machine_uuid TEXT NOT NULL, | ||
name TEXT NOT NULL, | ||
PRIMARY KEY (machine_uuid, name), |
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.
There is a potential pitfall here. The order in which lxd profiles are applied is important, as each successive profile over writes items defined prior profiles. The order should not change.
When a new profile is applied, it's to the end of the list. This is problematic with upgrading charms as the name of the profile includes the charm revision.
A caveat is that we always start the list of profiles for a machine with the default
profile and juju-<model>
profile. This never changes.
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.
You could cheat here and when you get the machine_lxd_profile
you sort by the magic rowid column.
But in reality, you need to add an index and just use the slice index when adding, and ordering it when it comes out.
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.
Nice! yeah we were just discussing this with @hmlanigan and I was even thinking about adding a new column just for sorting. Because the worst case scenario was to switch back to having one row per machine and profile names would be an array somehow.
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.
There is precedence of this:
juju/domain/schema/model/sql/0015-charm.sql
Line 511 in 3795cb2
array_index INT NOT NULL, |
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.
Done, see 82a3a30 @hmlanigan and @SimonRichardson.
domain/machine/state/state.go
Outdated
SELECT &lxdProfile.name | ||
FROM machine_lxd_profile | ||
WHERE machine_uuid = $machineUUID.uuid` |
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.
Ensure you order by index on the way out.
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.
Done, see 82a3a30.
return internalerrors.Errorf("checking if machine %q exists: %w", mUUID, err) | ||
} | ||
|
||
// Make sure to clean up any existing profiles on the given machine. |
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.
Before deleting the profiles, can we check that the profiles in the rows don't already match the ones that already exist. It's very common for the SetLXDProfiles to be triggered with the same content. This comes back to only do a write query if you really need to do one.
When we do wire up lxd profiles, we'll need to insert a logical row into the change stream at the end of this to fire the worker. Hence we need that select check to ensure we're not firing additional changes.
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.
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.
Keep it here for now.
domain/machine/state/state.go
Outdated
} | ||
|
||
return db.Txn(ctx, func(ctx context.Context, tx *sqlair.TX) error { | ||
queryMachineUUID := machineUUID{UUID: mUUID} |
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.
Make this at the top, and pass it to the Prepare
calls.
/merge |
This small patch adds the DDL (creation of the new lxd_profile table) and the state layer to add and retrieve all lxd profiles for a given machine.
Note: the new methods use the internal/errors new convention but I haven't updated the rest of the methods, this will be done little by little or on a following PR since the machines domain will continue to evolve.
Checklist
Integration tests, with comments saying what you're testingdoc.go added or updated in changed packagesQA steps
Only state layer changed, so unit tests must pass.
Links
Jira card: JUJU-6808