#4 Profiler - Collector간의 컨트롤 메시지 생성

1) 클라이언트 상태 코드 변경
2) Agent에 의존적인 코드 삭제 
3) 테스트 코드 변경
This commit is contained in:
koo-taejin
2014-08-19 11:28:08 +09:00
parent 8b28fc2ca8
commit d31845e23e
7 changed files with 72 additions and 84 deletions
@@ -204,7 +204,7 @@ public class PinpointSocketHandler extends SimpleChannelHandler implements Socke
return;
}
if (state.getState() == State.RUN_WITHOUT_REGISTER) {
if (state.getState() == State.RUN) {
sendEnableWorkerPacket();
reservationEnableWorkerPacketJob(this);
}
@@ -18,8 +18,8 @@ public class State {
// 0 핸드쉐이크 안함.. 1은 동작중, 2는 closed
public static final int INIT_RECONNECT = -1;
public static final int INIT = 0;
public static final int RUN_WITHOUT_REGISTER = 1;
public static final int RUN = 2;
public static final int RUN = 1;
public static final int RUN_DUPLEX_COMMUNICATION = 2;
public static final int CLOSED = 3;
// 이 상태가 있어야 되나?
public static final int RECONNECT = 4;
@@ -33,11 +33,11 @@ public class State {
public boolean isRun() {
int code = state.get();
return code == RUN_WITHOUT_REGISTER || code == RUN;
return code == RUN || code == RUN_DUPLEX_COMMUNICATION;
}
public boolean isRun(int code) {
return code == RUN_WITHOUT_REGISTER || code == RUN;
return code == RUN || code == RUN_DUPLEX_COMMUNICATION;
}
public boolean isClosed() {
@@ -45,29 +45,29 @@ public class State {
}
public boolean changeRunWithoutRegister() {
logger.debug("State Will Be Changed {}.", getString(RUN_WITHOUT_REGISTER));
final int current = state.get();
if (current == INIT) {
return this.state.compareAndSet(INIT, RUN_WITHOUT_REGISTER);
} else if(current == INIT_RECONNECT) {
return this.state.compareAndSet(INIT_RECONNECT, RUN_WITHOUT_REGISTER);
}
throw new IllegalStateException("InvalidState current:" + getString(current) + " change:" + getString(RUN_WITHOUT_REGISTER));
}
public boolean changeRun() {
logger.debug("State Will Be Changed {}.", getString(RUN));
final int current = state.get();
if (current == INIT) {
return this.state.compareAndSet(INIT, RUN);
} else if(current == INIT_RECONNECT) {
return this.state.compareAndSet(INIT_RECONNECT, RUN);
} else if (current == RUN_WITHOUT_REGISTER) {
return this.state.compareAndSet(RUN_WITHOUT_REGISTER, RUN);
}
throw new IllegalStateException("InvalidState current:" + getString(current) + " change:" + getString(RUN));
}
public boolean changeRun() {
logger.debug("State Will Be Changed {}.", getString(RUN_DUPLEX_COMMUNICATION));
final int current = state.get();
if (current == INIT) {
return this.state.compareAndSet(INIT, RUN_DUPLEX_COMMUNICATION);
} else if(current == INIT_RECONNECT) {
return this.state.compareAndSet(INIT_RECONNECT, RUN_DUPLEX_COMMUNICATION);
} else if (current == RUN) {
return this.state.compareAndSet(RUN, RUN_DUPLEX_COMMUNICATION);
}
throw new IllegalStateException("InvalidState current:" + getString(current) + " change:" + getString(RUN_DUPLEX_COMMUNICATION));
}
public boolean changeClosed(int before) {
logger.debug("State Will Be Changed {} -> {}.", getString(before), getString(CLOSED));
return this.state.compareAndSet(before, CLOSED);
@@ -75,7 +75,7 @@ public class State {
public boolean changeClosed() {
logger.debug("State Will Be Changed {}.", getString(CLOSED));
return this.state.compareAndSet(RUN_WITHOUT_REGISTER, CLOSED);
return this.state.compareAndSet(RUN, CLOSED);
}
public void setClosed() {
@@ -91,10 +91,10 @@ public class State {
switch (stateCode) {
case INIT:
return "INIT";
case RUN_WITHOUT_REGISTER:
return "RUN_WITHOUT_REGISTER";
case RUN:
return "RUN";
case RUN_DUPLEX_COMMUNICATION:
return "RUN_DUPLEX_COMMUNICATION";
case CLOSED:
return "CLOSED";
case RECONNECT:
@@ -7,7 +7,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.nhn.pinpoint.rpc.util.CopyUtils;
import com.nhn.pinpoint.rpc.util.MapUtils;
public class ChannelContext {
@@ -97,10 +96,6 @@ public class ChannelContext {
stateChangeEventListener.eventPerformed(this, PinpointServerSocketStateCode.ERROR_UNKOWN);
}
}
public String getVersion() {
return MapUtils.get(channelProperties, AgentPropertiesType.VERSION.getName(), String.class, "UNKNOWN");
}
public Map getChannelProperties() {
return channelProperties;
@@ -3,7 +3,6 @@ package com.nhn.pinpoint.rpc.server;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -556,54 +555,5 @@ public class PinpointServerSocket extends SimpleChannelHandler {
return channelContextList;
}
public ChannelContext getDuplexChannelContext(String applicationName, String agentId, long startTimeMillis) {
if (applicationName == null) {
return null;
}
if (agentId == null) {
return null;
}
if (startTimeMillis <= 0) {
return null;
}
List<ChannelContext> channelContextList = new ArrayList<ChannelContext>();
for (Channel channel : channelGroup) {
ChannelContext context = getChannelContext(channel);
if (context.getCurrentStateCode() == PinpointServerSocketStateCode.RUN_DUPLEX_COMMUNICATION) {
Map agentProperties = context.getChannelProperties();
if (!applicationName.equals(agentProperties.get(AgentPropertiesType.APPLICATION_NAME.getName()))) {
continue;
}
if (!agentId.equals(agentProperties.get(AgentPropertiesType.AGENT_ID.getName()))) {
continue;
}
if (startTimeMillis != (Long) agentProperties.get(AgentPropertiesType.START_TIMESTAMP.getName())) {
continue;
}
channelContextList.add(context);
}
}
if (channelContextList.size() == 0) {
return null;
}
if (channelContextList.size() == 1) {
return channelContextList.get(0);
} else {
logger.warn("Ambiguous Channel Context {}, {}, {} (Valid Agent list={}).", applicationName, agentId, startTimeMillis, channelContextList);
return null;
}
}
}
@@ -4,9 +4,6 @@ import java.util.Map;
import com.nhn.pinpoint.rpc.util.ClassUtils;
/**
* @author koo.taejin
*/
public class AgentProperties {
public static final String KEY_HOSTNAME = "hostName";
@@ -4,9 +4,6 @@ import java.util.Map;
import com.nhn.pinpoint.rpc.util.ClassUtils;
/**
* @author koo.taejin
*/
public enum AgentPropertiesType {
HOSTNAME("hostName", String.class),
@@ -150,10 +150,10 @@ public class MessageListenerTest {
Thread.sleep(500);
ChannelContext channelContext = ss.getDuplexChannelContext("application", "agent", (Long) params.get(AgentPropertiesType.START_TIMESTAMP.getName()));
ChannelContext channelContext = getChannelContext("application", "agent", (Long) params.get(AgentPropertiesType.START_TIMESTAMP.getName()), ss.getDuplexCommunicationChannelContext());
Assert.assertNotNull(channelContext);
channelContext = ss.getDuplexChannelContext("application", "agent", (Long) params.get(AgentPropertiesType.START_TIMESTAMP.getName()) + 1);
channelContext = getChannelContext("application", "agent", (Long) params.get(AgentPropertiesType.START_TIMESTAMP.getName()) + 1, ss.getDuplexCommunicationChannelContext());
Assert.assertNull(channelContext);
socket.close();
@@ -221,5 +221,54 @@ public class MessageListenerTest {
}
}
private ChannelContext getChannelContext(String applicationName, String agentId, long startTimeMillis, List<ChannelContext> duplexChannelContextList) {
if (applicationName == null) {
return null;
}
if (agentId == null) {
return null;
}
if (startTimeMillis <= 0) {
return null;
}
List<ChannelContext> channelContextList = new ArrayList<ChannelContext>();
for (ChannelContext eachContext : duplexChannelContextList) {
if (eachContext.getCurrentStateCode() == PinpointServerSocketStateCode.RUN_DUPLEX_COMMUNICATION) {
Map agentProperties = eachContext.getChannelProperties();
if (!applicationName.equals(agentProperties.get(AgentPropertiesType.APPLICATION_NAME.getName()))) {
continue;
}
if (!agentId.equals(agentProperties.get(AgentPropertiesType.AGENT_ID.getName()))) {
continue;
}
if (startTimeMillis != (Long) agentProperties.get(AgentPropertiesType.START_TIMESTAMP.getName())) {
continue;
}
channelContextList.add(eachContext);
}
}
if (channelContextList.size() == 0) {
return null;
}
if (channelContextList.size() == 1) {
return channelContextList.get(0);
} else {
logger.warn("Ambiguous Channel Context {}, {}, {} (Valid Agent list={}).", applicationName, agentId, startTimeMillis, channelContextList);
return null;
}
}
}