You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by mr...@apache.org on 2015/01/21 11:51:51 UTC

svn commit: r1653476 - in /jackrabbit/oak/branches/1.0: ./ oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/ oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/

Author: mreutegg
Date: Wed Jan 21 10:51:50 2015
New Revision: 1653476

URL: http://svn.apache.org/r1653476
Log:
OAK-2421: Clean up orphaned branches

Merge revision 1653463 from trunk

Added:
    jackrabbit/oak/branches/1.0/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/OrphanedBranchTest.java
      - copied unchanged from r1653463, jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/OrphanedBranchTest.java
Modified:
    jackrabbit/oak/branches/1.0/   (props changed)
    jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/Branch.java
    jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/Commit.java
    jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentMK.java
    jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java
    jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java
    jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/MergeCommit.java
    jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/RevisionContext.java
    jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/SplitOperations.java
    jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/UnmergedBranches.java
    jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/UnsavedModifications.java
    jackrabbit/oak/branches/1.0/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/CommitQueueTest.java
    jackrabbit/oak/branches/1.0/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreTest.java
    jackrabbit/oak/branches/1.0/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DummyRevisionContext.java

Propchange: jackrabbit/oak/branches/1.0/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed Jan 21 10:51:50 2015
@@ -1,2 +1,2 @@
-/jackrabbit/oak/trunk:1584578,1584602,1584614,1584616,1584709,1584781,1584937,1585297,1585304-1585305,1585420,1585424,1585427,1585448,1585465,1585468,1585486,1585497,1585509,1585647,1585655-1585656,1585661,1585665-1585666,1585669-1585670,1585673,1585680,1585719,1585763,1585770,1585896,1585904,1585907,1585940,1585949,1585951,1585956,1585962-1585963,1586287,1586320,1586364,1586372,1586655,1586836,1587130,1587224,1587399,1587408,1587472,1587485,1587488,1587538,1587580,1587807,1588033,1588042,1588046,1588066,1588201,1589025,1589101,1589137,1589141,1589263,1589440,1589442,1589484,1589488,1589661,1589664,1589682,1589708,1589741,1589748,1589789,1589794,1589850,1589864,1590628,1590660,1590684,1590697,1590701,1590980,1590988,1591101,1591226,1591229,1591293,1591314,1591317,1591362,1591374,1591381,1591438,1591467,1591552,1591704,1591713,1591715,1591723,1591874,1592487,1592512,1592658,1592665,1592677,1592742,1592744,1592787,1592809,1592955,1593036,1593048,1593061,1593133,1593210-1593211,1593231
 ,1593245,1593250,1593294,1593304,1593317,1593342,1593554,1594158-1594164,1594166-1594167,1594169,1594237,1594800,1594808,1594835,1594888,1595147,1595457,1595856,1596241,1596474,1596534,1596844,1597569,1597795,1597854,1597860,1598292,1598302,1598352,1598369,1598595,1598631,1598696,1598732,1598797-1598798,1599299,1599332,1599416,1599434,1599671,1600088,1600935,1601309,1601388,1601578,1601676,1601757,1601768,1601814,1601833,1601838,1601853,1601878,1601888,1601922,1602156,1602174,1602179,1602183,1602207,1602227,1602256,1602261,1602796-1602797,1602800,1602809,1602853,1602872,1602914,1603155,1603307,1603401,1603441,1603748,1604166,1605030,1605036,1605038,1605292,1605447,1605526,1605670,1605725,1605831,1605852,1606077,1606079,1606087,1606638,1606641,1606644,1606708,1606711,1607031-1607032,1607077,1607127,1607141,1607152,1607185,1607196,1607331,1607362,1607366,1607392,1607526,1607557,1607664,1607737,1608560,1608731,1608783,1609064,1609081,1609165,1609488,1610489,1610592,1610603,1610634,1610
 658,1610664,1611021,1611041,1611275,1611277,1611313,1611332,1611584,1612560,1612825,1612993,1613018,1613041,1614265,1614272,1614344-1614345,1614384-1614385,1614397,1614405-1614406,1614574,1614591,1614593,1614596,1614604,1614689,1614807,1614835,1614891,1615417-1615418,1616182,1616236,1616463,1616719,1617417,1617451,1617463,1617711,1618158,1618613,1618624,1618709,1619222,1619411,1619695,1619800,1619808,1619815,1619823-1619824,1620512,1620581,1620585,1620634,1620898,1620905,1621115,1621123-1621124,1621168,1621192,1621201,1621706,1621962,1622197,1622201,1622207,1622250,1622479,1623364,1623766,1623827,1623949,1623969,1623973,1624216,1624317,1624551,1624973,1624993-1624994,1625025,1625036,1625158,1625224,1625237,1625299,1625348,1625620,1625916,1625962-1625963,1626021,1626053,1626163,1626168,1626175,1626191,1626265,1626770,1627047,1627052,1627228,1627346,1627470,1627473,1627479,1627503,1627586,1627590,1627715,1627731,1628180,1628198,1628262,1628447,1628608,1629688,1629840,1629917,1630055-1
 630057,1630156,1630299,1630338,1630773,1631283-1631284,1631333-1631334,1631617-1631619,1631630,1631699,1631704,1631711,1631967-1631969,1631986,1631990,1631999,1632002-1632003,1632017,1632258,1632264,1632270,1632293,1632303,1632592,1632605,1633315,1633559-1633560,1633562,1633567,1633571,1633598,1633608,1633641,1633687,1633697,1633768,1633783,1634505,1634513,1634774,1634779,1634781,1634792,1634803,1634814,1634816,1634838,1634841,1634852,1634864,1634896,1634898,1635044-1635045,1635060,1635077,1635089,1635102,1635108,1635218,1635387,1635435,1635518,1635563,1635586,1636336,1636348,1636505,1636585,1636799,1637368,1637382,1637413,1637651,1637815,1638779-1638783,1639260,1639577,1639622,1639963,1639966,1639973,1640134,1640143,1640555-1640556,1640694-1640695,1640715,1640722-1640723,1640728,1640863-1640872,1641340,1641350,1641352,1641541,1641596-1641599,1641601,1641662,1641671,1641695,1641771,1641802,1641811,1641950,1642031,1642056,1642119,1642285,1642648,1642667,1642954,1642959,1643111,164317
 8,1643186,1643204,1643287,1643767,1643774,1643982,1644016,1644106,1644366,1644383,1644397-1644398,1644407,1644479,1644547,1644552,1644554,1644650,1644654,1644689,1644750,1645421,1645424,1645459,1645585,1645611,1645637,1645646,1645660-1645663,1645888,1645901,1645948,1645966,1645970-1645971,1646014,1646164,1646174,1649743,1649803,1650015,1650239,1650529,1650797,1651323,1651382,1651643,1651730,1651988-1651989,1651996,1652059,1652467,1652965,1652971,1653446
+/jackrabbit/oak/trunk:1584578,1584602,1584614,1584616,1584709,1584781,1584937,1585297,1585304-1585305,1585420,1585424,1585427,1585448,1585465,1585468,1585486,1585497,1585509,1585647,1585655-1585656,1585661,1585665-1585666,1585669-1585670,1585673,1585680,1585719,1585763,1585770,1585896,1585904,1585907,1585940,1585949,1585951,1585956,1585962-1585963,1586287,1586320,1586364,1586372,1586655,1586836,1587130,1587224,1587399,1587408,1587472,1587485,1587488,1587538,1587580,1587807,1588033,1588042,1588046,1588066,1588201,1589025,1589101,1589137,1589141,1589263,1589440,1589442,1589484,1589488,1589661,1589664,1589682,1589708,1589741,1589748,1589789,1589794,1589850,1589864,1590628,1590660,1590684,1590697,1590701,1590980,1590988,1591101,1591226,1591229,1591293,1591314,1591317,1591362,1591374,1591381,1591438,1591467,1591552,1591704,1591713,1591715,1591723,1591874,1592487,1592512,1592658,1592665,1592677,1592742,1592744,1592787,1592809,1592955,1593036,1593048,1593061,1593133,1593210-1593211,1593231
 ,1593245,1593250,1593294,1593304,1593317,1593342,1593554,1594158-1594164,1594166-1594167,1594169,1594237,1594800,1594808,1594835,1594888,1595147,1595457,1595856,1596241,1596474,1596534,1596844,1597569,1597795,1597854,1597860,1598292,1598302,1598352,1598369,1598595,1598631,1598696,1598732,1598797-1598798,1599299,1599332,1599416,1599434,1599671,1600088,1600935,1601309,1601388,1601578,1601676,1601757,1601768,1601814,1601833,1601838,1601853,1601878,1601888,1601922,1602156,1602174,1602179,1602183,1602207,1602227,1602256,1602261,1602796-1602797,1602800,1602809,1602853,1602872,1602914,1603155,1603307,1603401,1603441,1603748,1604166,1605030,1605036,1605038,1605292,1605447,1605526,1605670,1605725,1605831,1605852,1606077,1606079,1606087,1606638,1606641,1606644,1606708,1606711,1607031-1607032,1607077,1607127,1607141,1607152,1607185,1607196,1607331,1607362,1607366,1607392,1607526,1607557,1607664,1607737,1608560,1608731,1608783,1609064,1609081,1609165,1609488,1610489,1610592,1610603,1610634,1610
 658,1610664,1611021,1611041,1611275,1611277,1611313,1611332,1611584,1612560,1612825,1612993,1613018,1613041,1614265,1614272,1614344-1614345,1614384-1614385,1614397,1614405-1614406,1614574,1614591,1614593,1614596,1614604,1614689,1614807,1614835,1614891,1615417-1615418,1616182,1616236,1616463,1616719,1617417,1617451,1617463,1617711,1618158,1618613,1618624,1618709,1619222,1619411,1619695,1619800,1619808,1619815,1619823-1619824,1620512,1620581,1620585,1620634,1620898,1620905,1621115,1621123-1621124,1621168,1621192,1621201,1621706,1621962,1622197,1622201,1622207,1622250,1622479,1623364,1623766,1623827,1623949,1623969,1623973,1624216,1624317,1624551,1624973,1624993-1624994,1625025,1625036,1625158,1625224,1625237,1625299,1625348,1625620,1625916,1625962-1625963,1626021,1626053,1626163,1626168,1626175,1626191,1626265,1626770,1627047,1627052,1627228,1627346,1627470,1627473,1627479,1627503,1627586,1627590,1627715,1627731,1628180,1628198,1628262,1628447,1628608,1629688,1629840,1629917,1630055-1
 630057,1630156,1630299,1630338,1630773,1631283-1631284,1631333-1631334,1631617-1631619,1631630,1631699,1631704,1631711,1631967-1631969,1631986,1631990,1631999,1632002-1632003,1632017,1632258,1632264,1632270,1632293,1632303,1632592,1632605,1633315,1633559-1633560,1633562,1633567,1633571,1633598,1633608,1633641,1633687,1633697,1633768,1633783,1634505,1634513,1634774,1634779,1634781,1634792,1634803,1634814,1634816,1634838,1634841,1634852,1634864,1634896,1634898,1635044-1635045,1635060,1635077,1635089,1635102,1635108,1635218,1635387,1635435,1635518,1635563,1635586,1636336,1636348,1636505,1636585,1636799,1637368,1637382,1637413,1637651,1637815,1638779-1638783,1639260,1639577,1639622,1639963,1639966,1639973,1640134,1640143,1640555-1640556,1640694-1640695,1640715,1640722-1640723,1640728,1640863-1640872,1641340,1641350,1641352,1641541,1641596-1641599,1641601,1641662,1641671,1641695,1641771,1641802,1641811,1641950,1642031,1642056,1642119,1642285,1642648,1642667,1642954,1642959,1643111,164317
 8,1643186,1643204,1643287,1643767,1643774,1643982,1644016,1644106,1644366,1644383,1644397-1644398,1644407,1644479,1644547,1644552,1644554,1644650,1644654,1644689,1644750,1645421,1645424,1645459,1645585,1645611,1645637,1645646,1645660-1645663,1645888,1645901,1645948,1645966,1645970-1645971,1646014,1646164,1646174,1649743,1649803,1650015,1650239,1650529,1650797,1651323,1651382,1651643,1651730,1651988-1651989,1651996,1652059,1652467,1652965,1652971,1653446,1653463
 /jackrabbit/trunk:1345480

Modified: jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/Branch.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/Branch.java?rev=1653476&r1=1653475&r2=1653476&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/Branch.java (original)
+++ jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/Branch.java Wed Jan 21 10:51:50 2015
@@ -20,6 +20,8 @@ import static com.google.common.base.Pre
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.collect.Iterables.transform;
 
+import java.lang.ref.ReferenceQueue;
+import java.lang.ref.WeakReference;
 import java.util.Map;
 import java.util.NavigableMap;
 import java.util.SortedSet;
@@ -27,6 +29,7 @@ import java.util.concurrent.ConcurrentSk
 
 import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
 
 import org.apache.jackrabbit.oak.plugins.document.util.MapFactory;
 
@@ -52,15 +55,29 @@ class Branch {
     private MapFactory mapFactory;
 
     /**
+     * The branch reference.
+     */
+    private final BranchReference ref;
+
+    /**
      * Create a new branch instance with an initial set of commits and a given
-     * base revision.
+     * base revision. The life time of this branch can be controlled with
+     * the {@code guard} parameter. Once the {@code guard} object becomes weakly
+     * reachable, the {@link BranchReference} for this branch is appended to
+     * the passed {@code queue}. No {@link BranchReference} is appended if the
+     * passed {@code guard} is {@code null}.
      *
      * @param commits the initial branch commits.
      * @param base the base commit.
+     * @param queue a {@link BranchReference} to this branch will be appended to
+     *              this queue when {@code guard} becomes weakly reachable.
+     * @param guard controls the life time of this branch.
      * @throws IllegalArgumentException if base is a branch revision.
      */
     Branch(@Nonnull SortedSet<Revision> commits,
-           @Nonnull Revision base) {
+           @Nonnull Revision base,
+           @Nonnull ReferenceQueue<Object> queue,
+           @Nullable Object guard) {
         checkArgument(!checkNotNull(base).isBranch(), "base is not a trunk revision: %s", base);
         this.base = base;
         this.commits = new ConcurrentSkipListMap<Revision, BranchCommit>(commits.comparator());
@@ -68,6 +85,11 @@ class Branch {
             this.commits.put(r.asBranchRevision(),
                     new BranchCommitImpl(base, r.asBranchRevision()));
         }
+        if (guard != null) {
+            this.ref = new BranchReference(queue, this, guard);
+        } else {
+            this.ref = null;
+        }
     }
 
     /**
@@ -164,6 +186,15 @@ class Branch {
     }
 
     /**
+     * @return the branch reference or {@code null} if no guard object was
+     *         passed to the constructor of this branch. 
+     */
+    @CheckForNull
+    BranchReference getRef() {
+        return ref;
+    }
+
+    /**
      * Removes the commit with the given revision <code>r</code>. Does nothing
      * if there is no such commit.
      *
@@ -358,4 +389,20 @@ class Branch {
             throw new UnsupportedOperationException("RebaseCommit is read-only");
         }
     }
+
+    final static class BranchReference extends WeakReference<Object> {
+
+        private final Branch branch;
+
+        private BranchReference(@Nonnull ReferenceQueue<Object> queue,
+                                @Nonnull Branch branch,
+                                @Nonnull Object referent) {
+            super(checkNotNull(referent), queue);
+            this.branch = checkNotNull(branch);
+        }
+
+        Branch getBranch() {
+            return branch;
+        }
+    }
 }

Modified: jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/Commit.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/Commit.java?rev=1653476&r1=1653475&r2=1653476&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/Commit.java (original)
+++ jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/Commit.java Wed Jan 21 10:51:50 2015
@@ -37,6 +37,7 @@ import org.apache.jackrabbit.oak.commons
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static com.google.common.base.Preconditions.checkNotNull;
 import static org.apache.jackrabbit.oak.plugins.document.Collection.NODES;
 import static org.apache.jackrabbit.oak.plugins.document.NodeDocument.COLLISIONS;
 import static org.apache.jackrabbit.oak.plugins.document.NodeDocument.SPLIT_CANDIDATE_THRESHOLD;
@@ -49,6 +50,7 @@ public class Commit {
     private static final Logger LOG = LoggerFactory.getLogger(Commit.class);
 
     private final DocumentNodeStore nodeStore;
+    private final DocumentNodeStoreBranch branch;
     private final Revision baseRevision;
     private final Revision revision;
     private HashMap<String, UpdateOp> operations = new LinkedHashMap<String, UpdateOp>();
@@ -67,10 +69,25 @@ public class Commit {
     /** Set of all nodes which have binary properties. **/
     private HashSet<String> nodesWithBinaries = Sets.newHashSet();
 
-    Commit(DocumentNodeStore nodeStore, Revision baseRevision, Revision revision) {
+    /**
+     * Create a new Commit.
+     *  
+     * @param nodeStore the node store.
+     * @param revision the revision for this commit.
+     * @param baseRevision the base revision for this commit or {@code null} if
+     *                     there is none.
+     * @param branch the branch associated with this commit or {@code null} if
+     *               there is none.
+     *                              
+     */
+    Commit(@Nonnull DocumentNodeStore nodeStore,
+           @Nonnull Revision revision,
+           @Nullable Revision baseRevision,
+           @Nullable DocumentNodeStoreBranch branch) {
+        this.nodeStore = checkNotNull(nodeStore);
+        this.revision = checkNotNull(revision);
         this.baseRevision = baseRevision;
-        this.revision = revision;
-        this.nodeStore = nodeStore;
+        this.branch = branch;
     }
 
     UpdateOp getUpdateOperationForNode(String path) {
@@ -160,7 +177,8 @@ public class Commit {
             Branch b = nodeStore.getBranches().getBranch(baseRev);
             if (b == null) {
                 // baseRev is marker for new branch
-                b = nodeStore.getBranches().create(baseRev.asTrunkRevision(), rev);
+                b = nodeStore.getBranches().create(
+                        baseRev.asTrunkRevision(), rev, branch);
             } else {
                 b.addCommit(rev);
             }

Modified: jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentMK.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentMK.java?rev=1653476&r1=1653475&r2=1653476&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentMK.java (original)
+++ jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentMK.java Wed Jan 21 10:51:50 2015
@@ -249,7 +249,7 @@ public class DocumentMK implements Micro
         boolean success = false;
         boolean isBranch = false;
         Revision rev;
-        Commit commit = nodeStore.newCommit(baseRevId != null ? Revision.fromString(baseRevId) : null);
+        Commit commit = nodeStore.newCommit(baseRevId != null ? Revision.fromString(baseRevId) : null, null);
         try {
             Revision baseRev = commit.getBaseRevision();
             isBranch = baseRev != null && baseRev.isBranch();
@@ -315,7 +315,7 @@ public class DocumentMK implements Micro
         if (!ancestor.isBranch()) {
             throw new MicroKernelException("Not a branch revision: " + ancestorRevisionId);
         }
-        return nodeStore.reset(branch, ancestor).toString();
+        return nodeStore.reset(branch, ancestor, null).toString();
     }
 
     @Override

Modified: jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java?rev=1653476&r1=1653475&r2=1653476&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java (original)
+++ jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStore.java Wed Jan 21 10:51:50 2015
@@ -198,8 +198,6 @@ public final class DocumentNodeStore
     /**
      * Unmerged branches of this DocumentNodeStore instance.
      */
-    // TODO at some point, open (unmerged) branches
-    // need to be garbage collected (in-memory and on disk)
     private final UnmergedBranches branches;
 
     /**
@@ -401,7 +399,7 @@ public final class DocumentNodeStore
         if (store.find(Collection.NODES, Utils.getIdFromPath("/")) == null) {
             // root node is missing: repository is not initialized
             Revision head = newRevision();
-            Commit commit = new Commit(this, null, head);
+            Commit commit = new Commit(this, head, null, null);
             DocumentNodeState n = new DocumentNodeState(this, "/", head);
             commit.addNode(n);
             commit.applyToDocumentStore();
@@ -492,11 +490,6 @@ public final class DocumentNodeStore
         }
     }
 
-    @Nonnull
-    Revision getHeadRevision() {
-        return headRevision;
-    }
-
     Revision setHeadRevision(@Nonnull Revision newHead) {
         checkArgument(!newHead.isBranch());
         Revision previous = headRevision;
@@ -532,10 +525,16 @@ public final class DocumentNodeStore
      *
      * @param base the base revision for the commit or <code>null</code> if the
      *             commit should use the current head revision as base.
+     * @param branch the branch instance if this is a branch commit. The life
+     *               time of this branch commit is controlled by the
+     *               reachability of this parameter. Once {@code branch} is
+     *               weakly reachable, the document store implementation is
+     *               free to remove the commits associated with the branch.
      * @return a new commit.
      */
     @Nonnull
-    Commit newCommit(@Nullable Revision base) {
+    Commit newCommit(@Nullable Revision base,
+                     @Nullable DocumentNodeStoreBranch branch) {
         if (base == null) {
             base = headRevision;
         }
@@ -543,7 +542,7 @@ public final class DocumentNodeStore
         boolean success = false;
         Commit c;
         try {
-            c = new Commit(this, base, commitQueue.createRevision());
+            c = new Commit(this, commitQueue.createRevision(), base, branch);
             success = true;
         } finally {
             if (!success) {
@@ -1125,7 +1124,9 @@ public final class DocumentNodeStore
     }
 
     @Nonnull
-    Revision reset(@Nonnull Revision branchHead, @Nonnull Revision ancestor) {
+    Revision reset(@Nonnull Revision branchHead,
+                   @Nonnull Revision ancestor,
+                   @Nullable DocumentNodeStoreBranch branch) {
         checkNotNull(branchHead);
         checkNotNull(ancestor);
         Branch b = getBranches().getBranch(branchHead);
@@ -1145,7 +1146,7 @@ public final class DocumentNodeStore
             return branchHead;
         }
         boolean success = false;
-        Commit commit = newCommit(branchHead);
+        Commit commit = newCommit(branchHead, branch);
         try {
             Iterator<Revision> it = b.getCommits().tailSet(ancestor).iterator();
             // first revision is the ancestor (tailSet is inclusive)
@@ -1421,6 +1422,11 @@ public final class DocumentNodeStore
         return clusterId;
     }
 
+    @Nonnull
+    public Revision getHeadRevision() {
+        return headRevision;
+    }
+
     //----------------------< background operations >---------------------------
 
     public synchronized void runBackgroundOperations() {
@@ -1437,6 +1443,11 @@ public final class DocumentNodeStore
         try {
             long start = clock.getTime();
             long time = start;
+            // clean orphaned branches and collisions
+            cleanOrphanedBranches();
+            cleanCollisions();
+            long cleanTime = clock.getTime() - time;
+            time = clock.getTime();
             // split documents (does not create new revisions)
             backgroundSplit();
             long splitTime = clock.getTime() - time;
@@ -1448,12 +1459,12 @@ public final class DocumentNodeStore
             // pull in changes from other cluster nodes
             backgroundRead(true);
             long readTime = clock.getTime() - time;
-            String msg = "Background operations stats (split:{}, write:{}, read:{})";
+            String msg = "Background operations stats (clean:{}, split:{}, write:{}, read:{})";
             if (clock.getTime() - start > TimeUnit.SECONDS.toMillis(10)) {
                 // log as info if it took more than 10 seconds
-                LOG.info(msg, splitTime, writeTime, readTime);
+                LOG.info(msg, cleanTime, splitTime, writeTime, readTime);
             } else {
-                LOG.debug(msg, splitTime, writeTime, readTime);
+                LOG.debug(msg, cleanTime, splitTime, writeTime, readTime);
             }
         } catch (RuntimeException e) {
             if (isDisposed.get()) {
@@ -1583,6 +1594,47 @@ public final class DocumentNodeStore
         return Revision.getCurrentTimestamp() - REMEMBER_REVISION_ORDER_MILLIS;
     }
 
+    private void cleanOrphanedBranches() {
+        Branch b;
+        while ((b = branches.pollOrphanedBranch()) != null) {
+            LOG.debug("Cleaning up orphaned branch with base revision: {}, " + 
+                    "commits: {}", b.getBase(), b.getCommits());
+            UpdateOp op = new UpdateOp(Utils.getIdFromPath("/"), false);
+            for (Revision r : b.getCommits()) {
+                r = r.asTrunkRevision();
+                NodeDocument.removeRevision(op, r);
+            }
+            store.findAndUpdate(NODES, op);
+        }
+    }
+    
+    private void cleanCollisions() {
+        String id = Utils.getIdFromPath("/");
+        NodeDocument root = store.find(NODES, id);
+        if (root == null) {
+            return;
+        }
+        Revision head = getHeadRevision();
+        Map<Revision, String> map = root.getLocalMap(NodeDocument.COLLISIONS);
+        UpdateOp op = new UpdateOp(id, false);
+        for (Revision r : map.keySet()) {
+            if (r.getClusterId() == clusterId) {
+                // remove collision if there is no active branch with
+                // this revision and the revision is before the current
+                // head. That is, the collision cannot be related to commit
+                // which is progress.
+                if (branches.getBranchCommit(r) == null 
+                        && isRevisionNewer(head, r)) {
+                    NodeDocument.removeCollision(op, r);
+                }
+            }
+        }
+        if (op.hasChanges()) {
+            LOG.debug("Removing collisions {}", op.getChanges().keySet());
+            store.findAndUpdate(NODES, op);
+        }
+    }
+
     private void backgroundSplit() {
         for (Iterator<String> it = splitCandidates.keySet().iterator(); it.hasNext();) {
             String id = it.next();

Modified: jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java?rev=1653476&r1=1653475&r2=1653476&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java (original)
+++ jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java Wed Jan 21 10:51:50 2015
@@ -77,7 +77,11 @@ class DocumentNodeStoreBranch
     @Override
     protected DocumentNodeState reset(@Nonnull DocumentNodeState branchHead,
                                    @Nonnull DocumentNodeState ancestor) {
-        return store.getRoot(store.reset(branchHead.getRevision(), ancestor.getRevision()));
+        return store.getRoot(
+                store.reset(
+                        branchHead.getRevision(), 
+                        ancestor.getRevision(),
+                        this));
     }
 
     @Override
@@ -167,7 +171,7 @@ class DocumentNodeStoreBranch
                                       DocumentNodeState base,
                                       CommitInfo info) {
         boolean success = false;
-        Commit c = store.newCommit(base.getRevision());
+        Commit c = store.newCommit(base.getRevision(), this);
         Revision rev;
         try {
             op.with(c);

Modified: jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/MergeCommit.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/MergeCommit.java?rev=1653476&r1=1653475&r2=1653476&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/MergeCommit.java (original)
+++ jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/MergeCommit.java Wed Jan 21 10:51:50 2015
@@ -29,7 +29,7 @@ class MergeCommit extends Commit {
     MergeCommit(DocumentNodeStore nodeStore,
                 Revision baseRevision,
                 SortedSet<Revision> revisions) {
-        super(nodeStore, baseRevision, revisions.last());
+        super(nodeStore, revisions.last(), baseRevision, null);
         this.mergeRevs = revisions;
     }
 

Modified: jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/RevisionContext.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/RevisionContext.java?rev=1653476&r1=1653475&r2=1653476&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/RevisionContext.java (original)
+++ jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/RevisionContext.java Wed Jan 21 10:51:50 2015
@@ -18,6 +18,8 @@ package org.apache.jackrabbit.oak.plugin
 
 import java.util.Comparator;
 
+import javax.annotation.Nonnull;
+
 /**
  * Provides revision related context.
  */
@@ -44,4 +46,9 @@ public interface RevisionContext {
      */
     int getClusterId();
 
+    /**
+     * @return the current head revision.
+     */
+    @Nonnull
+    Revision getHeadRevision();
 }

Modified: jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/SplitOperations.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/SplitOperations.java?rev=1653476&r1=1653475&r2=1653476&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/SplitOperations.java (original)
+++ jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/SplitOperations.java Wed Jan 21 10:51:50 2015
@@ -20,6 +20,7 @@ package org.apache.jackrabbit.oak.plugin
 
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -73,6 +74,7 @@ class SplitOperations {
     private Revision low;
     private int numValues;
     private Map<String, NavigableMap<Revision, String>> committedChanges;
+    private Map<String, Set<Revision>> garbage;
     private Set<Revision> mostRecentRevs;
     private Set<Revision> splitRevs;
     private List<UpdateOp> splitOps;
@@ -115,6 +117,7 @@ class SplitOperations {
         splitOps = Lists.newArrayList();
         mostRecentRevs = Sets.newHashSet();
         splitRevs = Sets.newHashSet();
+        garbage = Maps.newHashMap();
         committedChanges = getCommittedLocalChanges();
 
         // revisions of the most recent committed changes on this document
@@ -133,6 +136,9 @@ class SplitOperations {
 
         // remove stale references to previous docs
         disconnectStalePrevDocs();
+        
+        // remove garbage
+        removeGarbage();
 
         // main document must be updated last
         if (main != null) {
@@ -370,11 +376,33 @@ class SplitOperations {
                 }
                 if (doc.isCommitted(rev)) {
                     splitMap.put(rev, entry.getValue());
+                } else if (isGarbage(rev)) {
+                    addGarbage(rev, property);
                 }
             }
         }
         return committedLocally;
     }
+    
+    private boolean isGarbage(Revision rev) {
+        Revision head = context.getHeadRevision();
+        Comparator<Revision> comp = context.getRevisionComparator();
+        if (comp.compare(head, rev) <= 0) {
+            // this may be an in-progress commit
+            return false;
+        }
+        // garbage if not part of an active branch
+        return context.getBranches().getBranchCommit(rev) == null;
+    }
+    
+    private void addGarbage(Revision rev, String property) {
+        Set<Revision> revisions = garbage.get(property);
+        if (revisions == null) {
+            revisions = Sets.newHashSet();
+            garbage.put(property, revisions);
+        }
+        revisions.add(rev);
+    }
 
     private void disconnectStalePrevDocs() {
         NavigableMap<Revision, Range> ranges = doc.getPreviousRanges(true);
@@ -410,6 +438,21 @@ class SplitOperations {
 
         }
     }
+    
+    private void removeGarbage() {
+        if (garbage.isEmpty()) {
+            return;
+        } else if (main == null) {
+            main = new UpdateOp(id, false);
+        }
+        for (Map.Entry<String, Set<Revision>> entry : garbage.entrySet()) {
+            for (Revision r : entry.getValue()) {
+                main.removeMapEntry(entry.getKey(), r);
+                NodeDocument.removeCommitRoot(main, r);
+                NodeDocument.removeRevision(main, r);
+            }
+        }
+    }
 
     private void trackHigh(Revision r) {
         if (high == null || isRevisionNewer(context, r, high)) {

Modified: jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/UnmergedBranches.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/UnmergedBranches.java?rev=1653476&r1=1653475&r2=1653476&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/UnmergedBranches.java (original)
+++ jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/UnmergedBranches.java Wed Jan 21 10:51:50 2015
@@ -16,6 +16,7 @@
  */
 package org.apache.jackrabbit.oak.plugins.document;
 
+import java.lang.ref.ReferenceQueue;
 import java.util.Comparator;
 import java.util.List;
 import java.util.SortedSet;
@@ -25,8 +26,10 @@ import java.util.concurrent.atomic.Atomi
 
 import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
 
 import org.apache.jackrabbit.oak.plugins.document.Branch.BranchCommit;
+import org.apache.jackrabbit.oak.plugins.document.Branch.BranchReference;
 import org.apache.jackrabbit.oak.plugins.document.util.Utils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -43,11 +46,19 @@ class UnmergedBranches {
     private final Logger log = LoggerFactory.getLogger(getClass());
 
     /**
-     * Map of branches with the head of the branch as key.
+     * List of active branches.
      */
     private final List<Branch> branches = new CopyOnWriteArrayList<Branch>();
 
     /**
+     * Queue for {@link BranchReference} when the
+     * {@link DocumentNodeStoreBranch} associated with a {@link Branch} is only
+     * weakly reachable.
+     */
+    private final ReferenceQueue<Object> queue = 
+            new ReferenceQueue<Object>();
+
+    /**
      * Set to <code>true</code> once initialized.
      */
     private final AtomicBoolean initialized = new AtomicBoolean(false);
@@ -89,20 +100,28 @@ class UnmergedBranches {
     /**
      * Create a branch with an initial commit revision.
      *
-     * @param base the base revision of the branch.
-     * @param initial the initial commit to the branch.
+     * @param base the base revision of the branch (must be a non-branch revision).
+     * @param initial the initial commit to the branch (must be a branch revision).
+     * @param guard an optional guard object controlling the life time of this
+     *              branch. When {@code guard} becomes weakly reachable, the
+     *              branch will at some point later be considered orphaned.
+     *              Orphaned branches can be retrieved with
+     *              {@link #pollOrphanedBranch()}.
      * @return the branch.
-     * @throws IllegalArgumentException if
+     * @throws IllegalArgumentException if {@code base} is a branch revision
+     *          or {@code initial} is not a branch revision.
      */
     @Nonnull
-    Branch create(@Nonnull Revision base, @Nonnull Revision initial) {
+    Branch create(@Nonnull Revision base,
+                  @Nonnull Revision initial,
+                  @Nullable Object guard) {
         checkArgument(!checkNotNull(base).isBranch(),
                 "base is not a trunk revision: %s", base);
         checkArgument(checkNotNull(initial).isBranch(),
                 "initial is not a branch revision: %s", initial);
         SortedSet<Revision> commits = new TreeSet<Revision>(comparator);
         commits.add(initial);
-        Branch b = new Branch(commits, base);
+        Branch b = new Branch(commits, base, queue, guard);
         branches.add(b);
         return b;
     }
@@ -157,4 +176,20 @@ class UnmergedBranches {
     int size(){
         return branches.size();
     }
+
+    /**
+     * Polls for an orphaned branch. The implementation will remove the branch
+     * from the internal list of unmerged branches.
+     *
+     * @return an orphaned branch or {@code null} if there is none at this time.
+     */
+    Branch pollOrphanedBranch() {
+        BranchReference ref = (BranchReference) queue.poll();
+        if (ref != null) {
+            if (branches.remove(ref.getBranch())) {
+                return ref.getBranch();
+            }
+        }
+        return null;
+    }
 }

Modified: jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/UnsavedModifications.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/UnsavedModifications.java?rev=1653476&r1=1653475&r2=1653476&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/UnsavedModifications.java (original)
+++ jackrabbit/oak/branches/1.0/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/UnsavedModifications.java Wed Jan 21 10:51:50 2015
@@ -181,7 +181,7 @@ class UnsavedModifications implements Cl
                 int size = pathList.size();
                 if (updateOp == null) {
                     // create UpdateOp
-                    Commit commit = new Commit(store, null, r);
+                    Commit commit = new Commit(store, r, null, null);
                     updateOp = commit.getUpdateOperationForNode(p);
                     NodeDocument.setLastRev(updateOp, r);
                     lastRev = r;

Modified: jackrabbit/oak/branches/1.0/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/CommitQueueTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.0/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/CommitQueueTest.java?rev=1653476&r1=1653475&r2=1653476&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.0/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/CommitQueueTest.java (original)
+++ jackrabbit/oak/branches/1.0/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/CommitQueueTest.java Wed Jan 21 10:51:50 2015
@@ -72,7 +72,7 @@ public class CommitQueueTest {
                 public void run() {
                     try {
                         for (int i = 0; i < COMMITS_PER_WRITER; i++) {
-                            Commit commit = store.newCommit(null);
+                            Commit commit = store.newCommit(null, null);
                             try {
                                 Thread.sleep(0, random.nextInt(1000));
                             } catch (InterruptedException e) {

Modified: jackrabbit/oak/branches/1.0/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.0/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreTest.java?rev=1653476&r1=1653475&r2=1653476&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.0/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreTest.java (original)
+++ jackrabbit/oak/branches/1.0/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreTest.java Wed Jan 21 10:51:50 2015
@@ -232,7 +232,7 @@ public class DocumentNodeStoreTest {
             public void run() {
                 try {
                     Revision r = store.newRevision();
-                    Commit c = new Commit(store, Revision.fromString(head), r);
+                    Commit c = new Commit(store, r, Revision.fromString(head), null);
                     c.addNode(new DocumentNodeState(store, "/foo/node", r));
                     c.addNode(new DocumentNodeState(store, "/bar/node", r));
                     c.apply();
@@ -249,7 +249,7 @@ public class DocumentNodeStoreTest {
         created.acquireUninterruptibly();
         // commit will succeed and add collision marker to writer commit
         Revision r = store.newRevision();
-        Commit c = new Commit(store, Revision.fromString(head), r);
+        Commit c = new Commit(store, r, Revision.fromString(head), null);
         c.addNode(new DocumentNodeState(store, "/foo/node", r));
         c.addNode(new DocumentNodeState(store, "/bar/node", r));
         c.apply();

Modified: jackrabbit/oak/branches/1.0/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DummyRevisionContext.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.0/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DummyRevisionContext.java?rev=1653476&r1=1653475&r2=1653476&view=diff
==============================================================================
--- jackrabbit/oak/branches/1.0/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DummyRevisionContext.java (original)
+++ jackrabbit/oak/branches/1.0/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/document/DummyRevisionContext.java Wed Jan 21 10:51:50 2015
@@ -18,6 +18,8 @@ package org.apache.jackrabbit.oak.plugin
 
 import java.util.Comparator;
 
+import javax.annotation.Nonnull;
+
 /**
  * A revision context for tests.
  */
@@ -47,4 +49,10 @@ public class DummyRevisionContext implem
     public int getClusterId() {
         return 1;
     }
+
+    @Nonnull
+    @Override
+    public Revision getHeadRevision() {
+        return Revision.newRevision(1);
+    }
 }