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

all: use net.JoinHostPort instead of fmt.Sprintf #388

Merged
merged 1 commit into from
Oct 10, 2023

Conversation

kevinburkesegment
Copy link
Contributor

@kevinburkesegment kevinburkesegment commented Sep 26, 2023

This will ensure ipv6 compatibility for provided hosts, and this method should also be faster than the general purpose fmt.Sprintf, which must first parse the input string.

Fixes #380.

Checklist

  • Tests updated
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]

@CLAassistant
Copy link

CLAassistant commented Sep 26, 2023

CLA assistant check
All committers have signed the CLA.

@kevinburkesegment
Copy link
Contributor Author

I tried to run the tests on my Mac and got the following error, which could be related to my change but I can't tell.

--- FAIL: TestMultipleClients (24.26s)
    memberlist_client_test.go:686: Waiting before start
    memberlist_client_test.go:694: Observing ring ...
    memberlist_client_test.go:708: Update 5.177208ms : Ring has 2 members, and 256 tokens, oldest timestamp: 680.249ms avg timestamp: 680.249ms youngest timestamp: 680.249ms
    memberlist_client_test.go:708: Update 135.099ms : Ring has 3 members, and 384 tokens, oldest timestamp: 810.17ms avg timestamp: 810.17ms youngest timestamp: 810.17ms
    memberlist_client_test.go:708: Update 612.187208ms : Ring has 4 members, and 512 tokens, oldest timestamp: 1.287254s avg timestamp: 1.287254s youngest timestamp: 1.287254s
    memberlist_client_test.go:708: Update 676.55475ms : Ring has 5 members, and 640 tokens, oldest timestamp: 1.351621s avg timestamp: 1.351621s youngest timestamp: 1.351621s
    memberlist_client_test.go:708: Update 676.757333ms : Ring has 6 members, and 768 tokens, oldest timestamp: 1.351824s avg timestamp: 1.351824s youngest timestamp: 1.351824s
    memberlist_client_test.go:708: Update 1.000354833s : Ring has 6 members, and 768 tokens, oldest timestamp: 1.675419s avg timestamp: 1.675419s youngest timestamp: 675.419ms
    memberlist_client_test.go:708: Update 1.025550166s : Ring has 10 members, and 1280 tokens, oldest timestamp: 2.700614s avg timestamp: 1.700614s youngest timestamp: 700.614ms
    memberlist_client_test.go:708: Update 1.025768625s : Ring has 10 members, and 1280 tokens, oldest timestamp: 2.700832s avg timestamp: 1.700832s youngest timestamp: 700.832ms
    memberlist_client_test.go:708: Update 1.220061041s : Ring has 10 members, and 1280 tokens, oldest timestamp: 1.895123s avg timestamp: 1.895123s youngest timestamp: 895.123ms
    memberlist_client_test.go:708: Update 1.417659041s : Ring has 10 members, and 1280 tokens, oldest timestamp: 2.09272s avg timestamp: 2.09272s youngest timestamp: 1.09272s
    memberlist_client_test.go:708: Update 1.417767458s : Ring has 10 members, and 1280 tokens, oldest timestamp: 2.092828s avg timestamp: 2.092828s youngest timestamp: 1.092828s
    memberlist_client_test.go:708: Update 1.498147416s : Ring has 10 members, and 1280 tokens, oldest timestamp: 2.173207s avg timestamp: 2.173207s youngest timestamp: 1.173207s
    memberlist_client_test.go:708: Update 1.49827025s : Ring has 10 members, and 1280 tokens, oldest timestamp: 2.17333s avg timestamp: 2.17333s youngest timestamp: 1.17333s
    memberlist_client_test.go:716: Ring updates observed: 13
    memberlist_client_test.go:737: KV 0: number of known members: 10
    memberlist_client_test.go:749: Member 0: oldest: 2.175085s, avg: 2.175085s, youngest: 1.175085s
    memberlist_client_test.go:757: Found tokens: 1280
    memberlist_client_test.go:737: KV 1: number of known members: 10
    memberlist_client_test.go:749: Member 1: oldest: 2.175183s, avg: 2.175183s, youngest: 1.175183s
    memberlist_client_test.go:737: KV 2: number of known members: 10
    memberlist_client_test.go:560:
        	Error Trace:	/Users/kburke/src/github.com/grafana/dskit/kv/memberlist/memberlist_client_test.go:560
        	Error:      	Received unexpected error:
        	            	Member 2: invalid state of member Member-9 in the ring: 2
        	Test:       	TestMultipleClients

@@ -107,7 +108,7 @@ func newIntegrationClientServer(
}()

httpURL := fmt.Sprintf("https://localhost:%d/hello", httpPort)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mean you could do e.g. := "https://"+net.JoinHostPort("localhost", strconv.Itoa(httpPort)) + "/hello" but it obscures the URL a bit too much for me in terms of a readability vs. fit for purpose tradeoff.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is fine as-is.

Choose a reason for hiding this comment

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

OK - I left as is so I think we are golden.

charleskorn
charleskorn previously approved these changes Sep 27, 2023
Copy link
Contributor

@charleskorn charleskorn left a comment

Choose a reason for hiding this comment

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

LGTM modulo nit

@@ -107,7 +108,7 @@ func newIntegrationClientServer(
}()

httpURL := fmt.Sprintf("https://localhost:%d/hello", httpPort)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is fine as-is.

server/server_tracing_test.go Show resolved Hide resolved
@charleskorn
Copy link
Contributor

I tried to run the tests on my Mac and got the following error, which could be related to my change but I can't tell.

I don't get the same failure when running these changes on my machine (also a Mac) - does the test consistently fail for you in the same way?

Copy link
Contributor

@charleskorn charleskorn left a comment

Choose a reason for hiding this comment

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

Could you please add a changelog entry for this change?

@charleskorn charleskorn self-requested a review September 27, 2023 05:05
@charleskorn charleskorn dismissed their stale review September 27, 2023 05:06

Pending changelog entry

@kevinburkesegment
Copy link
Contributor Author

I don't get the same failure when running these changes on my machine (also a Mac) - does the test consistently fail for you in the same way?

Yes, I'll open a new ticket.

Copy link
Contributor

@charleskorn charleskorn left a comment

Choose a reason for hiding this comment

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

Fixing what I broke yesterday

server/server_tracing_test.go Show resolved Hide resolved
server/server_tracing_test.go Show resolved Hide resolved
@kevinburkesegment
Copy link
Contributor Author

apologies - I will update shortly

@kevinburkesegment kevinburkesegment force-pushed the join-host-port branch 2 times, most recently from ee70bd0 to d3dae63 Compare October 5, 2023 22:27
@kevinburke
Copy link

OK, I've added a changelog entry and double checked the rest of the PR comments - I think this is ready for another look.

Copy link
Member

@pstibrany pstibrany left a comment

Choose a reason for hiding this comment

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

Thank you!

@pstibrany pstibrany enabled auto-merge (squash) October 6, 2023 08:56
Copy link
Contributor

@charleskorn charleskorn left a comment

Choose a reason for hiding this comment

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

One last change and then I think this is good to go 🚀

CHANGELOG.md Outdated
@@ -147,6 +147,7 @@
* [ENHANCEMENT] server: clarify documentation for `-server.grpc-max-concurrent-streams` CLI flag. #369
* [ENHANCEMENT] Ring: Add ID attribute to `InstanceDesc` for ring members. #387
* [ENHANCEMENT] httpgrpc, grpcutil: added constants and functions for adding request details into outgoing grpc metadata. #391
* [ENHANCEMENT] Use `net.JoinHostPort` instead of `fmt.Sprintf` to concatenate host and port throughout the codebase to ensure ipv6 compatibility for provided hosts. #388
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd rephrase this to focus on the outcome, not the mechanism we use to achieve it, and I think this can be classified as a bug fix rather than an enhancement (in which case, it should be moved to the end of the list):

Suggested change
* [ENHANCEMENT] Use `net.JoinHostPort` instead of `fmt.Sprintf` to concatenate host and port throughout the codebase to ensure ipv6 compatibility for provided hosts. #388
* [BUGFIX] Correctly format `host:port` addresses when using IPv6. #388

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

auto-merge was automatically disabled October 9, 2023 05:06

Head branch was pushed to by a user without write access

CHANGELOG.md Outdated
@@ -1,5 +1,6 @@
## Changelog

port throughout the codebase.
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like this line was left behind 😬

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ugh, not sure how I missed that one.

This will ensure ipv6 compatibility for provided hosts, and this
method should also be faster than the general purpose fmt.Sprintf,
which must first parse the input string.

Fixes grafana#380.
@charleskorn charleskorn merged commit 5729ba4 into grafana:main Oct 10, 2023
2 checks passed
@charleskorn
Copy link
Contributor

Thanks @kevinburkesegment!

ying-jeanne pushed a commit that referenced this pull request Nov 2, 2023
This will ensure ipv6 compatibility for provided hosts, and this
method should also be faster than the general purpose fmt.Sprintf,
which must first parse the input string.

Fixes #380.
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.

replace fmt.Sprintf("%s:%d") with net.JoinHostPort
5 participants