Skip to content
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

CosmosDB: fix a regression in ChangeFeedProcessor when handling split events #22718

Merged
merged 3 commits into from
Jul 6, 2021
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
Expand Up @@ -271,13 +271,10 @@ public Mono<Void> release(Lease lease) {
this.requestOptionsFactory.createItemRequestOptions(lease),
serverLease ->
{
if (serverLease.getOwner() != null) {
if (!serverLease.getOwner().equalsIgnoreCase(lease.getOwner())) {
logger.info("Partition {} no need to release lease. The lease was already taken by another host '{}'.", lease.getLeaseToken(), serverLease.getOwner());
throw new LeaseLostException(lease);
}
if (serverLease.getOwner() != null && !serverLease.getOwner().equalsIgnoreCase(lease.getOwner())) {
logger.info("Partition {} no need to release lease. The lease was already taken by another host '{}'.", lease.getLeaseToken(), serverLease.getOwner());
throw new LeaseLostException(lease);
}

serverLease.setOwner(null);

return serverLease;
Expand Down Expand Up @@ -317,7 +314,7 @@ public Mono<Lease> renew(Lease lease) {
this.requestOptionsFactory.createItemRequestOptions(lease),
serverLease ->
{
if (!serverLease.getOwner().equalsIgnoreCase(lease.getOwner())) {
if (serverLease.getOwner() != null && !serverLease.getOwner().equalsIgnoreCase(lease.getOwner())) {
logger.info("Partition {} lease was taken over by owner '{}'", lease.getLeaseToken(), serverLease.getOwner());
throw new LeaseLostException(lease);
}
Expand All @@ -333,7 +330,7 @@ public Mono<Lease> updateProperties(Lease lease) {
throw new IllegalArgumentException("lease");
}

if (!lease.getOwner().equalsIgnoreCase(this.settings.getHostName())) {
if (lease.getOwner() != null && !lease.getOwner().equalsIgnoreCase(this.settings.getHostName())) {
logger.info("Partition '{}' lease was taken over by owner '{}' before lease item update", lease.getLeaseToken(), lease.getOwner());
throw new LeaseLostException(lease);
}
Expand All @@ -344,7 +341,7 @@ public Mono<Lease> updateProperties(Lease lease) {
new PartitionKey(lease.getId()),
this.requestOptionsFactory.createItemRequestOptions(lease),
serverLease -> {
if (!serverLease.getOwner().equalsIgnoreCase(lease.getOwner())) {
if (serverLease.getOwner() != null && !serverLease.getOwner().equalsIgnoreCase(lease.getOwner())) {
logger.info("Partition '{}' lease was taken over by owner '{}'", lease.getLeaseToken(), serverLease.getOwner());
throw new LeaseLostException(lease);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public Mono<Void> run(CancellationToken cancellationToken) {
if (cancellationToken.isCancellationRequested()) return Flux.error(new TaskCancelledException());

final String continuationToken = documentFeedResponse.getContinuationToken();
final ChangeFeedState continuationState = ChangeFeedState.fromString(documentFeedResponse.getContinuationToken());
final ChangeFeedState continuationState = ChangeFeedState.fromString(continuationToken);
checkNotNull(continuationState, "Argument 'continuationState' must not be null.");
checkArgument(
continuationState
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,7 @@ public Flux<Lease> splitPartition(Lease lease) {
throw new IllegalArgumentException("lease");
}

String leaseToken = lease.getLeaseToken();

ChangeFeedState lastContinuationState = lease.getContinuationState(
this.collectionResourceId,
new FeedRangePartitionKeyRangeImpl(leaseToken)
);
final String leaseToken = lease.getLeaseToken();

// TODO fabianm - this needs more elaborate processing in case the initial
// FeedRangeContinuation has continuation state for multiple feed Ranges
Expand All @@ -93,11 +88,23 @@ public Flux<Lease> splitPartition(Lease lease) {
// or at least Service - this will be part of the next set of changes
// For now - no merge just simple V0 of lease contract
// this simplification will work
final String lastContinuationToken = lastContinuationState.getContinuation() != null ?
lastContinuationState.getContinuation().getCurrentContinuationToken().getToken() :
null;

logger.info("Partition {} is gone due to split.", leaseToken);
//
//ChangeFeedState lastContinuationState = lease.getContinuationState(
// this.collectionResourceId,
// new FeedRangePartitionKeyRangeImpl(leaseToken)
//);
//
//final String lastContinuationToken = lastContinuationState.getContinuation() != null ?
// lastContinuationState.getContinuation().getCurrentContinuationToken().getToken() :
// null;

// "Push" ChangeFeedProcessor is not merge-proof currently. For such cases we need a specific handler that can
// take multiple leases and "converge" them in a thread safe manner while also merging the various continuation
// tokens for each merged lease.
// We will directly reuse the original/parent continuation token as the seed for the new leases until then.
final String lastContinuationToken = lease.getContinuationToken();
milismsft marked this conversation as resolved.
Show resolved Hide resolved

logger.info("Partition {} is gone due to split; will attempt to resume using continuation token {}.", leaseToken, lastContinuationToken);

// After a split, the children are either all or none available
return this.enumPartitionKeyRanges()
Expand All @@ -116,7 +123,7 @@ public Flux<Lease> splitPartition(Lease lease) {
return this.leaseManager.createLeaseIfNotExist(addedRangeId, lastContinuationToken);
}, this.degreeOfParallelism)
.map(newLease -> {
logger.info("Partition {} split into new partition with lease token {}.", leaseToken, newLease.getLeaseToken());
logger.info("Partition {} split into new partition with lease token {} and continuation token {}.", leaseToken, newLease.getLeaseToken(), lastContinuationToken);
return newLease;
});
}
Expand Down
Loading