Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: failure in data query due to reconciler triggered by uncommitted transaction #5323

Merged
merged 3 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.ReactiveTransactionManager;
import org.springframework.transaction.reactive.TransactionalOperator;

@Configuration(proxyBeanMethods = false)
@EnableAsync
Expand All @@ -18,4 +20,9 @@ Jackson2ObjectMapperBuilderCustomizer objectMapperCustomizer() {
builder.featuresToEnable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS);
};
}

@Bean
TransactionalOperator transactionTemplate(ReactiveTransactionManager transactionManager) {
guqing marked this conversation as resolved.
Show resolved Hide resolved
return TransactionalOperator.create(transactionManager);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
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.reactive.TransactionalOperator;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;
Expand Down Expand Up @@ -57,6 +57,8 @@ public class ReactiveExtensionClientImpl implements ReactiveExtensionClient {

private final IndexedQueryEngine indexedQueryEngine;

private final TransactionalOperator transactionalOperator;

private final ConcurrentMap<GroupKind, AtomicBoolean> indexBuildingState =
new ConcurrentHashMap<>();

Expand Down Expand Up @@ -151,7 +153,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 +186,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 +223,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 +246,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 +258,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
Expand Up @@ -36,6 +36,7 @@
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.dao.DataIntegrityViolationException;
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 +64,9 @@ class ReactiveExtensionClientTest {
@Mock
IndexerFactory indexerFactory;

@Mock
TransactionalOperator transactionalOperator;

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

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