refactoring AgentInfo, AgentInfoBo

This commit is contained in:
Woonduk Kang
2015-10-06 11:28:30 +09:00
parent a964421ca2
commit ce435bceb2
10 changed files with 196 additions and 132 deletions
@@ -17,10 +17,8 @@
package com.navercorp.pinpoint.collector.mapper.thrift;
import com.navercorp.pinpoint.common.bo.AgentInfoBo;
import com.navercorp.pinpoint.common.service.ServiceTypeRegistryService;
import com.navercorp.pinpoint.thrift.dto.TAgentInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
@@ -28,8 +26,6 @@ import org.springframework.stereotype.Component;
*/
@Component
public class AgentInfoBoMapper implements ThriftBoMapper<AgentInfoBo, TAgentInfo> {
@Autowired
private ServiceTypeRegistryService registry;
@Override
public AgentInfoBo map(TAgentInfo thriftObject) {
@@ -53,7 +49,6 @@ public class AgentInfoBoMapper implements ThriftBoMapper<AgentInfoBo, TAgentInfo
builder.setAgentId(agentId);
builder.setApplicationName(applicationName);
builder.setServiceTypeCode(serviceType);
builder.setServiceType(registry.findServiceType(serviceType));
builder.setPid(pid);
builder.setVmVersion(vmVersion);
builder.setAgentVersion(agentVersion);
@@ -18,7 +18,6 @@ package com.navercorp.pinpoint.common.bo;
import com.navercorp.pinpoint.common.buffer.AutomaticBuffer;
import com.navercorp.pinpoint.common.buffer.Buffer;
import com.navercorp.pinpoint.common.trace.ServiceType;
import java.util.Comparator;
@@ -47,7 +46,6 @@ public class AgentInfoBo {
private final String agentId;
private final String applicationName;
private final short serviceTypeCode;
private final ServiceType serviceType;
private final int pid;
private final String vmVersion;
private final String agentVersion;
@@ -74,7 +72,6 @@ public class AgentInfoBo {
this.endTimeStamp = builder.endTimeStamp;
this.endStatus = builder.endStatus;
this.serverMetaData = builder.serverMetaData;
this.serviceType = builder.serviceType;
}
public String getIp() {
@@ -117,10 +114,7 @@ public class AgentInfoBo {
return serviceTypeCode;
}
public ServiceType getServiceType() {
return serviceType;
}
public String getVmVersion() {
return vmVersion;
}
@@ -203,7 +197,6 @@ public class AgentInfoBo {
private String agentId;
private String applicationName;
private short serviceTypeCode;
private ServiceType serviceType;
private int pid;
private String vmVersion;
private String agentVersion;
@@ -246,12 +239,6 @@ public class AgentInfoBo {
return serviceTypeCode;
}
public void setServiceType(ServiceType serviceType) {
if (serviceType == null) {
throw new NullPointerException("serviceType must not be null");
}
this.serviceType = serviceType;
}
public void setPid(int pid) {
this.pid = pid;
@@ -297,12 +284,6 @@ public class AgentInfoBo {
if (this.agentVersion == null) {
this.agentVersion = "";
}
if (this.serviceType == null) {
throw new IllegalStateException("serviceType not set");
}
if (this.serviceType.getCode() !=this.serviceTypeCode) {
throw new IllegalStateException("serviceType not equals");
}
return new AgentInfoBo(this);
}
}
@@ -87,7 +87,7 @@ public class ServerBuilder {
final String hostName = getHostName(agentHistogram.getId());
final ServiceType serviceType = agentHistogram.getServiceType();
final ServerInstance serverInstance = new ServerInstance(hostName, instanceName, serviceType);
final ServerInstance serverInstance = new ServerInstance(hostName, instanceName, serviceType.getCode());
serverInstanceList.addServerInstance(serverInstance);
}
return serverInstanceList;
@@ -16,14 +16,10 @@
package com.navercorp.pinpoint.web.applicationmap;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.navercorp.pinpoint.common.trace.ServiceType;
import com.navercorp.pinpoint.common.util.AgentLifeCycleState;
import com.navercorp.pinpoint.web.applicationmap.link.MatcherGroup;
import com.navercorp.pinpoint.web.applicationmap.link.ServerMatcher;
import com.navercorp.pinpoint.web.view.AgentLifeCycleStateSerializer;
import com.navercorp.pinpoint.web.view.ServerInstanceSerializer;
import com.navercorp.pinpoint.web.vo.AgentInfo;
import com.navercorp.pinpoint.web.vo.AgentStatus;
@@ -33,30 +29,26 @@ import com.navercorp.pinpoint.web.vo.AgentStatus;
* @author emeroad
* @author HyunGil Jeong
*/
@JsonSerialize(using = ServerInstanceSerializer.class)
public class ServerInstance {
private final String hostName;
private final String name;
private final ServiceType serviceType;
private final short serviceTypeCode;
private final ServerType serverType;
@JsonSerialize(using = AgentLifeCycleStateSerializer.class)
private final AgentLifeCycleState status;
// it is better for something else to inject this.
// it's difficult to do that since it is new'ed within logic
private static final MatcherGroup MATCHER_GROUP = new MatcherGroup();
private ServerMatcher match;
public ServerInstance(AgentInfo agentInfo) {
if (agentInfo == null) {
throw new NullPointerException("agentInfo must not be null");
}
this.hostName = agentInfo.getHostName();
this.name = agentInfo.getAgentId();
this.serviceType = agentInfo.getServiceType();
this.serviceTypeCode = agentInfo.getServiceTypeCode();
AgentStatus agentStatus = agentInfo.getStatus();
if (agentStatus != null) {
this.status = agentStatus.getState();
@@ -64,96 +56,62 @@ public class ServerInstance {
this.status = AgentLifeCycleState.UNKNOWN;
}
this.serverType = ServerType.Physical;
this.match = MATCHER_GROUP.match(hostName);
}
public ServerInstance(String hostName, String physicalName, ServiceType serviceType) {
public ServerInstance(String hostName, String physicalName, short serviceTypeCode) {
if (hostName == null) {
throw new NullPointerException("hostName must not be null");
}
if (physicalName == null) {
throw new NullPointerException("logicalName must not be null");
}
if (serviceType == null) {
throw new NullPointerException("serviceType must not be null");
}
this.hostName = hostName;
this.name = physicalName;
this.serviceType = serviceType;
this.serviceTypeCode = serviceTypeCode;
this.status = AgentLifeCycleState.UNKNOWN;
this.serverType = ServerType.Logical;
this.match = MATCHER_GROUP.match(hostName);
}
@JsonIgnore
public String getHostName() {
return hostName;
}
@JsonProperty("name")
public String getName() {
return name;
}
@JsonProperty("serviceType")
public ServiceType getServiceType() {
return serviceType;
public short getServiceTypeCode() {
return serviceTypeCode;
}
@JsonProperty("status")
@JsonSerialize(using = AgentLifeCycleStateSerializer.class)
public AgentLifeCycleState getStatus() {
return status;
}
@JsonIgnore
public ServerType getServerType() {
return serverType;
}
@JsonProperty("linkName")
public String getLinkName() {
return match.getLinkName();
}
@JsonProperty("linkURL")
public String getLinkURL() {
return match.getLink(hostName);
}
@JsonProperty("hasInspector")
public boolean hasInspector() {
if (serviceType.isWas()) {
return true;
} else {
return false;
}
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ServerInstance that = (ServerInstance)o;
ServerInstance that = (ServerInstance) o;
if (!name.equals(that.name))
return false;
if (serverType != that.serverType)
return false;
if (serviceType != that.serviceType)
return false;
if (serviceTypeCode != that.serviceTypeCode) return false;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
return serverType == that.serverType;
return true;
}
@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + serverType.hashCode();
result = 31 * result + serviceType.hashCode();
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (int) serviceTypeCode;
result = 31 * result + (serverType != null ? serverType.hashCode() : 0);
return result;
}
@@ -137,8 +137,7 @@ public class HbaseAgentInfoDao implements AgentInfoDao {
final AgentInfoBo.Builder agentInfoBoBuilder = createBuilderFromValue(serializedAgentInfo);
agentInfoBoBuilder.setAgentId(this.agentId);
agentInfoBoBuilder.setStartTime(startTime);
// TODO fix
agentInfoBoBuilder.setServiceType(registry.findServiceType(agentInfoBoBuilder.getServiceTypeCode()));
if (serializedServerMetaData != null) {
agentInfoBoBuilder.setServerMetaData(new ServerMetaDataBo.Builder(serializedServerMetaData).build());
}
@@ -20,6 +20,8 @@ import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.navercorp.pinpoint.common.service.ServiceTypeRegistryService;
import com.navercorp.pinpoint.common.trace.ServiceType;
import com.navercorp.pinpoint.common.util.AgentLifeCycleState;
import com.navercorp.pinpoint.web.applicationmap.link.MatcherGroup;
import com.navercorp.pinpoint.web.applicationmap.link.ServerMatcher;
@@ -42,9 +44,11 @@ public class ApplicationAgentListSerializer extends JsonSerializer<ApplicationAg
@Autowired(required = false)
private MatcherGroup matcherGroup;
@Autowired
private ServiceTypeRegistryService serviceTypeRegistryService;
@Override
public void serialize(ApplicationAgentList applicationAgentList, JsonGenerator jgen, SerializerProvider provider) throws IOException,
JsonProcessingException {
public void serialize(ApplicationAgentList applicationAgentList, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
jgen.writeStartObject();
Map<String, List<AgentInfo>> map = applicationAgentList.getApplicationAgentList();
@@ -66,7 +70,9 @@ public class ApplicationAgentListSerializer extends JsonSerializer<ApplicationAg
jgen.writeStringField("hostName", agentInfo.getHostName());
jgen.writeStringField("ip", agentInfo.getIp());
jgen.writeStringField("ports", agentInfo.getPorts());
jgen.writeStringField("serviceType", agentInfo.getServiceType().toString());
final ServiceType serviceType = serviceTypeRegistryService.findServiceType(agentInfo.getServiceTypeCode());
jgen.writeStringField("serviceType", serviceType.getDesc());
jgen.writeNumberField("pid", agentInfo.getPid());
jgen.writeStringField("vmVersion", agentInfo.getVmVersion());
jgen.writeStringField("agentVersion", agentInfo.getAgentVersion());
@@ -0,0 +1,87 @@
/*
* Copyright 2014 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.navercorp.pinpoint.web.view;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.navercorp.pinpoint.common.service.ServiceTypeRegistryService;
import com.navercorp.pinpoint.common.trace.ServiceType;
import com.navercorp.pinpoint.common.util.AgentLifeCycleState;
import com.navercorp.pinpoint.web.applicationmap.ServerInstance;
import com.navercorp.pinpoint.web.applicationmap.link.MatcherGroup;
import com.navercorp.pinpoint.web.applicationmap.link.ServerMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.IOException;
/**
* @author emeroad
* @author minwoo.jung
*/
public class ServerInstanceSerializer extends JsonSerializer<ServerInstance> {
@Autowired(required=false)
private MatcherGroup matcherGroup = new MatcherGroup();
@Autowired
private ServiceTypeRegistryService serviceTypeRegistryService;
private AgentLifeCycleStateSerializer agentLifeCycleStateSerializer = new AgentLifeCycleStateSerializer();
public void setServiceTypeRegistryService(ServiceTypeRegistryService serviceTypeRegistryService) {
this.serviceTypeRegistryService = serviceTypeRegistryService;
}
@Override
public void serialize(ServerInstance serverInstance, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
jgen.writeStartObject();
final short serviceTypeCode = serverInstance.getServiceTypeCode();
final ServiceType serviceType = serviceTypeRegistryService.findServiceType(serviceTypeCode);
jgen.writeBooleanField("hasInspector", hasInspector(serviceType));
final ServerMatcher serverMatcher = this.matcherGroup.match(serverInstance.getHostName());
jgen.writeStringField("linkName", serverMatcher.getLinkName());
jgen.writeStringField("linkURL", serverMatcher.getLink(serverInstance.getHostName()));
jgen.writeStringField("name", serverInstance.getName());
jgen.writeStringField("serviceType", serviceType.getName());
jgen.writeFieldName("status");
write(serverInstance.getStatus(), jgen, provider);
jgen.writeEndObject();
}
public void write(AgentLifeCycleState value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
agentLifeCycleStateSerializer.serialize(value, jgen, provider);
}
public boolean hasInspector(ServiceType serviceType) {
if (serviceType.isWas()) {
return true;
} else {
return false;
}
}
}
@@ -44,7 +44,7 @@ public class AgentInfo {
private String hostName;
private String ip;
private String ports;
private ServiceType serviceType;
private short serviceTypeCode;
private int pid;
private String vmVersion;
private String agentVersion;
@@ -66,7 +66,7 @@ public class AgentInfo {
this.hostName = agentInfoBo.getHostName();
this.ip = agentInfoBo.getIp();
this.ports = agentInfoBo.getPorts();
this.serviceType = agentInfoBo.getServiceType();
this.serviceTypeCode = agentInfoBo.getServiceTypeCode();
this.pid = agentInfoBo.getPid();
this.vmVersion = agentInfoBo.getVmVersion();
this.agentVersion = agentInfoBo.getAgentVersion();
@@ -121,12 +121,12 @@ public class AgentInfo {
this.ports = ports;
}
public ServiceType getServiceType() {
return serviceType;
public short getServiceTypeCode() {
return serviceTypeCode;
}
public void setServiceType(ServiceType serviceType) {
this.serviceType = serviceType;
public void setServiceTypeCode(short serviceTypeCode) {
this.serviceTypeCode = serviceTypeCode;
}
public int getPid() {
@@ -179,40 +179,29 @@ public class AgentInfo {
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((agentId == null) ? 0 : agentId.hashCode());
result = prime * result + ((serviceType == null) ? 0 : serviceType.hashCode());
int result = agentId != null ? agentId.hashCode() : 0;
result = 31 * result + (int) serviceTypeCode;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AgentInfo other = (AgentInfo)obj;
if (agentId == null) {
if (other.agentId != null)
return false;
} else if (!agentId.equals(other.agentId))
return false;
if (serviceType == null) {
if (other.serviceType != null)
return false;
} else if (!serviceType.equals(other.serviceType))
return false;
return true;
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AgentInfo agentInfo = (AgentInfo) o;
if (serviceTypeCode != agentInfo.serviceTypeCode) return false;
return !(agentId != null ? !agentId.equals(agentInfo.agentId) : agentInfo.agentId != null);
}
@Override
public String toString() {
return "AgentInfo [applicationName=" + applicationName + ", agentId=" + agentId + ", startTimestamp="
+ startTimestamp + ", hostName=" + hostName + ", ip=" + ip + ", ports=" + ports + ", serviceType="
+ serviceType + ", pid=" + pid + ", vmVersion=" + vmVersion + ", agentVersion=" + agentVersion
+ startTimestamp + ", hostName=" + hostName + ", ip=" + ip + ", ports=" + ports + ", serviceTypeCode="
+ serviceTypeCode + ", pid=" + pid + ", vmVersion=" + vmVersion + ", agentVersion=" + agentVersion
+ ", serverMetaData=" + serverMetaData + ", initialStartTimestamp=" + initialStartTimestamp
+ ", status=" + status + "]";
}
@@ -58,9 +58,6 @@ public class ServerInstanceListTest {
ServiceType serviceType = ServiceType.TEST_STAND_ALONE;
agentInfoBuilder.setServiceTypeCode(serviceType.getCode());
// TODO FIX api
agentInfoBuilder.setServiceType(serviceType);
agentInfoBuilder.setHostName(hostName);
return new AgentInfo(agentInfoBuilder.build());
@@ -17,14 +17,22 @@
package com.navercorp.pinpoint.web.view;
import java.util.HashSet;
import java.util.Set;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.cfg.HandlerInstantiator;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.jsontype.TypeIdResolver;
import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder;
import com.navercorp.pinpoint.common.service.DefaultServiceTypeRegistryService;
import com.navercorp.pinpoint.common.service.ServiceTypeRegistryService;
import com.navercorp.pinpoint.web.applicationmap.ServerInstanceListTest;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.navercorp.pinpoint.web.applicationmap.ServerBuilder;
import com.navercorp.pinpoint.web.applicationmap.ServerInstanceList;
import com.navercorp.pinpoint.web.vo.AgentInfo;
@@ -39,13 +47,11 @@ public class ServerInstanceListSerializerTest {
@Test
public void testSerialize() throws Exception {
PinpointObjectMapper mapper = new PinpointObjectMapper();
mapper.afterPropertiesSet();
PinpointObjectMapper mapper = createMapper();
AgentInfo agentInfo = ServerInstanceListTest.createAgentInfo("agentId1", "testHost");
HashSet<AgentInfo> agentInfoSet = new HashSet<AgentInfo>();
Set<AgentInfo> agentInfoSet = new HashSet<>();
agentInfoSet.add(agentInfo);
ServerBuilder builder = new ServerBuilder();
@@ -57,4 +63,50 @@ public class ServerInstanceListSerializerTest {
logger.debug(json);
}
private PinpointObjectMapper createMapper() throws Exception {
PinpointObjectMapper mapper = new PinpointObjectMapper();
// TODO FIX spring managed object
mapper.setHandlerInstantiator(new TestHandlerInstantiator());
mapper.afterPropertiesSet();
return mapper;
}
public class TestHandlerInstantiator extends HandlerInstantiator {
public TestHandlerInstantiator() {
}
public JsonSerializer<?> serializerInstance(SerializationConfig config, Annotated annotated, Class<?> keyDeserClass) {
if (annotated.getName().equals("com.navercorp.pinpoint.web.applicationmap.ServerInstance")) {
final ServiceTypeRegistryService serviceTypeRegistryService = new DefaultServiceTypeRegistryService();
final ServerInstanceSerializer serverInstanceSerializer = new ServerInstanceSerializer();
serverInstanceSerializer.setServiceTypeRegistryService(serviceTypeRegistryService);
return serverInstanceSerializer;
}
return null;
}
@Override
public JsonDeserializer<?> deserializerInstance(DeserializationConfig config, Annotated annotated, Class<?> deserClass) {
return null;
}
@Override
public KeyDeserializer keyDeserializerInstance(DeserializationConfig config, Annotated annotated, Class<?> keyDeserClass) {
return null;
}
@Override
public TypeResolverBuilder<?> typeResolverBuilderInstance(MapperConfig<?> config, Annotated annotated, Class<?> builderClass) {
return null;
}
@Override
public TypeIdResolver typeIdResolverInstance(MapperConfig<?> config, Annotated annotated, Class<?> resolverClass) {
return null;
}
}
}