AsyncResult 和处理回滚

AsyncResult and handling rollback

(警告:这也在 https://forums.fsharp.org/t/asyncresult-and-handling-rollback/928 上发布)

尝试在 F# 中实现类似 2PC 事务的工作流(参见 http://learnmongodbthehardway.com/article/transactions/)并遇到计算表达式(例如 asyncResult)和回滚问题。

如果你有如下伪代码:

let rollbackWorkflow parX parY =
… here calling rollbackService1 and rollbackService2

let executeWorkflow par1 par2 par3 =
asyncResult {
let! result1 = callService1 x y z
let! result2 = callService2 x2 y2 z2
}

如果 result1 and/or result2 出错,如何检查 executeWorkflow 然后调用 rollbackWorkflow 函数?我应该更改 callService1 和 callService2 以引发异常而不是在预期的错误情况下返回结果(资金不足,超出限制),还是应该使用像 teeError 这样的函数?非常感谢任何建议!

P.S。这就是我最终想要实现的:

function executeTransaction(from, to, amount) {
var transactionId = ObjectId();

transactions.insert({
_id: transactionId,
source: from,
destination: to,
amount: amount,
state: “initial”
});

var result = transactions.updateOne(
{ _id: transactionId },
{ $set: { state: “pending” } }
);

if (result.modifiedCount == 0) {
cancel(transactionId);
throw Error(“Failed to move transaction " + transactionId + " to pending”);
}

// Set up pending debit
result = accounts.updateOne({
name: from,
pendingTransactions: { $ne: transactionId },
balance: { $gte: amount }
}, {
$inc: { balance: -amount },
$push: { pendingTransactions: transactionId }
});

if (result.modifiedCount == 0) {
rollback(from, to, amount, transactionId);
throw Error(“Failed to debit " + from + " account”);
}

// Setup pending credit
result = accounts.updateOne({
name: to,
pendingTransactions: { $ne: transactionId }
}, {
$inc: { balance: amount },
$push: { pendingTransactions: transactionId }
});

if (result.modifiedCount == 0) {
rollback(from, to, amount, transactionId);
throw Error(“Failed to credit " + to + " account”);
}

// Update transaction to committed
result = transactions.updateOne(
{ _id: transactionId },
{ $set: { state: “committed” } }
);

if (result.modifiedCount == 0) {
rollback(from, to, amount, transactionId);
throw Error(“Failed to move transaction " + transactionId + " to committed”);
}

// Attempt cleanup
cleanup(from, to, transactionId);
}

executeTransaction(“Joe Moneylender”, “Peter Bum”, 100);

只需在工作流之外进行一些错误处理,例如:

type TransactionError =
    | NoFunds
    | Other

let rollbackWorkflow parX parY = async.Return ( printfn "here calling rollbackService1 and rollbackService2"; Ok -1 )
let callService1 parX parY = async.Return ( printfn "callService1"; if parX + parY > 0 then Ok 1 else Error NoFunds )
let callService2 parX parY = async.Return ( printfn "callService2"; if parX + parY > 0 then Ok 2 else Error Other )

let executeWorkflow par1 par2 par3 =
    asyncResult {
        let! result1 = callService1 par1 par2
        let! result2 = callService2 result1 par3
        return result2
        } |> AsyncResult.bindError (fun x -> if x = NoFunds then rollbackWorkflow 0 1 else rollbackWorkflow 1 0)

我用您链接的代码中的 AsyncResult 编写了该示例。加上 bindError 应该是这样的:

/// Apply a monadic function to an AsyncResult error  
let bindError (f: 'a -> AsyncResult<'b,'c>) (xAsyncResult : AsyncResult<_, _>) :AsyncResult<_,_> = async {
    let! xResult = xAsyncResult 
    match xResult with
    | Ok x -> return Ok x
    | Error err -> return! f err
    }

如果您考虑一下,bindError 就像是 catch 函数的纯版本,例如 this code fragment,使用另一个库。