Skip to content

Commit c6dfbed

Browse files
committed
[#9017] Reduce memory usage
1 parent b897f45 commit c6dfbed

File tree

3 files changed

+160
-118
lines changed

3 files changed

+160
-118
lines changed

web/src/main/java/com/navercorp/pinpoint/web/service/AgentInfoServiceImpl.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.navercorp.pinpoint.common.server.util.AgentEventType;
2222
import com.navercorp.pinpoint.common.server.util.AgentLifeCycleState;
2323
import com.navercorp.pinpoint.common.server.util.time.Range;
24+
import com.navercorp.pinpoint.loader.service.ServiceTypeRegistryService;
2425
import com.navercorp.pinpoint.web.dao.AgentInfoDao;
2526
import com.navercorp.pinpoint.web.dao.AgentLifeCycleDao;
2627
import com.navercorp.pinpoint.web.dao.ApplicationIndexDao;
@@ -79,16 +80,20 @@ public class AgentInfoServiceImpl implements AgentInfoService {
7980

8081
private final AgentStatDao<JvmGcBo> jvmGcDao;
8182

83+
private final ServiceTypeRegistryService serviceTypeRegistryService;
84+
8285
public AgentInfoServiceImpl(AgentEventService agentEventService,
8386
AgentWarningStatService agentWarningStatService, ApplicationIndexDao applicationIndexDao,
8487
AgentInfoDao agentInfoDao, AgentLifeCycleDao agentLifeCycleDao,
85-
AgentStatDao<JvmGcBo> jvmGcDao) {
88+
AgentStatDao<JvmGcBo> jvmGcDao,
89+
ServiceTypeRegistryService serviceTypeRegistryService) {
8690
this.agentEventService = Objects.requireNonNull(agentEventService, "agentEventService");
8791
this.agentWarningStatService = Objects.requireNonNull(agentWarningStatService, "agentWarningStatService");
8892
this.applicationIndexDao = Objects.requireNonNull(applicationIndexDao, "applicationIndexDao");
8993
this.agentInfoDao = Objects.requireNonNull(agentInfoDao, "agentInfoDao");
9094
this.agentLifeCycleDao = Objects.requireNonNull(agentLifeCycleDao, "agentLifeCycleDao");
9195
this.jvmGcDao = Objects.requireNonNull(jvmGcDao, "jvmGcDao");
96+
this.serviceTypeRegistryService = Objects.requireNonNull(serviceTypeRegistryService, "serviceTypeRegistryService");
9297
}
9398

9499
@Override
@@ -138,22 +143,29 @@ public ApplicationAgentHostList getApplicationAgentHostList(int offset, int limi
138143
private ApplicationAgentHostList getApplicationAgentHostList0(int offset, int limit, int durationDays) {
139144
List<String> applicationNameList = getApplicationNameList(applicationIndexDao.selectAllApplicationNames());
140145
if (offset > applicationNameList.size()) {
141-
return new ApplicationAgentHostList(offset, offset, applicationNameList.size());
146+
ApplicationAgentHostList.Builder builder = newBuilder(offset, offset, applicationNameList.size());
147+
return builder.build();
142148
}
143149

144-
long timeStamp = System.currentTimeMillis();
150+
final long timeStamp = System.currentTimeMillis();
151+
152+
final int startIndex = offset - 1;
153+
final int endIndex = Math.min(startIndex + limit, applicationNameList.size());
145154

146-
int startIndex = offset - 1;
147-
int endIndex = Math.min(startIndex + limit, applicationNameList.size());
148-
ApplicationAgentHostList applicationAgentHostList = new ApplicationAgentHostList(offset, endIndex, applicationNameList.size());
155+
ApplicationAgentHostList.Builder builder = newBuilder(offset, endIndex, applicationNameList.size());
149156
for (int i = startIndex; i < endIndex; i++) {
150157
String applicationName = applicationNameList.get(i);
151158

152159
List<String> agentIdList = getAgentIdList(applicationName, durationDays);
153160
List<AgentInfo> agentInfoList = this.agentInfoDao.getAgentInfos(agentIdList, timeStamp);
154-
applicationAgentHostList.put(applicationName, agentInfoList);
161+
builder.addAgentInfo(applicationName, agentInfoList);
155162
}
156-
return applicationAgentHostList;
163+
return builder.build();
164+
}
165+
166+
private ApplicationAgentHostList.Builder newBuilder(int offset, int endIndex, int totalApplications) {
167+
return ApplicationAgentHostList.newBuilder(serviceTypeRegistryService,
168+
offset, endIndex, totalApplications);
157169
}
158170

159171
private List<String> getAgentIdList(String applicationName, int durationDays) {

web/src/main/java/com/navercorp/pinpoint/web/view/ApplicationAgentHostListSerializer.java

Lines changed: 0 additions & 91 deletions
This file was deleted.

web/src/main/java/com/navercorp/pinpoint/web/vo/ApplicationAgentHostList.java

Lines changed: 140 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,63 +15,184 @@
1515

1616
package com.navercorp.pinpoint.web.vo;
1717

18-
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
19-
import com.navercorp.pinpoint.web.view.ApplicationAgentHostListSerializer;
18+
import com.fasterxml.jackson.annotation.JsonProperty;
19+
import com.navercorp.pinpoint.common.util.StringUtils;
20+
import com.navercorp.pinpoint.loader.service.ServiceTypeRegistryService;
2021

2122
import java.util.ArrayList;
22-
import java.util.LinkedHashMap;
23+
import java.util.Comparator;
24+
import java.util.HashMap;
2325
import java.util.List;
2426
import java.util.Map;
27+
import java.util.Objects;
28+
import java.util.stream.Collectors;
2529

2630
/**
2731
* @author Taejin Koo
2832
*/
29-
@JsonSerialize(using = ApplicationAgentHostListSerializer.class)
3033
public class ApplicationAgentHostList {
3134

32-
private final Map<String, List<AgentInfo>> map = new LinkedHashMap<>();
33-
3435
private final int startApplicationIndex;
3536
private final int endApplicationIndex;
3637
private final int totalApplications;
3738

38-
public ApplicationAgentHostList(int startApplicationIndex, int endApplicationIndex, int totalApplications) {
39+
private final List<ApplicationInfo> applications;
40+
41+
public ApplicationAgentHostList(int startApplicationIndex, int endApplicationIndex, int totalApplications,
42+
List<ApplicationInfo> applications) {
3943
this.startApplicationIndex = startApplicationIndex;
4044
this.endApplicationIndex = endApplicationIndex;
4145
this.totalApplications = totalApplications;
46+
this.applications = Objects.requireNonNull(applications, "applications");
4247
}
4348

49+
@JsonProperty("startIndex")
4450
public int getStartApplicationIndex() {
4551
return startApplicationIndex;
4652
}
4753

54+
@JsonProperty("endIndex")
4855
public int getEndApplicationIndex() {
4956
return endApplicationIndex;
5057
}
5158

59+
@JsonProperty("totalApplications")
5260
public int getTotalApplications() {
5361
return totalApplications;
5462
}
5563

56-
public Map<String, List<AgentInfo>> getMap() {
57-
return map;
64+
public List<ApplicationInfo> getApplications() {
65+
return applications;
5866
}
5967

60-
public void put(String applicationName, List<AgentInfo> agentInfoList) {
61-
if (applicationName == null) {
62-
return;
63-
}
68+
@Override
69+
public String toString() {
70+
return "ApplicationAgentHostList{" +
71+
"startApplicationIndex=" + startApplicationIndex +
72+
", endApplicationIndex=" + endApplicationIndex +
73+
", totalApplications=" + totalApplications +
74+
", applications=" + applications +
75+
'}';
76+
}
77+
78+
public static Builder newBuilder(ServiceTypeRegistryService serviceTypeRegistryService,
79+
int startApplicationIndex, int endApplicationIndex, int totalApplications) {
80+
return new Builder(startApplicationIndex, endApplicationIndex, totalApplications, serviceTypeRegistryService);
81+
}
6482

65-
List<AgentInfo> value = map.computeIfAbsent(applicationName, k -> new ArrayList<>());
83+
public static class Builder {
84+
private final int startApplicationIndex;
85+
private final int endApplicationIndex;
86+
private final int totalApplications;
87+
private final ServiceTypeRegistryService serviceTypeRegistryService;
6688

67-
if (agentInfoList == null) {
68-
return;
89+
private final Map<String, List<AgentHost>> map = new HashMap<>();
90+
91+
public Builder(int startApplicationIndex, int endApplicationIndex, int totalApplications, ServiceTypeRegistryService serviceTypeRegistryService) {
92+
this.startApplicationIndex = startApplicationIndex;
93+
this.endApplicationIndex = endApplicationIndex;
94+
this.totalApplications = totalApplications;
95+
this.serviceTypeRegistryService = Objects.requireNonNull(serviceTypeRegistryService, "serviceTypeRegistryService");
6996
}
7097

71-
for (AgentInfo agentInfo : agentInfoList) {
72-
if (agentInfo != null) {
73-
value.add(agentInfo);
98+
99+
public void addAgentInfo(String applicationName, List<AgentInfo> agentInfoList) {
100+
if (applicationName == null) {
101+
return;
102+
}
103+
104+
List<AgentHost> value = map.computeIfAbsent(applicationName, k -> new ArrayList<>());
105+
106+
if (agentInfoList == null) {
107+
return;
74108
}
109+
110+
for (AgentInfo agentInfo : agentInfoList) {
111+
if (agentInfo != null) {
112+
value.add(newAgentHost(agentInfo));
113+
}
114+
}
115+
}
116+
117+
private AgentHost newAgentHost(AgentInfo agentInfo) {
118+
String agentId = StringUtils.defaultString(agentInfo.getAgentId(), "");
119+
String hostName = StringUtils.defaultString(agentInfo.getHostName(), "");
120+
String ip = StringUtils.defaultString(agentInfo.getIp(), "");
121+
String serviceType = serviceTypeRegistryService.findServiceType(agentInfo.getServiceTypeCode()).getDesc();
122+
return new AgentHost(agentId, hostName, ip, serviceType);
123+
}
124+
125+
public ApplicationAgentHostList build() {
126+
List<ApplicationInfo> applicationInfos = buildApplicationInfo(this.map);
127+
ApplicationAgentHostList agents = new ApplicationAgentHostList(startApplicationIndex, endApplicationIndex, totalApplications,
128+
applicationInfos);
129+
return agents;
130+
}
131+
132+
private List<ApplicationInfo> buildApplicationInfo(Map<String, List<AgentHost>> map) {
133+
List<ApplicationInfo> applications = map.entrySet().stream()
134+
.map(Builder::newApplication)
135+
.sorted(Comparator.comparing(ApplicationInfo::getApplicationName))
136+
.collect(Collectors.toList());
137+
return applications;
138+
}
139+
140+
141+
private static ApplicationInfo newApplication(Map.Entry<String, List<AgentHost>> entry) {
142+
String applicationName = entry.getKey();
143+
144+
List<AgentHost> agentHosts = entry.getValue();
145+
agentHosts.sort(Comparator.comparing(AgentHost::getAgentId));
146+
147+
return new ApplicationInfo(applicationName, agentHosts);
148+
}
149+
}
150+
151+
public static class ApplicationInfo {
152+
private final String applicationName;
153+
private final List<AgentHost> agents;
154+
155+
public ApplicationInfo(String applicationName, List<AgentHost> agents) {
156+
this.applicationName = Objects.requireNonNull(applicationName, "applicationName");
157+
this.agents = Objects.requireNonNull(agents, "agents");
158+
}
159+
160+
public String getApplicationName() {
161+
return applicationName;
162+
}
163+
164+
public List<AgentHost> getAgents() {
165+
return agents;
166+
}
167+
}
168+
169+
public static class AgentHost {
170+
private final String agentId;
171+
private final String hostName;
172+
private final String ip;
173+
private final String serviceType;
174+
175+
public AgentHost(String agentId, String hostName, String ip, String serviceType) {
176+
this.agentId = Objects.requireNonNull(agentId, "agentId");
177+
this.hostName = Objects.requireNonNull(hostName, "hostName");
178+
this.ip = Objects.requireNonNull(ip, "ip");
179+
this.serviceType = Objects.requireNonNull(serviceType, "serviceType");
180+
}
181+
182+
public String getAgentId() {
183+
return agentId;
184+
}
185+
186+
public String getHostName() {
187+
return hostName;
188+
}
189+
190+
public String getIp() {
191+
return ip;
192+
}
193+
194+
public String getServiceType() {
195+
return serviceType;
75196
}
76197
}
77198

0 commit comments

Comments
 (0)