From ca9adfc9d8937e160bcf6c0e51f2fadd4dbd8855 Mon Sep 17 00:00:00 2001 From: guqing <38999863+guqing@users.noreply.github.com> Date: Sat, 28 Sep 2024 19:05:41 +0800 Subject: [PATCH] feat: add index build state as readiness check indicator (#6700) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind improvement /area core /milestone 2.20.x #### What this PR does / why we need it: 将索引构建状态添加到就绪检测的指标中 #### Which issue(s) this PR fixes: Fixes #6632 #### Does this PR introduce a user-facing change? ```release-note 将索引构建状态添加到就绪检测的指标中以优化就绪时访问出现索引不可用的问题 ``` --- .../ReactiveExtensionClientImpl.java | 8 +++++++ .../availability/IndexBuildState.java | 8 +++++++ .../IndexBuildStateHealthIndicator.java | 22 +++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 application/src/main/java/run/halo/app/extension/availability/IndexBuildState.java create mode 100644 application/src/main/java/run/halo/app/extension/availability/IndexBuildStateHealthIndicator.java diff --git a/application/src/main/java/run/halo/app/extension/ReactiveExtensionClientImpl.java b/application/src/main/java/run/halo/app/extension/ReactiveExtensionClientImpl.java index 0962d64de2..38377c297e 100644 --- a/application/src/main/java/run/halo/app/extension/ReactiveExtensionClientImpl.java +++ b/application/src/main/java/run/halo/app/extension/ReactiveExtensionClientImpl.java @@ -19,6 +19,8 @@ import lombok.NonNull; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.availability.AvailabilityChangeEvent; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.EventListener; import org.springframework.dao.DataIntegrityViolationException; @@ -30,6 +32,7 @@ import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.util.retry.Retry; +import run.halo.app.extension.availability.IndexBuildState; import run.halo.app.extension.exception.ExtensionNotFoundException; import run.halo.app.extension.index.DefaultExtensionIterator; import run.halo.app.extension.index.ExtensionIterator; @@ -443,6 +446,7 @@ class IndexBuildsManager { private final ExtensionConverter converter; private final ReactiveExtensionStoreClient client; private final SchemeWatcherManager schemeWatcherManager; + private final ApplicationEventPublisher eventPublisher; @NonNull private ExtensionIterator createExtensionIterator(Scheme scheme) { @@ -459,6 +463,8 @@ private ExtensionIterator createExtensionIterator(Scheme scheme) { @EventListener(ContextRefreshedEvent.class) public void startBuildingIndex() { + AvailabilityChangeEvent.publish(eventPublisher, this, IndexBuildState.BUILDING); + final long startTimeMs = System.currentTimeMillis(); log.info("Start building index for all extensions, please wait..."); schemeManager.schemes() @@ -474,6 +480,8 @@ public void startBuildingIndex() { indexerFactory.removeIndexer(scheme); } }); + + AvailabilityChangeEvent.publish(eventPublisher, this, IndexBuildState.BUILT); log.info("Successfully built index in {}ms, Preparing to lunch application...", System.currentTimeMillis() - startTimeMs); } diff --git a/application/src/main/java/run/halo/app/extension/availability/IndexBuildState.java b/application/src/main/java/run/halo/app/extension/availability/IndexBuildState.java new file mode 100644 index 0000000000..909d46bff8 --- /dev/null +++ b/application/src/main/java/run/halo/app/extension/availability/IndexBuildState.java @@ -0,0 +1,8 @@ +package run.halo.app.extension.availability; + +import org.springframework.boot.availability.AvailabilityState; + +public enum IndexBuildState implements AvailabilityState { + BUILDING, + BUILT; +} diff --git a/application/src/main/java/run/halo/app/extension/availability/IndexBuildStateHealthIndicator.java b/application/src/main/java/run/halo/app/extension/availability/IndexBuildStateHealthIndicator.java new file mode 100644 index 0000000000..20c5a5dbca --- /dev/null +++ b/application/src/main/java/run/halo/app/extension/availability/IndexBuildStateHealthIndicator.java @@ -0,0 +1,22 @@ +package run.halo.app.extension.availability; + +import org.springframework.boot.actuate.availability.AvailabilityStateHealthIndicator; +import org.springframework.boot.actuate.health.Status; +import org.springframework.boot.availability.ApplicationAvailability; +import org.springframework.stereotype.Component; + +@Component +public class IndexBuildStateHealthIndicator extends AvailabilityStateHealthIndicator { + /** + * Create a {@link IndexBuildStateHealthIndicator} instance by {@link ApplicationAvailability}. + * Mapping {@link IndexBuildState} to {@link Status}. + * + * @see IndexBuildState + */ + public IndexBuildStateHealthIndicator(ApplicationAvailability availability) { + super(availability, IndexBuildState.class, (statusMappings) -> { + statusMappings.add(IndexBuildState.BUILT, Status.UP); + statusMappings.add(IndexBuildState.BUILDING, Status.OUT_OF_SERVICE); + }); + } +}