Skip to content
Open
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 @@ -2,6 +2,8 @@

import io.arex.agent.bootstrap.ctx.ArexThreadLocal;
import io.arex.agent.bootstrap.internal.CallDepth;
import io.arex.inst.runtime.config.Config;
import io.arex.inst.runtime.log.LogManager;

/**
* Avoid collecting data multiple times on the call chain
Expand All @@ -22,7 +24,7 @@ public static boolean validate() {
return Context.get() == null;
}

public static boolean exitAndValidate() {
public static boolean exitAndValidate(String repeatMessage) {
CallDepth callDepth = Context.get();
if (callDepth == null) {
return true;
Expand All @@ -32,6 +34,10 @@ public static boolean exitAndValidate() {
Context.remove();
return true;
}

if (Config.get().isEnableDebug()) {
LogManager.info("repeat", repeatMessage);
}
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static boolean excludeOperation(String targetName) {
Set<String> excludeServiceOperations = Config.get().excludeServiceOperations();
boolean isOperationMatched = operationMatched(targetName, excludeServiceOperations);
if (isOperationMatched && ContextManager.needReplay()) {
LogManager.warn("replay.hitBlockList", StringUtil.format("Hit block list, target name: %s", targetName));
LogManager.info("replay.hitBlockList", StringUtil.format("Hit block list, target name: %s", targetName));
}
return isOperationMatched;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@

import static org.junit.jupiter.api.Assertions.*;

import io.arex.inst.runtime.config.ConfigBuilder;
import io.arex.inst.runtime.log.LogManager;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

/**
* @since 2024/1/12
*/
class RepeatedCollectManagerTest {
private static MockedStatic<LogManager> logManagerMockedStatic;
@BeforeAll
static void setUp() {
Mockito.mockStatic(ContextManager.class);
logManagerMockedStatic = Mockito.mockStatic(LogManager.class);
}
@AfterAll
static void tearDown() {
Expand All @@ -23,12 +28,15 @@ static void tearDown() {
@Test
void test() {
assertTrue(RepeatedCollectManager.validate());
assertTrue(RepeatedCollectManager.exitAndValidate());
assertTrue(RepeatedCollectManager.exitAndValidate("test"));

Mockito.when(ContextManager.needRecord()).thenReturn(true);
RepeatedCollectManager.enter();
RepeatedCollectManager.enter();
assertFalse(RepeatedCollectManager.exitAndValidate());
assertTrue(RepeatedCollectManager.exitAndValidate());
// enable debug
ConfigBuilder.create("test").enableDebug(true).build();
assertFalse(RepeatedCollectManager.exitAndValidate("test"));
logManagerMockedStatic.verify(() -> LogManager.info("repeat", "test"));
assertTrue(RepeatedCollectManager.exitAndValidate("test"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static void onExit(
@Advice.Thrown(readOnly = false) Throwable throwable,
@Advice.Local("mockResult") MockResult mockResult,
@Advice.Local("extractor") DatabaseExtractor extractor) {
if (!RepeatedCollectManager.exitAndValidate()) {
if (!RepeatedCollectManager.exitAndValidate("db insert repeat record")) {
return;
}

Expand Down Expand Up @@ -139,7 +139,7 @@ public static void onExit(
@Advice.Argument(8) String sql,
@Advice.Thrown(readOnly = false) Throwable throwable,
@Advice.Local("mockResult") MockResult mockResult) {
if (!RepeatedCollectManager.exitAndValidate()) {
if (!RepeatedCollectManager.exitAndValidate("db update repeat record")) {
return;
}

Expand Down Expand Up @@ -184,7 +184,7 @@ public static void onExit(
@Advice.Argument(4) String sql,
@Advice.Thrown(readOnly = false) Throwable throwable,
@Advice.Local("mockResult") MockResult mockResult) {
if (!RepeatedCollectManager.exitAndValidate()) {
if (!RepeatedCollectManager.exitAndValidate("db delete repeat record")) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static void onExit(@Advice.This Loader loader,
@Advice.Return(readOnly = false) List<?> list,
@Advice.Local("mockResult") MockResult mockResult,
@Advice.Local("extractor") DatabaseExtractor extractor) throws HibernateException {
if (!RepeatedCollectManager.exitAndValidate()) {
if (!RepeatedCollectManager.exitAndValidate("db query repeat record")) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.isA;

@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -77,10 +78,10 @@ void onInsertExit(Runnable mocker, Throwable throwable, MockResult mockResult, P

static Stream<Arguments> onInsertExitCase() {
Runnable emptyMocker = () -> {
Mockito.when(RepeatedCollectManager.exitAndValidate()).thenReturn(false);
Mockito.when(RepeatedCollectManager.exitAndValidate(anyString())).thenReturn(false);
};
Runnable exitAndValidate = () -> {
Mockito.when(RepeatedCollectManager.exitAndValidate()).thenReturn(true);
Mockito.when(RepeatedCollectManager.exitAndValidate(anyString())).thenReturn(true);
};
Runnable needRecord = () -> {
Mockito.when(ContextManager.needRecord()).thenReturn(true);
Expand Down Expand Up @@ -126,7 +127,7 @@ static Stream<Arguments> onUpdateOrInsertEnterCase() {
@MethodSource("onUpdateOrInsertExitCase")
void onUpdateOrInsertExit(Runnable recordType, Object mockResult) {
AtomicReference<DatabaseExtractor> mo = new AtomicReference<>();
Mockito.when(RepeatedCollectManager.exitAndValidate()).thenReturn(true);
Mockito.when(RepeatedCollectManager.exitAndValidate(anyString())).thenReturn(true);
Mockito.when(ContextManager.needRecord()).thenReturn(true);
try (MockedConstruction<DatabaseExtractor> mocked = Mockito.mockConstruction(DatabaseExtractor.class, (mock, context) -> {
mo.set(mock);
Expand All @@ -149,7 +150,7 @@ static Stream<Arguments> onUpdateOrInsertExitCase() {
void updateOrInsertExitReplayThrowable() {
MockResult mock = Mockito.mock(MockResult.class);
Mockito.when(ContextManager.needReplay()).thenReturn(true);
Mockito.when(RepeatedCollectManager.exitAndValidate()).thenReturn(true);
Mockito.when(RepeatedCollectManager.exitAndValidate(anyString())).thenReturn(true);
Throwable throwable = new Throwable();
Mockito.doReturn(true).when(mock).notIgnoreMockResult();
Mockito.doReturn(throwable).when(mock).getThrowable();
Expand Down Expand Up @@ -184,7 +185,7 @@ static Stream<Arguments> onDeleteEnterCase() {
@MethodSource("onDeleteExitCase")
void onDeleteExit(Runnable recordType, Object mockResult) {
AtomicReference<DatabaseExtractor> mo = new AtomicReference<>();
Mockito.when(RepeatedCollectManager.exitAndValidate()).thenReturn(true);
Mockito.when(RepeatedCollectManager.exitAndValidate(anyString())).thenReturn(true);
Mockito.when(ContextManager.needRecord()).thenReturn(true);
try (MockedConstruction<DatabaseExtractor> mocked = Mockito.mockConstruction(DatabaseExtractor.class, (mock, context) -> {
mo.set(mock);
Expand All @@ -207,11 +208,11 @@ static Stream<Arguments> onDeleteExitCase() {
void deleteExitReplayThrowable() {
MockResult mock = Mockito.mock(MockResult.class);
Mockito.when(ContextManager.needReplay()).thenReturn(true);
Mockito.when(RepeatedCollectManager.exitAndValidate()).thenReturn(true);
Mockito.when(RepeatedCollectManager.exitAndValidate(anyString())).thenReturn(true);
Throwable throwable = new Throwable();
Mockito.doReturn(true).when(mock).notIgnoreMockResult();
Mockito.doReturn(throwable).when(mock).getThrowable();
AbstractEntityPersisterInstrumentation.DeleteAdvice.onExit(null, null, throwable, mock);
Mockito.verify(mock, Mockito.times(2)).getThrowable();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void onExit(Runnable mocker, HibernateException exception, MockResult mockResult

static Stream<Arguments> onExitCase() {
Runnable emptyMocker = () -> {};
Runnable exitAndValidate = () -> Mockito.when(RepeatedCollectManager.exitAndValidate()).thenReturn(true);
Runnable exitAndValidate = () -> Mockito.when(RepeatedCollectManager.exitAndValidate(Mockito.anyString())).thenReturn(true);
Runnable needRecord = () -> Mockito.when(ContextManager.needRecord()).thenReturn(true);
Predicate<HibernateException> predicate1 = Objects::isNull;
Predicate<HibernateException> predicate2 = Objects::nonNull;
Expand All @@ -88,4 +88,4 @@ static Stream<Arguments> onExitCase() {
arguments(needRecord, null, null, predicate1)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public static void onExit(@Enter boolean needReplay,
}
return;
}
if (ContextManager.needRecord() && RepeatedCollectManager.exitAndValidate()) {

if (ContextManager.needRecord() && RepeatedCollectManager.exitAndValidate("mongo aggregate repeat record")) {
MongoHelper.record("Aggregate", namespace, pipeline, result, throwable);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static void onExit(@Advice.Enter boolean needReplay,
}
return;
}
if (ContextManager.needRecord() && RepeatedCollectManager.exitAndValidate()) {
if (ContextManager.needRecord() && RepeatedCollectManager.exitAndValidate("mongo listIndexes repeat record.")) {
MongoHelper.record("ListIndexes", namespace, null, result, throwable);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static void onExit(@Advice.Enter boolean needReplay,
}
return;
}
if (ContextManager.needRecord() && RepeatedCollectManager.exitAndValidate()) {
if (ContextManager.needRecord() && RepeatedCollectManager.exitAndValidate("mongo read operation repeat record.")) {
MongoHelper.record(thiz.getClass().getSimpleName(), namespace, filter, result, throwable);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static boolean onEnter() {
@Advice.OnMethodExit(suppress = Throwable.class)
public static void onExit() {
if (ContextManager.needRecord()) {
RepeatedCollectManager.exitAndValidate();
RepeatedCollectManager.exitAndValidate("mongo void operation repeat record");
}
}
}
Expand Down Expand Up @@ -97,7 +97,7 @@ public static void onExit(@Advice.Origin Method method,
return;
}

if (ContextManager.needRecord() && RepeatedCollectManager.exitAndValidate()) {
if (ContextManager.needRecord() && RepeatedCollectManager.exitAndValidate("mongo write operation repeat record")) {
MongoHelper.record(methodName, namespace, filter, result, throwable);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static void onExit(@Advice.Argument(0) MappedStatement var1,
return;
}

if (ContextManager.needRecord() && RepeatedCollectManager.exitAndValidate()) {
if (ContextManager.needRecord() && RepeatedCollectManager.exitAndValidate("mybatis query repeat record")) {
InternalExecutor.record(extractor, var2, result, throwable);
}
}
Expand Down Expand Up @@ -162,7 +162,7 @@ public static void onExit(@Advice.Argument(0) MappedStatement var1,
return;
}

if (ContextManager.needRecord() && RepeatedCollectManager.exitAndValidate()) {
if (ContextManager.needRecord() && RepeatedCollectManager.exitAndValidate("mybatis update repeat record")) {
InternalExecutor.record(extractor, var1, var2, result, throwable);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import net.bytebuddy.matcher.ElementMatchers;
import org.apache.ibatis.builder.StaticSqlSource;
import org.apache.ibatis.executor.BatchExecutor;
import org.apache.ibatis.executor.BatchResult;
Expand All @@ -21,6 +23,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

Expand Down Expand Up @@ -81,10 +84,10 @@ void onExit(Runnable mocker, MockResult mockResult, Predicate<MockResult> predic
static Stream<Arguments> onExitCase() {
Runnable emptyMocker = () -> {};
Runnable exitAndValidate = () -> {
Mockito.when(RepeatedCollectManager.exitAndValidate()).thenReturn(false);
Mockito.when(RepeatedCollectManager.exitAndValidate(ArgumentMatchers.anyString())).thenReturn(false);
};
Runnable needRecord = () -> {
Mockito.when(RepeatedCollectManager.exitAndValidate()).thenReturn(true);
Mockito.when(RepeatedCollectManager.exitAndValidate(ArgumentMatchers.anyString())).thenReturn(true);
Mockito.when(ContextManager.needRecord()).thenReturn(true);
};
Predicate<MockResult> predicate1 = Objects::isNull;
Expand All @@ -107,10 +110,10 @@ void onUpdateExit(Runnable mocker, DatabaseExtractor extractor, MockResult mockR

static Stream<Arguments> onUpdateExitCase() {
Runnable needReplay = () -> {
Mockito.when(RepeatedCollectManager.exitAndValidate()).thenReturn(false);
Mockito.when(RepeatedCollectManager.exitAndValidate(ArgumentMatchers.anyString())).thenReturn(false);
};
Runnable exitAndValidate = () -> {
Mockito.when(RepeatedCollectManager.exitAndValidate()).thenReturn(true);
Mockito.when(RepeatedCollectManager.exitAndValidate(ArgumentMatchers.anyString())).thenReturn(true);
};
Runnable needRecord = () -> {
Mockito.when(ContextManager.needRecord()).thenReturn(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static boolean onEnter(@Advice.This Invoker<?> invoker,
public static void onExit(@Advice.Return(readOnly = false) Result result,
@Advice.Local("extractor") DubboConsumerExtractor extractor,
@Advice.Local("mockResult") MockResult mockResult) {
if (extractor == null || !RepeatedCollectManager.exitAndValidate()) {
if (extractor == null || !RepeatedCollectManager.exitAndValidate("dubbo consumer repeat record")) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.ArgumentMatchers;
import org.mockito.MockedConstruction;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
Expand Down Expand Up @@ -74,7 +75,7 @@ void onExit(Runnable mocker, MockResult mockResult, Predicate<MockResult> predic
static Stream<Arguments> onExitCase() {
Runnable emptyMocker = () -> {};
Runnable exitAndValidate = () -> {
Mockito.when(RepeatedCollectManager.exitAndValidate()).thenReturn(true);
Mockito.when(RepeatedCollectManager.exitAndValidate(ArgumentMatchers.anyString())).thenReturn(true);
};
Runnable needRecord = () -> {
Mockito.when(ContextManager.needRecord()).thenReturn(true);
Expand All @@ -87,4 +88,4 @@ static Stream<Arguments> onExitCase() {
arguments(needRecord, null, predicate1)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static boolean onEnter(@Advice.This Invoker<?> invoker,
public static void onExit(@Advice.Return(readOnly = false) Result result,
@Advice.Local("extractor") DubboConsumerExtractor extractor,
@Advice.Local("mockResult") MockResult mockResult) {
if (extractor == null || !RepeatedCollectManager.exitAndValidate()) {
if (extractor == null || !RepeatedCollectManager.exitAndValidate("dubbo consumer repeat record")) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.ArgumentMatchers;
import org.mockito.MockedConstruction;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
Expand Down Expand Up @@ -74,7 +75,7 @@ void onExit(Runnable mocker, MockResult mockResult, Predicate<MockResult> predic
static Stream<Arguments> onExitCase() {
Runnable emptyMocker = () -> {};
Runnable exitAndValidate = () -> {
Mockito.when(RepeatedCollectManager.exitAndValidate()).thenReturn(true);
Mockito.when(RepeatedCollectManager.exitAndValidate(ArgumentMatchers.anyString())).thenReturn(true);
};
Runnable needRecord = () -> {
Mockito.when(ContextManager.needRecord()).thenReturn(true);
Expand All @@ -87,4 +88,4 @@ static Stream<Arguments> onExitCase() {
arguments(needRecord, null, predicate1)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static boolean onEnter(@Advice.This Invoker<?> invoker,
public static void onExit(@Advice.Return(readOnly = false) Result result,
@Advice.Local("extractor") DubboConsumerExtractor extractor,
@Advice.Local("mockResult") MockResult mockResult) {
if (extractor == null || !RepeatedCollectManager.exitAndValidate()) {
if (extractor == null || !RepeatedCollectManager.exitAndValidate("dubbo consumer repeat record")) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static void onExit(@Advice.This TripleClientCall clientCall,
@Advice.FieldValue("listener") ClientCall.Listener listener,
@Advice.Local("extractor") DubboStreamConsumerExtractor extractor,
@Advice.Local("mockResult") List<MockResult> mockResults) {
if (extractor == null || !RepeatedCollectManager.exitAndValidate()) {
if (extractor == null || !RepeatedCollectManager.exitAndValidate("dubbo stream consumer repeat record")) {
return;
}

Expand Down
Loading