Skip to content

Commit

Permalink
release_0.0.1.c
Browse files Browse the repository at this point in the history
  • Loading branch information
luanjia committed Nov 8, 2017
1 parent cb7c0db commit 4d2daa0
Show file tree
Hide file tree
Showing 45 changed files with 863 additions and 218 deletions.
66 changes: 35 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
# JVM-SANDBOX
>基于JVM的实时无侵入AOP框架容器
> 基于JVM的实时无侵入AOP框架容器

## 项目简介

#### JVM-SANDBOX的核心功能是什么?
### JVM-SANDBOX的核心功能是什么?

#### 实时无侵入AOP框架

##### 1.实时无侵入AOP框架
在常见的AOP框架实现方案中,有静态编织和动态编织两种。

###### 静态编织
静态编织发生在字节码生成时根据一定框架的规则提前将AOP字节码插入到目标类和方法中,实现AOP;
###### 动态编织
动态编织则允许在JVM运行过程中完成指定方法的AOP字节码增强.
常见的动态编织方案大多采用重命名原有方法,再新建一个同签名的方法来做代理的工作模式来完成AOP的功能(常见的实现方案如CgLib),但这种方式存在一些应用边界:
- 侵入性
对被代理的目标类需要进行侵入式改造。比如:在Spring中必须是托管于Spring容器中的Bean。
- 固化性
目标代理方法在启动之后即固化,无法重新对一个已有方法进行AOP增强。

###### 热部署
1. **静态编织**
静态编织发生在字节码生成时根据一定框架的规则提前将AOP字节码插入到目标类和方法中,实现AOP;

1. **动态编织**
动态编织则允许在JVM运行过程中完成指定方法的AOP字节码增强.常见的动态编织方案大多采用重命名原有方法,再新建一个同签名的方法来做代理的工作模式来完成AOP的功能(常见的实现方案如CgLib),但这种方式存在一些应用边界:

- **侵入性**
对被代理的目标类需要进行侵入式改造。比如:在Spring中必须是托管于Spring容器中的Bean。

- **固化性**
目标代理方法在启动之后即固化,无法重新对一个已有方法进行AOP增强。

#### 热部署

还有一些实现AOP的方式是通过类似热部署的方式完成,但现有的热部署实现方案也存在一些应用边界:

- 性能折损巨大
- 对JVM存在侵入性
- 必须启动时显式开启
1. 性能折损巨大
1. 对JVM存在侵入性
1. 必须启动时显式开启

基于此我通过JDK6所提供的Instrumentation-API实现了利用HotSwap技术在不重启JVM的情况下实现对任意方法的AOP增强。而且性能开销还在可以接受的范围之内。

#####2. 动态可插拔容器
#### 动态可插拔容器

为了实现沙箱模块的动态热插拔,容器客户端和沙箱动态可插拔容器采用HTTP协议进行通讯,底层用Jetty6作为HTTP服务器。

#### What can the JVM-SANDBOX do
### JVM-SANDBOX能做什么

在JVM沙箱(以下简称沙箱)的世界观中,任何一个Java方法的调用都可以分解为`BEFORE``RETURN``THROWS`三个环节,由此在三个环节上引申出对应环节的事件探测和流程控制机制。

Expand All @@ -51,7 +56,6 @@ try {
}
```


基于`BEFORE``RETURN``THROWS`三个环节事件,可以完成很多类AOP的操作。

1. 可以感知和改变方法调用的入参
Expand All @@ -62,7 +66,7 @@ try {
- 在方法体返回之前重新构造新的结果对象,甚至可以改变为抛出异常
- 在方法体抛出异常之后重新抛出新的异常,甚至可以改变为正常返回

#### JVM沙箱都有哪些可能的应用场景
### JVM沙箱都有哪些可能的应用场景

- 线上故障定位
- 线上系统流控
Expand All @@ -73,24 +77,24 @@ try {

JVM沙箱还能帮助你做很多很多,取决于你的脑洞有多大了。

# For English
# FOR ENGLISH

# JVM-SANDBOX
>Real - time non-invasive AOP framework container based on JVM
> Real - time non-invasive AOP framework container based on JVM

## Foreword

#### What is the core function of the JVM-SANDBOX?
### What is the core function of the JVM-SANDBOX?

##### Real-time non-invasive AOP framework
#### Real-time non-invasive AOP framework
In the common AOP framework to achieve the program, there are two kinds of static weaving and dynamic weaving.

###### Static weaving
##### Static weaving

Static weaving occurs in the bytecode generation according to a certain framework of the rules in advance AOP byte code into the target class and method to achieve AOP.

###### Dynamic weaving
##### Dynamic weaving

Dynamic weaving allows AOP bytecode enhancement of the specified method to be completed during the execution of the JVM.

Expand All @@ -104,7 +108,7 @@ Common dynamic weaving programs are mostly used to rename the original method, a

The target agent method is solidified after startup and can not re-validate an existing method

###### Hot deployment
##### Hot deployment
There are some ways to implement AOP is done through a similar hot deployment, but there are some application boundaries for existing hot deployment implementations:

- Performance damage huge
Expand All @@ -114,10 +118,10 @@ There are some ways to implement AOP is done through a similar hot deployment, b
Based on this I am through the JDK6 provided Instrumentation-API implementation of the use of HotSwap technology without restarting the JVM in the case of any method to achieve AOP enhancements. And performance overhead is still within acceptable limits


##### Provides a plug-and-play module management container
#### Provides a plug-and-play module management container
In order to realize the dynamic hot-swapping of the sandbox module, the container client and the sandbox dynamic pluggable container communicate with the HTTP protocol. The bottom layer uses Jetty6 as the HTTP server.

#### What can the JVM-SANDBOX do?
### What can the JVM-SANDBOX do?

In the JVM-SANDBOX (hereinafter referred to as the sandbox) world view, any one of the Java method calls can be broken down into `BEFORE`,` RETURN` and `THROWS` three links, which in three links on the corresponding link Event detection and process control mechanisms.

Expand Down Expand Up @@ -148,7 +152,7 @@ Based on the `BEFORE`,` RETURN` and `THROWS` three events, you can do a lot of A
- Throws a new exception after throwing an exception in the method body, and can even change to a normal return


#### What are the possible scenarios for the JVM-SANDBOX?
### What are the possible scenarios for the JVM-SANDBOX?

- Online fault location
- Online system flow control
Expand Down
2 changes: 1 addition & 1 deletion bin/sandbox-packages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ echo "${SANDBOX_VERSION}" > ${SANDBOX_TARGET_DIR}/cfg/version

# for test
## cp ../sandbox-debug-module/target/sandbox-debug-module-*-jar-with-dependencies.jar\
## ${SANDBOX_TARGET_DIR}/module/sandbox-debug-module.jar
## ${SANDBOX_TARGET_DIR}/module/sandbox-debug-module.jar

# for mgr
cp ../sandbox-mgr-module/target/sandbox-mgr-module-*-jar-with-dependencies.jar\
Expand Down
20 changes: 14 additions & 6 deletions bin/sandbox.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,26 @@
# define the sandbox's ${SYSTEM_MODULE} dir
## system_module=../module

# define the sandbox's ${USER_MODULE} dir
## user_module=~/.sandbox-module
# define the sandbox's ${USER_MODULE} dir, multi values, use ',' split
## user_module=~/.sandbox-module;~/.sandbox-module-1;~/.sandbox-module-2;~/.sandbox-module-n;
user_module=~/.sandbox-module;
#user_module=/home/staragent/plugins/jvm-sandbox-module/sandbox-module;/home/staragent/plugins/monkeyking;

# define the sandbox's ${PROVIDER_LIB} dir
## provider=../provider

# define the network interface
## server.ip=0.0.0.0

# define the network port
## server.port=4769

# switch the sandbox can enhance system class
unsafe.enable=true

# define the sandbox event pool arguments(min/max/total)
event.pool.enable=true
event.pool.key.min=100
event.pool.key.max=2000
event.pool.total=3000

event.pool.max.total=6000
event.pool.min.idle.per.event=50
event.pool.max.idle.per.event=100
event.pool.max.total.per.event=2000
19 changes: 15 additions & 4 deletions bin/sandbox.sh
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ usage: ${0} [h] [<p:> [vlRFfu:a:A:d:m:I:P:C:]]
EXAMPLE:
${0} -C -I 192.168.0.1 -P 3658 -m debug
-S : Shutdown server
Shutdown jvm-sandbox\` server
'
}

Expand All @@ -174,7 +177,10 @@ reset_for_env()

# if env define the JAVA_HOME, use it first
# if is alibaba opts, use alibaba ops's default JAVA_HOME
[ -z ${JAVA_HOME} ] && JAVA_HOME=/opt/taobao/java
# [ -z ${JAVA_HOME} ] && JAVA_HOME=/opt/taobao/java
if [[ -z ${JAVA_HOME} ]]; then
JAVA_HOME=$(ps aux|grep ${TARGET_JVM_PID}|grep java|awk '{print $11}'|xargs ls -l|awk '{if($1~/^l/){print $11}else{print $9}}'|sed 's/\/bin\/java//g')
fi

# check the jvm version, we need 1.6+
local JAVA_VERSION=$(${JAVA_HOME}/bin/java -version 2>&1|awk -F '"' '/version/&&$2>"1.5"{print $2}')
Expand Down Expand Up @@ -236,9 +242,8 @@ function sandbox_debug_curl() {
function main() {

check_permission
reset_for_env

while getopts "hp:vFfRu:a:A:d:m:I:P:Cl" ARG
while getopts "hp:vFfRu:a:A:d:m:I:P:ClS" ARG
do
case ${ARG} in
h) usage;exit;;
Expand All @@ -256,13 +261,15 @@ function main() {
I) TARGET_SERVER_IP=${OPTARG};;
P) TARGET_SERVER_PORT=${OPTARG};;
C) OP_CONNECT_ONLY=1;;
S) OP_SHUTDOWN=1;;
?) usage;exit_on_err 1;;
esac
done

reset_for_env

# reset IP
[ -z ${TARGET_SERVER_IP} ] && TARGET_SERVER_IP="127.0.0.1";
[ -z ${TARGET_SERVER_IP} ] && TARGET_SERVER_IP="0.0.0.0";

# reset PORT
[ -z ${TARGET_SERVER_PORT} ] && TARGET_SERVER_PORT=0;
Expand Down Expand Up @@ -315,6 +322,10 @@ function main() {
[[ ! -z ${OP_MODULE_DETAIL} ]] \
&& sandbox_curl_with_exit "module-mgr/detail" "&id=${ARG_MODULE_DETAIL}"

# -S shutdown
[[ ! -z ${OP_SHUTDOWN} ]] \
&& sandbox_curl_with_exit "control/shutdown"

# -d debug
if [[ ! -z ${OP_DEBUG} ]]; then
sandbox_debug_curl "module/http/${ARG_DEBUG}"
Expand Down
39 changes: 32 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?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">
<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">
<modelVersion>4.0.0</modelVersion>

<!-- 这个如果开源之后,不再需要
<parent>
<groupId>com.taobao</groupId>
<artifactId>parent</artifactId>
<version>1.0.3</version>
</parent>
-->

<groupId>com.alibaba.jvm.sandbox</groupId>
<artifactId>sandbox</artifactId>
Expand All @@ -17,9 +20,9 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<sandbox.api.version>1.0.8</sandbox.api.version>
<sandbox.provider.api.version>1.0.0</sandbox.provider.api.version>
<sandbox.common.api.version>1.0.0</sandbox.common.api.version>
<sandbox.api.version>1.0.9</sandbox.api.version>
<sandbox.provider.api.version>1.0.1</sandbox.provider.api.version>
<sandbox.common.api.version>1.0.1</sandbox.common.api.version>
</properties>

<build>
Expand Down Expand Up @@ -146,6 +149,14 @@
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>

<!-- ant -->
<dependency>
<groupId>ant</groupId>
<artifactId>ant</artifactId>
<version>1.7.0</version>
</dependency>

</dependencies>
</dependencyManagement>

Expand All @@ -160,6 +171,20 @@
<module>sandbox-mgr-module</module>
<module>sandbox-provider-api</module>
<module>sandbox-common-api</module>
<module>sandbox-mgr-provider</module>
</modules>
</project>
<module>sandbox-mgr-provider</module>
</modules>

<!--
<distributionManagement>
<repository>
<id>alibaba-releases</id>
<url>http://mvnrepo.alibaba-inc.com/mvn/releases</url>
</repository>
<snapshotRepository>
<id>alibaba-snapshots</id>
<url>http://mvnrepo.alibaba-inc.com/mvn/snapshots</url>
</snapshotRepository>
</distributionManagement>
-->

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ private static String substringBeforeLast(String str, String separator) {
private static volatile ClassLoader sandboxClassLoader;


private static boolean isBlankString(final String string) {
private static boolean isNotBlankString(final String string) {
return null != string
&& string.length() > 0
&& !string.matches("^\\s*$");
}

private static String getDefaultString(final String string, final String defaultString) {
return isBlankString(string)
? defaultString
: string;
return isNotBlankString(string)
? string
: defaultString;
}

/**
Expand Down
6 changes: 4 additions & 2 deletions sandbox-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.2</version>
<version>3.0.0-M1</version>
<!-- 过期的配置
<configuration>
<aggregate>true</aggregate>
</configuration>
-->
<executions>
<execution>
<id>attach-javadocs</id>
Expand All @@ -42,7 +44,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<version>3.0.1</version>
<executions>
<execution>
<goals>
Expand Down
Loading

0 comments on commit 4d2daa0

Please sign in to comment.