Skip to content

Commit

Permalink
[#9021] Reduce memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Jul 7, 2022
1 parent b897f45 commit 9e99ecb
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.navercorp.pinpoint.common.server.util.AgentEventType;
import com.navercorp.pinpoint.common.server.util.AgentLifeCycleState;
import com.navercorp.pinpoint.common.server.util.time.Range;
import com.navercorp.pinpoint.loader.service.ServiceTypeRegistryService;
import com.navercorp.pinpoint.web.dao.AgentInfoDao;
import com.navercorp.pinpoint.web.dao.AgentLifeCycleDao;
import com.navercorp.pinpoint.web.dao.ApplicationIndexDao;
Expand Down Expand Up @@ -79,16 +80,20 @@ public class AgentInfoServiceImpl implements AgentInfoService {

private final AgentStatDao<JvmGcBo> jvmGcDao;

private final ServiceTypeRegistryService serviceTypeRegistryService;

public AgentInfoServiceImpl(AgentEventService agentEventService,
AgentWarningStatService agentWarningStatService, ApplicationIndexDao applicationIndexDao,
AgentInfoDao agentInfoDao, AgentLifeCycleDao agentLifeCycleDao,
AgentStatDao<JvmGcBo> jvmGcDao) {
AgentStatDao<JvmGcBo> jvmGcDao,
ServiceTypeRegistryService serviceTypeRegistryService) {
this.agentEventService = Objects.requireNonNull(agentEventService, "agentEventService");
this.agentWarningStatService = Objects.requireNonNull(agentWarningStatService, "agentWarningStatService");
this.applicationIndexDao = Objects.requireNonNull(applicationIndexDao, "applicationIndexDao");
this.agentInfoDao = Objects.requireNonNull(agentInfoDao, "agentInfoDao");
this.agentLifeCycleDao = Objects.requireNonNull(agentLifeCycleDao, "agentLifeCycleDao");
this.jvmGcDao = Objects.requireNonNull(jvmGcDao, "jvmGcDao");
this.serviceTypeRegistryService = Objects.requireNonNull(serviceTypeRegistryService, "serviceTypeRegistryService");
}

@Override
Expand Down Expand Up @@ -138,22 +143,29 @@ public ApplicationAgentHostList getApplicationAgentHostList(int offset, int limi
private ApplicationAgentHostList getApplicationAgentHostList0(int offset, int limit, int durationDays) {
List<String> applicationNameList = getApplicationNameList(applicationIndexDao.selectAllApplicationNames());
if (offset > applicationNameList.size()) {
return new ApplicationAgentHostList(offset, offset, applicationNameList.size());
ApplicationAgentHostList.Builder builder = newBuilder(offset, offset, applicationNameList.size());
return builder.build();
}

long timeStamp = System.currentTimeMillis();
final long timeStamp = System.currentTimeMillis();

final int startIndex = offset - 1;
final int endIndex = Math.min(startIndex + limit, applicationNameList.size());

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

List<String> agentIdList = getAgentIdList(applicationName, durationDays);
List<AgentInfo> agentInfoList = this.agentInfoDao.getAgentInfos(agentIdList, timeStamp);
applicationAgentHostList.put(applicationName, agentInfoList);
builder.addAgentInfo(applicationName, agentInfoList);
}
return applicationAgentHostList;
return builder.build();
}

private ApplicationAgentHostList.Builder newBuilder(int offset, int endIndex, int totalApplications) {
return ApplicationAgentHostList.newBuilder(serviceTypeRegistryService,
offset, endIndex, totalApplications);
}

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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,63 +15,184 @@

package com.navercorp.pinpoint.web.vo;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.navercorp.pinpoint.web.view.ApplicationAgentHostListSerializer;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.navercorp.pinpoint.common.util.StringUtils;
import com.navercorp.pinpoint.loader.service.ServiceTypeRegistryService;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author Taejin Koo
*/
@JsonSerialize(using = ApplicationAgentHostListSerializer.class)
public class ApplicationAgentHostList {

private final Map<String, List<AgentInfo>> map = new LinkedHashMap<>();

private final int startApplicationIndex;
private final int endApplicationIndex;
private final int totalApplications;

public ApplicationAgentHostList(int startApplicationIndex, int endApplicationIndex, int totalApplications) {
private final List<ApplicationInfo> applications;

public ApplicationAgentHostList(int startApplicationIndex, int endApplicationIndex, int totalApplications,
List<ApplicationInfo> applications) {
this.startApplicationIndex = startApplicationIndex;
this.endApplicationIndex = endApplicationIndex;
this.totalApplications = totalApplications;
this.applications = Objects.requireNonNull(applications, "applications");
}

@JsonProperty("startIndex")
public int getStartApplicationIndex() {
return startApplicationIndex;
}

@JsonProperty("endIndex")
public int getEndApplicationIndex() {
return endApplicationIndex;
}

@JsonProperty("totalApplications")
public int getTotalApplications() {
return totalApplications;
}

public Map<String, List<AgentInfo>> getMap() {
return map;
public List<ApplicationInfo> getApplications() {
return applications;
}

public void put(String applicationName, List<AgentInfo> agentInfoList) {
if (applicationName == null) {
return;
}
@Override
public String toString() {
return "ApplicationAgentHostList{" +
"startApplicationIndex=" + startApplicationIndex +
", endApplicationIndex=" + endApplicationIndex +
", totalApplications=" + totalApplications +
", applications=" + applications +
'}';
}

public static Builder newBuilder(ServiceTypeRegistryService serviceTypeRegistryService,
int startApplicationIndex, int endApplicationIndex, int totalApplications) {
return new Builder(startApplicationIndex, endApplicationIndex, totalApplications, serviceTypeRegistryService);
}

List<AgentInfo> value = map.computeIfAbsent(applicationName, k -> new ArrayList<>());
public static class Builder {
private final int startApplicationIndex;
private final int endApplicationIndex;
private final int totalApplications;
private final ServiceTypeRegistryService serviceTypeRegistryService;

if (agentInfoList == null) {
return;
private final Map<String, List<AgentHost>> map = new HashMap<>();

public Builder(int startApplicationIndex, int endApplicationIndex, int totalApplications, ServiceTypeRegistryService serviceTypeRegistryService) {
this.startApplicationIndex = startApplicationIndex;
this.endApplicationIndex = endApplicationIndex;
this.totalApplications = totalApplications;
this.serviceTypeRegistryService = Objects.requireNonNull(serviceTypeRegistryService, "serviceTypeRegistryService");
}

for (AgentInfo agentInfo : agentInfoList) {
if (agentInfo != null) {
value.add(agentInfo);

public void addAgentInfo(String applicationName, List<AgentInfo> agentInfoList) {
if (applicationName == null) {
return;
}

List<AgentHost> value = map.computeIfAbsent(applicationName, k -> new ArrayList<>());

if (agentInfoList == null) {
return;
}

for (AgentInfo agentInfo : agentInfoList) {
if (agentInfo != null) {
value.add(newAgentHost(agentInfo));
}
}
}

private AgentHost newAgentHost(AgentInfo agentInfo) {
String agentId = StringUtils.defaultString(agentInfo.getAgentId(), "");
String hostName = StringUtils.defaultString(agentInfo.getHostName(), "");
String ip = StringUtils.defaultString(agentInfo.getIp(), "");
String serviceType = serviceTypeRegistryService.findServiceType(agentInfo.getServiceTypeCode()).getDesc();
return new AgentHost(agentId, hostName, ip, serviceType);
}

public ApplicationAgentHostList build() {
List<ApplicationInfo> applicationInfos = buildApplicationInfo(this.map);
ApplicationAgentHostList agents = new ApplicationAgentHostList(startApplicationIndex, endApplicationIndex, totalApplications,
applicationInfos);
return agents;
}

private List<ApplicationInfo> buildApplicationInfo(Map<String, List<AgentHost>> map) {
List<ApplicationInfo> applications = map.entrySet().stream()
.map(Builder::newApplication)
.sorted(Comparator.comparing(ApplicationInfo::getApplicationName))
.collect(Collectors.toList());
return applications;
}


private static ApplicationInfo newApplication(Map.Entry<String, List<AgentHost>> entry) {
String applicationName = entry.getKey();

List<AgentHost> agentHosts = entry.getValue();
agentHosts.sort(Comparator.comparing(AgentHost::getAgentId));

return new ApplicationInfo(applicationName, agentHosts);
}
}

public static class ApplicationInfo {
private final String applicationName;
private final List<AgentHost> agents;

public ApplicationInfo(String applicationName, List<AgentHost> agents) {
this.applicationName = Objects.requireNonNull(applicationName, "applicationName");
this.agents = Objects.requireNonNull(agents, "agents");
}

public String getApplicationName() {
return applicationName;
}

public List<AgentHost> getAgents() {
return agents;
}
}

public static class AgentHost {
private final String agentId;
private final String hostName;
private final String ip;
private final String serviceType;

public AgentHost(String agentId, String hostName, String ip, String serviceType) {
this.agentId = Objects.requireNonNull(agentId, "agentId");
this.hostName = Objects.requireNonNull(hostName, "hostName");
this.ip = Objects.requireNonNull(ip, "ip");
this.serviceType = Objects.requireNonNull(serviceType, "serviceType");
}

public String getAgentId() {
return agentId;
}

public String getHostName() {
return hostName;
}

public String getIp() {
return ip;
}

public String getServiceType() {
return serviceType;
}
}

Expand Down

0 comments on commit 9e99ecb

Please sign in to comment.