Skip to content

Commit

Permalink
1. 去掉DebugWatchModule的ThreadLocal
Browse files Browse the repository at this point in the history
2. SandboxClassFileTransformer自释放
3. Jetty优雅关闭
  • Loading branch information
dongchenxu committed Jan 6, 2019
1 parent 7901404 commit 2c0fdb3
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ public <T> T append(ReleaseResource<T> resource) {
}

/**
* 在当前模块下移除一个可释放资源
* 在当前模块下释放一个可释放资源
*
* @param target 待释放的资源实体
*/
public void remove(Object target) {
public void release(Object target) {
final Iterator<ReleaseResource<?>> resourceRefIt = releaseResources.iterator();
while (resourceRefIt.hasNext()) {
final ReleaseResource<?> resourceRef = resourceRefIt.next();
Expand All @@ -228,7 +228,26 @@ public void remove(Object target) {

if (target.equals(resourceRef.get())) {
resourceRefIt.remove();
logger.info("remove resource={} in module={}", resourceRef.get(), uniqueId);
logger.debug("release resource={} in module={}", resourceRef.get(), uniqueId);
try {
resourceRef.release();
} catch (Exception cause) {
logger.warn("release resource occur error in module={};", uniqueId, cause);
}
}
}
}

/**
* 在当前模块下移除所有可释放资源
*/
public void releaseAll() {
final Iterator<ReleaseResource<?>> resourceRefIt = releaseResources.iterator();
while (resourceRefIt.hasNext()) {
final ReleaseResource<?> resourceRef = resourceRefIt.next();
resourceRefIt.remove();
if (null != resourceRef) {
logger.debug("release resource={} in module={}", resourceRef.get(), uniqueId);
try {
resourceRef.release();
} catch (Exception cause) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void active(final int listenerId,
public void frozen(int listenerId) {
final EventProcessor processor = mappingOfEventProcessor.remove(listenerId);
if (null == processor) {
logger.debug("ignore frozen listener[id={};], because not found.");
logger.debug("ignore frozen listener={}, because not found.", listenerId);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,19 +214,6 @@ else if (ModuleEventWatcher.class.isAssignableFrom(fieldType)) {
cfg.isEnableUnsafe(),
cfg.getNamespace()
);
coreModule.append(new CoreModule.ReleaseResource<ModuleEventWatcher>(moduleEventWatcher) {

@Override
public void release() {
for (final SandboxClassFileTransformer transformer
: new ArrayList<SandboxClassFileTransformer>(coreModule.getSandboxClassFileTransformers())) {
logger.info("delete watch[id={}] by module[id={};] unload.",
transformer.getWatchId(), coreModule.getUniqueId());
moduleEventWatcher.delete(transformer.getWatchId());
}
}

});
writeField(
resourceField,
module,
Expand Down Expand Up @@ -373,6 +360,9 @@ public synchronized CoreModule unload(final CoreModule coreModule,
// 标记模块为:已卸载
coreModule.markLoaded(false);

// 释放所有可释放资源
coreModule.releaseAll();

// 尝试关闭ClassLoader
closeModuleClassLoaderIfNecessary(coreModule.getLoader());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private List<Class<?>> find(final Matcher matcher,
// 过滤掉对于JVM认为不可修改的类
if (isRemoveUnsupported
&& !inst.isModifiableClass(clazz)) {
logger.debug("remove from findForReTransform, because class:{} is unModifiable", clazz.getName());
// logger.debug("remove from findForReTransform, because class:{} is unModifiable", clazz.getName());
continue;
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.alibaba.jvm.sandbox.api.listener.ext.EventWatchCondition;
import com.alibaba.jvm.sandbox.api.resource.ModuleEventWatcher;
import com.alibaba.jvm.sandbox.core.CoreModule;
import com.alibaba.jvm.sandbox.core.CoreModule.ReleaseResource;
import com.alibaba.jvm.sandbox.core.enhance.weaver.EventListenerHandlers;
import com.alibaba.jvm.sandbox.core.manager.CoreLoadedClassDataSource;
import com.alibaba.jvm.sandbox.core.util.Sequencer;
Expand Down Expand Up @@ -176,29 +177,38 @@ public int watch(final Filter filter,
final EventListener listener,
final Progress progress,
final Event.Type... eventType) {
return watch(new ExtFilterMatcher(make(filter)), listener, progress, eventType);
return watch(true, new ExtFilterMatcher(make(filter)), listener, progress, eventType);
}

@Override
public int watch(final EventWatchCondition condition,
final EventListener listener,
final Progress progress,
final Event.Type... eventType) {
return watch(toOrGroupMatcher(condition.getOrFilterArray()), listener, progress, eventType);
return watch(true, toOrGroupMatcher(condition.getOrFilterArray()), listener, progress, eventType);
}

// 这里是用matcher重制过后的watch
private int watch(final Matcher matcher,
private int watch(final boolean isAutoRelease,
final Matcher matcher,
final EventListener listener,
final Progress progress,
final Event.Type... eventType) {
final int watchId = watchIdSequencer.next();
// 给对应的模块追加ClassFileTransformer
final SandboxClassFileTransformer sandClassFileTransformer = new SandboxClassFileTransformer(
watchId, coreModule.getUniqueId(), matcher, listener, isEnableUnsafe, eventType, namespace);
isAutoRelease, watchId, coreModule.getUniqueId(), matcher, listener, isEnableUnsafe, eventType, namespace);

// 注册到CoreModule中
coreModule.getSandboxClassFileTransformers().add(sandClassFileTransformer);
coreModule.append(new ReleaseResource<SandboxClassFileTransformer>(sandClassFileTransformer) {
@Override
public void release() {
if (get().isAutoRelease()) {
delete(get().getWatchId());
}
}
});

// 注册到JVM加载上ClassFileTransformer处理新增的类
inst.addTransformer(sandClassFileTransformer, true);
Expand Down Expand Up @@ -269,6 +279,9 @@ public void delete(final int watcherId,
// 清除掉该SandboxClassFileTransformer
cftIt.remove();

// 删除可释放资源
coreModule.release(sandboxClassFileTransformer);

}
}

Expand Down Expand Up @@ -308,7 +321,7 @@ public void watching(final Filter filter,
final WatchCallback watchCb,
final Progress dProgress,
final Event.Type... eventType) throws Throwable {
final int watchId = watch(filter, listener, wProgress, eventType);
final int watchId = watch(false, new ExtFilterMatcher(make(filter)), listener, wProgress, eventType);
try {
watchCb.watchCompleted();
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
public class SandboxClassFileTransformer implements ClassFileTransformer {

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

private final boolean isAutoRelease;
private final int watchId;
private final String uniqueId;
private final Matcher matcher;
Expand All @@ -37,13 +39,15 @@ public class SandboxClassFileTransformer implements ClassFileTransformer {
private final int listenerId;
private final AffectStatistic affectStatistic = new AffectStatistic();

SandboxClassFileTransformer(final int watchId,
SandboxClassFileTransformer(final boolean isAutoRelease,
final int watchId,
final String uniqueId,
final Matcher matcher,
final EventListener eventListener,
final boolean isEnableUnsafe,
final Event.Type[] eventTypeArray,
final String namespace) {
this.isAutoRelease = isAutoRelease;
this.watchId = watchId;
this.uniqueId = uniqueId;
this.matcher = matcher;
Expand Down Expand Up @@ -210,4 +214,12 @@ public AffectStatistic getAffectStatistic() {
return affectStatistic;
}

/**
* 是否是自释放资源
*
* @return TRUE:自释放;FALSE:非自释放
*/
public boolean isAutoRelease() {
return isAutoRelease;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public void process() throws Throwable {

// destroy http server
logger.info("{} is destroying", this);
while (!httpServer.isStopped());
httpServer.destroy();

} catch (Throwable cause) {
Expand Down Expand Up @@ -218,6 +219,9 @@ public void process() throws Throwable {
@Override
public void destroy() {

// 关闭JVM-SANDBOX
jvmSandbox.destroy();

// 关闭HTTP服务器
if (isBind()) {
try {
Expand All @@ -227,9 +231,6 @@ public void destroy() {
}
}

// 关闭JVM-SANDBOX
jvmSandbox.destroy();

// 关闭LOGBACK
LogbackUtils.destroy();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void release() {
} finally {
Thread.currentThread().setContextClassLoader(oriThreadContextClassLoader);
method.setAccessible(isAccessible);
coreModule.remove(writer);
coreModule.release(writer);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void onClose(int closeCode, String message) {
try {
listener.onClose(closeCode, message);
} finally {
coreModule.remove(conn);
coreModule.release(conn);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*/
@MetaInfServices(Module.class)
@Information(id = "debug-http-logger", version = "0.0.2", author = "luanjia@taobao.com")
public class HttpHttpAccessLoggerModule implements Module, LoadCompleted {
public class HttpAccessLoggerModule implements Module, LoadCompleted {

private final Logger stLogger = LoggerFactory.getLogger("DEBUG-SERVLET-LOGGER");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ public String getExpress() {
*/
class ExpressFactory {

private static final ThreadLocal<Express> expressRef = new ThreadLocal<Express>() {
@Override
protected Express initialValue() {
return new OgnlExpress();
}
};
// private static final ThreadLocal<Express> expressRef = new ThreadLocal<Express>() {
// @Override
// protected Express initialValue() {
// return new OgnlExpress();
// }
// };

/**
* 构造表达式执行类
Expand All @@ -103,7 +103,7 @@ protected Express initialValue() {
* @return 返回表达式实现
*/
public static Express newExpress(Object object) {
return expressRef.get().reset().bind(object);
return new OgnlExpress().reset().bind(object);
// return new OgnlExpress().bind(object);
}

Expand Down

0 comments on commit 2c0fdb3

Please sign in to comment.