Skip to content

Commit 3c03c6d

Browse files
committed
feat(event): opt-in dual-sink for native queue and event plugin
Allow the native ZMQ queue and the event plugin to run at the same time, with activation made explicit rather than inferred from a path so that existing native-queue deployments keep their current behavior after an upgrade. - Plugin activation is resolved explicitly in Args via EventPluginConfig.resolveUseEventPlugin(): with the native queue on it only joins when the operator opts in (runPluginWithNativeQueue = true), so a stale "path" in an upgraded config can never silently load a plugin. - Add pluginLoadFailurePolicy ("fail" | "ignore") to define behavior when the plugin cannot load; default "fail" preserves data integrity. - Isolate plugin trigger posting in try/catch so a plugin fault cannot tear down the node event thread; serialize each trigger once and share it with both sinks so the default path keeps its current cost. - Apply plugin back-pressure (isBusy) whenever the plugin is active, including dual-sink mode, in EventPluginLoader and HistoryEventService. - Tests: upgrade regression, dual-sink routing, plugin-failure isolation, load-failure policy, and updated back-pressure semantics.
1 parent e89c0d6 commit 3c03c6d

8 files changed

Lines changed: 340 additions & 67 deletions

File tree

‎common/src/main/java/org/tron/common/logsfilter/EventPluginConfig.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ public class EventPluginConfig {
3939
@Setter
4040
private boolean useNativeQueue;
4141

42+
// Whether the event plugin should be activated. Resolved in Args from the plugin path
43+
// and the runPluginWithNativeQueue opt-in, so the loader never has to infer activation
44+
// from a possibly-stale path on its own.
45+
@Getter
46+
@Setter
47+
private boolean useEventPlugin;
48+
49+
// See EventConfig#pluginLoadFailurePolicy ("fail" | "ignore").
50+
@Getter
51+
@Setter
52+
private String pluginLoadFailurePolicy = "fail";
53+
4254
@Getter
4355
@Setter
4456
private int bindPort;
@@ -52,6 +64,24 @@ public class EventPluginConfig {
5264
@Setter
5365
private List<TriggerConfig> triggerConfigList;
5466

67+
/**
68+
* Decide whether the event plugin should be an active sink.
69+
*
70+
* <p>Backward-compatible by design:
71+
* <ul>
72+
* <li>native queue OFF: the plugin is the only sink, so it is active whenever a
73+
* plugin path is configured (unchanged legacy behavior);</li>
74+
* <li>native queue ON: the plugin runs alongside the queue only when the operator
75+
* opts in via {@code runPluginWithNativeQueue}. A leftover path alone never
76+
* activates it, so upgrading a native-queue node cannot suddenly load a plugin.
77+
* </li>
78+
* </ul>
79+
*/
80+
public static boolean resolveUseEventPlugin(boolean hasPluginPath, boolean useNativeQueue,
81+
boolean runPluginWithNativeQueue) {
82+
return hasPluginPath && (!useNativeQueue || runPluginWithNativeQueue);
83+
}
84+
5585
public EventPluginConfig() {
5686
pluginPath = "";
5787
serverAddress = "";

‎common/src/main/java/org/tron/core/config/args/EventConfig.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ public class EventConfig {
2525
private String server = "";
2626
private String dbconfig = "";
2727
private boolean contractParse = true;
28+
// When the native queue is enabled, the event plugin is only activated in addition
29+
// to it when this flag is explicitly set to true. This keeps existing native-queue
30+
// deployments (which may still carry a stale "path") on native-only behavior after
31+
// an upgrade. Ignored when useNativeQueue = false (plugin is the only sink then).
32+
private boolean runPluginWithNativeQueue = false;
33+
// What to do when the event plugin fails to load:
34+
// "fail" - abort node startup (default, avoids silently losing plugin data)
35+
// "ignore" - log the error and keep running with the native queue only
36+
private String pluginLoadFailurePolicy = "fail";
2837
// "native" is a Java reserved word; config key cannot match field name directly.
2938
// @Setter(NONE) prevents ConfigBeanFactory from requiring a "nativeQueue" key.
3039
@Setter(lombok.AccessLevel.NONE)

‎common/src/main/resources/reference.conf‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,8 @@ event.subscribe = {
888888
# dbname|username|password|2 (if collection exists, indexes must be created manually).
889889
dbconfig = ""
890890
contractParse = true # Whether to parse contract event data.
891+
runPluginWithNativeQueue = false # Run the event plugin alongside the native queue.
892+
pluginLoadFailurePolicy = "fail" # Plugin load failure: "fail" (abort) or "ignore".
891893

892894
# Event trigger topics.
893895
topics = [

‎framework/src/main/java/org/tron/common/logsfilter/EventPluginLoader.java‎

Lines changed: 125 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.List;
1010
import java.util.Objects;
1111
import java.util.Set;
12+
import java.util.function.Consumer;
1213
import java.util.stream.Collectors;
1314
import lombok.Getter;
1415
import lombok.extern.slf4j.Slf4j;
@@ -91,6 +92,17 @@ public class EventPluginLoader {
9192
@Getter
9293
private boolean useNativeQueue = false;
9394

95+
// Whether the event plugin is an active sink. May be turned off at runtime if the
96+
// plugin fails to load under the "ignore" failure policy.
97+
@Getter
98+
private boolean useEventPlugin = false;
99+
100+
private static final String FAIL_POLICY = "fail";
101+
102+
private static final String IGNORE_POLICY = "ignore";
103+
104+
private String pluginLoadFailurePolicy = FAIL_POLICY;
105+
94106
public static EventPluginLoader getInstance() {
95107
if (Objects.isNull(instance)) {
96108
synchronized (EventPluginLoader.class) {
@@ -247,12 +259,54 @@ public boolean start(EventPluginConfig config) {
247259
this.triggerConfigList = config.getTriggerConfigList();
248260

249261
useNativeQueue = config.isUseNativeQueue();
262+
useEventPlugin = config.isUseEventPlugin();
263+
// Re-resolved every start() so no policy state leaks across runs; any unrecognized
264+
// value defaults to the safe "fail" policy rather than silently becoming "ignore".
265+
pluginLoadFailurePolicy = normalizeFailurePolicy(config.getPluginLoadFailurePolicy());
266+
267+
if (!useNativeQueue && !useEventPlugin) {
268+
logger.error("No event sink is configured: enable the native queue "
269+
+ "or set a plugin path.");
270+
return false;
271+
}
250272

251-
if (config.isUseNativeQueue()) {
252-
return launchNativeQueue(config);
273+
// Start the plugin first so that, under the "fail" policy, a plugin problem is
274+
// surfaced before the native queue starts accepting subscribers.
275+
if (useEventPlugin && !launchEventPlugin(config)) {
276+
if (!IGNORE_POLICY.equals(pluginLoadFailurePolicy)) {
277+
return false;
278+
}
279+
// "ignore" policy: fall back to native-queue-only operation.
280+
logger.warn("Event plugin failed to load; continuing without it "
281+
+ "(pluginLoadFailurePolicy = ignore).");
282+
useEventPlugin = false;
283+
if (!useNativeQueue) {
284+
return false;
285+
}
286+
}
287+
288+
if (useNativeQueue && !launchNativeQueue(config)) {
289+
// Native queue failed after the plugin already started; stop the plugin so a
290+
// failed startup does not leak its threads/resources.
291+
if (useEventPlugin) {
292+
stopPlugin();
293+
}
294+
return false;
253295
}
254296

255-
return launchEventPlugin(config);
297+
return true;
298+
}
299+
300+
private String normalizeFailurePolicy(String policy) {
301+
String p = Objects.isNull(policy) ? "" : policy.trim();
302+
if (IGNORE_POLICY.equalsIgnoreCase(p)) {
303+
return IGNORE_POLICY;
304+
}
305+
if (!p.isEmpty() && !FAIL_POLICY.equalsIgnoreCase(p)) {
306+
logger.warn("Unknown pluginLoadFailurePolicy '{}', falling back to '{}'.",
307+
p, FAIL_POLICY);
308+
}
309+
return FAIL_POLICY;
256310
}
257311

258312
private void setPluginConfig() {
@@ -284,7 +338,7 @@ private void setSingleTriggerConfig(TriggerConfig triggerConfig) {
284338
blockLogTriggerSolidified = false;
285339
}
286340

287-
if (!useNativeQueue) {
341+
if (useEventPlugin) {
288342
setPluginTopic(Trigger.BLOCK_TRIGGER, triggerConfig.getTopic());
289343
}
290344

@@ -304,7 +358,7 @@ private void setSingleTriggerConfig(TriggerConfig triggerConfig) {
304358
transactionLogTriggerSolidified = false;
305359
}
306360

307-
if (!useNativeQueue) {
361+
if (useEventPlugin) {
308362
setPluginTopic(Trigger.TRANSACTION_TRIGGER, triggerConfig.getTopic());
309363
}
310364

@@ -316,7 +370,7 @@ private void setSingleTriggerConfig(TriggerConfig triggerConfig) {
316370
contractEventTriggerEnable = false;
317371
}
318372

319-
if (!useNativeQueue) {
373+
if (useEventPlugin) {
320374
setPluginTopic(Trigger.CONTRACTEVENT_TRIGGER, triggerConfig.getTopic());
321375
}
322376

@@ -332,7 +386,7 @@ private void setSingleTriggerConfig(TriggerConfig triggerConfig) {
332386
contractLogTriggerRedundancy = false;
333387
}
334388

335-
if (!useNativeQueue) {
389+
if (useEventPlugin) {
336390
setPluginTopic(Trigger.CONTRACTLOG_TRIGGER, triggerConfig.getTopic());
337391
}
338392
} else if (EventPluginConfig.SOLIDITY_TRIGGER_NAME
@@ -342,7 +396,7 @@ private void setSingleTriggerConfig(TriggerConfig triggerConfig) {
342396
} else {
343397
solidityTriggerEnable = false;
344398
}
345-
if (!useNativeQueue) {
399+
if (useEventPlugin) {
346400
setPluginTopic(Trigger.SOLIDITY_TRIGGER, triggerConfig.getTopic());
347401
}
348402
} else if (EventPluginConfig.SOLIDITY_EVENT_NAME
@@ -353,7 +407,7 @@ private void setSingleTriggerConfig(TriggerConfig triggerConfig) {
353407
solidityEventTriggerEnable = false;
354408
}
355409

356-
if (!useNativeQueue) {
410+
if (useEventPlugin) {
357411
setPluginTopic(Trigger.SOLIDITY_EVENT_TRIGGER, triggerConfig.getTopic());
358412
}
359413
} else if (EventPluginConfig.SOLIDITY_LOG_NAME
@@ -367,19 +421,38 @@ private void setSingleTriggerConfig(TriggerConfig triggerConfig) {
367421
solidityLogTriggerEnable = false;
368422
solidityLogTriggerRedundancy = false;
369423
}
370-
if (!useNativeQueue) {
424+
if (useEventPlugin) {
371425
setPluginTopic(Trigger.SOLIDITY_LOG_TRIGGER, triggerConfig.getTopic());
372426
}
373427
}
374428
}
375429

430+
// Route a trigger to the plugin listeners, keeping a plugin fault from tearing down
431+
// the node's event thread. Serialization is done once by the caller and shared with
432+
// the native queue, so dual-sink mode adds no extra serialization on the hot path.
433+
private void postToPlugin(Consumer<IPluginEventListener> handler) {
434+
if (Objects.isNull(eventListeners)) {
435+
return;
436+
}
437+
// Isolate per listener so one misbehaving plugin can neither block delivery to the
438+
// others nor tear down the node's event thread. Errors (e.g. OutOfMemoryError) are
439+
// intentionally not caught and are allowed to propagate.
440+
for (IPluginEventListener listener : eventListeners) {
441+
try {
442+
handler.accept(listener);
443+
} catch (Exception e) {
444+
logger.error("event plugin listener failed to handle trigger", e);
445+
}
446+
}
447+
}
448+
376449
public void postSolidityTrigger(SolidityTrigger trigger) {
450+
String json = toJsonString(trigger);
377451
if (useNativeQueue) {
378-
NativeMessageQueue.getInstance()
379-
.publishTrigger(toJsonString(trigger), trigger.getTriggerName());
380-
} else {
381-
eventListeners.forEach(listener ->
382-
listener.handleSolidityTrigger(toJsonString(trigger)));
452+
NativeMessageQueue.getInstance().publishTrigger(json, trigger.getTriggerName());
453+
}
454+
if (useEventPlugin) {
455+
postToPlugin(listener -> listener.handleSolidityTrigger(json));
383456
}
384457
}
385458

@@ -440,6 +513,9 @@ public synchronized boolean isContractLogTriggerRedundancy() {
440513
}
441514

442515
private void setPluginTopic(int eventType, String topic) {
516+
if (Objects.isNull(eventListeners)) {
517+
return;
518+
}
443519
eventListeners.forEach(listener -> listener.setTopic(eventType, topic));
444520
}
445521

@@ -514,66 +590,70 @@ public void stopPlugin() {
514590
}
515591

516592
public void postBlockTrigger(BlockLogTrigger trigger) {
593+
String json = toJsonString(trigger);
517594
if (useNativeQueue) {
518-
NativeMessageQueue.getInstance()
519-
.publishTrigger(toJsonString(trigger), trigger.getTriggerName());
520-
} else {
521-
eventListeners.forEach(listener ->
522-
listener.handleBlockEvent(toJsonString(trigger)));
595+
NativeMessageQueue.getInstance().publishTrigger(json, trigger.getTriggerName());
596+
}
597+
if (useEventPlugin) {
598+
postToPlugin(listener -> listener.handleBlockEvent(json));
523599
}
524600
}
525601

526602
public void postSolidityLogTrigger(ContractLogTrigger trigger) {
603+
String json = toJsonString(trigger);
527604
if (useNativeQueue) {
528-
NativeMessageQueue.getInstance()
529-
.publishTrigger(toJsonString(trigger), trigger.getTriggerName());
530-
} else {
531-
eventListeners.forEach(listener ->
532-
listener.handleSolidityLogTrigger(toJsonString(trigger)));
605+
NativeMessageQueue.getInstance().publishTrigger(json, trigger.getTriggerName());
606+
}
607+
if (useEventPlugin) {
608+
postToPlugin(listener -> listener.handleSolidityLogTrigger(json));
533609
}
534610
}
535611

536612
public void postSolidityEventTrigger(ContractEventTrigger trigger) {
613+
String json = toJsonString(trigger);
537614
if (useNativeQueue) {
538-
NativeMessageQueue.getInstance()
539-
.publishTrigger(toJsonString(trigger), trigger.getTriggerName());
540-
} else {
541-
eventListeners.forEach(listener ->
542-
listener.handleSolidityEventTrigger(toJsonString(trigger)));
615+
NativeMessageQueue.getInstance().publishTrigger(json, trigger.getTriggerName());
616+
}
617+
if (useEventPlugin) {
618+
postToPlugin(listener -> listener.handleSolidityEventTrigger(json));
543619
}
544620
}
545621

546622
public void postTransactionTrigger(TransactionLogTrigger trigger) {
623+
String json = toJsonString(trigger);
547624
if (useNativeQueue) {
548-
NativeMessageQueue.getInstance()
549-
.publishTrigger(toJsonString(trigger), trigger.getTriggerName());
550-
} else {
551-
eventListeners.forEach(listener -> listener.handleTransactionTrigger(toJsonString(trigger)));
625+
NativeMessageQueue.getInstance().publishTrigger(json, trigger.getTriggerName());
626+
}
627+
if (useEventPlugin) {
628+
postToPlugin(listener -> listener.handleTransactionTrigger(json));
552629
}
553630
}
554631

555632
public void postContractLogTrigger(ContractLogTrigger trigger) {
633+
String json = toJsonString(trigger);
556634
if (useNativeQueue) {
557-
NativeMessageQueue.getInstance()
558-
.publishTrigger(toJsonString(trigger), trigger.getTriggerName());
559-
} else {
560-
eventListeners.forEach(listener ->
561-
listener.handleContractLogTrigger(toJsonString(trigger)));
635+
NativeMessageQueue.getInstance().publishTrigger(json, trigger.getTriggerName());
636+
}
637+
if (useEventPlugin) {
638+
postToPlugin(listener -> listener.handleContractLogTrigger(json));
562639
}
563640
}
564641

565642
public void postContractEventTrigger(ContractEventTrigger trigger) {
643+
String json = toJsonString(trigger);
566644
if (useNativeQueue) {
567-
NativeMessageQueue.getInstance()
568-
.publishTrigger(toJsonString(trigger), trigger.getTriggerName());
569-
} else {
570-
eventListeners.forEach(listener ->
571-
listener.handleContractEventTrigger(toJsonString(trigger)));
645+
NativeMessageQueue.getInstance().publishTrigger(json, trigger.getTriggerName());
646+
}
647+
if (useEventPlugin) {
648+
postToPlugin(listener -> listener.handleContractEventTrigger(json));
572649
}
573650
}
574651

575652
public boolean isBusy() {
576-
if (useNativeQueue) {
653+
// Back-pressure guards the plugin's async pending queue, so it applies whenever the
654+
// plugin is an active sink — including dual-sink mode. The native queue has no such
655+
// queue of its own and never reports busy.
656+
if (!useEventPlugin) {
577657
return false;
578658
}
579659
int queueSize = 0;

‎framework/src/main/java/org/tron/core/config/args/Args.java‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,13 @@ private static void applyEventConfig(EventConfig ec) {
370370
epc.setBindPort(nq.getBindport());
371371
epc.setSendQueueLength(nq.getSendqueuelength());
372372

373-
if (!nq.isUseNativeQueue()) {
374-
if (StringUtils.isNotEmpty(ec.getPath())) {
375-
epc.setPluginPath(ec.getPath().trim());
376-
}
373+
// Plugin connection settings are copied only when a plugin path is configured, so the
374+
// plugin can run either on its own (native queue off) or alongside the native queue.
375+
// Without a path the plugin cannot be activated, so server/dbconfig are left unset to
376+
// avoid leaving misleading connection settings on EventPluginConfig.
377+
boolean hasPluginPath = StringUtils.isNotEmpty(ec.getPath());
378+
if (hasPluginPath) {
379+
epc.setPluginPath(ec.getPath().trim());
377380
if (StringUtils.isNotEmpty(ec.getServer())) {
378381
epc.setServerAddress(ec.getServer().trim());
379382
}
@@ -382,6 +385,17 @@ private static void applyEventConfig(EventConfig ec) {
382385
}
383386
}
384387

388+
// Resolve plugin activation explicitly (never inferred later from the raw path):
389+
// - native queue OFF: the plugin is the only sink, so activate it whenever a path
390+
// is set (unchanged legacy behavior).
391+
// - native queue ON : the plugin runs alongside the queue ONLY when the operator
392+
// opts in via runPluginWithNativeQueue. A leftover "path" alone never activates
393+
// it, so upgrading a native-queue node cannot suddenly start loading a plugin.
394+
boolean useEventPlugin = EventPluginConfig.resolveUseEventPlugin(
395+
hasPluginPath, nq.isUseNativeQueue(), ec.isRunPluginWithNativeQueue());
396+
epc.setUseEventPlugin(useEventPlugin);
397+
epc.setPluginLoadFailurePolicy(ec.getPluginLoadFailurePolicy());
398+
385399
// topics
386400
List<TriggerConfig> triggerConfigs = new ArrayList<>();
387401
for (EventConfig.TopicConfig tc : ec.getTopics()) {

0 commit comments

Comments
 (0)