by thwarted · 2y ago · 0 comments
Consider this definition from go's database/sql package.
func (tx *Tx) Rollback() error
Rollback can fail, indicated as such by it can return an error. What are the cases where rollback could fail and what's the recovery mechanism? Does a failed rollback mean (logically) that the transaction is still open and uncommitted? But really, invoking rollback can never fail to abort the transaction, so the error result has little use.Now, obviously the error isn't totally useless as an error can be returned to indicate, say, the connection to the database was dropped. But even in that case, the definition of a transaction's validity/lifetime means that even if rollback fails, the transaction is in the same state as if rollback had succeeded.
Rolling back a transaction twice probably indicates an error in your program, which Go's API is giving you a chance to report to yourself.
Ultimately every network request can fail because of the Two Generals' Problem. Actually, every single operation on a computer can fail (as in, do something other than what you were promised or promised yourself it would do). Nothing in life is guaranteed, the environment is adversarial. The fact that we create machines that can be predicted with 0.00000000001% certainty and carve out a safe environment for them and feed them energy is not common and unnatural and all computers will also eventually succumb to entropy. The network is just where that is common enough that considering that another computer can die in our programs can be more useful than completely ignoring the possibility. This possibility is represented as a non-zero number called "error" in the Go API you've posted, so that you have the option of branching your program to a different execution path if the database you're communicating with stops existing or at least stops being reachable.
- Sleep(1) sleeps for exactly 1 second.
- Sleep(1) sleeps for approximately 1 second.
- Sleep(1) sleeps for at least 1 second.
- Sleep(1) sleeps for an unknown, but short amount of time. Certainly not weeks, months or years.
This is correct - Sleep(1) does sleep for approximately 1 second; the problem is you're confusing the 95% confidence interval (which might be as narrow as (999ms,1001ms) for some reasonable designs) with the absolute error bounds (which are (0ms,∞ms], and yes, the infinity end is inclusive).
This is surprisingly common when running on certain VM instance types on public cloud! Common enough that I've had to account for it in production code.
Yes, it means exactly that until something else manages to close the transaction.
Generally that's done by noticing that the connection has failed, or a timeout has expired, assuming "roll back unless confirmed" is the default behavior (usually true, but not always).
The reason you might want to explicitly tell something to roll back is to shorten ^ that latency, or to wait for it to complete before doing something else that requires it to be gone.
Has anybody ever did something useful with that exception?
There's a similar situation in raw socket programming in some OSes: You might be done writing all your outbound data into the socket (i.e. your last write() call returned indicating success), but the receiving application might crash before being able to read all of your data.
By implementing an application-level shutdown command that the other side acknowledges, that can be avoided; maybe some SQL protocol implementations do that, and that exception is thrown if the shutdown request is never acknowledged.
You can ask around and manually coordinate a decision, and tell the databasecwhat you decided.
But that's not the same thing as rollback failing (although it is the invocation of rollback failing) as neither rollback nor commit was fully issued and received, so it makes sense that the transaction would stay open (given that the database doesn't tie transactions to the a connection or a session, and for a two phase commit scenario I'd expect the transaction coordinator to "own" the transaction so it's lifetime isn't necessary tied to a session).
Someone decided to literally pull the plug and replace the master database server node from the rack, while the batch was still running. He assumed the other server nodes would pick up where this one left off. So the batch log of the application first complained about the master disappearing, then about the rollback failing on another master node because it wasn't the coordinator and had no idea of this transaction.
It also means the decision about the commit/rollback was irrelevant, as next week's batch run had deleted the records in question. Presumably, some ephemeral records were hanging around, deciding if they were deleted either in week X or week X+1.
The default final state of a transaction that is not explicitly committed (and even autocommit is still explicit) is that it is aborted, purely because the only way a transaction can be committed is by commiting it vs there are an infinite number of ways and reasons for the transactions to abort. If the rollback is successful or not there is logically no way that the state of the transaction after rollback being invoked is that the transaction is still usable for something.