1
1
package io .javaoperatorsdk .operator .processing .event ;
2
2
3
- import java .util .Collections ;
4
- import java .util .HashSet ;
5
3
import java .util .Objects ;
6
4
import java .util .Set ;
5
+ import java .util .concurrent .ConcurrentNavigableMap ;
6
+ import java .util .concurrent .ConcurrentSkipListMap ;
7
7
import java .util .concurrent .locks .ReentrantLock ;
8
8
9
9
import org .slf4j .Logger ;
@@ -25,7 +25,8 @@ public class EventSourceManager<R extends HasMetadata>
25
25
private static final Logger log = LoggerFactory .getLogger (EventSourceManager .class );
26
26
27
27
private final ReentrantLock lock = new ReentrantLock ();
28
- private final Set <EventSource > eventSources = Collections .synchronizedSet (new HashSet <>());
28
+ private final ConcurrentNavigableMap <String , EventSource > eventSources =
29
+ new ConcurrentSkipListMap <>();
29
30
private final EventProcessor <R > eventProcessor ;
30
31
private TimerEventSource <R > retryAndRescheduleTimerEventSource ;
31
32
private ControllerResourceEventSource <R > controllerResourceEventSource ;
@@ -56,7 +57,7 @@ public void start() throws OperatorException {
56
57
lock .lock ();
57
58
try {
58
59
log .debug ("Starting event sources." );
59
- for (var eventSource : eventSources ) {
60
+ for (var eventSource : eventSources . values () ) {
60
61
try {
61
62
eventSource .start ();
62
63
} catch (Exception e ) {
@@ -73,7 +74,7 @@ public void stop() {
73
74
lock .lock ();
74
75
try {
75
76
log .debug ("Closing event sources." );
76
- for (var eventSource : eventSources ) {
77
+ for (var eventSource : eventSources . values () ) {
77
78
try {
78
79
eventSource .stop ();
79
80
} catch (Exception e ) {
@@ -93,7 +94,7 @@ public final void registerEventSource(EventSource eventSource)
93
94
Objects .requireNonNull (eventSource , "EventSource must not be null" );
94
95
lock .lock ();
95
96
try {
96
- eventSources .add ( eventSource );
97
+ eventSources .put ( keyFor ( eventSource ), eventSource );
97
98
eventSource .setEventHandler (eventProcessor );
98
99
eventSource .setEventRegistry (this );
99
100
} catch (Throwable e ) {
@@ -108,10 +109,23 @@ public final void registerEventSource(EventSource eventSource)
108
109
}
109
110
}
110
111
112
+ private String keyFor (EventSource source ) {
113
+ var name = source .getClass ().getName ();
114
+ // make sure we process controller and timer event sources first
115
+ // this is needed so that these sources are set when informer sources start so that events can
116
+ // properly be processed
117
+ if (source == controllerResourceEventSource ) {
118
+ name = 0 + name ;
119
+ } else if (source == retryAndRescheduleTimerEventSource ) {
120
+ name = 1 + name ;
121
+ }
122
+ return name ;
123
+ }
124
+
111
125
public void cleanupForCustomResource (ResourceID customResourceUid ) {
112
126
lock .lock ();
113
127
try {
114
- for (EventSource eventSource : this .eventSources ) {
128
+ for (EventSource eventSource : this .eventSources . values () ) {
115
129
eventSource .cleanupForResource (customResourceUid );
116
130
}
117
131
} finally {
@@ -121,7 +135,7 @@ public void cleanupForCustomResource(ResourceID customResourceUid) {
121
135
122
136
@ Override
123
137
public Set <EventSource > getRegisteredEventSources () {
124
- return Collections . unmodifiableSet (eventSources );
138
+ return Set . copyOf (eventSources . values () );
125
139
}
126
140
127
141
@ Override
0 commit comments