Skip to content

Commit 0c686c7

Browse files
committed
consolidating the temporary cache updating logic
1 parent 002874e commit 0c686c7

File tree

4 files changed

+28
-43
lines changed

4 files changed

+28
-43
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/informer/InformerEventSource.java

+2-16
Original file line numberDiff line numberDiff line change
@@ -264,22 +264,8 @@ public synchronized void handleRecentResourceCreate(ResourceID resourceID, R res
264264

265265
private void handleRecentCreateOrUpdate(Operation operation, R newResource, R oldResource) {
266266
primaryToSecondaryIndex.onAddOrUpdate(newResource);
267-
ResourceID resourceID = ResourceID.fromResource(newResource);
268-
R cachedResource = get(resourceID).orElse(null);
269-
if ((oldResource == null && cachedResource == null)
270-
|| (cachedResource != null && oldResource != null
271-
&& cachedResource.getMetadata().getResourceVersion()
272-
.equals(oldResource.getMetadata().getResourceVersion()))) {
273-
log.debug(
274-
"Temporarily moving ahead to target version {} for resource id: {}",
275-
newResource.getMetadata().getResourceVersion(), resourceID);
276-
temporaryResourceCache.unconditionallyCacheResource(newResource);
277-
} else if (temporaryResourceCache.removeResourceFromCache(newResource).isPresent()) {
278-
// if the resource is not added to the temp cache, it is cleared, since
279-
// the cache is cleared by subsequent events after updates, but if those did not receive
280-
// the temp cache is still filled at this point with an old resource
281-
log.debug("Cleaning temporary cache for resource id: {}", resourceID);
282-
}
267+
temporaryResourceCache.putResource(newResource, Optional.ofNullable(oldResource)
268+
.map(r -> r.getMetadata().getResourceVersion()).orElse(null));
283269
}
284270

285271
private boolean useSecondaryToPrimaryIndex() {

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/informer/ManagedInformerEventSource.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void stop() {
9191
@Override
9292
public void handleRecentResourceUpdate(ResourceID resourceID, R resource,
9393
R previousVersionOfResource) {
94-
temporaryResourceCache.putUpdatedResource(resource,
94+
temporaryResourceCache.putResource(resource,
9595
previousVersionOfResource.getMetadata().getResourceVersion());
9696
}
9797

@@ -128,8 +128,10 @@ void setTemporalResourceCache(TemporaryResourceCache<R> temporaryResourceCache)
128128
this.temporaryResourceCache = temporaryResourceCache;
129129
}
130130

131+
@Override
131132
public abstract void addIndexers(Map<String, Function<R, List<String>>> indexers);
132133

134+
@Override
133135
public List<R> byIndex(String indexName, String indexKey) {
134136
return manager().byIndex(indexName, indexKey);
135137
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/informer/TemporaryResourceCache.java

+21-24
Original file line numberDiff line numberDiff line change
@@ -45,36 +45,33 @@ public synchronized Optional<T> removeResourceFromCache(T resource) {
4545
return Optional.ofNullable(cache.remove(ResourceID.fromResource(resource)));
4646
}
4747

48-
public synchronized void unconditionallyCacheResource(T newResource) {
49-
putToCache(newResource, null);
50-
}
51-
5248
public synchronized void putAddedResource(T newResource) {
53-
ResourceID resourceID = ResourceID.fromResource(newResource);
54-
if (managedInformerEventSource.get(resourceID).isEmpty()) {
55-
log.debug("Putting resource to cache with ID: {}", resourceID);
56-
putToCache(newResource, resourceID);
57-
} else {
58-
log.debug("Won't put resource into cache found already informer cache: {}", resourceID);
59-
}
49+
putResource(newResource, null);
6050
}
6151

62-
public synchronized void putUpdatedResource(T newResource, String previousResourceVersion) {
52+
/**
53+
* put the item into the cache if the previousResourceVersion matches the current state. If not
54+
* the currently cached item is removed.
55+
*
56+
* @param previousResourceVersion null indicates an add
57+
*/
58+
public synchronized void putResource(T newResource, String previousResourceVersion) {
6359
var resourceId = ResourceID.fromResource(newResource);
64-
var informerCacheResource = managedInformerEventSource.get(resourceId);
65-
if (informerCacheResource.isEmpty()) {
66-
log.debug("No cached value present for resource: {}", newResource);
67-
return;
68-
}
69-
// if this is not true that means the cache was already updated
70-
if (informerCacheResource.get().getMetadata().getResourceVersion()
71-
.equals(previousResourceVersion)) {
72-
log.debug("Putting resource to temporal cache with id: {}", resourceId);
60+
var cachedResource = getResourceFromCache(resourceId)
61+
.orElse(managedInformerEventSource.get(resourceId).orElse(null));
62+
63+
if ((previousResourceVersion == null && cachedResource == null)
64+
|| (cachedResource != null && previousResourceVersion != null
65+
&& cachedResource.getMetadata().getResourceVersion()
66+
.equals(previousResourceVersion))) {
67+
log.debug(
68+
"Temporarily moving ahead to target version {} for resource id: {}",
69+
newResource.getMetadata().getResourceVersion(), resourceId);
7370
putToCache(newResource, resourceId);
7471
} else {
75-
// if something is in cache it's surely obsolete now
76-
log.debug("Trying to remove an obsolete resource from cache for id: {}", resourceId);
77-
cache.remove(resourceId);
72+
if (cache.remove(resourceId) != null) {
73+
log.debug("Removed an obsolete resource from cache for id: {}", resourceId);
74+
}
7875
}
7976
}
8077

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/event/source/informer/TemporaryResourceCacheTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void updateAddsTheResourceIntoCacheIfTheInformerHasThePreviousResourceVersion()
3030
prevTestResource.getMetadata().setResourceVersion("0");
3131
when(informerEventSource.get(any())).thenReturn(Optional.of(prevTestResource));
3232

33-
temporaryResourceCache.putUpdatedResource(testResource, "0");
33+
temporaryResourceCache.putResource(testResource, "0");
3434

3535
var cached = temporaryResourceCache.getResourceFromCache(ResourceID.fromResource(testResource));
3636
assertThat(cached).isPresent();
@@ -43,7 +43,7 @@ void updateNotAddsTheResourceIntoCacheIfTheInformerHasOtherVersion() {
4343
informerCachedResource.getMetadata().setResourceVersion("x");
4444
when(informerEventSource.get(any())).thenReturn(Optional.of(informerCachedResource));
4545

46-
temporaryResourceCache.putUpdatedResource(testResource, "0");
46+
temporaryResourceCache.putResource(testResource, "0");
4747

4848
var cached = temporaryResourceCache.getResourceFromCache(ResourceID.fromResource(testResource));
4949
assertThat(cached).isNotPresent();

0 commit comments

Comments
 (0)