Skip to content

Commit

Permalink
1. 修复失败的测试用例
Browse files Browse the repository at this point in the history
2. 修复问题 alibaba#125
  • Loading branch information
dongchenxu committed Jan 6, 2019
1 parent 55c7454 commit 45f674d
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import com.alibaba.jvm.sandbox.api.event.ImmediatelyThrowsEvent;
import com.alibaba.jvm.sandbox.api.listener.EventListener;
import com.alibaba.jvm.sandbox.core.util.ObjectIDs;
import com.alibaba.jvm.sandbox.core.util.Sequencer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.com.alibaba.jvm.sandbox.spy.Spy;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

import static com.alibaba.jvm.sandbox.api.event.Event.Type.IMMEDIATELY_RETURN;
import static com.alibaba.jvm.sandbox.api.event.Event.Type.IMMEDIATELY_THROWS;
Expand All @@ -32,7 +32,8 @@ public class EventListenerHandlers {
private final Logger logger = LoggerFactory.getLogger(getClass());

// 调用序列生成器
private final Sequencer invokeIdSequencer = new Sequencer(1000);
// private final Sequencer invokeIdSequencer = new Sequencer();
private final AtomicInteger invokeIdSequencer = new AtomicInteger(1000);

// 全局处理器ID:处理器映射集合
private final Map<Integer/*LISTENER_ID*/, EventProcessor> mappingOfEventProcessor
Expand Down Expand Up @@ -314,7 +315,7 @@ private Spy.Ret handleOnBefore(final int listenerId,
final EventProcessor.Process process = wrap.processRef.get();

// 调用ID
final int invokeId = invokeIdSequencer.next();
final int invokeId = invokeIdSequencer.getAndIncrement();
process.pushInvokeId(invokeId);

// 调用过程ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class DefaultModuleEventWatcher implements ModuleEventWatcher {
private final String namespace;

// 观察ID序列生成器
private final Sequencer watchIdSequencer = new Sequencer(1000);
private final Sequencer watchIdSequencer = new Sequencer();

DefaultModuleEventWatcher(final Instrumentation inst,
final CoreLoadedClassDataSource classDataSource,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ObjectIDs {
/**
* 对象ID序列生成器,生成范围[1,{@link Integer#MAX_VALUE}]之间的整数
*/
private final Sequencer objectIDSequencer = new Sequencer(1);
private final Sequencer objectIDSequencer = new Sequencer();

/**
* 全局读写锁:用于维护世界的和平
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
package com.alibaba.jvm.sandbox.core.util;

import java.util.concurrent.atomic.AtomicInteger;
import java.com.alibaba.jvm.sandbox.spy.Spy;

/**
* 序列发生器
* 序列发生器用途非常广泛,主要用于圈定全局唯一性标识
* Created by luanjia@taobao.com on 16/5/20.
*
* @author luanjia@taobao.com
*/
public class Sequencer {

// 序列生成器
private final AtomicInteger generator;

public Sequencer(int initialValue) {
generator = new AtomicInteger(initialValue);
}

/**
* 生成下一条序列
*
* @return 下一条序列
*/
public int next() {
return generator.getAndIncrement();
// 这里直接修改为引用Spy的全局唯一序列,修复 #125
return Spy.nextSequence();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.alibaba.jvm.sandbox.core.enhance.weaver.EventListenerHandlers;
import com.alibaba.jvm.sandbox.core.util.ObjectIDs;
import com.alibaba.jvm.sandbox.core.util.SandboxReflectUtils;
import com.alibaba.jvm.sandbox.core.util.SpyUtils;
import com.alibaba.jvm.sandbox.core.util.matcher.ExtFilterMatcher;
import com.alibaba.jvm.sandbox.core.util.matcher.MatchingResult;
import com.alibaba.jvm.sandbox.core.util.matcher.structure.ClassStructureFactory;
Expand Down Expand Up @@ -40,6 +41,7 @@ public class JvmHelper {

public JvmHelper(final String namespace) {
this.namespace = namespace;
SpyUtils.init(namespace);
toConfigure(String.format(";namespace=%s;", namespace), "");
}

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ public static void clean(final String namespace) {
}


// 全局序列
private static volatile int sequence = 1000;

/**
* 生成全局唯一序列,
* 在JVM-SANDBOX中允许多个命名空间的存在,不同的命名空间下listenerId/objectId将会被植入到同一份字节码中,
* 此时需要用全局的ID生成策略规避不同的命名空间
*
* @return 全局自增序列
*/
public static synchronized int nextSequence() {
return sequence++;
}


private static void handleException(Throwable cause) throws Throwable {
if (isSpyThrowException) {
throw cause;
Expand Down

0 comments on commit 45f674d

Please sign in to comment.