Author: borges
Date: 2011-08-02 10:18:22 -0400 (Tue, 02 Aug 2011)
New Revision: 11095
Modified:
trunk/hornetq-journal/src/main/java/org/hornetq/core/journal/impl/JournalImpl.java
Log:
Fix race condition when manipulating AtomicLong.
Modified:
trunk/hornetq-journal/src/main/java/org/hornetq/core/journal/impl/JournalImpl.java
===================================================================
---
trunk/hornetq-journal/src/main/java/org/hornetq/core/journal/impl/JournalImpl.java 2011-08-02
10:18:08 UTC (rev 11094)
+++
trunk/hornetq-journal/src/main/java/org/hornetq/core/journal/impl/JournalImpl.java 2011-08-02
14:18:22 UTC (rev 11095)
@@ -1913,17 +1913,9 @@
int resultLastPost = JournalImpl.readJournalFile(fileFactory, file, new
JournalReaderCallback()
{
- private void checkID(final long id)
- {
- if (id > maxID.longValue())
- {
- maxID.set(id);
- }
- }
-
public void onReadAddRecord(final RecordInfo info) throws Exception
{
- checkID(info.id);
+ setAtomicLong(info.id, maxID);
hasData.set(true);
@@ -1934,7 +1926,7 @@
public void onReadUpdateRecord(final RecordInfo info) throws Exception
{
- checkID(info.id);
+ setAtomicLong(info.id, maxID);
hasData.set(true);
@@ -1975,7 +1967,7 @@
public void onReadAddRecordTX(final long transactionID, final RecordInfo
info) throws Exception
{
- checkID(info.id);
+ setAtomicLong(info.id, maxID);
hasData.set(true);
@@ -2220,10 +2212,7 @@
{
for (RecordInfo info : transaction.recordInfos)
{
- if (info.id > maxID.get())
- {
- maxID.set(info.id);
- }
+ setAtomicLong(info.id, maxID);
}
PreparedTransactionInfo info = new
PreparedTransactionInfo(transaction.transactionID, transaction.extraData);
@@ -2690,6 +2679,23 @@
}
+ private static final void setAtomicLong(final long target, AtomicLong atomic)
+ {
+ while (true)
+ {
+ long value = atomic.get();
+ if (target > value)
+ {
+ if (atomic.compareAndSet(value, target))
+ return;
+ }
+ else
+ {
+ return;
+ }
+ }
+ }
+
/**
* @param name
* @return