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

another minor Transaction code cleanup

上级 32413343
......@@ -60,7 +60,7 @@ public class Transaction {
*/
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"
};
static final int LOG_ID_BITS = 40;
......@@ -90,7 +90,7 @@ public class Transaction {
/**
* 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:
......@@ -236,7 +236,8 @@ public class Transaction {
}
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 {
public void rollbackToSavepoint(long savepointId) {
long lastState = setStatus(STATUS_ROLLING_BACK);
long logId = getLogId(lastState);
boolean success;
try {
store.rollbackTo(this, logId, savepointId);
} finally {
notifyAllWaitingTransactions();
long expectedState = composeState(STATUS_ROLLING_BACK, logId, hasRollback(lastState));
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(
DataUtils.ERROR_TRANSACTION_ILLEGAL_STATE,
"Transaction {0} concurrently modified " +
"while rollback to savepoint was in progress",
"Transaction {0} concurrently modified while rollback to savepoint was in progress",
transactionId);
}
}
}
/**
* Roll the transaction back. Afterwards, this transaction is closed.
......@@ -556,8 +561,15 @@ public class Transaction {
@Override
public String toString() {
long state = statusAndLogId.get();
return transactionId + "(" + sequenceNum + ") " + STATUS_NAMES[getStatus(state)] + " " + getLogId(state);
return transactionId + "(" + sequenceNum + ") " + stateToString();
}
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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论