Repository navigation
Expand file tree
/
Copy pathInMemoryQueueManager.java
More file actions
191 lines (166 loc) · 7.72 KB
/
Copy pathInMemoryQueueManager.java
File metadata and controls
191 lines (166 loc) · 7.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package io.a2a.server.events;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import io.a2a.server.tasks.TaskStateProvider;
import org.jspecify.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ApplicationScoped
public class InMemoryQueueManager implements QueueManager {
private static final Logger LOGGER = LoggerFactory.getLogger(InMemoryQueueManager.class);
private final ConcurrentMap<String, EventQueue> queues = new ConcurrentHashMap<>();
// Fields set by constructor injection cannot be final. We need a noargs constructor for
// Jakarta compatibility, and it seems that making fields set by constructor injection
// final, is not proxyable in all runtimes
private EventQueueFactory factory;
private TaskStateProvider taskStateProvider;
/**
* No-args constructor for CDI proxy creation.
* CDI requires a non-private constructor to create proxies for @ApplicationScoped beans.
* All fields are initialized by the @Inject constructor during actual bean creation.
*/
@SuppressWarnings("NullAway")
protected InMemoryQueueManager() {
// For CDI proxy creation
this.factory = null;
this.taskStateProvider = null;
}
@Inject
public InMemoryQueueManager(TaskStateProvider taskStateProvider) {
this.factory = new DefaultEventQueueFactory();
this.taskStateProvider = taskStateProvider;
}
// For testing with custom factory
public InMemoryQueueManager(EventQueueFactory factory, TaskStateProvider taskStateProvider) {
this.factory = factory;
this.taskStateProvider = taskStateProvider;
}
@Override
public void add(String taskId, EventQueue queue) {
EventQueue existing = queues.putIfAbsent(taskId, queue);
if (existing != null) {
throw new TaskQueueExistsException();
}
}
@Override
public @Nullable EventQueue get(String taskId) {
return queues.get(taskId);
}
@Override
public @Nullable EventQueue tap(String taskId) {
EventQueue queue = queues.get(taskId);
return queue == null ? null : queue.tap();
}
@Override
public void close(String taskId) {
EventQueue existing = queues.remove(taskId);
if (existing == null) {
throw new NoTaskQueueException();
}
// Close the queue to stop EventConsumer polling loop
LOGGER.debug("Closing queue {} for task {}", System.identityHashCode(existing), taskId);
existing.close();
}
@Override
public EventQueue createOrTap(String taskId) {
LOGGER.debug("createOrTap called for task {}, current map size: {}", taskId, queues.size());
EventQueue existing = queues.get(taskId);
// Lazy cleanup: only remove closed queues if task is finalized
// Don't remove queues for non-finalized tasks - they must stay for late-arriving events
if (existing != null && existing.isClosed()) {
boolean isFinalized = (taskStateProvider != null) && taskStateProvider.isTaskFinalized(taskId);
if (isFinalized) {
LOGGER.debug("Removing closed queue {} for finalized task {}", System.identityHashCode(existing), taskId);
queues.remove(taskId);
existing = null;
} else {
LOGGER.debug("Queue {} for task {} is closed but task not finalized - keeping for late-arriving events",
System.identityHashCode(existing), taskId);
// Don't remove or recreate - existing closed queue stays in map
// This is critical for replication where events may arrive after MainQueue closes
// The closed MainQueue can still receive enqueued events via enqueueEvent()
}
}
EventQueue newQueue = null;
if (existing == null) {
// Use builder pattern for cleaner queue creation
// Use the new taskId-aware builder method if available
newQueue = factory.builder(taskId).build();
// Make sure an existing queue has not been added in the meantime
existing = queues.putIfAbsent(taskId, newQueue);
}
EventQueue main = existing == null ? newQueue : existing;
if (main == null) {
throw new IllegalStateException("Failed to create or retrieve queue for task " + taskId);
}
EventQueue result = main.tap(); // Always return ChildQueue
if (existing == null) {
LOGGER.debug("Created new MainQueue {} for task {}, returning ChildQueue {} (map size: {})",
System.identityHashCode(main), taskId, System.identityHashCode(result), queues.size());
} else {
LOGGER.debug("Tapped existing MainQueue {} -> ChildQueue {} for task {}",
System.identityHashCode(main), System.identityHashCode(result), taskId);
}
return result;
}
@Override
public void awaitQueuePollerStart(EventQueue eventQueue) throws InterruptedException {
eventQueue.awaitQueuePollerStart();
}
@Override
public int getActiveChildQueueCount(String taskId) {
EventQueue queue = queues.get(taskId);
if (queue == null || queue.isClosed()) {
return -1; // Queue doesn't exist or is closed
}
// Cast to MainQueue to access getActiveChildCount()
if (queue instanceof EventQueue.MainQueue mainQueue) {
return mainQueue.getActiveChildCount();
}
// This should not happen in normal operation since we only store MainQueues
return -1;
}
/**
* Get the cleanup callback that removes a queue from the map when it closes.
* This is exposed so that subclasses (like ReplicatedQueueManager) can reuse
* this cleanup logic while adding their own callbacks in the correct order.
* <p>
* The cleanup callback checks if the task is finalized before removing the queue.
* If the task is not finalized, the queue remains in the map to handle late-arriving events.
* </p>
*
* @param taskId the task ID for the queue
* @return a Runnable that removes the queue from the map if appropriate
*/
public Runnable getCleanupCallback(String taskId) {
return () -> {
LOGGER.debug("Queue close callback invoked for task {}", taskId);
// Check if task is finalized before removing queue
boolean isFinalized = (taskStateProvider != null) && taskStateProvider.isTaskFinalized(taskId);
if (!isFinalized) {
LOGGER.debug("Task {} is not finalized, keeping queue in map for late-arriving events", taskId);
return; // Don't remove from map - task is still active
}
LOGGER.debug("Task {} is finalized, removing queue from map", taskId);
EventQueue removed = queues.remove(taskId);
if (removed != null) {
LOGGER.debug("Removed closed queue for task {} from QueueManager (map size: {})",
taskId, queues.size());
} else {
LOGGER.debug("Queue for task {} was already removed from map", taskId);
}
};
}
private class DefaultEventQueueFactory implements EventQueueFactory {
@Override
public EventQueue.EventQueueBuilder builder(String taskId) {
// Return builder with callback that removes queue from map when closed
return EventQueue.builder()
.taskId(taskId)
.addOnCloseCallback(getCleanupCallback(taskId))
.taskStateProvider(taskStateProvider);
}
}
}