This is an automated email from the ASF dual-hosted git repository. rcordier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 1403bdbdb2065c97ac26cbe751a389ab885ee3f2 Author: Gautier DI FOLCO <[email protected]> AuthorDate: Tue Jul 28 10:37:01 2020 +0200 JAMES-3343 Extract CassandraMailQueueView initialization into a StartUpCheck --- .../modules/blobstore/BlobStoreModulesChooser.java | 2 +- .../james/modules/rabbitmq/RabbitMQModule.java | 3 + .../view/cassandra/CassandraMailQueueView.java | 8 +-- .../CassandraMailQueueViewStartUpCheck.java | 64 ++++++++++++++++++++ .../RabbitMQMailQueueConfigurationChangeTest.java | 68 +++++++++++++--------- .../queue/rabbitmq/RabbitMQMailQueueTest.java | 1 - .../CassandraMailQueueViewTestFactory.java | 18 +----- 7 files changed, 112 insertions(+), 52 deletions(-) diff --git a/server/container/guice/cassandra-rabbitmq-guice/src/main/java/org/apache/james/modules/blobstore/BlobStoreModulesChooser.java b/server/container/guice/cassandra-rabbitmq-guice/src/main/java/org/apache/james/modules/blobstore/BlobStoreModulesChooser.java index 20bdf8f..b37e0ab 100644 --- a/server/container/guice/cassandra-rabbitmq-guice/src/main/java/org/apache/james/modules/blobstore/BlobStoreModulesChooser.java +++ b/server/container/guice/cassandra-rabbitmq-guice/src/main/java/org/apache/james/modules/blobstore/BlobStoreModulesChooser.java @@ -93,7 +93,7 @@ public class BlobStoreModulesChooser { public static List<Module> chooseModules(BlobStoreConfiguration choosingConfiguration) { ImmutableList.Builder<Module> moduleBuilder = ImmutableList.<Module>builder() .add(chooseDumBlobStoreModule(choosingConfiguration.getImplementation())) - .add( new StoragePolicyConfigurationSanityEnforcementModule(choosingConfiguration)); + .add(new StoragePolicyConfigurationSanityEnforcementModule(choosingConfiguration)); //TODO JAMES-3028 add the storage policy module for all implementation and unbind the ObjectStorageBlobStore if (choosingConfiguration.getImplementation() == BlobStoreConfiguration.BlobStoreImplName.CASSANDRA) { diff --git a/server/container/guice/rabbitmq/src/main/java/org/apache/james/modules/rabbitmq/RabbitMQModule.java b/server/container/guice/rabbitmq/src/main/java/org/apache/james/modules/rabbitmq/RabbitMQModule.java index f4d1c9f..b21be1d 100644 --- a/server/container/guice/rabbitmq/src/main/java/org/apache/james/modules/rabbitmq/RabbitMQModule.java +++ b/server/container/guice/rabbitmq/src/main/java/org/apache/james/modules/rabbitmq/RabbitMQModule.java @@ -34,6 +34,7 @@ import org.apache.james.core.healthcheck.HealthCheck; import org.apache.james.eventsourcing.Event; import org.apache.james.eventsourcing.eventstore.cassandra.dto.EventDTO; import org.apache.james.eventsourcing.eventstore.cassandra.dto.EventDTOModule; +import org.apache.james.lifecycle.api.StartUpCheck; import org.apache.james.queue.api.MailQueueFactory; import org.apache.james.queue.api.ManageableMailQueue; import org.apache.james.queue.rabbitmq.RabbitMQMailQueue; @@ -46,6 +47,7 @@ import org.apache.james.queue.rabbitmq.view.cassandra.CassandraMailQueueMailDele import org.apache.james.queue.rabbitmq.view.cassandra.CassandraMailQueueMailStore; import org.apache.james.queue.rabbitmq.view.cassandra.CassandraMailQueueView; import org.apache.james.queue.rabbitmq.view.cassandra.CassandraMailQueueViewModule; +import org.apache.james.queue.rabbitmq.view.cassandra.CassandraMailQueueViewStartUpCheck; import org.apache.james.queue.rabbitmq.view.cassandra.DeletedMailsDAO; import org.apache.james.queue.rabbitmq.view.cassandra.EnqueuedMailsDAO; import org.apache.james.queue.rabbitmq.view.cassandra.configuration.CassandraMailQueueViewConfiguration; @@ -87,6 +89,7 @@ public class RabbitMQModule extends AbstractModule { Multibinder<EventDTOModule<? extends Event, ? extends EventDTO>> eventDTOModuleBinder = Multibinder.newSetBinder(binder(), new TypeLiteral<EventDTOModule<? extends Event, ? extends EventDTO>>() {}); eventDTOModuleBinder.addBinding().toInstance(CassandraMailQueueViewConfigurationModule.MAIL_QUEUE_VIEW_CONFIGURATION); + Multibinder.newSetBinder(binder(), StartUpCheck.class).addBinding().to(CassandraMailQueueViewStartUpCheck.class); Multibinder.newSetBinder(binder(), HealthCheck.class).addBinding().to(RabbitMQHealthCheck.class); bind(ReactorRabbitMQChannelPool.Configuration.class).toInstance(ReactorRabbitMQChannelPool.Configuration.DEFAULT); diff --git a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/cassandra/CassandraMailQueueView.java b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/cassandra/CassandraMailQueueView.java index 754294f..5caaa4c 100644 --- a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/cassandra/CassandraMailQueueView.java +++ b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/cassandra/CassandraMailQueueView.java @@ -35,8 +35,6 @@ import org.apache.james.queue.rabbitmq.EnqueuedItem; import org.apache.james.queue.rabbitmq.MailQueueName; import org.apache.james.queue.rabbitmq.view.api.DeleteCondition; import org.apache.james.queue.rabbitmq.view.api.MailQueueView; -import org.apache.james.queue.rabbitmq.view.cassandra.configuration.CassandraMailQueueViewConfiguration; -import org.apache.james.queue.rabbitmq.view.cassandra.configuration.EventsourcingConfigurationManagement; import org.apache.james.queue.rabbitmq.view.cassandra.model.EnqueuedItemWithSlicingContext; import reactor.core.publisher.Flux; @@ -55,15 +53,11 @@ public class CassandraMailQueueView implements MailQueueView<CassandraMailQueueB public Factory(CassandraMailQueueMailStore storeHelper, CassandraMailQueueBrowser cassandraMailQueueBrowser, CassandraMailQueueMailDelete cassandraMailQueueMailDelete, - EventsourcingConfigurationManagement eventsourcingConfigurationManagement, - MimeMessageStore.Factory mimeMessageStoreFactory, - CassandraMailQueueViewConfiguration configuration) { + MimeMessageStore.Factory mimeMessageStoreFactory) { this.storeHelper = storeHelper; this.cassandraMailQueueBrowser = cassandraMailQueueBrowser; this.cassandraMailQueueMailDelete = cassandraMailQueueMailDelete; this.mimeMessageStoreFactory = mimeMessageStoreFactory; - - eventsourcingConfigurationManagement.registerConfiguration(configuration); } @Override diff --git a/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/cassandra/CassandraMailQueueViewStartUpCheck.java b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/cassandra/CassandraMailQueueViewStartUpCheck.java new file mode 100644 index 0000000..c29f40d --- /dev/null +++ b/server/queue/queue-rabbitmq/src/main/java/org/apache/james/queue/rabbitmq/view/cassandra/CassandraMailQueueViewStartUpCheck.java @@ -0,0 +1,64 @@ +/**************************************************************** + * 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.queue.rabbitmq.view.cassandra; + +import javax.inject.Inject; + +import org.apache.james.lifecycle.api.StartUpCheck; +import org.apache.james.queue.rabbitmq.view.cassandra.configuration.CassandraMailQueueViewConfiguration; +import org.apache.james.queue.rabbitmq.view.cassandra.configuration.EventsourcingConfigurationManagement; + +import com.google.common.annotations.VisibleForTesting; + +public class CassandraMailQueueViewStartUpCheck implements StartUpCheck { + private static final String NAME = "cassandra-mail-queue-view-startup-check"; + private final EventsourcingConfigurationManagement eventsourcingConfigurationManagement; + private final CassandraMailQueueViewConfiguration configuration; + + @VisibleForTesting + @Inject + public CassandraMailQueueViewStartUpCheck(EventsourcingConfigurationManagement eventsourcingConfigurationManagement, + CassandraMailQueueViewConfiguration configuration) { + this.eventsourcingConfigurationManagement = eventsourcingConfigurationManagement; + this.configuration = configuration; + } + + @Override + public CheckResult check() { + try { + eventsourcingConfigurationManagement.registerConfiguration(configuration); + return CheckResult.builder() + .checkName(NAME) + .resultType(ResultType.GOOD) + .build(); + } catch (IllegalArgumentException e) { + return CheckResult.builder() + .checkName(NAME) + .resultType(ResultType.BAD) + .description(e.getMessage()) + .build(); + } + } + + @Override + public String checkName() { + return NAME; + } +} diff --git a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueConfigurationChangeTest.java b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueConfigurationChangeTest.java index 6595c88..b4041f6 100644 --- a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueConfigurationChangeTest.java +++ b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueConfigurationChangeTest.java @@ -23,7 +23,6 @@ import static java.time.temporal.ChronoUnit.HOURS; import static java.time.temporal.ChronoUnit.MINUTES; import static org.apache.james.queue.api.Mails.defaultMail; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.time.Duration; import java.time.Instant; @@ -41,7 +40,11 @@ import org.apache.james.blob.api.HashBlobId; import org.apache.james.blob.cassandra.CassandraBlobModule; import org.apache.james.blob.cassandra.CassandraBlobStoreFactory; import org.apache.james.blob.mail.MimeMessageStore; +import org.apache.james.eventsourcing.eventstore.cassandra.CassandraEventStore; import org.apache.james.eventsourcing.eventstore.cassandra.CassandraEventStoreModule; +import org.apache.james.eventsourcing.eventstore.cassandra.EventStoreDao; +import org.apache.james.eventsourcing.eventstore.cassandra.JsonEventSerializer; +import org.apache.james.lifecycle.api.StartUpCheck; import org.apache.james.metrics.api.NoopGaugeRegistry; import org.apache.james.metrics.tests.RecordingMetricFactory; import org.apache.james.queue.api.MailQueue; @@ -51,8 +54,11 @@ import org.apache.james.queue.api.RawMailQueueItemDecoratorFactory; import org.apache.james.queue.rabbitmq.view.RabbitMQMailQueueConfiguration; import org.apache.james.queue.rabbitmq.view.cassandra.CassandraMailQueueView; import org.apache.james.queue.rabbitmq.view.cassandra.CassandraMailQueueViewModule; +import org.apache.james.queue.rabbitmq.view.cassandra.CassandraMailQueueViewStartUpCheck; import org.apache.james.queue.rabbitmq.view.cassandra.CassandraMailQueueViewTestFactory; import org.apache.james.queue.rabbitmq.view.cassandra.configuration.CassandraMailQueueViewConfiguration; +import org.apache.james.queue.rabbitmq.view.cassandra.configuration.CassandraMailQueueViewConfigurationModule; +import org.apache.james.queue.rabbitmq.view.cassandra.configuration.EventsourcingConfigurationManagement; import org.apache.james.util.streams.Iterators; import org.apache.james.utils.UpdatableTickingClock; import org.apache.mailet.Mail; @@ -61,6 +67,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; +import com.datastax.driver.core.Session; import com.github.fge.lambdas.Throwing; class RabbitMQMailQueueConfigurationChangeTest { @@ -107,19 +114,16 @@ class RabbitMQMailQueueConfigurationChangeTest { mqManagementApi.deleteAllQueues(); } - private RabbitMQMailQueue getRabbitMQMailQueue(CassandraCluster cassandra, CassandraMailQueueViewConfiguration mailQueueViewConfiguration) throws Exception { + private RabbitMQMailQueue getRabbitMQMailQueue(CassandraCluster cassandra, CassandraMailQueueViewConfiguration mailQueueViewConfiguration) { CassandraMailQueueView.Factory mailQueueViewFactory = CassandraMailQueueViewTestFactory.factory(clock, cassandra.getConf(), - cassandraCluster.getCassandraConsistenciesConfiguration(), mailQueueViewConfiguration, mimeMessageStoreFactory); - RabbitMQMailQueueConfiguration mailQueueSizeConfiguration = RabbitMQMailQueueConfiguration.builder() .sizeMetricsEnabled(true) .build(); - RabbitMQMailQueueFactory.PrivateFactory privateFactory = new RabbitMQMailQueueFactory.PrivateFactory( new RecordingMetricFactory(), new NoopGaugeRegistry(), @@ -132,11 +136,23 @@ class RabbitMQMailQueueConfigurationChangeTest { new RawMailQueueItemDecoratorFactory(), mailQueueSizeConfiguration); RabbitMQMailQueueFactory mailQueueFactory = new RabbitMQMailQueueFactory(rabbitMQExtension.getSender(), mqManagementApi, privateFactory); + assertThat(performStartUpCheck(cassandra.getConf(), mailQueueViewConfiguration)).isEqualTo(StartUpCheck.ResultType.GOOD); return mailQueueFactory.createQueue(SPOOL); } + private StartUpCheck.ResultType performStartUpCheck(Session session, CassandraMailQueueViewConfiguration configuration) { + EventStoreDao eventStoreDao = new EventStoreDao( + session, + JsonEventSerializer.forModules(CassandraMailQueueViewConfigurationModule.MAIL_QUEUE_VIEW_CONFIGURATION).withoutNestedType(), + cassandraCluster.getCassandraConsistenciesConfiguration()); + EventsourcingConfigurationManagement eventsourcingConfigurationManagement = new EventsourcingConfigurationManagement(new CassandraEventStore(eventStoreDao)); + + StartUpCheck check = new CassandraMailQueueViewStartUpCheck(eventsourcingConfigurationManagement, configuration); + return check.check().getResultType(); + } + @Test - void increasingBucketCountShouldAllowBrowsingAllQueueElements(CassandraCluster cassandra) throws Exception { + void increasingBucketCountShouldAllowBrowsingAllQueueElements(CassandraCluster cassandra) { RabbitMQMailQueue mailQueue = getRabbitMQMailQueue(cassandra, DEFAULT_CONFIGURATION); enqueueSomeMails(mailQueue, namePatternForSlice(1), 10); @@ -163,20 +179,20 @@ class RabbitMQMailQueueConfigurationChangeTest { } @Test - void decreasingBucketCountShouldBeRejected(CassandraCluster cassandra) throws Exception { - getRabbitMQMailQueue(cassandra, - CassandraMailQueueViewConfiguration.builder() - .bucketCount(THREE_BUCKET_COUNT + 2) - .updateBrowseStartPace(UPDATE_BROWSE_START_PACE) - .sliceWindow(ONE_HOUR_SLICE_WINDOW) - .build()); + void decreasingBucketCountShouldBeRejected(CassandraCluster cassandra) { + assertThat(performStartUpCheck(cassandra.getConf(), CassandraMailQueueViewConfiguration.builder() + .bucketCount(THREE_BUCKET_COUNT + 2) + .updateBrowseStartPace(UPDATE_BROWSE_START_PACE) + .sliceWindow(ONE_HOUR_SLICE_WINDOW) + .build())) + .isEqualTo(StartUpCheck.ResultType.GOOD); - assertThatThrownBy(() -> getRabbitMQMailQueue(cassandra, DEFAULT_CONFIGURATION)) - .isInstanceOf(IllegalArgumentException.class); + assertThat(performStartUpCheck(cassandra.getConf(), DEFAULT_CONFIGURATION)) + .isEqualTo(StartUpCheck.ResultType.BAD); } @Test - void divideSliceWindowShouldAllowBrowsingAllQueueElements(CassandraCluster cassandra) throws Exception { + void divideSliceWindowShouldAllowBrowsingAllQueueElements(CassandraCluster cassandra) { RabbitMQMailQueue mailQueue = getRabbitMQMailQueue(cassandra, DEFAULT_CONFIGURATION); clock.setInstant(IN_SLICE_1); @@ -209,29 +225,29 @@ class RabbitMQMailQueueConfigurationChangeTest { } @Test - void decreaseArbitrarilySliceWindowShouldBeRejected(CassandraCluster cassandra) throws Exception { - getRabbitMQMailQueue(cassandra, DEFAULT_CONFIGURATION); + void decreaseArbitrarilySliceWindowShouldBeRejected(CassandraCluster cassandra) { + assertThat(performStartUpCheck(cassandra.getConf(), DEFAULT_CONFIGURATION)) + .isEqualTo(StartUpCheck.ResultType.GOOD); - assertThatThrownBy(() -> getRabbitMQMailQueue(cassandra, - CassandraMailQueueViewConfiguration.builder() + assertThat(performStartUpCheck(cassandra.getConf(), CassandraMailQueueViewConfiguration.builder() .bucketCount(THREE_BUCKET_COUNT) .updateBrowseStartPace(UPDATE_BROWSE_START_PACE) .sliceWindow(Duration.ofMinutes(25)) .build())) - .isInstanceOf(IllegalArgumentException.class); + .isEqualTo(StartUpCheck.ResultType.BAD); } @Test - void increaseSliceWindowShouldBeRejected(CassandraCluster cassandra) throws Exception { - getRabbitMQMailQueue(cassandra, DEFAULT_CONFIGURATION); + void increaseSliceWindowShouldBeRejected(CassandraCluster cassandra) { + assertThat(performStartUpCheck(cassandra.getConf(), DEFAULT_CONFIGURATION)) + .isEqualTo(StartUpCheck.ResultType.GOOD); - assertThatThrownBy(() -> getRabbitMQMailQueue(cassandra, - CassandraMailQueueViewConfiguration.builder() + assertThat(performStartUpCheck(cassandra.getConf(), CassandraMailQueueViewConfiguration.builder() .bucketCount(THREE_BUCKET_COUNT) .updateBrowseStartPace(UPDATE_BROWSE_START_PACE) .sliceWindow(Duration.ofHours(2)) .build())) - .isInstanceOf(IllegalArgumentException.class); + .isEqualTo(StartUpCheck.ResultType.BAD); } private Function<Integer, String> namePatternForSlice(int sliceId) { diff --git a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueTest.java b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueTest.java index b933b52..3c810b4 100644 --- a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueTest.java +++ b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/RabbitMQMailQueueTest.java @@ -838,7 +838,6 @@ class RabbitMQMailQueueTest { MailQueueView.Factory mailQueueViewFactory = CassandraMailQueueViewTestFactory.factory( clock, cassandra.getConf(), - cassandraCluster.getCassandraConsistenciesConfiguration(), CassandraMailQueueViewConfiguration.builder() .bucketCount(THREE_BUCKET_COUNT) .updateBrowseStartPace(UPDATE_BROWSE_START_PACE) diff --git a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/view/cassandra/CassandraMailQueueViewTestFactory.java b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/view/cassandra/CassandraMailQueueViewTestFactory.java index 0d611a8..1d7174a 100644 --- a/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/view/cassandra/CassandraMailQueueViewTestFactory.java +++ b/server/queue/queue-rabbitmq/src/test/java/org/apache/james/queue/rabbitmq/view/cassandra/CassandraMailQueueViewTestFactory.java @@ -22,23 +22,16 @@ package org.apache.james.queue.rabbitmq.view.cassandra; import java.time.Clock; import java.util.Optional; -import org.apache.james.backends.cassandra.init.configuration.CassandraConsistenciesConfiguration; import org.apache.james.blob.api.HashBlobId; import org.apache.james.blob.mail.MimeMessageStore; -import org.apache.james.eventsourcing.eventstore.cassandra.CassandraEventStore; -import org.apache.james.eventsourcing.eventstore.cassandra.EventStoreDao; -import org.apache.james.eventsourcing.eventstore.cassandra.JsonEventSerializer; import org.apache.james.queue.rabbitmq.MailQueueName; import org.apache.james.queue.rabbitmq.view.cassandra.configuration.CassandraMailQueueViewConfiguration; -import org.apache.james.queue.rabbitmq.view.cassandra.configuration.CassandraMailQueueViewConfigurationModule; -import org.apache.james.queue.rabbitmq.view.cassandra.configuration.EventsourcingConfigurationManagement; import com.datastax.driver.core.Session; public class CassandraMailQueueViewTestFactory { public static CassandraMailQueueView.Factory factory(Clock clock, Session session, - CassandraConsistenciesConfiguration cassandraConsistenciesConfiguration, CassandraMailQueueViewConfiguration configuration, MimeMessageStore.Factory mimeMessageStoreFactory) { HashBlobId.Factory blobIdFactory = new HashBlobId.Factory(); @@ -51,20 +44,11 @@ public class CassandraMailQueueViewTestFactory { CassandraMailQueueMailStore cassandraMailQueueMailStore = new CassandraMailQueueMailStore(enqueuedMailsDao, browseStartDao, configuration, clock); CassandraMailQueueMailDelete cassandraMailQueueMailDelete = new CassandraMailQueueMailDelete(deletedMailsDao, browseStartDao, cassandraMailQueueBrowser, configuration); - - EventStoreDao eventStoreDao = new EventStoreDao( - session, - JsonEventSerializer.forModules(CassandraMailQueueViewConfigurationModule.MAIL_QUEUE_VIEW_CONFIGURATION).withoutNestedType(), - cassandraConsistenciesConfiguration); - EventsourcingConfigurationManagement eventsourcingConfigurationManagement = new EventsourcingConfigurationManagement(new CassandraEventStore(eventStoreDao)); - return new CassandraMailQueueView.Factory( cassandraMailQueueMailStore, cassandraMailQueueBrowser, cassandraMailQueueMailDelete, - eventsourcingConfigurationManagement, - mimeMessageStoreFactory, - configuration); + mimeMessageStoreFactory); } public static boolean isInitialized(Session session, MailQueueName mailQueueName) { --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
