Skip to content

Commit 40b9aa6

Browse files
authored
Merge pull request #98 from ContainerSolutions/generation-state-cleanup
removing generation state for deleted resources from memory
2 parents 2b36df1 + ac1d563 commit 40b9aa6

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

operator-framework/src/main/java/com/github/containersolutions/operator/processing/EventScheduler.java

+13-7
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,19 @@ void scheduleEventFromApi(CustomResourceEvent event) {
6464
try {
6565
lock.lock();
6666
log.debug("Scheduling event from Api: {}", event);
67-
if (event.getResource().getMetadata().getDeletionTimestamp() != null && event.getAction() == Action.DELETED) {
68-
// Note that we always use finalizers, we want to process delete event just in corner case,
69-
// when we are not able to add finalizer (lets say because of optimistic locking error, and the resource was deleted instantly).
70-
// We want to skip in case of finalizer was there since we don't want to execute delete method always at least 2x,
71-
// which would be the result if we don't skip here. (there is no deletion timestamp if resource deleted without finalizer.)
72-
log.debug("Skipping delete event since deletion timestamp is present on resource, so finalizer was in place.");
73-
return;
67+
if (event.getAction() == Action.DELETED) {
68+
// This removes data from memory for deleted resource (prevent memory leak basically).
69+
// Its quite interesting that this is always sufficient here (no finalizer or other mechanism needs to be involved).
70+
// Thus, if operator is running we get DELETE the event, if not the memory is already gone anyways.
71+
eventStore.removeLastGenerationForDeletedResource(event.resourceUid());
72+
if (event.getResource().getMetadata().getDeletionTimestamp() != null) {
73+
// Note that we always use finalizers, we want to process delete event just in corner case,
74+
// when we are not able to add finalizer (lets say because of optimistic locking error, and the resource was deleted instantly).
75+
// We want to skip in case of finalizer was there since we don't want to execute delete method always at least 2x,
76+
// which would be the result if we don't skip here. (there is no deletion timestamp if resource deleted without finalizer.)
77+
log.debug("Skipping delete event since deletion timestamp is present on resource, so finalizer was in place.");
78+
return;
79+
}
7480
}
7581
// In case of generation aware processing, we want to replace this even if generation not increased,
7682
// to have the most recent copy of the event.

operator-framework/src/main/java/com/github/containersolutions/operator/processing/EventStore.java

+4
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,8 @@ public boolean hasLargerGenerationThanLastStored(CustomResourceEvent event) {
5151
public Long getLastStoredGeneration(CustomResourceEvent event) {
5252
return lastGeneration.get(event.getResource().getMetadata().getUid());
5353
}
54+
55+
public void removeLastGenerationForDeletedResource(String uuid) {
56+
lastGeneration.remove(uuid);
57+
}
5458
}

0 commit comments

Comments
 (0)