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