Skip to content

Commit

Permalink
[#11186] Refactor DataSender
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Jul 5, 2024
1 parent f3f8b90 commit 9498659
Show file tree
Hide file tree
Showing 47 changed files with 320 additions and 398 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.google.inject.TypeLiteral;
import com.navercorp.pinpoint.common.profiler.message.DataSender;
import com.navercorp.pinpoint.common.profiler.message.EnhancedDataSender;
import com.navercorp.pinpoint.io.ResponseMessage;
import com.navercorp.pinpoint.loader.service.AnnotationKeyRegistryService;
import com.navercorp.pinpoint.loader.service.ServiceTypeRegistryService;
import com.navercorp.pinpoint.profiler.context.ServerMetaDataRegistryService;
Expand All @@ -42,15 +41,15 @@
public class ApplicationContextHandler {

private final OrderedSpanRecorder orderedSpanRecorder;
private final TestTcpDataSender tcpDataSender;
private final TestDataSender testDataSender;
private final ServerMetaDataRegistryService serverMetaDataRegistryService;
private final AnnotationKeyRegistryService annotationKeyRegistryService;
private final ServiceTypeRegistryService serviceTypeRegistry;

public ApplicationContextHandler(DefaultApplicationContext defaultApplicationContext) {
Injector injector = defaultApplicationContext.getInjector();
this.orderedSpanRecorder = findRecorder(injector);
this.tcpDataSender = findTestTcpDataSender(injector);
this.testDataSender = findTestTcpDataSender(injector);
this.serverMetaDataRegistryService = findServerMetaDataRegistryService(injector);
this.annotationKeyRegistryService = findAnnotationKeyRegistryService(injector);
this.serviceTypeRegistry = findServiceTypeRegistry(injector);
Expand All @@ -71,13 +70,13 @@ private OrderedSpanRecorder findRecorder(Injector injector) {
throw new IllegalStateException("unexpected dataSender:" + dataSender);
}

private TestTcpDataSender findTestTcpDataSender(Injector injector) {
TypeLiteral<EnhancedDataSender<MetaDataType, ResponseMessage>> dataSenderTypeLiteral = new TypeLiteral<EnhancedDataSender<MetaDataType, ResponseMessage>>() {
private TestDataSender findTestTcpDataSender(Injector injector) {
TypeLiteral<EnhancedDataSender<MetaDataType>> dataSenderTypeLiteral = new TypeLiteral<EnhancedDataSender<MetaDataType>>() {
};
Key<EnhancedDataSender<MetaDataType, ResponseMessage>> dataSenderKey = Key.get(dataSenderTypeLiteral);
EnhancedDataSender<MetaDataType, ResponseMessage> dataSender = injector.getInstance(dataSenderKey);
if (dataSender instanceof TestTcpDataSender) {
return (TestTcpDataSender) dataSender;
Key<EnhancedDataSender<MetaDataType>> dataSenderKey = Key.get(dataSenderTypeLiteral);
EnhancedDataSender<MetaDataType> dataSender = injector.getInstance(dataSenderKey);
if (dataSender instanceof TestDataSender) {
return (TestDataSender) dataSender;
}
throw new IllegalStateException("unexpected dataSender" + dataSender);
}
Expand Down Expand Up @@ -114,7 +113,7 @@ public List<String> getExecutedMethod() {
private void addApiDescription(List<String> list, List<SpanEvent> spanEventList) {
for (SpanEvent spanEvent : spanEventList) {
int apiId = spanEvent.getApiId();
String apiDescription = this.tcpDataSender.getApiDescription(apiId);
String apiDescription = this.testDataSender.getApiDescription(apiId);
list.add(apiDescription);
}
}
Expand All @@ -124,8 +123,8 @@ public OrderedSpanRecorder getOrderedSpanRecorder() {
return orderedSpanRecorder;
}

public TestTcpDataSender getTcpDataSender() {
return tcpDataSender;
public TestDataSender getTestDataSender() {
return testDataSender;
}

public ServerMetaDataRegistryService getServerMetaDataRegistryService() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.navercorp.pinpoint.profiler.test;

import com.navercorp.pinpoint.common.profiler.message.AsyncDataSender;
import com.navercorp.pinpoint.common.profiler.message.DefaultResultResponse;
import com.navercorp.pinpoint.common.profiler.message.EnhancedDataSender;
import com.navercorp.pinpoint.common.profiler.message.ResultResponse;
import com.navercorp.pinpoint.profiler.metadata.MetaDataType;

import java.util.Objects;
import java.util.concurrent.CompletableFuture;

public class AsyncDataSenderDelegator implements AsyncDataSender<MetaDataType, ResultResponse> {

private final EnhancedDataSender<MetaDataType> dataSender;

private final ResultResponse success = new DefaultResultResponse(true, "success");
private final ResultResponse failed = new DefaultResultResponse(false, "failed");

public AsyncDataSenderDelegator(EnhancedDataSender<MetaDataType> dataSender) {
this.dataSender = Objects.requireNonNull(dataSender, "dataSender");
}

@Override
public CompletableFuture<ResultResponse> request(MetaDataType data) {
if (this.dataSender.request(data)) {
return CompletableFuture.completedFuture(success);
} else {
return CompletableFuture.completedFuture(failed);
}
}

@Override
public boolean send(MetaDataType data) {
return this.dataSender.send(data);
}

@Override
public void stop() {
// empty
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.navercorp.pinpoint.bootstrap.context.MethodDescriptor;
import com.navercorp.pinpoint.common.profiler.message.EnhancedDataSender;
import com.navercorp.pinpoint.io.ResponseMessage;
import com.navercorp.pinpoint.profiler.cache.IdAllocator;
import com.navercorp.pinpoint.profiler.cache.Result;
import com.navercorp.pinpoint.profiler.cache.SimpleCache;
Expand All @@ -35,9 +34,9 @@ public class MockApiMetaDataService implements ApiMetaDataService {

private final SimpleCache<String> apiCache = new SimpleCache<>(new IdAllocator.ZigZagAllocator());

private final EnhancedDataSender<MetaDataType, ResponseMessage> enhancedDataSender;
private final EnhancedDataSender<MetaDataType> enhancedDataSender;

public MockApiMetaDataService(EnhancedDataSender<MetaDataType, ResponseMessage> enhancedDataSender) {
public MockApiMetaDataService(EnhancedDataSender<MetaDataType> enhancedDataSender) {
this.enhancedDataSender = Objects.requireNonNull(enhancedDataSender, "enhancedDataSender");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.navercorp.pinpoint.common.profiler.message.EnhancedDataSender;
import com.navercorp.pinpoint.io.ResponseMessage;
import com.navercorp.pinpoint.profiler.metadata.ApiMetaDataService;
import com.navercorp.pinpoint.profiler.metadata.MetaDataType;

Expand All @@ -30,16 +29,16 @@
*/
public class MockApiMetaDataServiceProvider implements Provider<ApiMetaDataService> {

private final Provider<EnhancedDataSender<MetaDataType, ResponseMessage>> enhancedDataSenderProvider;
private final Provider<EnhancedDataSender<MetaDataType>> enhancedDataSenderProvider;

@Inject
public MockApiMetaDataServiceProvider(Provider<EnhancedDataSender<MetaDataType, ResponseMessage>> enhancedDataSenderProvider) {
public MockApiMetaDataServiceProvider(Provider<EnhancedDataSender<MetaDataType>> enhancedDataSenderProvider) {
this.enhancedDataSenderProvider = Objects.requireNonNull(enhancedDataSenderProvider, "enhancedDataSenderProvider");
}

@Override
public ApiMetaDataService get() {
final EnhancedDataSender<MetaDataType, ResponseMessage> enhancedDataSender = this.enhancedDataSenderProvider.get();
final EnhancedDataSender<MetaDataType> enhancedDataSender = this.enhancedDataSenderProvider.get();
return new MockApiMetaDataService(enhancedDataSender);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.google.inject.TypeLiteral;
import com.navercorp.pinpoint.common.profiler.message.DataSender;
import com.navercorp.pinpoint.common.profiler.message.EnhancedDataSender;
import com.navercorp.pinpoint.io.ResponseMessage;
import com.navercorp.pinpoint.profiler.context.DefaultServerMetaDataRegistryService;
import com.navercorp.pinpoint.profiler.context.ServerMetaDataRegistryService;
import com.navercorp.pinpoint.profiler.context.SpanType;
Expand Down Expand Up @@ -66,9 +65,9 @@ protected void configure() {

bind(StorageFactory.class).to(TestSpanStorageFactory.class);

EnhancedDataSender<MetaDataType, ResponseMessage> enhancedDataSender = newTcpDataSender();
EnhancedDataSender<MetaDataType> enhancedDataSender = newTcpDataSender();
logger.debug("enhancedDataSender:{}", enhancedDataSender);
TypeLiteral<EnhancedDataSender<MetaDataType, ResponseMessage>> dataSenderTypeLiteral = new TypeLiteral<EnhancedDataSender<MetaDataType, ResponseMessage>>() {};
TypeLiteral<EnhancedDataSender<MetaDataType>> dataSenderTypeLiteral = new TypeLiteral<EnhancedDataSender<MetaDataType>>() {};
bind(dataSenderTypeLiteral).toInstance(enhancedDataSender);

ServerMetaDataRegistryService serverMetaDataRegistryService = newServerMetaDataRegistryService();
Expand All @@ -89,8 +88,8 @@ private DataSender<SpanType> newUdpSpanDataSender() {
return sender;
}

private EnhancedDataSender<MetaDataType, ResponseMessage> newTcpDataSender() {
return new TestTcpDataSender();
private EnhancedDataSender<MetaDataType> newTcpDataSender() {
return new TestDataSender();
}

private ServerMetaDataRegistryService newServerMetaDataRegistryService() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ private void verifySpan(final ResolvedExpectedTrace expected, ActualTrace actual
if (expected.exception != null) {
final IntStringValue actualExceptionInfo = actual.getExceptionInfo();
if (actualExceptionInfo != null) {
String actualExceptionClassName = this.handler.getTcpDataSender().getString(actualExceptionInfo.getIntValue());
String actualExceptionClassName = this.handler.getTestDataSender().getString(actualExceptionInfo.getIntValue());
String actualExceptionMessage = actualExceptionInfo.getStringValue();
verifyException(expected.exception, actualExceptionClassName, actualExceptionMessage);
} else {
Expand Down Expand Up @@ -459,7 +459,7 @@ private void annotationCompare(int index, ResolvedExpectedTrace expected, Actual
}

if (AnnotationKeyUtils.isCachedArgsKey(expectedAnnotationKey.getCode())) {
expectedValue = this.handler.getTcpDataSender().getStringId(expectedValue.toString());
expectedValue = this.handler.getTestDataSender().getStringId(expectedValue.toString());
}

if (!Objects.equals(expectedValue, actualAnnotation.getValue())) {
Expand Down Expand Up @@ -505,11 +505,11 @@ private void verifyException(Exception expectedException, String actualException
}

private void verifySql(int index, ExpectedSql expected, Annotation<?> actual) {
int id = this.handler.getTcpDataSender().getSqlId(expected.getQuery());
int id = this.handler.getTestDataSender().getSqlId(expected.getQuery());
IntStringStringValue actualSql = (IntStringStringValue) actual.getValue();

if (actualSql.getIntValue() != id) {
String actualQuery = this.handler.getTcpDataSender().getSql(actualSql.getIntValue());
String actualQuery = this.handler.getTestDataSender().getSql(actualSql.getIntValue());

AssertionErrorBuilder builder = new AssertionErrorBuilder(String.format("Annotation[%s].sqlId", index),
id + ":" + expected.getQuery(), actualSql.getIntValue() + ": " + actualQuery);
Expand All @@ -533,11 +533,11 @@ private void verifySql(int index, ExpectedSql expected, Annotation<?> actual) {
}

private void verifySqlUid(int index, ExpectedSql expected, Annotation<?> actual) {
byte[] uid = this.handler.getTcpDataSender().getSqlUid(expected.getQuery());
byte[] uid = this.handler.getTestDataSender().getSqlUid(expected.getQuery());
BytesStringStringValue actualSql = (BytesStringStringValue) actual.getValue();

if (!Arrays.equals(actualSql.getBytesValue(), uid)) {
String actualQuery = this.handler.getTcpDataSender().getSql(actualSql.getBytesValue());
String actualQuery = this.handler.getTestDataSender().getSql(actualSql.getBytesValue());

AssertionErrorBuilder builder = new AssertionErrorBuilder(String.format("Annotation[%s].sqlUid", index),
Arrays.toString(uid) + ":" + expected.getQuery(), Arrays.toString(actualSql.getBytesValue()) + ": " + actualQuery);
Expand Down Expand Up @@ -591,7 +591,7 @@ private String getConstructorInfo(Constructor<?> constructor) {

private int findApiId(String desc) throws AssertionError {
try {
return this.handler.getTcpDataSender().getApiId(desc);
return this.handler.getTestDataSender().getApiId(desc);
} catch (NoSuchElementException e) {
throw new AssertionError("Cannot find apiId of [" + desc + "]");
}
Expand Down Expand Up @@ -635,7 +635,7 @@ public List<String> getExecutedMethod() {
@Override
public void printCache(PrintStream out) {
this.handler.getOrderedSpanRecorder().print(out);
this.handler.getTcpDataSender().printDatas(out);
this.handler.getTestDataSender().printDatas(out);
}

@Override
Expand All @@ -651,7 +651,7 @@ public void initialize(boolean createTraceObject) {
}

this.handler.getOrderedSpanRecorder().clear();
this.handler.getTcpDataSender().clear();
this.handler.getTestDataSender().clear();
ignoredServiceTypes.clear();
}

Expand All @@ -663,7 +663,7 @@ public void cleanUp(boolean detachTraceObject) {
}

this.handler.getOrderedSpanRecorder().clear();
this.handler.getTcpDataSender().clear();
this.handler.getTestDataSender().clear();
ignoredServiceTypes.clear();
}

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

import com.google.common.primitives.UnsignedBytes;
import com.navercorp.pinpoint.common.profiler.message.EnhancedDataSender;
import com.navercorp.pinpoint.io.ResponseMessage;
import com.navercorp.pinpoint.profiler.metadata.ApiMetaData;
import com.navercorp.pinpoint.profiler.metadata.MetaDataType;
import com.navercorp.pinpoint.profiler.metadata.SqlMetaData;
Expand All @@ -33,13 +32,12 @@
import java.util.List;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.function.BiConsumer;

/**
* @author Jongho Moon
* @author jaehong.kim
*/
public class TestTcpDataSender implements EnhancedDataSender<MetaDataType, ResponseMessage> {
public class TestDataSender implements EnhancedDataSender<MetaDataType> {

private final List<Object> datas = Collections.synchronizedList(new ArrayList<>());

Expand Down Expand Up @@ -150,12 +148,6 @@ public boolean request(MetaDataType data, int retry) {
return true;
}

@Override
public boolean request(MetaDataType data, BiConsumer<ResponseMessage, Throwable> listener) {
addData(data);
return true;
}


public String getApiDescription(int id) {
return syncGet(apiIdMap, id);
Expand Down

This file was deleted.

Loading

0 comments on commit 9498659

Please sign in to comment.