Author: frm Date: Wed Sep 5 08:06:01 2018 New Revision: 1840098 URL: http://svn.apache.org/viewvc?rev=1840098&view=rev Log: OAK-7721 - Check for too big records when allocating space
Backport r1840024 from trunk. Modified: jackrabbit/oak/branches/1.8/ (props changed) jackrabbit/oak/branches/1.8/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriter.java jackrabbit/oak/branches/1.8/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriterTest.java Propchange: jackrabbit/oak/branches/1.8/ ------------------------------------------------------------------------------ --- svn:mergeinfo (original) +++ svn:mergeinfo Wed Sep 5 08:06:01 2018 @@ -1,3 +1,3 @@ /jackrabbit/oak/branches/1.0:1665962 -/jackrabbit/oak/trunk:1820660-1820661,1820729,1820734,1820859,1820861,1820878,1820888,1820947,1821027,1821130,1821140-1821141,1821178,1821237,1821240,1821249,1821258,1821325,1821358,1821361-1821362,1821370,1821375,1821393,1821477,1821487,1821516,1821617,1821663,1821665,1821668,1821681,1821847,1821975-1821983,1822121,1822201,1822207,1822527,1822723,1822808,1822850,1822934,1823135,1823163,1823169,1823172,1823655,1823669,1824196,1824198,1824253,1824255,1824896,1824962,1825065,1825362,1825381,1825442,1825448,1825466,1825470-1825471,1825475,1825523,1825525,1825561,1825619-1825621,1825651,1825654,1825992,1826079,1826090,1826096,1826216,1826237,1826338,1826516,1826532,1826551,1826560,1826638,1826640,1826730,1826932,1826957,1827423,1827472,1827486,1827977,1828349,1828439,1828502,1828529,1828948,1829527,1829534,1829546,1829569,1829587,1829665,1829854,1829864,1829978,1829985,1829987,1829998,1830019,1830048,1830160,1830171,1830197,1830209,1830239,1830347,1830748,1831157-1831158,1831163,1831190 ,1831374,1831560,1832258,1832376,1832379,1832535,1833308,1833347,1833833,1834112,1834117,1834336,1834428,1834610,1834648-1834649,1834681,1834823,1834857-1834858,1835060,1835518,1835642,1835780,1835819,1836487,1836493,1837326,1837475,1837657,1837998,1838076,1838637,1839549,1839637,1839746 +/jackrabbit/oak/trunk:1820660-1820661,1820729,1820734,1820859,1820861,1820878,1820888,1820947,1821027,1821130,1821140-1821141,1821178,1821237,1821240,1821249,1821258,1821325,1821358,1821361-1821362,1821370,1821375,1821393,1821477,1821487,1821516,1821617,1821663,1821665,1821668,1821681,1821847,1821975-1821983,1822121,1822201,1822207,1822527,1822723,1822808,1822850,1822934,1823135,1823163,1823169,1823172,1823655,1823669,1824196,1824198,1824253,1824255,1824896,1824962,1825065,1825362,1825381,1825442,1825448,1825466,1825470-1825471,1825475,1825523,1825525,1825561,1825619-1825621,1825651,1825654,1825992,1826079,1826090,1826096,1826216,1826237,1826338,1826516,1826532,1826551,1826560,1826638,1826640,1826730,1826932,1826957,1827423,1827472,1827486,1827977,1828349,1828439,1828502,1828529,1828948,1829527,1829534,1829546,1829569,1829587,1829665,1829854,1829864,1829978,1829985,1829987,1829998,1830019,1830048,1830160,1830171,1830197,1830209,1830239,1830347,1830748,1831157-1831158,1831163,1831190 ,1831374,1831560,1832258,1832376,1832379,1832535,1833308,1833347,1833833,1834112,1834117,1834336,1834428,1834610,1834648-1834649,1834681,1834823,1834857-1834858,1835060,1835518,1835642,1835780,1835819,1836487,1836493,1837326,1837475,1837657,1837998,1838076,1838637,1839549,1839637,1839746,1840024 /jackrabbit/trunk:1345480 Modified: jackrabbit/oak/branches/1.8/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriter.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.8/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriter.java?rev=1840098&r1=1840097&r2=1840098&view=diff ============================================================================== --- jackrabbit/oak/branches/1.8/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriter.java (original) +++ jackrabbit/oak/branches/1.8/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriter.java Wed Sep 5 08:06:01 2018 @@ -427,17 +427,36 @@ public class SegmentBufferWriter impleme segmentSize = align(headerSize + recordSize + length, 16); } + // If the resulting segment buffer would be too big we need to allocate + // additional space. Allocating additional space is a recursive + // operation guarded by the `dirty` flag. The recursion can iterate at + // most two times. The base case happens when the `dirty` flag is + // `false`: the current buffer is empty, the record is too big to fit in + // an empty segment, and we fail with an `IllegalArgumentException`. The + // recursive step happens when the `dirty` flag is `true`: + // the current buffer is non-empty, we flush it, allocate a new buffer + // for an empty segment, and invoke `prepare()` once more. + if (segmentSize > buffer.length) { - LOG.debug("Flushing full segment {} (headerSize={}, recordSize={}, length={}, segmentSize={})", + if (dirty) { + LOG.debug("Flushing full segment {} (headerSize={}, recordSize={}, length={}, segmentSize={})", segment.getSegmentId(), headerSize, recordSize, length, segmentSize); - flush(store); + flush(store); + return prepare(type, size, ids, store); + } + throw new IllegalArgumentException(String.format( + "Record too big: type=%s, size=%s, recordIds=%s, total=%s", + type, + size, + ids.size(), + recordSize + )); } statistics.recordCount++; length += recordSize; position = buffer.length - length; - checkState(position >= 0); int recordNumber = recordNumbers.addRecord(type, position); return new RecordId(segment.getSegmentId(), recordNumber); Modified: jackrabbit/oak/branches/1.8/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriterTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.8/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriterTest.java?rev=1840098&r1=1840097&r2=1840098&view=diff ============================================================================== --- jackrabbit/oak/branches/1.8/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriterTest.java (original) +++ jackrabbit/oak/branches/1.8/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriterTest.java Wed Sep 5 08:06:01 2018 @@ -24,7 +24,9 @@ import static org.junit.Assert.assertEqu import static org.junit.Assert.assertNotEquals; import java.io.File; +import java.util.Collections; import java.util.List; +import java.util.Optional; import org.apache.jackrabbit.oak.segment.file.FileStore; import org.apache.jackrabbit.oak.segment.file.ReadOnlyFileStore; @@ -95,4 +97,42 @@ public class SegmentBufferWriterTest { assertNotEquals(before, after); } + @Test + public void tooBigRecord() throws Exception { + + // See OAK-7721 to understand why this test exists. + + try (FileStore store = openFileStore()) { + + // Please don't change anything from the following statement yet. + // Read the next comment to understand why. + + SegmentBufferWriter writer = new SegmentBufferWriter( + store.getSegmentIdProvider(), + store.getReader(), + "t", + store.getRevisions().getHead().getSegment().getGcGeneration() + ); + + // The size of the record is chosen with the precise intention to + // fool `writer` into having enough space to write the record. In + // particular, at the end of `prepare()`, `writer` will have + // `this.length = 262144`, which is `MAX_SEGMENT_SIZE`, and + // `this.position = 0`. This result is particularly sensitive to the + // initial content of the segment, which in turn is influenced by + // the segment info. Try to change the writer ID in the constructor + // of `SegmentBufferWriter` to a longer string, and you will have + // `prepare()` throw ISEs because the writer ID is embedded in the + // segment info. + + Optional<IllegalArgumentException> error = Optional.empty(); + try { + writer.prepare(RecordType.BLOCK, 262101, Collections.emptyList(), store); + } catch (IllegalArgumentException e) { + error = Optional.of(e); + } + assertEquals("Record too big: type=BLOCK, size=262101, recordIds=0, total=262104", error.map(Exception::getMessage).orElse(null)); + } + } + }