AlexStocks commented on a change in pull request #708:
URL: https://github.com/apache/dubbo-go/pull/708#discussion_r469151351
##########
File path: cluster/router/chain/chain.go
##########
@@ -79,6 +103,99 @@ func (c *RouterChain) AddRouters(routers
[]router.PriorityRouter) {
c.routers = newRouters
}
+// SetInvokers receives updated invokers from registry center. If the times of
notification exceeds countThreshold and
+// time interval exceeds timeThreshold since last cache update, then notify to
update the cache.
+func (c *RouterChain) SetInvokers(invokers []protocol.Invoker) {
+ c.invokers = invokers
+
+ c.count++
+ now := time.Now()
+ if c.count >= countThreshold && now.Sub(c.last) >= timeThreshold {
+ c.last = now
+ c.count = 0
+ go func() {
+ c.ch <- struct{}{}
+ }()
+ }
+}
+
+// loop listens on events to update the address cache when it's necessary,
either when it receives notification
+// from address update, or when timeInterval exceeds.
+func (c *RouterChain) loop() {
+ for {
+ select {
+ case <-time.Tick(timeInterval):
+ c.buildCache()
+ case <-c.ch:
+ c.buildCache()
+ }
+ }
+}
+
+// copyRouters make a snapshot copy from RouterChain's router list.
+func (c *RouterChain) copyRouters() []router.PriorityRouter {
+ c.mutex.RLock()
+ ret := copySlice(c.routers)
+ c.mutex.RUnlock()
+ return ret.([]router.PriorityRouter)
+}
+
+// copyInvokers copies a snapshot of the received invokers.
+func (c *RouterChain) copyInvokers() []protocol.Invoker {
+ c.mutex.RLock()
+ ret := copySlice(c.invokers)
+ c.mutex.RUnlock()
+ return ret.([]protocol.Invoker)
+}
+
+func copySlice(s interface{}) interface{} {
+ t, v := reflect.TypeOf(s), reflect.ValueOf(s)
+ c := reflect.MakeSlice(t, v.Len(), v.Len())
+ reflect.Copy(c, v)
+ return c.Interface()
+}
+
+// loadCache loads cache from sync.Value to guarantee the visibility
+func (c *RouterChain) loadCache() *InvokerCache {
+ v := c.cache.Load()
+ if v == nil {
+ return nil
+ }
+
+ return v.(*InvokerCache)
+}
+
+// buildCache builds address cache with the new invokers for all poolable
routers.
+func (c *RouterChain) buildCache() {
+ if c.invokers == nil || len(c.invokers) == 0 {
+ return
+ }
+
+ invokers := c.copyInvokers()
+ cache := BuildCache(invokers)
+ origin := c.loadCache()
+
+ var mutex sync.Mutex
+ var wg sync.WaitGroup
+
+ for _, r := range c.copyRouters() {
+ if p, ok := r.(router.Poolable); ok {
+ wg.Add(1)
+ go func(p router.Poolable) {
+ pool, info := poolRouter(p, origin, invokers)
+ mutex.Lock()
Review comment:
You'd better use `defer mutex.Unlock()` to unlock safely.
##########
File path: cluster/router/healthcheck/health_check_route.go
##########
@@ -51,25 +58,48 @@ func NewHealthCheckRouter(url *common.URL)
(router.PriorityRouter, error) {
}
// Route gets a list of healthy invoker
-func (r *HealthCheckRouter) Route(invokers []protocol.Invoker, url
*common.URL, invocation protocol.Invocation) []protocol.Invoker {
+func (r *HealthCheckRouter) Route(invokers *roaring.Bitmap, cache
router.Cache, url *common.URL, invocation protocol.Invocation) *roaring.Bitmap {
if !r.enabled {
return invokers
}
- healthyInvokers := make([]protocol.Invoker, 0, len(invokers))
+
+ addrPool := cache.FindAddrPool(r)
// Add healthy invoker to the list
- for _, invoker := range invokers {
- if r.checker.IsHealthy(invoker) {
- healthyInvokers = append(healthyInvokers, invoker)
- }
- }
- // If all Invoke are considered unhealthy, downgrade to all inovker
- if len(healthyInvokers) == 0 {
+ healthyInvokers := utils.JoinIfNotEqual(addrPool[healthy], invokers)
+ // If all Invoke are considered unhealthy, downgrade to all invoker
+ if healthyInvokers.IsEmpty() {
logger.Warnf(" Now all invokers are unhealthy, so downgraded to
all! Service: [%s]", url.ServiceKey())
return invokers
}
return healthyInvokers
}
+// Pool separates healthy invokers from others.
+func (r *HealthCheckRouter) Pool(invokers []protocol.Invoker)
(router.AddrPool, router.AddrMetadata) {
+ if !r.enabled {
+ return nil, nil
+ }
+
+ rb := make(router.AddrPool)
Review comment:
pls do not use make to create a struct obj. pls init it in this way `rb
:= &router.AddrPool{}`.
##########
File path: cluster/router/router.go
##########
@@ -50,3 +55,39 @@ type PriorityRouter interface {
// 0 to ^int(0) is better
Priority() int64
}
+
+// Poolable caches address pool and address metadata for a router instance
which will be used later in Router's Route.
+type Poolable interface {
Review comment:
this interface name is not a noun. So it should be 'Pool' or
"RouterMetaPool'.
##########
File path: cluster/router/utils/bitmap_util.go
##########
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package utils
+
+import (
+ "github.com/RoaringBitmap/roaring"
+)
+
+import (
+ "github.com/apache/dubbo-go/protocol"
+)
+
+var EmptyAddr = roaring.NewBitmap()
+
+func JoinIfNotEqual(left *roaring.Bitmap, right *roaring.Bitmap)
*roaring.Bitmap {
+ if !left.Equals(right) {
+ left = left.Clone()
+ left.And(right)
+ }
+ return left
+}
+
+func FallbackIfJoinToEmpty(left *roaring.Bitmap, right *roaring.Bitmap)
*roaring.Bitmap {
+ ret := JoinIfNotEqual(left, right)
+ if ret == nil || ret.IsEmpty() {
+ return right
+ } else {
Review comment:
pls delete this else switch branch.
##########
File path: cluster/router/chain/chain.go
##########
@@ -79,6 +103,99 @@ func (c *RouterChain) AddRouters(routers
[]router.PriorityRouter) {
c.routers = newRouters
}
+// SetInvokers receives updated invokers from registry center. If the times of
notification exceeds countThreshold and
+// time interval exceeds timeThreshold since last cache update, then notify to
update the cache.
+func (c *RouterChain) SetInvokers(invokers []protocol.Invoker) {
+ c.invokers = invokers
+
+ c.count++
+ now := time.Now()
+ if c.count >= countThreshold && now.Sub(c.last) >= timeThreshold {
+ c.last = now
+ c.count = 0
+ go func() {
+ c.ch <- struct{}{}
+ }()
+ }
+}
+
+// loop listens on events to update the address cache when it's necessary,
either when it receives notification
+// from address update, or when timeInterval exceeds.
+func (c *RouterChain) loop() {
+ for {
+ select {
+ case <-time.Tick(timeInterval):
+ c.buildCache()
+ case <-c.ch:
+ c.buildCache()
+ }
+ }
+}
+
+// copyRouters make a snapshot copy from RouterChain's router list.
+func (c *RouterChain) copyRouters() []router.PriorityRouter {
+ c.mutex.RLock()
+ ret := copySlice(c.routers)
+ c.mutex.RUnlock()
+ return ret.([]router.PriorityRouter)
+}
+
+// copyInvokers copies a snapshot of the received invokers.
+func (c *RouterChain) copyInvokers() []protocol.Invoker {
+ c.mutex.RLock()
+ ret := copySlice(c.invokers)
+ c.mutex.RUnlock()
+ return ret.([]protocol.Invoker)
+}
+
+func copySlice(s interface{}) interface{} {
+ t, v := reflect.TypeOf(s), reflect.ValueOf(s)
+ c := reflect.MakeSlice(t, v.Len(), v.Len())
+ reflect.Copy(c, v)
+ return c.Interface()
+}
+
+// loadCache loads cache from sync.Value to guarantee the visibility
+func (c *RouterChain) loadCache() *InvokerCache {
+ v := c.cache.Load()
+ if v == nil {
+ return nil
+ }
+
+ return v.(*InvokerCache)
+}
+
+// buildCache builds address cache with the new invokers for all poolable
routers.
+func (c *RouterChain) buildCache() {
+ if c.invokers == nil || len(c.invokers) == 0 {
Review comment:
maybe lack of lock guard,
##########
File path: cluster/router/router.go
##########
@@ -50,3 +55,39 @@ type PriorityRouter interface {
// 0 to ^int(0) is better
Priority() int64
}
+
+// Poolable caches address pool and address metadata for a router instance
which will be used later in Router's Route.
+type Poolable interface {
+ // Pool created address pool and address metadata from the invokers.
+ Pool([]protocol.Invoker) (AddrPool, AddrMetadata)
+
+ // ShouldPool returns if it should pool. One typical scenario is a
router rule changes, in this case, a pooling
+ // is necessary, even if the addresses not changed at all.
+ ShouldPool() bool
Review comment:
maybe u should use "Enable" as this func's name. If u add this interface
or func as the Dubbo Interface, u can omit my suggestion.
##########
File path: cluster/router/chain/chain.go
##########
@@ -109,14 +226,62 @@ func NewRouterChain(url *common.URL) (*RouterChain,
error) {
chain := &RouterChain{
builtinRouters: routers,
routers: newRouters,
+ last: time.Now(),
+ ch: make(chan struct{}),
}
if url != nil {
chain.url = *url
}
+ go chain.loop()
return chain, nil
}
+// poolRouter calls poolable router's Pool() to create new address pool and
address metadata if necessary.
+// If the corresponding cache entry exists, and the poolable router answers no
need to re-pool (possibly because its
+// rule doesn't change), and the address list doesn't change, then the
existing data will be re-used.
+func poolRouter(p router.Poolable, origin *InvokerCache, invokers
[]protocol.Invoker) (router.AddrPool, router.AddrMetadata) {
+ name := p.Name()
+ if isCacheMiss(origin, name) || p.ShouldPool() ||
isInvokersChanged(origin.invokers, invokers) {
+ logger.Debugf("build address cache for router %q", name)
+ return p.Pool(invokers)
+ } else {
Review comment:
pls delete the else switch branch.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]