james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [james-project] branch master updated: JAMES-2649 Add additonal infos to CassandraMappingsSolveInconsistenciesTask
Date Thu, 14 Feb 2019 10:23:56 GMT
This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git


The following commit(s) were added to refs/heads/master by this push:
     new 9c836c5  JAMES-2649 Add additonal infos to CassandraMappingsSolveInconsistenciesTask
9c836c5 is described below

commit 9c836c5a4b19df16ded0193995e9a081e0ce63c2
Author: Rene Cordier <rcordier@linagora.com>
AuthorDate: Fri Feb 1 11:57:55 2019 +0700

    JAMES-2649 Add additonal infos to CassandraMappingsSolveInconsistenciesTask
---
 .../migration/MappingsSourcesMigration.java        | 57 ++++++++++++++++++++--
 .../migration/MappingsSourcesMigrationTest.java    | 33 ++++++++++++-
 .../CassandraMappingsSolveInconsistenciesTask.java |  8 +++
 .../routes/CassandraMappingsRoutesTest.java        | 10 +++-
 4 files changed, 102 insertions(+), 6 deletions(-)

diff --git a/server/data/data-cassandra/src/main/java/org/apache/james/rrt/cassandra/migration/MappingsSourcesMigration.java
b/server/data/data-cassandra/src/main/java/org/apache/james/rrt/cassandra/migration/MappingsSourcesMigration.java
index 1e612bc..9d3de07 100644
--- a/server/data/data-cassandra/src/main/java/org/apache/james/rrt/cassandra/migration/MappingsSourcesMigration.java
+++ b/server/data/data-cassandra/src/main/java/org/apache/james/rrt/cassandra/migration/MappingsSourcesMigration.java
@@ -19,6 +19,9 @@
 
 package org.apache.james.rrt.cassandra.migration;
 
+import java.util.Optional;
+import java.util.concurrent.atomic.AtomicLong;
+
 import javax.inject.Inject;
 
 import org.apache.commons.lang3.tuple.Pair;
@@ -28,6 +31,7 @@ import org.apache.james.rrt.cassandra.CassandraRecipientRewriteTableDAO;
 import org.apache.james.rrt.lib.Mapping;
 import org.apache.james.rrt.lib.MappingSource;
 import org.apache.james.task.Task;
+import org.apache.james.task.TaskExecutionDetails;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -35,14 +39,38 @@ import reactor.core.publisher.Mono;
 
 public class MappingsSourcesMigration implements Migration {
     private static final Logger LOGGER = LoggerFactory.getLogger(MappingsSourcesMigration.class);
+    private static final String TYPE = "mappingsSourcesMigration";
+
+    public static class AdditionalInformation implements TaskExecutionDetails.AdditionalInformation
{
+        private final long successfulMappingsCount;
+        private final long errorMappingsCount;
+
+        AdditionalInformation(long successfulMappingsCount, long errorMappingsCount) {
+            this.successfulMappingsCount = successfulMappingsCount;
+            this.errorMappingsCount = errorMappingsCount;
+        }
+
+        public long getSuccessfulMappingsCount() {
+            return successfulMappingsCount;
+        }
+
+        public long getErrorMappingsCount() {
+            return errorMappingsCount;
+        }
+    }
+
     private final CassandraRecipientRewriteTableDAO cassandraRecipientRewriteTableDAO;
     private final CassandraMappingsSourcesDAO cassandraMappingsSourcesDAO;
+    private final AtomicLong successfulMappingsCount;
+    private final AtomicLong errorMappingsCount;
 
     @Inject
     public MappingsSourcesMigration(CassandraRecipientRewriteTableDAO cassandraRecipientRewriteTableDAO,
                                     CassandraMappingsSourcesDAO cassandraMappingsSourcesDAO)
{
         this.cassandraRecipientRewriteTableDAO = cassandraRecipientRewriteTableDAO;
         this.cassandraMappingsSourcesDAO = cassandraMappingsSourcesDAO;
+        this.successfulMappingsCount = new AtomicLong(0);
+        this.errorMappingsCount = new AtomicLong(0);
     }
 
     @Override
@@ -57,8 +85,31 @@ public class MappingsSourcesMigration implements Migration {
 
     private Mono<Result> migrate(Pair<MappingSource, Mapping> mappingEntry) {
         return cassandraMappingsSourcesDAO.addMapping(mappingEntry.getRight(), mappingEntry.getLeft())
-            .map(any -> Result.COMPLETED)
-            .doOnError(e -> LOGGER.error("Error while performing migration of mappings
sources", e))
-            .onErrorResume(e -> Mono.just(Result.PARTIAL));
+            .then(Mono.fromCallable(() -> {
+                successfulMappingsCount.incrementAndGet();
+                return Result.COMPLETED;
+            }))
+            .onErrorResume(e -> {
+                LOGGER.error("Error while performing migration of mapping source: {} with
mapping: {}",
+                    mappingEntry.getLeft().asString(), mappingEntry.getRight().asString(),
e);
+                errorMappingsCount.incrementAndGet();
+                return Mono.just(Result.PARTIAL);
+            });
+    }
+
+    @Override
+    public String type() {
+        return TYPE;
+    }
+
+    @Override
+    public Optional<TaskExecutionDetails.AdditionalInformation> details() {
+        return Optional.of(createAdditionalInformation());
+    }
+
+    AdditionalInformation createAdditionalInformation() {
+        return new AdditionalInformation(
+            successfulMappingsCount.get(),
+            errorMappingsCount.get());
     }
 }
diff --git a/server/data/data-cassandra/src/test/java/org/apache/james/rrt/cassandra/migration/MappingsSourcesMigrationTest.java
b/server/data/data-cassandra/src/test/java/org/apache/james/rrt/cassandra/migration/MappingsSourcesMigrationTest.java
index d2c1c69..64d066d 100644
--- a/server/data/data-cassandra/src/test/java/org/apache/james/rrt/cassandra/migration/MappingsSourcesMigrationTest.java
+++ b/server/data/data-cassandra/src/test/java/org/apache/james/rrt/cassandra/migration/MappingsSourcesMigrationTest.java
@@ -46,6 +46,7 @@ import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.RegisterExtension;
 
 import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
 
 class MappingsSourcesMigrationTest {
     private static final int THREAD_COUNT = 10;
@@ -93,6 +94,9 @@ class MappingsSourcesMigrationTest {
 
         assertThat(cassandraMappingsSourcesDAO.retrieveSources(MAPPING).collectList().block())
             .containsExactly(SOURCE);
+
+        assertThat(migration.createAdditionalInformation().getSuccessfulMappingsCount()).isEqualTo(1);
+        assertThat(migration.createAdditionalInformation().getErrorMappingsCount()).isEqualTo(0);
     }
 
     @Test
@@ -106,6 +110,9 @@ class MappingsSourcesMigrationTest {
 
         assertThat(cassandraMappingsSourcesDAO.retrieveSources(MAPPING).collectList().block())
             .containsOnly(SOURCE, source2);
+
+        assertThat(migration.createAdditionalInformation().getSuccessfulMappingsCount()).isEqualTo(2);
+        assertThat(migration.createAdditionalInformation().getErrorMappingsCount()).isEqualTo(0);
     }
 
     @Test
@@ -117,6 +124,8 @@ class MappingsSourcesMigrationTest {
         when(cassandraRecipientRewriteTableDAO.getAllMappings()).thenReturn(Flux.error(new
RuntimeException()));
 
         assertThat(migration.run()).isEqualTo(Migration.Result.PARTIAL);
+        assertThat(migration.createAdditionalInformation().getSuccessfulMappingsCount()).isEqualTo(0);
+        assertThat(migration.createAdditionalInformation().getErrorMappingsCount()).isEqualTo(0);
     }
 
     @Test
@@ -128,9 +137,31 @@ class MappingsSourcesMigrationTest {
         when(cassandraRecipientRewriteTableDAO.getAllMappings())
             .thenReturn(Flux.just(Pair.of(SOURCE, MAPPING)));
         when(cassandraMappingsSourcesDAO.addMapping(any(Mapping.class), any(MappingSource.class)))
-            .thenThrow(new RuntimeException());
+            .thenReturn(Mono.error(new RuntimeException()));
+
+        assertThat(migration.run()).isEqualTo(Migration.Result.PARTIAL);
+        assertThat(migration.createAdditionalInformation().getSuccessfulMappingsCount()).isEqualTo(0);
+        assertThat(migration.createAdditionalInformation().getErrorMappingsCount()).isEqualTo(1);
+    }
+
+    @Test
+    void migrationShouldHaveCorrectErrorCountWhenMultipleAddMappingFails() {
+        MappingSource source2 = MappingSource.fromUser("bob", Domain.LOCALHOST);
+
+        CassandraRecipientRewriteTableDAO cassandraRecipientRewriteTableDAO = mock(CassandraRecipientRewriteTableDAO.class);
+        CassandraMappingsSourcesDAO cassandraMappingsSourcesDAO = mock(CassandraMappingsSourcesDAO.class);
+        migration = new MappingsSourcesMigration(cassandraRecipientRewriteTableDAO, cassandraMappingsSourcesDAO);
+
+        when(cassandraRecipientRewriteTableDAO.getAllMappings())
+            .thenReturn(Flux.just(
+                Pair.of(SOURCE, MAPPING),
+                Pair.of(source2, MAPPING)));
+        when(cassandraMappingsSourcesDAO.addMapping(any(Mapping.class), any(MappingSource.class)))
+            .thenReturn(Mono.error(new RuntimeException()));
 
         assertThat(migration.run()).isEqualTo(Migration.Result.PARTIAL);
+        assertThat(migration.createAdditionalInformation().getSuccessfulMappingsCount()).isEqualTo(0);
+        assertThat(migration.createAdditionalInformation().getErrorMappingsCount()).isEqualTo(2);
     }
 
     @Test
diff --git a/server/protocols/webadmin/webadmin-cassandra-data/src/main/java/org/apache/james/webadmin/service/CassandraMappingsSolveInconsistenciesTask.java
b/server/protocols/webadmin/webadmin-cassandra-data/src/main/java/org/apache/james/webadmin/service/CassandraMappingsSolveInconsistenciesTask.java
index 14a1c47..d487f34 100644
--- a/server/protocols/webadmin/webadmin-cassandra-data/src/main/java/org/apache/james/webadmin/service/CassandraMappingsSolveInconsistenciesTask.java
+++ b/server/protocols/webadmin/webadmin-cassandra-data/src/main/java/org/apache/james/webadmin/service/CassandraMappingsSolveInconsistenciesTask.java
@@ -19,11 +19,14 @@
 
 package org.apache.james.webadmin.service;
 
+import java.util.Optional;
+
 import javax.inject.Inject;
 
 import org.apache.james.rrt.cassandra.CassandraMappingsSourcesDAO;
 import org.apache.james.rrt.cassandra.migration.MappingsSourcesMigration;
 import org.apache.james.task.Task;
+import org.apache.james.task.TaskExecutionDetails;
 
 import reactor.core.publisher.Mono;
 
@@ -53,4 +56,9 @@ public class CassandraMappingsSolveInconsistenciesTask implements Task {
     public String type() {
         return TYPE;
     }
+
+    @Override
+    public Optional<TaskExecutionDetails.AdditionalInformation> details() {
+        return mappingsSourcesMigration.details();
+    }
 }
diff --git a/server/protocols/webadmin/webadmin-cassandra-data/src/test/java/org/apache/james/webadmin/routes/CassandraMappingsRoutesTest.java
b/server/protocols/webadmin/webadmin-cassandra-data/src/test/java/org/apache/james/webadmin/routes/CassandraMappingsRoutesTest.java
index fedea9c..235ea27 100644
--- a/server/protocols/webadmin/webadmin-cassandra-data/src/test/java/org/apache/james/webadmin/routes/CassandraMappingsRoutesTest.java
+++ b/server/protocols/webadmin/webadmin-cassandra-data/src/test/java/org/apache/james/webadmin/routes/CassandraMappingsRoutesTest.java
@@ -126,6 +126,8 @@ class CassandraMappingsRoutesTest {
         .then()
             .body("status", is("completed"))
             .body("taskId", is(taskId))
+            .body("additionalInformation.successfulMappingsCount", is(0))
+            .body("additionalInformation.errorMappingsCount", is(0))
             .body("type", is(CassandraMappingsSolveInconsistenciesTask.TYPE))
             .body("startedDate", is(notNullValue()))
             .body("submitDate", is(notNullValue()))
@@ -176,7 +178,9 @@ class CassandraMappingsRoutesTest {
         .when()
             .get(taskId + "/await")
         .then()
-            .body("status", is("completed"));
+            .body("status", is("completed"))
+            .body("additionalInformation.successfulMappingsCount", is(2))
+            .body("additionalInformation.errorMappingsCount", is(0));
 
         assertThat(cassandraMappingsSourcesDAO.retrieveSources(MAPPING).collectList().block())
             .containsOnly(SOURCE_1, SOURCE_2);
@@ -200,7 +204,9 @@ class CassandraMappingsRoutesTest {
         .when()
             .get(taskId + "/await")
         .then()
-            .body("status", is("completed"));
+            .body("status", is("completed"))
+            .body("additionalInformation.successfulMappingsCount", is(1))
+            .body("additionalInformation.errorMappingsCount", is(0));
 
         assertThat(cassandraMappingsSourcesDAO.retrieveSources(MAPPING).collectList().block())
             .containsOnly(SOURCE_1);


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


Mime
View raw message