author | egahlin |
Wed, 18 Sep 2019 03:45:46 +0200 | |
branch | JEP-349-branch |
changeset 58197 | 0ef79bd7fb5c |
parent 58153 | 0f7562601338 |
child 58200 | 2d147d680311 |
permissions | -rw-r--r-- |
58145
bc54ed8d908a
Move implementation into jdk.jfr.internal.consumer
egahlin
parents:
58129
diff
changeset
|
1 |
package jdk.jfr.internal.consumer; |
58129 | 2 |
|
3 |
import java.time.Instant; |
|
4 |
import java.util.ArrayList; |
|
5 |
import java.util.List; |
|
6 |
import java.util.function.Consumer; |
|
7 |
||
8 |
import jdk.jfr.EventType; |
|
58145
bc54ed8d908a
Move implementation into jdk.jfr.internal.consumer
egahlin
parents:
58129
diff
changeset
|
9 |
import jdk.jfr.consumer.RecordedEvent; |
58129 | 10 |
import jdk.jfr.internal.LongMap; |
58145
bc54ed8d908a
Move implementation into jdk.jfr.internal.consumer
egahlin
parents:
58129
diff
changeset
|
11 |
import jdk.jfr.internal.consumer.ChunkParser.ParserConfiguration; |
58129 | 12 |
|
58145
bc54ed8d908a
Move implementation into jdk.jfr.internal.consumer
egahlin
parents:
58129
diff
changeset
|
13 |
final class Dispatcher { |
58129 | 14 |
|
58197 | 15 |
final static class EventDispatcher { |
16 |
private final static EventDispatcher[] NO_DISPATCHERS = new EventDispatcher[0]; |
|
58129 | 17 |
|
58197 | 18 |
private final String eventName; |
19 |
private final Consumer<RecordedEvent> action; |
|
58129 | 20 |
|
21 |
public EventDispatcher(String eventName, Consumer<RecordedEvent> action) { |
|
22 |
this.eventName = eventName; |
|
23 |
this.action = action; |
|
24 |
} |
|
25 |
||
58197 | 26 |
private void offer(RecordedEvent event) { |
58129 | 27 |
action.accept(event); |
28 |
} |
|
29 |
||
58197 | 30 |
private boolean accepts(EventType eventType) { |
58129 | 31 |
return (eventName == null || eventType.getName().equals(eventName)); |
32 |
} |
|
58197 | 33 |
|
34 |
public Consumer<RecordedEvent> getAction() { |
|
35 |
return action; |
|
36 |
} |
|
58129 | 37 |
} |
38 |
||
58197 | 39 |
private final Consumer<Throwable>[] errorActions; |
40 |
private final Runnable[] flushActions; |
|
41 |
private final Runnable[] closeActions; |
|
42 |
private final EventDispatcher[] dispatchers; |
|
43 |
private final LongMap<EventDispatcher[]> dispatcherLookup = new LongMap<>(); |
|
58129 | 44 |
final ParserConfiguration parserConfiguration; |
45 |
final Instant startTime; |
|
46 |
final Instant endTime; |
|
47 |
final long startNanos; |
|
48 |
final long endNanos; |
|
49 |
||
50 |
// Cache |
|
51 |
private EventType cacheEventType; |
|
52 |
private EventDispatcher[] cacheDispatchers; |
|
53 |
||
54 |
@SuppressWarnings({"unchecked","rawtypes"}) |
|
55 |
public Dispatcher(StreamConfiguration c) { |
|
56 |
this.flushActions = c.flushActions.toArray(new Runnable[0]); |
|
57 |
this.closeActions = c.closeActions.toArray(new Runnable[0]); |
|
58 |
this.errorActions = c.errorActions.toArray(new Consumer[0]); |
|
59 |
this.dispatchers = c.eventActions.toArray(new EventDispatcher[0]); |
|
60 |
this.parserConfiguration = new ParserConfiguration(0, Long.MAX_VALUE, c.reuse, c.ordered, buildFilter(dispatchers)); |
|
61 |
this.startTime = c.startTime; |
|
62 |
this.endTime = c.endTime; |
|
63 |
this.startNanos = c.startNanos; |
|
64 |
this.endNanos = c.endNanos; |
|
65 |
} |
|
66 |
||
58197 | 67 |
public void runFlushActions() { |
68 |
Runnable[] flushActions = this.flushActions; |
|
69 |
for (int i = 0; i < flushActions.length; i++) { |
|
70 |
try { |
|
71 |
flushActions[i].run(); |
|
72 |
} catch (Exception e) { |
|
73 |
handleError(e); |
|
74 |
} |
|
75 |
} |
|
76 |
} |
|
77 |
||
78 |
public void runCloseActions() { |
|
79 |
Runnable[] closeActions = this.closeActions; |
|
80 |
for (int i = 0; i < closeActions.length; i++) { |
|
81 |
try { |
|
82 |
closeActions[i].run(); |
|
83 |
} catch (Exception e) { |
|
84 |
handleError(e); |
|
85 |
} |
|
86 |
} |
|
87 |
} |
|
88 |
||
58146 | 89 |
private static ParserFilter buildFilter(EventDispatcher[] dispatchers) { |
90 |
ParserFilter ef = new ParserFilter(); |
|
58129 | 91 |
for (EventDispatcher ed : dispatchers) { |
92 |
String name = ed.eventName; |
|
93 |
if (name == null) { |
|
58146 | 94 |
return ParserFilter.ACCEPT_ALL; |
58129 | 95 |
} |
96 |
ef.setThreshold(name, 0); |
|
97 |
} |
|
98 |
return ef; |
|
99 |
} |
|
100 |
||
58197 | 101 |
void dispatch(RecordedEvent event) { |
58129 | 102 |
EventType type = event.getEventType(); |
103 |
EventDispatcher[] dispatchers = null; |
|
104 |
if (type == cacheEventType) { |
|
105 |
dispatchers = cacheDispatchers; |
|
106 |
} else { |
|
107 |
dispatchers = dispatcherLookup.get(type.getId()); |
|
108 |
if (dispatchers == null) { |
|
109 |
List<EventDispatcher> list = new ArrayList<>(); |
|
110 |
for (EventDispatcher e : this.dispatchers) { |
|
111 |
if (e.accepts(type)) { |
|
112 |
list.add(e); |
|
113 |
} |
|
114 |
} |
|
115 |
dispatchers = list.isEmpty() ? EventDispatcher.NO_DISPATCHERS : list.toArray(new EventDispatcher[0]); |
|
116 |
dispatcherLookup.put(type.getId(), dispatchers); |
|
117 |
} |
|
118 |
cacheDispatchers = dispatchers; |
|
119 |
} |
|
58153 | 120 |
// Expected behavior if exception occurs in onEvent: |
121 |
// |
|
122 |
// Synchronous: |
|
123 |
// - User has added onError action: |
|
124 |
// Catch exception, call onError and continue with next event |
|
125 |
// Let Errors propagate to caller of EventStream::start |
|
126 |
// - Default action |
|
127 |
// Catch exception, e.printStackTrace() and continue with next event |
|
128 |
// Let Errors propagate to caller of EventStream::start |
|
129 |
// |
|
130 |
// Asynchronous |
|
131 |
// - User has added onError action |
|
132 |
// Catch exception, call onError and continue with next event |
|
133 |
// Let Errors propagate, shutdown thread and stream |
|
134 |
// - Default action |
|
135 |
// Catch exception, e.printStackTrace() and continue with next event |
|
136 |
// Let Errors propagate and shutdown thread and stream |
|
137 |
// |
|
58129 | 138 |
for (int i = 0; i < dispatchers.length; i++) { |
139 |
try { |
|
140 |
dispatchers[i].offer(event); |
|
141 |
} catch (Exception e) { |
|
142 |
handleError(e); |
|
143 |
} |
|
144 |
} |
|
145 |
} |
|
146 |
||
58197 | 147 |
private void handleError(Throwable e) { |
58129 | 148 |
Consumer<?>[] consumers = this.errorActions; |
149 |
if (consumers.length == 0) { |
|
150 |
defaultErrorHandler(e); |
|
151 |
return; |
|
152 |
} |
|
153 |
for (int i = 0; i < consumers.length; i++) { |
|
154 |
@SuppressWarnings("unchecked") |
|
155 |
Consumer<Throwable> conusmer = (Consumer<Throwable>) consumers[i]; |
|
156 |
conusmer.accept(e); |
|
157 |
} |
|
158 |
} |
|
159 |
||
58197 | 160 |
private void defaultErrorHandler(Throwable e) { |
58129 | 161 |
e.printStackTrace(); |
162 |
} |
|
163 |
} |