Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#9932] Remove thrift dependency of Tools module #9996

Merged
merged 1 commit into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 2 additions & 47 deletions tools/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,11 @@
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-bootstrap-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-thrift</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
</dependency>
<!-- Logging dependencies -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>

</dependencies>

<build>
<plugins>
<!-- any other plugins -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</plugin>
</plugins>

</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,125 +16,96 @@

package com.navercorp.pinpoint.tools;

import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig;
import com.navercorp.pinpoint.bootstrap.config.ProfilerConfigLoader;
import com.navercorp.pinpoint.bootstrap.config.Profiles;
import com.navercorp.pinpoint.common.util.PropertyUtils;
import com.navercorp.pinpoint.thrift.io.HeaderTBaseSerializer;
import com.navercorp.pinpoint.thrift.io.HeaderTBaseSerializerFactory;
import com.navercorp.pinpoint.thrift.io.NetworkAvailabilityCheckPacket;
import com.navercorp.pinpoint.tools.network.NetworkChecker;
import com.navercorp.pinpoint.tools.network.TCPChecker;
import com.navercorp.pinpoint.tools.network.UDPChecker;
import com.navercorp.pinpoint.tools.network.grpc.GrpcTransportConfig;
import com.navercorp.pinpoint.tools.network.thrift.ThriftTransportConfig;
import org.apache.thrift.TException;
import com.navercorp.pinpoint.tools.utils.Logger;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Properties;

/**
* @author netspider
*/
public class NetworkAvailabilityChecker {

private static final String SEPARATOR = File.separator;
public static final String ACTIVE_PROFILE_KEY = "pinpoint.profiler.profiles.active";
public static final String CONFIG_FILE_NAME = "pinpoint-root.config";
public static final String PROFILE_CONFIG_FILE_NAME = "pinpoint.config";

private static final Logger logger = new Logger();

public static void main(String[] args) {
if (args.length != 1) {
System.out.println("usage : " + NetworkAvailabilityChecker.class.getSimpleName() + " AGENT_CONFIG_FILE");
logger.info("usage : " + NetworkAvailabilityChecker.class.getSimpleName() + " AGENT_CONFIG_FILE");
return;
}

String configPath = args[0];
Path filePath = Paths.get(configPath);

final Properties defaultProperties = new Properties();
loadFileProperties(defaultProperties, configPath);
loadFileProperties(defaultProperties, filePath);

File file = new File(configPath);
String path = file.getAbsoluteFile().getParent();
Path path = filePath.toAbsolutePath().getParent();

if (configPath.contains(Profiles.CONFIG_FILE_NAME)) {
if (configPath.contains(CONFIG_FILE_NAME)) {
// 2. load profile
final String activeProfile = getActiveProfile(defaultProperties);
System.out.println("Active profile : " + activeProfile);
logger.info("Active profile : " + activeProfile);

if (activeProfile == null) {
System.out.println("Could not find activeProfile : null");
logger.info("Could not find activeProfile : null");
return;
}

final File activeProfileConfigFile = new File(path, "profiles" + SEPARATOR + activeProfile + SEPARATOR + Profiles.PROFILE_CONFIG_FILE_NAME);
loadFileProperties(defaultProperties, activeProfileConfigFile.getAbsolutePath());
}

final ProfilerConfig profilerConfig = ProfilerConfigLoader.load(defaultProperties);
if (profilerConfig.getTransportModule().toString().equals("GRPC")) {

System.out.println("Transport Module set to GRPC");

GrpcTransportConfig grpcTransportConfig = new GrpcTransportConfig();
grpcTransportConfig.read(defaultProperties);
final Path activeProfileConfigFile = Paths.get(path.toString(), "profiles", activeProfile, PROFILE_CONFIG_FILE_NAME);

try {
checkGRPCBase(grpcTransportConfig);
} catch (Exception e) {
e.printStackTrace();
}
loadFileProperties(defaultProperties, activeProfileConfigFile.toAbsolutePath());
}

try {
checkGRPCMeta(grpcTransportConfig);
} catch (Exception e) {
e.printStackTrace();
}
logger.info("Transport Module set to GRPC");

try {
checkGRPCStat(grpcTransportConfig);
} catch (Exception e) {
e.printStackTrace();
}
GrpcTransportConfig grpcTransportConfig = new GrpcTransportConfig(defaultProperties);

try {
checkGRPCSpan(grpcTransportConfig);
} catch (Exception e) {
e.printStackTrace();
}

} else {
try {
checkGRPCBase(grpcTransportConfig);
} catch (Exception e) {
e.printStackTrace();
}

System.out.println("Transport Module set to THRIFT");
ThriftTransportConfig thriftTransportConfig = new ThriftTransportConfig();
thriftTransportConfig.read(defaultProperties);
try {
checkUDPStat(thriftTransportConfig);
} catch (Exception e) {
e.printStackTrace();
}
try {
checkGRPCMeta(grpcTransportConfig);
} catch (Exception e) {
e.printStackTrace();
}

try {
checkUDPSpan(thriftTransportConfig);
} catch (Exception e) {
e.printStackTrace();
}
try {
checkGRPCStat(grpcTransportConfig);
} catch (Exception e) {
e.printStackTrace();
}

try {
checkTCP(thriftTransportConfig);
} catch (Exception e) {
e.printStackTrace();
}
try {
checkGRPCSpan(grpcTransportConfig);
} catch (Exception e) {
e.printStackTrace();
}

}

private static String getActiveProfile(Properties defaultProperties) {
return defaultProperties.getProperty(Profiles.ACTIVE_PROFILE_KEY, "release");
return defaultProperties.getProperty(ACTIVE_PROFILE_KEY, "release");
}

private static void loadFileProperties(Properties properties, String filePath) {
private static void loadFileProperties(Properties properties, Path filePath) {
try {
PropertyUtils.loadProperty(properties, Paths.get(filePath));
InputStream inputStream = Files.newInputStream(filePath);
properties.load(inputStream);
} catch (IOException e) {
throw new IllegalStateException(String.format("%s load fail Caused by:%s", filePath, e.getMessage()), e);
}
Expand Down Expand Up @@ -172,38 +143,4 @@ private static void checkGRPCSpan(GrpcTransportConfig grpcTransportConfig) throw
checker.check();
}

private static void checkUDPStat(ThriftTransportConfig profilerConfig) throws Exception {
String ip = profilerConfig.getCollectorStatServerIp();
int port = profilerConfig.getCollectorStatServerPort();

NetworkChecker checker = new UDPChecker("UDP-STAT", ip, port);
checker.check(getNetworkCheckPayload(), getNetworkCheckResponsePayload());
}


private static void checkUDPSpan(ThriftTransportConfig profilerConfig) throws Exception {
String ip = profilerConfig.getCollectorSpanServerIp();
int port = profilerConfig.getCollectorSpanServerPort();

NetworkChecker checker = new UDPChecker("UDP-SPAN", ip, port);
checker.check(getNetworkCheckPayload(), getNetworkCheckResponsePayload());
}

private static void checkTCP(ThriftTransportConfig profilerConfig) throws Exception {
String ip = profilerConfig.getCollectorTcpServerIp();
int port = profilerConfig.getCollectorTcpServerPort();

NetworkChecker checker = new TCPChecker("TCP", ip, port);
checker.check();
}

private static byte[] getNetworkCheckPayload() throws TException {
HeaderTBaseSerializer serializer = new HeaderTBaseSerializerFactory(65535).createSerializer();
return serializer.serialize(new NetworkAvailabilityCheckPacket());
}

private static byte[] getNetworkCheckResponsePayload() {
return Arrays.copyOf(NetworkAvailabilityCheckPacket.DATA_OK, NetworkAvailabilityCheckPacket.DATA_OK.length);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.navercorp.pinpoint.tools.network;

import com.navercorp.pinpoint.tools.utils.HostResolver;
import com.navercorp.pinpoint.tools.utils.Logger;

import java.io.IOException;
import java.net.InetAddress;
Expand All @@ -16,6 +17,8 @@ public abstract class AbstractNetworkChecker implements NetworkChecker {
private static final String WHITE_SPACE = " "; // 4space
private static final String LINE_SEPARATOR = System.lineSeparator();

private final Logger logger = new Logger();

private final String testName;

private final InetSocketAddress hostAddress;
Expand Down Expand Up @@ -43,8 +46,7 @@ public void check() throws IOException {
report.append(createReport(ipAddress, check));
}

System.out.println(report);

logger.info(report);
}

@Override
Expand All @@ -59,7 +61,7 @@ public void check(byte[] requestData, byte[] expectedResponseData) throws IOExce
report.append(createReport(ipAddress, check));
}

System.out.println(report);
logger.info(report);
}

private String getHostName(InetSocketAddress hostAddress) {
Expand All @@ -70,10 +72,9 @@ private String createReport(InetSocketAddress socketAddress, boolean check) {
String ip = getIp(socketAddress, socketAddress.getHostName());
int port = socketAddress.getPort();

StringBuilder report = new StringBuilder();
report.append(WHITE_SPACE).append("=> ").append(ip).append(":").append(port);
report.append(" [").append(check ? "SUCCESS" : "FAIL").append("]").append(LINE_SEPARATOR);
return report.toString();
String report = WHITE_SPACE + "=> " + ip + ":" + port +
" [" + (check ? "SUCCESS" : "FAIL") + "]" + LINE_SEPARATOR;
return report;
}

protected String getIp(InetSocketAddress socketAddress, String defaultValue) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.navercorp.pinpoint.tools.network;

import com.navercorp.pinpoint.tools.utils.IOUtils;
import com.navercorp.pinpoint.tools.utils.Logger;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -15,6 +16,8 @@
*/
public class TCPChecker extends AbstractNetworkChecker {

private static final Logger logger = new Logger();

public TCPChecker(String testName, String hostName, int port) throws UnknownHostException {
this(testName, new InetSocketAddress(hostName, port));
}
Expand All @@ -29,7 +32,7 @@ protected boolean check(InetSocketAddress address) throws IOException {
try (Socket socket = createSocket(address)) {
return socket.isConnected();
} catch (IOException e) {
e.printStackTrace();
logger.info(e);
}
return false;
}
Expand All @@ -43,17 +46,16 @@ protected boolean check(InetSocketAddress address, byte[] requestData, byte[] ex

return Arrays.equals(expectedResponseData, responseData);
} catch (IOException e) {
e.printStackTrace();
logger.info(e);
}
return false;
}

private Socket createSocket(InetSocketAddress socketAddress) throws IOException {
try (Socket socket = new Socket()) {
socket.setSoTimeout(3000);
socket.connect(socketAddress);
return socket;
}
Socket socket = new Socket();
socket.setSoTimeout(3000);
socket.connect(socketAddress);
return socket;
}

private void write(Socket socket, byte[] requestData) throws IOException {
Expand Down
Loading