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

add arthas-grpc-services, support ObjectService #2698

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
145 changes: 145 additions & 0 deletions arthas-grpc-services/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>arthas-all</artifactId>
<groupId>com.taobao.arthas</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>arthas-grpc-services</artifactId>
<name>arthas-grpc-services</name>
<url>https://github.com/alibaba/arthas</url>
<properties>
<java.version>1.8</java.version>
<grpc.version>1.46.0</grpc.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-bom</artifactId>
<version>${grpc.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>


<dependencies>
<dependency>
<groupId>com.taobao.arthas</groupId>
<artifactId>arthas-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.taobao.arthas</groupId>
<artifactId>arthas-vmtool</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-codec-http</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.arthas</groupId>
<artifactId>arthas-repackage-logger</artifactId>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-services</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
<profile>
<id>mac</id>
<activation>
<os>
<family>mac</family>
</os>
</activation>
<properties>
<os.detected.classifier>osx-x86_64</os.detected.classifier>
</properties>
</profile>
</profiles>
<build>
<finalName>${project.artifactId}</finalName>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.2</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protoSourceRoot>${basedir}/src/main/proto</protoSourceRoot>
<protocArtifact>com.google.protobuf:protoc:3.11.0:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.28.0:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package io.arthas.services;


import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import io.arthas.api.ArthasServices.ArrayElement;
import io.arthas.api.ArthasServices.ArrayValue;
import io.arthas.api.ArthasServices.BasicValue;
import io.arthas.api.ArthasServices.CollectionValue;
import io.arthas.api.ArthasServices.CollectionValue.Builder;
import io.arthas.api.ArthasServices.JavaField;
import io.arthas.api.ArthasServices.JavaFields;
import io.arthas.api.ArthasServices.JavaObject;
import io.arthas.api.ArthasServices.MapEntry;
import io.arthas.api.ArthasServices.MapValue;
import io.arthas.api.ArthasServices.NullValue;
import io.arthas.api.ArthasServices.UnexpandedObject;



public class JavaObjectConverter {
private static final int MAX_DEPTH = 5;

public static JavaObject toJavaObject(Object obj) {
return toJavaObject(obj, 0);
}

public static JavaObject toJavaObject(Object obj, int depth) {
if (depth >= MAX_DEPTH) {
return null;
}

if (obj == null) {
return JavaObject.newBuilder().setNullValue(NullValue.getDefaultInstance()).build();
}

JavaObject.Builder objectBuilder = JavaObject.newBuilder();
Class<? extends Object> objClazz = obj.getClass();
objectBuilder.setClassName(objClazz.getName());

// 基础类型
if (isBasicType(objClazz)) {
return objectBuilder.setBasicValue(createBasicValue(obj)).build();
} else if (obj instanceof Collection) { // 集合
return objectBuilder.setCollection(createCollectionValue((Collection<?>) obj, depth)).build();
} else if (obj instanceof Map) { // map
return objectBuilder.setMap(createMapValue((Map<?, ?>) obj, depth)).build();
} else if (objClazz.isArray()) {
return objectBuilder.setArrayValue(toArrayValue(obj, depth)).build();
}

Field[] fields = objClazz.getDeclaredFields();
List<JavaField> javaFields = new ArrayList<>();

for (Field field : fields) {
field.setAccessible(true);
JavaField.Builder fieldBuilder = JavaField.newBuilder();
fieldBuilder.setName(field.getName());

try {
Object fieldValue = field.get(obj);
Class<?> fieldType = field.getType();

if (fieldValue == null) {
fieldBuilder.setNullValue(NullValue.newBuilder().setClassName(fieldType.getName()).build());
} else if (fieldType.isArray()) {
ArrayValue arrayValue = toArrayValue(fieldValue, depth + 1);
if (arrayValue != null) {
fieldBuilder.setArrayValue(arrayValue);
} else {
fieldBuilder.setUnexpandedObject(
UnexpandedObject.newBuilder().setClassName(fieldType.getName()).build());
}
} else if (fieldType.isPrimitive() || isBasicType(fieldType)) {
BasicValue basicValue = createBasicValue(fieldValue);
fieldBuilder.setBasicValue(basicValue);
} else if (fieldValue instanceof Collection) { // 集合
fieldBuilder.setCollection(createCollectionValue((Collection<?>) fieldValue, depth));
} else if (fieldValue instanceof Map) { // map
fieldBuilder.setMap(createMapValue((Map<?, ?>) fieldValue, depth));
} else {
JavaObject nestedObject = toJavaObject(fieldValue, depth + 1);
if (nestedObject != null) {
fieldBuilder.setObjectValue(nestedObject);
} else {
fieldBuilder.setUnexpandedObject(
UnexpandedObject.newBuilder().setClassName(fieldType.getName()).build());
}
}
} catch (IllegalAccessException e) {
// TODO ignore ?
}
javaFields.add(fieldBuilder.build());
}

objectBuilder.setFields(JavaFields.newBuilder().addAllFields(javaFields).build());
return objectBuilder.build();
}

private static ArrayValue toArrayValue(Object array, int depth) {
if (array == null || depth >= MAX_DEPTH) {
return null;
}

ArrayValue.Builder arrayBuilder = ArrayValue.newBuilder();
Class<?> componentType = array.getClass().getComponentType();

arrayBuilder.setClassName(componentType.getName());

int length = Array.getLength(array);
for (int i = 0; i < length; i++) {
Object element = Array.get(array, i);

if (element != null) {
if (componentType.isArray()) {
ArrayValue nestedArrayValue = toArrayValue(element, depth + 1);
if (nestedArrayValue == null) {
arrayBuilder.addElements(ArrayElement.newBuilder().setArrayValue(nestedArrayValue));
} else {
arrayBuilder.addElements(ArrayElement.newBuilder().setUnexpandedObject(
UnexpandedObject.newBuilder().setClassName(element.getClass().getName()).build()));
}

} else if (componentType.isPrimitive() || isBasicType(componentType)) {
BasicValue basicValue = createBasicValue(element);
arrayBuilder.addElements(ArrayElement.newBuilder().setBasicValue(basicValue));
} else {
JavaObject nestedObject = toJavaObject(element, depth + 1);
if (nestedObject != null) {
arrayBuilder.addElements(ArrayElement.newBuilder().setObjectValue(nestedObject));
} else {
arrayBuilder.addElements(ArrayElement.newBuilder().setUnexpandedObject(
UnexpandedObject.newBuilder().setClassName(element.getClass().getName()).build()));
}

}
} else {
arrayBuilder.addElements(ArrayElement.newBuilder()
.setNullValue(NullValue.newBuilder().setClassName(componentType.getName()).build()));
}
}

return arrayBuilder.build();
}

private static MapValue createMapValue(Map<?, ?> map, int depth) {
MapValue.Builder builder = MapValue.newBuilder();

for (Entry<?, ?> entry : map.entrySet()) {
MapEntry mapEntry = MapEntry.newBuilder().setKey(toJavaObject(entry.getKey(), depth))
.setValue(toJavaObject(entry.getValue(), depth)).build();
builder.addEntries(mapEntry);
}
return builder.build();
}

private static CollectionValue createCollectionValue(Collection<?> collection, int depth) {
Builder builder = CollectionValue.newBuilder();
for (Object o : collection) {
builder.addElements(toJavaObject(o, depth));
}
return builder.build();
}

private static BasicValue createBasicValue(Object value) {
BasicValue.Builder builder = BasicValue.newBuilder();

if (value instanceof Integer) {
builder.setInt((int) value);
} else if (value instanceof Long) {
builder.setLong((long) value);
} else if (value instanceof Float) {
builder.setFloat((float) value);
} else if (value instanceof Double) {
builder.setDouble((double) value);
} else if (value instanceof Boolean) {
builder.setBoolean((boolean) value);
} else if (value instanceof String) {
builder.setString((String) value);
}

return builder.build();
}

private static boolean isBasicType(Class<?> clazz) {
if (String.class.equals(clazz) || Integer.class.equals(clazz) || Long.class.equals(clazz)
|| Float.class.equals(clazz) || Double.class.equals(clazz) || Boolean.class.equals(clazz)) {
return true;
}
return false;
}
}
Loading