Skip to content

Commit a1d42e6

Browse files
author
Alex Tymchenko
authored
Merge pull request #454 from SpineEventEngine/failure-ctor-less-generation
`Failure` propagation improvements.
2 parents 9f21dec + f2eaf4d commit a1d42e6

File tree

13 files changed

+125
-46
lines changed

13 files changed

+125
-46
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ buildscript {
3333
guavaVersion = '20.0'
3434
protobufGradlePluginVerison = '0.8.0'
3535

36-
spineVersion = '0.8.39-SNAPSHOT'
36+
spineVersion = '0.8.40-SNAPSHOT'
3737

3838
//TODO:2016-12-12:alexander.yevsyukov: Advance the plug-in version together with all
3939
// the components and change the version of the dependency below to
4040
// `spineVersion` defined above.
41-
spineToolsVersion = '0.8.13-SNAPSHOT'
41+
spineToolsVersion = '0.8.15-SNAPSHOT'
4242

4343
// Defines option for the `spine-model-compiler` plugin.
4444
// Indicates whether the generation of the validating builders is enabled.

client/src/main/java/org/spine3/base/FailureThrowable.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ public abstract class FailureThrowable extends Throwable {
4848
/** The moment of creation of this object. */
4949
private final Timestamp timestamp;
5050

51-
protected FailureThrowable(GeneratedMessageV3 unused,
52-
CommandContext ignored,
53-
GeneratedMessageV3 failureMessage) {
51+
protected FailureThrowable(GeneratedMessageV3 failureMessage) {
5452
super();
5553
this.failureMessage = failureMessage;
5654
this.timestamp = getCurrentTime();
@@ -76,7 +74,7 @@ public Timestamp getTimestamp() {
7674
public Failure toFailure(Command command) {
7775
final Any packedMessage = pack(failureMessage);
7876
final FailureContext context = createContext(command);
79-
final FailureId id = Failures.generateId();
77+
final FailureId id = Failures.generateId(command.getId());
8078
return Failure.newBuilder()
8179
.setId(id)
8280
.setMessage(packedMessage)

client/src/main/java/org/spine3/base/Failures.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
package org.spine3.base;
2222

23+
import com.google.common.annotations.VisibleForTesting;
2324
import com.google.protobuf.Any;
2425
import com.google.protobuf.Message;
2526

@@ -34,15 +35,25 @@
3435
*/
3536
public final class Failures {
3637

38+
@VisibleForTesting
39+
static final String FAILURE_ID_FORMAT = "%s-fail";
40+
3741
private Failures() {
3842
// Prevent instantiation of this utility class.
3943
}
4044

41-
/** Generates a new random UUID-based {@code FailureId}. */
42-
public static FailureId generateId() {
43-
final String value = Identifiers.newUuid();
45+
/**
46+
* Generates a {@code FailureId} based upon a {@linkplain CommandId command ID} in a format:
47+
*
48+
* <pre>{@code <commandId>-fail}</pre>
49+
*
50+
* @param id the identifier of the {@linkplain Command command}, which processing caused the
51+
* failure
52+
**/
53+
public static FailureId generateId(CommandId id) {
54+
final String idValue = String.format(FAILURE_ID_FORMAT, id.getUuid());
4455
return FailureId.newBuilder()
45-
.setUuid(value)
56+
.setValue(idValue)
4657
.build();
4758
}
4859

client/src/main/proto/spine/base/failure.proto

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ import "spine/base/version.proto";
3838

3939
// Failure identifier.
4040
message FailureId {
41-
string uuid = 1;
41+
42+
// The value is based upon the ID of the command, which caused the failure.
43+
// It has the following format:
44+
//
45+
// `<commandId>-fail`
46+
//
47+
string value = 1;
4248
}
4349

4450
// A business failure is a condition in business, which does not allow to

client/src/test/java/org/spine3/base/FailureThrowableShould.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private static class TestFailure extends FailureThrowable {
8383
protected TestFailure(GeneratedMessageV3 commandMessage,
8484
CommandContext context,
8585
GeneratedMessageV3 failure) {
86-
super(commandMessage, context, failure);
86+
super(failure);
8787
}
8888

8989
private static final long serialVersionUID = 0L;

client/src/test/java/org/spine3/base/FailuresShould.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
import com.google.common.testing.NullPointerTester;
2424
import org.junit.Test;
2525

26-
import static org.junit.Assert.assertFalse;
26+
import static org.junit.Assert.assertEquals;
27+
import static org.spine3.base.Failures.FAILURE_ID_FORMAT;
2728
import static org.spine3.test.Tests.assertHasPrivateParameterlessCtor;
2829

2930
/**
@@ -40,13 +41,16 @@ public void have_utility_ctor() {
4041
public void pass_null_tolerance_check() {
4142
new NullPointerTester()
4243
.setDefault(Command.class, Command.getDefaultInstance())
44+
.setDefault(CommandId.class, CommandId.getDefaultInstance())
4345
.testAllPublicStaticMethods(Failures.class);
4446
}
4547

4648
@Test
47-
public void generate_failure_id() {
48-
assertFalse(Failures.generateId()
49-
.getUuid()
50-
.isEmpty());
49+
public void generate_failure_id_upon_command_id() {
50+
final CommandId commandId = Commands.generateId();
51+
final FailureId actual = Failures.generateId(commandId);
52+
53+
final String expected = String.format(FAILURE_ID_FORMAT, commandId.getUuid());
54+
assertEquals(expected, actual.getValue());
5155
}
5256
}

server/src/main/java/org/spine3/server/commandbus/CommandBus.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626
import io.grpc.stub.StreamObserver;
2727
import org.spine3.annotations.Internal;
2828
import org.spine3.base.Command;
29+
import org.spine3.base.FailureThrowable;
2930
import org.spine3.base.Identifiers;
3031
import org.spine3.base.Response;
3132
import org.spine3.base.Responses;
3233
import org.spine3.envelope.CommandEnvelope;
34+
import org.spine3.io.StreamObservers;
3335
import org.spine3.server.bus.Bus;
3436
import org.spine3.server.commandstore.CommandStore;
3537
import org.spine3.server.failure.FailureBus;
@@ -242,6 +244,20 @@ void doPost(CommandEnvelope commandEnvelope) {
242244
} catch (RuntimeException e) {
243245
final Throwable cause = getRootCause(e);
244246
commandStore.updateCommandStatus(commandEnvelope, cause, log);
247+
248+
emitFailure(commandEnvelope, cause);
249+
}
250+
}
251+
252+
/**
253+
* Emits the {@code Failure} and posts it to the {@code FailureBus},
254+
* if the given {@code cause} is in fact a {@code FailureThrowable} instance.
255+
*/
256+
private void emitFailure(CommandEnvelope commandEnvelope, Throwable cause) {
257+
if (cause instanceof FailureThrowable) {
258+
final FailureThrowable failure = (FailureThrowable) cause;
259+
failureBus.post(failure.toFailure(commandEnvelope.getCommand()),
260+
StreamObservers.<Response>noOpObserver());
245261
}
246262
}
247263

server/src/main/java/org/spine3/server/entity/AbstractVersionableEntity.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import com.google.protobuf.Message;
2424
import com.google.protobuf.Timestamp;
25-
import org.spine3.base.Command;
2625
import org.spine3.base.Version;
2726
import org.spine3.base.Versions;
2827
import org.spine3.server.entity.failure.CannotModifyArchivedEntity;
@@ -298,34 +297,28 @@ protected void setDeleted(boolean deleted) {
298297
/**
299298
* Ensures that the entity is not marked as {@code archived}.
300299
*
301-
* @param modification the {@linkplain Command} which execution triggered this check
302300
* @throws CannotModifyArchivedEntity if the entity in in the archived status
303301
* @see #getLifecycleFlags()
304302
* @see LifecycleFlags#getArchived()
305303
*/
306-
protected void checkNotArchived(Command modification) throws CannotModifyArchivedEntity {
304+
protected void checkNotArchived() throws CannotModifyArchivedEntity {
307305
if (getLifecycleFlags().getArchived()) {
308306
final String idStr = idToString(getId());
309-
throw new CannotModifyArchivedEntity(modification.getMessage(),
310-
modification.getContext(),
311-
idStr);
307+
throw new CannotModifyArchivedEntity(idStr);
312308
}
313309
}
314310

315311
/**
316312
* Ensures that the entity is not marked as {@code deleted}.
317313
*
318-
* @param modification the {@linkplain Command} which execution triggered this check
319314
* @throws CannotModifyDeletedEntity if the entity is marked as {@code deleted}
320315
* @see #getLifecycleFlags()
321316
* @see LifecycleFlags#getDeleted()
322317
*/
323-
protected void checkNotDeleted(Command modification) throws CannotModifyDeletedEntity {
318+
protected void checkNotDeleted() throws CannotModifyDeletedEntity {
324319
if (getLifecycleFlags().getDeleted()) {
325320
final String idStr = idToString(getId());
326-
throw new CannotModifyDeletedEntity(modification.getMessage(),
327-
modification.getContext(),
328-
idStr);
321+
throw new CannotModifyDeletedEntity(idStr);
329322
}
330323
}
331324

server/src/test/java/org/spine3/server/commandbus/AbstractCommandBusTestSuite.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ protected static Command newCommandWithoutTenantId() {
129129
return invalidCmd;
130130
}
131131

132+
protected static Command clearTenantId(Command cmd) {
133+
final ActorContext.Builder withNoTenant =
134+
ActorContext.newBuilder()
135+
.setTenantId(TenantId.getDefaultInstance());
136+
final Command result = cmd.toBuilder()
137+
.setContext(cmd.getContext()
138+
.toBuilder()
139+
.setActorContext(withNoTenant))
140+
.build();
141+
return result;
142+
}
143+
132144
@Before
133145
public void setUp() {
134146
final InMemoryStorageFactory storageFactory =

server/src/test/java/org/spine3/server/commandbus/CommandStoreShould.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,7 @@ private static class TestFailure extends FailureThrowable {
274274
private static final long serialVersionUID = 1L;
275275

276276
private TestFailure() {
277-
super(Wrapper.forString("some Command message"),
278-
CommandContext.getDefaultInstance(),
279-
Wrapper.forString(TestFailure.class.getName()));
277+
super(Wrapper.forString(TestFailure.class.getName()));
280278
}
281279
}
282280

0 commit comments

Comments
 (0)