Skip to content

Commit

Permalink
feat: add index build state as readiness check indicator (#6700)
Browse files Browse the repository at this point in the history
#### 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
将索引构建状态添加到就绪检测的指标中以优化就绪时访问出现索引不可用的问题
```
  • Loading branch information
guqing authored Sep 28, 2024
1 parent a53aa78 commit ca9adfc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Extension> createExtensionIterator(Scheme scheme) {
Expand All @@ -459,6 +463,8 @@ private ExtensionIterator<Extension> 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()
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package run.halo.app.extension.availability;

import org.springframework.boot.availability.AvailabilityState;

public enum IndexBuildState implements AvailabilityState {
BUILDING,
BUILT;
}
Original file line number Diff line number Diff line change
@@ -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);
});
}
}

0 comments on commit ca9adfc

Please sign in to comment.