提交 b56c16ef authored 作者: andrei's avatar andrei

minor refactoring between Transaction and TransactionStore

上级 d5478abd
......@@ -230,7 +230,7 @@ public class Transaction {
* @param key the key
* @param oldValue the old value
*/
void log(int mapId, Object key, VersionedValue oldValue) {
long log(int mapId, Object key, VersionedValue oldValue) {
long currentState = statusAndLogId.getAndIncrement();
long logId = getLogId(currentState);
if (logId >= LOG_ID_LIMIT) {
......@@ -239,7 +239,10 @@ public class Transaction {
"Transaction {0} has too many changes",
transactionId);
}
store.log(this, logId, mapId, key, oldValue);
int currentStatus = getStatus(currentState);
checkOpen(currentStatus);
long undoKey = store.addUndoLogRecord(transactionId, logId, new Object[]{ mapId, key, oldValue });
return undoKey;
}
/**
......@@ -254,7 +257,9 @@ public class Transaction {
"Transaction {0} has internal error",
transactionId);
}
store.logUndo(this, logId);
int currentStatus = getStatus(currentState);
checkOpen(currentStatus);
store.removeUndoLogRecord(transactionId, logId);
}
/**
......@@ -313,11 +318,29 @@ public class Transaction {
*/
public void commit() {
assert store.openTransactions.get().get(transactionId);
long state = setStatus(STATUS_COMMITTING);
long logId = Transaction.getLogId(state);
boolean hasChanges = hasChanges(state);
store.commit(this, logId, hasChanges);
Throwable ex = null;
boolean hasChanges = false;
try {
long state = setStatus(STATUS_COMMITTING);
long logId = getLogId(state);
hasChanges = hasChanges(state);
if (hasChanges) {
store.commit(this, logId);
}
} catch (Throwable e) {
ex = e;
throw e;
} finally {
try {
store.endTransaction(this, hasChanges);
} catch (Throwable e) {
if (ex == null) {
throw e;
} else {
ex.addSuppressed(e);
}
}
}
}
/**
......@@ -376,13 +399,24 @@ public class Transaction {
return getLogId(statusAndLogId.get());
}
/**
* Check whether this transaction is open.
*/
private void checkOpen(int status) {
if (status != STATUS_OPEN) {
throw DataUtils.newIllegalStateException(
DataUtils.ERROR_TRANSACTION_ILLEGAL_STATE,
"Transaction {0} has status {1}, not open", transactionId, status);
}
}
/**
* Check whether this transaction is open or prepared.
*/
void checkNotClosed() {
if (getStatus() == STATUS_CLOSED) {
throw DataUtils.newIllegalStateException(
DataUtils.ERROR_CLOSED, "Transaction is closed");
DataUtils.ERROR_CLOSED, "Transaction {0} is closed", transactionId);
}
}
......
......@@ -366,18 +366,14 @@ public class TransactionStore {
}
/**
* Log an entry.
* Add an undoLog entry.
*
* @param t the transaction
* @param logId the log id
* @param mapId the map id
* @param key the key
* @param oldValue the old value
*/
long log(Transaction t, long logId, int mapId,
Object key, Object oldValue) {
Long undoKey = getOperationId(t.getId(), logId);
Object[] log = { mapId, key, oldValue };
* @param transactionId id of the transaction
* @param logId sequential number of the log record within transaction
* @param undoLogRecord Object[]
*/
long addUndoLogRecord(int transactionId, long logId, Object[] undoLogRecord) {
Long undoKey = getOperationId(transactionId, logId);
rwLock.writeLock().lock();
try {
if (logId == 0) {
......@@ -386,10 +382,10 @@ public class TransactionStore {
DataUtils.ERROR_TOO_MANY_OPEN_TRANSACTIONS,
"An old transaction with the same id " +
"is still open: {0}",
t.getId());
transactionId);
}
}
undoLog.put(undoKey, log);
undoLog.put(undoKey, undoLogRecord);
} finally {
rwLock.writeLock().unlock();
}
......@@ -399,11 +395,11 @@ public class TransactionStore {
/**
* Remove a log entry.
*
* @param t the transaction
* @param logId the log id
* @param transactionId id of the transaction
* @param logId sequential number of the log record within transaction
*/
public void logUndo(Transaction t, long logId) {
Long undoKey = getOperationId(t.getId(), logId);
public void removeUndoLogRecord(int transactionId, long logId) {
Long undoKey = getOperationId(transactionId, logId);
rwLock.writeLock().lock();
try {
Object[] old = undoLog.remove(undoKey);
......@@ -411,7 +407,7 @@ public class TransactionStore {
throw DataUtils.newIllegalStateException(
DataUtils.ERROR_TRANSACTION_ILLEGAL_STATE,
"Transaction {0} was concurrently rolled back",
t.getId());
transactionId);
}
} finally {
rwLock.writeLock().unlock();
......@@ -435,11 +431,8 @@ public class TransactionStore {
*
* @param t the transaction
* @param maxLogId the last log id
* @param hasChanges true if there were updates within specified
* transaction (even fully rolled back),
* false if just data access
*/
void commit(Transaction t, long maxLogId, boolean hasChanges) {
void commit(Transaction t, long maxLogId) {
if (store.isClosed()) {
return;
}
......@@ -487,7 +480,6 @@ public class TransactionStore {
rwLock.writeLock().unlock();
flipCommittingTransactionsBit(transactionId, false);
}
endTransaction(t, hasChanges);
}
private void flipCommittingTransactionsBit(int transactionId, boolean flag) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论