Skip to content

Commit

Permalink
fix: concurrent modification when getting event sources (#2573)
Browse files Browse the repository at this point in the history
  • Loading branch information
csviri authored Nov 13, 2024
1 parent 07e744b commit e43b954
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.javaoperatorsdk.operator.processing.event;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -93,7 +93,8 @@ public void add(NamedEventSource eventSource) {
+ keyAsString(getResourceType(original), name)
+ " class/name combination");
}
sources.computeIfAbsent(keyFor(original), k -> new HashMap<>()).put(name, eventSource);
sources.computeIfAbsent(keyFor(original), k -> new ConcurrentHashMap<>()).put(name,
eventSource);
}

@SuppressWarnings("rawtypes")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package io.javaoperatorsdk.operator.processing.event;

import java.util.ConcurrentModificationException;
import java.util.concurrent.Phaser;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.junit.jupiter.api.Test;

import io.fabric8.kubernetes.api.model.ConfigMap;
Expand Down Expand Up @@ -184,4 +190,45 @@ void getEventSourcesShouldWork() {

assertThat(eventSources.getEventSources(Service.class)).isEmpty();
}

@Test
void testConcurrentAddRemoveAndGet() throws InterruptedException {
final var concurrentExceptionFound = new AtomicBoolean(false);
for (int i = 0; i < 1000 && !concurrentExceptionFound.get(); i++) {
final var eventSources = new EventSources();
var eventSourceList =
IntStream.range(1, 20).mapToObj(n -> {
var mockResES = mock(ResourceEventSource.class);
NamedEventSource eventSource = mock(NamedEventSource.class);
when(eventSource.original()).thenReturn(mockResES);
when(eventSource.name()).thenReturn("name" + n);
when(mockResES.resourceType()).thenReturn(HasMetadata.class);
return eventSource;
}).collect(Collectors.toList());

IntStream.range(1, 10).forEach(n -> eventSources.add(eventSourceList.get(n - 1)));

var phaser = new Phaser(2);

var t1 = new Thread(() -> {
phaser.arriveAndAwaitAdvance();
IntStream.range(11, 20).forEach(n -> eventSources.add(eventSourceList.get(n - 1)));
});
var t2 = new Thread(() -> {
phaser.arriveAndAwaitAdvance();
try {
eventSources.getEventSources(HasMetadata.class);
} catch (ConcurrentModificationException e) {
concurrentExceptionFound.set(true);
}
});
t1.start();
t2.start();
t1.join();
t2.join();
}
assertThat(concurrentExceptionFound)
.withFailMessage("ConcurrentModificationException thrown")
.isFalse();
}
}

0 comments on commit e43b954

Please sign in to comment.