Search in sources :

Example 21 with BStruct

use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.

the class CPU method beginTransaction.

private static void beginTransaction(WorkerExecutionContext ctx, int transactionBlockId, int retryCountRegIndex, int committedFuncIndex, int abortedFuncIndex) {
    // If global tx enabled, it is managed via transaction coordinator. Otherwise it is managed locally without
    // any interaction with the transaction coordinator.
    boolean isGlobalTransactionEnabled = ctx.getGlobalTransactionEnabled();
    // Transaction is attempted three times by default to improve resiliency
    int retryCount = TransactionConstants.DEFAULT_RETRY_COUNT;
    if (retryCountRegIndex != -1) {
        retryCount = (int) ctx.workerLocal.longRegs[retryCountRegIndex];
        if (retryCount < 0) {
            ctx.setError(BLangVMErrors.createError(ctx, BLangExceptionHelper.getErrorMessage(RuntimeErrors.INVALID_RETRY_COUNT)));
            handleError(ctx);
            return;
        }
    }
    // Register committed function handler if exists.
    if (committedFuncIndex != -1) {
        FunctionRefCPEntry funcRefCPEntry = (FunctionRefCPEntry) ctx.constPool[committedFuncIndex];
        BFunctionPointer fpCommitted = new BFunctionPointer(funcRefCPEntry);
        TransactionResourceManager.getInstance().registerCommittedFunction(transactionBlockId, fpCommitted);
    }
    // Register aborted function handler if exists.
    if (abortedFuncIndex != -1) {
        FunctionRefCPEntry funcRefCPEntry = (FunctionRefCPEntry) ctx.constPool[abortedFuncIndex];
        BFunctionPointer fpAborted = new BFunctionPointer(funcRefCPEntry);
        TransactionResourceManager.getInstance().registerAbortedFunction(transactionBlockId, fpAborted);
    }
    LocalTransactionInfo localTransactionInfo = ctx.getLocalTransactionInfo();
    if (localTransactionInfo == null) {
        String globalTransactionId;
        String protocol = null;
        String url = null;
        if (isGlobalTransactionEnabled) {
            BValue[] returns = TransactionUtils.notifyTransactionBegin(ctx, null, null, transactionBlockId, TransactionConstants.DEFAULT_COORDINATION_TYPE);
            BStruct txDataStruct = (BStruct) returns[0];
            globalTransactionId = txDataStruct.getStringField(1);
            protocol = txDataStruct.getStringField(2);
            url = txDataStruct.getStringField(3);
        } else {
            globalTransactionId = UUID.randomUUID().toString().replaceAll("-", "");
        }
        localTransactionInfo = new LocalTransactionInfo(globalTransactionId, url, protocol);
        ctx.setLocalTransactionInfo(localTransactionInfo);
    } else {
        if (isGlobalTransactionEnabled) {
            TransactionUtils.notifyTransactionBegin(ctx, localTransactionInfo.getGlobalTransactionId(), localTransactionInfo.getURL(), transactionBlockId, localTransactionInfo.getProtocol());
        }
    }
    localTransactionInfo.beginTransactionBlock(transactionBlockId, retryCount);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) FunctionRefCPEntry(org.ballerinalang.util.codegen.cpentries.FunctionRefCPEntry) LocalTransactionInfo(org.ballerinalang.util.transactions.LocalTransactionInfo) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BFunctionPointer(org.ballerinalang.model.values.BFunctionPointer)

Example 22 with BStruct

use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.

the class CallableWorkerResponseContext method propagateErrorToTarget.

protected WorkerExecutionContext propagateErrorToTarget() {
    BStruct error = BLangVMErrors.createCallFailedException(this.targetCtx, this.getWorkerErrors());
    WorkerExecutionContext ctx = this.onFinalizedError(this.targetCtx, error);
    this.doFailCallbackNotify(error);
    return ctx;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct)

Example 23 with BStruct

use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.

the class AsyncInvocableWorkerResponseContext method sendAsyncCancelErrorSignal.

private void sendAsyncCancelErrorSignal() {
    WorkerData result = BLangVMUtils.createWorkerData(this.callableUnitInfo.retWorkerIndex);
    BStruct error = BLangVMErrors.createCallCancelledException(this.callableUnitInfo);
    WorkerExecutionContext ctx = this.signal(new WorkerSignal(new WorkerExecutionContext(error), SignalType.ERROR, result));
    BLangScheduler.resume(ctx);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct)

Example 24 with BStruct

use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.

the class BLangVMErrors method getCasueStackTrace.

public static String getCasueStackTrace(BStruct error) {
    StringBuilder sb = new StringBuilder();
    // Get error type name and the message (if any)
    String errorMsg = getErrorMessage(error);
    sb.append(errorMsg).append("\n\tat ");
    BStruct stackFrame = (BStruct) error.getNativeData(STRUCT_CALL_STACK_ELEMENT);
    // Append function/action/resource name with package path (if any)
    if (stackFrame.getStringField(1).isEmpty() || stackFrame.getStringField(1).equals(PACKAGE_BUILTIN)) {
        sb.append(stackFrame.getStringField(0));
    } else {
        sb.append(stackFrame.getStringField(1)).append(":").append(stackFrame.getStringField(0));
    }
    // Append the filename
    sb.append("(").append(stackFrame.getStringField(2));
    // Append the line number
    if (stackFrame.getIntField(0) > 0) {
        sb.append(":").append(stackFrame.getIntField(0));
    }
    sb.append(")");
    BRefValueArray cause = (BRefValueArray) error.getRefField(0);
    if (cause != null && cause.size() > 0) {
        sb.append("\ncaused by ").append(getCauseStackTraceArray(cause));
    }
    return sb.toString();
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BRefValueArray(org.ballerinalang.model.values.BRefValueArray)

Example 25 with BStruct

use of org.ballerinalang.model.values.BStruct in project ballerina by ballerina-lang.

the class BLangVMStructs method createBStruct.

/**
 * Create BStruct for given StructInfo and BValues.
 *
 * @param structInfo {@link StructInfo} of the BStruct
 * @param values     field values of the BStruct.
 * @return BStruct instance.
 */
public static BStruct createBStruct(StructInfo structInfo, Object... values) {
    BStructType structType = structInfo.getType();
    BStruct bStruct = new BStruct(structType);
    int[] indexes = new int[] { -1, -1, -1, -1, -1, -1 };
    BStructType.StructField[] structFields = structType.getStructFields();
    for (int i = 0; i < structFields.length; i++) {
        if (values.length < i + 1) {
            break;
        }
        BType paramType = structFields[i].getFieldType();
        setValue(bStruct, indexes, paramType.getTag(), values[i]);
    }
    return bStruct;
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) BStruct(org.ballerinalang.model.values.BStruct) BType(org.ballerinalang.model.types.BType)

Aggregations

BStruct (org.ballerinalang.model.values.BStruct)460 BValue (org.ballerinalang.model.values.BValue)187 Test (org.testng.annotations.Test)161 BString (org.ballerinalang.model.values.BString)131 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)53 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)39 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)37 BInteger (org.ballerinalang.model.values.BInteger)33 BMap (org.ballerinalang.model.values.BMap)29 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)26 BStructType (org.ballerinalang.model.types.BStructType)25 IOException (java.io.IOException)23 BBoolean (org.ballerinalang.model.values.BBoolean)23 BJSON (org.ballerinalang.model.values.BJSON)22 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)21 StructInfo (org.ballerinalang.util.codegen.StructInfo)21 EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)20 File (java.io.File)17 PackageInfo (org.ballerinalang.util.codegen.PackageInfo)17 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)16