From be6aaa157f66a1761169b4205c696fdf1be2df10 Mon Sep 17 00:00:00 2001 From: kimchy Date: Thu, 8 Jul 2010 22:18:26 +0300 Subject: [PATCH] fixed a bug in fs translog where it wasn't seeking correctly, and not counting the number of operations --- .../java/org/elasticsearch/index/translog/fs/FsSnapshot.java | 2 ++ .../java/org/elasticsearch/index/translog/fs/FsTranslog.java | 3 ++- .../elasticsearch/index/translog/memory/MemoryTranslog.java | 2 +- .../index/translog/AbstractSimpleTranslogTests.java | 3 +++ 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/translog/fs/FsSnapshot.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/translog/fs/FsSnapshot.java index e2dff0f3de6..6af94ed1d44 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/translog/fs/FsSnapshot.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/translog/fs/FsSnapshot.java @@ -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; diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/translog/fs/FsTranslog.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/translog/fs/FsTranslog.java index 0afecd3ef8b..f24b54ccb84 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/translog/fs/FsTranslog.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/translog/fs/FsTranslog.java @@ -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); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/translog/memory/MemoryTranslog.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/translog/memory/MemoryTranslog.java index 6d6d7e17e73..af0a112e6d1 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/translog/memory/MemoryTranslog.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/translog/memory/MemoryTranslog.java @@ -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; } } diff --git a/modules/elasticsearch/src/test/java/org/elasticsearch/index/translog/AbstractSimpleTranslogTests.java b/modules/elasticsearch/src/test/java/org/elasticsearch/index/translog/AbstractSimpleTranslogTests.java index 861537f9299..15ac5ca2840 100644 --- a/modules/elasticsearch/src/test/java/org/elasticsearch/index/translog/AbstractSimpleTranslogTests.java +++ b/modules/elasticsearch/src/test/java/org/elasticsearch/index/translog/AbstractSimpleTranslogTests.java @@ -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);