gnodet-bot commented on code in PR #30805: URL: https://github.com/apache/camel/pull/30805#discussion_r4702547035
########## components/camel-azure/camel-azure-eventhubs/src/main/java/org/apache/camel/component/azure/eventhubs/EventHubsPartitionCheckpoints.java: ########## @@ -0,0 +1,230 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.azure.eventhubs; + +import java.util.Map; +import java.util.NavigableMap; +import java.util.TreeMap; +import java.util.concurrent.locks.Lock; + +import com.azure.messaging.eventhubs.models.EventContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import reactor.core.publisher.Mono; + +/** + * The checkpoint state of one partition owned by an {@link EventHubsConsumer}: the events delivered to the route and + * not yet passed by the checkpoint, and the checkpoint writes of the partition. + * <p> + * The exchanges of a partition can complete in any order (any asynchronous step in the route), while the checkpoint + * store keeps one position per partition and the next owner of the partition resumes after it. So a checkpoint is + * written only for an event below which every delivered event has completed (successfully or not), only when it is + * after the last checkpoint written, and only when the write before it has answered: the stored checkpoint never passes + * an event still in flight and never goes back. + * <p> + * Used under the lock of the consumer only (the answers of the writes take it). + */ +final class EventHubsPartitionCheckpoints { + + private static final Logger LOG = LoggerFactory.getLogger(EventHubsPartitionCheckpoints.class); + + private final Lock lock; + // the delivered events by sequence number, until every event before them has completed; the events completed one + // after the other are one entry, under the last of them, so an event that never completes holds one entry per run + // of events completed after it, not one per event + private final NavigableMap<Long, Delivered> delivered = new TreeMap<>(); + private long lastDelivered = -1; + // the events in delivered that completed successfully: they wait for an earlier event still in flight + private int succeededAhead; + // the last event below which every delivered event has completed: the furthest a checkpoint may go + private EventContext completedUpTo; + // the sequence number of the last checkpoint written (or waiting to be written), and of the last one stored + private long written = -1; + private long stored = -1; + // a write is in flight; the next checkpoint waits for its answer + private boolean writing; + private EventContext waiting; + private Runnable waitingOnError; + // the partition was lost: its checkpoints belong to its new owner + private boolean closed; + + EventHubsPartitionCheckpoints(Lock lock) { + this.lock = lock; + } + + /** + * The event is delivered to the route. The SDK delivers the events of a partition one at a time, in order. + * + * @return false if the event is not after the last one delivered: the SDK delivers the partition again from the + * stored checkpoint, so this state no longer applies + */ + boolean delivered(EventContext eventContext) { + final long sequenceNumber = sequenceNumber(eventContext); + if (sequenceNumber <= lastDelivered) { + return false; + } + lastDelivered = sequenceNumber; + delivered.put(sequenceNumber, new Delivered(eventContext)); + return true; + } + + /** + * The exchange of the event is done, successfully or not. + * + * @return false if the event was not delivered by this owner of the partition (it was delivered before the consumer + * was stopped or lost the partition), so it must not move the checkpoint + */ + boolean completed(EventContext eventContext, boolean succeeded) { + final long sequenceNumber = sequenceNumber(eventContext); + final Delivered event = delivered.get(sequenceNumber); + if (event == null || event.eventContext != eventContext || event.completed) { + return false; + } + event.completed = true; + if (succeeded) { + event.succeeded = 1; + succeededAhead++; + } + // join the completed events next to it: a checkpoint can only use the last event of such a run + final Map.Entry<Long, Delivered> before = delivered.lowerEntry(sequenceNumber); + if (before != null && before.getValue().completed) { + event.succeeded += before.getValue().succeeded; + delivered.remove(before.getKey()); + } + final Map.Entry<Long, Delivered> after = delivered.higherEntry(sequenceNumber); + if (after != null && after.getValue().completed) { + after.getValue().succeeded += event.succeeded; + delivered.remove(sequenceNumber); + } + Map.Entry<Long, Delivered> first; + while ((first = delivered.firstEntry()) != null && first.getValue().completed) { + delivered.pollFirstEntry(); + completedUpTo = first.getValue().eventContext; + succeededAhead -= first.getValue().succeeded; + } + return true; + } + + /** + * The number of events that completed successfully after an event still in flight: a checkpoint now does not cover + * them. + */ + int succeededAhead() { + return succeededAhead; + } + + /** + * Whether a checkpoint now would move the checkpoint of the partition forward. + */ + boolean canCheckpoint() { + return !closed && completedUpTo != null && sequenceNumber(completedUpTo) > written; + } + + /** + * Checkpoints the partition up to the last event below which every delivered event has completed, if that is after + * the last checkpoint written. When a write is in flight, the checkpoint is written when it answers (a later + * checkpoint replaces a waiting one), so the writes land in order. + * + * @param onError called if the write of this checkpoint fails + * @return false if there is nothing to checkpoint + */ + boolean checkpoint(Runnable onError) { + if (!canCheckpoint()) { + return false; + } + written = sequenceNumber(completedUpTo); + if (writing) { + waiting = completedUpTo; + waitingOnError = onError; + } else { + write(completedUpTo, onError); + } + return true; + } + + /** + * The partition was lost, or is delivered again: the checkpoints waiting to be written are dropped, and nothing + * more is written. + */ + void close() { + closed = true; + waiting = null; + waitingOnError = null; + delivered.clear(); + } Review Comment: 🔧 **`close()` retains the `completedUpTo` reference** After `close()`, `canCheckpoint()` always returns `false`, so `completedUpTo` is never read again — but it pins the `EventContext` (and its `EventData`) until the `EventHubsPartitionCheckpoints` instance is GC'd. Null it on close to release the reference promptly: ```suggestion void close() { closed = true; waiting = null; waitingOnError = null; completedUpTo = null; delivered.clear(); } ``` ########## components/camel-azure/camel-azure-eventhubs/src/main/java/org/apache/camel/component/azure/eventhubs/EventHubsPartitionCheckpoints.java: ########## @@ -0,0 +1,230 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.azure.eventhubs; + +import java.util.Map; +import java.util.NavigableMap; +import java.util.TreeMap; +import java.util.concurrent.locks.Lock; + +import com.azure.messaging.eventhubs.models.EventContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import reactor.core.publisher.Mono; + +/** + * The checkpoint state of one partition owned by an {@link EventHubsConsumer}: the events delivered to the route and + * not yet passed by the checkpoint, and the checkpoint writes of the partition. + * <p> + * The exchanges of a partition can complete in any order (any asynchronous step in the route), while the checkpoint + * store keeps one position per partition and the next owner of the partition resumes after it. So a checkpoint is + * written only for an event below which every delivered event has completed (successfully or not), only when it is + * after the last checkpoint written, and only when the write before it has answered: the stored checkpoint never passes + * an event still in flight and never goes back. + * <p> + * Used under the lock of the consumer only (the answers of the writes take it). + */ +final class EventHubsPartitionCheckpoints { + + private static final Logger LOG = LoggerFactory.getLogger(EventHubsPartitionCheckpoints.class); + + private final Lock lock; + // the delivered events by sequence number, until every event before them has completed; the events completed one + // after the other are one entry, under the last of them, so an event that never completes holds one entry per run + // of events completed after it, not one per event + private final NavigableMap<Long, Delivered> delivered = new TreeMap<>(); + private long lastDelivered = -1; + // the events in delivered that completed successfully: they wait for an earlier event still in flight + private int succeededAhead; + // the last event below which every delivered event has completed: the furthest a checkpoint may go + private EventContext completedUpTo; + // the sequence number of the last checkpoint written (or waiting to be written), and of the last one stored + private long written = -1; + private long stored = -1; + // a write is in flight; the next checkpoint waits for its answer + private boolean writing; + private EventContext waiting; + private Runnable waitingOnError; + // the partition was lost: its checkpoints belong to its new owner + private boolean closed; + + EventHubsPartitionCheckpoints(Lock lock) { + this.lock = lock; + } + + /** + * The event is delivered to the route. The SDK delivers the events of a partition one at a time, in order. + * + * @return false if the event is not after the last one delivered: the SDK delivers the partition again from the + * stored checkpoint, so this state no longer applies + */ + boolean delivered(EventContext eventContext) { + final long sequenceNumber = sequenceNumber(eventContext); + if (sequenceNumber <= lastDelivered) { + return false; + } + lastDelivered = sequenceNumber; + delivered.put(sequenceNumber, new Delivered(eventContext)); + return true; + } + + /** + * The exchange of the event is done, successfully or not. + * + * @return false if the event was not delivered by this owner of the partition (it was delivered before the consumer + * was stopped or lost the partition), so it must not move the checkpoint + */ + boolean completed(EventContext eventContext, boolean succeeded) { + final long sequenceNumber = sequenceNumber(eventContext); + final Delivered event = delivered.get(sequenceNumber); + if (event == null || event.eventContext != eventContext || event.completed) { Review Comment: 💡 **Identity comparison — add an inline comment to prevent future `equals()` "fix"** `event.eventContext != eventContext` uses identity, not `equals()`. This is intentional: a re-delivered event at the same sequence number after a rebalance has a different `EventContext` instance. Future contributors will read this as an oversight and "fix" it. A one-line comment prevents the regression: ```suggestion // identity check: a re-delivered event at the same sequence number has a distinct EventContext if (event == null || event.eventContext != eventContext || event.completed) { ``` ########## components/camel-azure/camel-azure-eventhubs/src/main/java/org/apache/camel/component/azure/eventhubs/EventHubsPartitionCheckpoints.java: ########## @@ -0,0 +1,230 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.azure.eventhubs; + +import java.util.Map; +import java.util.NavigableMap; +import java.util.TreeMap; +import java.util.concurrent.locks.Lock; + +import com.azure.messaging.eventhubs.models.EventContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import reactor.core.publisher.Mono; + +/** + * The checkpoint state of one partition owned by an {@link EventHubsConsumer}: the events delivered to the route and + * not yet passed by the checkpoint, and the checkpoint writes of the partition. + * <p> + * The exchanges of a partition can complete in any order (any asynchronous step in the route), while the checkpoint + * store keeps one position per partition and the next owner of the partition resumes after it. So a checkpoint is + * written only for an event below which every delivered event has completed (successfully or not), only when it is + * after the last checkpoint written, and only when the write before it has answered: the stored checkpoint never passes + * an event still in flight and never goes back. + * <p> + * Used under the lock of the consumer only (the answers of the writes take it). + */ +final class EventHubsPartitionCheckpoints { + + private static final Logger LOG = LoggerFactory.getLogger(EventHubsPartitionCheckpoints.class); + + private final Lock lock; + // the delivered events by sequence number, until every event before them has completed; the events completed one + // after the other are one entry, under the last of them, so an event that never completes holds one entry per run + // of events completed after it, not one per event + private final NavigableMap<Long, Delivered> delivered = new TreeMap<>(); + private long lastDelivered = -1; + // the events in delivered that completed successfully: they wait for an earlier event still in flight + private int succeededAhead; + // the last event below which every delivered event has completed: the furthest a checkpoint may go + private EventContext completedUpTo; + // the sequence number of the last checkpoint written (or waiting to be written), and of the last one stored + private long written = -1; + private long stored = -1; + // a write is in flight; the next checkpoint waits for its answer + private boolean writing; + private EventContext waiting; + private Runnable waitingOnError; + // the partition was lost: its checkpoints belong to its new owner + private boolean closed; + + EventHubsPartitionCheckpoints(Lock lock) { + this.lock = lock; + } + + /** + * The event is delivered to the route. The SDK delivers the events of a partition one at a time, in order. + * + * @return false if the event is not after the last one delivered: the SDK delivers the partition again from the + * stored checkpoint, so this state no longer applies + */ + boolean delivered(EventContext eventContext) { + final long sequenceNumber = sequenceNumber(eventContext); + if (sequenceNumber <= lastDelivered) { + return false; + } + lastDelivered = sequenceNumber; + delivered.put(sequenceNumber, new Delivered(eventContext)); + return true; + } + + /** + * The exchange of the event is done, successfully or not. + * + * @return false if the event was not delivered by this owner of the partition (it was delivered before the consumer + * was stopped or lost the partition), so it must not move the checkpoint + */ + boolean completed(EventContext eventContext, boolean succeeded) { + final long sequenceNumber = sequenceNumber(eventContext); + final Delivered event = delivered.get(sequenceNumber); + if (event == null || event.eventContext != eventContext || event.completed) { + return false; + } + event.completed = true; + if (succeeded) { + event.succeeded = 1; + succeededAhead++; + } + // join the completed events next to it: a checkpoint can only use the last event of such a run + final Map.Entry<Long, Delivered> before = delivered.lowerEntry(sequenceNumber); + if (before != null && before.getValue().completed) { + event.succeeded += before.getValue().succeeded; + delivered.remove(before.getKey()); + } + final Map.Entry<Long, Delivered> after = delivered.higherEntry(sequenceNumber); + if (after != null && after.getValue().completed) { + after.getValue().succeeded += event.succeeded; + delivered.remove(sequenceNumber); + } + Map.Entry<Long, Delivered> first; + while ((first = delivered.firstEntry()) != null && first.getValue().completed) { + delivered.pollFirstEntry(); + completedUpTo = first.getValue().eventContext; + succeededAhead -= first.getValue().succeeded; + } + return true; + } + + /** + * The number of events that completed successfully after an event still in flight: a checkpoint now does not cover + * them. + */ + int succeededAhead() { + return succeededAhead; + } + + /** + * Whether a checkpoint now would move the checkpoint of the partition forward. + */ + boolean canCheckpoint() { + return !closed && completedUpTo != null && sequenceNumber(completedUpTo) > written; + } + + /** + * Checkpoints the partition up to the last event below which every delivered event has completed, if that is after + * the last checkpoint written. When a write is in flight, the checkpoint is written when it answers (a later + * checkpoint replaces a waiting one), so the writes land in order. + * + * @param onError called if the write of this checkpoint fails + * @return false if there is nothing to checkpoint + */ + boolean checkpoint(Runnable onError) { + if (!canCheckpoint()) { + return false; + } + written = sequenceNumber(completedUpTo); + if (writing) { + waiting = completedUpTo; + waitingOnError = onError; + } else { + write(completedUpTo, onError); + } + return true; + } + + /** + * The partition was lost, or is delivered again: the checkpoints waiting to be written are dropped, and nothing + * more is written. + */ + void close() { + closed = true; + waiting = null; + waitingOnError = null; + delivered.clear(); + } + + private void write(EventContext eventContext, Runnable onError) { + final long sequenceNumber = sequenceNumber(eventContext); + final Mono<Void> update; + try { + update = eventContext.updateCheckpointAsync(); + } catch (RuntimeException e) { + written = stored; + throw e; + } + writing = true; + update.subscribe(unused -> LOG.debug("Processed one event..."), + error -> { + LOG.warn("Error when updating Checkpoint: {}", error.getMessage(), error); + answered(sequenceNumber, false); + if (onError != null) { + onError.run(); + } + }, Review Comment: ⚠️ **Race: `onError.run()` executes outside the lock** `answered(sequenceNumber, false)` acquires and releases the consumer lock, then `onError.run()` calls `processedEvents.addAndGet(covered)` with no lock held. A concurrent `processCommit` on the consumer thread can call `processedEvents.getAndSet(ahead)` between the two, then the `addAndGet(covered)` runs on the stale base, inflating the counter. The checkpoint *position* (`completedUpTo`) is unaffected — this cannot cause data loss or backward movement — but the batch-size trigger can fire one checkpoint early. Fix: pass `onError` into `answered()` and invoke it *inside* the `try` block before `lock.unlock()`, so both the state update and the counter restore are atomic under the same lock acquisition. ```suggestion error -> { LOG.warn("Error when updating Checkpoint: {}", error.getMessage(), error); answered(sequenceNumber, false, onError); }, ``` (and add `Runnable onError` parameter to `answered()`, calling it before `lock.unlock()`) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
