Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

snapshot cache: fix heartbeat panic #579

Merged
merged 3 commits into from
Aug 8, 2022
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: 5 additions & 1 deletion pkg/cache/v3/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,11 @@ func NewSnapshotCacheWithHeartbeating(ctx context.Context, ads bool, hash NodeHa
}

func (cache *snapshotCache) sendHeartbeats(ctx context.Context, node string) {
snapshot := cache.snapshots[node]
snapshot, ok := cache.snapshots[node]
if !ok {
return
}

if info, ok := cache.status[node]; ok {
info.mu.Lock()
for id, watch := range info.watches {
Expand Down
31 changes: 31 additions & 0 deletions pkg/cache/v3/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,3 +604,34 @@ func TestSnapshotSingleResourceFetch(t *testing.T) {
protocmp.Transform()),
)
}

func TestAvertPanicForWatchOnNonExistentSnapshot(t *testing.T) {
ctx := context.Background()
c := cache.NewSnapshotCacheWithHeartbeating(ctx, false, cache.IDHash{}, nil, time.Millisecond)

// Create watch.
req := &cache.Request{
Node: &core.Node{Id: "test"},
ResourceNames: []string{"rtds"},
TypeUrl: rsrc.RuntimeType,
}
ss := stream.NewStreamState(false, map[string]string{"cluster": "abcdef"})
responder := make(chan cache.Response)
c.CreateWatch(req, ss, responder)

go func() {
// Wait for at least one heartbeat to occur, then set snapshot.
time.Sleep(time.Millisecond * 5)
srs := &singleResourceSnapshot{
version: "version-one",
typeurl: rsrc.RuntimeType,
name: "one-second",
resource: durationpb.New(time.Second),
}
if err := c.SetSnapshot(ctx, "test", srs); err != nil {
t.Errorf("unexpected error setting snapshot %v", err)
}
}()

<-responder
}