Skip to content

Commit fc22f77

Browse files
committed
Fix NPE in SnapshottingInstanceRepository
fixes #1125
1 parent 1e6a60d commit fc22f77

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/server/domain/entities/SnapshottingInstanceRepository.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ public void stop() {
8383
}
8484

8585
protected Mono<Instance> rehydrateSnapshot(InstanceId id) {
86-
Instance outdatedSnapshot = this.snapshots.get(id);
87-
return super.find(id).map(instance -> {
88-
if (this.snapshots.replace(id, outdatedSnapshot, instance)) {
86+
return super.find(id).map(instance -> this.snapshots.compute(id, (key, snapshot) -> {
87+
//check if the loaded version hasn't been already outdated by a snapshot
88+
if (snapshot == null || instance.getVersion() > snapshot.getVersion()) {
8989
return instance;
9090
} else {
91-
return this.snapshots.get(id);
91+
return snapshot;
9292
}
93-
});
93+
}));
9494
}
9595

9696
protected void updateSnapshot(InstanceEvent event) {

spring-boot-admin-server/src/test/java/de/codecentric/boot/admin/server/domain/entities/SnapshottingInstanceRepositoryTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import de.codecentric.boot.admin.server.domain.values.Registration;
2222
import de.codecentric.boot.admin.server.domain.values.StatusInfo;
2323
import de.codecentric.boot.admin.server.eventstore.InMemoryEventStore;
24+
import de.codecentric.boot.admin.server.eventstore.OptimisticLockingException;
2425
import reactor.core.publisher.Flux;
2526
import reactor.core.publisher.Mono;
2627
import reactor.test.StepVerifier;
@@ -100,6 +101,16 @@ public void should_update_cache_after_error() {
100101
.verifyComplete();
101102
}
102103

104+
@Test
105+
public void should_return_outdated_instance_not_present_in_cache() {
106+
this.repository.stop();
107+
//given
108+
StepVerifier.create(this.repository.save(this.instance)).expectNext(this.instance).verifyComplete();
109+
StepVerifier.create(this.repository.save(this.instance)).verifyError(OptimisticLockingException.class);
110+
//when
111+
StepVerifier.create(this.repository.find(this.instance.getId())).expectNext(this.instance).verifyComplete();
112+
}
113+
103114
@Test
104115
public void should_refresh_snapshots_eagerly_on_optimistick_lock_exception() {
105116
//given

0 commit comments

Comments
 (0)