Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/openconfig/ygot v0.36.0
github.com/prometheus/client_golang v1.25.0
github.com/scrapli/scrapligo v1.4.4
github.com/sdcio/cache v0.0.38
github.com/sdcio/cache v0.0.39
github.com/sdcio/logger v0.0.3
github.com/sdcio/schema-server v0.0.34
github.com/sdcio/sdc-protos v0.0.56
Expand All @@ -46,8 +46,8 @@ require (
github.com/clipperhouse/uax29/v2 v2.6.0 // indirect
github.com/creack/pty v1.1.24 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dgraph-io/badger/v4 v4.9.0 // indirect
github.com/dgraph-io/ristretto/v2 v2.2.0 // indirect
github.com/dgraph-io/badger/v4 v4.9.2 // indirect
github.com/dgraph-io/ristretto/v2 v2.4.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
github.com/fatih/color v1.18.0 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgraph-io/badger/v4 v4.9.0 h1:tpqWb0NewSrCYqTvywbcXOhQdWcqephkVkbBmaaqHzc=
github.com/dgraph-io/badger/v4 v4.9.0/go.mod h1:5/MEx97uzdPUHR4KtkNt8asfI2T4JiEiQlV7kWUo8c0=
github.com/dgraph-io/ristretto/v2 v2.2.0 h1:bkY3XzJcXoMuELV8F+vS8kzNgicwQFAaGINAEJdWGOM=
github.com/dgraph-io/ristretto/v2 v2.2.0/go.mod h1:RZrm63UmcBAaYWC1DotLYBmTvgkrs0+XhBd7Npn7/zI=
github.com/dgraph-io/badger/v4 v4.9.2 h1:Wb5qw8gElqwV1a8msHTeQKova9b1V10heFKMIiPd80E=
github.com/dgraph-io/badger/v4 v4.9.2/go.mod h1:nJjaJTUOSsQEBhsq209FmwCvMJzEA3e74RjZw6V2pQI=
github.com/dgraph-io/ristretto/v2 v2.4.0 h1:I/w09yLjhdcVD2QV192UJcq8dPBaAJb9pOuMyNy0XlU=
github.com/dgraph-io/ristretto/v2 v2.4.0/go.mod h1:0KsrXtXvnv0EqnzyowllbVJB8yBonswa2lTCK2gGo9E=
github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da h1:aIftn67I1fkbMa512G+w+Pxci9hJPB8oMnkcP3iZF38=
github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
Expand Down Expand Up @@ -183,8 +183,8 @@ github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/scrapli/scrapligo v1.4.4 h1:F3BiLL1tmFvEsKraq8iOqFuRRIjPlKC2SXxnCaZlzXY=
github.com/scrapli/scrapligo v1.4.4/go.mod h1:pOWxVyPsQRrWTrkoSSDg05tjOqtWfLffAZtAsCc0w3M=
github.com/sdcio/cache v0.0.38 h1:8meykZC/DAgzKI+R3GtZvTs0/OXWRCeNN5mxVKG0Ba8=
github.com/sdcio/cache v0.0.38/go.mod h1:PHJd4pTmfFZBLdPohtXJAVBNmAKhhKdZHTi3MejGrk0=
github.com/sdcio/cache v0.0.39 h1:8Vk5pJueBrp4fK58aC+c9pkOtUcI2GNDp/9iuJA2Ko8=
github.com/sdcio/cache v0.0.39/go.mod h1:sOhPItnWwrdr3b8QwPTrXxEk3PtcnHkhL1KQ+ey3wzs=
github.com/sdcio/goyang v1.6.2-2 h1:qfeUKBmoKpiKAruuEc3+V8wgHKP/n1jRDEnTy23knV8=
github.com/sdcio/goyang v1.6.2-2/go.mod h1:5WolITjek1NF8yrNERyVZ7jqjOClJTpO8p/+OwmETM4=
github.com/sdcio/logger v0.0.3 h1:IFUbObObGry+S8lHGwOQKKRxJSuOphgRU/hxVhOdMOM=
Expand Down
3 changes: 3 additions & 0 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@ type Client interface {
InstanceIntentModify(ctx context.Context, cacheName string, intent *tree_persist.Intent) error
InstanceIntentDelete(ctx context.Context, cacheName string, intentName string, IgnoreNonExisting bool) error
InstanceIntentExists(ctx context.Context, cacheName string, intentName string) (bool, error)
// InstanceIntentGetAll streams all intents not in excludeIntentNames to intentChan, sends at most
// one error on errChan, and closes errChan before intentChan. A buffered errChan always receives
// the error; an unbuffered errChan receives it unless ctx is cancelled with no reader waiting.
InstanceIntentGetAll(ctx context.Context, cacheName string, excludeIntentNames []string, intentChan chan<- *tree_persist.Intent, errChan chan<- error)
}
65 changes: 38 additions & 27 deletions pkg/cache/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,41 +61,52 @@ func (l *LocalCache) InstanceIntentGet(ctx context.Context, cacheName string, in
}

func (l *LocalCache) InstanceIntentGetAll(ctx context.Context, cacheName string, excludeIntentNames []string, intentChanOrig chan<- *tree_persist.Intent, errChanOrig chan<- error) {
// create new channels
var streamErr error
defer func() {
finishIntentStream(ctx, streamErr, intentChanOrig, errChanOrig)
}()

// Cancel before finishIntentStream so the store stream releases before any errChanOrig wait.
streamCtx, cancel := context.WithCancel(ctx)
defer cancel()

intentChan := make(chan *types.Intent, 5)
errChan := make(chan error, 1)

go l.Cache.InstanceIntentGetAll(ctx, cacheName, excludeIntentNames, intentChan, errChan)

defer close(intentChanOrig)
defer close(errChanOrig)
go l.Cache.InstanceIntentGetAll(streamCtx, cacheName, excludeIntentNames, intentChan, errChan)

for {
select {
case <-ctx.Done(): // Or stop if context is canceled
for intent := range intentChan {
tpIntent := &tree_persist.Intent{}
if err := proto.Unmarshal(intent.Data(), tpIntent); err != nil {
streamErr = err
return
case intent, ok := <-intentChan: // retieve intent
if !ok {
return
}
// unmarshall it into a tree_persit.Intent
tpIntent := &tree_persist.Intent{}
err := proto.Unmarshal(intent.Data(), tpIntent)
if err != nil {
errChanOrig <- err
return
}
// forward to caller
intentChanOrig <- tpIntent
case err, ok := <-errChan: // Handle errors after intents
if !ok {
errChan = nil // Mark errChan as nil so select ignores it
continue
}
errChanOrig <- err
}
select {
case intentChanOrig <- tpIntent:
case <-ctx.Done():
streamErr = ctx.Err()
return
}
}
streamErr = <-errChan
}

func finishIntentStream(ctx context.Context, err error, intentChan chan<- *tree_persist.Intent, errChan chan<- error) {
defer close(intentChan)
defer close(errChan)
if err == nil {
return
}
// Buffered or waiting errChan receives err even when ctx is already done.
select {
case errChan <- err:
return
default:
}
select {
case errChan <- err:
case <-ctx.Done():
}
}

func (l *LocalCache) InstanceIntentModify(ctx context.Context, cacheName string, intent *tree_persist.Intent) error {
Expand Down
142 changes: 142 additions & 0 deletions pkg/cache/local_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package cache

import (
"context"
"errors"
"fmt"
"testing"
"time"

"github.com/sdcio/cache/pkg/config"
"github.com/sdcio/sdc-protos/tree_persist"
)

// An abandoned InstanceIntentGetAll must exit cleanly and release the store for both buffered and unbuffered errChan.
func TestLocalCache_InstanceIntentGetAll_AbandonedReleasesStore(t *testing.T) {
for _, errCap := range []int{0, 1} {
t.Run(fmt.Sprintf("errChanCap=%d", errCap), func(t *testing.T) {
ctx := context.Background()
c, err := NewLocalCache(&config.CacheConfig{Dir: t.TempDir()})
if err != nil {
t.Fatal(err)
}
const name = "ds"
if err := c.InstanceCreate(ctx, name); err != nil {
t.Fatal(err)
}
for i := range 20 {
if err := c.InstanceIntentModify(ctx, name, &tree_persist.Intent{IntentName: fmt.Sprintf("intent-%d", i)}); err != nil {
t.Fatal(err)
}
}

getCtx, cancel := context.WithCancel(ctx)
intentChan := make(chan *tree_persist.Intent)
errChan := make(chan error, errCap)
streamDone := make(chan struct{})
go func() {
defer close(streamDone)
c.InstanceIntentGetAll(getCtx, name, nil, intentChan, errChan)
}()

<-intentChan
time.Sleep(50 * time.Millisecond)
cancel()

select {
case <-streamDone:
case <-time.After(5 * time.Second):
t.Fatal("InstanceIntentGetAll blocked after ctx cancellation")
}
if errCap > 0 {
if err := <-errChan; !errors.Is(err, context.Canceled) {
t.Fatalf("got err %v, want %v", err, context.Canceled)
}
}

done := make(chan error, 1)
go func() { done <- c.InstanceDelete(ctx, name) }()
select {
case err := <-done:
if err != nil {
t.Fatal(err)
}
case <-time.After(5 * time.Second):
t.Fatal("InstanceDelete blocked: store lock still held by abandoned InstanceIntentGetAll")
}
})
}
}

func TestLocalCache_InstanceIntentGetAll_UnmarshalErrorReleasesStore(t *testing.T) {
ctx := context.Background()
raw, err := NewLocalCache(&config.CacheConfig{Dir: t.TempDir()})
if err != nil {
t.Fatal(err)
}
lc := raw.(*LocalCache)

const name = "ds"
if err := lc.InstanceCreate(ctx, name); err != nil {
t.Fatal(err)
}
if err := lc.Cache.InstanceIntentModify(ctx, name, "corrupt", []byte{0xff, 0xff, 0xff}); err != nil {
t.Fatal(err)
}
for i := range 20 {
if err := lc.InstanceIntentModify(ctx, name, &tree_persist.Intent{IntentName: fmt.Sprintf("intent-%d", i)}); err != nil {
t.Fatal(err)
}
}

// Unbuffered errChan with no reader yet: the store stream must still be released immediately.
getCtx, cancel := context.WithCancel(ctx)
defer cancel()
intentChan := make(chan *tree_persist.Intent)
errChan := make(chan error)
streamDone := make(chan struct{})
go func() {
defer close(streamDone)
lc.InstanceIntentGetAll(getCtx, name, nil, intentChan, errChan)
}()

// Drain any valid intents emitted before the corrupt one, then stop reading errChan briefly.
readLoop:
for {
select {
case _, ok := <-intentChan:
if !ok {
break readLoop
}
case <-time.After(50 * time.Millisecond):
break readLoop
}
}

// Even before errChan is read or getCtx is cancelled, InstanceDelete must not deadlock on the store.
deleted := make(chan error, 1)
go func() { deleted <- lc.InstanceDelete(ctx, name) }()
select {
case err := <-deleted:
if err != nil {
t.Fatal(err)
}
case <-time.After(5 * time.Second):
t.Fatal("InstanceDelete blocked on unmarshal error with unbuffered errChan")
}

// Reading errChan receives the unmarshal error and lets InstanceIntentGetAll finish.
select {
case err := <-errChan:
if err == nil {
t.Fatal("expected unmarshal error, got nil")
}
case <-time.After(5 * time.Second):
t.Fatal("timed out waiting for unmarshal error")
}
select {
case <-streamDone:
case <-time.After(5 * time.Second):
t.Fatal("InstanceIntentGetAll did not return after delivering unmarshal error")
}
}
Loading
Loading