You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by GitBox <gi...@apache.org> on 2022/03/17 09:27:02 UTC

[GitHub] [james-project] chibenwa commented on a change in pull request #921: JAMES-3726 Allow relaxing CassandraMailRepository LWT usage

chibenwa commented on a change in pull request #921:
URL: https://github.com/apache/james-project/pull/921#discussion_r828913852



##########
File path: server/mailrepository/mailrepository-cassandra/src/main/java/org/apache/james/mailrepository/cassandra/CassandraMailRepositoryKeysDAO.java
##########
@@ -28,56 +28,91 @@
 import static org.apache.james.mailrepository.cassandra.MailRepositoryTable.MAIL_KEY;
 import static org.apache.james.mailrepository.cassandra.MailRepositoryTable.REPOSITORY_NAME;
 
+import java.util.function.Predicate;
+
 import javax.inject.Inject;
 
+import org.apache.james.backends.cassandra.init.configuration.CassandraConfiguration;
 import org.apache.james.backends.cassandra.utils.CassandraAsyncExecutor;
 import org.apache.james.mailrepository.api.MailKey;
 import org.apache.james.mailrepository.api.MailRepositoryUrl;
+import org.apache.james.util.FunctionalUtils;
 
 import com.datastax.driver.core.PreparedStatement;
 import com.datastax.driver.core.Session;
+import com.datastax.driver.core.querybuilder.Delete;
+import com.datastax.driver.core.querybuilder.Insert;
 
 import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
 
 public class CassandraMailRepositoryKeysDAO {
 
     private final CassandraAsyncExecutor executor;
+    private final PreparedStatement selectKey;
     private final PreparedStatement insertKey;
     private final PreparedStatement deleteKey;
     private final PreparedStatement listKeys;
+    private final boolean strongConsistency;
 
     @Inject
-    public CassandraMailRepositoryKeysDAO(Session session) {
+    public CassandraMailRepositoryKeysDAO(Session session, CassandraConfiguration cassandraConfiguration) {
+        this.strongConsistency = cassandraConfiguration.isMailRepositoryStrongConsistency();
         this.executor = new CassandraAsyncExecutor(session);
 
+        this.selectKey = prepareGet(session);
         this.insertKey = prepareInsert(session);
         this.deleteKey = prepareDelete(session);
         this.listKeys = prepareList(session);
     }
 
+    private PreparedStatement prepareGet(Session session) {
+        return session.prepare(select(MAIL_KEY)
+            .from(KEYS_TABLE_NAME)
+            .where(eq(REPOSITORY_NAME, bindMarker(REPOSITORY_NAME)))
+            .and(eq(MAIL_KEY, bindMarker(MAIL_KEY))));
+    }
+
     private PreparedStatement prepareList(Session session) {
         return session.prepare(select(MAIL_KEY)
             .from(KEYS_TABLE_NAME)
             .where(eq(REPOSITORY_NAME, bindMarker(REPOSITORY_NAME))));
     }
 
     private PreparedStatement prepareDelete(Session session) {
-        return session.prepare(delete()
+        Delete.Where deleteStatement = delete()
             .from(KEYS_TABLE_NAME)
-            .ifExists()
             .where(eq(REPOSITORY_NAME, bindMarker(REPOSITORY_NAME)))
-            .and(eq(MAIL_KEY, bindMarker(MAIL_KEY))));
+            .and(eq(MAIL_KEY, bindMarker(MAIL_KEY)));
+
+        if (strongConsistency) {
+            return session.prepare(deleteStatement.ifExists());
+        }
+        return session.prepare(deleteStatement);
     }
 
     private PreparedStatement prepareInsert(Session session) {
-        return session.prepare(insertInto(KEYS_TABLE_NAME)
-            .ifNotExists()
+        Insert insertStatement = insertInto(KEYS_TABLE_NAME)
             .value(REPOSITORY_NAME, bindMarker(REPOSITORY_NAME))
-            .value(MAIL_KEY, bindMarker(MAIL_KEY)));
+            .value(MAIL_KEY, bindMarker(MAIL_KEY));
+
+        if (strongConsistency) {
+            return session.prepare(insertStatement.ifNotExists());
+        }
+        return session.prepare(insertStatement);
     }
 
     public Mono<Boolean> store(MailRepositoryUrl url, MailKey key) {
+        if (strongConsistency) {
+            return storeExecute(url, key);
+        }
+        return exists(url, key)
+            .filter(FunctionalUtils.identityPredicate())
+            .map(FunctionalUtils.negate())
+            .switchIfEmpty(storeExecute(url, key));

Review comment:
       Please let's not do read - before writes. I agree having a different behaviour than when we use the strong consistency mode.
   
   That is: always store!

##########
File path: server/mailrepository/mailrepository-cassandra/src/main/java/org/apache/james/mailrepository/cassandra/CassandraMailRepositoryKeysDAO.java
##########
@@ -90,8 +125,24 @@ private PreparedStatement prepareInsert(Session session) {
     }
 
     public Mono<Boolean> remove(MailRepositoryUrl url, MailKey key) {
+        if (strongConsistency) {
+            return removeExecute(url, key);
+        }
+        return exists(url, key)
+            .filter(Predicate.not(FunctionalUtils.identityPredicate()))
+            .switchIfEmpty(removeExecute(url, key));

Review comment:
       Idem always delete!




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