Skip to content

Commit d8682cc

Browse files
committed
Simplified EventHandling
Signed-off-by: Attila Mészáros <a_meszaros@apple.com>
1 parent 9273843 commit d8682cc

5 files changed

Lines changed: 23 additions & 26 deletions

File tree

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/controller/ControllerEventSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ private void handleOnAddOrUpdate(
141141
ResourceAction action, T oldCustomResource, T newCustomResource) {
142142
var handling =
143143
temporaryResourceCache.onAddOrUpdateEvent(action, newCustomResource, oldCustomResource);
144-
if (handling == EventHandling.NEW || handling == EventHandling.INTERMEDIATE) {
144+
if (handling == EventHandling.PROPAGATE) {
145145
handleEvent(action, newCustomResource, oldCustomResource, null);
146146
} else if (log.isDebugEnabled()) {
147147
log.debug("{} event propagation for action: {}", handling, action);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ private synchronized void onAddOrUpdate(ResourceAction action, R newObject, R ol
154154

155155
var eventHandling = temporaryResourceCache.onAddOrUpdateEvent(action, newObject, oldObject);
156156

157-
if (eventHandling != EventHandling.NEW && eventHandling != EventHandling.INTERMEDIATE) {
157+
if (eventHandling != EventHandling.PROPAGATE) {
158158
log.debug(
159-
"{} event propagation", eventHandling == EventHandling.DEFER ? "Deferring" : "Skipping");
159+
"{} event propagation", eventHandling == EventHandling.IGNORE ? "Deferring" : "Skipping");
160160
} else if (eventAcceptedByFilter(action, newObject, oldObject)) {
161161
log.debug(
162162
"Propagating event for {}, resource with same version not result of a our update.",

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,8 @@ public class TemporaryResourceCache<T extends HasMetadata> {
6161
private final ManagedInformerEventSource<T, ?, ?> managedInformerEventSource;
6262

6363
public enum EventHandling {
64-
DEFER,
65-
OBSOLETE,
66-
INTERMEDIATE,
67-
NEW
64+
IGNORE,
65+
PROPAGATE
6866
}
6967

7068
public TemporaryResourceCache(
@@ -110,15 +108,15 @@ public EventHandling onAddOrUpdateEvent(
110108
private synchronized EventHandling onEvent(
111109
ResourceAction action, T resource, T prevResourceVersion, boolean unknownState) {
112110
if (!comparableResourceVersions) {
113-
return EventHandling.NEW;
111+
return EventHandling.PROPAGATE;
114112
}
115113

116114
var resourceId = ResourceID.fromResource(resource);
117115
if (log.isDebugEnabled()) {
118116
log.debug("Processing event");
119117
}
120118
var cached = cache.get(resourceId);
121-
EventHandling result = EventHandling.NEW;
119+
EventHandling result = EventHandling.PROPAGATE;
122120
if (cached != null) {
123121
int comp = ReconcilerUtilsInternal.compareResourceVersions(resource, cached);
124122
if (comp >= 0 || unknownState) {
@@ -130,21 +128,20 @@ private synchronized EventHandling onEvent(
130128
// we propagate event only for our update or newer other can be discarded since we know we
131129
// will receive
132130
// additional event
133-
result = comp == 0 ? EventHandling.OBSOLETE : EventHandling.NEW;
131+
result = comp == 0 ? EventHandling.IGNORE : EventHandling.PROPAGATE;
134132
} else {
135133
// in this case we received and event that might be in some edge case that was
136134
// already used in reconciler or after that, but before our updated resource version.
137135
// That would be hard to distinguish, so for those we are propagating the event further.
138136
log.debug("Received intermediate event.");
139-
result = EventHandling.INTERMEDIATE;
140137
}
141138
}
142139
var au = activeUpdates.get(resourceId);
143140
if (au != null) {
144141
log.debug("Recording relevant event");
145142
au.addRelatedEvent(
146143
new GenericResourceEvent(action, resource, prevResourceVersion, unknownState));
147-
return EventHandling.DEFER;
144+
return EventHandling.IGNORE;
148145
} else {
149146
return result;
150147
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ void skipsEventPropagation() {
118118
.thenReturn(Optional.of(testDeployment()));
119119

120120
when(temporaryResourceCache.onAddOrUpdateEvent(any(), any(), any()))
121-
.thenReturn(EventHandling.OBSOLETE);
121+
.thenReturn(EventHandling.IGNORE);
122122

123123
informerEventSource.onAdd(testDeployment());
124124
informerEventSource.onUpdate(testDeployment(), testDeployment());
@@ -129,7 +129,7 @@ void skipsEventPropagation() {
129129
@Test
130130
void processEventPropagationWithoutAnnotation() {
131131
when(temporaryResourceCache.onAddOrUpdateEvent(any(), any(), any()))
132-
.thenReturn(EventHandling.NEW);
132+
.thenReturn(EventHandling.PROPAGATE);
133133
informerEventSource.onUpdate(testDeployment(), testDeployment());
134134

135135
verify(eventHandlerMock, times(1)).handleEvent(any());
@@ -138,7 +138,7 @@ void processEventPropagationWithoutAnnotation() {
138138
@Test
139139
void processEventPropagationWithIncorrectAnnotation() {
140140
when(temporaryResourceCache.onAddOrUpdateEvent(any(), any(), any()))
141-
.thenReturn(EventHandling.NEW);
141+
.thenReturn(EventHandling.PROPAGATE);
142142
informerEventSource.onAdd(
143143
new DeploymentBuilder(testDeployment())
144144
.editMetadata()
@@ -152,7 +152,7 @@ void processEventPropagationWithIncorrectAnnotation() {
152152
@Test
153153
void propagatesIntermediateEventHandling() {
154154
when(temporaryResourceCache.onAddOrUpdateEvent(any(), any(), any()))
155-
.thenReturn(EventHandling.INTERMEDIATE);
155+
.thenReturn(EventHandling.PROPAGATE);
156156
informerEventSource.onUpdate(testDeployment(), testDeployment());
157157

158158
verify(eventHandlerMock, times(1)).handleEvent(any());

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,14 @@ void putBeforeEvent() {
215215
// first ensure an event is not known
216216
var result =
217217
temporaryResourceCache.onAddOrUpdateEvent(ResourceAction.ADDED, testResource, null);
218-
assertThat(result).isEqualTo(EventHandling.NEW);
218+
assertThat(result).isEqualTo(EventHandling.PROPAGATE);
219219

220220
var nextResource = testResource();
221221
nextResource.getMetadata().setResourceVersion("3");
222222
temporaryResourceCache.putResource(nextResource);
223223

224224
result = temporaryResourceCache.onAddOrUpdateEvent(ResourceAction.UPDATED, nextResource, null);
225-
assertThat(result).isEqualTo(EventHandling.OBSOLETE);
225+
assertThat(result).isEqualTo(EventHandling.IGNORE);
226226
}
227227

228228
@Test
@@ -232,7 +232,7 @@ void putBeforeEventWithEventFiltering() {
232232
// first ensure an event is not known
233233
var result =
234234
temporaryResourceCache.onAddOrUpdateEvent(ResourceAction.ADDED, testResource, null);
235-
assertThat(result).isEqualTo(EventHandling.NEW);
235+
assertThat(result).isEqualTo(EventHandling.PROPAGATE);
236236
latestSyncVersion = RESOURCE_VERSION;
237237

238238
var nextResource = testResource();
@@ -245,7 +245,7 @@ void putBeforeEventWithEventFiltering() {
245245

246246
latestSyncVersion = "3";
247247
result = temporaryResourceCache.onAddOrUpdateEvent(ResourceAction.UPDATED, nextResource, null);
248-
assertThat(result).isEqualTo(EventHandling.OBSOLETE);
248+
assertThat(result).isEqualTo(EventHandling.IGNORE);
249249
}
250250

251251
@Test
@@ -255,7 +255,7 @@ void putAfterEventWithEventFilteringNoPost() {
255255
// first ensure an event is not known
256256
var result =
257257
temporaryResourceCache.onAddOrUpdateEvent(ResourceAction.ADDED, testResource, null);
258-
assertThat(result).isEqualTo(EventHandling.NEW);
258+
assertThat(result).isEqualTo(EventHandling.PROPAGATE);
259259

260260
var nextResource = testResource();
261261
nextResource.getMetadata().setResourceVersion("3");
@@ -266,7 +266,7 @@ void putAfterEventWithEventFilteringNoPost() {
266266
temporaryResourceCache.onAddOrUpdateEvent(
267267
ResourceAction.UPDATED, nextResource, testResource);
268268
// the result is deferred
269-
assertThat(result).isEqualTo(EventHandling.DEFER);
269+
assertThat(result).isEqualTo(EventHandling.IGNORE);
270270
temporaryResourceCache.putResource(nextResource);
271271
var postEvent = temporaryResourceCache.doneEventFilterModify(resourceId);
272272

@@ -287,7 +287,7 @@ void putAfterEventWithEventFilteringWithPost() {
287287
nextResource.getMetadata().setResourceVersion("4");
288288
var result =
289289
temporaryResourceCache.onAddOrUpdateEvent(ResourceAction.ADDED, nextResource, null);
290-
assertThat(result).isEqualTo(EventHandling.DEFER);
290+
assertThat(result).isEqualTo(EventHandling.IGNORE);
291291

292292
var postEvent = temporaryResourceCache.doneEventFilterModify(resourceId);
293293

@@ -310,7 +310,7 @@ void intermediateEventPropagatedWhenNoActiveUpdate() {
310310
var result =
311311
temporaryResourceCache.onAddOrUpdateEvent(ResourceAction.UPDATED, olderEvent, null);
312312

313-
assertThat(result).isEqualTo(EventHandling.INTERMEDIATE);
313+
assertThat(result).isEqualTo(EventHandling.PROPAGATE);
314314
}
315315

316316
@Test
@@ -329,7 +329,7 @@ void intermediateEventRecorded() {
329329

330330
var result = temporaryResourceCache.onAddOrUpdateEvent(ResourceAction.UPDATED, external, null);
331331

332-
assertThat(result).isEqualTo(EventHandling.DEFER);
332+
assertThat(result).isEqualTo(EventHandling.IGNORE);
333333
}
334334

335335
@Test
@@ -353,7 +353,7 @@ void intermediateEventDeferredWhenItIsOurOwnIntermediateUpdate() {
353353

354354
var result = temporaryResourceCache.onAddOrUpdateEvent(ResourceAction.UPDATED, ourFirst, null);
355355

356-
assertThat(result).isEqualTo(EventHandling.DEFER);
356+
assertThat(result).isEqualTo(EventHandling.IGNORE);
357357
}
358358

359359
@Test

0 commit comments

Comments
 (0)