ywxzm03 opened a new pull request, #960:
URL: https://github.com/apache/dubbo-go-pixiu/pull/960
**What this PR does**:
This PR optimizes consistent hash reuse in `EndpointSnapshot`.
For consistent-hash load balancers such as RingHashing and MaglevHashing,
when a new endpoint snapshot is published, if the new snapshot has the same
routing-relevant healthy endpoint set as the previous snapshot and the
consistent hash configuration is unchanged, the new snapshot reuses the
previously built `consistentHash`. This avoids unnecessary CPU work and memory
allocations caused by rebuilding the hash structure.
Main changes:
- Add safe cached consistent hash reuse logic to `EndpointSnapshot`.
- Reuse the previous hash only when the following fields are equivalent:
- load balancer policy
- consistent hash config
- healthy endpoint ID
- healthy endpoint address / host
- endpoint metadata
- Forbid reuse when endpoints are added or removed, endpoint addresses
change, the healthy endpoint set changes, the hash config changes, or metadata
changes.
- Add unit tests for reusable and non-reusable cases:
- Reuse is allowed when a health update does not change the healthy
endpoint set.
- Reuse is forbidden when a health update changes the healthy endpoint set.
- Reuse is forbidden when an endpoint address changes.
- Reuse is forbidden when an endpoint is added or deleted.
- Reuse is forbidden when the consistent hash config changes.
- Reuse is forbidden when endpoint metadata changes.
- Add benchmark coverage for snapshot refresh cost with unchanged healthy
sets for RingHashing and MaglevHashing.
**Which issue(s) this PR fixes**:
Fixes #942
**Special notes for your reviewer**:
This PR does not make any out-of-scope changes and keeps the following
constraints:
- Do not change consistent hash algorithms.
- Do not change endpoint identity generation.
- Do not reuse hashes based on slice pointer equality alone.
- Do not weaken health filtering.
- Keep lazy initialization behavior intact.
Verified with:
```bash
go test ./pkg/cluster/... ./pkg/server/... ./pkg/cluster/loadbalancer/...
go test -run '^$' -bench
'^BenchmarkClusterConsistentHashSnapshotRefreshUnchangedHealthySet$' -benchmem
./pkg/server
```
Output:
Unit tests:
```text
ok github.com/apache/dubbo-go-pixiu/pkg/cluster (cached)
ok github.com/apache/dubbo-go-pixiu/pkg/cluster/healthcheck
(cached)
ok github.com/apache/dubbo-go-pixiu/pkg/cluster/loadbalancer
(cached)
?
github.com/apache/dubbo-go-pixiu/pkg/cluster/loadbalancer/internal/lbtest
[no test files]
ok github.com/apache/dubbo-go-pixiu/pkg/cluster/loadbalancer/maglev
(cached)
ok github.com/apache/dubbo-go-pixiu/pkg/cluster/loadbalancer/rand
(cached)
ok github.com/apache/dubbo-go-pixiu/pkg/cluster/loadbalancer/ringhash
(cached)
ok github.com/apache/dubbo-go-pixiu/pkg/cluster/loadbalancer/roundrobin
(cached)
ok
github.com/apache/dubbo-go-pixiu/pkg/cluster/loadbalancer/weightrandom (cached)
ok github.com/apache/dubbo-go-pixiu/pkg/cluster/retry (cached)
ok github.com/apache/dubbo-go-pixiu/pkg/cluster/retry/countbased
(cached)
ok
github.com/apache/dubbo-go-pixiu/pkg/cluster/retry/exponentialbackoff (cached)
ok github.com/apache/dubbo-go-pixiu/pkg/cluster/retry/noretry
(cached)
ok github.com/apache/dubbo-go-pixiu/pkg/server (cached)
? github.com/apache/dubbo-go-pixiu/pkg/server/controls [no test
files]
? github.com/apache/dubbo-go-pixiu/pkg/server/controls/mocks [no
test files]
```
Benchmark:
```text
goos: darwin
goarch: arm64
pkg: github.com/apache/dubbo-go-pixiu/pkg/server
cpu: Apple M5
BenchmarkClusterConsistentHashSnapshotRefreshUnchangedHealthySet/RingHashing/endpoints=1/reuse-cached-previous-10
1607060 746.3 ns/op 1976 B/op 30
allocs/op
BenchmarkClusterConsistentHashSnapshotRefreshUnchangedHealthySet/RingHashing/endpoints=1/rebuild-uncached-previous-10
45808 26038 ns/op 20115 B/op 218
allocs/op
BenchmarkClusterConsistentHashSnapshotRefreshUnchangedHealthySet/RingHashing/endpoints=32/reuse-cached-previous-10
86304 13888 ns/op 23530 B/op 539
allocs/op
BenchmarkClusterConsistentHashSnapshotRefreshUnchangedHealthySet/RingHashing/endpoints=32/rebuild-uncached-previous-10
387 3078728 ns/op 380584 B/op 5749
allocs/op
BenchmarkClusterConsistentHashSnapshotRefreshUnchangedHealthySet/RingHashing/endpoints=256/reuse-cached-previous-10
10000 105583 ns/op 179754 B/op 4123
allocs/op
BenchmarkClusterConsistentHashSnapshotRefreshUnchangedHealthySet/RingHashing/endpoints=256/rebuild-uncached-previous-10
20 54802988 ns/op 1528823 B/op 45423
allocs/op
BenchmarkClusterConsistentHashSnapshotRefreshUnchangedHealthySet/RingHashing/endpoints=1024/reuse-cached-previous-10
2889 420657 ns/op 726992 B/op 16423
allocs/op
BenchmarkClusterConsistentHashSnapshotRefreshUnchangedHealthySet/RingHashing/endpoints=1024/rebuild-uncached-previous-10
5 242039292 ns/op 4597780 B/op 181381
allocs/op
BenchmarkClusterConsistentHashSnapshotRefreshUnchangedHealthySet/MaglevHashing/endpoints=1/reuse-cached-previous-10
1605380 749.8 ns/op 1976 B/op 30
allocs/op
BenchmarkClusterConsistentHashSnapshotRefreshUnchangedHealthySet/MaglevHashing/endpoints=1/rebuild-uncached-previous-10
377761 3165 ns/op 9100 B/op 41
allocs/op
BenchmarkClusterConsistentHashSnapshotRefreshUnchangedHealthySet/MaglevHashing/endpoints=32/reuse-cached-previous-10
86888 13777 ns/op 23529 B/op 539
allocs/op
BenchmarkClusterConsistentHashSnapshotRefreshUnchangedHealthySet/MaglevHashing/endpoints=32/rebuild-uncached-previous-10
4636 253168 ns/op 766625 B/op 708
allocs/op
BenchmarkClusterConsistentHashSnapshotRefreshUnchangedHealthySet/MaglevHashing/endpoints=256/reuse-cached-previous-10
13172 88850 ns/op 179687 B/op 4123
allocs/op
BenchmarkClusterConsistentHashSnapshotRefreshUnchangedHealthySet/MaglevHashing/endpoints=256/rebuild-uncached-previous-10
164 7211631 ns/op 42814169 B/op 5413
allocs/op
BenchmarkClusterConsistentHashSnapshotRefreshUnchangedHealthySet/MaglevHashing/endpoints=1024/reuse-cached-previous-10
2998 420360 ns/op 726738 B/op 16423
allocs/op
BenchmarkClusterConsistentHashSnapshotRefreshUnchangedHealthySet/MaglevHashing/endpoints=1024/rebuild-uncached-previous-10
7 161302827 ns/op 514595870 B/op 21554
allocs/op
PASS
ok github.com/apache/dubbo-go-pixiu/pkg/server 26.679s
```
```release-note
NONE
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]