From bf702150e3289ce6b97e48e2e5a7da4cb33fb9c6 Mon Sep 17 00:00:00 2001 From: Fahad Naeem Date: Sat, 11 Nov 2023 15:23:07 -0500 Subject: [PATCH 1/3] fixed: target not in cache to stop subscription see https://github.com/openconfig/gnmic/issues/286 for details --- pkg/cache/oc_cache.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pkg/cache/oc_cache.go b/pkg/cache/oc_cache.go index a14feba3..e4145c58 100644 --- a/pkg/cache/oc_cache.go +++ b/pkg/cache/oc_cache.go @@ -184,8 +184,9 @@ func (gc *gnmiCache) subscribe(ctx context.Context, ro *ReadOpts, ch chan *Notif } func (gc *gnmiCache) handleSingleQuery(ctx context.Context, ro *ReadOpts, ch chan *Notification) { + target := ro.Target if gc.debug { - gc.logger.Printf("running single query for target %q", ro.Target) + gc.logger.Printf("running single query for target %q", target) } caches := gc.getCaches(ro.Subscription) @@ -199,6 +200,12 @@ func (gc *gnmiCache) handleSingleQuery(ctx context.Context, ro *ReadOpts, ch cha for name, c := range caches { go func(name string, c *subCache) { defer wg.Done() + if !c.c.HasTarget(target) { + if gc.debug { + gc.logger.Printf("subscription-cache %q doesn't have target: %q", name, target) + } + return + } for _, p := range ro.Paths { fp, err := path.CompletePath(p, nil) if err != nil { @@ -296,6 +303,7 @@ func (gc *gnmiCache) handleSampledQuery(ctx context.Context, ro *ReadOpts, ch ch } func (gc *gnmiCache) handleOnChangeQuery(ctx context.Context, ro *ReadOpts, ch chan *Notification) { + target := ro.Target caches := gc.getCaches(ro.Subscription) numCaches := len(caches) gc.logger.Printf("on-change query got %d caches", numCaches) @@ -306,6 +314,12 @@ func (gc *gnmiCache) handleOnChangeQuery(ctx context.Context, ro *ReadOpts, ch c for name, c := range caches { go func(name string, c *subCache) { defer wg.Done() + if !c.c.HasTarget(target) { + if gc.debug { + gc.logger.Printf("subscription-cache %q doesn't have target: %q", name, target) + } + return + } for _, p := range ro.Paths { // handle updates only if !ro.UpdatesOnly { From b6ef4e86a9d4b3c5f08a9f1e02e9e5b0dce15029 Mon Sep 17 00:00:00 2001 From: Karim Radhouani Date: Sat, 11 Nov 2023 15:21:11 -0800 Subject: [PATCH 2/3] do not create new var for target name --- pkg/cache/oc_cache.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/pkg/cache/oc_cache.go b/pkg/cache/oc_cache.go index e4145c58..ac8e7376 100644 --- a/pkg/cache/oc_cache.go +++ b/pkg/cache/oc_cache.go @@ -29,7 +29,7 @@ import ( ) const ( - loggingPrefixOC = "[cache:%s] " + loggingPrefixOC = "[cache:oc] " defaultTimeout = 10 * time.Second ) @@ -184,9 +184,8 @@ func (gc *gnmiCache) subscribe(ctx context.Context, ro *ReadOpts, ch chan *Notif } func (gc *gnmiCache) handleSingleQuery(ctx context.Context, ro *ReadOpts, ch chan *Notification) { - target := ro.Target if gc.debug { - gc.logger.Printf("running single query for target %q", target) + gc.logger.Printf("running single query for target %q", ro.Target) } caches := gc.getCaches(ro.Subscription) @@ -200,9 +199,9 @@ func (gc *gnmiCache) handleSingleQuery(ctx context.Context, ro *ReadOpts, ch cha for name, c := range caches { go func(name string, c *subCache) { defer wg.Done() - if !c.c.HasTarget(target) { + if !c.c.HasTarget(ro.Target) { if gc.debug { - gc.logger.Printf("subscription-cache %q doesn't have target: %q", name, target) + gc.logger.Printf("subscription-cache %q doesn't have target: %q", name, ro.Target) } return } @@ -303,7 +302,6 @@ func (gc *gnmiCache) handleSampledQuery(ctx context.Context, ro *ReadOpts, ch ch } func (gc *gnmiCache) handleOnChangeQuery(ctx context.Context, ro *ReadOpts, ch chan *Notification) { - target := ro.Target caches := gc.getCaches(ro.Subscription) numCaches := len(caches) gc.logger.Printf("on-change query got %d caches", numCaches) @@ -314,9 +312,9 @@ func (gc *gnmiCache) handleOnChangeQuery(ctx context.Context, ro *ReadOpts, ch c for name, c := range caches { go func(name string, c *subCache) { defer wg.Done() - if !c.c.HasTarget(target) { + if !c.c.HasTarget(ro.Target) { if gc.debug { - gc.logger.Printf("subscription-cache %q doesn't have target: %q", name, target) + gc.logger.Printf("subscription-cache %q doesn't have target: %q", name, ro.Target) } return } From c5874f85c0d280b2ce16009333b29de640d9e8cd Mon Sep 17 00:00:00 2001 From: Karim Radhouani Date: Sat, 11 Nov 2023 15:27:30 -0800 Subject: [PATCH 3/3] fix fmt error --- pkg/cache/oc_cache.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/cache/oc_cache.go b/pkg/cache/oc_cache.go index ac8e7376..3e24ea57 100644 --- a/pkg/cache/oc_cache.go +++ b/pkg/cache/oc_cache.go @@ -10,7 +10,6 @@ package cache import ( "context" - "fmt" "io" "log" "strings" @@ -73,7 +72,7 @@ func newGNMICache(cfg *Config, loggingPrefix string, opts ...Option) *gnmiCache if loggingPrefix == "" { loggingPrefix = "oc" } - gc.logger.SetPrefix(fmt.Sprintf(loggingPrefixOC, loggingPrefix)) + gc.logger.SetPrefix(loggingPrefixOC) } return gc }