com.alibaba.jvm.sandbox.api.ProcessControlException Java Examples

The following examples show how to use com.alibaba.jvm.sandbox.api.ProcessControlException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultEventListener.java    From jvm-sandbox-repeater with Apache License 2.0 5 votes vote down vote up
/**
 * 处理before事件
 *
 * @param event before事件
 */
protected void doBefore(BeforeEvent event) throws ProcessControlException {
    // 回放流量;如果是入口则放弃;子调用则进行mock
    if (RepeatCache.isRepeatFlow(Tracer.getTraceId())) {
        processor.doMock(event, entrance, invokeType);
        return;
    }
    Invocation invocation = initInvocation(event);
    invocation.setStart(System.currentTimeMillis());
    invocation.setTraceId(Tracer.getTraceId());
    invocation.setIndex(entrance ? 0 : SequenceGenerator.generate(Tracer.getTraceId()));
    invocation.setIdentity(processor.assembleIdentity(event));
    invocation.setEntrance(entrance);
    invocation.setType(invokeType);
    invocation.setProcessId(event.processId);
    invocation.setInvokeId(event.invokeId);
    invocation.setRequest(processor.assembleRequest(event));
    invocation.setResponse(processor.assembleResponse(event));
    invocation.setSerializeToken(ClassloaderBridge.instance().encode(event.javaClassLoader));
    try {
        // fix issue#14 : useGeneratedKeys
        if (processor.inTimeSerializeRequest(invocation, event)) {
            SerializerWrapper.inTimeSerialize(invocation);
        }
    } catch (SerializeException e) {
        Tracer.getContext().setSampled(false);
        log.error("Error occurred serialize", e);
    }
    RecordCache.cacheInvocation(event.invokeId, invocation);
}
 
Example #2
Source File: HttpStandaloneListener.java    From jvm-sandbox-repeater with Apache License 2.0 5 votes vote down vote up
@Override
protected void doBefore(BeforeEvent event) throws ProcessControlException {
    // 回放流量;入口直接返回
    if (RepeatCache.isRepeatFlow(Tracer.getTraceId())) {
        return;
    }
    Object request = event.argumentArray[0];
    Object response = event.argumentArray[1];
    if (!(request instanceof HttpServletRequest && response instanceof HttpServletResponse)) {
        return;
    }
    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse resp = (HttpServletResponse) response;
    // 根据 requestURI 进行采样匹配
    List<String> patterns = ApplicationModel.instance().getConfig().getHttpEntrancePatterns();
    if (!matchRequestURI(patterns, req.getRequestURI())) {
        LogUtil.debug("current uri {} can't match any httpEntrancePatterns, ignore this request", req.getRequestURI());
        Tracer.getContext().setSampled(false);
        return;
    }
    WrapperResponseCopier wrapperRes = new WrapperResponseCopier(resp);
    WrapperRequest wrapperReq;
    try {
        wrapperReq = new WrapperRequest(req, wrapperRes, this);
    } catch (IOException e) {
        LogUtil.error("error occurred when assemble wrapper request", e);
        Tracer.getContext().setSampled(false);
        return;
    }
    WrapperTransModel wtm = WrapperTransModel.build(wrapperReq);
    wtm.setBody(wrapperReq.getBody());
    wtm.copier = wrapperRes;
    wtm.request = wrapperReq;
    onRequest(wrapperReq, event);
    event.argumentArray[0] = wrapperReq;
    event.argumentArray[1] = wrapperRes;
    wtmRef.set(wtm);
}
 
Example #3
Source File: DefaultEventListener.java    From jvm-sandbox-repeater with Apache License 2.0 4 votes vote down vote up
@Override
public void onEvent(Event event) throws Throwable {
    try {
        /*
         * event过滤;针对单个listener,只处理top的事件
         */
        if (!isTopEvent(event)) {
            if (log.isDebugEnabled()) {
                log.debug("not top event ,type={},event={},offset={}", invokeType, event, eventOffset.get().get());
            }
            return;
        }
        /*
         * 初始化Tracer
         */
        initContext(event);
        /*
         * 执行基础过滤
         */
        if (!access(event)) {
            if (log.isDebugEnabled()) {
                log.debug("event access failed,type={},event={}", invokeType, event);
            }
            return;
        }
        /*
         * 执行采样计算(只有entrance插件负责计算采样,子调用插件不计算)
         */
        if (!sample(event)) {
            if (log.isDebugEnabled()) {
                log.debug("event missing sample rule,type={},event={}", invokeType, event);
            }
            return;
        }
        /*
         * processor filter
         */
        if (processor != null && processor.ignoreEvent((InvokeEvent) event)) {
            if (log.isDebugEnabled()) {
                log.debug("event is ignore by processor,type={},event={},processor={}", invokeType, event, processor);
            }
            return;
        }
        /*
         * 分发事件处理(对于一次around事件可以收集到入参/返回值的可以直接使用;需要从多次before实践获取的)
         */
        switch (event.type) {
            case BEFORE:
                doBefore((BeforeEvent) event);
                break;
            case RETURN:
                doReturn((ReturnEvent) event);
                break;
            case THROWS:
                doThrow((ThrowsEvent) event);
                break;
            default:
                break;
        }
    } catch (ProcessControlException pe) {
        /*
         * sandbox流程干预
         */
        // process control 会中断事件,不会有return/throw事件过来,因此需要清除偏移量
        eventOffset.remove();
        throw pe;
    } catch (Throwable throwable) {
        // uncaught exception
        log.error("[Error-0000]-uncaught exception occurred when dispatch event,type={},event={}", invokeType, event, throwable);
        ApplicationModel.instance().exceptionOverflow(throwable);
    } finally {
        /*
         * 入口插件 && 完成事件
         */
        clearContext(event);
    }
}
 
Example #4
Source File: EventListenerHandler.java    From jvm-sandbox with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void compensateProcessControlEvent(ProcessControlException pce, EventProcessor processor, EventProcessor.Process process, Event event) {

        // 核对是否需要补偿,如果目标监听器没监听过这类事件,则不需要进行补偿
        if (!(event instanceof InvokeEvent)
                || !contains(processor.eventTypes, event.type)) {
            return;
        }

        final InvokeEvent iEvent = (InvokeEvent) event;
        final Event compensateEvent;

        // 补偿立即返回事件
        if (pce.getState() == ProcessControlException.State.RETURN_IMMEDIATELY
                && contains(processor.eventTypes, IMMEDIATELY_RETURN)) {
            compensateEvent = process
                    .getEventFactory()
                    .makeImmediatelyReturnEvent(iEvent.processId, iEvent.invokeId, pce.getRespond());
        }

        // 补偿立即抛出事件
        else if (pce.getState() == ProcessControlException.State.THROWS_IMMEDIATELY
                && contains(processor.eventTypes, IMMEDIATELY_THROWS)) {
            compensateEvent = process
                    .getEventFactory()
                    .makeImmediatelyThrowsEvent(iEvent.processId, iEvent.invokeId, (Throwable) pce.getRespond());
        }

        // 异常情况不补偿
        else {
            return;
        }

        try {
            logger.debug("compensate-event: event|{}|{}|{}|{} when ori-event:{}",
                    compensateEvent.type,
                    iEvent.processId,
                    iEvent.invokeId,
                    processor.listenerId,
                    event.type
            );
            processor.listener.onEvent(compensateEvent);
        } catch (Throwable cause) {
            logger.warn("compensate-event: event|{}|{}|{}|{} when ori-event:{} occur error.",
                    compensateEvent.type,
                    iEvent.processId,
                    iEvent.invokeId,
                    processor.listenerId,
                    event.type,
                    cause
            );
        } finally {
            process.getEventFactory().returnEvent(compensateEvent);
        }
    }
 
Example #5
Source File: InvocationProcessor.java    From jvm-sandbox-repeater with Apache License 2.0 2 votes vote down vote up
/**
 * 执行mock功能
 *
 * @param event    before事件
 * @param entrance 是否入口
 * @param type     调用类型
 * @throws ProcessControlException 流程控制异常
 */
void doMock(BeforeEvent event, Boolean entrance, InvokeType type) throws ProcessControlException;