Skip to content

Commit 4cf9991

Browse files
author
attila.meszaros
committed
- remove generation awareness
- tests execute - tests, comments
1 parent 7fa7fe7 commit 4cf9991

File tree

4 files changed

+34
-19
lines changed

4 files changed

+34
-19
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,19 @@ void scheduleEventFromApi(CustomResourceEvent event) {
6565
lock.lock();
6666
log.debug("Scheduling event from Api: {}", event);
6767
if (event.getAction() == Action.DELETED) {
68-
// This removes data from memory for deleted resource (prevent memory leak).
69-
eventStore.cleanup(event.resourceUid());
7068
log.debug("Skipping delete event");
7169
return;
7270
}
7371
if (eventStore.containsNotScheduledEvent(event.resourceUid())) {
7472
log.debug("Replacing not scheduled event with actual event." +
7573
" New event: {}", event);
76-
eventStore.addOrReplaceEventAsNotScheduledAndUpdateLastGeneration(event);
74+
eventStore.addOrReplaceEventAsNotScheduled(event);
7775
return;
7876
}
7977
if (eventStore.containsEventUnderProcessing(event.resourceUid())) {
8078
log.debug("Scheduling event for later processing since there is an event under processing for same kind." +
8179
" New event: {}", event);
82-
eventStore.addOrReplaceEventAsNotScheduledAndUpdateLastGeneration(event);
80+
eventStore.addOrReplaceEventAsNotScheduled(event);
8381
return;
8482
}
8583
scheduleEventForExecution(event);

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ public class EventStore {
77

88
private final Map<String, CustomResourceEvent> eventsNotScheduled = new HashMap<>();
99
private final Map<String, CustomResourceEvent> eventsUnderProcessing = new HashMap<>();
10-
private final Map<String, Long> lastGeneration = new HashMap<>();
1110

1211
public boolean containsNotScheduledEvent(String uuid) {
1312
return eventsNotScheduled.containsKey(uuid);
@@ -17,9 +16,8 @@ public CustomResourceEvent removeEventNotScheduled(String uid) {
1716
return eventsNotScheduled.remove(uid);
1817
}
1918

20-
public void addOrReplaceEventAsNotScheduledAndUpdateLastGeneration(CustomResourceEvent event) {
19+
public void addOrReplaceEventAsNotScheduled(CustomResourceEvent event) {
2120
eventsNotScheduled.put(event.resourceUid(), event);
22-
2321
}
2422

2523
public boolean containsEventUnderProcessing(String uuid) {
@@ -33,7 +31,4 @@ public CustomResourceEvent removeEventUnderProcessing(String uid) {
3331
return eventsUnderProcessing.remove(uid);
3432
}
3533

36-
public void cleanup(String uuid) {
37-
lastGeneration.remove(uuid);
38-
}
3934
}

operator-framework/src/test/java/com/github/containersolutions/operator/EventSchedulerTest.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ public void eventsAreNotExecutedConcurrentlyForSameResource() throws Interrupted
6262
CustomResource resource1 = sampleResource();
6363
CustomResource resource2 = sampleResource();
6464
resource2.getMetadata().setResourceVersion("2");
65-
resource2.getMetadata().setGeneration(2l);
6665

6766
eventScheduler.eventReceived(Watcher.Action.MODIFIED, resource1);
6867
eventScheduler.eventReceived(Watcher.Action.MODIFIED, resource2);
@@ -78,7 +77,6 @@ public void eventsAreNotExecutedConcurrentlyForSameResource() throws Interrupted
7877

7978
@Test
8079
public void processesAllEventsRegardlessOfGeneration() {
81-
generationUnAwareScheduler();
8280
normalDispatcherExecution();
8381
CustomResource resource1 = sampleResource();
8482
CustomResource resource2 = sampleResource();
@@ -104,10 +102,8 @@ public void onlyLastEventIsScheduledIfMoreReceivedDuringAndExecution() {
104102
CustomResource resource1 = sampleResource();
105103
CustomResource resource2 = sampleResource();
106104
resource2.getMetadata().setResourceVersion("2");
107-
resource2.getMetadata().setGeneration(2l);
108105
CustomResource resource3 = sampleResource();
109106
resource3.getMetadata().setResourceVersion("3");
110-
resource3.getMetadata().setGeneration(3l);
111107

112108
eventScheduler.eventReceived(Watcher.Action.MODIFIED, resource1);
113109
eventScheduler.eventReceived(Watcher.Action.MODIFIED, resource2);
@@ -147,7 +143,6 @@ public void processesNewEventIfItIsReceivedAfterExecutionInError() {
147143
CustomResource resource1 = sampleResource();
148144
CustomResource resource2 = sampleResource();
149145
resource2.getMetadata().setResourceVersion("2");
150-
resource2.getMetadata().setGeneration(2l);
151146

152147
doAnswer(this::exceptionInExecution).when(eventDispatcher).handleEvent(ArgumentMatchers.argThat(new ArgumentMatcher<CustomResourceEvent>() {
153148
@Override
@@ -210,9 +205,9 @@ private Object normalExecution(InvocationOnMock invocation) {
210205
}
211206
}
212207

213-
private void generationUnAwareScheduler() {
214-
eventScheduler = initScheduler();
215-
}
208+
209+
210+
216211

217212
private EventScheduler initScheduler() {
218213
return new EventScheduler(eventDispatcher, new GenericRetry().setMaxAttempts(MAX_RETRY_ATTEMPTS).withLinearRetry(), finalizer);

operator-framework/src/test/java/com/github/containersolutions/operator/SubResourceUpdateIT.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void updatesSubResourceStatus() {
4646

4747
/**
4848
* Note that we check on controller impl if there is finalizer on execution.
49-
* */
49+
*/
5050
@Test
5151
public void ifNoFinalizerPresentFirstAddsTheFinalizerThenExecutesControllerAgain() {
5252
initAndCleanup();
@@ -63,10 +63,37 @@ public void ifNoFinalizerPresentFirstAddsTheFinalizerThenExecutesControllerAgain
6363
});
6464
}
6565

66+
/**
67+
* Not that here status sub-resource update will fail on optimistic locking. This solves a tricky situation:
68+
* If this would not happen (no optimistic locking on status sub-resource) we could receive and store an event
69+
* while processing the controller method. But this event would always fail since its resource version is outdated
70+
* already.
71+
* */
72+
@Test
73+
public void updateCustomResourceAfterSubResourceChange() {
74+
initAndCleanup();
75+
integrationTestSupport.teardownIfSuccess(() -> {
76+
SubResourceTestCustomResource resource = createTestCustomResource("1");
77+
integrationTestSupport.getCrOperations().inNamespace(TEST_NAMESPACE).create(resource);
78+
79+
resource.getSpec().setValue("new value");
80+
integrationTestSupport.getCrOperations().inNamespace(TEST_NAMESPACE).createOrReplace(resource);
81+
82+
awaitStatusUpdated(resource.getMetadata().getName());
83+
84+
// wait for sure, there are no more events
85+
waitXms(200);
86+
// there is no event on status update processed
87+
assertThat(integrationTestSupport.numberOfControllerExecutions()).isEqualTo(3);
88+
});
89+
90+
}
91+
6692
void awaitStatusUpdated(String name) {
6793
await("cr status updated").atMost(5, TimeUnit.SECONDS)
6894
.untilAsserted(() -> {
6995
SubResourceTestCustomResource cr = (SubResourceTestCustomResource) integrationTestSupport.getCrOperations().inNamespace(TEST_NAMESPACE).withName(name).get();
96+
assertThat(cr.getMetadata().getFinalizers()).hasSize(1);
7097
assertThat(cr).isNotNull();
7198
assertThat(cr.getStatus()).isNotNull();
7299
assertThat(cr.getStatus().getState()).isEqualTo(SUCCESS);

0 commit comments

Comments
 (0)