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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package tech.ydb.topic.read.events;

import tech.ydb.topic.read.PartitionSession;

/**
* @author Nikolay Perfilov
*/
public interface PartitionSessionEndedEvent {
PartitionSession getPartitionSession();
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ default void onStopPartitionSession(StopPartitionSessionEvent event) {

default void onPartitionSessionClosed(PartitionSessionClosedEvent event) { }

default void onPartitionSessionEnded(PartitionSessionEndedEvent event) { }

default void onReaderClosed(ReaderClosedEvent event) { }

default void onSessionStarted(SessionStartedEvent event) { }
Expand Down
19 changes: 11 additions & 8 deletions topic/src/main/java/tech/ydb/topic/read/impl/AsyncReaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import tech.ydb.topic.read.events.CommitOffsetAcknowledgementEvent;
import tech.ydb.topic.read.events.DataReceivedEvent;
import tech.ydb.topic.read.events.PartitionSessionClosedEvent;
import tech.ydb.topic.read.events.PartitionSessionEndedEvent;
import tech.ydb.topic.read.events.ReadEventHandler;
import tech.ydb.topic.read.events.ReaderClosedEvent;
import tech.ydb.topic.read.events.StartPartitionSessionEvent;
Expand Down Expand Up @@ -148,15 +149,8 @@ private class AsyncHandler implements ReadSession.Handler {
@Override
public void onData(ReadSession.PartitionControl control, DataReceivedEvent event) {
try {
int messagesCount = event.getMessages().size();
long offsetStart = event.getMessages().get(0).getOffset();
long offsetEnd = event.getMessages().get(event.getMessages().size() - 1).getOffset();
logger.debug("[{}] DataReceivedEvent callback with {} message(s) (offsets {}-{}) is about "
+ "to be called...", debugId, messagesCount, offsetStart, offsetEnd);
config.getMetrics().reportDelivered(messagesCount, event.getPartitionSession().getPath());
config.getMetrics().reportDelivered(event.getMessages().size(), event.getPartitionSession().getPath());
eventHandler.onMessages(event);
logger.debug("[{}] DataReceivedEvent callback with {} message(s) (offsets {}-{}) "
+ "successfully finished", debugId, messagesCount, offsetStart, offsetEnd);
} catch (Throwable th) {
failSession(th, "onMessages");
} finally {
Expand Down Expand Up @@ -199,5 +193,14 @@ public void onPartitionClosed(PartitionSessionClosedEvent event) {
failSession(th, "onPartitionSessionClosed");
}
}

@Override
public void onPartitionEnded(PartitionSessionEndedEvent event) {
try {
eventHandler.onPartitionSessionEnded(event);
} catch (Throwable th) {
failSession(th, "onPartitionSessionEnded");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ public void releasePartition(Long id) {
}
}

public boolean hasPartition(Long id) {
PartitionBuffer part = partitions.get(id);
return part != null && !part.isEmpty();
}

// Thread safe
public void releaseRange(Long id, OffsetsRange range) {
PartitionBuffer part = partitions.get(id);
Expand Down Expand Up @@ -180,6 +185,10 @@ public void add(BatchBuffer range) {
batches.add(range);
}

public boolean isEmpty() {
return batches.isEmpty();
}

public long release(OffsetsRange range) {
long released = 0;

Expand Down
129 changes: 90 additions & 39 deletions topic/src/main/java/tech/ydb/topic/read/impl/ReadPartition.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import tech.ydb.topic.read.impl.events.CommitOffsetAcknowledgementEventImpl;
import tech.ydb.topic.read.impl.events.DataReceivedEventImpl;
import tech.ydb.topic.read.impl.events.PartitionSessionClosedEventImpl;
import tech.ydb.topic.read.impl.events.PartitionSessionEndedEventImpl;
import tech.ydb.topic.read.impl.events.StartPartitionSessionEventImpl;
import tech.ydb.topic.read.impl.events.StopPartitionSessionEventImpl;
import tech.ydb.topic.settings.StartPartitionSessionSettings;
Expand Down Expand Up @@ -58,6 +59,8 @@ private enum State {

private final AtomicReference<State> state = new AtomicReference<>(State.CREATED);
private final AtomicReference<CommitOffsetAcknowledgementEvent> commitOffsetAck = new AtomicReference<>(null);
private final AtomicReference<PartitionSessionEndedEventImpl> partitionEnd = new AtomicReference<>(null);
private volatile boolean isPaused = true;

ReadPartition(String traceID, ReadSession session, PartitionSession partition, long lastCommittedOffset) {
this.traceID = traceID;
Expand All @@ -74,22 +77,31 @@ private enum State {
this.committer = new ReadPartitionCommitter(traceID, session, partition, lastCommittedOffset);
this.decoder = new ReadPartitionDecoder(traceID, sessionDecoder, partition, committer, this::sendDataToReaders);
this.queue = new ReadPartitionQueue(traceID, decoder, config.getMaxBatchSize(), lastCommittedOffset);

logger.info("[{}] started for {}", traceID, partition);
}

@Override
public boolean isActive() {
return State.IS_ACTIVE.contains(state.get());
}

public PartitionSession getPartition() {
return partition;
}

@Override
public void confirmRangeProcessed(OffsetsRange range) {
session.getBufferManager().releaseRange(partition.getId(), range);
decoder.releaseRange(range);
session.getBufferManager().releaseRange(partition.getId(), range);
sendDataToReaders();
}

public PartitionSession getPartition() {
return partition;
public void confirmPartitionEnded(List<TopicPartition> childs) {
PartitionSessionEndedEventImpl event = new PartitionSessionEndedEventImpl(partition, childs);
logger.info("[{}] got EndPartitionSession with child partitions {}", traceID, event.getChildsString());
partitionEnd.set(event);
sendDataToReaders();
}

public void confirmCommittedOffset(long committedOffset) {
Expand All @@ -99,40 +111,75 @@ public void confirmCommittedOffset(long committedOffset) {
sendDataToReaders();
}

private void sendCommitOffsetAck() {
CommitOffsetAcknowledgementEvent event = commitOffsetAck.getAndSet(null);
if (event != null) {
session.getHandler().onCommitAck(event);
public void unpause() {
if (state.get() != State.CREATED) {
logger.info("[{}] was unpaused and ready to send data", traceID);
}
isPaused = false;
sendDataToReaders();
}

public boolean addBatches(List<YdbTopic.StreamReadMessage.ReadResponse.Batch> batchList) {
if (!isActive()) {
return false;
}
queue.addBatches(batchList);
long messagesCount = 0;
for (YdbTopic.StreamReadMessage.ReadResponse.Batch batch : batchList) {
messagesCount += batch.getMessageDataCount();
}

long messagesCount = batchList.stream()
.mapToInt(YdbTopic.StreamReadMessage.ReadResponse.Batch::getMessageDataCount)
.sum();
metrics.reportReceivedMessages(messagesCount, partition.getPath());
sendDataToReaders();

if (messagesCount > 0) {
queue.addBatches(batchList);
sendDataToReaders();
}

return isActive();
}

private void sendCommitOffsetAck() {
CommitOffsetAcknowledgementEvent event = commitOffsetAck.getAndSet(null);
if (event != null) {
session.getHandler().onCommitAck(event);
}
}

private void sendDataToReaders() {
dataExecutor.execute(() -> {
sendCommitOffsetAck(); // ack may be sent even state is not active
if (isPaused) {
return;
}

while (state.get() == State.STARTED || state.get() == State.PRE_STOPPED) {
List<Message> list = queue.getNextBatch();
if (list == null) {
return;
break;
}

DataReceivedEventImpl event = new DataReceivedEventImpl(partition, committer, list);

int messagesCount = event.getMessages().size();
long offsetStart = event.getMessages().get(0).getOffset();
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(this, event);
logger.debug("[{}] onData with {} message(s) (offsets {}-{}) successfully finished",
traceID, messagesCount, offsetStart, offsetEnd);

sendCommitOffsetAck();
}

if (!session.getBufferManager().hasPartition(partition.getId())) {
PartitionSessionEndedEventImpl event = partitionEnd.getAndSet(null);
if (event != null) {
logger.info("[{}] has finished processing and unpaused child partitions {}", traceID,
event.getChildsString());
session.getHandler().onPartitionEnded(event);
session.releaseLocks(partition, event.getChilds());
}
}
});
}

Expand All @@ -145,18 +192,23 @@ public void close() {
session.getBufferManager().releasePartition(partition.getId());
decoder.close();
committer.close();
logger.info("[{}] with state {} was stopped", traceID, old);
logger.info("[{}] with state {} was closed", traceID, old);
Comment thread
alex268 marked this conversation as resolved.
Resolved

PartitionSessionEndedEventImpl endEvent = partitionEnd.getAndSet(null);
if (endEvent != null) {
session.removeLocks(partition, endEvent.getChilds());
}

if (old != State.STOPPED && old != State.CREATED) {
PartitionSessionClosedEventImpl event = new PartitionSessionClosedEventImpl(partition);
PartitionSessionClosedEventImpl closedEvent = new PartitionSessionClosedEventImpl(partition);
// partition close event doesn't use partition's executors
controlExecutor.execute(() -> session.getHandler().onPartitionClosed(event));
controlExecutor.execute(() -> session.getHandler().onPartitionClosed(closedEvent));
}
}

public void start(long committed, OffsetsRange offsets) {
logger.info("[{}] Received StartPartitionSessionRequest for {} and consumer \"{}\" with committedOffset {} and "
+ "partitionOffsets {}", traceID, partition, session.getConfig().getConsumerName(), committed, offsets);
logger.info("[{}] got StartPartitionSessionRequest with committed offset {} and partition offsets {}",
traceID, committed, offsets);

committer.updateCommittedOffset(committed);
queue.updateLastReadOffset(committed);
Expand All @@ -166,16 +218,15 @@ public void start(long committed, OffsetsRange offsets) {
session.getHandler().onPartitionStarted(new StartEvent(committed, offsets));
return;
}
logger.info("[{}] skipped onStart event because the partition session is already {}", traceID, state.get());
logger.info("[{}] skipped start confirmation event because is already {}", traceID, state.get());
});
}

public void stop(long committed) {
logger.info("[{}] Received graceful StopPartitionSessionRequest for {} with committedOffset {}",
traceID, partition, committed);
logger.info("[{}] got graceful StopPartitionSessionRequest with committedOffset {}", traceID, committed);

if (state.compareAndSet(State.CREATED, State.STOPPED)) {
logger.info("[{}] was auto stopped because the partition is not initialized yet", traceID);
logger.info("[{}] stop was auto confirmed the partition is not initialized yet", traceID);
session.sendStopPartition(partition);
return;
}
Expand All @@ -195,7 +246,7 @@ public void stop(long committed) {
session.getHandler().onPartitionStopped(new StopEvent(committed));
return;
}
logger.warn("[{}] skipped onStop event because the partition session is already {}", traceID, state.get());
logger.warn("[{}] skipped onStop because the partition session is already {}", traceID, state.get());
});
}

Expand All @@ -209,21 +260,21 @@ public void confirm(StartPartitionSessionSettings settings) {
Long readFrom = settings != null ? settings.getReadOffset() : null;
Long commitTo = settings != null ? settings.getCommitOffset() : null;

String consumer = session.getConfig().getConsumerName();
if (state.compareAndSet(State.INITED, State.STARTED)) {
if (commitTo != null) {
committer.updateCommittedOffset(commitTo);
queue.updateLastReadOffset(commitTo); // avoid lags of commits
}

logger.info("[{}] Sending StartPartitionSessionResponse for {} and consumer \"{}\" with readOffset {} "
+ "and commitOffset {}", traceID, partition, consumer, readFrom, commitTo);
session.sendStartPartition(partition, readFrom, commitTo);
sendDataToReaders();
} else {
logger.warn("[{}] Need to send StartPartitionSessionResponse, but the partition session is already {}",
if (!state.compareAndSet(State.INITED, State.STARTED)) {
logger.warn("[{}] needs to send StartPartitionSessionResponse, but the partition session is already {}",
traceID, state.get());
return;
}

if (commitTo != null) {
committer.updateCommittedOffset(commitTo);
queue.updateLastReadOffset(commitTo); // avoid lags of commits
}

logger.info("[{}] sent StartPartitionSessionResponse with readOffset {} and commitOffset {}", traceID,
readFrom, commitTo);
session.sendStartPartition(partition, readFrom, commitTo);
sendDataToReaders();
}
}

Expand All @@ -235,10 +286,10 @@ private class StopEvent extends StopPartitionSessionEventImpl {
@Override
public void confirm() {
if (state.compareAndSet(State.PRE_STOPPED, State.STOPPED)) {
logger.info("[{}] Sending StopPartitionSessionResponse for {}", traceID, partition);
logger.info("[{}] sent StopPartitionSessionResponse", traceID);
session.sendStopPartition(partition);
} else {
logger.warn("[{}] Need to send StopPartitionSessionResponse, but the partition session is already {}",
logger.warn("[{}] needs to send StopPartitionSessionResponse, but the partition session is already {}",
traceID, partition);
}
}
Expand Down
Loading
Loading