Search in sources :

Example 1 with SyncCallableWorkerResponseContext

use of org.ballerinalang.bre.bvm.SyncCallableWorkerResponseContext in project ballerina by ballerina-lang.

the class BLangFunctions method invokeForkJoin.

public static WorkerExecutionContext invokeForkJoin(WorkerExecutionContext parentCtx, ForkjoinInfo forkjoinInfo, int joinTargetIp, int joinVarReg, int timeoutRegIndex, int timeoutTargetIp, int timeoutVarReg) {
    WorkerInfo[] workerInfos = forkjoinInfo.getWorkerInfos();
    Set<String> joinWorkerNames = new LinkedHashSet<>(Lists.of(forkjoinInfo.getJoinWorkerNames()));
    if (joinWorkerNames.isEmpty()) {
        /* if no join workers are specified, that means, all should be considered */
        joinWorkerNames.addAll(forkjoinInfo.getWorkerInfoMap().keySet());
    }
    Map<String, String> channels = getChannels(forkjoinInfo);
    int reqJoinCount;
    if (forkjoinInfo.getJoinType().equalsIgnoreCase(JOIN_TYPE_SOME)) {
        reqJoinCount = forkjoinInfo.getWorkerCount();
    } else {
        reqJoinCount = joinWorkerNames.size();
    }
    SyncCallableWorkerResponseContext respCtx = new ForkJoinWorkerResponseContext(parentCtx, joinTargetIp, joinVarReg, timeoutTargetIp, timeoutVarReg, workerInfos.length, reqJoinCount, joinWorkerNames, channels);
    if (forkjoinInfo.isTimeoutAvailable()) {
        long timeout = parentCtx.workerLocal.longRegs[timeoutRegIndex];
        // fork join timeout is in seconds, hence converting to milliseconds
        AsyncTimer.schedule(new ForkJoinTimeoutCallback(respCtx), timeout * 1000);
    }
    Map<String, Object> globalProps = parentCtx.globalProps;
    BLangScheduler.workerWaitForResponse(parentCtx);
    for (int i = 1; i < workerInfos.length; i++) {
        executeWorker(respCtx, parentCtx, forkjoinInfo.getArgRegs(), workerInfos[i], globalProps, false);
    }
    return executeWorker(respCtx, parentCtx, forkjoinInfo.getArgRegs(), workerInfos[0], globalProps, true);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ForkJoinTimeoutCallback(org.ballerinalang.bre.bvm.ForkJoinTimeoutCallback) WorkerInfo(org.ballerinalang.util.codegen.WorkerInfo) ForkJoinWorkerResponseContext(org.ballerinalang.bre.bvm.ForkJoinWorkerResponseContext) SyncCallableWorkerResponseContext(org.ballerinalang.bre.bvm.SyncCallableWorkerResponseContext)

Example 2 with SyncCallableWorkerResponseContext

use of org.ballerinalang.bre.bvm.SyncCallableWorkerResponseContext in project ballerina by ballerina-lang.

the class BLangFunctions method invokeCallable.

/**
 * This method does not short circuit the execution of the first worker to execute in the
 * same calling thread, but rather executes all the workers in their own separate threads.
 * This is specifically useful in executing service resources, where the calling transport
 * threads shouldn't be blocked, but rather the worker threads should be used.
 */
public static void invokeCallable(CallableUnitInfo callableUnitInfo, WorkerExecutionContext parentCtx, int[] argRegs, int[] retRegs, CallableUnitCallback responseCallback) {
    WorkerSet workerSet = callableUnitInfo.getWorkerSet();
    SyncCallableWorkerResponseContext respCtx = new SyncCallableWorkerResponseContext(callableUnitInfo.getRetParamTypes(), workerSet.generalWorkers.length);
    respCtx.registerResponseCallback(responseCallback);
    if (TraceManagerWrapper.getInstance().isTraceEnabled()) {
        respCtx.registerResponseCallback(new TraceableCallback(parentCtx));
    }
    respCtx.joinTargetContextInfo(parentCtx, retRegs);
    WorkerDataIndex wdi = callableUnitInfo.retWorkerIndex;
    /* execute the init worker and extract the local variables created by it */
    WorkerData initWorkerLocalData = null;
    CodeAttributeInfo initWorkerCAI = null;
    if (workerSet.initWorker != null) {
        initWorkerLocalData = executeInitWorker(parentCtx, argRegs, callableUnitInfo, workerSet.initWorker, wdi);
        if (initWorkerLocalData == null) {
            handleError(parentCtx);
            return;
        }
        initWorkerCAI = workerSet.initWorker.getCodeAttributeInfo();
    }
    for (int i = 0; i < workerSet.generalWorkers.length; i++) {
        executeWorker(respCtx, parentCtx, argRegs, callableUnitInfo, workerSet.generalWorkers[i], wdi, initWorkerLocalData, initWorkerCAI, false);
    }
}
Also used : WorkerSet(org.ballerinalang.util.codegen.CallableUnitInfo.WorkerSet) CodeAttributeInfo(org.ballerinalang.util.codegen.attributes.CodeAttributeInfo) WorkerData(org.ballerinalang.bre.bvm.WorkerData) TraceableCallback(org.ballerinalang.util.tracer.TraceableCallback) SyncCallableWorkerResponseContext(org.ballerinalang.bre.bvm.SyncCallableWorkerResponseContext)

Aggregations

SyncCallableWorkerResponseContext (org.ballerinalang.bre.bvm.SyncCallableWorkerResponseContext)2 LinkedHashSet (java.util.LinkedHashSet)1 ForkJoinTimeoutCallback (org.ballerinalang.bre.bvm.ForkJoinTimeoutCallback)1 ForkJoinWorkerResponseContext (org.ballerinalang.bre.bvm.ForkJoinWorkerResponseContext)1 WorkerData (org.ballerinalang.bre.bvm.WorkerData)1 WorkerSet (org.ballerinalang.util.codegen.CallableUnitInfo.WorkerSet)1 WorkerInfo (org.ballerinalang.util.codegen.WorkerInfo)1 CodeAttributeInfo (org.ballerinalang.util.codegen.attributes.CodeAttributeInfo)1 TraceableCallback (org.ballerinalang.util.tracer.TraceableCallback)1