Skip to content

Commit 1ef73b0

Browse files
committed
using uid as resource identifier
1 parent 0c42263 commit 1ef73b0

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public String getEventInfo() {
3737

3838
}
3939

40-
public String resourceKey() {
41-
return resource.getKind() + "_" + resource.getMetadata().getName();
40+
public String resourceUid() {
41+
return resource.getMetadata().getUid();
4242
}
4343

4444
public Boolean sameResourceAs(CustomResourceEvent otherEvent) {

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ public class EventScheduler<R extends CustomResource> implements Watcher<R> {
5454
private final ScheduledThreadPoolExecutor executor;
5555
private final HashMap<CustomResourceEvent, BackOffExecution> backoffSchedulerCache = new HashMap<>();
5656

57-
// todo check uid for key
58-
// note that these hash maps does not needs to be concurrent, since we are already locking all methods where are used
5957
private final Map<String, CustomResourceEvent> eventsNotScheduledYet = new HashMap<>();
6058
private final Map<String, ResourceScheduleHolder> eventsScheduledForProcessing = new HashMap<>();
6159
private final Map<String, CustomResourceEvent> eventsUnderProcessing = new HashMap<>();
@@ -102,29 +100,29 @@ void scheduleEvent(CustomResourceEvent newEvent) {
102100
lock.lock();
103101

104102
// if there is an event waiting for to be scheduled we just replace that.
105-
if (eventsNotScheduledYet.containsKey(newEvent.resourceKey()) &&
106-
newEvent.isSameResourceAndNewerVersion(eventsNotScheduledYet.get(newEvent.resourceKey()))) {
103+
if (eventsNotScheduledYet.containsKey(newEvent.resourceUid()) &&
104+
newEvent.isSameResourceAndNewerVersion(eventsNotScheduledYet.get(newEvent.resourceUid()))) {
107105
log.debug("Replacing event which is not scheduled yet, since incoming event is more recent. new Event:{}"
108106
, newEvent);
109-
eventsNotScheduledYet.put(newEvent.resourceKey(), newEvent);
107+
eventsNotScheduledYet.put(newEvent.resourceUid(), newEvent);
110108
return;
111-
} else if (eventsUnderProcessing.containsKey(newEvent.resourceKey()) &&
112-
newEvent.isSameResourceAndNewerVersion(eventsUnderProcessing.get(newEvent.resourceKey()))) {
109+
} else if (eventsUnderProcessing.containsKey(newEvent.resourceUid()) &&
110+
newEvent.isSameResourceAndNewerVersion(eventsUnderProcessing.get(newEvent.resourceUid()))) {
113111
log.debug("Scheduling event for later processing since there is an event under processing for same kind." +
114112
" New event: {}", newEvent);
115-
eventsNotScheduledYet.put(newEvent.resourceKey(), newEvent);
113+
eventsNotScheduledYet.put(newEvent.resourceUid(), newEvent);
116114
return;
117115
}
118116

119-
if (eventsScheduledForProcessing.containsKey(newEvent.resourceKey())) {
120-
ResourceScheduleHolder scheduleHolder = eventsScheduledForProcessing.get(newEvent.resourceKey());
117+
if (eventsScheduledForProcessing.containsKey(newEvent.resourceUid())) {
118+
ResourceScheduleHolder scheduleHolder = eventsScheduledForProcessing.get(newEvent.resourceUid());
121119
CustomResourceEvent scheduledEvent = scheduleHolder.getCustomResourceEvent();
122120
ScheduledFuture<?> scheduledFuture = scheduleHolder.getScheduledFuture();
123121
// If newEvent is newer than existing in queue, cancel and remove queuedEvent
124122
if (newEvent.isSameResourceAndNewerVersion(scheduledEvent)) {
125123
log.debug("Queued event canceled because incoming event is newer. [{}]", scheduledEvent);
126124
scheduledFuture.cancel(false);
127-
eventsScheduledForProcessing.remove(scheduledEvent.resourceKey());
125+
eventsScheduledForProcessing.remove(scheduledEvent.resourceUid());
128126
}
129127
// If newEvent is older than existing in queue, don't schedule and remove from cache
130128
if (scheduledEvent.isSameResourceAndNewerVersion(newEvent)) {
@@ -135,7 +133,7 @@ void scheduleEvent(CustomResourceEvent newEvent) {
135133
backoffSchedulerCache.put(newEvent, backOff.start());
136134
ScheduledFuture<?> scheduledTask = executor.schedule(new EventConsumer(newEvent, eventDispatcher, this),
137135
backoffSchedulerCache.get(newEvent).nextBackOff(), TimeUnit.MILLISECONDS);
138-
eventsScheduledForProcessing.put(newEvent.resourceKey(), new ResourceScheduleHolder(newEvent, scheduledTask));
136+
eventsScheduledForProcessing.put(newEvent.resourceUid(), new ResourceScheduleHolder(newEvent, scheduledTask));
139137
} finally {
140138
lock.unlock();
141139
}
@@ -144,15 +142,15 @@ void scheduleEvent(CustomResourceEvent newEvent) {
144142
boolean eventProcessingStarted(CustomResourceEvent event) {
145143
try {
146144
lock.lock();
147-
ResourceScheduleHolder res = eventsScheduledForProcessing.remove(event.resourceKey());
145+
ResourceScheduleHolder res = eventsScheduledForProcessing.remove(event.resourceUid());
148146
if (res == null) {
149147
// if its still scheduled for processing.
150148
// note that it can happen that we scheduled an event for processing, it took some time that is was picked
151149
// by executor, and it was removed during that time from the schedule but not cancelled yet. So to be correct
152150
// this should be checked also here. In other word scheduleEvent function can run in parallel with eventDispatcher.
153151
return false;
154152
}
155-
eventsUnderProcessing.put(event.resourceKey(), event);
153+
eventsUnderProcessing.put(event.resourceUid(), event);
156154
return true;
157155
} finally {
158156
lock.unlock();
@@ -162,10 +160,10 @@ boolean eventProcessingStarted(CustomResourceEvent event) {
162160
void eventProcessingFinishedSuccessfully(CustomResourceEvent event) {
163161
try {
164162
lock.lock();
165-
eventsUnderProcessing.remove(event.resourceKey());
163+
eventsUnderProcessing.remove(event.resourceUid());
166164
backoffSchedulerCache.remove(event);
167165

168-
CustomResourceEvent notScheduledYetEvent = eventsNotScheduledYet.remove(event.resourceKey());
166+
CustomResourceEvent notScheduledYetEvent = eventsNotScheduledYet.remove(event.resourceUid());
169167
if (notScheduledYetEvent != null) {
170168
scheduleEvent(notScheduledYetEvent);
171169
}

0 commit comments

Comments
 (0)