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

feat(loadbalance): do not cache all the keys for Consistent Hash #1370

Merged
merged 3 commits into from
Jun 7, 2024

Conversation

DMwangnima
Copy link
Contributor

@DMwangnima DMwangnima commented Jun 4, 2024

What type of PR is this?

feat

Check the PR title.

  • This PR title match the format: <type>(optional scope): <description>
  • The description of this PR title is user-oriented and clear enough for others to understand.
  • Attach the PR updating the user documentation if the current PR requires user awareness at the usage level. User docs repo

(Optional) Translate the PR title into Chinese.

feat: 对于一致性哈希,不缓存所有的键

(Optional) More detailed description for this PR(en: English/zh: Chinese).

en:
Consistent hashing caches all the Keys, which in a scenario where the Keys are very fragmented, may cause a constant increase in memory and increase the overhead of managing the cache, thus affecting performance.
zh(optional):
一致性哈希会缓存所有的 Key, 在 Key 非常分散的场景下,可能会造成内存的持续升高以及增大管理缓存的开销,进而影响性能

(Optional) Which issue(s) this PR fixes:

(optional) The PR that updates user documentation:

@DMwangnima DMwangnima requested review from a team as code owners June 4, 2024 12:41
@DMwangnima DMwangnima force-pushed the fix/remove_chash_cache branch 4 times, most recently from 3373097 to c8fc23e Compare June 4, 2024 13:36
@DMwangnima DMwangnima changed the title feat: do not cache all the keys for Consistent Hash by default feat: do not cache all the keys for Consistent Hash Jun 4, 2024
@DMwangnima DMwangnima changed the title feat: do not cache all the keys for Consistent Hash feat(loadbalance): do not cache all the keys for Consistent Hash Jun 4, 2024
@DMwangnima DMwangnima force-pushed the fix/remove_chash_cache branch 3 times, most recently from b482ee1 to dd75508 Compare June 5, 2024 06:12
YangruiEmma
YangruiEmma previously approved these changes Jun 5, 2024
@DMwangnima DMwangnima force-pushed the fix/remove_chash_cache branch 3 times, most recently from cd16086 to f3286bc Compare June 5, 2024 07:12
@DMwangnima DMwangnima changed the base branch from develop to online/chash_remove_cache June 5, 2024 07:30
@DMwangnima DMwangnima changed the base branch from online/chash_remove_cache to develop June 5, 2024 07:36
@DMwangnima
Copy link
Contributor Author

DMwangnima commented Jun 6, 2024

Key Characteristic

Random Key with length of 30.
Generate Func:

func getRandomString(length int) string {
	var resBuilder strings.Builder
	resBuilder.Grow(length)
	corpus := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
	rand.Seed(time.Now().UnixNano() + int64(100))
	for i := 0; i < length; i++ {
		resBuilder.WriteByte(corpus[rand.Intn(len(corpus))])
	}
	return resBuilder.String()
}

Benchmark Result:

  • New version (without cache)
BenchmarkConsistPicker_RandomDistributionKey
BenchmarkConsistPicker_RandomDistributionKey/10ins
BenchmarkConsistPicker_RandomDistributionKey/10ins-12         	 2417481	       508.9 ns/op	      48 B/op	       1 allocs/op
BenchmarkConsistPicker_RandomDistributionKey/100ins
BenchmarkConsistPicker_RandomDistributionKey/100ins-12        	 2140726	       534.6 ns/op	      48 B/op	       1 allocs/op
BenchmarkConsistPicker_RandomDistributionKey/1000ins
BenchmarkConsistPicker_RandomDistributionKey/1000ins-12       	 2848216	       407.7 ns/op	      48 B/op	       1 allocs/op
BenchmarkConsistPicker_RandomDistributionKey/10000ins
BenchmarkConsistPicker_RandomDistributionKey/10000ins-12      	 2701766	       492.7 ns/op	      48 B/op	       1 allocs/op
  • Old version (with cache)
BenchmarkConsistPicker_RandomDistributionKey
BenchmarkConsistPicker_RandomDistributionKey/10ins
BenchmarkConsistPicker_RandomDistributionKey/10ins-12         	  671974	      1959 ns/op	     363 B/op	       9 allocs/op
BenchmarkConsistPicker_RandomDistributionKey/100ins
BenchmarkConsistPicker_RandomDistributionKey/100ins-12        	  568735	      2182 ns/op	     379 B/op	       9 allocs/op
BenchmarkConsistPicker_RandomDistributionKey/1000ins
BenchmarkConsistPicker_RandomDistributionKey/1000ins-12       	  576940	      2358 ns/op	     486 B/op	       9 allocs/op
BenchmarkConsistPicker_RandomDistributionKey/10000ins
BenchmarkConsistPicker_RandomDistributionKey/10000ins-12      	  475820	      2702 ns/op	     515 B/op	       9 allocs/op

@ppzqh ppzqh merged commit bb385e7 into cloudwego:develop Jun 7, 2024
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants