From 926f9ce614307b621d954e8f2d14867f023cbbc2 Mon Sep 17 00:00:00 2001 From: "Hiram R. Chirino" Date: Wed, 15 Aug 2007 14:46:03 +0000 Subject: [PATCH] Control file was not properly loading state when the the record length stored was not == to the max record length. git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@566202 13f79535-47bb-0310-9956-ffa450edef68 --- .../activemq/kaha/impl/async/ControlFile.java | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/activemq-core/src/main/java/org/apache/activemq/kaha/impl/async/ControlFile.java b/activemq-core/src/main/java/org/apache/activemq/kaha/impl/async/ControlFile.java index 3d4844dfbc..3de3d94ec8 100644 --- a/activemq-core/src/main/java/org/apache/activemq/kaha/impl/async/ControlFile.java +++ b/activemq-core/src/main/java/org/apache/activemq/kaha/impl/async/ControlFile.java @@ -39,6 +39,10 @@ public final class ControlFile { /** The File that holds the control data. */ private final RandomAccessFile randomAccessFile; private final int maxRecordSize; + private final int firstRecordStart; + private final int secondRecordStart; + private final int firstRecordEnd; + private final int secondRecordEnd; private long version; private FileLock lock; @@ -47,6 +51,13 @@ public final class ControlFile { public ControlFile(File file, int recordSize) throws IOException { this.file = file; this.maxRecordSize = recordSize + 4; + + // Calculate where the records start and end. + this.firstRecordStart = 8; + this.secondRecordStart = 8 + maxRecordSize + 8 + 8; + this.firstRecordEnd = firstRecordStart+maxRecordSize; + this.secondRecordEnd = secondRecordStart+maxRecordSize; + randomAccessFile = new RandomAccessFile(file, "rw"); } @@ -105,26 +116,26 @@ public final class ControlFile { return null; } - randomAccessFile.seek(0); + randomAccessFile.seek(firstRecordStart-8); long v1 = randomAccessFile.readLong(); - randomAccessFile.seek(maxRecordSize + 8); + randomAccessFile.seek(firstRecordEnd); long v1check = randomAccessFile.readLong(); - randomAccessFile.seek(maxRecordSize + 16); + randomAccessFile.seek(secondRecordStart - 8); long v2 = randomAccessFile.readLong(); - randomAccessFile.seek((maxRecordSize * 2) + 24); + randomAccessFile.seek(secondRecordEnd); long v2check = randomAccessFile.readLong(); byte[] data = null; if (v2 == v2check) { version = v2; - randomAccessFile.seek(maxRecordSize + 24); + randomAccessFile.seek(secondRecordStart); int size = randomAccessFile.readInt(); data = new byte[size]; randomAccessFile.readFully(data); } else if (v1 == v1check) { version = v1; - randomAccessFile.seek(maxRecordSize + 8); + randomAccessFile.seek(firstRecordStart); int size = randomAccessFile.readInt(); data = new byte[size]; randomAccessFile.readFully(data); @@ -147,12 +158,14 @@ public final class ControlFile { randomAccessFile.writeLong(version); randomAccessFile.writeInt(data.getLength()); randomAccessFile.write(data.getData()); + randomAccessFile.seek(firstRecordEnd); randomAccessFile.writeLong(version); // Write the second copy of the control data. randomAccessFile.writeLong(version); randomAccessFile.writeInt(data.getLength()); randomAccessFile.write(data.getData()); + randomAccessFile.seek(secondRecordEnd); randomAccessFile.writeLong(version); if (sync) {