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
commit bd4f32f3ddc1eddbf3b58dbd797c613566e31e73 Author: Benoit Tellier <[email protected]> AuthorDate: Thu May 21 10:19:12 2020 +0700 JAMES-3140 Guice bindings for CachedBlobStore --- .../blob/cassandra/cache/CachedBlobStore.java | 6 +- .../cassandra/cache/CassandraBlobStoreCache.java | 4 +- .../james/CassandraRabbitMQJamesConfiguration.java | 2 +- .../james/CassandraRabbitMQJamesServerMain.java | 4 +- .../blobstore/BlobStoreCacheModulesChooser.java | 102 +++++++++++++++++++++ .../modules/blobstore/BlobStoreModulesChooser.java | 8 +- .../java/org/apache/james/WithCacheExtension.java | 86 +++++++++++++++++ .../test/java/org/apache/james/WithCacheTest.java | 27 ++++++ .../BlobStoreCacheModulesChooserTest.java | 45 +++++++++ .../CassandraRabbitMQLdapJamesServerMain.java | 4 +- 10 files changed, 279 insertions(+), 9 deletions(-) diff --git a/server/blob/blob-cassandra/src/main/java/org/apache/james/blob/cassandra/cache/CachedBlobStore.java b/server/blob/blob-cassandra/src/main/java/org/apache/james/blob/cassandra/cache/CachedBlobStore.java index 04857e4..45952a8e 100644 --- a/server/blob/blob-cassandra/src/main/java/org/apache/james/blob/cassandra/cache/CachedBlobStore.java +++ b/server/blob/blob-cassandra/src/main/java/org/apache/james/blob/cassandra/cache/CachedBlobStore.java @@ -28,6 +28,7 @@ import java.util.Arrays; import java.util.Optional; import javax.inject.Inject; +import javax.inject.Named; import org.apache.commons.io.IOUtils; import org.apache.james.blob.api.BlobId; @@ -98,12 +99,15 @@ public class CachedBlobStore implements BlobStore { } + public static final String BACKEND = "blobStoreBackend"; + private final BlobStoreCache cache; private final BlobStore backend; private final Integer sizeThresholdInBytes; @Inject - public CachedBlobStore(BlobStoreCache cache, BlobStore backend, + public CachedBlobStore(BlobStoreCache cache, + @Named(BACKEND) BlobStore backend, CassandraCacheConfiguration cacheConfiguration) { this.cache = cache; this.backend = backend; diff --git a/server/blob/blob-cassandra/src/main/java/org/apache/james/blob/cassandra/cache/CassandraBlobStoreCache.java b/server/blob/blob-cassandra/src/main/java/org/apache/james/blob/cassandra/cache/CassandraBlobStoreCache.java index 32f5d62..f2211a5 100644 --- a/server/blob/blob-cassandra/src/main/java/org/apache/james/blob/cassandra/cache/CassandraBlobStoreCache.java +++ b/server/blob/blob-cassandra/src/main/java/org/apache/james/blob/cassandra/cache/CassandraBlobStoreCache.java @@ -35,7 +35,9 @@ import static org.apache.james.blob.cassandra.BlobTables.BucketBlobTable.ID; import java.nio.ByteBuffer; import javax.inject.Inject; +import javax.inject.Named; +import org.apache.james.backends.cassandra.init.configuration.InjectionNames; import org.apache.james.backends.cassandra.utils.CassandraAsyncExecutor; import org.apache.james.blob.api.BlobId; import org.slf4j.Logger; @@ -62,7 +64,7 @@ public class CassandraBlobStoreCache implements BlobStoreCache { @Inject @VisibleForTesting - CassandraBlobStoreCache(Session session, CassandraCacheConfiguration cacheConfiguration) { + CassandraBlobStoreCache(@Named(InjectionNames.CACHE) Session session, CassandraCacheConfiguration cacheConfiguration) { this.cassandraAsyncExecutor = new CassandraAsyncExecutor(session); this.insertStatement = prepareInsert(session); this.selectStatement = prepareSelect(session); diff --git a/server/container/guice/cassandra-rabbitmq-guice/src/main/java/org/apache/james/CassandraRabbitMQJamesConfiguration.java b/server/container/guice/cassandra-rabbitmq-guice/src/main/java/org/apache/james/CassandraRabbitMQJamesConfiguration.java index 50c3f59..83c2b7e 100644 --- a/server/container/guice/cassandra-rabbitmq-guice/src/main/java/org/apache/james/CassandraRabbitMQJamesConfiguration.java +++ b/server/container/guice/cassandra-rabbitmq-guice/src/main/java/org/apache/james/CassandraRabbitMQJamesConfiguration.java @@ -116,7 +116,7 @@ public class CassandraRabbitMQJamesConfiguration implements Configuration { return directories; } - public BlobStoreConfiguration blobstoreconfiguration() { + public BlobStoreConfiguration blobStoreConfiguration() { return blobStoreConfiguration; } } diff --git a/server/container/guice/cassandra-rabbitmq-guice/src/main/java/org/apache/james/CassandraRabbitMQJamesServerMain.java b/server/container/guice/cassandra-rabbitmq-guice/src/main/java/org/apache/james/CassandraRabbitMQJamesServerMain.java index ed2b316..e9730fa 100644 --- a/server/container/guice/cassandra-rabbitmq-guice/src/main/java/org/apache/james/CassandraRabbitMQJamesServerMain.java +++ b/server/container/guice/cassandra-rabbitmq-guice/src/main/java/org/apache/james/CassandraRabbitMQJamesServerMain.java @@ -23,6 +23,7 @@ import static org.apache.james.CassandraJamesServerMain.REQUIRE_TASK_MANAGER_MOD import org.apache.james.modules.DistributedTaskManagerModule; import org.apache.james.modules.TaskSerializationModule; +import org.apache.james.modules.blobstore.BlobStoreCacheModulesChooser; import org.apache.james.modules.blobstore.BlobStoreConfiguration; import org.apache.james.modules.blobstore.BlobStoreModulesChooser; import org.apache.james.modules.event.RabbitMQEventBusModule; @@ -54,6 +55,7 @@ public class CassandraRabbitMQJamesServerMain implements JamesServerMain { return GuiceJamesServer.forConfiguration(configuration) .combineWith(MODULES) - .combineWith(BlobStoreModulesChooser.chooseModules(blobStoreConfiguration)); + .combineWith(BlobStoreModulesChooser.chooseModules(blobStoreConfiguration)) + .combineWith(BlobStoreCacheModulesChooser.chooseModules(blobStoreConfiguration)); } } diff --git a/server/container/guice/cassandra-rabbitmq-guice/src/main/java/org/apache/james/modules/blobstore/BlobStoreCacheModulesChooser.java b/server/container/guice/cassandra-rabbitmq-guice/src/main/java/org/apache/james/modules/blobstore/BlobStoreCacheModulesChooser.java new file mode 100644 index 0000000..0fb5b3e --- /dev/null +++ b/server/container/guice/cassandra-rabbitmq-guice/src/main/java/org/apache/james/modules/blobstore/BlobStoreCacheModulesChooser.java @@ -0,0 +1,102 @@ +/**************************************************************** + * 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.modules.blobstore; + +import java.io.FileNotFoundException; +import java.util.List; + +import javax.inject.Named; + +import org.apache.commons.configuration2.Configuration; +import org.apache.commons.configuration2.ex.ConfigurationException; +import org.apache.james.backends.cassandra.components.CassandraModule; +import org.apache.james.backends.cassandra.init.configuration.InjectionNames; +import org.apache.james.blob.api.BlobStore; +import org.apache.james.blob.api.MetricableBlobStore; +import org.apache.james.blob.cassandra.cache.BlobStoreCache; +import org.apache.james.blob.cassandra.cache.CachedBlobStore; +import org.apache.james.blob.cassandra.cache.CassandraBlobCacheModule; +import org.apache.james.blob.cassandra.cache.CassandraBlobStoreCache; +import org.apache.james.blob.cassandra.cache.CassandraCacheConfiguration; +import org.apache.james.modules.mailbox.CassandraCacheSessionModule; +import org.apache.james.modules.mailbox.ConfigurationComponent; +import org.apache.james.utils.PropertiesProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.collect.ImmutableList; +import com.google.inject.AbstractModule; +import com.google.inject.Module; +import com.google.inject.Provides; +import com.google.inject.Scopes; +import com.google.inject.Singleton; +import com.google.inject.multibindings.Multibinder; +import com.google.inject.name.Names; + +public class BlobStoreCacheModulesChooser { + private static final Logger LOGGER = LoggerFactory.getLogger(BlobStoreCacheModulesChooser.class); + + static class CacheDisabledModule extends AbstractModule { + @Provides + @Named(MetricableBlobStore.BLOB_STORE_IMPLEMENTATION) + @Singleton + BlobStore provideBlobStore(@Named(CachedBlobStore.BACKEND) BlobStore blobStore) { + return blobStore; + } + } + + static class CacheEnabledModule extends AbstractModule { + @Override + protected void configure() { + bind(CassandraBlobStoreCache.class).in(Scopes.SINGLETON); + bind(BlobStoreCache.class).to(CassandraBlobStoreCache.class); + + Multibinder.newSetBinder(binder(), CassandraModule.class, Names.named(InjectionNames.CACHE)) + .addBinding() + .toInstance(CassandraBlobCacheModule.MODULE); + } + + @Provides + @Named(MetricableBlobStore.BLOB_STORE_IMPLEMENTATION) + @Singleton + BlobStore provideBlobStore(CachedBlobStore cachedBlobStore) { + return cachedBlobStore; + } + + @Provides + @Singleton + CassandraCacheConfiguration providesCacheConfiguration(PropertiesProvider propertiesProvider) throws ConfigurationException { + try { + Configuration configuration = propertiesProvider.getConfigurations(ConfigurationComponent.NAMES); + return CassandraCacheConfiguration.from(configuration); + } catch (FileNotFoundException e) { + LOGGER.warn("Could not find " + ConfigurationComponent.NAME + " configuration file, using cassandra cache defaults"); + return CassandraCacheConfiguration.DEFAULT; + } + } + } + + public static List<Module> chooseModules(BlobStoreConfiguration blobStoreConfiguration) { + if (blobStoreConfiguration.cacheEnabled()) { + return ImmutableList.of(new CassandraCacheSessionModule(), new CacheEnabledModule()); + } + return ImmutableList.of(new CacheDisabledModule()); + } +} 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 f1cc709..763e66d 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 @@ -28,8 +28,8 @@ import javax.inject.Singleton; import org.apache.commons.configuration2.Configuration; import org.apache.commons.configuration2.ex.ConfigurationException; import org.apache.james.blob.api.BlobStore; -import org.apache.james.blob.api.MetricableBlobStore; import org.apache.james.blob.cassandra.CassandraBlobStore; +import org.apache.james.blob.cassandra.cache.CachedBlobStore; import org.apache.james.blob.objectstorage.ObjectStorageBlobStore; import org.apache.james.blob.union.HybridBlobStore; import org.apache.james.modules.mailbox.CassandraBlobStoreDependenciesModule; @@ -50,7 +50,7 @@ public class BlobStoreModulesChooser { protected void configure() { install(new CassandraBlobStoreDependenciesModule()); bind(BlobStore.class) - .annotatedWith(Names.named(MetricableBlobStore.BLOB_STORE_IMPLEMENTATION)) + .annotatedWith(Names.named(CachedBlobStore.BACKEND)) .to(CassandraBlobStore.class); } } @@ -60,7 +60,7 @@ public class BlobStoreModulesChooser { protected void configure() { install(new ObjectStorageDependenciesModule()); bind(BlobStore.class) - .annotatedWith(Names.named(MetricableBlobStore.BLOB_STORE_IMPLEMENTATION)) + .annotatedWith(Names.named(CachedBlobStore.BACKEND)) .to(ObjectStorageBlobStore.class); } } @@ -85,7 +85,7 @@ public class BlobStoreModulesChooser { } @Provides - @Named(MetricableBlobStore.BLOB_STORE_IMPLEMENTATION) + @Named(CachedBlobStore.BACKEND) @Singleton BlobStore providesHybridBlobStore(HybridBlobStore.Configuration hybridBlobStoreConfiguration, CassandraBlobStore cassandraBlobStore, diff --git a/server/container/guice/cassandra-rabbitmq-guice/src/test/java/org/apache/james/WithCacheExtension.java b/server/container/guice/cassandra-rabbitmq-guice/src/test/java/org/apache/james/WithCacheExtension.java new file mode 100644 index 0000000..85891e6 --- /dev/null +++ b/server/container/guice/cassandra-rabbitmq-guice/src/test/java/org/apache/james/WithCacheExtension.java @@ -0,0 +1,86 @@ +/**************************************************************** + * 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; + +import org.apache.james.jmap.draft.JmapJamesServerContract; +import org.apache.james.modules.AwsS3BlobStoreExtension; +import org.apache.james.modules.RabbitMQExtension; +import org.apache.james.modules.TestJMAPServerModule; +import org.apache.james.modules.blobstore.BlobStoreConfiguration; +import org.junit.jupiter.api.extension.AfterAllCallback; +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.BeforeAllCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.ParameterContext; +import org.junit.jupiter.api.extension.ParameterResolutionException; +import org.junit.jupiter.api.extension.ParameterResolver; + +public class WithCacheExtension implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback, ParameterResolver { + + private final JamesServerExtension jamesServerExtension; + + WithCacheExtension() { + jamesServerExtension = new JamesServerBuilder<CassandraRabbitMQJamesConfiguration>(tmpDir -> + CassandraRabbitMQJamesConfiguration.builder() + .workingDirectory(tmpDir) + .configurationFromClasspath() + .blobStore(BlobStoreConfiguration.objectStorage().enableCache()) + .build()) + .extension(new DockerElasticSearchExtension()) + .extension(new CassandraExtension()) + .extension(new RabbitMQExtension()) + .extension(new AwsS3BlobStoreExtension()) + .server(configuration -> CassandraRabbitMQJamesServerMain.createServer(configuration) + .overrideWith(new TestJMAPServerModule()) + .overrideWith(JmapJamesServerContract.DOMAIN_LIST_CONFIGURATION_MODULE)) + .build(); + } + + @Override + public void beforeAll(ExtensionContext context) throws Exception { + jamesServerExtension.beforeAll(context); + } + + @Override + public void afterAll(ExtensionContext context) throws Exception { + jamesServerExtension.afterAll(context); + } + + @Override + public void beforeEach(ExtensionContext context) throws Exception { + jamesServerExtension.beforeEach(context); + } + + @Override + public void afterEach(ExtensionContext context) throws Exception { + jamesServerExtension.afterEach(context); + } + + @Override + public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { + return (parameterContext.getParameter().getType() == GuiceJamesServer.class); + } + + @Override + public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { + return jamesServerExtension.getGuiceJamesServer(); + } +} diff --git a/server/container/guice/cassandra-rabbitmq-guice/src/test/java/org/apache/james/WithCacheTest.java b/server/container/guice/cassandra-rabbitmq-guice/src/test/java/org/apache/james/WithCacheTest.java new file mode 100644 index 0000000..ee5ebbc --- /dev/null +++ b/server/container/guice/cassandra-rabbitmq-guice/src/test/java/org/apache/james/WithCacheTest.java @@ -0,0 +1,27 @@ +/**************************************************************** + * 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; + +import org.apache.james.jmap.draft.JmapJamesServerContract; +import org.junit.jupiter.api.extension.ExtendWith; + +@ExtendWith(WithCacheExtension.class) +class WithCacheTest implements JmapJamesServerContract, MailsShouldBeWellReceived, JamesServerContract { +} \ No newline at end of file diff --git a/server/container/guice/cassandra-rabbitmq-guice/src/test/java/org/apache/james/modules/blobstore/BlobStoreCacheModulesChooserTest.java b/server/container/guice/cassandra-rabbitmq-guice/src/test/java/org/apache/james/modules/blobstore/BlobStoreCacheModulesChooserTest.java new file mode 100644 index 0000000..5cb8c50 --- /dev/null +++ b/server/container/guice/cassandra-rabbitmq-guice/src/test/java/org/apache/james/modules/blobstore/BlobStoreCacheModulesChooserTest.java @@ -0,0 +1,45 @@ +/**************************************************************** + * 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.modules.blobstore; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.apache.james.modules.mailbox.CassandraCacheSessionModule; +import org.junit.jupiter.api.Test; + +class BlobStoreCacheModulesChooserTest { + @Test + void chooseModulesShouldReturnCacheDisabledModuleWhenCacheDisabled() { + assertThat(BlobStoreCacheModulesChooser.chooseModules(BlobStoreConfiguration.objectStorage().disableCache())) + .hasSize(1) + .first() + .isInstanceOf(BlobStoreCacheModulesChooser.CacheDisabledModule.class); + } + + @Test + void chooseModulesShouldReturnCacheEnabledAndCassandraCacheModulesWhenCacheEnabled() { + assertThat(BlobStoreCacheModulesChooser.chooseModules(BlobStoreConfiguration.objectStorage().enableCache())) + .hasSize(2) + .allSatisfy(module -> + assertThat(module).isOfAnyClassIn( + BlobStoreCacheModulesChooser.CacheEnabledModule.class, + CassandraCacheSessionModule.class)); + } +} \ No newline at end of file diff --git a/server/container/guice/cassandra-rabbitmq-ldap-guice/src/main/java/org/apache/james/CassandraRabbitMQLdapJamesServerMain.java b/server/container/guice/cassandra-rabbitmq-ldap-guice/src/main/java/org/apache/james/CassandraRabbitMQLdapJamesServerMain.java index f1d487a..7743afa 100644 --- a/server/container/guice/cassandra-rabbitmq-ldap-guice/src/main/java/org/apache/james/CassandraRabbitMQLdapJamesServerMain.java +++ b/server/container/guice/cassandra-rabbitmq-ldap-guice/src/main/java/org/apache/james/CassandraRabbitMQLdapJamesServerMain.java @@ -20,6 +20,7 @@ package org.apache.james; import org.apache.james.data.LdapUsersRepositoryModule; +import org.apache.james.modules.blobstore.BlobStoreCacheModulesChooser; import org.apache.james.modules.blobstore.BlobStoreConfiguration; import org.apache.james.modules.blobstore.BlobStoreModulesChooser; import org.apache.james.modules.server.JMXServerModule; @@ -48,6 +49,7 @@ public class CassandraRabbitMQLdapJamesServerMain implements JamesServerMain { return GuiceJamesServer.forConfiguration(configuration) .combineWith(MODULES) - .combineWith(BlobStoreModulesChooser.chooseModules(blobStoreConfiguration)); + .combineWith(BlobStoreModulesChooser.chooseModules(blobStoreConfiguration)) + .combineWith(BlobStoreCacheModulesChooser.chooseModules(blobStoreConfiguration)); } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
