-
Notifications
You must be signed in to change notification settings - Fork 39
Per-message processing in topic readers #695
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
Open
alex268
wants to merge
12
commits into
ydb-platform:master
Choose a base branch
from
alex268:memory_manager
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d7feca6
Added BufferManager to control every message lifecycle
alex268 17302a1
Added per-message MessageDecoder
alex268 520f48e
Added unit tests for MessageDecoder & ReadPartitionDecoder
alex268 81472d2
Fixed integer overflow in BufferManager
alex268 8810b04
Extends TopicReadersIntegrationTest
alex268 d336c0d
Fixed data race in ReadPartitionSession
alex268 62fd707
Added empty messages guard to BufferManager
alex268 dbae2cf
style fixes
alex268 d86e2e9
Typo fixes
alex268 bd17fd1
Fixed corner case in MessageDecoder
alex268 521dfa8
Extended BufferManager logs
alex268 af96f01
Removed class Batch
alex268 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
233 changes: 233 additions & 0 deletions
233
topic/src/main/java/tech/ydb/topic/read/impl/BufferManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,233 @@ | ||
| package tech.ydb.topic.read.impl; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ConcurrentLinkedQueue; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
| import java.util.function.Consumer; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import tech.ydb.proto.topic.YdbTopic; | ||
| import tech.ydb.topic.description.OffsetsRange; | ||
|
|
||
| /** | ||
| * | ||
| * @author Aleksandr Gorshenin {@literal <alexandr268@ydb.tech>} | ||
| */ | ||
| public class BufferManager { | ||
| private static final Logger logger = LoggerFactory.getLogger(BufferManager.class); | ||
| private static final OffsetsRange ALL = OffsetsRange.of(Long.MIN_VALUE, Long.MAX_VALUE); | ||
|
|
||
| private final String traceID; | ||
| private final long maxBufferSize; | ||
| private final Consumer<Long> requestFunc; | ||
|
|
||
| private final AtomicLong released = new AtomicLong(0); | ||
| private final ConcurrentHashMap<Long, PartitionBuffer> partitions = new ConcurrentHashMap<>(); | ||
|
|
||
| private final AtomicLong totalAllocated = new AtomicLong(0); | ||
| private final AtomicLong totalReleased = new AtomicLong(0); | ||
|
|
||
| public BufferManager(String traceID, long maxBufferSize, Consumer<Long> requestFunc) { | ||
| this.traceID = traceID; | ||
| this.maxBufferSize = maxBufferSize; | ||
| this.requestFunc = requestFunc; | ||
| } | ||
|
|
||
| public void init(String sessionId) { | ||
| logger.info("[{}] Session {} initialized. Requesting {} bytes...", traceID, sessionId, maxBufferSize); | ||
| requestFunc.accept(maxBufferSize); | ||
| } | ||
|
|
||
| // Has no reentrant thread safety | ||
| public void allocate(long bufferSize, List<YdbTopic.StreamReadMessage.ReadResponse.PartitionData> dataList) { | ||
| logger.debug("[{}] Received ReadResponse of {} bytes, {} allocated and {} released before", | ||
| traceID, bufferSize, totalAllocated.get(), totalReleased.get()); | ||
| totalAllocated.addAndGet(bufferSize); | ||
|
|
||
| // calculate message count | ||
| int messagesCount = 0; | ||
| for (YdbTopic.StreamReadMessage.ReadResponse.PartitionData data: dataList) { | ||
| for (YdbTopic.StreamReadMessage.ReadResponse.Batch batch: data.getBatchesList()) { | ||
| messagesCount += batch.getMessageDataCount(); | ||
| } | ||
| } | ||
|
|
||
| if (messagesCount == 0) { | ||
| logger.error("[{}] Received empty ReadResponse of {} bytes", traceID, bufferSize); | ||
| release(bufferSize); | ||
| return; | ||
| } | ||
|
|
||
| // get real size for every message | ||
| int[] msgSize = new int[messagesCount]; | ||
| int msgIdx = 0; | ||
| for (YdbTopic.StreamReadMessage.ReadResponse.PartitionData data: dataList) { | ||
| for (YdbTopic.StreamReadMessage.ReadResponse.Batch batch: data.getBatchesList()) { | ||
| for (YdbTopic.StreamReadMessage.ReadResponse.MessageData msg: batch.getMessageDataList()) { | ||
| msgSize[msgIdx] = msg.getData().size(); | ||
| msgIdx++; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // recalculate real messages size to expected buffer size | ||
| recalcBuffer(msgSize, bufferSize); | ||
|
|
||
| // build batch and messages with calculated buffer size | ||
| msgIdx = 0; | ||
| for (YdbTopic.StreamReadMessage.ReadResponse.PartitionData data: dataList) { | ||
| PartitionBuffer part = partitions.computeIfAbsent(data.getPartitionSessionId(), PartitionBuffer::new); | ||
| for (YdbTopic.StreamReadMessage.ReadResponse.Batch batch: data.getBatchesList()) { | ||
| if (batch.getMessageDataCount() <= 0) { | ||
| continue; | ||
| } | ||
|
|
||
| long startOffset = batch.getMessageData(0).getOffset(); | ||
| int[] batchSizes = new int[batch.getMessageDataCount()]; | ||
| for (int idx = 0; idx < batch.getMessageDataCount(); idx++) { | ||
| batchSizes[idx] = msgSize[msgIdx++]; | ||
| } | ||
|
|
||
| part.add(new BatchBuffer(startOffset, batchSizes)); | ||
| } | ||
|
|
||
| if (!partitions.containsKey(data.getPartitionSessionId())) { | ||
| release(part.release(ALL)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Thread safe | ||
| public void releasePartition(Long id) { | ||
| PartitionBuffer part = partitions.remove(id); | ||
| if (part != null) { | ||
| release(part.release(ALL)); | ||
| } | ||
| } | ||
|
|
||
| // Thread safe | ||
| public void releaseRange(Long id, OffsetsRange range) { | ||
| PartitionBuffer part = partitions.get(id); | ||
| if (part != null) { | ||
| release(part.release(range)); | ||
| } | ||
| } | ||
|
|
||
| private void release(long total) { | ||
| long now = released.addAndGet(total); | ||
| if (now >= maxBufferSize / 10) { // threshold | ||
| long request = released.getAndSet(0); | ||
| if (request > 0) { | ||
| totalReleased.addAndGet(request); | ||
| requestFunc.accept(request); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void recalcBuffer(int[] buffer, long buffSize) { | ||
| // buffSize guard | ||
| if (buffSize == 0) { | ||
| Arrays.fill(buffer, 0); | ||
| return; | ||
| } | ||
|
|
||
| long total = 0; | ||
| for (int v: buffer) { | ||
| total += v; | ||
| } | ||
|
|
||
| // empty messages guard | ||
|
alex268 marked this conversation as resolved.
|
||
| if (total == 0) { | ||
| Arrays.fill(buffer, 0); | ||
| buffer[buffer.length - 1] = (int) buffSize; | ||
| return; | ||
| } | ||
|
|
||
| long currBuff = 0; | ||
| long currSum = 0; | ||
| for (int idx = 0; idx < buffer.length; idx += 1) { | ||
| currSum += buffer[idx]; | ||
| long newBuff = currSum * buffSize / total; | ||
|
alex268 marked this conversation as resolved.
|
||
| buffer[idx] = (int) (newBuff - currBuff); | ||
| currBuff = newBuff; | ||
| } | ||
| } | ||
|
|
||
| private static class PartitionBuffer { | ||
| private final ConcurrentLinkedQueue<BatchBuffer> batches = new ConcurrentLinkedQueue<>(); | ||
|
|
||
| PartitionBuffer(Long id) { | ||
| } | ||
|
|
||
| public void add(BatchBuffer range) { | ||
| batches.add(range); | ||
| } | ||
|
|
||
| public long release(OffsetsRange range) { | ||
| long released = 0; | ||
|
|
||
| Iterator<BatchBuffer> it = batches.iterator(); | ||
| while (it.hasNext()) { | ||
| BatchBuffer next = it.next(); | ||
| if (next.getStartOffset() > range.getEnd()) { // fast path | ||
| break; | ||
| } | ||
|
|
||
| released += next.release(range); | ||
| if (!next.isActive()) { | ||
| it.remove(); | ||
| } | ||
| } | ||
|
|
||
| return released; | ||
| } | ||
| } | ||
|
|
||
| private static class BatchBuffer { | ||
| private final long startOffset; | ||
| private final AtomicInteger[] messages; | ||
| private final AtomicLong total; | ||
|
|
||
| BatchBuffer(long startOffset, int[] messageSizes) { | ||
| this.startOffset = startOffset; | ||
| this.messages = new AtomicInteger[messageSizes.length]; | ||
| long totalSize = 0; | ||
| for (int idx = 0; idx < messageSizes.length; idx++) { | ||
| this.messages[idx] = new AtomicInteger(messageSizes[idx]); | ||
| totalSize += messageSizes[idx]; | ||
| } | ||
| this.total = new AtomicLong(totalSize); | ||
| } | ||
|
|
||
| public long getStartOffset() { | ||
| return startOffset; | ||
| } | ||
|
|
||
| public boolean isActive() { | ||
| return total.get() > 0; | ||
| } | ||
|
|
||
| public long release(OffsetsRange range) { | ||
| int first = (int) (Math.max(startOffset, range.getStart()) - startOffset); | ||
| int last = (int) Math.min(messages.length, range.getEnd() - startOffset); | ||
|
|
||
| if (last <= first) { | ||
| return 0; | ||
| } | ||
|
|
||
| long released = 0; | ||
| for (int idx = first; idx < last; idx++) { | ||
| released += messages[idx].getAndSet(0); | ||
| } | ||
| total.addAndGet(-released); | ||
|
|
||
| return released; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.