Skip to content

Commit

Permalink
refactor: add list options for sync all synchronizer (#6145)
Browse files Browse the repository at this point in the history
#### What type of PR is this?
/kind improvement
/area core
/milestone 2.17.x

#### What this PR does / why we need it:
为启动时同步添加 ListOptions 选项为后续保持 ExtensionMatcher 的纯粹做准备,后续将移除 ExtensionMatcher 中多余的方法声明,只保留 match 方法,最终的结果希望是
```java
@FunctionalInterface
public interface ExtensionMatcher {
    boolean match(Extension extension);
}
```
以前构建 Controller 的写法
```java
public Controller setupWith(ControllerBuilder builder) {
         return builder
            .extension(new Post())
            .onAddMatcher(DefaultExtensionMatcher.builder(client, Post.GVK)
                .fieldSelector(FieldSelector.of(
                    equal(Post.REQUIRE_SYNC_ON_STARTUP_INDEX_NAME, TRUE))
                )
                .build()
            )
           .build();
}
```
现在的写法
```java
public Controller setupWith(ControllerBuilder builder) {
        var post = new Post();
        return builder
            .extension(post)
            // 当有新数据添加时
            .onAddMatcher(extension -> "fake-post".equals(extension.getMetadata().getName()))
            // 使用 syncAllListOptions 作为启动时同步的查询条件过滤不需要的数据
            .syncAllListOptions(ListOptions.builder()
                .fieldQuery(equal(Post.REQUIRE_SYNC_ON_STARTUP_INDEX_NAME, TRUE))
                .build()
            )
            .build();
    }
```

#### Does this PR introduce a user-facing change?
```release-note
开发者相关:重构 ControllerBuilder 的匹配条件并增加 syncAllListOptions 作为启动时同步的查询条件
```
  • Loading branch information
guqing authored Jun 26, 2024
1 parent 50f751d commit 3f94cfc
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 22 deletions.
15 changes: 12 additions & 3 deletions api/src/main/java/run/halo/app/extension/ExtensionMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@
import run.halo.app.extension.router.selector.LabelSelector;

public interface ExtensionMatcher {
GroupVersionKind getGvk();
@Deprecated(since = "2.17.0", forRemoval = true)
default GroupVersionKind getGvk() {
return null;
}

LabelSelector getLabelSelector();
@Deprecated(since = "2.17.0", forRemoval = true)
default LabelSelector getLabelSelector() {
return null;
}

FieldSelector getFieldSelector();
@Deprecated(since = "2.17.0", forRemoval = true)
default FieldSelector getFieldSelector() {
return null;
}

boolean match(Extension extension);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import lombok.Builder;
import lombok.Getter;
import org.springframework.util.Assert;
import run.halo.app.extension.router.selector.FieldSelector;
import run.halo.app.extension.router.selector.LabelSelector;

public class WatcherExtensionMatchers {
@Getter
Expand Down Expand Up @@ -38,15 +40,15 @@ public GroupVersionKind getGroupVersionKind() {
}

public ExtensionMatcher onAddMatcher() {
return this.onAddMatcher;
return delegateExtensionMatcher(this.onAddMatcher);
}

public ExtensionMatcher onUpdateMatcher() {
return this.onUpdateMatcher;
return delegateExtensionMatcher(this.onUpdateMatcher);
}

public ExtensionMatcher onDeleteMatcher() {
return this.onDeleteMatcher;
return delegateExtensionMatcher(this.onDeleteMatcher);
}

public static WatcherExtensionMatchersBuilder builder(ExtensionClient client,
Expand All @@ -58,4 +60,32 @@ static ExtensionMatcher emptyMatcher(ExtensionClient client,
GroupVersionKind gvk) {
return DefaultExtensionMatcher.builder(client, gvk).build();
}

/**
* Remove this method when the deprecated methods are removed.
*/
ExtensionMatcher delegateExtensionMatcher(ExtensionMatcher matcher) {
return new ExtensionMatcher() {

@Override
public GroupVersionKind getGvk() {
return matcher.getGvk();
}

@Override
public LabelSelector getLabelSelector() {
return matcher.getLabelSelector();
}

@Override
public FieldSelector getFieldSelector() {
return matcher.getFieldSelector();
}

@Override
public boolean match(Extension extension) {
return extension.groupVersionKind().equals(gvk) && matcher.match(extension);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import run.halo.app.extension.Extension;
import run.halo.app.extension.ExtensionClient;
import run.halo.app.extension.ExtensionMatcher;
import run.halo.app.extension.ListOptions;
import run.halo.app.extension.WatcherExtensionMatchers;
import run.halo.app.extension.controller.Reconciler.Request;

Expand All @@ -30,6 +31,8 @@ public class ControllerBuilder {

private ExtensionMatcher onUpdateMatcher;

private ListOptions syncAllListOptions;

private final ExtensionClient client;

private boolean syncAllOnStart = true;
Expand Down Expand Up @@ -84,6 +87,11 @@ public ControllerBuilder syncAllOnStart(boolean syncAllAtStart) {
return this;
}

public ControllerBuilder syncAllListOptions(ListOptions syncAllListOptions) {
this.syncAllListOptions = syncAllListOptions;
return this;
}

public ControllerBuilder workerCount(int workerCount) {
this.workerCount = workerCount;
return this;
Expand Down Expand Up @@ -116,8 +124,23 @@ public Controller build() {
client,
extension,
watcher,
extensionMatchers.onAddMatcher());
determineSyncAllListOptions());
return new DefaultController<>(name, reconciler, queue, synchronizer, minDelay, maxDelay,
workerCount);
}

ListOptions determineSyncAllListOptions() {
if (syncAllListOptions != null) {
return syncAllListOptions;
}
// In order to be compatible with the previous version of the code
// The previous version of the code determined syncAllListOptions through onAddMatcher
// TODO Will be removed later
if (onAddMatcher != null) {
return new ListOptions()
.setLabelSelector(onAddMatcher.getLabelSelector())
.setFieldSelector(onAddMatcher.getFieldSelector());
}
return new ListOptions();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.springframework.data.domain.Sort;
import run.halo.app.extension.Extension;
import run.halo.app.extension.ExtensionClient;
import run.halo.app.extension.ExtensionMatcher;
import run.halo.app.extension.GroupVersionKind;
import run.halo.app.extension.ListOptions;
import run.halo.app.extension.Watcher;
Expand All @@ -27,7 +26,7 @@ public class RequestSynchronizer implements Synchronizer<Request> {

private final Watcher watcher;

private final ExtensionMatcher listMatcher;
private final ListOptions listOptions;

@Getter
private volatile boolean started = false;
Expand All @@ -36,13 +35,13 @@ public RequestSynchronizer(boolean syncAllOnStart,
ExtensionClient client,
Extension extension,
Watcher watcher,
ExtensionMatcher listMatcher) {
ListOptions listOptions) {
this.syncAllOnStart = syncAllOnStart;
this.client = client;
this.type = extension.groupVersionKind();
this.watcher = watcher;
this.indexedQueryEngine = client.indexedQueryEngine();
this.listMatcher = listMatcher;
this.listOptions = listOptions;
}

@Override
Expand All @@ -54,11 +53,6 @@ public void start() {
started = true;

if (syncAllOnStart) {
var listOptions = new ListOptions();
if (listMatcher != null) {
listOptions.setFieldSelector(listMatcher.getFieldSelector());
listOptions.setLabelSelector(listMatcher.getLabelSelector());
}
indexedQueryEngine.retrieveAll(type, listOptions, Sort.by("metadata.creationTimestamp"))
.forEach(name -> watcher.onAdd(new Request(name)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.domain.Sort;
import run.halo.app.extension.ExtensionClient;
import run.halo.app.extension.ExtensionMatcher;
import run.halo.app.extension.FakeExtension;
import run.halo.app.extension.GroupVersionKind;
import run.halo.app.extension.ListOptions;
Expand All @@ -37,16 +36,13 @@ class RequestSynchronizerTest {
@Mock
Watcher watcher;

@Mock
ExtensionMatcher listMatcher;

RequestSynchronizer synchronizer;

@BeforeEach
void setUp() {
when(client.indexedQueryEngine()).thenReturn(indexedQueryEngine);
synchronizer =
new RequestSynchronizer(true, client, new FakeExtension(), watcher, listMatcher);
new RequestSynchronizer(true, client, new FakeExtension(), watcher, new ListOptions());
assertFalse(synchronizer.isDisposed());
assertFalse(synchronizer.isStarted());
}
Expand All @@ -71,7 +67,7 @@ void shouldStartCorrectlyWhenSyncingAllOnStart() {
@Test
void shouldStartCorrectlyWhenNotSyncingAllOnStart() {
synchronizer =
new RequestSynchronizer(false, client, new FakeExtension(), watcher, listMatcher);
new RequestSynchronizer(false, client, new FakeExtension(), watcher, new ListOptions());
assertFalse(synchronizer.isDisposed());
assertFalse(synchronizer.isStarted());

Expand Down

0 comments on commit 3f94cfc

Please sign in to comment.