Skip to content

Commit f2de42d

Browse files
committed
linear: one modified, respond all requested resources in streamState
1 parent 83bd3f2 commit f2de42d

File tree

2 files changed

+24
-48
lines changed

2 files changed

+24
-48
lines changed

pkg/cache/v3/linear.go

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -143,29 +143,27 @@ func (cache *LinearCache) respond(value chan Response, staleResources []string)
143143
}
144144
}
145145

146-
func (cache *LinearCache) notifyAll(modified map[string]struct{}, fromDeletion bool) {
146+
func (cache *LinearCache) notifyAll(modified map[string]struct{}) {
147147
// de-duplicate watches that need to be responded
148148
notifyList := make(map[chan Response][]string)
149-
if !fromDeletion {
150-
for name := range modified {
151-
for watch := range cache.watches[name] {
152-
notifyList[watch] = append(notifyList[watch], name)
153-
}
154-
delete(cache.watches, name)
155-
}
156-
} else {
157-
for deletedName := range modified {
158-
for watch, streamState := range cache.watches[deletedName] {
159-
resourceNames := streamState.GetKnownResourceNames(cache.typeURL)
160-
for resourceName := range resourceNames {
161-
// To avoid the stale in notifyList becomes empty slice.
162-
// Don't skip resource name that has been deleted here.
163-
// It would be filtered out in respond because the corresponding resource has been deleted.
164-
notifyList[watch] = append(notifyList[watch], resourceName)
149+
for name := range modified {
150+
for watch, streamState := range cache.watches[name] {
151+
resourceNames := streamState.GetKnownResourceNames(cache.typeURL)
152+
modifiedNameInResourceName := false
153+
for resourceName := range resourceNames {
154+
if !modifiedNameInResourceName && resourceName == name {
155+
modifiedNameInResourceName = true
165156
}
157+
// To avoid the stale in notifyList becomes empty slice.
158+
// Don't skip resource name that has been deleted here.
159+
// It would be filtered out in respond because the corresponding resource has been deleted.
160+
notifyList[watch] = append(notifyList[watch], resourceName)
161+
}
162+
if !modifiedNameInResourceName {
163+
notifyList[watch] = append(notifyList[watch], name)
166164
}
167-
delete(cache.watches, deletedName)
168165
}
166+
delete(cache.watches, name)
169167
}
170168

171169
for value, stale := range notifyList {
@@ -189,31 +187,6 @@ func (cache *LinearCache) notifyAll(modified map[string]struct{}, fromDeletion b
189187
}
190188
}
191189

192-
func (cache *LinearCache) notifyAllFromDeletion(modified map[string]struct{}) {
193-
notifyList := make(map[chan Response][]string)
194-
for deletedName := range modified {
195-
for watch, streamState := range cache.watches[deletedName] {
196-
names := streamState.GetKnownResourceNames(cache.typeURL)
197-
for name := range names {
198-
if name == deletedName {
199-
// skip the resource name has been deleted.
200-
continue
201-
}
202-
notifyList[watch] = append(notifyList[watch], name)
203-
}
204-
}
205-
delete(cache.watches, deletedName)
206-
}
207-
208-
for value, stale := range notifyList {
209-
cache.respond(value, stale)
210-
}
211-
212-
for value := range cache.watchAll {
213-
cache.respond(value, nil)
214-
}
215-
}
216-
217190
func (cache *LinearCache) respondDelta(request *DeltaRequest, value chan DeltaResponse, state stream.StreamState) *RawDeltaResponse {
218191
resp := createDeltaResponse(context.Background(), request, state, resourceContainer{
219192
resourceMap: cache.resources,
@@ -246,7 +219,7 @@ func (cache *LinearCache) UpdateResource(name string, res types.Resource) error
246219
cache.resources[name] = res
247220

248221
// TODO: batch watch closures to prevent rapid updates
249-
cache.notifyAll(map[string]struct{}{name: {}}, false)
222+
cache.notifyAll(map[string]struct{}{name: {}})
250223

251224
return nil
252225
}
@@ -261,7 +234,7 @@ func (cache *LinearCache) DeleteResource(name string) error {
261234
delete(cache.resources, name)
262235

263236
// TODO: batch watch closures to prevent rapid updates
264-
cache.notifyAll(map[string]struct{}{name: {}}, true)
237+
cache.notifyAll(map[string]struct{}{name: {}})
265238
return nil
266239
}
267240

@@ -293,7 +266,7 @@ func (cache *LinearCache) SetResources(resources map[string]types.Resource) {
293266
modified[name] = struct{}{}
294267
}
295268

296-
cache.notifyAll(modified, false)
269+
cache.notifyAll(modified)
297270
}
298271

299272
// GetResources returns current resources stored in the cache
@@ -346,7 +319,10 @@ func (cache *LinearCache) CreateWatch(request *Request, streamState stream.Strea
346319
// When a resource is removed, its version defaults 0 and it is not considered stale.
347320
if lastVersion < version || (!has && exists) {
348321
stale = true
349-
staleResources = append(staleResources, name)
322+
323+
// Here we collect all requested names.
324+
// It would be filtered out in respond if the resource name doesn't appear in cache.
325+
staleResources = request.ResourceNames
350326
}
351327
}
352328
}

pkg/cache/v3/linear_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ func TestLinearWatchTwo(t *testing.T) {
336336
mustBlock(t, w1)
337337
require.NoError(t, c.UpdateResource("a", testResource("aa")))
338338
// should only get the modified resource
339-
verifyResponse(t, w, "1", 1)
339+
verifyResponse(t, w, "1", 2)
340340
verifyResponse(t, w1, "1", 2)
341341
}
342342

0 commit comments

Comments
 (0)