提交 d8701908 authored 作者: Andrei Tokar's avatar Andrei Tokar

another minor Transaction code cleanup

上级 32413343
...@@ -60,7 +60,7 @@ public class Transaction { ...@@ -60,7 +60,7 @@ public class Transaction {
*/ */
private static final int STATUS_ROLLED_BACK = 5; private static final int STATUS_ROLLED_BACK = 5;
private static final String STATUS_NAMES[] = { private static final String[] STATUS_NAMES = {
"CLOSED", "OPEN", "PREPARED", "COMMITTED", "ROLLING_BACK", "ROLLED_BACK" "CLOSED", "OPEN", "PREPARED", "COMMITTED", "ROLLING_BACK", "ROLLED_BACK"
}; };
static final int LOG_ID_BITS = 40; static final int LOG_ID_BITS = 40;
...@@ -90,7 +90,7 @@ public class Transaction { ...@@ -90,7 +90,7 @@ public class Transaction {
/** /**
* This is really a transaction identity, because it's not re-used. * This is really a transaction identity, because it's not re-used.
*/ */
public final long sequenceNum; final long sequenceNum;
/* /*
* Transaction state is an atomic composite field: * Transaction state is an atomic composite field:
...@@ -236,7 +236,8 @@ public class Transaction { ...@@ -236,7 +236,8 @@ public class Transaction {
} }
public int getBlockerId() { public int getBlockerId() {
return blockingTransaction == null ? 0 : blockingTransaction.ownerId; Transaction blocker = this.blockingTransaction;
return blocker == null ? 0 : blocker.ownerId;
} }
/** /**
...@@ -389,21 +390,25 @@ public class Transaction { ...@@ -389,21 +390,25 @@ public class Transaction {
public void rollbackToSavepoint(long savepointId) { public void rollbackToSavepoint(long savepointId) {
long lastState = setStatus(STATUS_ROLLING_BACK); long lastState = setStatus(STATUS_ROLLING_BACK);
long logId = getLogId(lastState); long logId = getLogId(lastState);
boolean success;
try { try {
store.rollbackTo(this, logId, savepointId); store.rollbackTo(this, logId, savepointId);
} finally { } finally {
notifyAllWaitingTransactions(); notifyAllWaitingTransactions();
long expectedState = composeState(STATUS_ROLLING_BACK, logId, hasRollback(lastState)); long expectedState = composeState(STATUS_ROLLING_BACK, logId, hasRollback(lastState));
long newState = composeState(STATUS_OPEN, savepointId, true); long newState = composeState(STATUS_OPEN, savepointId, true);
if (!statusAndLogId.compareAndSet(expectedState, newState)) { do {
success = statusAndLogId.compareAndSet(expectedState, newState);
} while (!success && statusAndLogId.get() == expectedState);
}
// this is moved outside of finally block to avert masking original exception, if any
if (!success) {
throw DataUtils.newIllegalStateException( throw DataUtils.newIllegalStateException(
DataUtils.ERROR_TRANSACTION_ILLEGAL_STATE, DataUtils.ERROR_TRANSACTION_ILLEGAL_STATE,
"Transaction {0} concurrently modified " + "Transaction {0} concurrently modified while rollback to savepoint was in progress",
"while rollback to savepoint was in progress",
transactionId); transactionId);
} }
} }
}
/** /**
* Roll the transaction back. Afterwards, this transaction is closed. * Roll the transaction back. Afterwards, this transaction is closed.
...@@ -556,8 +561,15 @@ public class Transaction { ...@@ -556,8 +561,15 @@ public class Transaction {
@Override @Override
public String toString() { public String toString() {
long state = statusAndLogId.get(); return transactionId + "(" + sequenceNum + ") " + stateToString();
return transactionId + "(" + sequenceNum + ") " + STATUS_NAMES[getStatus(state)] + " " + getLogId(state); }
private String stateToString() {
return stateToString(statusAndLogId.get());
}
private static String stateToString(long state) {
return STATUS_NAMES[getStatus(state)] + (hasRollback(state) ? "" : "!") + " " + getLogId(state);
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论