Skip to content

Commit

Permalink
feat: optimized post reconciliation process for enhanced performance …
Browse files Browse the repository at this point in the history
…and resource utilization
  • Loading branch information
guqing committed Jan 25, 2024
1 parent 3f27f6f commit 57dfc52
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
10 changes: 4 additions & 6 deletions api/src/main/java/run/halo/app/core/extension/content/Post.java
Expand Up @@ -16,7 +16,6 @@
import run.halo.app.extension.GVK;
import run.halo.app.extension.GroupVersionKind;
import run.halo.app.extension.MetadataOperator;
import run.halo.app.extension.MetadataUtil;
import run.halo.app.infra.ConditionList;

/**
Expand All @@ -35,6 +34,8 @@ public class Post extends AbstractExtension {

public static final String KIND = "Post";

public static final String REQUIRE_SYNC_ON_STARTUP_INDEX_NAME = "requireSyncOnStartup";

public static final GroupVersionKind GVK = GroupVersionKind.fromExtension(Post.class);

public static final String CATEGORIES_ANNO = "content.halo.run/categories";
Expand Down Expand Up @@ -158,6 +159,8 @@ public static class PostStatus {

private Instant lastModifyTime;

private long observedVersion;

@JsonIgnore
public ConditionList getConditionsOrDefault() {
if (this.conditions == null) {
Expand Down Expand Up @@ -268,9 +271,4 @@ public CompactPost build() {
}
}
}

public static void changePublishedState(Post post, boolean value) {
Map<String, String> labels = MetadataUtil.nullSafeLabels(post);
labels.put(PUBLISHED_LABEL, String.valueOf(value));
}
}
Expand Up @@ -4,6 +4,7 @@
import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
import static run.halo.app.extension.ExtensionUtil.addFinalizers;
import static run.halo.app.extension.ExtensionUtil.removeFinalizers;
import static run.halo.app.extension.index.query.QueryFactory.equal;

import com.google.common.hash.Hashing;
import java.time.Instant;
Expand Down Expand Up @@ -36,6 +37,7 @@
import run.halo.app.event.post.PostUnpublishedEvent;
import run.halo.app.event.post.PostUpdatedEvent;
import run.halo.app.event.post.PostVisibleChangedEvent;
import run.halo.app.extension.DefaultExtensionMatcher;
import run.halo.app.extension.ExtensionClient;
import run.halo.app.extension.ExtensionOperator;
import run.halo.app.extension.ListOptions;
Expand Down Expand Up @@ -211,7 +213,11 @@ public Result reconcile(Request request) {
status.setInProgress(
!StringUtils.equals(headSnapshot, post.getSpec().getReleaseSnapshot()));

// version + 1 is required to truly equal version
// as a version will be incremented after the update
status.setObservedVersion(post.getMetadata().getVersion() + 1);
client.update(post);

// fire event after updating post
events.forEach(eventPublisher::publishEvent);
});
Expand All @@ -222,8 +228,12 @@ public Result reconcile(Request request) {
public Controller setupWith(ControllerBuilder builder) {
return builder
.extension(new Post())
// TODO Make it configurable
.workerCount(1)
.onAddMatcher(DefaultExtensionMatcher.builder(client, Post.GVK)
.fieldSelector(FieldSelector.of(
equal(Post.REQUIRE_SYNC_ON_STARTUP_INDEX_NAME, BooleanUtils.TRUE))
)
.build()
)
.build();
}

Expand Down
Expand Up @@ -5,6 +5,7 @@
import static run.halo.app.extension.index.IndexAttributeFactory.simpleAttribute;

import java.util.Set;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.boot.context.event.ApplicationContextInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.lang.NonNull;
Expand Down Expand Up @@ -146,6 +147,15 @@ public void onApplicationEvent(@NonNull ApplicationContextInitializedEvent event
.setName("status.excerpt")
.setIndexFunc(
simpleAttribute(Post.class, post -> post.getStatusOrDefault().getExcerpt())));

indexSpecs.add(new IndexSpec()
.setName(Post.REQUIRE_SYNC_ON_STARTUP_INDEX_NAME)
.setIndexFunc(simpleAttribute(Post.class, post -> {
var version = post.getMetadata().getVersion();
var observedVersion = post.getStatusOrDefault().getObservedVersion();
// do not care about the false case so return null to avoid indexing
return observedVersion >= version ? BooleanUtils.TRUE : null;
})));
});
schemeManager.register(Category.class, indexSpecs -> {
indexSpecs.add(new IndexSpec()
Expand Down
2 changes: 2 additions & 0 deletions application/src/test/java/run/halo/app/content/TestPost.java
Expand Up @@ -19,6 +19,7 @@ public static Post postV1() {
post.setApiVersion(getApiVersion(Post.class));
Metadata metadata = new Metadata();
metadata.setName("post-A");
metadata.setVersion(1L);
post.setMetadata(metadata);

Post.PostSpec postSpec = new Post.PostSpec();
Expand All @@ -38,6 +39,7 @@ public static Snapshot snapshotV1() {
snapshot.setApiVersion(getApiVersion(Snapshot.class));
Metadata metadata = new Metadata();
metadata.setName("snapshot-A");
metadata.setVersion(1L);
metadata.setCreationTimestamp(Instant.now());
snapshot.setMetadata(metadata);
MetadataUtil.nullSafeAnnotations(snapshot).put(Snapshot.KEEP_RAW_ANNO, "true");
Expand Down

0 comments on commit 57dfc52

Please sign in to comment.