Skip to content

Commit da05a19

Browse files
Merge pull request #6234 from zeusoo001/event-his
feat(event): optimize history event service close logic
2 parents 979567e + 2519294 commit da05a19

File tree

4 files changed

+37
-22
lines changed

4 files changed

+37
-22
lines changed

framework/src/main/java/org/tron/core/services/event/EventService.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,39 +24,44 @@ public class EventService {
2424
@Autowired
2525
private Manager manager;
2626

27+
private EventPluginLoader instance = EventPluginLoader.getInstance();
28+
2729
public void init() {
2830
logger.info("Start to load eventPlugin. {}, {}, {} "
2931
+ "block: {}, {} trx: {}, {}, {} event: {}, {} log: {}, {}, {}, {} solid: {}",
3032
manager.isEventPluginLoaded(),
3133

32-
EventPluginLoader.getInstance().getVersion(),
33-
EventPluginLoader.getInstance().getStartSyncBlockNum(),
34+
instance.getVersion(),
35+
instance.getStartSyncBlockNum(),
3436

35-
EventPluginLoader.getInstance().isBlockLogTriggerEnable(),
36-
EventPluginLoader.getInstance().isBlockLogTriggerSolidified(),
37+
instance.isBlockLogTriggerEnable(),
38+
instance.isBlockLogTriggerSolidified(),
3739

38-
EventPluginLoader.getInstance().isTransactionLogTriggerEnable(),
39-
EventPluginLoader.getInstance().isTransactionLogTriggerSolidified(),
40-
EventPluginLoader.getInstance().isTransactionLogTriggerEthCompatible(),
40+
instance.isTransactionLogTriggerEnable(),
41+
instance.isTransactionLogTriggerSolidified(),
42+
instance.isTransactionLogTriggerEthCompatible(),
4143

42-
EventPluginLoader.getInstance().isContractEventTriggerEnable(),
43-
EventPluginLoader.getInstance().isSolidityEventTriggerEnable(),
44+
instance.isContractEventTriggerEnable(),
45+
instance.isSolidityEventTriggerEnable(),
4446

45-
EventPluginLoader.getInstance().isContractLogTriggerEnable(),
46-
EventPluginLoader.getInstance().isContractLogTriggerRedundancy(),
47-
EventPluginLoader.getInstance().isSolidityLogTriggerEnable(),
48-
EventPluginLoader.getInstance().isSolidityLogTriggerRedundancy(),
47+
instance.isContractLogTriggerEnable(),
48+
instance.isContractLogTriggerRedundancy(),
49+
instance.isSolidityLogTriggerEnable(),
50+
instance.isSolidityLogTriggerRedundancy(),
4951

50-
EventPluginLoader.getInstance().isSolidityTriggerEnable());
52+
instance.isSolidityTriggerEnable());
5153

52-
if (!manager.isEventPluginLoaded() || EventPluginLoader.getInstance().getVersion() != 1) {
54+
if (!manager.isEventPluginLoaded() || instance.getVersion() != 1) {
5355
return;
5456
}
5557

5658
historyEventService.init();
5759
}
5860

5961
public void close() {
62+
if (!manager.isEventPluginLoaded() || instance.getVersion() != 1) {
63+
return;
64+
}
6065
historyEventService.close();
6166
blockEventLoad.close();
6267
realtimeEventService.close();

framework/src/main/java/org/tron/core/services/event/HistoryEventService.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,32 @@ public class HistoryEventService {
2929
@Autowired
3030
private Manager manager;
3131

32-
private volatile boolean isRunning;
32+
private volatile Thread thread;
3333

3434
public void init() {
3535
if (instance.getStartSyncBlockNum() <= 0) {
3636
initEventService(manager.getChainBaseManager().getHeadBlockId());
3737
return;
3838
}
3939

40-
isRunning = true;
41-
42-
new Thread(() -> syncEvent(), "history-event").start();
40+
thread = new Thread(() -> syncEvent(), "history-event");
41+
thread.start();
4342

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

4746
public void close() {
48-
isRunning = false;
47+
if (thread != null) {
48+
thread.interrupt();
49+
}
50+
logger.info("History event service close.");
4951
}
5052

5153
private void syncEvent() {
5254
try {
5355
long tmp = instance.getStartSyncBlockNum();
5456
long endNum = manager.getDynamicPropertiesStore().getLatestSolidifiedBlockNum();
55-
while (tmp <= endNum && isRunning) {
57+
while (tmp <= endNum) {
5658
BlockEvent blockEvent = blockEventGet.getBlockEvent(tmp);
5759
realtimeEventService.flush(blockEvent, false);
5860
solidEventService.flush(blockEvent);

framework/src/test/java/org/tron/core/event/EventServiceTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import org.junit.Test;
66
import org.mockito.Mockito;
7+
import org.tron.common.logsfilter.EventPluginLoader;
78
import org.tron.common.utils.ReflectUtils;
89
import org.tron.core.capsule.BlockCapsule;
910
import org.tron.core.db.Manager;
@@ -38,5 +39,10 @@ public void test() {
3839

3940
eventService.init();
4041
eventService.close();
42+
43+
EventPluginLoader instance = mock(EventPluginLoader.class);
44+
Mockito.when(instance.getVersion()).thenReturn(1);
45+
ReflectUtils.setFieldValue(eventService, "instance", instance);
46+
eventService.close();
4147
}
4248
}

framework/src/test/java/org/tron/core/event/HistoryEventServiceTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public void test() throws Exception {
4444
ReflectUtils.setFieldValue(historyEventService, "realtimeEventService", realtimeEventService);
4545
ReflectUtils.setFieldValue(historyEventService, "blockEventLoad", blockEventLoad);
4646
historyEventService.init();
47+
historyEventService.close();
4748
solidEventService.close();
4849
realtimeEventService.close();
4950
blockEventLoad.close();
@@ -71,7 +72,6 @@ public void test() throws Exception {
7172
Mockito.when(blockEventGet.getBlockEvent(1)).thenReturn(be2);
7273

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

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

84+
historyEventService.init();
85+
historyEventService.close();
8486
}
8587
}

0 commit comments

Comments
 (0)