Skip to content

Commit a1cdabe

Browse files
committed
Allow event version to be non-consecutive
Because the eventstore may remove outdated events the versions can be non-consecutive fixes #1149
1 parent 5f35457 commit a1cdabe

File tree

2 files changed

+34
-12
lines changed
  • spring-boot-admin-server/src

2 files changed

+34
-12
lines changed

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2018 the original author or authors.
2+
* Copyright 2014-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -158,8 +158,7 @@ public Instance withStatusInfo(StatusInfo statusInfo) {
158158

159159
public Instance withEndpoints(Endpoints endpoints) {
160160
Assert.notNull(endpoints, "'endpoints' must not be null");
161-
Endpoints endpointsWithHealth = this.registration != null ? endpoints.withEndpoint(
162-
Endpoint.HEALTH,
161+
Endpoints endpointsWithHealth = this.registration != null ? endpoints.withEndpoint(Endpoint.HEALTH,
163162
this.registration.getHealthUrl()
164163
) : endpoints;
165164
if (Objects.equals(this.endpoints, endpointsWithHealth)) {
@@ -189,8 +188,7 @@ Instance clearUnsavedEvents() {
189188
this.registration,
190189
this.registered,
191190
this.statusInfo,
192-
this.statusTimestamp,
193-
info,
191+
this.statusTimestamp, this.info,
194192
this.endpoints,
195193
this.buildVersion,
196194
this.tags,
@@ -214,8 +212,8 @@ Instance apply(InstanceEvent event) {
214212
private Instance apply(InstanceEvent event, boolean isNewEvent) {
215213
Assert.notNull(event, "'event' must not be null");
216214
Assert.isTrue(this.id.equals(event.getInstance()), "'event' must refer the same instance");
217-
Assert.isTrue(this.nextVersion() == event.getVersion(),
218-
() -> "Event " + event.getVersion() + " doesn't match exptected version " + this.nextVersion()
215+
Assert.isTrue(event.getVersion() >= this.nextVersion(),
216+
() -> "Event " + event.getVersion() + " must be greater or equal to " + this.nextVersion()
219217
);
220218

221219
List<InstanceEvent> unsavedEvents = appendToEvents(event, isNewEvent);

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

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2018 the original author or authors.
2+
* Copyright 2014-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,13 +18,16 @@
1818

1919
import de.codecentric.boot.admin.server.domain.events.InstanceDeregisteredEvent;
2020
import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
21+
import de.codecentric.boot.admin.server.domain.events.InstanceInfoChangedEvent;
2122
import de.codecentric.boot.admin.server.domain.values.BuildVersion;
2223
import de.codecentric.boot.admin.server.domain.values.Endpoints;
2324
import de.codecentric.boot.admin.server.domain.values.Info;
2425
import de.codecentric.boot.admin.server.domain.values.InstanceId;
2526
import de.codecentric.boot.admin.server.domain.values.Registration;
2627
import de.codecentric.boot.admin.server.domain.values.StatusInfo;
2728

29+
import java.util.List;
30+
import java.util.stream.Collectors;
2831
import org.junit.Test;
2932

3033
import static java.util.Collections.singletonMap;
@@ -145,8 +148,9 @@ public void should_throw_when_applied_wrong_event() {
145148
))).isInstanceOf(IllegalArgumentException.class)
146149
.hasMessage("'event' must refer the same instance");
147150

148-
assertThatThrownBy(() -> instance.apply(new InstanceDeregisteredEvent(InstanceId.of("id"), 1L))).isInstanceOf(
149-
IllegalArgumentException.class).hasMessage("Event 1 doesn't match exptected version 0");
151+
assertThatThrownBy(() -> instance.apply(new InstanceDeregisteredEvent(InstanceId.of("id"), 1L))
152+
.apply(new InstanceDeregisteredEvent(InstanceId.of("id"), 1L))).isInstanceOf(
153+
IllegalArgumentException.class).hasMessage("Event 1 must be greater or equal to 2");
150154
}
151155

152156
@Test
@@ -186,8 +190,7 @@ public void should_extract_tags() {
186190
assertThat(instance.getTags().getValues()).containsExactly(entry("environment", "test"), entry("region", "EU"));
187191

188192
instance = instance.withInfo(Info.from(singletonMap("tags", singletonMap("region", "US-East"))));
189-
assertThat(instance.getTags().getValues()).containsExactly(
190-
entry("environment", "test"),
193+
assertThat(instance.getTags().getValues()).containsExactly(entry("environment", "test"),
191194
entry("region", "US-East")
192195
);
193196

@@ -197,4 +200,25 @@ public void should_extract_tags() {
197200
instance = instance.register(registration.toBuilder().clearMetadata().build());
198201
assertThat(instance.getTags().getValues()).isEmpty();
199202
}
203+
204+
@Test
205+
public void shoud_rebuild_instance() {
206+
Instance instance = Instance.create(InstanceId.of("id"))
207+
.register(Registration.create("test", "http://test").build())
208+
.withInfo(Info.from(singletonMap("info", "remove")))
209+
.withInfo(Info.from(singletonMap("info", "test2")));
210+
211+
List<InstanceEvent> relevantEvents = instance.getUnsavedEvents()
212+
.stream()
213+
.filter(e -> !(e instanceof InstanceInfoChangedEvent &&
214+
((InstanceInfoChangedEvent) e).getInfo()
215+
.getValues()
216+
.get("info")
217+
.equals("remove")))
218+
.collect(Collectors.toList());
219+
220+
Instance rebuilt = Instance.create(InstanceId.of("id")).apply(relevantEvents);
221+
222+
assertThat(rebuilt).isEqualTo(instance);
223+
}
200224
}

0 commit comments

Comments
 (0)