Skip to content

Commit 965c9a4

Browse files
odrotbohmciberkleid
authored andcommitted
GH-750 - Optimize publication completion by event and target identifier.
We now additionally guard the completion query by event and target identifier to also only apply to publications that have not been completed yet. This will allow databases to optimize the query plan to apply simple comparisons (the date being null) over complex comparisons (the event payload) to reduce the intermediate results to process further and thus improve performance.
1 parent 73050ed commit 965c9a4

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

spring-modulith-events/spring-modulith-events-jdbc/src/main/java/org/springframework/modulith/events/jdbc/JdbcEventPublicationRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ INSERT INTO EVENT_PUBLICATION (ID, EVENT_TYPE, LISTENER_ID, PUBLICATION_DATE, SE
8585
SET COMPLETION_DATE = ?
8686
WHERE
8787
LISTENER_ID = ?
88+
AND COMPLETION_DATE IS NULL
8889
AND SERIALIZED_EVENT = ?
8990
""";
9091

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.modulith.events.jdbc;
17+
18+
import static org.mockito.ArgumentMatchers.*;
19+
import static org.mockito.Mockito.*;
20+
21+
import java.time.Instant;
22+
import java.util.List;
23+
import java.util.Random;
24+
import java.util.UUID;
25+
import java.util.concurrent.TimeUnit;
26+
import java.util.stream.IntStream;
27+
28+
import org.junit.jupiter.api.BeforeEach;
29+
import org.junit.jupiter.api.Test;
30+
import org.springframework.beans.factory.annotation.Autowired;
31+
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
32+
import org.springframework.jdbc.core.JdbcOperations;
33+
import org.springframework.modulith.events.core.EventPublicationRepository;
34+
import org.springframework.modulith.events.core.EventSerializer;
35+
import org.springframework.modulith.events.core.PublicationTargetIdentifier;
36+
import org.springframework.modulith.events.core.TargetEventPublication;
37+
import org.springframework.modulith.testapp.TestApplication;
38+
import org.springframework.test.context.ActiveProfiles;
39+
import org.springframework.test.context.ContextConfiguration;
40+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
41+
import org.springframework.util.StopWatch;
42+
import org.testcontainers.junit.jupiter.Testcontainers;
43+
44+
/**
45+
* Not an actual test but can be executed in case of suspected regressions.
46+
*
47+
* @author Oliver Drotbohm
48+
*/
49+
@JdbcTest(properties = "spring.modulith.events.jdbc.schema-initialization.enabled=true")
50+
@Testcontainers(disabledWithoutDocker = true)
51+
@ContextConfiguration(classes = { TestApplication.class, JdbcEventPublicationAutoConfiguration.class })
52+
@ActiveProfiles("postgres")
53+
class JdbcEventPublicationRepositoryPerformance {
54+
55+
@Autowired JdbcOperations operations;
56+
@Autowired EventPublicationRepository repository;
57+
@MockitoBean EventSerializer serializer;
58+
59+
TargetEventPublication toComplete;
60+
Instant now = Instant.now();
61+
List<TargetEventPublication> publications;
62+
63+
int number = 10000;
64+
int fractionCompleted = 95;
65+
String serializedEvent = "{\"eventId\":\"id\"}";
66+
67+
@BeforeEach
68+
void setUp() {
69+
70+
operations.update("DELETE FROM event_publication");
71+
72+
when(serializer.serialize(any())).thenReturn(serializedEvent);
73+
74+
publications = IntStream.range(0, number)
75+
.mapToObj(it -> TargetEventPublication.of(new SampleEvent(),
76+
PublicationTargetIdentifier.of(UUID.randomUUID().toString())))
77+
.map(repository::create)
78+
.toList();
79+
80+
new Random()
81+
.ints(number / 100 * fractionCompleted, 0, number)
82+
.mapToObj(publications::get)
83+
.forEach(it -> repository.markCompleted(it, Instant.now()));
84+
85+
do {
86+
87+
var candidate = publications.get(new Random().nextInt(number));
88+
89+
if (!candidate.isPublicationCompleted()) {
90+
toComplete = candidate;
91+
}
92+
93+
} while (toComplete == null);
94+
}
95+
96+
@Test
97+
void marksPublicationAsCompletedById() {
98+
99+
runWithMeasurement("By id", () -> repository.markCompleted(toComplete.getIdentifier(), now));
100+
}
101+
102+
@Test
103+
void marksPublicationAsCompleted() {
104+
105+
runWithMeasurement("By event and target identifier",
106+
() -> repository.markCompleted(toComplete.getEvent(), toComplete.getTargetIdentifier(), now));
107+
}
108+
109+
void runWithMeasurement(String prefix, Runnable runnable) {
110+
111+
var watch = new StopWatch();
112+
watch.start();
113+
114+
runnable.run();
115+
116+
watch.stop();
117+
118+
System.out.println(prefix + " took: " + watch.lastTaskInfo().getTime(TimeUnit.MILLISECONDS) + "ms");
119+
}
120+
121+
static class SampleEvent {};
122+
}

spring-modulith-events/spring-modulith-events-jpa/src/main/java/org/springframework/modulith/events/jpa/JpaEventPublicationRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class JpaEventPublicationRepository implements EventPublicationRepository {
8383
set p.completionDate = ?3
8484
where p.serializedEvent = ?1
8585
and p.listenerId = ?2
86+
and p.completionDate is null
8687
""";
8788

8889
private static final String DELETE = """

spring-modulith-events/spring-modulith-events-neo4j/src/main/java/org/springframework/modulith/events/neo4j/Neo4jEventPublicationRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ class Neo4jEventPublicationRepository implements EventPublicationRepository {
107107
private static final Statement COMPLETE_STATEMENT = Cypher.match(EVENT_PUBLICATION_NODE)
108108
.where(EVENT_PUBLICATION_NODE.property(EVENT_HASH).eq(Cypher.parameter(EVENT_HASH)))
109109
.and(EVENT_PUBLICATION_NODE.property(LISTENER_ID).eq(Cypher.parameter(LISTENER_ID)))
110+
.and(EVENT_PUBLICATION_NODE.property(COMPLETION_DATE).isNull())
110111
.set(EVENT_PUBLICATION_NODE.property(COMPLETION_DATE).to(Cypher.parameter(COMPLETION_DATE)))
111112
.build();
112113

0 commit comments

Comments
 (0)