chibenwa commented on a change in pull request #618: URL: https://github.com/apache/james-project/pull/618#discussion_r694903186
########## File path: server/blob/blob-storage-strategy/src/main/java/org/apache/james/server/blob/deduplication/BloomFilterGCAlgorithm.java ########## @@ -0,0 +1,212 @@ +/**************************************************************** + * 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.james.server.blob.deduplication; + +import java.nio.charset.StandardCharsets; +import java.time.Clock; +import java.time.Instant; +import java.util.Objects; +import java.util.UUID; +import java.util.concurrent.atomic.AtomicLong; + +import org.apache.james.blob.api.BlobId; +import org.apache.james.blob.api.BlobReferenceSource; +import org.apache.james.blob.api.BlobStoreDAO; +import org.apache.james.blob.api.BucketName; +import org.apache.james.task.Task; +import org.apache.james.task.Task.Result; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.MoreObjects; +import com.google.common.hash.BloomFilter; +import com.google.common.hash.Funnel; +import com.google.common.hash.Funnels; + +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +public class BloomFilterGCAlgorithm { + + private static final Logger LOGGER = LoggerFactory.getLogger(BloomFilterGCAlgorithm.class); + private static final Funnel<CharSequence> BLOOM_FILTER_FUNNEL = Funnels.stringFunnel(StandardCharsets.US_ASCII); + + public static class Context { + + public static class Snapshot { + private final long referenceSourceCount; + private final long blobCount; + private final long gcedBlobCount; + private final long errorCount; + private final long bloomFilterExpectedBlobCount; + private final double bloomFilterAssociatedProbability; + + public Snapshot(long referenceSourceCount, + long blobCount, + long gcedBlobCount, + long errorCount, + long bloomFilterExpectedBlobCount, + double bloomFilterAssociatedProbability) { + this.referenceSourceCount = referenceSourceCount; + this.blobCount = blobCount; + this.gcedBlobCount = gcedBlobCount; + this.errorCount = errorCount; + this.bloomFilterExpectedBlobCount = bloomFilterExpectedBlobCount; + this.bloomFilterAssociatedProbability = bloomFilterAssociatedProbability; + } + + @Override + public final boolean equals(Object o) { + if (o instanceof Snapshot) { + Snapshot that = (Snapshot) o; + + return Objects.equals(this.referenceSourceCount, that.referenceSourceCount) + && Objects.equals(this.blobCount, that.blobCount) + && Objects.equals(this.gcedBlobCount, that.gcedBlobCount) + && Objects.equals(this.errorCount, that.errorCount) + && Objects.equals(this.bloomFilterExpectedBlobCount, that.bloomFilterExpectedBlobCount) + && Objects.equals(this.bloomFilterAssociatedProbability, that.bloomFilterAssociatedProbability); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(referenceSourceCount, blobCount, gcedBlobCount, errorCount, bloomFilterExpectedBlobCount, bloomFilterAssociatedProbability); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("referenceSourceCount", referenceSourceCount) + .add("blobCount", blobCount) + .add("gcedBlobCount", gcedBlobCount) + .add("errorCount", errorCount) + .add("bloomFilterExpectedBlobCount", bloomFilterExpectedBlobCount) + .add("bloomFilterAssociatedProbability", bloomFilterAssociatedProbability) + .toString(); + } + } + + private final AtomicLong referenceSourceCount; + private final AtomicLong blobCount; + private final AtomicLong gcedBlobCount; + private final AtomicLong errorCount; + private final Long bloomFilterExpectedBlobCount; + private final Double bloomFilterAssociatedProbability; + + public Context(long bloomFilterExpectedBlobCount, double bloomFilterAssociatedProbability) { + this.referenceSourceCount = new AtomicLong(); + this.blobCount = new AtomicLong(); + this.gcedBlobCount = new AtomicLong(); + this.errorCount = new AtomicLong(); + this.bloomFilterExpectedBlobCount = bloomFilterExpectedBlobCount; + this.bloomFilterAssociatedProbability = bloomFilterAssociatedProbability; + } + + public void incrementBlobCount() { + blobCount.incrementAndGet(); + } + + public void incrementReferenceSourceCount() { + referenceSourceCount.incrementAndGet(); + } + + public void incrementGCedBlobCount() { + gcedBlobCount.incrementAndGet(); + } + + public void incrementErrorCount() { + errorCount.incrementAndGet(); + } + + Snapshot snapshot() { + return new Snapshot( + referenceSourceCount.get(), + blobCount.get(), + gcedBlobCount.get(), + errorCount.get(), + bloomFilterExpectedBlobCount, + bloomFilterAssociatedProbability); + } + } + + private final BlobReferenceSource referenceSource; + private final BlobStoreDAO blobStoreDAO; + private final GenerationAwareBlobId.Factory generationAwareBlobIdFactory; + private final GenerationAwareBlobId.Configuration generationAwareBlobIdConfiguration; + private final Instant now; + + // Avoids two subsequent run to have the same false positives. + private final String salt; + + public BloomFilterGCAlgorithm(BlobReferenceSource referenceSource, + BlobStoreDAO blobStoreDAO, + GenerationAwareBlobId.Factory generationAwareBlobIdFactory, + GenerationAwareBlobId.Configuration generationAwareBlobIdConfiguration, + Clock clock) { + this.referenceSource = referenceSource; + this.blobStoreDAO = blobStoreDAO; + this.generationAwareBlobIdFactory = generationAwareBlobIdFactory; + this.generationAwareBlobIdConfiguration = generationAwareBlobIdConfiguration; + this.salt = UUID.randomUUID().toString(); + this.now = clock.instant(); + } + + public Mono<Result> gc(int expectedBlobCount, double associatedProbability, BucketName bucketName, Context context) { + return populatedBloomFilter(expectedBlobCount, associatedProbability, context) + .flatMap(bloomFilter -> gc(bloomFilter, bucketName, context)); + } + + public Mono<Result> gc(BloomFilter<CharSequence> bloomFilter, BucketName bucketName, Context context) { + return Flux.from(blobStoreDAO.listBlobs(bucketName)) + .doOnNext(blobId -> context.incrementBlobCount()) + .flatMap(blobId -> gcBlob(bloomFilter, blobId, bucketName, context)) + .reduce(Task::combine); + } + + public Mono<BloomFilter<CharSequence>> populatedBloomFilter(int expectedBlobCount, double associatedProbability, Context context) { Review comment: For now we should just run the GC on the default bucket. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
