8000 fix: improve log on provisioner daemon started with pk by defelmnq · Pull Request #15588 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

fix: improve log on provisioner daemon started with pk #15588

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 19 commits into from
Nov 25, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
pre-allocate map
  • Loading branch information
defelmnq committed Nov 25, 2024
commit b5ff46568772651516d24fc2e465d58c42f2f15b
14 changes: 10 additions & 4 deletions enterprise/cli/provisionerdaemonstart.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,20 @@ func (r *RootCmd) provisionerDaemonStart() *serpent.Command {
return err
}

displayedTags := make(map[string]string, len(tags))
for key, val := range tags {
displayedTags[key] = val
}

if provisionerKey != "" {
pkDetails, err := client.GetProvisionerKey(ctx, provisionerKey)
if err != nil {
return xerrors.Errorf("unable to get provisioner key details: %w", err)
return xerrors.New("unable to get provisioner key details")
}

displayedTags = make(map[string]string, len(pkDetails.Tags))
Copy link
Contributor

Choose a reason for hiding this comment

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

What is your intention here? To clear out the map if the provisioner key is set?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So globally, either we have no key and some tags (line 107 to 110) - either we have no key (line 112) and want to fill the displayedTags with the ones fetched in pkDetails.

That's why at first I was using the same map without allocating it , as we can't really be sure about the size before knowing in which case we'll enter
anyway, we want the map to be declared in the whole function to be used below.

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like you should add an else block which populates displayedTags if the provision key is nil?

I don't think what you have is a big deal performance-wise since maps can never shrink, so if the make call on L107 has a length of 4 and the call on L118 has a length of 2, it'll keep the originally-allocated memory.
Preallocation is not strictly necessary but it's a good practice. In this case if there's a chance we'll allocate twice then there's not much of a saving since that's what preallocation is trying to prevent.

Copy link
Contributor Author
@defelmnq defelmnq Nov 25, 2024

Choose a reason for hiding this comment

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

tried to improve it from your feedback, was it what you had in mind ? 🙏

I basically allocate before the logic, then fill it with the tags i want based on the case - worst situation, it will reallocate with more space for the tags fetched if required.

for k, v := range pkDetails.Tags {
tags[k] = v
displayedTags[k] = v
}
}

Expand Down Expand Up @@ -142,7 +148,7 @@ func (r *RootCmd) provisionerDaemonStart() *serpent.Command {
defer closeLogger()
}

if len(tags) == 0 {
if len(displayedTags) == 0 {
logger.Info(ctx, "note: untagged provisioners can only pick up jobs from untagged templates")
}

Expand Down Expand Up @@ -213,7 +219,7 @@ func (r *RootCmd) provisionerDaemonStart() *serpent.Command {
defer closeFunc()
}

logger.Info(ctx, "starting provisioner daemon", slog.F("tags", tags), slog.F("name", name))
logger.Info(ctx, "starting provisioner daemon", slog.F("tags", displayedTags), slog.F("name", name))

connector := provisionerd.LocalProvisioners{
string(database.ProvisionerTypeTerraform): proto.NewDRPCProvisionerClient(terraformClient),
Expand Down
Loading
0