fixed a bug in fs translog where it wasn't seeking correctly, and not counting the number of operations

This commit is contained in:
kimchy 2010-07-08 22:18:26 +03:00
parent ef148077fc
commit be6aaa157f
4 changed files with 8 additions and 2 deletions

View File

@ -78,6 +78,8 @@ public class FsSnapshot implements Translog.Snapshot {
int opSize = dis.readInt();
position += 4;
if ((position + opSize) > length) {
// restore the position to before we read the opSize
position -= 4;
return false;
}
position += opSize;

View File

@ -108,6 +108,7 @@ public class FsTranslog extends AbstractIndexShardComponent implements Translog
bosOs.flush();
raf.raf().writeInt(bos.size());
raf.raf().write(bos.unsafeByteArray(), 0, bos.size());
operationCounter.incrementAndGet();
} catch (Exception e) {
throw new TranslogException(shardId, "Failed to write operation [" + operation + "]", e);
}
@ -134,7 +135,7 @@ public class FsTranslog extends AbstractIndexShardComponent implements Translog
FsSnapshot fsSnapshot = (FsSnapshot) snapshot;
raf.increaseRefCount();
FsSnapshot newSnapshot = new FsSnapshot(shardId, id, raf, raf.raf().getFilePointer());
newSnapshot.seekForward(fsSnapshot.length());
newSnapshot.seekForward(fsSnapshot.position());
return newSnapshot;
} catch (IOException e) {
throw new TranslogException(shardId, "Failed to snapshot", e);

View File

@ -95,7 +95,7 @@ public class MemoryTranslog extends AbstractIndexShardComponent implements Trans
return snapshot();
}
MemorySnapshot newSnapshot = new MemorySnapshot(currentId(), operations, operationCounter.get());
newSnapshot.seekForward(memorySnapshot.length());
newSnapshot.seekForward(memorySnapshot.position());
return newSnapshot;
}
}

View File

@ -124,6 +124,9 @@ public abstract class AbstractSimpleTranslogTests {
snapshot.release();
Translog.Snapshot snapshot1 = translog.snapshot();
// we use the translogSize to also navigate to the last position on this snapshot
// so snapshot(Snapshot) will work properly
assertThat(snapshot1, translogSize(1));
translog.add(new Translog.Index("test", "2", new byte[]{2}));
snapshot = translog.snapshot(snapshot1);