Skip to content

Commit

Permalink
Merge pull request #1153 from SpineEventEngine/delivery-improvements
Browse files Browse the repository at this point in the history
Delivery improvements
  • Loading branch information
armiol authored Aug 31, 2019
2 parents efbe3d2 + 0ab5620 commit 1ca7d12
Show file tree
Hide file tree
Showing 48 changed files with 1,975 additions and 268 deletions.
32 changes: 16 additions & 16 deletions license-report.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


# Dependencies of `io.spine:spine-client:1.0.2`
# Dependencies of `io.spine:spine-client:1.0.5-SNAPSHOT`

## Runtime
1. **Group:** com.google.android **Name:** annotations **Version:** 4.1.1.4
Expand Down Expand Up @@ -457,12 +457,12 @@
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.


This report was generated on **Fri Aug 30 13:17:23 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Fri Aug 30 20:32:22 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine:spine-core:1.0.2`
# Dependencies of `io.spine:spine-core:1.0.5-SNAPSHOT`

## Runtime
1. **Group:** com.google.code.findbugs **Name:** jsr305 **Version:** 3.0.2
Expand Down Expand Up @@ -865,12 +865,12 @@ This report was generated on **Fri Aug 30 13:17:23 EEST 2019** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.


This report was generated on **Fri Aug 30 13:17:27 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Fri Aug 30 20:32:22 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine.tools:spine-model-assembler:1.0.2`
# Dependencies of `io.spine.tools:spine-model-assembler:1.0.5-SNAPSHOT`

## Runtime
1. **Group:** com.google.android **Name:** annotations **Version:** 4.1.1.4
Expand Down Expand Up @@ -1321,12 +1321,12 @@ This report was generated on **Fri Aug 30 13:17:27 EEST 2019** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.


This report was generated on **Fri Aug 30 13:17:30 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Fri Aug 30 20:32:23 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine.tools:spine-model-verifier:1.0.2`
# Dependencies of `io.spine.tools:spine-model-verifier:1.0.5-SNAPSHOT`

## Runtime
1. **Group:** aopalliance **Name:** aopalliance **Version:** 1.0
Expand Down Expand Up @@ -1939,12 +1939,12 @@ This report was generated on **Fri Aug 30 13:17:30 EEST 2019** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.


This report was generated on **Fri Aug 30 13:17:34 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Fri Aug 30 20:32:24 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine:spine-server:1.0.2`
# Dependencies of `io.spine:spine-server:1.0.5-SNAPSHOT`

## Runtime
1. **Group:** com.google.android **Name:** annotations **Version:** 4.1.1.4
Expand Down Expand Up @@ -2461,12 +2461,12 @@ This report was generated on **Fri Aug 30 13:17:34 EEST 2019** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.


This report was generated on **Fri Aug 30 13:17:38 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Fri Aug 30 20:32:24 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine:spine-testutil-client:1.0.2`
# Dependencies of `io.spine:spine-testutil-client:1.0.5-SNAPSHOT`

## Runtime
1. **Group:** com.google.android **Name:** annotations **Version:** 4.1.1.4
Expand Down Expand Up @@ -2993,12 +2993,12 @@ This report was generated on **Fri Aug 30 13:17:38 EEST 2019** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.


This report was generated on **Fri Aug 30 13:17:41 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Fri Aug 30 20:32:25 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine:spine-testutil-core:1.0.2`
# Dependencies of `io.spine:spine-testutil-core:1.0.5-SNAPSHOT`

## Runtime
1. **Group:** com.google.android **Name:** annotations **Version:** 4.1.1.4
Expand Down Expand Up @@ -3533,12 +3533,12 @@ This report was generated on **Fri Aug 30 13:17:41 EEST 2019** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.


This report was generated on **Fri Aug 30 13:17:45 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Fri Aug 30 20:32:25 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).




# Dependencies of `io.spine:spine-testutil-server:1.0.2`
# Dependencies of `io.spine:spine-testutil-server:1.0.5-SNAPSHOT`

## Runtime
1. **Group:** com.google.android **Name:** annotations **Version:** 4.1.1.4
Expand Down Expand Up @@ -4119,4 +4119,4 @@ This report was generated on **Fri Aug 30 13:17:45 EEST 2019** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.


This report was generated on **Fri Aug 30 13:17:49 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Fri Aug 30 20:32:26 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ all modules and does not describe the project structure per-subproject.

<groupId>io.spine</groupId>
<artifactId>spine-core-java</artifactId>
<version>1.0.2</version>
<version>1.0.5-SNAPSHOT</version>

<inceptionYear>2015</inceptionYear>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ final class AggregateCommandEndpoint<I, A extends Aggregate<I, ?, ?>>
}

@Override
protected DispatchOutcome invokeDispatcher(A aggregate, CommandEnvelope envelope) {
protected DispatchOutcome invokeDispatcher(A aggregate) {
EntityLifecycle lifecycle = repository().lifecycleOf(aggregate.id());
DispatchCommand<I> dispatch = operationFor(lifecycle, aggregate, envelope);
DispatchCommand<I> dispatch = operationFor(lifecycle, aggregate, envelope());
return dispatch.perform();
}

Expand All @@ -64,7 +64,8 @@ protected void afterDispatched(I entityId) {
* @throws IllegalStateException always
*/
@Override
protected void onEmptyResult(A aggregate, CommandEnvelope cmd) throws IllegalStateException {
protected void onEmptyResult(A aggregate) throws IllegalStateException {
CommandEnvelope cmd = envelope();
String entityId = aggregate.idAsString();
String entityClass = aggregate.getClass()
.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private void storeAndPost(A aggregate, DispatchOutcome outcome) {
} else if (success.hasRejection()) {
post(success.getRejection());
} else {
onEmptyResult(aggregate, envelope());
onEmptyResult(aggregate);
}
afterDispatched(aggregate.id());
}
Expand All @@ -118,7 +118,7 @@ private A loadOrCreate(I aggregateId) {

@CanIgnoreReturnValue
final DispatchOutcome handleAndApplyEvents(A aggregate) {
DispatchOutcome outcome = invokeDispatcher(aggregate, envelope());
DispatchOutcome outcome = invokeDispatcher(aggregate);
Success successfulOutcome = outcome.getSuccess();
return successfulOutcome.hasProducedEvents()
? applyProducedEvents(aggregate, outcome)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ final class AggregateEventReactionEndpoint<I, A extends Aggregate<I, ?, ?>>
}

@Override
protected DispatchOutcome invokeDispatcher(A aggregate, EventEnvelope event) {
return aggregate.reactOn(event);
protected DispatchOutcome invokeDispatcher(A aggregate) {
return aggregate.reactOn(envelope());
}

@Override
Expand All @@ -52,7 +52,7 @@ protected void afterDispatched(I entityId) {
* updated upon reacting on an event.
*/
@Override
protected void onEmptyResult(A aggregate, EventEnvelope event) {
protected void onEmptyResult(A aggregate) {
// Do nothing.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package io.spine.server.aggregate;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets.SetView;
import com.google.errorprone.annotations.OverridingMethodsMustInvokeSuper;
Expand All @@ -33,11 +34,13 @@
import io.spine.server.ServerEnvironment;
import io.spine.server.aggregate.model.AggregateClass;
import io.spine.server.commandbus.CommandDispatcher;
import io.spine.server.delivery.BatchDeliveryListener;
import io.spine.server.delivery.Delivery;
import io.spine.server.delivery.Inbox;
import io.spine.server.delivery.InboxLabel;
import io.spine.server.entity.EntityLifecycle;
import io.spine.server.entity.Repository;
import io.spine.server.entity.RepositoryCache;
import io.spine.server.event.EventBus;
import io.spine.server.event.EventDispatcherDelegate;
import io.spine.server.event.RejectionEnvelope;
Expand Down Expand Up @@ -104,6 +107,8 @@ public abstract class AggregateRepository<I, A extends Aggregate<I, ?, ?>>
*/
private @MonotonicNonNull Inbox<I> inbox;

private @MonotonicNonNull RepositoryCache<I, A> cache;

/** The number of events to store between snapshots. */
private int snapshotTrigger = DEFAULT_SNAPSHOT_TRIGGER;

Expand Down Expand Up @@ -147,10 +152,15 @@ public void registerWith(BoundedContext context) {
context.importBus()
.register(EventImportDispatcher.of(this));
}
initCache(context.isMultitenant());
initInbox();
initMirror();
}

private void initCache(boolean multitenant) {
cache = new RepositoryCache<>(multitenant, this::doLoadOrCreate, this::doStore);
}

/**
* Initializes the {@code Inbox}.
*/
Expand All @@ -159,6 +169,17 @@ private void initInbox() {
.delivery();
inbox = delivery
.<I>newInbox(entityStateType())
.withBatchListener(new BatchDeliveryListener<I>() {
@Override
public void onStart(I id) {
cache.startCaching(id);
}

@Override
public void onEnd(I id) {
cache.stopCaching(id);
}
})
.addEventEndpoint(InboxLabel.REACT_UPON_EVENT,
e -> new AggregateEventReactionEndpoint<>(this, e))
.addEventEndpoint(InboxLabel.IMPORT_EVENT,
Expand Down Expand Up @@ -264,6 +285,11 @@ protected AggregateClass<A> toModelClass(Class<A> cls) {
*/
@Override
protected final void store(A aggregate) {
cache.store(aggregate);
}

@VisibleForTesting
protected void doStore(A aggregate) {
Write<I> operation = Write.operationFor(this, aggregate);
operation.perform();
}
Expand Down Expand Up @@ -533,6 +559,11 @@ protected AggregateStorage<I> aggregateStorage() {
* @return loaded or created aggregate instance
*/
final A loadOrCreate(I id) {
return cache.load(id);
}

@VisibleForTesting
protected A doLoadOrCreate(I id) {
A result = load(id).orElseGet(() -> createNew(id));
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ final class EventImportEndpoint<I, A extends Aggregate<I, ?, ?>>
* applied}.
*/
@Override
protected DispatchOutcome invokeDispatcher(A aggregate, EventEnvelope eventEnvelope) {
Event event = eventEnvelope.outerObject();
protected DispatchOutcome invokeDispatcher(A aggregate) {
Event event = envelope().outerObject();
Success.Builder success = Success.newBuilder();
success.getProducedEventsBuilder()
.addEvent(event);
Expand All @@ -72,8 +72,8 @@ protected void afterDispatched(I entityId) {
}

@Override
protected void onEmptyResult(A aggregate, EventEnvelope event) {
protected void onEmptyResult(A aggregate) {
_error().log("The aggregate `%s` was not modified during the import of the event `%s`.",
aggregate, event);
aggregate, envelope());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2019, TeamDev. All rights reserved.
*
* Redistribution and use in source and/or binary forms, with or without
* modification, must retain the above copyright notice and the following
* disclaimer.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package io.spine.server.delivery;

/**
* Listens to start and end of the {@code Inbox} batch delivery.
*
* <p>When the consequent messages in the {@code Inbox} are targeting the same entity,
* the delivery may be optimized by using either the same transaction or caching the storage
* operations while the batch is delivered.
*
* <p>The implementing classes may define their own behavior and react upon such use cases.
*/
public interface BatchDeliveryListener<I> {

/**
* Invoked before the batch delivery to the target with the given ID is started.
*
* @param id
* the ID of the delivery target
*/
void onStart(I id);

/**
* Invoked after the batch delivery to the target with the given ID is ended.
*
* @param id
* the ID of the delivery target
*/
void onEnd(I id);
}
Loading

0 comments on commit 1ca7d12

Please sign in to comment.