Author: mreutegg Date: Mon Sep 19 14:12:13 2016 New Revision: 1761444 URL: http://svn.apache.org/viewvc?rev=1761444&view=rev Log: OAK-4819: Improve revision GC resilience
Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/VersionGarbageCollector.java jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/Utils.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/VersionGarbageCollectorIT.java jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/util/UtilsTest.java Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/VersionGarbageCollector.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/VersionGarbageCollector.java?rev=1761444&r1=1761443&r2=1761444&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/VersionGarbageCollector.java (original) +++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/VersionGarbageCollector.java Mon Sep 19 14:12:13 2016 @@ -218,10 +218,17 @@ public class VersionGarbageCollector { */ void possiblyDeleted(NodeDocument doc) throws IOException { + // construct an id that also contains + // the _modified time of the document + String id = doc.getId() + "/" + doc.getModified(); + // check if id is valid + try { + Utils.getDepthFromId(id); + } catch (IllegalArgumentException e) { + log.warn("Invalid GC id {} for document {}", id, doc); + return; + } if (doc.getNodeAtRevision(nodeStore, headRevision, null) == null) { - // construct an id that also contains - // the _modified time of the document - String id = doc.getId() + "/" + doc.getModified(); addDocument(id); // Collect id of all previous docs also Iterator<NodeDocument> it = doc.getAllPreviousDocs(); Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/Utils.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/Utils.java?rev=1761444&r1=1761443&r2=1761444&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/Utils.java (original) +++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/Utils.java Mon Sep 19 14:12:13 2016 @@ -314,10 +314,16 @@ public class Utils { return id.substring(index + 1); } - public static int getDepthFromId(String id) { - int index = id.indexOf(':'); - assert index > 0 : "Invalid id " + id; - return Integer.parseInt(id.substring(0, index)); + public static int getDepthFromId(String id) throws IllegalArgumentException { + try { + int index = id.indexOf(':'); + if (index >= 0) { + return Integer.parseInt(id.substring(0, index)); + } + } catch (NumberFormatException e) { + // ignore and throw IllegalArgumentException + } + throw new IllegalArgumentException("Invalid id: " + id); } public static String getPreviousPathFor(String path, Revision r, int height) { Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/VersionGarbageCollectorIT.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/VersionGarbageCollectorIT.java?rev=1761444&r1=1761443&r2=1761444&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/VersionGarbageCollectorIT.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/VersionGarbageCollectorIT.java Mon Sep 19 14:12:13 2016 @@ -39,6 +39,7 @@ import static org.apache.jackrabbit.oak. import static org.apache.jackrabbit.oak.commons.FixturesHelper.Fixture.DOCUMENT_RDB; import static org.apache.jackrabbit.oak.commons.FixturesHelper.getFixtures; import static org.apache.jackrabbit.oak.plugins.document.Collection.NODES; +import static org.apache.jackrabbit.oak.plugins.document.Document.ID; import static org.apache.jackrabbit.oak.plugins.document.NodeDocument.NUM_REVS_THRESHOLD; import static org.apache.jackrabbit.oak.plugins.document.NodeDocument.PREV_SPLIT_FACTOR; import static org.apache.jackrabbit.oak.plugins.document.NodeDocument.SplitDocType; @@ -92,10 +93,6 @@ public class VersionGarbageCollectorIT { @Parameterized.Parameters(name="{0}") public static Collection<Object[]> fixtures() throws IOException { List<Object[]> fixtures = Lists.newArrayList(); - if (getFixtures().contains(DOCUMENT_MEM)) { - fixtures.add(new Object[] { new DocumentStoreFixture.MemoryFixture() }); - } - DocumentStoreFixture mongo = new DocumentStoreFixture.MongoFixture(); if (getFixtures().contains(DOCUMENT_NS) && mongo.isAvailable()) { fixtures.add(new Object[] { mongo }); @@ -105,6 +102,10 @@ public class VersionGarbageCollectorIT { if (getFixtures().contains(DOCUMENT_RDB) && rdb.isAvailable()) { fixtures.add(new Object[] { rdb }); } + if (fixtures.isEmpty() || getFixtures().contains(DOCUMENT_MEM)) { + fixtures.add(new Object[] { new DocumentStoreFixture.MemoryFixture() }); + } + return fixtures; } @@ -499,6 +500,37 @@ public class VersionGarbageCollectorIT { assertEquals(2, stats.splitDocGCCount); } + // OAK-4819 + @Test + public void malformedId() throws Exception { + long maxAge = 1; //hrs + long delta = TimeUnit.MINUTES.toMillis(10); + + NodeBuilder builder = store.getRoot().builder(); + builder.child("foo"); + store.merge(builder, EmptyHook.INSTANCE, CommitInfo.EMPTY); + // remove again + builder = store.getRoot().builder(); + builder.child("foo").remove(); + store.merge(builder, EmptyHook.INSTANCE, CommitInfo.EMPTY); + + store.runBackgroundOperations(); + + // add a document with a malformed id + String id = "42"; + UpdateOp op = new UpdateOp(id, true); + op.set(ID, id); + NodeDocument.setDeletedOnce(op); + NodeDocument.setModified(op, store.newRevision()); + store.getDocumentStore().create(NODES, Lists.newArrayList(op)); + + clock.waitUntil(clock.getTime() + HOURS.toMillis(maxAge) + delta); + + // gc must not fail + VersionGCStats stats = gc.gc(maxAge, HOURS); + assertEquals(1, stats.deletedDocGCCount); + } + private void createTestNode(String name) throws CommitFailedException { DocumentStore ds = store.getDocumentStore(); NodeBuilder builder = store.getRoot().builder(); Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/util/UtilsTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/util/UtilsTest.java?rev=1761444&r1=1761443&r2=1761444&view=diff ============================================================================== --- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/util/UtilsTest.java (original) +++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/util/UtilsTest.java Mon Sep 19 14:12:13 2016 @@ -240,4 +240,14 @@ public class UtilsTest { assertEquals(4, Utils.getMinTimestampForDiff(to, from, minRevs)); } + + @Test(expected = IllegalArgumentException.class) + public void getDepthFromIdIllegalArgumentException1() { + Utils.getDepthFromId("a:/foo"); + } + + @Test(expected = IllegalArgumentException.class) + public void getDepthFromIdIllegalArgumentException2() { + Utils.getDepthFromId("42"); + } }