-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Event processor - API review (#4582)
* Support context for ConfigurationAsyncClient APIs * Initial commit to event processor * add comments * Update checkpoint * Update javadocs * address cr comments * address cr comments * Bug fixes * Add more sample scenarios and cleanup dead code * Fix checkstyle * Move InMemoryPartitionManager to main package * Add javadocs * Update instanceId to ownerId * Fix checkstyle * More javadocs
- Loading branch information
Showing
13 changed files
with
1,160 additions
and
0 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
...nthubs/azure-eventhubs/src/main/java/com/azure/messaging/eventhubs/CheckpointManager.java
This file contains 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,87 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.messaging.eventhubs; | ||
|
||
import com.azure.messaging.eventhubs.models.Checkpoint; | ||
import com.azure.messaging.eventhubs.models.PartitionContext; | ||
|
||
import java.util.concurrent.atomic.AtomicReference; | ||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* The checkpoint manager that clients should use to update checkpoints to track progress of events processed. Each | ||
* instance of a {@link PartitionProcessor} will be provided with it's own instance of a CheckpointManager. | ||
*/ | ||
public class CheckpointManager { | ||
|
||
private final PartitionContext partitionContext; | ||
private final PartitionManager partitionManager; | ||
private final AtomicReference<String> eTag; | ||
private final String ownerId; | ||
|
||
/** | ||
* Creates a new checkpoint manager which {@link PartitionProcessor} can use to update checkpoints. | ||
* | ||
* @param ownerId The event processor identifier that is responsible for updating checkpoints. | ||
* @param partitionContext The partition context providing necessary partition and event hub information for updating | ||
* checkpoints. | ||
* @param partitionManager The {@link PartitionManager} implementation that will be store the checkpoint information. | ||
* @param eTag The last known ETag stored in {@link PartitionManager} for this partition. When the update checkpoint | ||
* is called from this CheckpointManager, this ETag will be used to provide <a href="https://en.wikipedia.org/wiki/Optimistic_concurrency_control">optimistic | ||
* concurrency</a>. | ||
*/ | ||
CheckpointManager(String ownerId, PartitionContext partitionContext, PartitionManager partitionManager, | ||
String eTag) { | ||
this.ownerId = ownerId; | ||
this.partitionContext = partitionContext; | ||
this.partitionManager = partitionManager; | ||
this.eTag = new AtomicReference<>(eTag); | ||
} | ||
|
||
/** | ||
* Updates the checkpoint for this partition using the event data. This will serve as the last known successfully | ||
* processed event in this partition if the update is successful. | ||
* | ||
* @param eventData The event data to use for updating the checkpoint. | ||
* @return a representation of deferred execution of this call. | ||
*/ | ||
public Mono<Void> updateCheckpoint(EventData eventData) { | ||
String previousETag = this.eTag.get(); | ||
Checkpoint checkpoint = new Checkpoint() | ||
.consumerGroupName(partitionContext.consumerGroupName()) | ||
.eventHubName(partitionContext.eventHubName()) | ||
.ownerId(ownerId) | ||
.partitionId(partitionContext.partitionId()) | ||
.sequenceNumber(eventData.sequenceNumber()) | ||
.offset(eventData.offset()) | ||
.eTag(previousETag); | ||
return this.partitionManager.updateCheckpoint(checkpoint) | ||
.map(eTag -> this.eTag.compareAndSet(previousETag, eTag)) | ||
.then(); | ||
} | ||
|
||
/** | ||
* Updates a checkpoint using the given offset and sequence number. This will serve as the last known successfully | ||
* processed event in this partition if the update is successful. | ||
* | ||
* @param sequenceNumber The sequence number to update the checkpoint. | ||
* @param offset The offset to update the checkpoint. | ||
* @return a representation of deferred execution of this call. | ||
*/ | ||
public Mono<Void> updateCheckpoint(long sequenceNumber, String offset) { | ||
String previousETag = this.eTag.get(); | ||
Checkpoint checkpoint = new Checkpoint() | ||
.consumerGroupName(partitionContext.consumerGroupName()) | ||
.eventHubName(partitionContext.eventHubName()) | ||
.ownerId(ownerId) | ||
.partitionId(partitionContext.partitionId()) | ||
.sequenceNumber(sequenceNumber) | ||
.offset(offset) | ||
.eTag(previousETag); | ||
|
||
return this.partitionManager.updateCheckpoint(checkpoint) | ||
.map(eTag -> this.eTag.compareAndSet(previousETag, eTag)) | ||
.then(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
sdk/eventhubs/azure-eventhubs/src/main/java/com/azure/messaging/eventhubs/CloseReason.java
This file contains 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,27 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.messaging.eventhubs; | ||
|
||
/** | ||
* Enumeration of all possible reasons a {@link PartitionProcessor} may be closed. | ||
*/ | ||
public enum CloseReason { | ||
/** | ||
* If another event processor instance stole the ownership of a partition, this reason will be provided to {@link | ||
* PartitionProcessor#close(CloseReason)}. | ||
*/ | ||
LOST_PARTITION_OWNERSHIP, | ||
|
||
/** | ||
* If the event processor is shutting down by calling {@link EventProcessorAsyncClient#stop()}, the {@link | ||
* PartitionProcessor#close(CloseReason)} will be called with this reason. | ||
*/ | ||
EVENT_PROCESSOR_SHUTDOWN, | ||
|
||
/** | ||
* If a non-retryable exception occured when receiving events from Event Hub, this reason will be provided when {@link | ||
* PartitionProcessor#close(CloseReason)} is called. | ||
*/ | ||
EVENT_HUB_EXCEPTION | ||
} |
This file contains 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
Oops, something went wrong.