daniancu commented on code in PR #2252: URL: https://github.com/apache/jackrabbit-oak/pull/2252#discussion_r2065699539
########## oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoFullGcNodeBinSumBsonSize.java: ########## @@ -0,0 +1,137 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jackrabbit.oak.plugins.document.mongo; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import com.mongodb.BasicDBObject; +import com.mongodb.client.model.Updates; +import org.bson.conversions.Bson; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.jackrabbit.oak.plugins.document.FullGcNodeBin; +import org.apache.jackrabbit.oak.plugins.document.NodeDocument; +import org.apache.jackrabbit.oak.plugins.document.UpdateOp; + +import static com.mongodb.client.model.Filters.eq; +import static com.mongodb.client.model.Filters.in; +import static org.apache.jackrabbit.oak.plugins.document.Collection.NODES; +import static org.apache.jackrabbit.oak.plugins.document.Collection.SETTINGS; +import static org.apache.jackrabbit.oak.plugins.document.Document.ID; + +/** + * This class is a wrapper around a MongoFullGcNodeBin + * that sums the bson size of the documents that are removed or updated and then update the value in the SETTINGS collection + */ +class MongoFullGcNodeBinSumBsonSize implements FullGcNodeBin { + private static final Logger LOG = LoggerFactory.getLogger(MongoFullGcNodeBinSumBsonSize.class); + + private final MongoFullGcNodeBin delegate; + private final MongoDocumentStore store; + + public MongoFullGcNodeBinSumBsonSize(MongoFullGcNodeBin delegate) { + this.delegate = delegate; + this.store = delegate.getMongoDocumentStore(); + } + + @Override + public void setEnabled(boolean value) { + delegate.setEnabled(value); + } + + /** + * Remove the documents from the collection and sum the the bson size of the removed properties + * @param updateOpList the list of the documents to be removed + * @return the list of the documents removed + */ + @Override + public List<NodeDocument> findAndUpdate(List<UpdateOp> updateOpList) { + List<String> ids = updateOpList.stream().map(UpdateOp::getId).collect(Collectors.toList()); + //get the total bson size before the update + long initialBsonSize = getBsonSize(ids); + LOG.debug("bson size before update: {}", initialBsonSize); + //remove garbage properties + List<NodeDocument> updated = delegate.findAndUpdate(updateOpList); + if (!updated.isEmpty()) { + //calculate the diff of the bson size after update + long afterUpdateBsonSize = getBsonSize(ids); + LOG.debug("bson size after update: {}", afterUpdateBsonSize); + if (initialBsonSize > 0 && afterUpdateBsonSize > 0) { + //sum up the removed bson size + addBsonSize(initialBsonSize - afterUpdateBsonSize); + } + } + return updated; + } + /** + * Remove the documents from the collection and sum the their bson size + * @param orphanOrDeletedRemovalMap the map of the documents to be removed + * @return the number of documents removed + */ + @Override + public int remove(Map<String, Long> orphanOrDeletedRemovalMap) { + //get the total bson size before the update + long bsonSize = getBsonSize(new ArrayList<>(orphanOrDeletedRemovalMap.keySet())); + LOG.debug("bson size before remove: {}", bsonSize); + //remove garbage documents + int removed = delegate.remove(orphanOrDeletedRemovalMap); + //sum up the removed bson size + if (removed > 0 && bsonSize > 0) { + addBsonSize(bsonSize); + } + return removed; + } + + private void addBsonSize(long bsonSize) { + if (bsonSize <= 0) { + LOG.warn("bson size {} is not positive", bsonSize); + return; + } + //sum the bson size with the value from fullGcBsonSize document in the SETTINGS collection + Bson query = eq(ID, "versionGC"); + Bson update = Updates.inc("fullGcRemovedTotalBsonSize", bsonSize); Review Comment: created, needed to make them public btw ########## oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoFullGcNodeBinSumBsonSize.java: ########## @@ -0,0 +1,137 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jackrabbit.oak.plugins.document.mongo; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import com.mongodb.BasicDBObject; +import com.mongodb.client.model.Updates; +import org.bson.conversions.Bson; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.jackrabbit.oak.plugins.document.FullGcNodeBin; +import org.apache.jackrabbit.oak.plugins.document.NodeDocument; +import org.apache.jackrabbit.oak.plugins.document.UpdateOp; + +import static com.mongodb.client.model.Filters.eq; +import static com.mongodb.client.model.Filters.in; +import static org.apache.jackrabbit.oak.plugins.document.Collection.NODES; +import static org.apache.jackrabbit.oak.plugins.document.Collection.SETTINGS; +import static org.apache.jackrabbit.oak.plugins.document.Document.ID; + +/** + * This class is a wrapper around a MongoFullGcNodeBin + * that sums the bson size of the documents that are removed or updated and then update the value in the SETTINGS collection + */ +class MongoFullGcNodeBinSumBsonSize implements FullGcNodeBin { + private static final Logger LOG = LoggerFactory.getLogger(MongoFullGcNodeBinSumBsonSize.class); + + private final MongoFullGcNodeBin delegate; + private final MongoDocumentStore store; + + public MongoFullGcNodeBinSumBsonSize(MongoFullGcNodeBin delegate) { + this.delegate = delegate; + this.store = delegate.getMongoDocumentStore(); + } + + @Override + public void setEnabled(boolean value) { + delegate.setEnabled(value); + } + + /** + * Remove the documents from the collection and sum the the bson size of the removed properties + * @param updateOpList the list of the documents to be removed + * @return the list of the documents removed + */ + @Override + public List<NodeDocument> findAndUpdate(List<UpdateOp> updateOpList) { + List<String> ids = updateOpList.stream().map(UpdateOp::getId).collect(Collectors.toList()); + //get the total bson size before the update + long initialBsonSize = getBsonSize(ids); + LOG.debug("bson size before update: {}", initialBsonSize); + //remove garbage properties + List<NodeDocument> updated = delegate.findAndUpdate(updateOpList); + if (!updated.isEmpty()) { + //calculate the diff of the bson size after update + long afterUpdateBsonSize = getBsonSize(ids); + LOG.debug("bson size after update: {}", afterUpdateBsonSize); + if (initialBsonSize > 0 && afterUpdateBsonSize > 0) { + //sum up the removed bson size + addBsonSize(initialBsonSize - afterUpdateBsonSize); + } + } + return updated; + } + /** + * Remove the documents from the collection and sum the their bson size + * @param orphanOrDeletedRemovalMap the map of the documents to be removed + * @return the number of documents removed + */ + @Override + public int remove(Map<String, Long> orphanOrDeletedRemovalMap) { + //get the total bson size before the update + long bsonSize = getBsonSize(new ArrayList<>(orphanOrDeletedRemovalMap.keySet())); + LOG.debug("bson size before remove: {}", bsonSize); + //remove garbage documents + int removed = delegate.remove(orphanOrDeletedRemovalMap); + //sum up the removed bson size + if (removed > 0 && bsonSize > 0) { + addBsonSize(bsonSize); + } + return removed; + } + + private void addBsonSize(long bsonSize) { + if (bsonSize <= 0) { + LOG.warn("bson size {} is not positive", bsonSize); + return; + } + //sum the bson size with the value from fullGcBsonSize document in the SETTINGS collection + Bson query = eq(ID, "versionGC"); + Bson update = Updates.inc("fullGcRemovedTotalBsonSize", bsonSize); + //increment the value in SETTINGS collection with the new bson size + store.getDBCollection(SETTINGS).updateOne(query, update); + LOG.info("Incremented bson size with {}", bsonSize); + } + + /** + * Calculate the total bson size of documents in the list + * @param ids the list of ids to be iterated + * @return the total size of the bson + */ + long getBsonSize(List<String> ids) { Review Comment: done -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: oak-dev-unsubscr...@jackrabbit.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org