|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.jackrabbit.oak.plugins.document; |
| 18 | + |
| 19 | +import org.slf4j.Logger; |
| 20 | +import static org.slf4j.LoggerFactory.getLogger; |
| 21 | + |
| 22 | +import java.time.Instant; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.Date; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.stream.Collectors; |
| 28 | + |
| 29 | +public class FullGcBin { |
| 30 | + private static final Logger LOG = getLogger(FullGcBin.class); |
| 31 | + private final DocumentStore documentStore; |
| 32 | + private boolean enabled; |
| 33 | + |
| 34 | + public FullGcBin(DocumentStore ds) { |
| 35 | + documentStore = ds; |
| 36 | + } |
| 37 | + |
| 38 | + public int remove(Map<String, Long> orphanOrDeletedRemovalMap) { |
| 39 | + if (orphanOrDeletedRemovalMap.isEmpty() || !addToBin(orphanOrDeletedRemovalMap)) { |
| 40 | + return 0; |
| 41 | + } |
| 42 | + |
| 43 | + // use remove() with the modified check to rule |
| 44 | + // out any further race-condition where this removal |
| 45 | + // races with a un-orphan/re-creation as a result of which |
| 46 | + // the node should now not be removed. The modified check |
| 47 | + // ensures a node would then not be removed |
| 48 | + // (and as a result the removedSize != map.size()) |
| 49 | + return documentStore.remove(Collection.NODES, orphanOrDeletedRemovalMap); |
| 50 | + } |
| 51 | + |
| 52 | + public List<NodeDocument> findAndUpdate(List<UpdateOp> updateOpList) { |
| 53 | + LOG.info("Updating {} documents", updateOpList.size()); |
| 54 | + if (updateOpList.isEmpty() || !addToBin(updateOpList)) { |
| 55 | + return Collections.emptyList(); |
| 56 | + } |
| 57 | + return documentStore.findAndUpdate(Collection.NODES, updateOpList); |
| 58 | + } |
| 59 | + |
| 60 | + private boolean addToBin(Map<String, Long> orphanOrDeletedRemovalMap) { |
| 61 | + if (!enabled) { |
| 62 | + LOG.info("Bin is disabled, skipping adding delete candidate documents to bin"); |
| 63 | + return true; |
| 64 | + } |
| 65 | + LOG.info("Adding {} delete candidate documents to bin", orphanOrDeletedRemovalMap.size()); |
| 66 | + List<UpdateOp> docs = orphanOrDeletedRemovalMap.keySet().stream() |
| 67 | + .map(e -> new UpdateOp(e, true)) |
| 68 | + .map(this::insertOp) |
| 69 | + .collect(Collectors.toList()); |
| 70 | + try { |
| 71 | + return documentStore.create(Collection.SETTINGS, docs); |
| 72 | + } catch (Exception e) { |
| 73 | + LOG.error("Error while adding delete candidate documents to bin", e); |
| 74 | + } |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + private boolean addToBin(List<UpdateOp> updateOpList) { |
| 79 | + if (!enabled) { |
| 80 | + LOG.info("Bin is disabled, skipping adding removed properties to bin"); |
| 81 | + return true; |
| 82 | + } |
| 83 | + LOG.info("Adding {} removed properties to bin", updateOpList.size()); |
| 84 | + List<UpdateOp> binOpList = updateOpList.stream().map(this::insertOp).collect(Collectors.toList()); |
| 85 | + try { |
| 86 | + documentStore.createOrUpdate(Collection.SETTINGS, binOpList); |
| 87 | + return true; |
| 88 | + } catch (Exception e) { |
| 89 | + LOG.error("Error while adding removed properties to bin", e); |
| 90 | + } |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Create an insert operation from the given update operation |
| 96 | + * |
| 97 | + * @param op the update operation |
| 98 | + * @return the insert operation |
| 99 | + */ |
| 100 | + private UpdateOp insertOp(UpdateOp op) { |
| 101 | + UpdateOp insertOp = new UpdateOp("/bin/" + op.getId(), true); |
| 102 | + //copy removed properties to the new document |
| 103 | + op.getChanges().forEach((k, v) -> { |
| 104 | + if (v.type == UpdateOp.Operation.Type.REMOVE) { |
| 105 | + insertOp.set(k.getName(), ""); |
| 106 | + } |
| 107 | + }); |
| 108 | + //this property is used to track the time when the document was added to the bin |
| 109 | + //it can be used as a TTL index property to automatically remove the document after a certain time |
| 110 | + //see https://www.mongodb.com/docs/manual/core/index-ttl/#std-label-index-feature-ttl |
| 111 | + insertOp.set("_gcCollectedAt", Instant.now().toEpochMilli()); |
| 112 | + return insertOp; |
| 113 | + } |
| 114 | + |
| 115 | + public void setEnabled(boolean value) { |
| 116 | + this.enabled = value; |
| 117 | + } |
| 118 | +} |
0 commit comments