james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adup...@apache.org
Subject [39/41] james-project git commit: JAMES-2083 Cassandra version manager should indicate schema state
Date Mon, 10 Jul 2017 17:54:51 GMT
JAMES-2083 Cassandra version manager should indicate schema state


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/e5e54b76
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/e5e54b76
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/e5e54b76

Branch: refs/heads/master
Commit: e5e54b762d02a0220375f2eea53e05507a12542a
Parents: 2474f1b
Author: benwa <btellier@linagora.com>
Authored: Wed Jul 5 08:54:52 2017 +0700
Committer: Antoine Duprat <aduprat@linagora.com>
Committed: Mon Jul 10 14:24:00 2017 +0200

----------------------------------------------------------------------
 .../versions/CassandraSchemaVersionManager.java | 52 ++++++++-------
 .../CassandraSchemaVersionManagerTest.java      | 70 ++++++--------------
 .../modules/mailbox/CassandraSessionModule.java | 24 ++++++-
 3 files changed, 72 insertions(+), 74 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/e5e54b76/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionManager.java
----------------------------------------------------------------------
diff --git a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionManager.java
b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionManager.java
index 84b25e5..2a9266e 100644
--- a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionManager.java
+++ b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionManager.java
@@ -19,13 +19,18 @@
 
 package org.apache.james.backends.cassandra.versions;
 
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Preconditions;
+import static org.apache.james.backends.cassandra.versions.CassandraSchemaVersionManager.SchemaState.TOO_OLD;
+import static org.apache.james.backends.cassandra.versions.CassandraSchemaVersionManager.SchemaState.TOO_RECENT;
+import static org.apache.james.backends.cassandra.versions.CassandraSchemaVersionManager.SchemaState.UPGRADABLE;
+import static org.apache.james.backends.cassandra.versions.CassandraSchemaVersionManager.SchemaState.UP_TO_DATE;
+
+import javax.inject.Inject;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import javax.inject.Inject;
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
 
 public class CassandraSchemaVersionManager {
     public static final int MIN_VERSION = 1;
@@ -36,7 +41,14 @@ public class CassandraSchemaVersionManager {
 
     private final int minVersion;
     private final int maxVersion;
-    private final int version;
+    private final CassandraSchemaVersionDAO schemaVersionDAO;
+
+    public enum SchemaState {
+        UP_TO_DATE,
+        TOO_RECENT,
+        TOO_OLD,
+        UPGRADABLE
+    }
 
     @Inject
     public CassandraSchemaVersionManager(CassandraSchemaVersionDAO schemaVersionDAO) {
@@ -50,9 +62,13 @@ public class CassandraSchemaVersionManager {
         Preconditions.checkArgument(maxVersion >= minVersion,
             "maxVersion should not be inferior to minVersion");
 
+        this.schemaVersionDAO = schemaVersionDAO;
         this.minVersion = minVersion;
         this.maxVersion = maxVersion;
-        this.version = schemaVersionDAO
+    }
+
+    public int computeVersion() {
+        return schemaVersionDAO
             .getCurrentSchemaVersion()
             .join()
             .orElseGet(() -> {
@@ -62,10 +78,6 @@ public class CassandraSchemaVersionManager {
             });
     }
 
-    public int getVersion() {
-        return version;
-    }
-
     public int getMinimumSupportedVersion() {
         return minVersion;
     }
@@ -74,24 +86,16 @@ public class CassandraSchemaVersionManager {
         return maxVersion;
     }
 
-    public void ensureSchemaIsSupported() {
+    public SchemaState computeSchemaState() {
+        int version = computeVersion();
         if (version < minVersion) {
-
-            throw new IllegalStateException(
-                String.format("Current schema version is %d whereas minimum required version
is %d. " +
-                    "Recommended version is %d", version, minVersion, maxVersion));
+            return TOO_OLD;
         } else if (version < maxVersion) {
-
-            LOGGER.warn("Current schema version is %d. Recommended version is %d", version,
maxVersion);
-        } else if (version == maxVersion){
-
-            LOGGER.info("Schema version is up-to-date");
+            return UPGRADABLE;
+        } else if (version == maxVersion) {
+            return UP_TO_DATE;
         } else {
-
-            throw new IllegalStateException(
-                String.format("Current schema version is %d whereas the minimum supported
version is %d. " +
-                    "Recommended version is %d.", version, minVersion, maxVersion));
+            return TOO_RECENT;
         }
     }
-
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/e5e54b76/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionManagerTest.java
----------------------------------------------------------------------
diff --git a/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionManagerTest.java
b/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionManagerTest.java
index d6576dc..708c492 100644
--- a/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionManagerTest.java
+++ b/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionManagerTest.java
@@ -19,30 +19,28 @@
 
 package org.apache.james.backends.cassandra.versions;
 
+import static org.apache.james.backends.cassandra.versions.CassandraSchemaVersionManager.SchemaState.UP_TO_DATE;
+import static org.apache.james.backends.cassandra.versions.CassandraSchemaVersionManager.SchemaState.UPGRADABLE;
+import static org.apache.james.backends.cassandra.versions.CassandraSchemaVersionManager.SchemaState.TOO_OLD;
+import static org.apache.james.backends.cassandra.versions.CassandraSchemaVersionManager.SchemaState.TOO_RECENT;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
-import static uk.org.lidalia.slf4jtest.LoggingEvent.info;
-import static uk.org.lidalia.slf4jtest.LoggingEvent.warn;
 
 import java.util.Optional;
 import java.util.concurrent.CompletableFuture;
 
-import org.junit.After;
+import org.apache.james.backends.cassandra.versions.CassandraSchemaVersionManager.SchemaState;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 
-import uk.org.lidalia.slf4jtest.TestLogger;
-import uk.org.lidalia.slf4jtest.TestLoggerFactory;
-
 public class CassandraSchemaVersionManagerTest {
 
     private final int minVersion = 2;
     private final int maxVersion = 4;
     private CassandraSchemaVersionDAO schemaVersionDAO;
-    private TestLogger logger = TestLoggerFactory.getTestLogger(CassandraSchemaVersionManager.class);
 
     @Rule
     public ExpectedException expectedException = ExpectedException.none();
@@ -52,15 +50,8 @@ public class CassandraSchemaVersionManagerTest {
         schemaVersionDAO = mock(CassandraSchemaVersionDAO.class);
     }
 
-    @After
-    public void tearDown() {
-        TestLoggerFactory.clear();
-    }
-
     @Test
-    public void ensureSchemaIsSupportedShouldThrowIfSchemaVersionIsTooOld() {
-        expectedException.expect(IllegalStateException.class);
-
+    public void computeSchemaStateShouldReturnTooOldWhenVersionIsLessThanMinVersion() {
         int currentVersion = minVersion - 1;
 
         when(schemaVersionDAO.getCurrentSchemaVersion())
@@ -71,13 +62,11 @@ public class CassandraSchemaVersionManagerTest {
             minVersion,
             maxVersion);
 
-        testee.ensureSchemaIsSupported();
+        assertThat(testee.computeSchemaState()).isEqualTo(TOO_OLD);
     }
 
     @Test
-    public void ensureSchemaIsSupportedShouldThrowIfSchemaVersionIsTooRecent() {
-        expectedException.expect(IllegalStateException.class);
-
+    public void computeSchemaStateShouldReturnTooOldWhenVersionIsMoreThanMaxVersion() {
         int currentVersion = maxVersion + 1;
 
         when(schemaVersionDAO.getCurrentSchemaVersion())
@@ -88,11 +77,11 @@ public class CassandraSchemaVersionManagerTest {
             minVersion,
             maxVersion);
 
-        testee.ensureSchemaIsSupported();
+        assertThat(testee.computeSchemaState()).isEqualTo(TOO_RECENT);
     }
 
     @Test
-    public void ensureSchemaIsSupportedShouldLogOkIfSchemaIsUpToDate() {
+    public void computeSchemaStateShouldReturnUpToDateWhenVersionEqualsMaxVersion() {
         int currentVersion = maxVersion;
 
         when(schemaVersionDAO.getCurrentSchemaVersion())
@@ -103,13 +92,11 @@ public class CassandraSchemaVersionManagerTest {
             minVersion,
             maxVersion);
 
-        testee.ensureSchemaIsSupported();
-
-        assertThat(logger.getLoggingEvents()).containsExactly(info("Schema version is up-to-date"));
+        assertThat(testee.computeSchemaState()).isEqualTo(UP_TO_DATE);
     }
 
     @Test
-    public void ensureSchemaIsSupportedShouldLogAsWarningIfSchemaIsSupportedButNotUpToDate()
{
+    public void computeSchemaStateShouldReturnUpgradableWhenVersionBetweenMinAnd() {
         int currentVersion = maxVersion - 1;
 
         when(schemaVersionDAO.getCurrentSchemaVersion())
@@ -120,12 +107,7 @@ public class CassandraSchemaVersionManagerTest {
             minVersion,
             maxVersion);
 
-        testee.ensureSchemaIsSupported();
-
-        assertThat(logger.getLoggingEvents())
-            .containsExactly(warn("Current schema version is %d. Recommended version is %d",
-                currentVersion,
-                maxVersion));
+        assertThat(testee.computeSchemaState()).isEqualTo(UPGRADABLE);
     }
 
     @Test
@@ -181,7 +163,7 @@ public class CassandraSchemaVersionManagerTest {
     }
 
     @Test
-    public void ensureSchemaIsSupportedShouldActAsUpToDateWhenMinMaxAndCurrentVersionsAreTheSame()
{
+    public void computeSchemaStateShouldReturnUpToDateWhenMinMaxAndVersionEquals() {
         int minVersion = 4;
         int maxVersion = 4;
         int currentVersion = 4;
@@ -193,29 +175,19 @@ public class CassandraSchemaVersionManagerTest {
             minVersion,
             maxVersion);
 
-        testee.ensureSchemaIsSupported();
-
-        assertThat(logger.getLoggingEvents()).containsExactly(info("Schema version is up-to-date"));
+        assertThat(testee.computeSchemaState()).isEqualTo(UP_TO_DATE);
     }
 
     @Test
-    public void ensureSchemaIsSupportedShouldNotThrowOnNewCassandra() {
+    public void defaultComputedSchemaShouldNotBeTooOldNeitherTooRecent() {
         when(schemaVersionDAO.getCurrentSchemaVersion())
             .thenReturn(CompletableFuture.completedFuture(Optional.of(CassandraSchemaVersionManager.DEFAULT_VERSION)));
 
-        new CassandraSchemaVersionManager(schemaVersionDAO).ensureSchemaIsSupported();
-    }
-
-    @Test
-    public void ensureSchemaIsSupportedShouldNotThrowButLogWhenNoVersionNumberFoundOnCassandra()
{
-        when(schemaVersionDAO.getCurrentSchemaVersion())
-            .thenReturn(CompletableFuture.completedFuture(Optional.empty()));
-
-        new CassandraSchemaVersionManager(schemaVersionDAO).ensureSchemaIsSupported();
+        SchemaState schemaState = new CassandraSchemaVersionManager(schemaVersionDAO).computeSchemaState();
 
-        assertThat(logger.getAllLoggingEvents())
-            .contains(warn("No schema version information found on Cassandra, we assume schema
is at version {}",
-                CassandraSchemaVersionManager.DEFAULT_VERSION));
+        assertThat(schemaState)
+            .isNotEqualTo(TOO_RECENT)
+            .isNotEqualTo(TOO_OLD);
     }
 
     @Test
@@ -235,6 +207,6 @@ public class CassandraSchemaVersionManagerTest {
 
         CassandraSchemaVersionManager testee = new CassandraSchemaVersionManager(schemaVersionDAO);
 
-        assertThat(testee.getMinimumSupportedVersion()).isEqualTo(CassandraSchemaVersionManager.MAX_VERSION);
+        assertThat(testee.getMaximumSupportedVersion()).isEqualTo(CassandraSchemaVersionManager.MAX_VERSION);
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/e5e54b76/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/CassandraSessionModule.java
----------------------------------------------------------------------
diff --git a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/CassandraSessionModule.java
b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/CassandraSessionModule.java
index 8459560..12dc844 100644
--- a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/CassandraSessionModule.java
+++ b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/CassandraSessionModule.java
@@ -41,6 +41,7 @@ import org.apache.james.backends.cassandra.init.SessionWithInitializedTablesFact
 import org.apache.james.backends.cassandra.utils.CassandraUtils;
 import org.apache.james.backends.cassandra.versions.CassandraSchemaVersionDAO;
 import org.apache.james.backends.cassandra.versions.CassandraSchemaVersionManager;
+import org.apache.james.backends.cassandra.versions.CassandraSchemaVersionManager.SchemaState;
 import org.apache.james.backends.cassandra.versions.CassandraSchemaVersionModule;
 import org.apache.james.lifecycle.api.Configurable;
 import org.apache.james.mailbox.store.BatchSizes;
@@ -326,7 +327,28 @@ public class CassandraSessionModule extends AbstractModule {
 
         @Override
         public void initModule() {
-            versionManager.ensureSchemaIsSupported();
+            SchemaState schemaState = versionManager.computeSchemaState();
+            switch (schemaState) {
+                case TOO_OLD:
+                    throw new IllegalStateException(
+                        String.format("Current schema version is %d whereas minimum required
version is %d. " +
+                            "Recommended version is %d", versionManager.computeVersion(),
versionManager.getMinimumSupportedVersion(),
+                            versionManager.getMaximumSupportedVersion()));
+                case TOO_RECENT:
+                    throw new IllegalStateException(
+                        String.format("Current schema version is %d whereas the minimum supported
version is %d. " +
+                            "Recommended version is %d.", versionManager.computeVersion(),
versionManager.getMinimumSupportedVersion(),
+                            versionManager.getMaximumSupportedVersion()));
+                case UP_TO_DATE:
+                    LOGGER.info("Schema version is up-to-date");
+                    return;
+                case UPGRADABLE:
+                    LOGGER.warn("Current schema version is {}. Recommended version is {}",
versionManager.computeVersion(),
+                        versionManager.getMaximumSupportedVersion());
+                    return;
+                default:
+                    throw new IllegalStateException("Unknown schema state " + schemaState);
+            }
         }
 
         @Override


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