Copilot commented on code in PR #960:
URL: https://github.com/apache/dubbo-go-pixiu/pull/960#discussion_r3367023540
##########
pkg/cluster/cluster.go:
##########
@@ -545,5 +575,78 @@ func (s *EndpointSnapshot) rebuildConsistentHash() {
if !ok {
return
}
- s.consistentHash =
model.ReadOnlyConsistentHash(newConsistentHash(s.consistentHashConfig,
s.healthy))
+ hash :=
model.ReadOnlyConsistentHash(newConsistentHash(s.consistentHashConfig,
s.healthy))
+ s.storeHealthyConsistentHash(hash)
+}
+
+func (s *EndpointSnapshot) reuseHealthyConsistentHashFrom(previous
*EndpointSnapshot) {
+ if !canReuseHealthyConsistentHash(previous, s) {
+ return
+ }
+ s.seedHealthyConsistentHash(previous.cachedHealthyConsistentHash())
+}
+
+func canReuseHealthyConsistentHash(previous, next *EndpointSnapshot) bool {
+ if previous == nil || next == nil {
+ return false
+ }
+ if previous.lbPolicy != next.lbPolicy {
+ return false
+ }
+ if _, ok := model.ConsistentHashInitMap[next.lbPolicy]; !ok {
+ return false
+ }
+ if !sameConsistentHashConfig(previous.consistentHashConfig,
next.consistentHashConfig) {
+ return false
+ }
+ return sameHealthyEndpointsForConsistentHash(previous, next)
+}
+
+func sameConsistentHashConfig(a, b model.ConsistentHash) bool {
+ return a.ReplicaNum == b.ReplicaNum &&
+ a.MaxVnodeNum == b.MaxVnodeNum &&
+ a.MaglevTableSize == b.MaglevTableSize
+}
+
+func sameHealthyEndpointsForConsistentHash(previous, next *EndpointSnapshot)
bool {
+ if previous == nil || next == nil {
+ return false
+ }
+ if len(previous.healthy) != len(next.healthy) {
+ return false
+ }
+ for i := range previous.healthy {
+ if !sameEndpointForConsistentHash(previous.healthy[i],
next.healthy[i]) {
+ return false
+ }
+ }
+ return true
+}
+
+func sameEndpointForConsistentHash(a, b *model.Endpoint) bool {
+ if a == nil || b == nil {
+ return a == b
+ }
+ if a.ID != b.ID {
+ return false
+ }
+ if a.Address.GetAddress() != b.Address.GetAddress() {
+ return false
+ }
+ if a.GetHost() != b.GetHost() {
+ return false
+ }
Review Comment:
sameEndpointForConsistentHash compares Address.GetAddress() and GetHost() by
calling helpers that format strings with fmt.Sprintf (see
model.SocketAddress.GetAddress and model.Endpoint.GetHost). This introduces
per-endpoint allocations during snapshot publication, partially offsetting the
intended reuse optimization. Prefer comparing the underlying
address/port/domain fields directly to avoid string formatting allocations in
this hot path.
--
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]