Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
ruibaby committed Feb 5, 2024
2 parents 445ff87 + 7341f9d commit d8a1c77
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 37 deletions.
3 changes: 2 additions & 1 deletion .github/actions/docker-buildx-push/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ runs:
type=schedule,pattern=nightly-{{date 'YYYYMMDD'}},enabled=${{ github.event_name == 'schedule' }}
type=ref,event=branch,enabled=${{ github.event_name == 'push' }}
type=ref,event=pr,enabled=${{ github.event_name == 'pull_request' }}
type=semver,pattern={{ version }}
type=semver,pattern={{major}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{ version }}
type=sha,enabled=${{ github.event_name == 'push' }}
flavor: |
latest=false
Expand Down
8 changes: 6 additions & 2 deletions api/src/main/java/run/halo/app/extension/PageRequestImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ public class PageRequestImpl implements PageRequest {

public PageRequestImpl(int pageNumber, int pageSize, Sort sort) {
Assert.notNull(sort, "Sort must not be null");
Assert.isTrue(pageNumber >= 0, "Page index must not be less than zero!");
Assert.isTrue(pageSize >= 0, "Page size must not be less than one!");
if (pageNumber < 1) {
pageNumber = 1;
}
if (pageSize < 0) {
pageSize = 0;
}
this.pageNumber = pageNumber;
this.pageSize = pageSize;
this.sort = sort;
Expand Down
27 changes: 27 additions & 0 deletions api/src/test/java/run/halo/app/extension/PageRequestImplTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package run.halo.app.extension;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Random;
import org.junit.jupiter.api.RepeatedTest;
import org.springframework.data.domain.Sort;

class PageRequestImplTest {

@RepeatedTest(10)
void shouldBeCompatibleZeroAndNegativePageNumber() {
var randomPageNumber = -(new Random().nextInt(0, Integer.MAX_VALUE));
var page = new PageRequestImpl(randomPageNumber, 10, Sort.unsorted());
assertEquals(1, page.getPageNumber());
assertEquals(10, page.getPageSize());
}

@RepeatedTest(10)
void shouldBeCompatibleNegativePageSize() {
var randomPageSize = -(new Random().nextInt(1, Integer.MAX_VALUE));
var page = new PageRequestImpl(10, randomPageSize, Sort.unsorted());
assertEquals(10, page.getPageNumber());
assertEquals(0, page.getPageSize());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
import org.springframework.data.domain.Sort;
import org.springframework.data.util.Predicates;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.ReactiveTransactionManager;
import org.springframework.transaction.reactive.TransactionalOperator;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;
Expand All @@ -40,7 +41,6 @@

@Slf4j
@Component
@RequiredArgsConstructor
public class ReactiveExtensionClientImpl implements ReactiveExtensionClient {

private final ReactiveExtensionStoreClient client;
Expand All @@ -60,6 +60,28 @@ public class ReactiveExtensionClientImpl implements ReactiveExtensionClient {
private final ConcurrentMap<GroupKind, AtomicBoolean> indexBuildingState =
new ConcurrentHashMap<>();

private TransactionalOperator transactionalOperator;

public ReactiveExtensionClientImpl(ReactiveExtensionStoreClient client,
ExtensionConverter converter, SchemeManager schemeManager, ObjectMapper objectMapper,
IndexerFactory indexerFactory, IndexedQueryEngine indexedQueryEngine,
ReactiveTransactionManager reactiveTransactionManager) {
this.client = client;
this.converter = converter;
this.schemeManager = schemeManager;
this.objectMapper = objectMapper;
this.indexerFactory = indexerFactory;
this.indexedQueryEngine = indexedQueryEngine;
this.transactionalOperator = TransactionalOperator.create(reactiveTransactionManager);
}

/**
* Only for test.
*/
void setTransactionalOperator(TransactionalOperator transactionalOperator) {
this.transactionalOperator = transactionalOperator;
}

@Override
public <E extends Extension> Flux<E> list(Class<E> type, Predicate<E> predicate,
Comparator<E> comparator) {
Expand Down Expand Up @@ -151,7 +173,6 @@ private Mono<Unstructured> get(GroupVersionKind gvk, String name) {
}

@Override
@Transactional
public <E extends Extension> Mono<E> create(E extension) {
checkClientWritable(extension);
return Mono.just(extension)
Expand Down Expand Up @@ -185,7 +206,6 @@ && hasText(extension.getMetadata().getGenerateName()))
}

@Override
@Transactional
public <E extends Extension> Mono<E> update(E extension) {
checkClientWritable(extension);
// Refactor the atomic reference if we have a better solution.
Expand Down Expand Up @@ -223,7 +243,6 @@ private Mono<? extends Extension> getLatest(Extension extension) {
}

@Override
@Transactional
public <E extends Extension> Mono<E> delete(E extension) {
checkClientWritable(extension);
// set deletionTimestamp
Expand All @@ -247,7 +266,8 @@ <E extends Extension> Mono<E> doCreate(E oldExtension, String name, byte[] data)
var indexer = indexerFactory.getIndexer(gvk);
return client.create(name, data)
.map(created -> converter.convertFrom(type, created))
.doOnNext(indexer::indexRecord);
.doOnNext(indexer::indexRecord)
.as(transactionalOperator::transactional);
});
}

Expand All @@ -258,7 +278,8 @@ <E extends Extension> Mono<E> doUpdate(E oldExtension, String name, Long version
var indexer = indexerFactory.getIndexer(oldExtension.groupVersionKind());
return client.update(name, version, data)
.map(updated -> converter.convertFrom(type, updated))
.doOnNext(indexer::updateRecord);
.doOnNext(indexer::updateRecord)
.as(transactionalOperator::transactional);
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package run.halo.app.extension.gc;

import static run.halo.app.extension.Comparators.compareCreationTimestamp;

import java.util.List;
import java.util.function.Predicate;
import org.springframework.data.domain.Sort;
import run.halo.app.extension.Extension;
import run.halo.app.extension.ExtensionClient;
Expand Down Expand Up @@ -64,8 +61,6 @@ public void start() {
if (event instanceof SchemeRegistered registeredEvent) {
var newScheme = registeredEvent.getNewScheme();
listDeleted(newScheme.type()).forEach(watcher::onDelete);
client.list(newScheme.type(), deleted(), compareCreationTimestamp(true))
.forEach(watcher::onDelete);
}
});
client.watch(watcher);
Expand All @@ -77,16 +72,8 @@ public void start() {
<E extends Extension> List<E> listDeleted(Class<E> type) {
var options = new ListOptions()
.setFieldSelector(
FieldSelector.of(QueryFactory.all("metadata.deletionTimestamp"))
FieldSelector.of(QueryFactory.isNotNull("metadata.deletionTimestamp"))
);
return client.listAll(type, options, Sort.by("metadata.creationTimestamp"))
.stream()
.sorted(compareCreationTimestamp(true))
.toList();
}

private <E extends Extension> Predicate<E> deleted() {
return extension -> extension.getMetadata().getDeletionTimestamp() != null;
return client.listAll(type, options, Sort.by(Sort.Order.asc("metadata.creationTimestamp")));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import run.halo.app.extension.ListOptions;
import run.halo.app.extension.ListResult;
import run.halo.app.extension.PageRequest;
import run.halo.app.extension.index.query.All;
import run.halo.app.extension.index.query.QueryIndexViewImpl;
import run.halo.app.extension.router.selector.FieldSelector;
import run.halo.app.extension.router.selector.LabelSelector;
Expand Down Expand Up @@ -155,12 +154,13 @@ List<String> doRetrieve(Indexer indexer, ListOptions options, Sort sort) {
stopWatch.stop();

stopWatch.start("retrieve matched metadata names");
var hasLabelSelector = hasLabelSelector(options.getLabelSelector());
final List<String> matchedByLabels = hasLabelSelector
? retrieveForLabelMatchers(options.getLabelSelector().getMatchers(), fieldPathEntryMap,
allMetadataNames)
: allMetadataNames;
indexView.removeByIdNotIn(new TreeSet<>(matchedByLabels));
if (hasLabelSelector(options.getLabelSelector())) {
var matchedByLabels = retrieveForLabelMatchers(options.getLabelSelector().getMatchers(),
fieldPathEntryMap, allMetadataNames);
if (allMetadataNames.size() != matchedByLabels.size()) {
indexView.removeByIdNotIn(new TreeSet<>(matchedByLabels));
}
}
stopWatch.stop();

stopWatch.start("retrieve matched metadata names by fields");
Expand Down Expand Up @@ -188,8 +188,6 @@ boolean hasLabelSelector(LabelSelector labelSelector) {
}

boolean hasFieldSelector(FieldSelector fieldSelector) {
return fieldSelector != null
&& fieldSelector.query() != null
&& !(fieldSelector.query() instanceof All);
return fieldSelector != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

@Slf4j
@Component
// @Component
// TODO Remove this class on next version
public class IndicesInitializer {

private final IndicesService indicesService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.transaction.ReactiveTransactionManager;
import org.springframework.transaction.reactive.TransactionalOperator;
import reactor.core.Exceptions;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -63,6 +65,9 @@ class ReactiveExtensionClientTest {
@Mock
IndexerFactory indexerFactory;

@Mock
ReactiveTransactionManager reactiveTransactionManager;

@Spy
ObjectMapper objectMapper = JsonMapper.builder()
.addModule(new JavaTimeModule())
Expand All @@ -76,6 +81,10 @@ void setUp() {
lenient().when(schemeManager.get(eq(FakeExtension.class)))
.thenReturn(fakeScheme);
lenient().when(schemeManager.get(eq(fakeScheme.groupVersionKind()))).thenReturn(fakeScheme);
var transactionalOperator = mock(TransactionalOperator.class);
client.setTransactionalOperator(transactionalOperator);
lenient().when(transactionalOperator.transactional(any(Mono.class)))
.thenAnswer(invocation -> invocation.getArgument(0));
}

FakeExtension createFakeExtension(String name, Long version) {
Expand Down

0 comments on commit d8a1c77

Please sign in to comment.