Author: frm
Date: Tue Jul 26 08:38:41 2016
New Revision: 1754084

URL: http://svn.apache.org/viewvc?rev=1754084&view=rev
Log:
OAK-4596 - Don't flush a segment buffer if it contains only the segment info 
record

Added:
    
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriterTest.java
   (with props)
Modified:
    
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriter.java

Modified: 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriter.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriter.java?rev=1754084&r1=1754083&r2=1754084&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriter.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriter.java
 Tue Jul 26 08:38:41 2016
@@ -148,6 +148,12 @@ public class SegmentBufferWriter impleme
 
     private Statistics statistics;
 
+    /**
+     * Mark this buffer as dirty. A dirty buffer needs to be flushed to disk
+     * regularly to avoid data loss.
+     */
+    private boolean dirty;
+
     public SegmentBufferWriter(@Nonnull SegmentStore store,
                                @Nonnull SegmentTracker tracker,
                                @Nonnull SegmentReader reader,
@@ -218,15 +224,19 @@ public class SegmentBufferWriter impleme
         } catch (IOException e) {
             LOG.error("Unable to write meta info to segment {} {}", 
segment.getSegmentId(), metaInfo, e);
         }
+
+        dirty = false;
     }
 
     public void writeByte(byte value) {
         buffer[position++] = value;
+        dirty = true;
     }
 
     public void writeShort(short value) {
         buffer[position++] = (byte) (value >> 8);
         buffer[position++] = (byte) value;
+        dirty = true;
     }
 
     public void writeInt(int value) {
@@ -234,6 +244,7 @@ public class SegmentBufferWriter impleme
         buffer[position++] = (byte) (value >> 16);
         buffer[position++] = (byte) (value >> 8);
         buffer[position++] = (byte) value;
+        dirty = true;
     }
 
     public void writeLong(long value) {
@@ -276,6 +287,8 @@ public class SegmentBufferWriter impleme
         buffer[position++] = (byte) (offset >> Segment.RECORD_ALIGN_BITS);
 
         statistics.recordIdCount++;
+
+        dirty = true;
     }
 
     // FIXME OAK-4287: Disable / remove SegmentBufferWriter#checkGCGeneration
@@ -328,6 +341,7 @@ public class SegmentBufferWriter impleme
     public void writeBytes(byte[] data, int offset, int length) {
         arraycopy(data, offset, buffer, position, length);
         position += length;
+        dirty = true;
     }
 
     /**
@@ -337,7 +351,7 @@ public class SegmentBufferWriter impleme
      */
     @Override
     public void flush() throws IOException {
-        if (length > 0) {
+        if (dirty) {
             int refcount = segment.getRefCount();
             statistics.segmentIdCount = refcount;
 

Added: 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriterTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriterTest.java?rev=1754084&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriterTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriterTest.java
 Tue Jul 26 08:38:41 2016
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jackrabbit.oak.segment;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static 
org.apache.jackrabbit.oak.segment.SegmentWriterBuilder.segmentWriterBuilder;
+import static 
org.apache.jackrabbit.oak.segment.file.FileStoreBuilder.fileStoreBuilder;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+
+import java.io.File;
+import java.util.List;
+
+import org.apache.jackrabbit.oak.segment.file.FileStore;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+public class SegmentBufferWriterTest {
+
+    @Rule
+    public TemporaryFolder folder = new TemporaryFolder(new File("target"));
+
+    private FileStore openFileStore() throws Exception {
+        return fileStoreBuilder(folder.getRoot()).build();
+    }
+
+    @Test
+    public void nonDirtyBuffersShouldNotBeFlushed() throws Exception {
+        List<SegmentId> before;
+
+        try (FileStore store = openFileStore()) {
+            before = newArrayList(store.getSegmentIds());
+            segmentWriterBuilder("t").build(store).flush();
+        }
+
+        List<SegmentId> after;
+
+        try (FileStore store = openFileStore()) {
+            after = newArrayList(store.getSegmentIds());
+        }
+
+        assertEquals(before, after);
+    }
+
+    @Test
+    public void dirtyBuffersShouldBeFlushed() throws Exception {
+        List<SegmentId> before;
+
+        try (FileStore store = openFileStore()) {
+            before = newArrayList(store.getSegmentIds());
+            SegmentWriter writer = segmentWriterBuilder("t").build(store);
+            writer.writeString("test");
+            writer.flush();
+        }
+
+        List<SegmentId> after;
+
+        try (FileStore store = openFileStore()) {
+            after = newArrayList(store.getSegmentIds());
+        }
+
+        assertNotEquals(before, after);
+    }
+
+}

Propchange: 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to