Skip to content

Commit a50b7e2

Browse files
committed
[pinpoint-apm#12195] Add new agent list using uid
1 parent 028fa59 commit a50b7e2

37 files changed

+1150
-90
lines changed

collector/src/main/java/com/navercorp/pinpoint/collector/handler/grpc/GrpcAgentInfoHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private PResult handleAgentInfo(ServerHeader header, PAgentInfo agentInfo) {
9191
try {
9292
// agent info
9393
final AgentInfoBo agentInfoBo = this.agentInfoBoMapper.map(agentInfo, header);
94-
this.agentInfoService.insert(agentInfoBo);
94+
this.agentInfoService.insert(header.getServiceUid(), header.getApplicationUid(), agentInfoBo);
9595
return PResult.newBuilder().setSuccess(true).build();
9696
} catch (Exception e) {
9797
logger.warn("Failed to handle. agentInfo={}", MessageFormatUtils.debugLog(agentInfo), e);

collector/src/main/java/com/navercorp/pinpoint/collector/service/AgentInfoService.java

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,24 @@
1818

1919
import com.navercorp.pinpoint.collector.dao.AgentInfoDao;
2020
import com.navercorp.pinpoint.collector.dao.ApplicationIndexDao;
21+
import com.navercorp.pinpoint.collector.uid.dao.AgentNameDao;
22+
import com.navercorp.pinpoint.collector.uid.service.ApplicationUidService;
2123
import com.navercorp.pinpoint.common.server.bo.AgentInfoBo;
24+
import com.navercorp.pinpoint.common.server.uid.ApplicationUid;
25+
import com.navercorp.pinpoint.common.server.uid.ServiceUid;
26+
import com.navercorp.pinpoint.io.request.UidException;
2227
import jakarta.validation.Valid;
2328
import jakarta.validation.constraints.NotBlank;
2429
import jakarta.validation.constraints.PositiveOrZero;
30+
import org.apache.logging.log4j.LogManager;
31+
import org.apache.logging.log4j.Logger;
32+
import org.springframework.beans.factory.annotation.Value;
2533
import org.springframework.stereotype.Service;
2634
import org.springframework.validation.annotation.Validated;
2735

2836
import java.util.Objects;
37+
import java.util.Optional;
38+
import java.util.function.Supplier;
2939

3040
/**
3141
* @author emeroad
@@ -35,19 +45,59 @@
3545
@Service
3646
@Validated
3747
public class AgentInfoService {
48+
private final Logger logger = LogManager.getLogger(this.getClass());
3849

3950
private final AgentInfoDao agentInfoDao;
40-
4151
private final ApplicationIndexDao applicationIndexDao;
4252

43-
public AgentInfoService(AgentInfoDao agentInfoDao, ApplicationIndexDao applicationIndexDao) {
53+
private final ApplicationUidService applicationUidService;
54+
private final AgentNameDao agentNameDao;
55+
56+
private final boolean uidAgentListEnable;
57+
58+
public AgentInfoService(AgentInfoDao agentInfoDao, ApplicationIndexDao applicationIndexDao,
59+
ApplicationUidService applicationUidService, Optional<AgentNameDao> agentNameDao,
60+
@Value("${pinpoint.collector.application.uid.agent.list.enable:false}") boolean uidAgentListEnable) {
4461
this.agentInfoDao = Objects.requireNonNull(agentInfoDao, "agentInfoDao");
4562
this.applicationIndexDao = Objects.requireNonNull(applicationIndexDao, "applicationIndexDao");
63+
this.applicationUidService = Objects.requireNonNull(applicationUidService, "applicationUidService");
64+
this.agentNameDao = agentNameDao.orElse(null);
65+
this.uidAgentListEnable = uidAgentListEnable;
4666
}
4767

48-
public void insert(@Valid final AgentInfoBo agentInfoBo) {
68+
public void insert(Supplier<ServiceUid> serviceUidSupplier, Supplier<ApplicationUid> applicationUidSupplier, @Valid final AgentInfoBo agentInfoBo) {
4969
agentInfoDao.insert(agentInfoBo);
5070
applicationIndexDao.insert(agentInfoBo);
71+
uidAgentListInsert(serviceUidSupplier, applicationUidSupplier, agentInfoBo);
72+
}
73+
74+
private void uidAgentListInsert(Supplier<ServiceUid> serviceUidSupplier, Supplier<ApplicationUid> applicationUidSupplier, AgentInfoBo agentInfoBo) {
75+
if (!uidAgentListEnable || agentNameDao == null) {
76+
return;
77+
}
78+
ServiceUid serviceUid = serviceUidSupplier.get();
79+
ApplicationUid applicationUid = getApplicationUid(applicationUidSupplier, agentInfoBo, serviceUid);
80+
insertApplicationName(agentInfoBo, serviceUid, applicationUid);
81+
}
82+
83+
private ApplicationUid getApplicationUid(Supplier<ApplicationUid> applicationUidSupplier, AgentInfoBo agentInfoBo, ServiceUid serviceUid) {
84+
try {
85+
return applicationUidSupplier.get();
86+
} catch (UidException exception) {
87+
return applicationUidService.getOrCreateApplicationUid(serviceUid, agentInfoBo.getApplicationName());
88+
}
89+
}
90+
91+
private void insertApplicationName(AgentInfoBo agentInfoBo, ServiceUid serviceUid, ApplicationUid applicationUid) {
92+
if (applicationUid == null) {
93+
logger.warn("null applicationUid. {}, applicationName: {}, id: {}, name: {}", serviceUid, agentInfoBo.getApplicationName(), agentInfoBo.getAgentId(), agentInfoBo.getAgentName());
94+
return;
95+
}
96+
try {
97+
agentNameDao.insert(serviceUid, applicationUid, agentInfoBo.getAgentId(), agentInfoBo.getStartTime(), agentInfoBo.getAgentName());
98+
} catch (Exception e) {
99+
logger.warn("Failed to insert uid agent list. {}, applicationName: {}, id: {}, name: {}", serviceUid, agentInfoBo.getApplicationName(), agentInfoBo.getAgentId(), agentInfoBo.getAgentName(), e);
100+
}
51101
}
52102

53103
public AgentInfoBo getSimpleAgentInfo(@NotBlank final String agentId, @PositiveOrZero final long timestamp) {

collector/src/main/java/com/navercorp/pinpoint/collector/uid/UidModule.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,4 @@ public UidModule() {
3535
logger.info("Install UidModule");
3636
}
3737

38-
@Bean
39-
@ConditionalOnMissingBean(ApplicationUidService.class)
40-
public ApplicationUidService emptyApplicationUidService() {
41-
return new EmptyApplicationUidService();
42-
}
43-
44-
@Bean
45-
@ConditionalOnMissingBean(ServiceGroupService.class)
46-
public ServiceGroupService defaultServiceGroupService(@Value("${collector.service.uid.default.value:0}") int staticServiceUid) {
47-
return new StaticServiceGroupService(staticServiceUid);
48-
}
4938
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.navercorp.pinpoint.collector.uid.dao;
2+
3+
import com.navercorp.pinpoint.common.server.uid.ApplicationUid;
4+
import com.navercorp.pinpoint.common.server.uid.ServiceUid;
5+
6+
public interface AgentNameDao {
7+
void insert(ServiceUid serviceUid, ApplicationUid applicationUid, String agentId, long agentStartTime, String agentName);
8+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.navercorp.pinpoint.collector.uid.dao.hbase;
2+
3+
import com.navercorp.pinpoint.collector.uid.dao.AgentNameDao;
4+
import com.navercorp.pinpoint.common.hbase.HbaseColumnFamily;
5+
import com.navercorp.pinpoint.common.hbase.HbaseOperations;
6+
import com.navercorp.pinpoint.common.hbase.HbaseTables;
7+
import com.navercorp.pinpoint.common.hbase.TableNameProvider;
8+
import com.navercorp.pinpoint.common.server.uid.ApplicationUid;
9+
import com.navercorp.pinpoint.common.server.uid.ServiceUid;
10+
import com.navercorp.pinpoint.common.server.util.AgentListRowKeyUtils;
11+
import org.apache.hadoop.hbase.TableName;
12+
import org.apache.hadoop.hbase.client.Put;
13+
import org.apache.hadoop.hbase.util.Bytes;
14+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
15+
import org.springframework.stereotype.Repository;
16+
17+
import java.util.Objects;
18+
19+
@Repository
20+
@ConditionalOnProperty(name = "pinpoint.collector.application.uid.enable", havingValue = "true")
21+
public class HbaseAgentNameDao implements AgentNameDao {
22+
23+
private static final HbaseColumnFamily DESCRIPTOR = HbaseTables.AGENT_NAME;
24+
25+
private final HbaseOperations hbaseOperations;
26+
private final TableNameProvider tableNameProvider;
27+
28+
public HbaseAgentNameDao(HbaseOperations hbaseOperations, TableNameProvider tableNameProvider) {
29+
this.hbaseOperations = Objects.requireNonNull(hbaseOperations, "hbaseOperations");
30+
this.tableNameProvider = Objects.requireNonNull(tableNameProvider, "tableNameProvider");
31+
}
32+
33+
@Override
34+
public void insert(ServiceUid serviceUid, ApplicationUid applicationUid, String agentId, long agentStartTime, String agentName) {
35+
byte[] rowKey = AgentListRowKeyUtils.makeRowKey(serviceUid, applicationUid, agentId, agentStartTime);
36+
byte[] value = Bytes.toBytes(agentName);
37+
38+
Put put = new Put(rowKey, true);
39+
put.addColumn(DESCRIPTOR.getName(), DESCRIPTOR.getName(), value);
40+
41+
final TableName agentListTableName = tableNameProvider.getTableName(DESCRIPTOR.getTable());
42+
hbaseOperations.put(agentListTableName, put);
43+
}
44+
}

collector/src/main/java/com/navercorp/pinpoint/collector/uid/service/EmptyApplicationUidService.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,19 @@
22

33
import com.navercorp.pinpoint.common.server.uid.ApplicationUid;
44
import com.navercorp.pinpoint.common.server.uid.ServiceUid;
5+
import org.apache.logging.log4j.LogManager;
6+
import org.apache.logging.log4j.Logger;
7+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
8+
import org.springframework.stereotype.Service;
59

10+
@Service
11+
@ConditionalOnProperty(value = "pinpoint.collector.application.uid.enable", havingValue = "false", matchIfMissing = true)
612
public class EmptyApplicationUidService implements ApplicationUidService {
13+
private final Logger logger = LogManager.getLogger(this.getClass());
14+
15+
public EmptyApplicationUidService() {
16+
logger.info("EmptyApplicationUidService initialized");
17+
}
718

819
@Override
920
public ApplicationUid getApplicationUid(ServiceUid serviceUid, String applicationName) {

collector/src/main/java/com/navercorp/pinpoint/collector/uid/service/StaticServiceGroupService.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
package com.navercorp.pinpoint.collector.uid.service;
22

33
import com.navercorp.pinpoint.common.server.uid.ServiceUid;
4+
import org.apache.logging.log4j.LogManager;
5+
import org.apache.logging.log4j.Logger;
6+
import org.springframework.beans.factory.annotation.Value;
7+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
8+
import org.springframework.stereotype.Service;
49

10+
@Service
11+
@ConditionalOnProperty(name = "pinpoint.collector.v4.enable", havingValue = "false", matchIfMissing = true)
512
public class StaticServiceGroupService implements ServiceGroupService {
13+
private final Logger logger = LogManager.getLogger(this.getClass());
614

715
private final ServiceUid staticServiceUid;
816

9-
public StaticServiceGroupService(int uid) {
17+
public StaticServiceGroupService(@Value("${collector.service.uid.default.value:0}") int uid) {
1018
this.staticServiceUid = ServiceUid.of(uid);
19+
logger.info("StaticServiceGroupService initialized. {}", this.staticServiceUid);
1120
}
1221

1322
@Override

collector/src/main/resources/pinpoint-collector-root.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ collector.service.uid.cache.spec.maximumSize=200
6969
collector.service.uid.cache.spec.expireAfterWrite=300s
7070

7171
pinpoint.collector.application.uid.enable=false
72+
pinpoint.collector.application.uid.agent.list.enable=false
7273
collector.application.uid.cache.spec.initialCapacity=10
73-
collector.application.uid.cache.spec.maximumSize=200
74+
collector.application.uid.cache.spec.maximumSize=2000
7475
collector.application.uid.cache.spec.expireAfterWrite=300s
7576

7677
###########################################################

commons-hbase/src/main/java/com/navercorp/pinpoint/common/hbase/HbaseTable.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public enum HbaseTable {
3232
APPLICATION_INDEX("ApplicationIndex"),
3333
APPLICATION_UID("ApplicationUid"),
3434
APPLICATION_NAME("ApplicationName"),
35+
AGENT_NAME("AgentName"),
3536
APPLICATION_TRACE_INDEX("ApplicationTraceIndex"),
3637
HOST_APPLICATION_MAP_VER2("HostApplicationMap_Ver2"),
3738
MAP_STATISTICS_CALLEE_VER2("ApplicationMapStatisticsCallee_Ver2"),

commons-hbase/src/main/java/com/navercorp/pinpoint/common/hbase/HbaseTables.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ private ApiMetadata(HbaseTable hBaseTable, byte[] columnFamilyName) {
4444

4545
public static final HbaseColumnFamily APPLICATION_NAME = new HbaseColumnFamily(HbaseTable.APPLICATION_NAME, Bytes.toBytes("N"));
4646

47+
public static final HbaseColumnFamily AGENT_NAME = new HbaseColumnFamily(HbaseTable.AGENT_NAME, Bytes.toBytes("A"));
4748

4849
public static final HbaseColumnFamily APPLICATION_INDEX_AGENTS = new HbaseColumnFamily(HbaseTable.APPLICATION_INDEX, Bytes.toBytes("Agents"));
4950

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.navercorp.pinpoint.common.server.uid;
2+
3+
import java.util.Objects;
4+
5+
public class AgentIdentifier {
6+
7+
private final String id;
8+
private final String name;
9+
private final long startTimestamp;
10+
11+
public AgentIdentifier(String id, String name, long startTimestamp) {
12+
this.id = Objects.requireNonNull(id, "agentId");
13+
this.name = Objects.requireNonNull(name, "agentName");
14+
this.startTimestamp = startTimestamp;
15+
}
16+
17+
public String getId() {
18+
return id;
19+
}
20+
21+
public String getName() {
22+
return name;
23+
}
24+
25+
public long getStartTimestamp() {
26+
return startTimestamp;
27+
}
28+
29+
@Override
30+
public boolean equals(Object o) {
31+
if (this == o) return true;
32+
if (o == null || getClass() != o.getClass()) return false;
33+
34+
AgentIdentifier that = (AgentIdentifier) o;
35+
36+
return id.equals(that.id);
37+
}
38+
39+
@Override
40+
public int hashCode() {
41+
return id.hashCode();
42+
}
43+
44+
@Override
45+
public String toString() {
46+
return "AgentIdentifier{" +
47+
"id=" + id +
48+
", name='" + name + '\'' +
49+
", startTimestamp=" + startTimestamp +
50+
'}';
51+
}
52+
}

commons-server/src/main/java/com/navercorp/pinpoint/common/server/uid/ApplicationUid.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ public int hashCode() {
4747

4848
@Override
4949
public String toString() {
50-
return "ApplicationUid{" +
51-
"uid=" + uid +
52-
'}';
50+
return "ApplicationUid{" + uid + '}';
5351
}
5452
}

commons-server/src/main/java/com/navercorp/pinpoint/common/server/uid/HbaseCellData.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,24 @@
55
public class HbaseCellData {
66

77
private final byte[] rowKey;
8-
private final Object value;
98
long timestamp;
9+
private final Object value;
1010

11-
public HbaseCellData(byte[] rowKey, Object valueObject, long timestamp) {
11+
public HbaseCellData(byte[] rowKey, long timestamp, Object valueObject) {
1212
this.rowKey = Objects.requireNonNull(rowKey, "rowKey");
13-
this.value = valueObject;
1413
this.timestamp = timestamp;
14+
this.value = valueObject;
1515
}
1616

1717
public byte[] getRowKey() {
1818
return rowKey;
1919
}
2020

21-
public Object getValue() {
22-
return value;
23-
}
24-
2521
public long getTimestamp() {
2622
return timestamp;
2723
}
2824

25+
public Object getValue() {
26+
return value;
27+
}
2928
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.navercorp.pinpoint.common.server.uid.mapper;
2+
3+
import com.navercorp.pinpoint.common.hbase.HbaseColumnFamily;
4+
import com.navercorp.pinpoint.common.hbase.HbaseTables;
5+
import com.navercorp.pinpoint.common.hbase.RowMapper;
6+
import com.navercorp.pinpoint.common.hbase.util.CellUtils;
7+
import com.navercorp.pinpoint.common.server.uid.AgentIdentifier;
8+
import com.navercorp.pinpoint.common.server.util.AgentListRowKeyUtils;
9+
import org.apache.hadoop.hbase.Cell;
10+
import org.apache.hadoop.hbase.client.Result;
11+
import org.springframework.stereotype.Component;
12+
13+
@Component
14+
public class AgentNameMapper implements RowMapper<AgentIdentifier> {
15+
16+
private static final HbaseColumnFamily DESCRIPTOR = HbaseTables.AGENT_NAME;
17+
18+
@Override
19+
public AgentIdentifier mapRow(Result result, int rowNum) throws Exception {
20+
if (result.isEmpty()) {
21+
return null;
22+
}
23+
24+
byte[] rowKey = result.getRow();
25+
String agentId = AgentListRowKeyUtils.getAgentId(rowKey);
26+
long agentStartTime = AgentListRowKeyUtils.getAgentStartTime(rowKey);
27+
28+
Cell cell = result.getColumnLatestCell(DESCRIPTOR.getName(), DESCRIPTOR.getName());
29+
String agentName = CellUtils.valueToString(cell);
30+
31+
return new AgentIdentifier(agentId, agentName, agentStartTime);
32+
}
33+
}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import org.springframework.stereotype.Component;
1111

1212
@Component
13-
public class ApplicationNameRowMapper implements RowMapper<HbaseCellData> {
13+
public class ApplicationNameCellMapper implements RowMapper<HbaseCellData> {
1414

1515
private static final HbaseColumnFamily DESCRIPTOR = HbaseTables.APPLICATION_NAME;
1616

@@ -22,10 +22,9 @@ public HbaseCellData mapRow(Result result, int rowNum) throws Exception {
2222

2323
byte[] rowKey = result.getRow();
2424
Cell cell = result.getColumnLatestCell(DESCRIPTOR.getName(), DESCRIPTOR.getName());
25-
String applicationName = CellUtils.valueToString(cell);
2625
long timeStamp = cell.getTimestamp();
2726

28-
29-
return new HbaseCellData(rowKey, applicationName, timeStamp);
27+
String applicationName = CellUtils.valueToString(cell);
28+
return new HbaseCellData(rowKey, timeStamp, applicationName);
3029
}
3130
}

0 commit comments

Comments
 (0)