-
Notifications
You must be signed in to change notification settings - Fork 138
/
Transactions.java
551 lines (472 loc) · 20.6 KB
/
Transactions.java
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2007, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.jvnet.hk2.config;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.inject.Provider;
import java.beans.PropertyChangeEvent;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.hk2.api.PostConstruct;
import org.glassfish.hk2.api.PreDestroy;
import org.jvnet.hk2.annotations.Optional;
import org.jvnet.hk2.annotations.Service;
/**
* Transactions is a singleton service that receives transaction notifications and dispatch these
* notifications asynchronously to listeners.
*
* @author Jerome Dochez
*/
@Service
public final class Transactions implements PostConstruct, PreDestroy {
// each transaction listener has a notification pump.
private final List<Provider<ListenerNotifier<TransactionListener, ?, Void>>> listeners =
new ArrayList<>();
private final Map<Class, Set<ConfigListener>> typeListeners = new HashMap<>();
@Inject
@Named("transactions-executor")
@Optional
private ExecutorService executor;
// all configuration listeners are notified though one notifier.
private final Provider<ConfigListenerNotifier> configListenerNotifier = new Provider<>() {
private final ConfigListenerNotifier configListenerNotifier = new ConfigListenerNotifier();
private final CountDownLatch initialized = new CountDownLatch(1);
@Override
public ConfigListenerNotifier get() {
//synchronized(initialized) {
if (initialized.getCount()>0) {
configListenerNotifier.start();
initialized.countDown();
}
return configListenerNotifier;
//}
}
};
@Override
public void postConstruct() {
if (executor==null) {
executor = Executors.newCachedThreadPool();
}
}
@Override
public void preDestroy() {
for (Provider<ListenerNotifier<TransactionListener, ?, Void>> listener : listeners) {
listener.get().stop();
}
configListenerNotifier.get().stop();
executor.shutdown();
}
/**
* Abstract notification pump, it adds jobs to the queue and process them in the order
* jobs were added.
*
* Jobs are just a wrapper for events of type <U> and a notification mechanism for
* completion notification
*
* @param <T> type of listener interface
* @param <U> type of events the listener methods are expecting
* @param <V> return type of the listener interface methods.
*/
private abstract class Notifier<T, U, V> {
private final BlockingQueue<FutureTask> pendingJobs = new ArrayBlockingQueue<>(50);
private final CountDownLatch latch = new CountDownLatch(1);
/**
* Creates the task that will notify the listeners of a particular job.
* @param job contains the specifics of the notification like the events that need to be notified.
* @return a task that can be run and return an optional value.
*/
protected abstract FutureTask<V> prepare(final Job<T, U, V> job);
/**
* Adds a job to the notification pump. This job will be processed as soon as all other pending
* jobs have completed.
*
* @param job new notification job.
* @return a future on the return value.
*/
public Future<V> add(final Job<T, U, V> job) {
// NOTE that this is put() which blocks, *not* add() which will not block and will
// throw an IllegalStateException if the queue is full.
if (latch.getCount()==0) {
throw new RuntimeException("TransactionListener is inactive, yet jobs are published to it");
}
try {
pendingJobs.put(prepare(job));
} catch (InterruptedException e ) {
throw new RuntimeException(e);
}
return null;
}
protected void start() {
executor.submit(new Runnable() {
@Override
public void run() {
while (latch.getCount()>0) {
try {
final FutureTask job = pendingJobs.take();
// when listeners start a transaction themselves, several jobs try to get published
// simultaneously so we cannot block the pump while delivering the messages.
executor.submit(new Runnable() {
@Override
public void run() {
job.run();
}
});
}
catch (InterruptedException e) {
// do anything here?
}
}
}
});
}
void stop() {
latch.countDown();
// last event to force the close
pendingJobs.add(prepare(new Job<T, U, V>(null, null) {
@Override
public V process(T target) {
return null;
}
}));
}
}
/**
* Default listener notification pump. One thread per listener, jobs processed in
* the order it was received.
*
* @param <T> type of listener interface
* @param <U> type of events the listener methods are expecting
* @param <V> return type of the listener interface methods.
*/
private class ListenerNotifier<T,U,V> extends Notifier<T,U,V> {
final T listener;
public ListenerNotifier(T listener) {
this.listener = listener;
}
@Override
protected FutureTask<V> prepare(final Job<T, U, V> job) {
return new FutureTask<>(new Callable<V>() {
@Override
public V call() throws Exception {
try {
if ( job.mEvents.size() != 0 ) {
return job.process(listener);
}
} finally {
job.releaseLatch();
}
return null;
}
});
}
}
/**
* Configuration listener notification pump. All Listeners are notified within their own thread, only on thread
* takes care of the job pump.
*
*/
private class ConfigListenerNotifier extends Notifier<ConfigListener,PropertyChangeEvent,UnprocessedChangeEvents> {
@Override
protected FutureTask<UnprocessedChangeEvents>
prepare(final Job<ConfigListener, PropertyChangeEvent, UnprocessedChangeEvents> job) {
// first, calculate the recipients.
final Set<ConfigListener> configListeners = new HashSet<>();
if (job.mEvents != null) {
for (PropertyChangeEvent event : job.mEvents) {
final Dom dom = (Dom) ((ConfigView) Proxy.getInvocationHandler(event.getSource())).getMasterView();
configListeners.addAll(dom.getListeners());
// we also notify the parent.
if (dom.parent()!=null) {
configListeners.addAll(dom.parent().getListeners());
}
// and now, notify all listeners for the changed types.
Set<ConfigListener> listeners = typeListeners.get(dom.getProxyType());
if (listeners!=null) {
configListeners.addAll(listeners);
}
// we need to check if elements are removed to ensure
// the typed listeners are notified.
if (event.getNewValue()==null) {
Object oldValue = event.getOldValue();
if (oldValue instanceof ConfigBeanProxy) {
Dom domOldValue = Dom.unwrap((ConfigBeanProxy) oldValue);
Set<ConfigListener> typedListeners = typeListeners.get(domOldValue.<ConfigBeanProxy>getProxyType());
if (typedListeners!=null) {
configListeners.addAll(typedListeners);
}
}
}
}
}
return new FutureTask<>(new Callable<UnprocessedChangeEvents>() {
@Override
public UnprocessedChangeEvents call() throws Exception {
try {
// temporary structure to store our future notifications with pointer to the
// originator config listener
Map<Future<UnprocessedChangeEvents>, ConfigListener> futures = new HashMap<>();
for (final ConfigListener listener : configListeners) {
// each listener is notified in it's own thread.
futures.put(executor.submit(new Callable<UnprocessedChangeEvents>() {
@Override
public UnprocessedChangeEvents call() throws Exception {
UnprocessedChangeEvents e = job.process(listener);
return e;
}
}), listener);
}
List<UnprocessedChangeEvents> unprocessed = new ArrayList<>(futures.size());
for (Map.Entry<Future<UnprocessedChangeEvents>, ConfigListener> futureEntry : futures.entrySet()) {
Future<UnprocessedChangeEvents> future = futureEntry.getKey();
try {
UnprocessedChangeEvents result = future.get(200, TimeUnit.SECONDS);
if (result!=null && result.getUnprocessed()!=null && result.getUnprocessed().size()>0) {
for (UnprocessedChangeEvent event : result.getUnprocessed()) {
Logger.getAnonymousLogger().log(Level.WARNING, "Unprocessed event : " + event);
}
unprocessed.add(result);
}
} catch (InterruptedException e) {
Logger.getAnonymousLogger().log(Level.SEVERE, "Config Listener notification got interrupted", e);
} catch (ExecutionException e) {
Logger.getAnonymousLogger().log(Level.SEVERE, "Config Listener notification got interrupted", e);
} catch (TimeoutException e) {
ConfigListener cl = futureEntry.getValue();
Logger.getAnonymousLogger().log(Level.SEVERE, "Config Listener " + cl.getClass() + " notification took too long", e);
}
}
// all notification have been successful, I just need to notify the unprocessed events.
// note these events are always synchronous so far.
if (!unprocessed.isEmpty()) {
Job unprocessedJob = new UnprocessedEventsJob(unprocessed, null);
for (Provider<ListenerNotifier<TransactionListener, ?, Void>> listener : Transactions.this.listeners) {
listener.get().add(unprocessedJob);
}
}
} finally {
job.releaseLatch();
}
// in theory I should aggregate my unprocessed events but nobody cares.
return null;
}
});
}
}
/**
A job contains an optional CountdownLatch so that a caller can learn when the
transaction has "cleared" by blocking until that time.
*/
private abstract static class Job<T,U,V> {
private final CountDownLatch mLatch;
protected final List<U> mEvents;
public Job(List<U> events, final CountDownLatch latch ) {
mLatch = latch;
mEvents = events;
}
public void waitForLatch() throws InterruptedException {
if ( mLatch != null ) {
mLatch.await();
}
}
public void releaseLatch() {
if ( mLatch != null ) {
mLatch.countDown();
}
}
public abstract V process(T target);
}
private static class TransactionListenerJob extends Job<TransactionListener, PropertyChangeEvent, Void> {
public TransactionListenerJob(List<PropertyChangeEvent> events, CountDownLatch latch) {
super(events, latch);
}
@Override
public Void process(TransactionListener listener) {
try {
listener.transactionCommited(mEvents);
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
}
private static class UnprocessedEventsJob extends Job<TransactionListener, UnprocessedChangeEvents, Void> {
public UnprocessedEventsJob(List<UnprocessedChangeEvents> events, CountDownLatch latch) {
super(events, latch);
}
@Override
public Void process(TransactionListener listener) {
try {
listener.unprocessedTransactedEvents(mEvents);
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
}
private class ConfigListenerJob extends Job<ConfigListener, PropertyChangeEvent, UnprocessedChangeEvents> {
final PropertyChangeEvent[] eventsArray;
public ConfigListenerJob(List<PropertyChangeEvent> events, CountDownLatch latch) {
super(events, latch);
eventsArray = mEvents.toArray(new PropertyChangeEvent[mEvents.size()]);
}
@Override
public UnprocessedChangeEvents process(ConfigListener target) {
return target.changed(eventsArray);
}
}
/**
* adds a listener for a particular config type
* @param listenerType the config type
* @param listener the config listener
*/
public synchronized void addListenerForType(Class listenerType, ConfigListener listener) {
Set<ConfigListener> listeners = typeListeners.get(listenerType);
if (listeners==null) {
listeners = new HashSet<>();
typeListeners.put(listenerType, listeners);
}
listeners.add(listener);
}
/**
* removes a listener for a particular config type
*
* @param listenerType the config type
* @param listener the config listener
* @return true if the listener was removed successfully, false otherwise.
*/
public synchronized boolean removeListenerForType(Class listenerType, ConfigListener listener) {
Set<ConfigListener> listeners = typeListeners.get(listenerType);
if (listeners==null) {
return false;
}
return listeners.remove(listener);
}
/**
* add a new listener to all transaction events.
*
* @param listener to be added.
*/
public void addTransactionsListener(final TransactionListener listener) {
synchronized(listeners) {
listeners.add(new Provider<ListenerNotifier<TransactionListener, ?, Void>>() {
final ListenerNotifier<TransactionListener, PropertyChangeEvent, Void> tsListener = new ListenerNotifier<>(listener);
final CountDownLatch initialized = new CountDownLatch(1);
@Override
public ListenerNotifier<TransactionListener, PropertyChangeEvent, Void> get() {
//synchronized(initialized) {
if (initialized.getCount()>0) {
tsListener.start();
initialized.countDown();
}
//}
return tsListener;
}
});
}
}
/**
* Removes an existing listener for transaction events
* @param listener the registered listener
* @return true if the listener unregistration was successful
*/
public boolean removeTransactionsListener(TransactionListener listener) {
synchronized(listeners) {
for (Provider<ListenerNotifier<TransactionListener, ?, Void>> holder : listeners) {
ListenerNotifier info = holder.get();
if (info.listener==listener) {
info.stop();
return listeners.remove(holder);
}
}
}
return false;
}
public List<TransactionListener> currentListeners() {
synchronized(listeners) {
List<TransactionListener> l = new ArrayList<>();
for (Provider<ListenerNotifier<TransactionListener, ?, Void>> holder : listeners) {
ListenerNotifier<TransactionListener, ?, Void> info = holder.get();
l.add(info.listener);
}
return l;
}
}
/**
* Synchronous notification of a new transactional configuration change operation.
*
* @param events list of changes
*/
void addTransaction( final List<PropertyChangeEvent> events) {
addTransaction(events, true);
}
/**
* Notification of a new transaction completion
*
* @param events accumulated list of changes
* @param waitTillCleared synchronous semantics; wait until all change events are sent
*/
void addTransaction(
final List<PropertyChangeEvent> events,
final boolean waitTillCleared ) {
final List<ListenerNotifier<TransactionListener, ?, Void>> listInfos = new ArrayList<>();
for (Provider<ListenerNotifier<TransactionListener, ?, Void>> holder : listeners) {
ListenerNotifier<TransactionListener, ?, Void> info = holder.get();
listInfos.add(info);
}
// create a CountDownLatch to implement waiting for events to actually be sent
final Job<TransactionListener, ?, Void> job = new TransactionListenerJob( events,
waitTillCleared ? new CountDownLatch(listInfos.size()) : null);
final ConfigListenerJob configJob = new ConfigListenerJob(events,
waitTillCleared? new CountDownLatch(1):null);
// NOTE that this is put() which blocks, *not* add() which will not block and will
// throw an IllegalStateException if the queue is full.
try {
for (ListenerNotifier listener : listInfos) {
listener.add(job);
}
configListenerNotifier.get().add(configJob);
job.waitForLatch();
configJob.waitForLatch();
} catch (InterruptedException e ) {
throw new RuntimeException(e);
}
}
public void waitForDrain() {
// insert a dummy Job and block until is has been processed. This guarantees
// that all prior jobs have finished
addTransaction( new ArrayList<PropertyChangeEvent>(), true );
// at this point all prior transactions are guaranteed to have cleared
}
}