Skip to content

feat(coderd): insert provisioner daemons #11207

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

Merged
merged 20 commits into from
Dec 18, 2023
Merged

Conversation

johnstcn
Copy link
Member

@johnstcn johnstcn commented Dec 14, 2023

Relates to #10676

  • upsert provisioner daemons on connection
  • add heartbeats
  • update unit tests

@johnstcn johnstcn force-pushed the cj/populate-provisionerdaemons branch from ee108ad to 02b6743 Compare December 14, 2023 16:59
@johnstcn johnstcn force-pushed the cj/populate-provisionerdaemons branch from 876ee58 to b7bb176 Compare December 15, 2023 10:02
@johnstcn johnstcn marked this pull request as ready for review December 15, 2023 10:28
Copy link
Member

@mtojek mtojek left a comment

Choose a reason for hiding this comment

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

I haven't looked into provisionerdserver_test.go yet, but I can submit some feedback now.

q.provisionerDaemons[idx].LastSeenAt = arg.LastSeenAt
return nil
}
return sql.ErrNoRows
Copy link
Member

Choose a reason for hiding this comment

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

Is this really the same return signature the DB gives? I believe you have to check the affected rows of the Exec as it will not return an error, just zero affected rows.

I wonder if we rely on similar assumptions elsewhere in the code, seeing as the generated code doesn't check affected rows 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

Other dbmem queries have very similar logic; I just copied it.

Copy link
Member

Choose a reason for hiding this comment

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

Ideally we should change it so that our dbmem logic matches reality. Otherwise a developer may rely on this behavior and wonder why it doesn't work in the real world.

Copy link
Member Author

@johnstcn johnstcn Dec 18, 2023

Choose a reason for hiding this comment

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

I'll file a follow-up issue 👍
EDIT: #11263

Copy link
Member

@mtojek mtojek left a comment

Choose a reason for hiding this comment

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

Thanks for addressing my comments 👍

Comment on lines +17 to +37
// Otherwise, the "owner" tag is always an empty string.
// NOTE: "owner" must NEVER be nil. Otherwise it will end up being
// duplicated in the database, as idx_provisioner_daemons_name_owner_key
// is a partial unique index that includes a JSON field.
func MutateTags(userID uuid.UUID, tags map[string]string) map[string]string {
if tags == nil {
tags = map[string]string{}
}
_, ok := tags[TagScope]
if !ok {
tags[TagScope] = ScopeOrganization
delete(tags, TagOwner)
tags[TagOwner] = ""
}
switch tags[TagScope] {
case ScopeUser:
tags[TagOwner] = userID.String()
case ScopeOrganization:
delete(tags, TagOwner)
tags[TagOwner] = ""
default:
tags[TagScope] = ScopeOrganization
tags[TagOwner] = ""
Copy link
Member Author

Choose a reason for hiding this comment

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

review: calling this out here -- I had assumed that the index over (name, tags["owner"]) would disallow multiple rows with ("foo", NULL) but this is apparently not the case. However, it does disallow multiple rows with ("foo", "").

I updated to use MutateProvisionerTags wherever possible when inserting a provisioner daemon.

Copy link
Member Author

Choose a reason for hiding this comment

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

@mafredri helpfully pointed out that we can use COALESCE in the index to ensure that an absent value is treated the same as an empty string. 🎉 Adding a migration for this now.

api.AccessURL,
id,
daemon.ID,
Copy link
Member Author

Choose a reason for hiding this comment

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

review: another call-out: I am now setting the ID of the provisionerd server to be the same as the provisioner daemon that was inserted into the database, instead of a random UUID as it was before.

I believe this should be OK, but deferring to better judgement here.

Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

LGTM

q.provisionerDaemons[idx].LastSeenAt = arg.LastSeenAt
return nil
}
return sql.ErrNoRows
Copy link
Member

Choose a reason for hiding this comment

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

Ideally we should change it so that our dbmem logic matches reality. Otherwise a developer may rely on this behavior and wonder why it doesn't work in the real world.

USING btree (name, lower(coalesce(tags->>'owner', '')::text));

COMMENT ON INDEX idx_provisioner_daemons_name_owner_key
IS 'Allow unique provisioner daemon names by user';
Copy link
Member

Choose a reason for hiding this comment

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

This makes it much more clear what it's purpose is ❤️

@johnstcn johnstcn merged commit 213b768 into main Dec 18, 2023
@johnstcn johnstcn deleted the cj/populate-provisionerdaemons branch December 18, 2023 16:44
@github-actions github-actions bot locked and limited conversation to collaborators Dec 18, 2023
//nolint:gocritic // This is specifically for updating the last seen at timestamp.
return db.UpdateProvisionerDaemonLastSeenAt(dbauthz.AsSystemRestricted(hbCtx), database.UpdateProvisionerDaemonLastSeenAtParams{
ID: id,
LastSeenAt: sql.NullTime{Time: time.Now(), Valid: true},
Copy link
Member

Choose a reason for hiding this comment

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

Noticed this was s.timeNow(), but turned into time.Now(). I suppose that's OK? Should it be dbtime.Now(), though?

Copy link
Member Author

Choose a reason for hiding this comment

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

Should be s.timeNow, will hotfix 👍 Thanks for the catch

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants