@@ -255,28 +255,33 @@ public Set<R> getSecondaryResources(P primary) {
255
255
256
256
if (useSecondaryToPrimaryIndex ()) {
257
257
var primaryID = ResourceID .fromResource (primary );
258
- var complementaryIds = temporalPrimaryToSecondaryIndex .getSecondaryResources (primaryID );
258
+ // Note that the order matter is these lines. This method is not synchronized
259
+ // because of performance reasons. If it was in reverse order, it could happen
260
+ // that we did not receive yet an event in the informer so the index would not
261
+ // be updated. However, before reading it from temp IDs the event arrives and erases
262
+ // the temp index. So in case of Add not id would be found.
263
+ var temporalIds = temporalPrimaryToSecondaryIndex .getSecondaryResources (primaryID );
259
264
var resources = byIndex (PRIMARY_TO_SECONDARY_INDEX_NAME , resourceIdToString (primaryID ));
260
- log . debug ( "Resources in cache: {} kind: {}" , resources , resourceType (). getSimpleName ());
265
+
261
266
log .debug (
262
267
"Using informer primary to secondary index to find secondary resources for primary name:"
263
- + " {} namespace: {}. Found {}" ,
268
+ + " {} namespace: {}. Found number {}" ,
264
269
primary .getMetadata ().getName (),
265
270
primary .getMetadata ().getNamespace (),
266
271
resources .size ());
267
272
268
- log .debug ("Complementary ids: {}" , complementaryIds );
273
+ log .debug ("Complementary ids: {}" , temporalIds );
269
274
var res =
270
275
resources .stream ()
271
276
.map (
272
277
r -> {
273
278
var resourceId = ResourceID .fromResource (r );
274
279
Optional <R > resource = temporaryResourceCache .getResourceFromCache (resourceId );
275
- complementaryIds .remove (resourceId );
280
+ temporalIds .remove (resourceId );
276
281
return resource .orElse (r );
277
282
})
278
283
.collect (Collectors .toSet ());
279
- complementaryIds .forEach (
284
+ temporalIds .forEach (
280
285
id -> {
281
286
Optional <R > resource = get (id );
282
287
resource .ifPresentOrElse (res ::add , () -> log .warn ("Resource not found: {}" , id ));
@@ -299,15 +304,15 @@ public Set<R> getSecondaryResources(P primary) {
299
304
@ Override
300
305
public synchronized void handleRecentResourceUpdate (
301
306
ResourceID resourceID , R resource , R previousVersionOfResource ) {
302
- handleRecentCreateOrUpdate (Operation . UPDATE , resource , previousVersionOfResource );
307
+ handleRecentCreateOrUpdate (resource , previousVersionOfResource );
303
308
}
304
309
305
310
@ Override
306
311
public synchronized void handleRecentResourceCreate (ResourceID resourceID , R resource ) {
307
- handleRecentCreateOrUpdate (Operation . ADD , resource , null );
312
+ handleRecentCreateOrUpdate (resource , null );
308
313
}
309
314
310
- private void handleRecentCreateOrUpdate (Operation operation , R newResource , R oldResource ) {
315
+ private void handleRecentCreateOrUpdate (R newResource , R oldResource ) {
311
316
temporalPrimaryToSecondaryIndex .explicitAddOrUpdate (newResource );
312
317
temporaryResourceCache .putResource (
313
318
newResource ,
0 commit comments