Skip to content

Commit

Permalink
[#5384] Cleanup code related to cluster
Browse files Browse the repository at this point in the history
remove PinpointServerRepository class
  • Loading branch information
koo-taejin committed Mar 29, 2019
1 parent 51934dc commit 022e6ad
Show file tree
Hide file tree
Showing 11 changed files with 290 additions and 283 deletions.
@@ -0,0 +1,112 @@
/*
* Copyright 2019 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.collector.cluster;

import java.util.Objects;

/**
* @author Taejin Koo
*/
public class AgentInfo {

private final String applicationName;
private final String agentId;
private final long startTimestamp;
private final String version;

private final String agentKey;

public AgentInfo(String applicationName, String agentId, long startTimestamp) {
this(applicationName, agentId, startTimestamp, "");
}

public AgentInfo(String applicationName, String agentId, long startTimestamp, String version) {
this.applicationName = applicationName;
this.agentId = agentId;
this.startTimestamp = startTimestamp;
this.version = version;
this.agentKey = createAgentKey(applicationName, agentId, startTimestamp);
}

private String createAgentKey(String applicationName, String agentId, long startTimestamp) {
StringBuilder key = new StringBuilder();
key.append(applicationName);
key.append(":");
key.append(agentId);
key.append(":");
key.append(startTimestamp);

return key.toString();
}

public String getApplicationName() {
return applicationName;
}

public String getAgentId() {
return agentId;
}

public long getStartTimestamp() {
return startTimestamp;
}

public String getVersion() {
return version;
}

public String getAgentKey() {
return agentKey;
}

public boolean equals(String applicationName, String agentId, long startTimestamp) {
if (!this.applicationName.equals(applicationName)) {
return false;
}
if (!this.agentId.equals(agentId)) {
return false;
}
if (this.startTimestamp != startTimestamp) {
return false;
}
return true;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AgentInfo agentInfo = (AgentInfo) o;
return startTimestamp == agentInfo.startTimestamp &&
Objects.equals(applicationName, agentInfo.applicationName) &&
Objects.equals(agentId, agentInfo.agentId);
}

@Override
public int hashCode() {
return Objects.hash(applicationName, agentId, startTimestamp);
}

@Override
public String toString() {
return "AgentInfo{" +
getAgentKey() + ":"
+ version +
'}';
}

}
Expand Up @@ -24,4 +24,6 @@ public interface ClusterPoint {

Future request(byte[] data);

AgentInfo getDestAgentInfo();

}
Expand Up @@ -16,53 +16,97 @@

package com.navercorp.pinpoint.collector.cluster;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import com.navercorp.pinpoint.rpc.common.SocketStateCode;
import com.navercorp.pinpoint.rpc.server.PinpointServer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ClusterPointRepository<T extends ClusterPoint> implements ClusterPointLocator<T> {

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

private final CopyOnWriteArrayList<T> clusterPointRepository = new CopyOnWriteArrayList<>();

public boolean addClusterPoint(T clusterPoint) {
if (logger.isInfoEnabled()) {
logger.info("addClusterPoint(clusterPoint = [{}])", clusterPoint);
private final Map<String, Set<T>> clusterPointRepository = new HashMap<>();

public boolean addAndIsKeyCreated(T clusterPoint) {
AgentInfo destAgentInfo = clusterPoint.getDestAgentInfo();
String key = destAgentInfo.getAgentKey();
synchronized (this) {
final Set<T> clusterPointSet = clusterPointRepository.get(key);
if (clusterPointSet != null) {
clusterPointSet.add(clusterPoint);

return false;
} else {
Set<T> newSet = new HashSet<>();
newSet.add(clusterPoint);

clusterPointRepository.put(key, newSet);
return true;
}
}
}

boolean isAdd = clusterPointRepository.addIfAbsent(clusterPoint);

if (!isAdd) {
logger.warn("Already registered ClusterPoint({}).", clusterPoint);
public boolean removeAndGetIsKeyRemoved(T clusterPoint) {
AgentInfo destAgentInfo = clusterPoint.getDestAgentInfo();
String key = destAgentInfo.getAgentKey();
synchronized (this) {
final Set<T> clusterPointSet = clusterPointRepository.get(key);
if (clusterPointSet != null) {
clusterPointSet.remove(clusterPoint);

if (clusterPointSet.isEmpty()) {
clusterPointRepository.remove(key);
return true;
}
}
return false;
}

return isAdd;
}

public boolean removeClusterPoint(T clusterPoint) {
if (logger.isInfoEnabled()) {
logger.info("removeClusterPoint(clusterPoint = [{}])", clusterPoint);
}
public List<T> getClusterPointList() {
synchronized (this) {
List<T> clusterPointList = new ArrayList<>(clusterPointRepository.size());

boolean isRemove = clusterPointRepository.remove(clusterPoint);
for (Set<T> eachKeysValue : clusterPointRepository.values()) {
clusterPointList.addAll(eachKeysValue);
}

if (!isRemove) {
logger.warn("Already unregistered or not registered ClusterPoint({}).", clusterPoint);
return clusterPointList;
}

return isRemove;
}

public List<T> getClusterPointList() {
return new ArrayList<>(clusterPointRepository);
public Set<String> getAvailableAgentKeyList() {
synchronized (this) {
Set<String> availableAgentKeySet = new HashSet<>(clusterPointRepository.size());

Set<String> keySet = clusterPointRepository.keySet();
for (String key : keySet) {
Set<T> clusterPointSet = clusterPointRepository.get(key);
for (T clusterPoint : clusterPointSet) {
if (clusterPoint instanceof PinpointServerClusterPoint) {
PinpointServer pinpointServer = ((PinpointServerClusterPoint) clusterPoint).getPinpointServer();
if (SocketStateCode.isRunDuplex(pinpointServer.getCurrentStateCode())) {
availableAgentKeySet.add(key);
}
}
}

}
return availableAgentKeySet;
}
}

public void clear() {

synchronized (this) {
clusterPointRepository.clear();
}
}

}
Expand Up @@ -39,31 +39,30 @@ public class PinpointServerClusterPoint implements TargetClusterPoint {

private final PinpointServer pinpointServer;

private final String applicationName;
private final String agentId;
private final long startTimeStamp;
private final AgentInfo agentInfo;

private final String version;
private final List<TCommandType> supportCommandList;

public PinpointServerClusterPoint(PinpointServer pinpointServer) {
Assert.requireNonNull(pinpointServer, "pinpointServer must not be null.");
this.pinpointServer = pinpointServer;

Map<Object, Object> properties = pinpointServer.getChannelProperties();
this.version = MapUtils.getString(properties, HandshakePropertyType.VERSION.getName());
Assert.isTrue(!StringUtils.isBlank(version), "Version must not be null or empty.");

this.supportCommandList = newSupportCommandList(properties);

this.applicationName = MapUtils.getString(properties, HandshakePropertyType.APPLICATION_NAME.getName());
String applicationName = MapUtils.getString(properties, HandshakePropertyType.APPLICATION_NAME.getName());
Assert.isTrue(!StringUtils.isBlank(applicationName), "ApplicationName must not be null or empty.");

this.agentId = MapUtils.getString(properties, HandshakePropertyType.AGENT_ID.getName());
String agentId = MapUtils.getString(properties, HandshakePropertyType.AGENT_ID.getName());
Assert.isTrue(!StringUtils.isBlank(agentId), "AgentId must not be null or empty.");

this.startTimeStamp = MapUtils.getLong(properties, HandshakePropertyType.START_TIMESTAMP.getName());
long startTimeStamp = MapUtils.getLong(properties, HandshakePropertyType.START_TIMESTAMP.getName());
Assert.isTrue(startTimeStamp > 0, "StartTimeStamp is must greater than zero.");

String version = MapUtils.getString(properties, HandshakePropertyType.VERSION.getName());
Assert.isTrue(!StringUtils.isBlank(version), "Version must not be null or empty.");

this.agentInfo = new AgentInfo(applicationName, agentId, startTimeStamp, version);
this.supportCommandList = newSupportCommandList(properties);
}

private List<TCommandType> newSupportCommandList(Map<Object, Object> properties) {
Expand Down Expand Up @@ -96,22 +95,8 @@ public Future request(byte[] payload) {
}

@Override
public String getApplicationName() {
return applicationName;
}

@Override
public String getAgentId() {
return agentId;
}

public long getStartTimeStamp() {
return startTimeStamp;
}

@Override
public String gerVersion() {
return version;
public AgentInfo getDestAgentInfo() {
return agentInfo;
}

@Override
Expand All @@ -122,7 +107,7 @@ public boolean isSupportCommand(TBase command) {
}
}

TCommandTypeVersion commandVersion = TCommandTypeVersion.getVersion(version);
TCommandTypeVersion commandVersion = TCommandTypeVersion.getVersion(agentInfo.getVersion());
if (commandVersion.isSupportCommand(command)) {
return true;
}
Expand All @@ -139,34 +124,21 @@ public String toString() {
StringBuilder log = new StringBuilder(32);
log.append(this.getClass().getSimpleName());
log.append("(");
log.append(applicationName);
log.append("/");
log.append(agentId);
log.append("/");
log.append(startTimeStamp);
log.append(")");
log.append(", version:");
log.append(version);
log.append(agentInfo.toString());
log.append(", supportCommandList:");
log.append(supportCommandList);
log.append(", pinpointServer:");
log.append(pinpointServer);
log.append(")");

return log.toString();
}

@Override
public int hashCode() {
final int prime = 31;
int result = 17;

result = prime * result + ((applicationName == null) ? 0 : applicationName.hashCode());
result = prime * result + ((agentId == null) ? 0 : agentId.hashCode());
result = prime * result + (int) (startTimeStamp ^ (startTimeStamp >>> 32));
result = prime * result + ((version == null) ? 0 : version.hashCode());
return result;
return agentInfo.hashCode();
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
Expand Down
Expand Up @@ -20,14 +20,6 @@

public interface TargetClusterPoint extends ClusterPoint {

String getApplicationName();

String getAgentId();

long getStartTimeStamp();

String gerVersion();

boolean isSupportCommand(TBase command);

}

0 comments on commit 022e6ad

Please sign in to comment.