forked from apache/james-project
-
Notifications
You must be signed in to change notification settings - Fork 62
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
JAMES-3142 Rely on event sourcing to correct current group registration #3303
Closed
Closed
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0083e87
JAMES-3142 eventsourcing for group unregistration
chibenwa 5d5ec72
JAMES-3142 Add timestamp & hostname to the event for diagnostic purposes
chibenwa 62368fb
fixup! JAMES-3142 eventsourcing for group unregistration
chibenwa b124ca6
JAMES-3142 s/StartCommand/RequireGroupsCommand/
chibenwa 7b2f233
JAMES-3142 Avoid divergence between registered groups and required gr…
chibenwa 9ddf3df
JAMES-3142 Avoid divergence between registered groups and required gr…
chibenwa 2f99e70
JAMES-3142 Rely on event sourcing for group registration too
chibenwa 924d59e
JAMES-3142 Avoid poluting aggregate history upon noop
chibenwa 398eeaa
JAMES-3117 PeriodicalHealthChecksTest should use reactive health check
chibenwa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...src/main/java/org/apache/james/mailbox/events/eventsourcing/GroupUnregistringManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/**************************************************************** | ||
* Licensed to the Apache Software Foundation (ASF) under one * | ||
* or more contributor license agreements. See the NOTICE file * | ||
* distributed with this work for additional information * | ||
* regarding copyright ownership. The ASF licenses this file * | ||
* to you under the Apache License, Version 2.0 (the * | ||
* "License"); you may not use this file except in compliance * | ||
* with the License. You may obtain a copy of the License at * | ||
* * | ||
* http://www.apache.org/licenses/LICENSE-2.0 * | ||
* * | ||
* Unless required by applicable law or agreed to in writing, * | ||
* software distributed under the License is distributed on an * | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * | ||
* KIND, either express or implied. See the License for the * | ||
* specific language governing permissions and limitations * | ||
* under the License. * | ||
****************************************************************/ | ||
|
||
package org.apache.james.mailbox.events.eventsourcing; | ||
|
||
import static org.apache.james.mailbox.events.eventsourcing.RegisteredGroupsSubscriber.Registrer; | ||
|
||
import java.time.Clock; | ||
|
||
import org.apache.james.eventsourcing.EventSourcingSystem; | ||
import org.apache.james.eventsourcing.eventstore.EventStore; | ||
import org.apache.james.mailbox.events.Group; | ||
import org.apache.james.mailbox.events.eventsourcing.RegisteredGroupsSubscriber.Unregisterer; | ||
import org.reactivestreams.Publisher; | ||
|
||
import com.github.steveash.guavate.Guavate; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.collect.ImmutableSet; | ||
|
||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
public class GroupUnregistringManager { | ||
@FunctionalInterface | ||
public interface RegisteredGroupsProvider { | ||
Publisher<Group> registeredGroups(); | ||
} | ||
|
||
private final EventSourcingSystem eventSourcingSystem; | ||
private final EventStore eventStore; | ||
private final RegisteredGroupsProvider registeredGroupsProvider; | ||
|
||
public GroupUnregistringManager(EventStore eventStore, | ||
Unregisterer unregisterer, | ||
Registrer registrer, | ||
RegisteredGroupsProvider registeredGroupsProvider, | ||
Clock clock) { | ||
|
||
this.eventStore = eventStore; | ||
this.registeredGroupsProvider = registeredGroupsProvider; | ||
this.eventSourcingSystem = EventSourcingSystem.fromJava(ImmutableSet.of(new RequireGroupsCommandHandler(eventStore, clock)), | ||
ImmutableSet.of(new RegisteredGroupsSubscriber(unregisterer, registrer)), | ||
this.eventStore); | ||
} | ||
|
||
public Mono<Void> start(ImmutableSet<Group> requiredGroups) { | ||
return Flux.from(registeredGroupsProvider.registeredGroups()) | ||
.collect(Guavate.toImmutableSet()) | ||
.map(registeredGroups -> new RequireGroupsCommand(requiredGroups, registeredGroups)) | ||
.flatMap(command -> Mono.from(eventSourcingSystem.dispatch(command))); | ||
} | ||
|
||
@VisibleForTesting | ||
Mono<ImmutableSet<Group>> requiredGroups() { | ||
return Mono.from(eventStore.getEventsOfAggregate(RegisteredGroupsAggregate.AGGREGATE_ID)) | ||
.map(RegisteredGroupsAggregate::load) | ||
.map(RegisteredGroupsAggregate::requiredGroups); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
.../event-rabbitmq/src/main/java/org/apache/james/mailbox/events/eventsourcing/Hostname.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/**************************************************************** | ||
* Licensed to the Apache Software Foundation (ASF) under one * | ||
* or more contributor license agreements. See the NOTICE file * | ||
* distributed with this work for additional information * | ||
* regarding copyright ownership. The ASF licenses this file * | ||
* to you under the Apache License, Version 2.0 (the * | ||
* "License"); you may not use this file except in compliance * | ||
* with the License. You may obtain a copy of the License at * | ||
* * | ||
* http://www.apache.org/licenses/LICENSE-2.0 * | ||
* * | ||
* Unless required by applicable law or agreed to in writing, * | ||
* software distributed under the License is distributed on an * | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * | ||
* KIND, either express or implied. See the License for the * | ||
* specific language governing permissions and limitations * | ||
* under the License. * | ||
****************************************************************/ | ||
|
||
package org.apache.james.mailbox.events.eventsourcing; | ||
|
||
import java.io.UncheckedIOException; | ||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
import java.util.Objects; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import com.google.common.base.Preconditions; | ||
|
||
public class Hostname { | ||
public static Hostname of(String value) { | ||
Preconditions.checkNotNull(value, "'value' should not be null"); | ||
Preconditions.checkArgument(StringUtils.isBlank(value), "'value' "); | ||
|
||
return new Hostname(value); | ||
} | ||
|
||
public static Hostname localHost() { | ||
try { | ||
return new Hostname(InetAddress.getLocalHost().getHostName()); | ||
} catch (UnknownHostException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
private final String value; | ||
|
||
private Hostname(String value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public final boolean equals(Object o) { | ||
if (o instanceof Hostname) { | ||
Hostname hostname = (Hostname) o; | ||
|
||
return Objects.equals(this.value, hostname.value); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public final int hashCode() { | ||
return Objects.hash(value); | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
...ava/org/apache/james/mailbox/events/eventsourcing/RegisteredGroupListenerChangeEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/**************************************************************** | ||
* Licensed to the Apache Software Foundation (ASF) under one * | ||
* or more contributor license agreements. See the NOTICE file * | ||
* distributed with this work for additional information * | ||
* regarding copyright ownership. The ASF licenses this file * | ||
* to you under the Apache License, Version 2.0 (the * | ||
* "License"); you may not use this file except in compliance * | ||
* with the License. You may obtain a copy of the License at * | ||
* * | ||
* http://www.apache.org/licenses/LICENSE-2.0 * | ||
* * | ||
* Unless required by applicable law or agreed to in writing, * | ||
* software distributed under the License is distributed on an * | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * | ||
* KIND, either express or implied. See the License for the * | ||
* specific language governing permissions and limitations * | ||
* under the License. * | ||
****************************************************************/ | ||
|
||
package org.apache.james.mailbox.events.eventsourcing; | ||
|
||
import static org.apache.james.mailbox.events.eventsourcing.RegisteredGroupsAggregate.AGGREGATE_ID; | ||
|
||
import java.time.ZonedDateTime; | ||
import java.util.Objects; | ||
|
||
import org.apache.james.eventsourcing.AggregateId; | ||
import org.apache.james.eventsourcing.Event; | ||
import org.apache.james.eventsourcing.EventId; | ||
import org.apache.james.mailbox.events.Group; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
|
||
public class RegisteredGroupListenerChangeEvent implements Event { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are not describing events (like |
||
private final EventId eventId; | ||
private final Hostname hostname; | ||
private final ZonedDateTime zonedDateTime; | ||
private final ImmutableSet<Group> requiredGroups; | ||
private final ImmutableSet<Group> registeredGroups; | ||
|
||
public RegisteredGroupListenerChangeEvent(EventId eventId, Hostname hostname, ZonedDateTime zonedDateTime, ImmutableSet<Group> requiredGroups, ImmutableSet<Group> registeredGroups) { | ||
this.hostname = hostname; | ||
this.zonedDateTime = zonedDateTime; | ||
this.eventId = eventId; | ||
this.requiredGroups = requiredGroups; | ||
this.registeredGroups = registeredGroups; | ||
} | ||
|
||
@Override | ||
public EventId eventId() { | ||
return eventId; | ||
} | ||
|
||
@Override | ||
public AggregateId getAggregateId() { | ||
return AGGREGATE_ID; | ||
} | ||
|
||
public ImmutableSet<Group> getRegisteredGroups() { | ||
return registeredGroups; | ||
} | ||
|
||
public ImmutableSet<Group> getRequiredGroups() { | ||
return requiredGroups; | ||
} | ||
|
||
public EventId getEventId() { | ||
return eventId; | ||
} | ||
|
||
public Hostname getHostname() { | ||
return hostname; | ||
} | ||
|
||
public ZonedDateTime getZonedDateTime() { | ||
return zonedDateTime; | ||
} | ||
|
||
@Override | ||
public final boolean equals(Object o) { | ||
if (o instanceof RegisteredGroupListenerChangeEvent) { | ||
RegisteredGroupListenerChangeEvent that = (RegisteredGroupListenerChangeEvent) o; | ||
|
||
return Objects.equals(this.eventId, that.eventId) | ||
&& Objects.equals(this.zonedDateTime, that.zonedDateTime) | ||
&& Objects.equals(this.hostname, that.hostname) | ||
&& Objects.equals(this.registeredGroups, that.registeredGroups) | ||
&& Objects.equals(this.requiredGroups, that.requiredGroups); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public final int hashCode() { | ||
return Objects.hash(eventId, hostname, zonedDateTime, registeredGroups, requiredGroups); | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
...rc/main/java/org/apache/james/mailbox/events/eventsourcing/RegisteredGroupsAggregate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/**************************************************************** | ||
* Licensed to the Apache Software Foundation (ASF) under one * | ||
* or more contributor license agreements. See the NOTICE file * | ||
* distributed with this work for additional information * | ||
* regarding copyright ownership. The ASF licenses this file * | ||
* to you under the Apache License, Version 2.0 (the * | ||
* "License"); you may not use this file except in compliance * | ||
* with the License. You may obtain a copy of the License at * | ||
* * | ||
* http://www.apache.org/licenses/LICENSE-2.0 * | ||
* * | ||
* Unless required by applicable law or agreed to in writing, * | ||
* software distributed under the License is distributed on an * | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * | ||
* KIND, either express or implied. See the License for the * | ||
* specific language governing permissions and limitations * | ||
* under the License. * | ||
****************************************************************/ | ||
|
||
package org.apache.james.mailbox.events.eventsourcing; | ||
|
||
import java.time.Clock; | ||
import java.time.ZonedDateTime; | ||
import java.util.List; | ||
|
||
import org.apache.james.eventsourcing.AggregateId; | ||
import org.apache.james.eventsourcing.Event; | ||
import org.apache.james.eventsourcing.eventstore.History; | ||
import org.apache.james.mailbox.events.Group; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableSet; | ||
|
||
public class RegisteredGroupsAggregate { | ||
static AggregateId AGGREGATE_ID = () -> "RegisteredGroupListenerChangeEvent"; | ||
|
||
private static class State { | ||
static State initial() { | ||
return new State(ImmutableSet.of(), ImmutableSet.of()); | ||
} | ||
|
||
final ImmutableSet<Group> registeredGroups; | ||
final ImmutableSet<Group> requiredGroups; | ||
|
||
public State(ImmutableSet<Group> registeredGroups, ImmutableSet<Group> requiredGroups) { | ||
this.registeredGroups = registeredGroups; | ||
this.requiredGroups = requiredGroups; | ||
} | ||
|
||
private State apply(RegisteredGroupListenerChangeEvent event) { | ||
return new State(event.getRegisteredGroups(), | ||
event.getRequiredGroups()); | ||
} | ||
} | ||
|
||
public static RegisteredGroupsAggregate load(History history) { | ||
return new RegisteredGroupsAggregate(history); | ||
} | ||
|
||
private final History history; | ||
private State state; | ||
|
||
private RegisteredGroupsAggregate(History history) { | ||
this.history = history; | ||
this.state = State.initial(); | ||
|
||
history.getEventsJava() | ||
.forEach(this::apply); | ||
} | ||
|
||
public List<RegisteredGroupListenerChangeEvent> handle(RequireGroupsCommand requireGroupsCommand, Clock clock) { | ||
List<RegisteredGroupListenerChangeEvent> detectedChanges = detectChanges(requireGroupsCommand, clock); | ||
|
||
detectedChanges.forEach(this::apply); | ||
|
||
return detectedChanges; | ||
} | ||
|
||
public ImmutableSet<Group> requiredGroups() { | ||
return state.requiredGroups; | ||
} | ||
|
||
private List<RegisteredGroupListenerChangeEvent> detectChanges(RequireGroupsCommand command, Clock clock) { | ||
boolean historyChange = !state.requiredGroups.equals(command.getRequiredGroups()) || | ||
!state.registeredGroups.equals(command.getRegisteredGroups()); | ||
boolean registeredChangeNeeded = !command.getRegisteredGroups().equals(command.getRequiredGroups()); | ||
|
||
if (historyChange || registeredChangeNeeded) { | ||
return emitEvent(command, clock); | ||
} | ||
return ImmutableList.of(); | ||
} | ||
|
||
private List<RegisteredGroupListenerChangeEvent> emitEvent(RequireGroupsCommand command, Clock clock) { | ||
ZonedDateTime now = ZonedDateTime.ofInstant(clock.instant(), clock.getZone()); | ||
RegisteredGroupListenerChangeEvent event = new RegisteredGroupListenerChangeEvent(history.getNextEventId(), | ||
Hostname.localHost(), | ||
now, | ||
command.getRequiredGroups(), | ||
command.getRegisteredGroups()); | ||
return ImmutableList.of(event); | ||
} | ||
|
||
private void apply(Event event) { | ||
Preconditions.checkArgument(event instanceof RegisteredGroupListenerChangeEvent); | ||
|
||
state = state.apply((RegisteredGroupListenerChangeEvent) event); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you need your aggregate to build your command, that looks like a design violation.