Skip to content

auto-tune: refactor to use text/template library #58

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ryanwittrup
Copy link
Contributor

While reviewing this to understand the story better, I noticed that we could simplify creating the cnf file by using the text/template package

This feels a little more clear about what's happening compared to hacking together the strings as it was done before

The only gotcha - we need to use hyphens, "-", to clean up whitespace and make sure we don't accidentally add newlines. But the existing tests drove this out

TNZ-36064

Authored-by: Ryan Wittrup [email protected]

While reviewing this to understand the story better, I noticed that we
could simplify creating the cnf file by using the text/template package

This feels a little more clear about what's happening compared to
hacking together the strings as it was done before

The only gotcha - we need to use hyphens, "-", to clean up whitespace
and make sure we don't accidentally add newlines. But the existing tests
drove this out

[TNZ-36064](https://jira.eng.vmware.com/browse/TNZ-36064)

Authored-by: Ryan Wittrup <[email protected]>
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/format"
Copy link
Member

Choose a reason for hiding this comment

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

$ go vet ./...
vet: ./auto_tune_generator_test.go:9:2: "github.com/onsi/gomega/format" imported and not used

$ go run github.com/onsi/ginkgo/v2/ginkgo run -v
...
# github.com/cloudfoundry/generate-auto-tune-mysql_test [github.com/cloudfoundry/generate-auto-tune-mysql.test]
./auto_tune_generator_test.go:9:2: "github.com/onsi/gomega/format" imported and not used

tmpl, _ := template.New("auto_tune").Parse(`
[mysqld]
innodb_buffer_pool_size = {{.BufferPoolSize}}
{{if ne .TargetPercentageOfDisk 0.0 -}}
Copy link
Member

Choose a reason for hiding this comment

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

I understand we were programmatically comparing this before, but I would be keen to consider turning this into a boolean to reduce template logic, particularly around float comparisons.

i.e. Compute the inputs up-front and the template is just rendering logic.

For example, this go playground snippet:

https://go.dev/play/p/Blw8zMVeDj9

maxBinlogSize := min(int64(binLogSpaceLimit/3), 1024*1024*1024)
maxBinlogSize = (maxBinlogSize / binlogBlockSize) * binlogBlockSize

tmpl, _ := template.New("auto_tune").Parse(`
Copy link
Member

Choose a reason for hiding this comment

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

I would feel better if we assert the error though.

There are conditionals that are hopefully driven out in the unit tests (effectively exercising all the template paths).

Since this should only be a programmatic error, maybe https://pkg.go.dev/text/template#Must could be reasonable.

@github-project-automation github-project-automation bot moved this from Inbox to Waiting for Changes | Open for Contribution in Foundational Infrastructure Working Group Apr 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Waiting for Changes | Open for Contribution
Development

Successfully merging this pull request may close these issues.

2 participants