Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 9 additions & 5 deletions topic/src/main/java/tech/ydb/topic/read/impl/MessageDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,29 @@ public class MessageDecoder {
private final long maxBufferSize;
private final AtomicLong totalAvailable;

private final Executor decompressionExecutor;
private final Executor decompressor;
private final CodecRegistry codecRegistry;
private final ReaderMetrics metrics;

private final Queue<ReadPartitionDecoder.EncodedMessage> decodingQueue = new ConcurrentLinkedQueue<>();
private final SerialRunnable decodeNext = new SerialRunnable(new DecodeNext());
private volatile boolean isStopped = false;

public MessageDecoder(ReadConfig config) {
this(config.getMaxMemoryUsageBytes(), config.getDecompressor(), config.getCodecRegistry());
this(config.getMaxMemoryUsageBytes(), config.getDecompressor(), config.getCodecRegistry(), config.getMetrics());
}

MessageDecoder(long maxBufferSize, Executor decompressionExecutor, CodecRegistry codecRegistry) {
@VisibleForTesting
MessageDecoder(long maxBufferSize, Executor decompressor, CodecRegistry codecRegistry, ReaderMetrics metrics) {
if (maxBufferSize <= 0) {
throw new IllegalArgumentException("maxBufferSize must be positive, but got " + maxBufferSize);
}

this.maxBufferSize = maxBufferSize;
this.totalAvailable = new AtomicLong(maxBufferSize);
this.decompressionExecutor = decompressionExecutor;
this.decompressor = decompressor;
this.codecRegistry = codecRegistry;
this.metrics = metrics;
}

public void decodeNext() {
Expand Down Expand Up @@ -96,7 +100,7 @@ public void run() {
long size = next.allocate();
totalAvailable.addAndGet(-size);
try {
decompressionExecutor.execute(() -> next.decode(codecRegistry));
decompressor.execute(() -> next.decode(codecRegistry, metrics));
} catch (Throwable ex) {
logger.error("Cannot execute decompression ", ex);
next.setError(ex);
Expand Down
19 changes: 10 additions & 9 deletions topic/src/main/java/tech/ydb/topic/read/impl/ReadPartition.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private enum State {
private final ReaderMetrics metrics;
private final Executor dataExecutor;
private final Executor controlExecutor;
private final ReadSession.Handler handler;

private final ReadPartitionCommitter committer;
private final ReadPartitionDecoder decoder;
Expand All @@ -68,15 +69,15 @@ private enum State {
this.session = session;
this.partition = partition;

this.handler = session.getHandler();
ReadConfig config = session.getConfig();

this.metrics = config.getMetrics();
this.dataExecutor = new SerialExecutor(config.getDataExecutor());
this.controlExecutor = config.getControlExecutor();

this.committer = new ReadPartitionCommitter(traceID, session, this, lastCommittedOffset);
this.decoder = new ReadPartitionDecoder(traceID, session.getDecoder(), partition, committer,
this::sendDataToReaders, metrics);
this::sendDataToReaders);
this.queue = new ReadPartitionQueue(traceID, decoder, config.getMaxBatchSize(), lastCommittedOffset);

logger.info("[{}] started for {}", traceID, partition);
Expand Down Expand Up @@ -150,7 +151,7 @@ public boolean addBatches(List<YdbTopic.StreamReadMessage.ReadResponse.Batch> ba
private void sendCommitOffsetAck() {
CommitOffsetAcknowledgementEvent event = commitOffsetAck.getAndSet(null);
if (event != null) {
session.getHandler().onCommitAck(event);
handler.onCommitAck(event);
}
}

Expand All @@ -174,7 +175,7 @@ private void sendDataToReaders() {
long offsetEnd = event.getMessages().get(event.getMessages().size() - 1).getOffset();
logger.debug("[{}] onData with {} message(s) (offsets {}-{}) is about to be called...",
traceID, messagesCount, offsetStart, offsetEnd);
session.getHandler().onData(event);
handler.onData(event);
logger.debug("[{}] onData with {} message(s) (offsets {}-{}) successfully finished",
traceID, messagesCount, offsetStart, offsetEnd);

Expand All @@ -186,7 +187,7 @@ private void sendDataToReaders() {
if (event != null) {
logger.info("[{}] has finished processing and unpaused child partitions {}", traceID,
event.getChildsString());
session.getHandler().onPartitionEnded(event);
handler.onPartitionEnded(event);
session.releaseLocks(partition, event.getChilds());
}
}
Expand All @@ -213,7 +214,7 @@ public void close() {
if (old != State.STOPPED && old != State.CREATED) {
PartitionSessionClosedEventImpl closedEvent = new PartitionSessionClosedEventImpl(partition);
// partition close event doesn't use partition's executors
controlExecutor.execute(() -> session.getHandler().onPartitionClosed(closedEvent));
controlExecutor.execute(() -> handler.onPartitionClosed(closedEvent));
}
}

Expand All @@ -227,7 +228,7 @@ public void start(long committed, OffsetsRange offsets) {
// partition start event doesn't use partition's executors
controlExecutor.execute(() -> {
if (state.compareAndSet(State.CREATED, State.INITED)) {
session.getHandler().onPartitionStarted(new StartEvent(committed, offsets));
handler.onPartitionStarted(new StartEvent(committed, offsets));
return;
}
logger.info("[{}] skipped start confirmation event because is already {}", traceID, state.get());
Expand All @@ -251,11 +252,11 @@ public void stop(long committed) {

// partition close event doesn't use partition's executors
PartitionSessionClosedEventImpl event = new PartitionSessionClosedEventImpl(partition);
session.getConfig().getControlExecutor().execute(() -> session.getHandler().onPartitionClosed(event));
controlExecutor.execute(() -> handler.onPartitionClosed(event));
return;
}
if (state.compareAndSet(State.STARTED, State.PRE_STOPPED)) {
session.getHandler().onPartitionStopped(new StopEvent(committed));
handler.onPartitionStopped(new StopEvent(committed));
return;
}
logger.warn("[{}] skipped onStop because the partition session is already {}", traceID, state.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import tech.ydb.core.metrics.Meter;
import tech.ydb.proto.topic.YdbTopic;
import tech.ydb.topic.description.Codec;
import tech.ydb.topic.description.CodecRegistry;
Expand All @@ -32,22 +31,15 @@ class ReadPartitionDecoder {
private final PartitionSession partition;
private final MessageCommitter committer;
private final MessageDecoder decoder;
private final ReaderMetrics metrics;
private final Runnable readyHandler;

private final Queue<EncodedMessage> allocatedMessages = new ConcurrentLinkedQueue<>();
private volatile boolean isStopped = false;

ReadPartitionDecoder(String traceId, MessageDecoder decoder, PartitionSession partition,
MessageCommitter committer, Runnable readyHandler) {
this(traceId, decoder, partition, committer, readyHandler, new ReaderMetrics(Meter.NOOP, null, null));
}

ReadPartitionDecoder(String traceId, MessageDecoder decoder, PartitionSession partition,
MessageCommitter committer, Runnable readyHandler, ReaderMetrics metrics) {
this.traceID = traceId;
this.decoder = decoder;
this.metrics = metrics;
this.partition = partition;
this.committer = committer;
this.readyHandler = readyHandler;
Expand Down Expand Up @@ -179,7 +171,7 @@ public long allocate() {
return uncompressedSize;
}

public void decode(CodecRegistry registry) {
public void decode(CodecRegistry registry, ReaderMetrics metrics) {
if (isStopped) {
release();
return;
Expand Down
20 changes: 11 additions & 9 deletions topic/src/main/java/tech/ydb/topic/read/impl/ReadSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public interface Handler {
private final ReadConfig config;
private final MessageDecoder decoder;
private final BufferManager bufferManager;
private final ReaderMetrics metrics;
private final Handler handler;

private final Map<Long, ReadPartition> partitions = new ConcurrentHashMap<>();
Expand All @@ -66,6 +67,7 @@ public ReadSession(String id, GrpcReadWriteStream<FromServer, FromClient> stream
this.config = config;
this.decoder = new MessageDecoder(config);
this.bufferManager = new BufferManager(id, config.getMaxMemoryUsageBytes(), new ReadRequest());
this.metrics = config.getMetrics();
this.handler = handler;
}

Expand Down Expand Up @@ -124,7 +126,7 @@ public boolean commitOffsets(PartitionSession session, List<OffsetsRange> ranges
.build();

for (OffsetsRange range : rangesToCommit) {
config.getMetrics().reportCommitQueued(session.getId(), range.getEnd(),
metrics.reportCommitQueued(session.getId(), range.getEnd(),
range.getEnd() - range.getStart(), session.getPath());
}
send(FromClient.newBuilder().setCommitOffsetRequest(req).build());
Expand All @@ -144,7 +146,7 @@ public void sendStartPartition(PartitionSession ps, Long readFrom, Long commitTo
}
if (commitTo != null) {
resp.setCommitOffset(commitTo);
config.getMetrics().updateCommittedOffset(ps.getId(), commitTo);
metrics.updateCommittedOffset(ps.getId(), commitTo);
}

send(YdbTopic.StreamReadMessage.FromClient.newBuilder().setStartPartitionSessionResponse(resp.build()).build());
Expand All @@ -164,7 +166,7 @@ public void sendStopPartition(PartitionSession ps) {

ReadPartition partition = partitions.remove(ps.getId());
if (partition != null) {
config.getMetrics().removePartition(ps.getId());
metrics.removePartition(ps.getId());
partition.close();
}
}
Expand All @@ -190,7 +192,7 @@ public void removeLocks(PartitionSession ps, List<TopicPartition> childs) {

public void onInit(YdbTopic.StreamReadMessage.InitResponse response) {
bufferManager.init(response.getSessionId());
config.getMetrics().register(bufferManager::getCreditBalanceBytes);
metrics.register(bufferManager::getCreditBalanceBytes);
}

public void onStartPartition(YdbTopic.StreamReadMessage.StartPartitionSessionRequest req) {
Expand Down Expand Up @@ -219,7 +221,7 @@ public void onStartPartition(YdbTopic.StreamReadMessage.StartPartitionSessionReq
return;
}

config.getMetrics().addPartition(psid, committed, ps.getPath());
metrics.addPartition(psid, committed, ps.getPath());
partition.start(committed, offsets);
TopicPartition lockKey = new TopicPartition(ps);
if (locks.computeIfPresent(lockKey, (key, lock) -> lock.bind(partition)) != null) {
Expand Down Expand Up @@ -258,7 +260,7 @@ public void onClosePartition(long psid) {
}

logger.info("[{}] Received force StopPartitionSessionRequest for {} ", debugId, partition.getPartition());
config.getMetrics().removePartition(psid);
metrics.removePartition(psid);
partition.close();
}

Expand All @@ -267,7 +269,7 @@ public void onStopPartition(YdbTopic.StreamReadMessage.StopPartitionSessionReque
long psid = request.getPartitionSessionId();
ReadPartition partition = partitions.get(psid);
if (partition != null) {
config.getMetrics().updateCommittedOffset(psid, committedOffset);
metrics.updateCommittedOffset(psid, committedOffset);
partition.stop(committedOffset);
return;
}
Expand All @@ -281,7 +283,7 @@ public void onStopPartition(YdbTopic.StreamReadMessage.StopPartitionSessionReque

public void onRead(YdbTopic.StreamReadMessage.ReadResponse response) {
logger.debug("[{}] Received ReadResponse of {} bytes", debugId, response.getBytesSize());
config.getMetrics().reportReceivedBytes(response.getBytesSize());
metrics.reportReceivedBytes(response.getBytesSize());
bufferManager.allocate(response.getBytesSize(), response.getPartitionDataList());

for (YdbTopic.StreamReadMessage.ReadResponse.PartitionData data: response.getPartitionDataList()) {
Expand All @@ -308,7 +310,7 @@ public void onCommitOffset(YdbTopic.StreamReadMessage.CommitOffsetResponse respo
continue;
}

config.getMetrics().reportCommitAcknowledged(offset.getPartitionSessionId(),
metrics.reportCommitAcknowledged(offset.getPartitionSessionId(),
offset.getCommittedOffset(), partition.getPartition().getPath());
partition.confirmCommittedOffset(offset.getCommittedOffset());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.slf4j.LoggerFactory;

import tech.ydb.core.Status;
import tech.ydb.topic.settings.WriterSettings;
import tech.ydb.topic.write.QueueOverflowException;

/**
Expand All @@ -30,11 +29,11 @@ public class BufferManager {

private volatile Status closed = null;

public BufferManager(String id, WriterSettings settings) {
public BufferManager(String id, long bufferMaxSize, int bufferMaxMessageCount) {
this.debugId = id;

this.maxCount = settings.getMaxSendBufferMessagesCount();
this.bufferMaxSize = settings.getMaxSendBufferMemorySize();
this.maxCount = bufferMaxMessageCount;
this.bufferMaxSize = bufferMaxSize;

this.blockBitsCount = calculateBlockSize(bufferMaxSize);

Expand Down
50 changes: 50 additions & 0 deletions topic/src/main/java/tech/ydb/topic/write/impl/WriteConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package tech.ydb.topic.write.impl;

import java.util.concurrent.Executor;

import tech.ydb.topic.description.Codec;
import tech.ydb.topic.description.CodecRegistry;
import tech.ydb.topic.settings.WriterSettings;

/**
*
* @author Aleksandr Gorshenin {@literal <[email protected]>}
*/
public class WriteConfig {
private final long bufferMaxSize;
private final int bufferMaxMessagesCount;
private final Codec codec;
private final Executor compressor;
private final WriterMetrics metrics;

public WriteConfig(CodecRegistry codecRegistry, Executor compressor, WriterSettings settings) {
this.bufferMaxSize = settings.getMaxSendBufferMemorySize();
this.bufferMaxMessagesCount = settings.getMaxSendBufferMessagesCount();
this.compressor = compressor;
this.metrics = new WriterMetrics(settings.getMeter(), settings.getTopicPath(), settings.getWriterName());
this.codec = codecRegistry.getCodec(settings.getCodec());
if (codec == null) {
throw new IllegalArgumentException("Unsupported codec: " + settings.getCodec());
}
}

public long getBufferMaxSize() {
return bufferMaxSize;
}

public int getBufferMaxMessagesCount() {
return bufferMaxMessagesCount;
}

public Executor getCompressor() {
return compressor;
}

public Codec getCodec() {
return codec;
}

WriterMetrics getMetrics() {
return metrics;
}
}
14 changes: 10 additions & 4 deletions topic/src/main/java/tech/ydb/topic/write/impl/WriterImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public class WriterImpl {

private final String debugId;
private final WriterQueue writeQueue;
private final WriterMetrics metrics;
private final WriteSession stream;
private final Runnable sendTask = new SerialRunnable(new SendTask());

Expand All @@ -58,11 +57,12 @@ public WriterImpl(TopicRpc topicRpc, WriterSettings settings, Executor compressi
public WriterImpl(TopicRpc topicRpc, WriteStreamFactory factory, WriterSettings settings,
Executor compressionExecutor, @Nonnull CodecRegistry codecRegistry) {
this.debugId = DebugTools.createDebugId(settings.getLogPrefix());
this.metrics = new WriterMetrics(settings.getMeter(), settings.getTopicPath(), settings.getWriterName());
this.stream = new WriteSession(debugId, factory, settings, topicRpc.getScheduler(), new ListenerImpl());
this.writeQueue = new WriterQueue(debugId, settings, metrics, codecRegistry, compressionExecutor, sendTask);

WriteConfig config = new WriteConfig(codecRegistry, compressionExecutor, settings);
Observability.reportMetricsUsage(settings.getMeter());

this.stream = new WriteSession(debugId, factory, settings, topicRpc.getScheduler(), new ListenerImpl(config));
this.writeQueue = new WriterQueue(debugId, config, sendTask);
logger.info("Writer with id {} created for topic \"{}\" with producerId \"{}\" and messageGroupId \"{}\"",
debugId, settings.getTopicPath(), settings.getProducerId(), settings.getMessageGroupId());
}
Expand Down Expand Up @@ -174,6 +174,12 @@ public CompletableFuture<WriteAck> nonblockingSend(Message msg, SendSettings set
}

private class ListenerImpl implements WriteSession.Listener {
private final WriterMetrics metrics;

ListenerImpl(WriteConfig config) {
this.metrics = config.getMetrics();
}

@Override
public void onStart(long lastSeqNo, String sessionId) {
// resend all sent messages in writing queue
Expand Down
Loading