Skip to content

feat(event): optimize history event service close logic #6234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,44 @@ public class EventService {
@Autowired
private Manager manager;

private EventPluginLoader instance = EventPluginLoader.getInstance();

public void init() {
logger.info("Start to load eventPlugin. {}, {}, {} "
+ "block: {}, {} trx: {}, {}, {} event: {}, {} log: {}, {}, {}, {} solid: {}",
manager.isEventPluginLoaded(),

EventPluginLoader.getInstance().getVersion(),
EventPluginLoader.getInstance().getStartSyncBlockNum(),
instance.getVersion(),
instance.getStartSyncBlockNum(),

EventPluginLoader.getInstance().isBlockLogTriggerEnable(),
EventPluginLoader.getInstance().isBlockLogTriggerSolidified(),
instance.isBlockLogTriggerEnable(),
instance.isBlockLogTriggerSolidified(),

EventPluginLoader.getInstance().isTransactionLogTriggerEnable(),
EventPluginLoader.getInstance().isTransactionLogTriggerSolidified(),
EventPluginLoader.getInstance().isTransactionLogTriggerEthCompatible(),
instance.isTransactionLogTriggerEnable(),
instance.isTransactionLogTriggerSolidified(),
instance.isTransactionLogTriggerEthCompatible(),

EventPluginLoader.getInstance().isContractEventTriggerEnable(),
EventPluginLoader.getInstance().isSolidityEventTriggerEnable(),
instance.isContractEventTriggerEnable(),
instance.isSolidityEventTriggerEnable(),

EventPluginLoader.getInstance().isContractLogTriggerEnable(),
EventPluginLoader.getInstance().isContractLogTriggerRedundancy(),
EventPluginLoader.getInstance().isSolidityLogTriggerEnable(),
EventPluginLoader.getInstance().isSolidityLogTriggerRedundancy(),
instance.isContractLogTriggerEnable(),
instance.isContractLogTriggerRedundancy(),
instance.isSolidityLogTriggerEnable(),
instance.isSolidityLogTriggerRedundancy(),

EventPluginLoader.getInstance().isSolidityTriggerEnable());
instance.isSolidityTriggerEnable());

if (!manager.isEventPluginLoaded() || EventPluginLoader.getInstance().getVersion() != 1) {
if (!manager.isEventPluginLoaded() || instance.getVersion() != 1) {
return;
}

historyEventService.init();
}

public void close() {
if (!manager.isEventPluginLoaded() || instance.getVersion() != 1) {
return;
}
historyEventService.close();
blockEventLoad.close();
realtimeEventService.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,32 @@ public class HistoryEventService {
@Autowired
private Manager manager;

private volatile boolean isRunning;
private volatile Thread thread;

public void init() {
if (instance.getStartSyncBlockNum() <= 0) {
initEventService(manager.getChainBaseManager().getHeadBlockId());
return;
}

isRunning = true;

new Thread(() -> syncEvent(), "history-event").start();
thread = new Thread(() -> syncEvent(), "history-event");
thread.start();

logger.info("History event service start.");
}

public void close() {
isRunning = false;
if (thread != null) {
thread.interrupt();
}
logger.info("History event service close.");
}

private void syncEvent() {
try {
long tmp = instance.getStartSyncBlockNum();
long endNum = manager.getDynamicPropertiesStore().getLatestSolidifiedBlockNum();
while (tmp <= endNum && isRunning) {
while (tmp <= endNum) {
BlockEvent blockEvent = blockEventGet.getBlockEvent(tmp);
realtimeEventService.flush(blockEvent, false);
solidEventService.flush(blockEvent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.junit.Test;
import org.mockito.Mockito;
import org.tron.common.logsfilter.EventPluginLoader;
import org.tron.common.utils.ReflectUtils;
import org.tron.core.capsule.BlockCapsule;
import org.tron.core.db.Manager;
Expand Down Expand Up @@ -38,5 +39,10 @@ public void test() {

eventService.init();
eventService.close();

EventPluginLoader instance = mock(EventPluginLoader.class);
Mockito.when(instance.getVersion()).thenReturn(1);
ReflectUtils.setFieldValue(eventService, "instance", instance);
eventService.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public void test() throws Exception {
ReflectUtils.setFieldValue(historyEventService, "realtimeEventService", realtimeEventService);
ReflectUtils.setFieldValue(historyEventService, "blockEventLoad", blockEventLoad);
historyEventService.init();
historyEventService.close();
solidEventService.close();
realtimeEventService.close();
blockEventLoad.close();
Expand Down Expand Up @@ -71,7 +72,6 @@ public void test() throws Exception {
Mockito.when(blockEventGet.getBlockEvent(1)).thenReturn(be2);

Mockito.when(instance.getStartSyncBlockNum()).thenReturn(1L);
ReflectUtils.setFieldValue(historyEventService, "isRunning", true);
Mockito.when(dynamicPropertiesStore.getLatestSolidifiedBlockNum()).thenReturn(1L);

Mockito.when(chainBaseManager.getBlockIdByNum(1L))
Expand All @@ -81,5 +81,7 @@ public void test() throws Exception {
method1.setAccessible(true);
method1.invoke(historyEventService);

historyEventService.init();
historyEventService.close();
}
}