From 29541a519453463ee0627d6922edd1f8b49efba3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20=C3=87elik?= <34758411+omercelikceng@users.noreply.github.com> Date: Sat, 7 Dec 2024 01:00:33 +0300 Subject: [PATCH] Replace synchronized blocks with ReentrantLocks for virtual thread support * The lock call was moved outside the try block * Unnecessary locking has been removed. --- .../CassandraConsumerConfiguration.java | 11 ++++++++- .../trace/InMemoryTraceRepository.java | 23 +++++++++++-------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/consumer/spring-cassandra-consumer/src/main/java/org/springframework/cloud/fn/consumer/cassandra/CassandraConsumerConfiguration.java b/consumer/spring-cassandra-consumer/src/main/java/org/springframework/cloud/fn/consumer/cassandra/CassandraConsumerConfiguration.java index 34d9a6e8..8e991d55 100644 --- a/consumer/spring-cassandra-consumer/src/main/java/org/springframework/cloud/fn/consumer/cassandra/CassandraConsumerConfiguration.java +++ b/consumer/spring-cassandra-consumer/src/main/java/org/springframework/cloud/fn/consumer/cassandra/CassandraConsumerConfiguration.java @@ -23,6 +23,8 @@ import java.util.Map; import java.util.Optional; import java.util.UUID; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; import java.util.function.Consumer; import java.util.function.Function; @@ -62,6 +64,7 @@ * @author Thomas Risberg * @author Ashu Gairola * @author Akos Ratku + * @author Omer Celik */ @AutoConfiguration(after = CassandraReactiveDataAutoConfiguration.class) @EnableConfigurationProperties(CassandraConsumerProperties.class) @@ -147,6 +150,8 @@ private static class PayloadToMatrixTransformer extends AbstractPayloadTransform private final ISO8601StdDateFormat dateFormat = new ISO8601StdDateFormat(); + private final Lock dateLock = new ReentrantLock(); + PayloadToMatrixTransformer(ObjectMapper objectMapper, String query, ColumnNameExtractor columnNameExtractor) { this.jsonObjectMapper = new Jackson2JsonObjectMapper(objectMapper); this.columns.addAll(columnNameExtractor.extract(query)); @@ -170,9 +175,13 @@ protected List> transformPayload(Object payload) { Object value = entity.get(column); if (value instanceof String string) { if (this.dateFormat.looksLikeISO8601(string)) { - synchronized (this.dateFormat) { + this.dateLock.lock(); + try { value = new Date(this.dateFormat.parse(string).getTime()).toLocalDate(); } + finally { + this.dateLock.unlock(); + } } if (isUuid(string)) { value = UUID.fromString(string); diff --git a/consumer/spring-websocket-consumer/src/main/java/org/springframework/cloud/fn/consumer/websocket/trace/InMemoryTraceRepository.java b/consumer/spring-websocket-consumer/src/main/java/org/springframework/cloud/fn/consumer/websocket/trace/InMemoryTraceRepository.java index 7b1a3b63..1911961c 100644 --- a/consumer/spring-websocket-consumer/src/main/java/org/springframework/cloud/fn/consumer/websocket/trace/InMemoryTraceRepository.java +++ b/consumer/spring-websocket-consumer/src/main/java/org/springframework/cloud/fn/consumer/websocket/trace/InMemoryTraceRepository.java @@ -21,6 +21,8 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; /** * A repository for {@link Trace}s. @@ -31,6 +33,7 @@ * @author Dave Syer * @author Olivier Bourgain * @author Artem Bilan + * @author Omer Celik * @since 2.0 */ public class InMemoryTraceRepository { @@ -41,14 +44,14 @@ public class InMemoryTraceRepository { private final List traces = new LinkedList<>(); + private final Lock tracesLock = new ReentrantLock(); + /** * Flag to say that the repository lists traces in reverse order. * @param reverse flag value (default true) */ public void setReverse(boolean reverse) { - synchronized (this.traces) { - this.reverse = reverse; - } + this.reverse = reverse; } /** @@ -56,20 +59,17 @@ public void setReverse(boolean reverse) { * @param capacity the capacity */ public void setCapacity(int capacity) { - synchronized (this.traces) { - this.capacity = capacity; - } + this.capacity = capacity; } public List findAll() { - synchronized (this.traces) { - return Collections.unmodifiableList(this.traces); - } + return Collections.unmodifiableList(this.traces); } public void add(Map map) { Trace trace = new Trace(new Date(), map); - synchronized (this.traces) { + this.tracesLock.lock(); + try { while (this.traces.size() >= this.capacity) { this.traces.remove(this.reverse ? this.capacity - 1 : 0); } @@ -80,6 +80,9 @@ public void add(Map map) { this.traces.add(trace); } } + finally { + this.tracesLock.unlock(); + } } }