You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by "hungphan227 (via GitHub)" <gi...@apache.org> on 2023/08/30 08:22:08 UTC

[GitHub] [james-project] hungphan227 commented on a diff in pull request #1706: JAMES-3926 adopt cassandra quota helpers for mailbox quota limits

hungphan227 commented on code in PR #1706:
URL: https://github.com/apache/james-project/pull/1706#discussion_r1309882758


##########
mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/quota/CassandraPerUserMaxQuotaManagerMigration.java:
##########
@@ -0,0 +1,96 @@
+/****************************************************************
+ * 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.quota;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+
+import org.apache.james.backends.cassandra.migration.Migration;
+import org.apache.james.core.Domain;
+import org.apache.james.domainlist.api.DomainList;
+import org.apache.james.domainlist.api.DomainListException;
+import org.apache.james.mailbox.model.Quota;
+import org.apache.james.mailbox.quota.MaxQuotaManager;
+import org.apache.james.mailbox.quota.UserQuotaRootResolver;
+import org.apache.james.user.api.UsersRepository;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+public class CassandraPerUserMaxQuotaManagerMigration implements Migration {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(CassandraPerUserMaxQuotaManagerMigration.class);
+    private final UsersRepository usersRepository;
+    private final DomainList domainDao;
+    private final MaxQuotaManager oldMaxQuotaManager;
+    private final MaxQuotaManager newMaxQuotaManager;
+    private UserQuotaRootResolver userQuotaRootResolver;
+
+    @Inject
+    public CassandraPerUserMaxQuotaManagerMigration(UsersRepository usersRepository, DomainList domainDao,
+                                                    @Named("old") MaxQuotaManager oldMaxQuotaManager, MaxQuotaManager newMaxQuotaManager,
+                                                    UserQuotaRootResolver userQuotaRootResolver) {
+        this.usersRepository = usersRepository;
+        this.domainDao = domainDao;
+        this.oldMaxQuotaManager = oldMaxQuotaManager;
+        this.newMaxQuotaManager = newMaxQuotaManager;
+        this.userQuotaRootResolver = userQuotaRootResolver;
+    }
+
+    @Override
+    public void apply() throws InterruptedException {
+        Mono.from(oldMaxQuotaManager.getGlobalMaxMessageReactive()).flatMap(quotaCountLimit -> Mono.from(newMaxQuotaManager.setGlobalMaxMessageReactive(quotaCountLimit))).block();
+        Mono.from(oldMaxQuotaManager.getGlobalMaxStorageReactive()).flatMap(quotaSizeLimit -> Mono.from(newMaxQuotaManager.setGlobalMaxStorageReactive(quotaSizeLimit))).block();
+
+        Flux.fromIterable(getDomains())
+            .flatMap(domain -> Mono.from(oldMaxQuotaManager.getDomainMaxMessageReactive(domain))
+                .flatMap(quotaCountLimit -> Mono.from(newMaxQuotaManager.setDomainMaxMessageReactive(domain, quotaCountLimit)))
+                .then(Mono.from(oldMaxQuotaManager.getDomainMaxStorageReactive(domain))
+                    .flatMap(quotaSizeLimit -> Mono.from(newMaxQuotaManager.setDomainMaxStorageReactive(domain, quotaSizeLimit)))))
+            .then()
+            .block();
+
+        Flux.from(usersRepository.listReactive())
+            .map(userQuotaRootResolver::forUser)
+            .flatMap(quotaRoot -> Mono.from(oldMaxQuotaManager.listMaxMessagesDetailsReactive(quotaRoot))
+                .map(map -> map.get(Quota.Scope.User))
+                .flatMap(quotaCountLimit -> Mono.from(newMaxQuotaManager.setMaxMessageReactive(quotaRoot, quotaCountLimit)))
+                .then(Mono.from(oldMaxQuotaManager.listMaxStorageDetailsReactive(quotaRoot))
+                    .map(map -> map.get(Quota.Scope.User))
+                    .flatMap(quotaSizeLimit -> Mono.from(newMaxQuotaManager.setMaxStorageReactive(quotaRoot, quotaSizeLimit)))))
+            .then()
+            .block();
+    }
+
+    private List<Domain> getDomains() {
+        List<Domain> domains = new ArrayList<>();
+        try {
+            domains = domainDao.getDomains();
+        } catch (DomainListException ex) {
+            LOGGER.warn("Could not get domain list", ex);
+        }
+        return domains;
+    }

Review Comment:
   domainDao.getDomains() throw DomainListException. In case the exception occur, log an warning and then continue migration by returning empty list.



-- 
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: notifications-unsubscribe@james.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org