You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by rc...@apache.org on 2021/05/04 07:27:19 UTC

[james-project] 03/03: JAMES-3467 Domain cache should be refreshed periodically under read load

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 f93cdfc8ff70ca569e77eed79e7496a64785f507
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Mon Apr 26 23:39:38 2021 +0700

    JAMES-3467 Domain cache should be refreshed periodically under read load
    
    The documentation states:
    
    ```
    Expiracy of the cache. Longer means less reads are performed to the backend but writes will take longer to propagate.
    ```
    
    Which implies writes will e visible after the expiracy period.
    
    Sadly it was not the case as the added test proves it...
---
 .../domainlist/cassandra/CacheDomainListTest.java  | 28 ++++++++++++++++++----
 .../james/domainlist/lib/AbstractDomainList.java   |  2 +-
 2 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/server/data/data-cassandra/src/test/java/org/apache/james/domainlist/cassandra/CacheDomainListTest.java b/server/data/data-cassandra/src/test/java/org/apache/james/domainlist/cassandra/CacheDomainListTest.java
index c74d661..f216b01 100644
--- a/server/data/data-cassandra/src/test/java/org/apache/james/domainlist/cassandra/CacheDomainListTest.java
+++ b/server/data/data-cassandra/src/test/java/org/apache/james/domainlist/cassandra/CacheDomainListTest.java
@@ -22,6 +22,7 @@ package org.apache.james.domainlist.cassandra;
 import static org.assertj.core.api.Assertions.assertThat;
 
 import java.net.UnknownHostException;
+import java.time.Duration;
 
 import org.apache.james.backends.cassandra.CassandraCluster;
 import org.apache.james.backends.cassandra.CassandraClusterExtension;
@@ -29,10 +30,8 @@ import org.apache.james.backends.cassandra.StatementRecorder;
 import org.apache.james.core.Domain;
 import org.apache.james.dnsservice.api.DNSService;
 import org.apache.james.dnsservice.api.InMemoryDNSService;
-import org.apache.james.domainlist.api.DomainList;
 import org.apache.james.domainlist.api.DomainListException;
 import org.apache.james.domainlist.lib.DomainListConfiguration;
-import org.apache.james.domainlist.lib.DomainListContract;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.RegisterExtension;
@@ -40,6 +39,8 @@ import org.junit.jupiter.api.extension.RegisterExtension;
 import com.github.fge.lambdas.Throwing;
 
 import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+import reactor.core.scheduler.Schedulers;
 
 class CacheDomainListTest {
 
@@ -57,6 +58,7 @@ class CacheDomainListTest {
             .autoDetect(false)
             .autoDetectIp(false)
             .cacheEnabled(true)
+            .cacheExpiracy(Duration.ofSeconds(1))
             .build());
     }
 
@@ -77,12 +79,30 @@ class CacheDomainListTest {
     }
 
     @Test
-    void additionIsInstant() throws DomainListException {
+    void cacheShouldBeRefreshedPeriodicallyUnderReadLoad(CassandraCluster cassandra) throws DomainListException {
+        domainList.addDomain(DOMAIN_1);
+
+        StatementRecorder statementRecorder = new StatementRecorder();
+        cassandra.getConf().recordStatements(statementRecorder);
+
+        Flux.range(0, 6)
+            .delayElements(Duration.ofMillis(500))
+            .flatMap(Throwing.function(i -> Mono.fromCallable(() ->domainList.containsDomain(DOMAIN_1)).subscribeOn(Schedulers.elastic())))
+            .subscribeOn(Schedulers.elastic())
+            .blockLast();
+
+        assertThat(statementRecorder.listExecutedStatements(
+            StatementRecorder.Selector.preparedStatement("SELECT domain FROM domains WHERE domain=:domain;")))
+            .hasSize(2);
+    }
+
+    @Test
+    void additionIsNotInstant() throws DomainListException {
         domainList.containsDomain(DOMAIN_1);
 
         domainList.addDomain(DOMAIN_1);
 
-        assertThat(domainList.containsDomain(DOMAIN_1)).isEqualTo(true);
+        assertThat(domainList.containsDomain(DOMAIN_1)).isEqualTo(false);
     }
 
     @Test
diff --git a/server/data/data-library/src/main/java/org/apache/james/domainlist/lib/AbstractDomainList.java b/server/data/data-library/src/main/java/org/apache/james/domainlist/lib/AbstractDomainList.java
index 9f371eb..f505db6 100644
--- a/server/data/data-library/src/main/java/org/apache/james/domainlist/lib/AbstractDomainList.java
+++ b/server/data/data-library/src/main/java/org/apache/james/domainlist/lib/AbstractDomainList.java
@@ -93,7 +93,7 @@ public abstract class AbstractDomainList implements DomainList, Configurable {
         this.configuration = domainListConfiguration;
 
         this.cache = CacheBuilder.newBuilder()
-            .expireAfterAccess(configuration.getCacheExpiracy())
+            .expireAfterWrite(configuration.getCacheExpiracy())
             .build(new CacheLoader<>() {
                 @Override
                 public Boolean load(Domain key) throws DomainListException {

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