Author: borges
Date: 2011-08-01 05:35:28 -0400 (Mon, 01 Aug 2011)
New Revision: 11079
Modified:
branches/HORNETQ-720_Replication/hornetq-journal/src/main/java/org/hornetq/core/journal/impl/JournalFilesRepository.java
Log:
HORNETQ-720 FIX race condition when setting next fileID
Modified:
branches/HORNETQ-720_Replication/hornetq-journal/src/main/java/org/hornetq/core/journal/impl/JournalFilesRepository.java
===================================================================
---
branches/HORNETQ-720_Replication/hornetq-journal/src/main/java/org/hornetq/core/journal/impl/JournalFilesRepository.java 2011-08-01
04:54:03 UTC (rev 11078)
+++
branches/HORNETQ-720_Replication/hornetq-journal/src/main/java/org/hornetq/core/journal/impl/JournalFilesRepository.java 2011-08-01
09:35:28 UTC (rev 11079)
@@ -150,22 +150,31 @@
for (JournalFile file : files)
{
- long fileID = file.getFileID();
- if (nextFileID.get() < fileID)
- {
- nextFileID.set(fileID);
- }
+ final long fileIdFromFile = file.getFileID();
+ final long fileIdFromName = getFileNameID(file.getFile().getFileName());
- long fileNameID = getFileNameID(file.getFile().getFileName());
-
// The compactor could create a fileName but use a previously assigned ID.
// Because of that we need to take both parts into account
- if (nextFileID.get() < fileNameID)
- {
- nextFileID.set(fileNameID);
- }
+ setNextFileID(Math.max(fileIdFromName, fileIdFromFile));
}
+ }
+ /**
+ * Set the {link #nextFileID} value to {@code targetUpdate} if the current value is
less than
+ * {@code targetUpdate}.
+ * @param targetUpdate
+ */
+ public void setNextFileID(final long targetUpdate)
+ {
+ while (true)
+ {
+ final long current = nextFileID.get();
+ if (current >= targetUpdate)
+ return;
+
+ if (nextFileID.compareAndSet(current, targetUpdate))
+ return;
+ }
}
public void ensureMinFiles() throws Exception
@@ -536,11 +545,6 @@
return nextFileID.incrementAndGet();
}
- public void setNextFileID(long value)
- {
- nextFileID.set(value);
- }
-
/** Get the ID part of the name */
private long getFileNameID(final String fileName)
{
Show replies by date