fix: update post with retry (#5823)

#### What type of PR is this?
/kind improvement
/area core
/milestone 2.15.x

#### What this PR does / why we need it:
修复重试更新文章的错误写法

#### Does this PR introduce a user-facing change?
```release-note
None
```
This commit is contained in:
guqing 2024-04-29 17:48:35 +08:00 committed by GitHub
parent 966558d1ce
commit 5770ad4c55
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 16 deletions

View File

@ -7,6 +7,7 @@ import java.time.Instant;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.OptimisticLockingFailureException;
@ -328,9 +329,11 @@ public class PostServiceImpl extends AbstractContentService implements PostServi
.flatMap(post -> {
var headSnapshotName = post.getSpec().getHeadSnapshot();
if (StringUtils.equals(headSnapshotName, snapshotName)) {
// update head to release
post.getSpec().setHeadSnapshot(post.getSpec().getReleaseSnapshot());
return updatePostWithRetry(post);
return updatePostWithRetry(post, record -> {
// update head to release
record.getSpec().setHeadSnapshot(record.getSpec().getReleaseSnapshot());
return record;
});
}
return Mono.just(post);
})
@ -352,14 +355,15 @@ public class PostServiceImpl extends AbstractContentService implements PostServi
});
}
private Mono<Post> updatePostWithRetry(Post post) {
return client.update(post)
private Mono<Post> updatePostWithRetry(Post post, UnaryOperator<Post> func) {
return client.update(func.apply(post))
.onErrorResume(OptimisticLockingFailureException.class,
e -> Mono.defer(() -> client.get(Post.class, post.getMetadata().getName())
.flatMap(client::update))
.retryWhen(Retry.backoff(8, Duration.ofMillis(100))
.filter(OptimisticLockingFailureException.class::isInstance)
.map(func)
.flatMap(client::update)
)
.retryWhen(Retry.backoff(8, Duration.ofMillis(100))
.filter(OptimisticLockingFailureException.class::isInstance))
);
}

View File

@ -5,6 +5,7 @@ import java.time.Instant;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.OptimisticLockingFailureException;
@ -206,9 +207,11 @@ public class SinglePageServiceImpl extends AbstractContentService implements Sin
.flatMap(page -> {
var headSnapshotName = page.getSpec().getHeadSnapshot();
if (StringUtils.equals(headSnapshotName, snapshotName)) {
// update head to release
page.getSpec().setHeadSnapshot(page.getSpec().getReleaseSnapshot());
return updatePostWithRetry(page);
return updatePageWithRetry(page, record -> {
// update head to release
page.getSpec().setHeadSnapshot(page.getSpec().getReleaseSnapshot());
return record;
});
}
return Mono.just(page);
})
@ -230,14 +233,15 @@ public class SinglePageServiceImpl extends AbstractContentService implements Sin
});
}
private Mono<SinglePage> updatePostWithRetry(SinglePage page) {
return client.update(page)
private Mono<SinglePage> updatePageWithRetry(SinglePage page, UnaryOperator<SinglePage> func) {
return client.update(func.apply(page))
.onErrorResume(OptimisticLockingFailureException.class,
e -> Mono.defer(() -> client.get(SinglePage.class, page.getMetadata().getName())
.flatMap(client::update))
.retryWhen(Retry.backoff(8, Duration.ofMillis(100))
.filter(OptimisticLockingFailureException.class::isInstance)
.map(func)
.flatMap(client::update)
)
.retryWhen(Retry.backoff(8, Duration.ofMillis(100))
.filter(OptimisticLockingFailureException.class::isInstance))
);
}