Skip to content

Commit

Permalink
refactor: reduce the number of failures due to conflict post update
Browse files Browse the repository at this point in the history
  • Loading branch information
guqing committed Mar 27, 2024
1 parent c2a7154 commit 6eda37e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package run.halo.app.content;

import java.security.Principal;
import java.time.Duration;
import java.time.Instant;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.lang.Nullable;
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.util.Assert;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;
import run.halo.app.core.extension.content.Snapshot;
import run.halo.app.extension.MetadataUtil;
import run.halo.app.extension.ReactiveExtensionClient;
Expand Down Expand Up @@ -94,20 +97,23 @@ protected Mono<ContentWrapper> updateContent(String baseSnapshotName,
Assert.notNull(contentRequest, "The contentRequest must not be null");
Assert.notNull(baseSnapshotName, "The baseSnapshotName must not be null");
Assert.notNull(contentRequest.headSnapshotName(), "The headSnapshotName must not be null");
return client.fetch(Snapshot.class, contentRequest.headSnapshotName())
.flatMap(headSnapshot -> client.fetch(Snapshot.class, baseSnapshotName)
.map(baseSnapshot -> determineRawAndContentPatch(headSnapshot, baseSnapshot,
contentRequest)
return Mono.defer(() -> client.fetch(Snapshot.class, contentRequest.headSnapshotName())
.flatMap(headSnapshot -> client.fetch(Snapshot.class, baseSnapshotName)
.map(baseSnapshot -> determineRawAndContentPatch(headSnapshot, baseSnapshot,
contentRequest)
)
)
.flatMap(headSnapshot -> getContextUsername()
.map(username -> {
Snapshot.addContributor(headSnapshot, username);
return headSnapshot;
})
.defaultIfEmpty(headSnapshot)
)
.flatMap(client::update)
)
.flatMap(headSnapshot -> getContextUsername()
.map(username -> {
Snapshot.addContributor(headSnapshot, username);
return headSnapshot;
})
.defaultIfEmpty(headSnapshot)
)
.flatMap(client::update)
.retryWhen(Retry.backoff(5, Duration.ofMillis(100))
.filter(throwable -> throwable instanceof OptimisticLockingFailureException))
.flatMap(head -> restoredContent(baseSnapshotName, head));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,11 @@ public Mono<Post> updatePost(PostRequest postRequest) {
return Mono.defer(() -> updateContent(baseSnapshot, postRequest.contentRequest())
.flatMap(contentWrapper -> {
post.getSpec().setHeadSnapshot(contentWrapper.getSnapshotName());
return client.update(post);
return client.get(Post.class, post.getMetadata().getName())
.flatMap(existPost -> {
post.getMetadata().setVersion(existPost.getMetadata().getVersion());
return client.update(post);
});
}))
.retryWhen(Retry.backoff(5, Duration.ofMillis(100))
.filter(throwable -> throwable instanceof OptimisticLockingFailureException));
Expand Down

0 comments on commit 6eda37e

Please sign in to comment.