Author: frm
Date: Tue Aug 20 12:02:50 2019
New Revision: 1865533

URL: http://svn.apache.org/viewvc?rev=1865533&view=rev
Log:
OAK-8066 - Prevent OOME when rebasing nodes with many children

Contribution by Rahul Bhardwaj.

Added:
    
jackrabbit/oak/branches/1.8/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/LargeNumberOfChildNodeUpdatesIT.java
      - copied, changed from r1854579, 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/LargeNumberOfChildNodeUpdatesIT.java
Modified:
    jackrabbit/oak/branches/1.8/   (props changed)
    
jackrabbit/oak/branches/1.8/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/DefaultSegmentWriter.java

Propchange: jackrabbit/oak/branches/1.8/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Tue Aug 20 12:02:50 2019
@@ -1,4 +1,4 @@
 /jackrabbit/oak/branches/1.0:1665962
 /jackrabbit/oak/branches/1.10:1854524
-/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,1822182,1822201,1822207,1822496,1822527,1822638,1822642,1822645,1822723,1822802,1822808,1822850,1822934,1823135,1823163,1823169,1823172,1823655,1823669,1823671,1824196,1824198,1824253,1824255,1824896,1824962,1825065,1825362,1825381,1825442,1825448,1825466,1825470-1825471,1825475,1825523,1825525,1825561,1825619-1825621,1825651,1825654,1825992,1826079,1826089-1826090,1826096,1826216,1826237,1826338,1826516,1826532,1826551,1826560,1826638,1826640,1826730,1826833,1826932,1826957,1827423,1827472,1827486,1827816,1827977,1828349,1828439,1828502,1828529,1828827,1828868,1828948,1829527,1829534,1829546,1829569,1829587,1829665,1829854,1829864,1829978,1829985,1829987,1829998,1830019
 
,1830048,1830160,1830171,1830197,1830209,1830239,1830347,1830748,1830911,1830923,1831157-1831158,1831163,1831190,1831374,1831560,1831689,1832258,1832376,1832379,1832535,1833308,1833347,1833702,1833833,1834109,1834112,1834117,1834287,1834291,1834302,1834312,1834326,1834328,1834336,1834428,1834468,1834483,1834610,1834648-1834649,1834681,1834823,1834857-1834858,1835056,1835060,1835062,1835518,1835521,1835635,1835642,1835780,1835819,1836082,1836121,1836167-1836168,1836170-1836187,1836189-1836196,1836206,1836487,1836493,1836548,1837057,1837274,1837296,1837326,1837475,1837503,1837547,1837569,1837596,1837600,1837657,1837718,1837998,1838076,1838637,1839549,1839570,1839637,1839746,1840019,1840024,1840031,1840226,1840455,1840462,1840574,1840769,1841314,1841352,1841909,1842089,1842240,1842677,1843175,1843222,1843231,1843398,1843618,1843621,1843637,1843652,1843669,1843905,1843911,1843994,1844070,1844110,1844325,1844549,1844625,1844627,1844642,1844728,1844775,1844932,1845135,1845336,1845405,1845
 
415,1845730-1845731,1845863,1845865,1846057,1846162,1846396,1846429,1846486,1846581,1846617,1847088,1847096,1848073,1848181-1848182,1848191,1848217,1848729,1848769,1848822-1848823,1850221,1850319,1850343,1850837,1850874,1851533-1851535,1851619,1852120,1852135,1852451,1852492-1852493,1852528,1852582,1852584,1852601,1853083,1853141,1853229,1853393,1853429,1853433,1853435,1853866,1853868,1853870,1853893,1853969,1853997,1854034,1854044,1854055,1854113,1854373,1854377,1854380,1854385,1854401,1854403,1854455,1854461-1854462,1854466,1854468,1854515,1854539,1854701,1854773-1854774,1854827,1854848,1854859,1854930,1855032,1855522,1855776,1855905,1855993,1856049,1856056,1856538,1856545,1856818,1857000,1857010,1857221,1857242,1857247,1857253,1857294,1857314,1857332,1857463,1857480,1857634,1857638,1857936,1858032,1858139,1858385,1858424,1858571,1858578,1858810,1858926,1859020,1859231,1859292,1859294,1859533,1859609,1859612,1859711,1859716,1859772,1859776,1859780,1859843,1859881,1860120,1860131,1
 
860137,1860202,1860328,1860548,1860564-1860565,1861114,1861626,1861743,1861757,1861763,1861770,1861776,1861780,1862044,1862266,1862370,1862422,1862447-1862448,1862465,1862499,1862536-1862537,1862728,1862806,1862817,1862881,1862926-1862927,1862977,1863076,1863081,1863086,1863369,1863405,1863540,1863633-1863634,1863642,1864349,1864353
+/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,1822182,1822201,1822207,1822496,1822527,1822638,1822642,1822645,1822723,1822802,1822808,1822850,1822934,1823135,1823163,1823169,1823172,1823655,1823669,1823671,1824196,1824198,1824253,1824255,1824896,1824962,1825065,1825362,1825381,1825442,1825448,1825466,1825470-1825471,1825475,1825523,1825525,1825561,1825619-1825621,1825651,1825654,1825992,1826079,1826089-1826090,1826096,1826216,1826237,1826338,1826516,1826532,1826551,1826560,1826638,1826640,1826730,1826833,1826932,1826957,1827423,1827472,1827486,1827816,1827977,1828349,1828439,1828502,1828529,1828827,1828868,1828948,1829527,1829534,1829546,1829569,1829587,1829665,1829854,1829864,1829978,1829985,1829987,1829998,1830019
 
,1830048,1830160,1830171,1830197,1830209,1830239,1830347,1830748,1830911,1830923,1831157-1831158,1831163,1831190,1831374,1831560,1831689,1832258,1832376,1832379,1832535,1833308,1833347,1833702,1833833,1834109,1834112,1834117,1834287,1834291,1834302,1834312,1834326,1834328,1834336,1834428,1834468,1834483,1834610,1834648-1834649,1834681,1834823,1834857-1834858,1835056,1835060,1835062,1835518,1835521,1835635,1835642,1835780,1835819,1836082,1836121,1836167-1836168,1836170-1836187,1836189-1836196,1836206,1836487,1836493,1836548,1837057,1837274,1837296,1837326,1837475,1837503,1837547,1837569,1837596,1837600,1837657,1837718,1837998,1838076,1838637,1839549,1839570,1839637,1839746,1840019,1840024,1840031,1840226,1840455,1840462,1840574,1840769,1841314,1841352,1841909,1842089,1842240,1842677,1843175,1843222,1843231,1843398,1843618,1843621,1843637,1843652,1843669,1843905,1843911,1843994,1844070,1844110,1844325,1844549,1844625,1844627,1844642,1844728,1844775,1844932,1845135,1845336,1845405,1845
 
415,1845730-1845731,1845863,1845865,1846057,1846162,1846396,1846429,1846486,1846581,1846617,1847088,1847096,1848073,1848181-1848182,1848191,1848217,1848729,1848769,1848822-1848823,1850221,1850319,1850343,1850837,1850874,1851533-1851535,1851619,1852120,1852135,1852451,1852492-1852493,1852528,1852582,1852584,1852601,1853083,1853141,1853229,1853393,1853429,1853433,1853435,1853866,1853868,1853870,1853893,1853969,1853997,1854034,1854044,1854055,1854113,1854373,1854377,1854380,1854385,1854401,1854403,1854455,1854461-1854462,1854466,1854468,1854515,1854539,1854579-1854580,1854596,1854701,1854773-1854774,1854827,1854848,1854859,1854930,1855032,1855522,1855776,1855905,1855993,1856049,1856056,1856538,1856545,1856818,1857000,1857010,1857221,1857242,1857247,1857253,1857294,1857314,1857332,1857463,1857480,1857634,1857638,1857936,1858032,1858139,1858385,1858424,1858571,1858578,1858810,1858926,1859020,1859231,1859292,1859294,1859533,1859609,1859612,1859711,1859716,1859772,1859776,1859780,1859843,1
 
859881,1860120,1860131,1860137,1860202,1860328,1860548,1860564-1860565,1861114,1861626,1861743,1861757,1861763,1861770,1861776,1861780,1862044,1862266,1862370,1862422,1862447-1862448,1862465,1862499,1862536-1862537,1862728,1862806,1862817,1862881,1862926-1862927,1862977,1863076,1863081,1863086,1863369,1863405,1863540,1863633-1863634,1864349,1864353
 /jackrabbit/trunk:1345480

Modified: 
jackrabbit/oak/branches/1.8/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/DefaultSegmentWriter.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.8/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/DefaultSegmentWriter.java?rev=1865533&r1=1865532&r2=1865533&view=diff
==============================================================================
--- 
jackrabbit/oak/branches/1.8/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/DefaultSegmentWriter.java
 (original)
+++ 
jackrabbit/oak/branches/1.8/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/DefaultSegmentWriter.java
 Tue Aug 20 12:02:50 2019
@@ -33,7 +33,6 @@ import static com.google.common.collect.
 import static com.google.common.collect.Lists.partition;
 import static com.google.common.collect.Maps.newHashMap;
 import static com.google.common.io.ByteStreams.read;
-import static java.lang.Integer.getInteger;
 import static java.lang.Long.numberOfLeadingZeros;
 import static java.lang.Math.min;
 import static java.util.Arrays.asList;
@@ -44,6 +43,7 @@ import static org.apache.jackrabbit.oak.
 import static org.apache.jackrabbit.oak.api.Type.NAME;
 import static org.apache.jackrabbit.oak.api.Type.NAMES;
 import static org.apache.jackrabbit.oak.api.Type.STRING;
+import static 
org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState.compareAgainstEmptyState;
 import static org.apache.jackrabbit.oak.segment.MapEntry.newModifiedMapEntry;
 import static org.apache.jackrabbit.oak.segment.MapRecord.BUCKETS_PER_LEVEL;
 import static 
org.apache.jackrabbit.oak.segment.RecordWriters.newNodeStateWriter;
@@ -70,7 +70,6 @@ import org.apache.jackrabbit.oak.segment
 import org.apache.jackrabbit.oak.segment.WriteOperationHandler.WriteOperation;
 import org.apache.jackrabbit.oak.segment.file.tar.GCGeneration;
 import org.apache.jackrabbit.oak.spi.blob.BlobStore;
-import org.apache.jackrabbit.oak.spi.state.ChildNodeEntry;
 import org.apache.jackrabbit.oak.spi.state.DefaultNodeStateDiff;
 import org.apache.jackrabbit.oak.spi.state.NodeState;
 import org.jetbrains.annotations.NotNull;
@@ -89,17 +88,10 @@ public class DefaultSegmentWriter implem
     private static final Logger LOG = 
LoggerFactory.getLogger(DefaultSegmentWriter.class);
 
     /**
-     * Default threshold of the number of modified child nodes of a node after 
which to
-     * log a warning.
+     * Number of updates to child nodes before changes are flushed to disk.
      */
-    public static final int CHILD_NODE_COUNT_WARN_THRESHOLD_DEFAULT = 1000000;
-
-    /**
-     * Threshold of the number of modified child nodes of a node after which 
to log
-     * a warning.
-     */
-    private static final int CHILD_NODE_COUNT_WARN_THRESHOLD =
-            getInteger("oak.segment.childNodeCountWarnThreshold", 
CHILD_NODE_COUNT_WARN_THRESHOLD_DEFAULT);
+    private static final int CHILD_NODE_UPDATE_LIMIT = Integer
+            .getInteger("child.node.update.limit", 10000);
 
     @NotNull
     private final WriterCacheManager cacheManager;
@@ -211,7 +203,7 @@ public class DefaultSegmentWriter implem
             @Nullable final ByteBuffer stableIdBytes)
     throws IOException {
         return new 
SegmentWriteOperation(writeOperationHandler.getGCGeneration())
-                .writeNode("/", state, stableIdBytes);
+                .writeNode(state, stableIdBytes);
     }
 
     /**
@@ -229,8 +221,6 @@ public class DefaultSegmentWriter implem
 
         private final Cache<String, RecordId> nodeCache;
 
-        private int childNodeCountWarnThreshold = 
CHILD_NODE_COUNT_WARN_THRESHOLD;
-
         SegmentWriteOperation(@NotNull GCGeneration gcGeneration) {
             int generation = gcGeneration.getGeneration();
             this.gcGeneration = gcGeneration;
@@ -766,10 +756,7 @@ public class DefaultSegmentWriter implem
             return tid;
         }
 
-        private RecordId writeNode(
-                @NotNull String path,
-                @NotNull NodeState state,
-                @Nullable ByteBuffer stableIdBytes)
+        private RecordId writeNode(@NotNull NodeState state, @Nullable 
ByteBuffer stableIdBytes)
         throws IOException {
             RecordId compactedId = deduplicateNode(state);
 
@@ -780,7 +767,7 @@ public class DefaultSegmentWriter implem
             if (state instanceof SegmentNodeState && stableIdBytes == null) {
                 stableIdBytes = ((SegmentNodeState) state).getStableIdBytes();
             }
-            RecordId recordId = writeNodeUncached(path, state, stableIdBytes);
+            RecordId recordId = writeNodeUncached(state, stableIdBytes);
 
             if (stableIdBytes != null) {
                 // This node state has been rewritten because it is from an 
older
@@ -796,30 +783,14 @@ public class DefaultSegmentWriter implem
             return (byte) (Byte.MIN_VALUE + 64 - 
numberOfLeadingZeros(childCount));
         }
 
-        private void warnOnManyChildren(Map<?, ?> map, String path) {
-            if (map.size() >= childNodeCountWarnThreshold) {
-                childNodeCountWarnThreshold += CHILD_NODE_COUNT_WARN_THRESHOLD;
-                LOG.warn("Large number of modified child nodes: {} @ {}", 
map.size(), path);
-            }
-        }
-
-        private RecordId writeNodeUncached(
-                @NotNull String path,
-                @NotNull NodeState state,
-                @Nullable ByteBuffer stableIdBytes)
+        private RecordId writeNodeUncached(@NotNull NodeState state, @Nullable 
ByteBuffer stableIdBytes)
         throws IOException {
-            ModifiedNodeState after = null;
-
-            if (state instanceof ModifiedNodeState) {
-                after = (ModifiedNodeState) state;
-            }
 
             RecordId beforeId = null;
-
-            if (after != null) {
+            if (state instanceof ModifiedNodeState) {
                 // Pass null to indicate we don't want to update the node 
write statistics
                 // when deduplicating the base state
-                beforeId = deduplicateNode(after.getBaseState());
+                beforeId = deduplicateNode(((ModifiedNodeState) 
state).getBaseState());
             }
 
             SegmentNodeState before = null;
@@ -840,26 +811,9 @@ public class DefaultSegmentWriter implem
 
             String childName = template.getChildName();
             if (childName == Template.MANY_CHILD_NODES) {
-                MapRecord base;
-                Map<String, RecordId> childNodes;
-                if (before != null
-                        && before.getChildNodeCount(2) > 1
-                        && after.getChildNodeCount(2) > 1) {
-                    base = before.getChildNodeMap();
-                    childNodes = new ChildNodeCollectorDiff(path).diff(before, 
after);
-                } else {
-                    base = null;
-                    childNodes = newHashMap();
-                    for (ChildNodeEntry entry : state.getChildNodeEntries()) {
-                        childNodes.put(
-                                entry.getName(),
-                                writeNode(path + "/" + entry.getName(), 
entry.getNodeState(), null));
-                        warnOnManyChildren(childNodes, path);
-                    }
-                }
-                ids.add(writeMap(base, childNodes));
+                ids.add(writeChildNodes(before, state));
             } else if (childName != Template.ZERO_CHILD_NODES) {
-                ids.add(writeNode(path + "/" + template.getChildName(), 
state.getChildNode(template.getChildName()), null));
+                ids.add(writeNode(state.getChildNode(template.getChildName()), 
null));
             }
 
             List<RecordId> pIds = newArrayList();
@@ -925,6 +879,20 @@ public class DefaultSegmentWriter implem
                 newNodeStateWriter(stableId, ids)));
         }
 
+        @NotNull
+        private RecordId writeChildNodes(@Nullable SegmentNodeState before, 
@NotNull NodeState after)
+        throws IOException {
+            if (before != null
+                    && before.getChildNodeCount(2) > 1
+                    && after.getChildNodeCount(2) > 1) {
+                return new ChildNodeCollectorDiff(before.getChildNodeMap())
+                    .diff(before, after);
+            } else {
+                return new ChildNodeCollectorDiff()
+                    .diff(after);
+            }
+        }
+
         /**
          * Try to deduplicate the passed {@code node}. This succeeds if
          * the passed node state has already been persisted to this store and
@@ -1004,30 +972,42 @@ public class DefaultSegmentWriter implem
         }
 
         private class ChildNodeCollectorDiff extends DefaultNodeStateDiff {
-
             private final Map<String, RecordId> childNodes = newHashMap();
 
-            private final String path;
+            @Nullable
+            private MapRecord base;
 
             private IOException exception;
 
-            public ChildNodeCollectorDiff(String path) {
-                this.path = path;
+            private ChildNodeCollectorDiff(@Nullable MapRecord base) {
+                this.base = base;
+            }
+
+            private ChildNodeCollectorDiff() {
+                this(null);
+            }
+
+            public RecordId diff(NodeState after) throws IOException {
+                compareAgainstEmptyState(after, this);
+                if (exception != null) {
+                    throw new IOException(exception);
+                }
+                return flush();
             }
 
-            public Map<String, RecordId> diff(SegmentNodeState before, 
ModifiedNodeState after) throws IOException {
+            public RecordId diff(NodeState before, NodeState after)
+            throws IOException {
                 after.compareAgainstBaseState(before, this);
                 if (exception != null) {
                     throw new IOException(exception);
-                } else {
-                    return childNodes;
                 }
+                return flush();
             }
 
             @Override
             public boolean childNodeAdded(String name, NodeState after) {
                 try {
-                    addChild(name, writeNode(path + "/" + name, after, null));
+                    onChildNode(name, writeNode(after, null));
                 } catch (IOException e) {
                     exception = e;
                     return false;
@@ -1040,7 +1020,7 @@ public class DefaultSegmentWriter implem
                     String name, NodeState before, NodeState after
             ) {
                 try {
-                    addChild(name, writeNode(path + "/" + name, after, null));
+                    onChildNode(name, writeNode(after, null));
                 } catch (IOException e) {
                     exception = e;
                     return false;
@@ -1050,14 +1030,29 @@ public class DefaultSegmentWriter implem
 
             @Override
             public boolean childNodeDeleted(String name, NodeState before) {
-                addChild(name, null);
+                try {
+                    onChildNode(name, null);
+                } catch (IOException e) {
+                    exception = e;
+                    return false;
+                }
                 return true;
             }
 
-            private void addChild(String name, @Nullable RecordId recordId) {
-                childNodes.put(name, recordId);
-                warnOnManyChildren(childNodes, path);
+            private void onChildNode(String nodeName, @Nullable RecordId 
recordId) throws IOException {
+                childNodes.put(nodeName, recordId);
+                if (childNodes.size() > CHILD_NODE_UPDATE_LIMIT) {
+                    flush();
+                }
+            }
+
+            private RecordId flush() throws IOException {
+                RecordId mapId = writeMap(base, childNodes);
+                base = reader.readMap(mapId);
+                childNodes.clear();
+                return mapId;
             }
+
         }
     }
 

Copied: 
jackrabbit/oak/branches/1.8/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/LargeNumberOfChildNodeUpdatesIT.java
 (from r1854579, 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/LargeNumberOfChildNodeUpdatesIT.java)
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.8/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/LargeNumberOfChildNodeUpdatesIT.java?p2=jackrabbit/oak/branches/1.8/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/LargeNumberOfChildNodeUpdatesIT.java&p1=jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/LargeNumberOfChildNodeUpdatesIT.java&r1=1854579&r2=1865533&rev=1865533&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/LargeNumberOfChildNodeUpdatesIT.java
 (original)
+++ 
jackrabbit/oak/branches/1.8/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/LargeNumberOfChildNodeUpdatesIT.java
 Tue Aug 20 12:02:50 2019
@@ -37,9 +37,6 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
 
-// *<p>The test is <b>disabled</b> by default, to run it you need to set the 
{@code SegmentCompactionIT} system property:<br>
-// * {@code mvn test -Dtest=SegmentCompactionIT -Dtest.opts.memory=-Xmx4G}
-
 /**
  * This test asserts that a large number of child nodes can be added in a 
single
  * transaction. Due to its long running time the test needs to be explicitly 
enabled


Reply via email to