chibenwa commented on code in PR #2408:
URL: https://github.com/apache/james-project/pull/2408#discussion_r1756750616


##########
mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/task/SolveMessageDeletedInconsistenciesService.java:
##########
@@ -0,0 +1,120 @@
+/****************************************************************
+ * 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.mailbox.cassandra.mail.task;
+
+import java.time.Duration;
+import java.util.concurrent.ConcurrentLinkedDeque;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.stream.Collectors;
+
+import jakarta.inject.Inject;
+import jakarta.mail.Flags;
+
+import org.apache.james.mailbox.cassandra.ids.CassandraId;
+import org.apache.james.mailbox.cassandra.mail.CassandraDeletedMessageDAO;
+import org.apache.james.mailbox.cassandra.mail.CassandraMailboxDAO;
+import org.apache.james.mailbox.cassandra.mail.CassandraMessageIdDAO;
+import org.apache.james.mailbox.model.Mailbox;
+import org.apache.james.mailbox.model.MessageRange;
+import org.apache.james.task.Task;
+import org.apache.james.util.ReactorUtils;
+import org.apache.james.util.streams.Limit;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+
+import reactor.core.publisher.Mono;
+
+public class SolveMessageDeletedInconsistenciesService {
+
+    public record Context(AtomicLong processedMailboxEntries,
+                          ConcurrentLinkedDeque<CassandraId> errors) {
+
+        record Snapshot(long processedMailboxEntries, 
ImmutableList<CassandraId> errors) {
+        }
+
+        public Context() {
+            this(new AtomicLong(0), new ConcurrentLinkedDeque<>());
+        }
+
+        void incrementProcessedMailboxEntries() {
+            processedMailboxEntries.incrementAndGet();
+        }
+
+        void addError(CassandraId cassandraId) {
+            errors.add(cassandraId);
+        }
+
+        Snapshot snapshot() {
+            return new Snapshot(processedMailboxEntries.get(), 
ImmutableList.copyOf(errors));
+        }
+    }
+
+    public record RunningOptions(int messagesPerSecond) {
+        public static final RunningOptions DEFAULT = new RunningOptions(100);
+
+        public RunningOptions {
+            Preconditions.checkArgument(messagesPerSecond > 0, 
"'messagesPerSecond' must be strictly positive");
+        }
+    }
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(SolveMessageDeletedInconsistenciesService.class);
+    private static final Duration PERIOD = Duration.ofSeconds(1);
+    private final CassandraMessageIdDAO messageIdDAO;
+    private final CassandraDeletedMessageDAO deletedMessageDAO;
+    private final CassandraMailboxDAO mailboxDAO;
+
+    @Inject
+    public SolveMessageDeletedInconsistenciesService(CassandraMessageIdDAO 
messageIdDAO,
+                                                     
CassandraDeletedMessageDAO deletedMessageDAO,
+                                                     CassandraMailboxDAO 
mailboxDAO) {
+        this.messageIdDAO = messageIdDAO;
+        this.deletedMessageDAO = deletedMessageDAO;
+        this.mailboxDAO = mailboxDAO;
+    }
+
+    public Mono<Task.Result> fixInconsistencies(Context context, 
RunningOptions options) {
+        return mailboxDAO.retrieveAllMailboxes()
+            .transform(ReactorUtils.<Mailbox, Task.Result>throttle()
+                .elements(options.messagesPerSecond())

Review Comment:
   The code is currently throtling mailboxes which is not what the intent 
seemed to be (throttling messages)
   
   Also does the idea of applying a per-second count of processed messages 
makes sens? My feeling is that it might be overkill, and just a concatMap to 
process each mailbox sequentially as fast as possible?
   
   
   
   



-- 
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]

Reply via email to