Skip to content

Commit

Permalink
[no issue] code cleanup
Browse files Browse the repository at this point in the history
1. Use the constructor to change the object creation to use the factory and builder (AgentActiveThreadDump, AgentActiveThreadCount).
2. Changes some variable names.
  • Loading branch information
koo-taejin committed Jan 3, 2017
1 parent 32171c5 commit 19c6fa9
Show file tree
Hide file tree
Showing 15 changed files with 703 additions and 257 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@
import com.navercorp.pinpoint.web.cluster.PinpointRouteResponse;
import com.navercorp.pinpoint.web.service.AgentService;
import com.navercorp.pinpoint.web.vo.AgentActiveThreadDump;
import com.navercorp.pinpoint.web.vo.AgentActiveThreadDumpFactory;
import com.navercorp.pinpoint.web.vo.AgentActiveThreadDumpList;
import com.navercorp.pinpoint.web.vo.AgentInfo;
import org.apache.thrift.TBase;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -54,6 +57,8 @@
@RequestMapping("/agent")
public class AgentCommandController {

private final Logger logger = LoggerFactory.getLogger(this.getClass());

@Autowired
private SerializerFactory<HeaderTBaseSerializer> commandSerializerFactory;

Expand Down Expand Up @@ -96,8 +101,14 @@ public ModelAndView getActiveThreadDump(@RequestParam(value = "applicationName")
AgentActiveThreadDumpList activeThreadDumpList = new AgentActiveThreadDumpList(activeThreadDumpResponse.getThreadDumpsSize());
List<TActiveThreadDump> activeThreadDumps = activeThreadDumpResponse.getThreadDumps();
if (activeThreadDumps != null) {
AgentActiveThreadDumpFactory factory = new AgentActiveThreadDumpFactory();
for (TActiveThreadDump activeThreadDump : activeThreadDumps) {
activeThreadDumpList.add(new AgentActiveThreadDump(activeThreadDump));
try {
AgentActiveThreadDump agentActiveThreadDump = factory.create(activeThreadDump);
activeThreadDumpList.add(agentActiveThreadDump);
} catch (Exception e) {
logger.warn("create AgentActiveThreadDump fail. arguments(TActiveThreadDump:{})", activeThreadDump);
}
}
}

Expand Down Expand Up @@ -151,8 +162,14 @@ public ModelAndView getActiveThreadLightDump(@RequestParam(value = "applicationN
AgentActiveThreadDumpList activeThreadDumpList = new AgentActiveThreadDumpList(activeThreadDumpResponse.getThreadDumpsSize());
List<TActiveThreadLightDump> activeThreadDumps = activeThreadDumpResponse.getThreadDumps();
if (activeThreadDumps != null) {
AgentActiveThreadDumpFactory factory = new AgentActiveThreadDumpFactory();
for (TActiveThreadLightDump activeThreadDump : activeThreadDumps) {
activeThreadDumpList.add(new AgentActiveThreadDump(activeThreadDump));
try {
AgentActiveThreadDump agentActiveThreadDump = factory.create(activeThreadDump);
activeThreadDumpList.add(agentActiveThreadDump);
} catch (Exception e) {
logger.warn("create AgentActiveThreadDump fail. arguments(TActiveThreadDump:{})", activeThreadDump);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.navercorp.pinpoint.web.cluster.FailedPinpointRouteResponse;
import com.navercorp.pinpoint.web.cluster.PinpointRouteResponse;
import com.navercorp.pinpoint.web.vo.AgentActiveThreadCount;
import com.navercorp.pinpoint.web.vo.AgentActiveThreadCountFactory;
import com.navercorp.pinpoint.web.vo.AgentActiveThreadCountList;
import com.navercorp.pinpoint.web.vo.AgentInfo;
import org.apache.thrift.TBase;
Expand Down Expand Up @@ -280,24 +281,31 @@ public AgentActiveThreadCountList getActiveThreadCount(List<AgentInfo> agentInfo
@Override
public AgentActiveThreadCountList getActiveThreadCount(List<AgentInfo> agentInfoList, byte[] payload)
throws TException {
AgentActiveThreadCountList agentActiveThreadStatusList = new AgentActiveThreadCountList(agentInfoList.size());
AgentActiveThreadCountList activeThreadCountList = new AgentActiveThreadCountList(agentInfoList.size());

Map<AgentInfo, PinpointRouteResponse> responseList = invoke(agentInfoList, payload);
for (Map.Entry<AgentInfo, PinpointRouteResponse> entry : responseList.entrySet()) {
AgentInfo agentInfo = entry.getKey();
PinpointRouteResponse response = entry.getValue();

AgentActiveThreadCount agentActiveThreadStatus = new AgentActiveThreadCount(agentInfo.getAgentId());
TRouteResult routeResult = response.getRouteResult();
if (routeResult == TRouteResult.OK) {
agentActiveThreadStatus.setResult(response.getResponse(TCmdActiveThreadCountRes.class, null));
} else {
agentActiveThreadStatus.setFail(routeResult.name());
}
agentActiveThreadStatusList.add(agentActiveThreadStatus);
AgentActiveThreadCount activeThreadCount = createActiveThreadCount(agentInfo.getAgentId(), response);
activeThreadCountList.add(activeThreadCount);
}

return agentActiveThreadStatusList;
return activeThreadCountList;
}

private AgentActiveThreadCount createActiveThreadCount(String agentId, PinpointRouteResponse response) {
TRouteResult routeResult = response.getRouteResult();
if (routeResult == TRouteResult.OK) {
AgentActiveThreadCountFactory factory = new AgentActiveThreadCountFactory();
factory.setAgentId(agentId);
return factory.create(response.getResponse(TCmdActiveThreadCountRes.class, null));
} else {
AgentActiveThreadCountFactory factory = new AgentActiveThreadCountFactory();
factory.setAgentId(agentId);
return factory.createFail(routeResult.name());
}
}

private TCommandTransfer createCommandTransferObject(AgentInfo agentInfo, byte[] payload) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2017 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.util.CollectionUtils;
import com.navercorp.pinpoint.web.vo.AgentActiveThreadCount;
import com.navercorp.pinpoint.web.vo.AgentActiveThreadCountList;

import java.io.IOException;
import java.util.List;

/**
* @author Taejin Koo
*/
public class AgentActiveThreadCountListSerializer extends JsonSerializer<AgentActiveThreadCountList> {

@Override
public void serialize(AgentActiveThreadCountList agentActiveThreadStatusList, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
List<AgentActiveThreadCount> agentActiveThreadRepository = agentActiveThreadStatusList.getAgentActiveThreadRepository();

jgen.writeStartObject();

for (AgentActiveThreadCount agentActiveThread : agentActiveThreadRepository) {
jgen.writeFieldName(agentActiveThread.getAgentId());
jgen.writeStartObject();

jgen.writeNumberField("code", agentActiveThread.getCode());
jgen.writeStringField("message", agentActiveThread.getCodeMessage());

List<Integer> activeThreadCountList = agentActiveThread.getActiveThreadCountList();
if (CollectionUtils.nullSafeSize(activeThreadCountList) == 4) {
jgen.writeFieldName("status");
jgen.writeStartArray();
jgen.writeNumber(activeThreadCountList.get(0));
jgen.writeNumber(activeThreadCountList.get(1));
jgen.writeNumber(activeThreadCountList.get(2));
jgen.writeNumber(activeThreadCountList.get(3));
jgen.writeEndArray();
}

jgen.writeEndObject();
}

jgen.writeEndObject();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,68 +16,108 @@

package com.navercorp.pinpoint.web.vo;

import com.navercorp.pinpoint.thrift.dto.command.TCmdActiveThreadCountRes;
import java.util.List;

/**
* @Author Taejin Koo
*/
public class AgentActiveThreadCount {

private static final short OK_CODE = 0;
private static final String OK_CODE_MESSAGE = "OK";

private final String agentId;
private final List<Integer> activeThreadCountList;
private final Status status;

private short code = -1;
private String codeMessage = "UNKNOWN";

private TCmdActiveThreadCountRes activeThreadCount;

public AgentActiveThreadCount(String agentId) {
this.agentId = agentId;
}

public void setResult(TCmdActiveThreadCountRes activeThreadCount) {
if (activeThreadCount != null) {
this.activeThreadCount = activeThreadCount;
this.code = OK_CODE;
this.codeMessage = OK_CODE_MESSAGE;
}
}

public void setFail(String codeMessage) {
setFail((short) -1, codeMessage);
}

public void setFail(short code, String codeMessage) {
this.code = code;
this.codeMessage = codeMessage;
private AgentActiveThreadCount(Builder builder) {
this.agentId = builder.agentId;
this.activeThreadCountList = builder.activeThreadCountList;
this.status = builder.status;
}

public String getAgentId() {
return agentId;
}

public short getCode() {
return code;
public List<Integer> getActiveThreadCountList() {
return activeThreadCountList;
}

public String getCodeMessage() {
return codeMessage;
public short getCode() {
return status.code;
}

public TCmdActiveThreadCountRes getActiveThreadCount() {
return activeThreadCount;
public String getCodeMessage() {
return status.codeMessage;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("AgentActiveThreadCount{");
sb.append("agentId='").append(agentId).append('\'');
sb.append(", code=").append(code);
sb.append(", codeMessage='").append(codeMessage).append('\'');
sb.append(", activeThreadCount=").append(activeThreadCount);
sb.append(", activeThreadCountList=").append(activeThreadCountList);
sb.append(", code=").append(status.code);
sb.append(", codeMessage='").append(status.codeMessage).append('\'');
sb.append('}');
return sb.toString();
}

static class Builder {

static final Status SUCCESS_STATUS = new Status((short) 0, "OK");
static final Status UNKNOWN_STATUS = new Status((short) -1, "UNKNOWN");

private String agentId;
private List<Integer> activeThreadCountList;
private Status status;

Builder() {
}

Builder setAgentId(String agentId) {
this.agentId = agentId;
return this;
}

Builder setStatus(short code, String codeMessage) {
this.status = new Status(code, codeMessage);
return this;
}

Builder setActiveThreadCountList(List<Integer> activeThreadCountList) {
this.activeThreadCountList = activeThreadCountList;
return this;
}

Builder setStatus(Status status) {
this.status = status;
return this;
}

AgentActiveThreadCount build() {
if (agentId == null) {
throw new NullPointerException("agentId may not be null");
}
if (activeThreadCountList == null) {
throw new NullPointerException("activeThreadCountList may not be null");
}
if (status == null) {
throw new NullPointerException("status may not be null");
}

return new AgentActiveThreadCount(this);
}

}

static class Status {

private final short code;
private final String codeMessage;

Status(short code, String codeMessage) {
this.code = code;
this.codeMessage = codeMessage;
}

}

}
Loading

0 comments on commit 19c6fa9

Please sign in to comment.