Skip to content

Commit 57d66c4

Browse files
Merge pull request #237 from alibaba/develop-for-20191204
Develop for 20191204
2 parents 1d941b6 + 7f2903f commit 57d66c4

File tree

41 files changed

+1020
-693
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1020
-693
lines changed

bin/sandbox.sh

+217-202
Large diffs are not rendered by default.

pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
<groupId>com.alibaba.jvm.sandbox</groupId>
77
<artifactId>sandbox</artifactId>
8-
<version>1.2.2</version>
8+
<version>1.3.0-SNAPSHOT</version>
99
<packaging>pom</packaging>
1010

1111
<name>sandbox ${sandbox.version}</name>
1212

1313
<properties>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15-
<sandbox.version>1.2.2</sandbox.version>
15+
<sandbox.version>1.3.0-SNAPSHOT</sandbox.version>
1616
</properties>
1717

1818
<build>

sandbox-agent/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.alibaba.jvm.sandbox</groupId>
88
<artifactId>sandbox</artifactId>
9-
<version>1.2.2</version>
9+
<version>1.3.0-SNAPSHOT</version>
1010
</parent>
1111
<artifactId>sandbox-agent</artifactId>
1212
<name>sandbox-agent ${sandbox.version}</name>

sandbox-api/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.alibaba.jvm.sandbox</groupId>
88
<artifactId>sandbox</artifactId>
9-
<version>1.2.2</version>
9+
<version>1.3.0-SNAPSHOT</version>
1010
</parent>
1111
<artifactId>sandbox-api</artifactId>
1212
<name>sandbox-api ${sandbox.version}</name>

sandbox-api/src/main/java/com/alibaba/jvm/sandbox/api/event/Event.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,14 @@ public enum Type {
9292
* 立即调用:THROWS
9393
* 由{@link com.alibaba.jvm.sandbox.api.ProcessControlException#throwThrowsImmediately(Throwable)}触发
9494
*/
95-
IMMEDIATELY_THROWS,
95+
IMMEDIATELY_THROWS;
96+
97+
/**
98+
* 空类型
99+
*
100+
* @since {@code sandbox-api:1.3.0}
101+
*/
102+
public static final Event.Type[] EMPTY = new Event.Type[0];
96103

97104
}
98105

sandbox-api/src/main/java/com/alibaba/jvm/sandbox/api/http/printer/ConcurrentLinkedQueuePrinter.java

-5
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ public boolean waitingForBroken(final long time,
157157
: 0;
158158

159159
try {
160-
int heartBeat = 0;
161160
while (!writer.checkError()
162161
&& !isBrokenRef.get()
163162
&& !Thread.currentThread().isInterrupted()) {
@@ -169,10 +168,6 @@ public boolean waitingForBroken(final long time,
169168

170169
if (writeQueue.isEmpty()) {
171170
delay();
172-
if (heartBeat++ > 20) {
173-
heartBeat = 0;
174-
writer.write(0x0);
175-
}
176171
} else {
177172
flush();
178173
resetDelayTimeMs();

sandbox-api/src/main/java/com/alibaba/jvm/sandbox/api/listener/ext/EventWatchBuilder.java

+17-3
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,15 @@ public interface IBuildingForBehavior {
181181

182182
IBuildingForWatching onWatching();
183183

184+
/**
185+
* <p>兼容老版本BUGFIX</p>
186+
* 在1.2.0的版本中没有考虑好老版本的API向下兼容性问题,这里做一个修复 #236
187+
*
188+
* @param adviceListener advice监听器
189+
* @return this
190+
*/
191+
EventWatcher onWatch(AdviceListener adviceListener);
192+
184193
EventWatcher onWatch(AdviceListener adviceListener, Event.Type... eventTypeArray);
185194

186195
EventWatcher onWatch(EventListener eventListener, Event.Type... eventTypeArray);
@@ -628,6 +637,11 @@ public IBuildingForWatching onWatching() {
628637
return new BuildingForWatching();
629638
}
630639

640+
@Override
641+
public EventWatcher onWatch(AdviceListener adviceListener) {
642+
return onWatch(adviceListener, EMPTY);
643+
}
644+
631645
@Override
632646
public EventWatcher onWatch(final AdviceListener adviceListener, Event.Type... eventTypeArray) {
633647
if (eventTypeArray == null
@@ -681,7 +695,7 @@ public EventWatcher onWatch(AdviceListener adviceListener) {
681695
return build(
682696
new AdviceAdapterListener(adviceListener),
683697
toProgressGroup(progresses),
684-
eventTypeSet.toArray(new Event.Type[0])
698+
eventTypeSet.toArray(EMPTY)
685699
);
686700
}
687701

@@ -816,14 +830,14 @@ public void begin(int total) {
816830
}
817831

818832
@Override
819-
public void progressOnSuccess(Class clazz, int index) {
833+
public void progressOnSuccess(Class<?> clazz, int index) {
820834
for (final Progress progress : progresses) {
821835
progress.progressOnSuccess(clazz, index);
822836
}
823837
}
824838

825839
@Override
826-
public void progressOnFailed(Class clazz, int index, Throwable cause) {
840+
public void progressOnFailed(Class<?> clazz, int index, Throwable cause) {
827841
for (final Progress progress : progresses) {
828842
progress.progressOnFailed(clazz, index, cause);
829843
}

sandbox-api/src/main/java/com/alibaba/jvm/sandbox/api/resource/ModuleEventWatcher.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ interface Progress {
156156
* @param clazz 当前进行行变的类
157157
* @param index 当前形变类的序号,从0开始
158158
*/
159-
void progressOnSuccess(Class clazz, int index);
159+
void progressOnSuccess(Class<?> clazz, int index);
160160

161161
/**
162162
* 进度报告(失败)
@@ -165,7 +165,7 @@ interface Progress {
165165
* @param index 当前形变类的序号,从0开始
166166
* @param cause 失败异常
167167
*/
168-
void progressOnFailed(Class clazz, int index, Throwable cause);
168+
void progressOnFailed(Class<?> clazz, int index, Throwable cause);
169169

170170
/**
171171
* 进度结束

sandbox-common-api/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<artifactId>sandbox</artifactId>
88
<groupId>com.alibaba.jvm.sandbox</groupId>
9-
<version>1.2.2</version>
9+
<version>1.3.0-SNAPSHOT</version>
1010
</parent>
1111
<artifactId>sandbox-common-api</artifactId>
1212
<name>sandbox-common-api ${sandbox.version}</name>

sandbox-core/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.alibaba.jvm.sandbox</groupId>
88
<artifactId>sandbox</artifactId>
9-
<version>1.2.2</version>
9+
<version>1.3.0-SNAPSHOT</version>
1010
</parent>
1111
<artifactId>sandbox-core</artifactId>
1212
<name>sandbox-core ${sandbox.version}</name>
@@ -58,7 +58,7 @@
5858
<artifactId>maven-surefire-plugin</artifactId>
5959
<configuration>
6060
<forkMode>once</forkMode>
61-
<argLine>-Xbootclasspath/p:./src/test/resources/lib/sandbox-spy-1.2.0-for-qatest.jar</argLine>
61+
<argLine>-Xbootclasspath/p:./src/test/resources/lib/sandbox-spy-1.3.0-for-qatest.jar</argLine>
6262
</configuration>
6363
</plugin>
6464
</plugins>

sandbox-core/src/main/java/com/alibaba/jvm/sandbox/core/CoreConfigure.java

+64-30
Original file line numberDiff line numberDiff line change
@@ -38,66 +38,100 @@ public class CoreConfigure {
3838

3939
private static final String KEY_UNSAFE_ENABLE = "unsafe.enable";
4040

41-
// 受保护key数组,在保护key范围之内,如果前端已经传递过参数了,只能认前端,后端无法修改
41+
// 受保护key数组,在保护key范围之内,以用户传递的配置为准,系统配置不允许覆盖
4242
private static final String[] PROTECT_KEY_ARRAY = {KEY_NAMESPACE, KEY_SANDBOX_HOME, KEY_LAUNCH_MODE, KEY_SERVER_IP, KEY_SERVER_PORT, KEY_SERVER_CHARSET};
4343

4444
// 用户配置和系统默认配置都可以,需要进行合并的key,例如user_module
4545
private static final String[] MULTI_KEY_ARRAY = {KEY_USER_MODULE_LIB_PATH};
4646

4747
private static final FeatureCodec codec = new FeatureCodec(';', '=');
4848

49-
private final Map<String, String> featureMap;
49+
private final Map<String, String> featureMap = new LinkedHashMap<String, String>();
5050

51-
private CoreConfigure(final String featureString) {
52-
this.featureMap = codec.toMap(featureString);
51+
private CoreConfigure(final String featureString,
52+
final String propertiesFilePath) {
53+
final Map<String, String> featureMap = toFeatureMap(featureString);
54+
final Map<String, String> propertiesMap = toPropertiesMap(propertiesFilePath);
55+
this.featureMap.putAll(merge(featureMap, propertiesMap));
5356
}
5457

55-
private static volatile CoreConfigure instance;
56-
57-
public static CoreConfigure toConfigure(final String featureString, final String propertiesFilePath) {
58-
return instance = mergePropertiesFile(new CoreConfigure(featureString), propertiesFilePath);
58+
private Map<String, String> toFeatureMap(String featureString) {
59+
return codec.toMap(featureString);
5960
}
6061

61-
// 从配置文件中合并配置到CoreConfigure中
62-
private static CoreConfigure mergePropertiesFile(final CoreConfigure cfg, final String propertiesFilePath) {
63-
Map<String, String> propertiesMap = propertiesToStringMap(cfg, fetchProperties(propertiesFilePath));
64-
for (String key : MULTI_KEY_ARRAY) {
65-
if (cfg.featureMap.containsKey(key) && propertiesMap.containsKey(key)) {
66-
propertiesMap.put(key, cfg.featureMap.get(key) + ";" + propertiesMap.get(key));
67-
}
62+
private Map<String, String> toPropertiesMap(String propertiesFilePath) {
63+
final Map<String, String> propertiesMap = new LinkedHashMap<String, String>();
64+
65+
if(null == propertiesFilePath) {
66+
return propertiesMap;
6867
}
69-
cfg.featureMap.putAll(propertiesMap);
70-
return cfg;
71-
}
7268

73-
// 从指定配置文件路径中获取配置信息
74-
private static Properties fetchProperties(final String propertiesFilePath) {
69+
final File propertiesFile = new File(propertiesFilePath);
70+
if (!propertiesFile.exists()
71+
|| !propertiesFile.canRead()) {
72+
return propertiesMap;
73+
}
74+
75+
76+
// 从指定配置文件路径中获取配置信息
7577
final Properties properties = new Properties();
7678
InputStream is = null;
7779
try {
78-
is = FileUtils.openInputStream(new File(propertiesFilePath));
80+
is = FileUtils.openInputStream(propertiesFile);
7981
properties.load(is);
8082
} catch (Throwable cause) {
8183
// cause.printStackTrace(System.err);
8284
} finally {
8385
IOUtils.closeQuietly(is);
8486
}
85-
return properties;
86-
}
8787

88-
// 配置转map
89-
private static Map<String, String> propertiesToStringMap(CoreConfigure cfg, final Properties properties) {
90-
final Map<String, String> map = new HashMap<String, String>();
88+
// 转换为Map
9189
for (String key : properties.stringPropertyNames()) {
90+
propertiesMap.put(key, properties.getProperty(key));
91+
}
92+
93+
return propertiesMap;
94+
}
95+
96+
private Map<String, String> merge(Map<String, String> featureMap, Map<String, String> propertiesMap) {
9297

93-
//如果受保护的key已经由入参指定,则过滤掉受保护的key,防止入参被覆盖
94-
if (cfg.featureMap.containsKey(key) && ArrayUtils.contains(PROTECT_KEY_ARRAY, key)) {
98+
// 以featureMap配置为准
99+
final Map<String, String> mergeMap = new LinkedHashMap<String, String>(featureMap);
100+
101+
// 合并propertiesMap
102+
for (final Map.Entry<String, String> propertiesEntry : propertiesMap.entrySet()) {
103+
104+
// 如果是受保护的KEY,则以featureMap中的非空值为准
105+
if (mergeMap.containsKey(propertiesEntry.getKey())
106+
&& ArrayUtils.contains(PROTECT_KEY_ARRAY, propertiesEntry.getKey())) {
95107
continue;
96108
}
97109

98-
map.put(key, properties.getProperty(key));
110+
// 如果是多值合并的KEY,则不进行覆盖,转为合并
111+
else if (ArrayUtils.contains(MULTI_KEY_ARRAY, propertiesEntry.getKey())
112+
&& mergeMap.containsKey(propertiesEntry.getKey())) {
113+
mergeMap.put(
114+
propertiesEntry.getKey(),
115+
mergeMap.get(propertiesEntry.getKey()) + ";" + propertiesEntry.getValue()
116+
);
117+
continue;
118+
}
119+
120+
// 合并K,V
121+
else {
122+
mergeMap.put(propertiesEntry.getKey(), propertiesEntry.getValue());
123+
}
124+
99125
}
100-
return map;
126+
127+
return mergeMap;
128+
129+
}
130+
131+
private static volatile CoreConfigure instance;
132+
133+
public static CoreConfigure toConfigure(final String featureString, final String propertiesFilePath) {
134+
return instance = new CoreConfigure(featureString, propertiesFilePath);
101135
}
102136

103137
public static CoreConfigure getInstance() {

sandbox-core/src/main/java/com/alibaba/jvm/sandbox/core/CoreModule.java

+41-35
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ public <T> T append(ReleaseResource<T> resource) {
197197
|| null == resource.get()) {
198198
return null;
199199
}
200-
releaseResources.add(resource);
200+
synchronized (releaseResources) {
201+
releaseResources.add(resource);
202+
}
201203
logger.debug("append resource={} in module[id={};]", resource.get(), uniqueId);
202204
return resource.get();
203205
}
@@ -208,32 +210,34 @@ public <T> T append(ReleaseResource<T> resource) {
208210
* @param target 待释放的资源实体
209211
*/
210212
public void release(Object target) {
211-
final Iterator<ReleaseResource<?>> resourceRefIt = releaseResources.iterator();
212-
while (resourceRefIt.hasNext()) {
213-
final ReleaseResource<?> resourceRef = resourceRefIt.next();
214-
215-
// 删除掉无效的资源
216-
if (null == resourceRef) {
217-
resourceRefIt.remove();
218-
logger.info("remove null resource in module={}", uniqueId);
219-
continue;
220-
}
213+
synchronized (releaseResources) {
214+
final Iterator<ReleaseResource<?>> resourceRefIt = releaseResources.iterator();
215+
while (resourceRefIt.hasNext()) {
216+
final ReleaseResource<?> resourceRef = resourceRefIt.next();
217+
218+
// 删除掉无效的资源
219+
if (null == resourceRef) {
220+
resourceRefIt.remove();
221+
logger.info("remove null resource in module={}", uniqueId);
222+
continue;
223+
}
221224

222-
// 删除掉已经被GC掉的资源
223-
final Object resource = resourceRef.get();
224-
if (null == resource) {
225-
resourceRefIt.remove();
226-
logger.info("remove empty resource in module={}", uniqueId);
227-
continue;
228-
}
225+
// 删除掉已经被GC掉的资源
226+
final Object resource = resourceRef.get();
227+
if (null == resource) {
228+
resourceRefIt.remove();
229+
logger.info("remove empty resource in module={}", uniqueId);
230+
continue;
231+
}
229232

230-
if (target.equals(resource)) {
231-
resourceRefIt.remove();
232-
logger.debug("release resource={} in module={}", resourceRef.get(), uniqueId);
233-
try {
234-
resourceRef.release();
235-
} catch (Exception cause) {
236-
logger.warn("release resource occur error in module={};", uniqueId, cause);
233+
if (target.equals(resource)) {
234+
resourceRefIt.remove();
235+
logger.debug("release resource={} in module={}", resourceRef.get(), uniqueId);
236+
try {
237+
resourceRef.release();
238+
} catch (Exception cause) {
239+
logger.warn("release resource occur error in module={};", uniqueId, cause);
240+
}
237241
}
238242
}
239243
}
@@ -243,16 +247,18 @@ public void release(Object target) {
243247
* 在当前模块下移除所有可释放资源
244248
*/
245249
public void releaseAll() {
246-
final Iterator<ReleaseResource<?>> resourceRefIt = releaseResources.iterator();
247-
while (resourceRefIt.hasNext()) {
248-
final ReleaseResource<?> resourceRef = resourceRefIt.next();
249-
resourceRefIt.remove();
250-
if (null != resourceRef) {
251-
logger.debug("release resource={} in module={}", resourceRef.get(), uniqueId);
252-
try {
253-
resourceRef.release();
254-
} catch (Exception cause) {
255-
logger.warn("release resource occur error in module={};", uniqueId, cause);
250+
synchronized (releaseResources) {
251+
final Iterator<ReleaseResource<?>> resourceRefIt = releaseResources.iterator();
252+
while (resourceRefIt.hasNext()) {
253+
final ReleaseResource<?> resourceRef = resourceRefIt.next();
254+
resourceRefIt.remove();
255+
if (null != resourceRef) {
256+
logger.debug("release resource={} in module={}", resourceRef.get(), uniqueId);
257+
try {
258+
resourceRef.release();
259+
} catch (Exception cause) {
260+
logger.warn("release resource occur error in module={};", uniqueId, cause);
261+
}
256262
}
257263
}
258264
}

0 commit comments

Comments
 (0)