diff --git a/actuator/src/main/java/org/tron/core/utils/TransactionRegister.java b/actuator/src/main/java/org/tron/core/utils/TransactionRegister.java index 867ea06bfe2..14746211045 100644 --- a/actuator/src/main/java/org/tron/core/utils/TransactionRegister.java +++ b/actuator/src/main/java/org/tron/core/utils/TransactionRegister.java @@ -1,24 +1,58 @@ package org.tron.core.utils; import java.util.Set; +import java.util.concurrent.atomic.AtomicBoolean; import lombok.extern.slf4j.Slf4j; import org.reflections.Reflections; import org.tron.core.actuator.AbstractActuator; +import org.tron.core.exception.TronError; @Slf4j(topic = "TransactionRegister") public class TransactionRegister { + private static final AtomicBoolean REGISTERED = new AtomicBoolean(false); + private static final String PACKAGE_NAME = "org.tron.core.actuator"; + public static void registerActuator() { - Reflections reflections = new Reflections("org.tron"); - Set> subTypes = reflections - .getSubTypesOf(AbstractActuator.class); - for (Class _class : subTypes) { - try { - _class.newInstance(); - } catch (Exception e) { - logger.error("{} contract actuator register fail!", _class, e); + if (REGISTERED.get()) { + logger.debug("Actuator already registered."); + return; + } + + synchronized (TransactionRegister.class) { + if (REGISTERED.get()) { + logger.debug("Actuator already registered."); + return; + } + logger.debug("Register actuator start."); + Reflections reflections = new Reflections(PACKAGE_NAME); + Set> subTypes = reflections + .getSubTypesOf(AbstractActuator.class); + + for (Class clazz : subTypes) { + try { + logger.debug("Registering actuator: {} start", clazz.getName()); + clazz.getDeclaredConstructor().newInstance(); + logger.debug("Registering actuator: {} done", clazz.getName()); + } catch (Exception e) { + Throwable cause = e.getCause() != null ? e.getCause() : e; + String detail = cause.getMessage() != null ? cause.getMessage() : cause.toString(); + throw new TronError(clazz.getName() + ": " + detail, + e, TronError.ErrCode.ACTUATOR_REGISTER); + } } + + REGISTERED.set(true); + logger.debug("Register actuator done, total {}.", subTypes.size()); } } + static boolean isRegistered() { + return REGISTERED.get(); + } + + // For testing only — resets registration state between tests. + static void resetForTesting() { + REGISTERED.set(false); + } } diff --git a/common/src/main/java/org/tron/core/exception/TronError.java b/common/src/main/java/org/tron/core/exception/TronError.java index f407c6dfe3c..4ee7cdae916 100644 --- a/common/src/main/java/org/tron/core/exception/TronError.java +++ b/common/src/main/java/org/tron/core/exception/TronError.java @@ -49,6 +49,7 @@ public enum ErrCode { RATE_LIMITER_INIT(1), SOLID_NODE_INIT(0), PARAMETER_INIT(1), + ACTUATOR_REGISTER(1), JDK_VERSION(1); private final int code; diff --git a/framework/build.gradle b/framework/build.gradle index 59d070e066d..9cda923d755 100644 --- a/framework/build.gradle +++ b/framework/build.gradle @@ -136,11 +136,11 @@ test { exclude 'org/tron/core/ShieldedTRC20BuilderTest.class' exclude 'org/tron/common/runtime/vm/WithdrawRewardTest.class' } - maxHeapSize = "1024m" + maxHeapSize = "512m" + maxParallelForks = Math.max(1, Math.min(4, Runtime.runtime.availableProcessors())) doFirst { // Restart the JVM after every 100 tests to avoid memory leaks and ensure test isolation forkEvery = 100 - jvmArgs "-XX:MetaspaceSize=128m","-XX:MaxMetaspaceSize=256m", "-XX:+UseG1GC" } } @@ -175,7 +175,9 @@ def binaryRelease(taskName, jarName, mainClass) { exclude "META-INF/*.SF" exclude "META-INF/*.DSA" exclude "META-INF/*.RSA" - + // for service SPI loader for dnsjava + // see https://issues.apache.org/jira/browse/HADOOP-19288 + exclude "META-INF/services/java.net.spi.InetAddressResolverProvider" manifest { attributes "Main-Class": "${mainClass}" } diff --git a/framework/src/test/java/org/tron/core/utils/TransactionRegisterTest.java b/framework/src/test/java/org/tron/core/utils/TransactionRegisterTest.java new file mode 100644 index 00000000000..cb87abff14f --- /dev/null +++ b/framework/src/test/java/org/tron/core/utils/TransactionRegisterTest.java @@ -0,0 +1,137 @@ +package org.tron.core.utils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mockConstruction; +import static org.mockito.Mockito.when; + +import java.util.Collections; +import java.util.HashSet; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.MockedConstruction; +import org.mockito.junit.MockitoJUnitRunner; +import org.reflections.Reflections; +import org.tron.core.actuator.AbstractActuator; +import org.tron.core.actuator.TransferActuator; +import org.tron.core.config.args.Args; +import org.tron.core.exception.TronError; + +@RunWith(MockitoJUnitRunner.class) +public class TransactionRegisterTest { + + @Before + public void init() { + Args.getInstance().setActuatorSet(new HashSet<>()); + TransactionRegister.resetForTesting(); + } + + @After + public void destroy() { + Args.clearParam(); + } + + @Test + public void testAlreadyRegisteredSkipRegistration() { + TransactionRegister.registerActuator(); + assertTrue("First registration should be completed", TransactionRegister.isRegistered()); + + TransactionRegister.registerActuator(); + assertTrue("Registration should still be true", TransactionRegister.isRegistered()); + } + + @Test + public void testConcurrentAccessThreadSafe() throws InterruptedException { + final int threadCount = 5; + Thread[] threads = new Thread[threadCount]; + final AtomicBoolean testPassed = new AtomicBoolean(true); + + for (int i = 0; i < threadCount; i++) { + threads[i] = new Thread(() -> { + try { + TransactionRegister.registerActuator(); + } catch (Exception e) { + testPassed.set(false); + } + }); + } + + for (Thread thread : threads) { + thread.start(); + } + + for (Thread thread : threads) { + thread.join(); + } + + assertTrue("All threads should complete without exceptions", testPassed.get()); + assertTrue("Registration should be completed", TransactionRegister.isRegistered()); + } + + @Test + public void testDoubleCheckLockingAtomicBoolean() { + assertFalse("Initial registration state should be false", TransactionRegister.isRegistered()); + + TransactionRegister.registerActuator(); + assertTrue("After first call, should be registered", TransactionRegister.isRegistered()); + + TransactionRegister.registerActuator(); + assertTrue("After second call, should still be registered", TransactionRegister.isRegistered()); + } + + @Test + public void testRegistrationRunsExactlyOnce() { + final AtomicInteger constructorCallCount = new AtomicInteger(0); + + try (MockedConstruction ignored = mockConstruction(Reflections.class, + (mock, context) -> { + constructorCallCount.incrementAndGet(); + when(mock.getSubTypesOf(AbstractActuator.class)).thenReturn(Collections.emptySet()); + })) { + + // Call multiple times; Reflections should only be constructed once + for (int i = 0; i < 5; i++) { + TransactionRegister.registerActuator(); + } + + assertEquals("Reflections should be constructed exactly once regardless of call count", + 1, constructorCallCount.get()); + assertTrue(TransactionRegister.isRegistered()); + } + } + + @Test + public void testMultipleCallsConsistency() { + assertFalse("Should start unregistered", TransactionRegister.isRegistered()); + + TransactionRegister.registerActuator(); + assertTrue("Should be registered after first call", TransactionRegister.isRegistered()); + + for (int i = 0; i < 5; i++) { + TransactionRegister.registerActuator(); + assertTrue("Should remain registered after call " + (i + 2), + TransactionRegister.isRegistered()); + } + } + + @Test + public void testThrowsTronError() { + try (MockedConstruction ignored = mockConstruction(Reflections.class, + (mock, context) -> when(mock.getSubTypesOf(AbstractActuator.class)) + .thenReturn(Collections.singleton(TransferActuator.class))); + MockedConstruction ignored1 = mockConstruction(TransferActuator.class, + (mock, context) -> { + throw new RuntimeException("boom"); + })) { + TronError error = assertThrows(TronError.class, TransactionRegister::registerActuator); + assertEquals(TronError.ErrCode.ACTUATOR_REGISTER, error.getErrCode()); + assertTrue(error.getMessage().contains("TransferActuator")); + } + } +} \ No newline at end of file diff --git a/framework/src/test/resources/logback-test.xml b/framework/src/test/resources/logback-test.xml index 9cf4a04062f..cc8c84e831f 100644 --- a/framework/src/test/resources/logback-test.xml +++ b/framework/src/test/resources/logback-test.xml @@ -1,40 +1,42 @@ - - - - - - - %d{HH:mm:ss.SSS} %p [%c{1}] %m%n - - - INFO - - - - - ./logs/tron-test.log - - - ./logs/tron-test-%d{yyyy-MM-dd}.%i.log.zip - - - 100MB - 60 - 20GB - - - %d{HH:mm:ss.SSS} %p [%c{1}] %m%n - - - - - - - - - - - - + + + + + + + %d{HH:mm:ss.SSS} %p [%c{1}] %m%n + + + ${console.log.level:-ERROR} + + + + + ./logs/tron-test.log + 8192 + true + + + ./logs/tron-test-%d{yyyy-MM-dd}.%i.log.zip + + + 100MB + 60 + 20GB + + + %d{HH:mm:ss.SSS} %p [%c{1}] %m%n + + + + + + + + + + + +