Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Make ObjectsCache thread safe #3836

Merged
merged 3 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.paimon.memory.MemorySegmentSource;

import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;

import java.io.EOFException;
import java.io.IOException;
Expand All @@ -36,11 +37,11 @@
import java.util.function.BiFunction;

/** Cache records to {@link SegmentsCache} by compacted serializer. */
@ThreadSafe
public class ObjectsCache<K, V> {

private final SegmentsCache<K> cache;
private final ObjectSerializer<V> serializer;
private final InternalRowSerializer rowSerializer;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introduce a threadlocal serializers?

private final BiFunction<K, Long, CloseableIterator<InternalRow>> reader;

public ObjectsCache(
Expand All @@ -49,7 +50,6 @@ public ObjectsCache(
BiFunction<K, Long, CloseableIterator<InternalRow>> reader) {
this.cache = cache;
this.serializer = serializer;
this.rowSerializer = new InternalRowSerializer(serializer.fieldTypes());
this.reader = reader;
}

Expand All @@ -59,7 +59,9 @@ public List<V> read(
Filter<InternalRow> loadFilter,
Filter<InternalRow> readFilter)
throws IOException {
Segments segments = cache.getSegments(key, k -> readSegments(k, fileSize, loadFilter));
InternalRowSerializer rowSerializer = new InternalRowSerializer(serializer.fieldTypes());
Segments segments =
cache.getSegments(key, k -> readSegments(k, fileSize, loadFilter, rowSerializer));
List<V> entries = new ArrayList<>();
RandomAccessInputView view =
new RandomAccessInputView(
Expand All @@ -77,7 +79,11 @@ public List<V> read(
}
}

private Segments readSegments(K key, @Nullable Long fileSize, Filter<InternalRow> loadFilter) {
private Segments readSegments(
K key,
@Nullable Long fileSize,
Filter<InternalRow> loadFilter,
InternalRowSerializer rowSerializer) {
try (CloseableIterator<InternalRow> iterator = reader.apply(key, fileSize)) {
ArrayList<MemorySegment> segments = new ArrayList<>();
MemorySegmentSource segmentSource =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,28 @@ public void test() throws IOException {
r -> r.getString(0).toString().endsWith("5"),
Filter.alwaysTrue());
assertThat(values).isEmpty();

// test read concurrently
map.clear();
for (int i = 0; i < 10; i++) {
map.put(String.valueOf(i), Collections.singletonList(String.valueOf(i)));
}
map.keySet().stream()
.parallel()
.forEach(
k -> {
try {
assertThat(
cache.read(
k,
null,
Filter.alwaysTrue(),
Filter.alwaysTrue()))
.containsExactly(k);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}

private static class StringSerializer extends ObjectSerializer<String> {
Expand Down
Loading