Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 34 additions & 47 deletions src/main/java/io/eigr/spawn/api/ActorRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import com.google.protobuf.GeneratedMessageV3;
import io.eigr.functions.protocol.Protocol;
import io.eigr.functions.protocol.actors.ActorOuterClass;
import io.eigr.spawn.api.exceptions.ActorInvokeException;
import io.eigr.spawn.api.exceptions.ActorCreationException;
import io.eigr.spawn.api.exceptions.ActorInvocationException;
import io.eigr.spawn.api.exceptions.ActorNotFoundException;
import io.eigr.spawn.api.exceptions.SpawnException;
import io.eigr.spawn.internal.transport.client.SpawnClient;

import java.time.Duration;
Expand Down Expand Up @@ -49,7 +51,7 @@ private ActorRef(ActorOuterClass.ActorId actorId, SpawnClient client) {
* @return the ActorRef instance
* @since 0.0.1
*/
protected static ActorRef of(SpawnClient client, String system, String name) throws Exception {
protected static ActorRef of(SpawnClient client, String system, String name) throws ActorCreationException {
ActorOuterClass.ActorId actorId = buildActorId(system, name);
ActorRef ref = ACTOR_REF_CACHE.getIfPresent(actorId);
if (Objects.nonNull(ref)) {
Expand All @@ -72,7 +74,7 @@ protected static ActorRef of(SpawnClient client, String system, String name) thr
* @return the ActorRef instance
* @since 0.0.1
*/
protected static ActorRef of(SpawnClient client, String system, String name, String parent) throws Exception {
protected static ActorRef of(SpawnClient client, String system, String name, String parent) throws ActorCreationException {
ActorOuterClass.ActorId actorId = buildActorId(system, name, parent);
ActorRef ref = ACTOR_REF_CACHE.getIfPresent(actorId);
if (Objects.nonNull(ref)) {
Expand Down Expand Up @@ -101,7 +103,7 @@ private static ActorOuterClass.ActorId buildActorId(String system, String name,
.build();
}

private static void spawnActor(ActorOuterClass.ActorId actorId, SpawnClient client) throws Exception {
private static void spawnActor(ActorOuterClass.ActorId actorId, SpawnClient client) throws ActorCreationException {
Protocol.SpawnRequest req = Protocol.SpawnRequest.newBuilder()
.addActors(actorId)
.build();
Expand All @@ -118,13 +120,10 @@ private static void spawnActor(ActorOuterClass.ActorId actorId, SpawnClient clie
* @return an Optional containing, or not, the response object to the Action call
* @since 0.0.1
*/
public <T extends GeneratedMessageV3> Optional<T> invoke(String action, Class<T> outputType) throws Exception {
public <T extends GeneratedMessageV3> Optional<T> invoke(String action, Class<T> outputType) throws ActorInvocationException {
Optional<T> res = invokeActor(action, Empty.getDefaultInstance(), outputType, Optional.empty());
if (res.isPresent()) {
return Optional.of(outputType.cast(res.get()));
}
return res.map(outputType::cast);

return res;
}

/**
Expand All @@ -139,13 +138,10 @@ public <T extends GeneratedMessageV3> Optional<T> invoke(String action, Class<T>
* @return an Optional containing, or not, the response object to the Action call
* @since 0.0.1
*/
public <T extends GeneratedMessageV3> Optional<T> invoke(String action, Class<T> outputType, InvocationOpts opts) throws Exception {
public <T extends GeneratedMessageV3> Optional<T> invoke(String action, Class<T> outputType, InvocationOpts opts) throws ActorInvocationException {
Optional<T> res = invokeActor(action, Empty.getDefaultInstance(), outputType, Optional.ofNullable(opts));
if (res.isPresent()) {
return Optional.of(outputType.cast(res.get()));
}
return res.map(outputType::cast);

return res;
}

/**
Expand All @@ -159,13 +155,10 @@ public <T extends GeneratedMessageV3> Optional<T> invoke(String action, Class<T>
* @return an Optional containing, or not, the response object to the Action call
* @since 0.0.1
*/
public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T> invoke(String action, S value, Class<T> outputType) throws Exception {
public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T> invoke(String action, S value, Class<T> outputType) throws ActorInvocationException {
Optional<T> res = invokeActor(action, value, outputType, Optional.empty());
if (res.isPresent()) {
return Optional.of(outputType.cast(res.get()));
}
return res.map(outputType::cast);

return res;
}

/**
Expand All @@ -181,13 +174,10 @@ public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T>
* @return an Optional containing, or not, the response object to the Action call
* @since 0.0.1
*/
public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T> invoke(String action, S value, Class<T> outputType, InvocationOpts opts) throws Exception {
public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T> invoke(String action, S value, Class<T> outputType, InvocationOpts opts) throws ActorInvocationException {
Optional<T> res = invokeActor(action, value, outputType, Optional.ofNullable(opts));
if (res.isPresent()) {
return Optional.of(outputType.cast(res.get()));
}
return res.map(outputType::cast);

return res;
}

/**
Expand All @@ -198,7 +188,7 @@ public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T>
* @param action name of the action to be called.
* @since 0.0.1
*/
public <T extends GeneratedMessageV3> void invokeAsync(String action) throws Exception {
public <T extends GeneratedMessageV3> void invokeAsync(String action) throws ActorInvocationException {
InvocationOpts opts = InvocationOpts.builder().async(true).build();
invokeActor(action, Empty.getDefaultInstance(), null, Optional.of(opts));
}
Expand All @@ -213,10 +203,10 @@ public <T extends GeneratedMessageV3> void invokeAsync(String action) throws Exc
* Please see the {@link io.eigr.spawn.api.InvocationOpts} class for more information
* @since 0.0.1
*/
public <T extends GeneratedMessageV3> void invokeAsync(String action, InvocationOpts opts) throws Exception {
public <T extends GeneratedMessageV3> void invokeAsync(String action, InvocationOpts opts) throws ActorInvocationException {
InvocationOpts mergedOpts = InvocationOpts.builder()
.async(true)
.delay(opts.getDelay())
.delaySeconds(opts.getDelaySeconds())
.scheduledTo(opts.getScheduledTo())
.timeoutSeconds(opts.getTimeoutSeconds())
.build();
Expand All @@ -233,7 +223,7 @@ public <T extends GeneratedMessageV3> void invokeAsync(String action, Invocation
* @param value the action argument object.
* @since 0.0.1
*/
public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> void invokeAsync(String action, S value) throws Exception {
public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> void invokeAsync(String action, S value) throws ActorInvocationException {
InvocationOpts opts = InvocationOpts.builder().async(true).build();
invokeActor(action, value, null, Optional.of(opts));
}
Expand All @@ -249,10 +239,10 @@ public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> void invokeA
* Please see the {@link io.eigr.spawn.api.InvocationOpts} class for more information
* @since 0.0.1
*/
public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> void invokeAsync(String action, S value, InvocationOpts opts) throws Exception {
public <T extends GeneratedMessageV3, S extends GeneratedMessageV3> void invokeAsync(String action, S value, InvocationOpts opts) throws ActorInvocationException {
InvocationOpts mergedOpts = InvocationOpts.builder()
.async(true)
.delay(opts.getDelay())
.delaySeconds(opts.getDelaySeconds())
.scheduledTo(opts.getScheduledTo())
.timeoutSeconds(opts.getTimeoutSeconds())
.build();
Expand Down Expand Up @@ -285,33 +275,27 @@ public boolean isUnNamedActor() {
}

private <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T> invokeActor(
String cmd, S argument, Class<T> outputType, Optional<InvocationOpts> options) throws Exception {
String cmd, S argument, Class<T> outputType, Optional<InvocationOpts> options) throws ActorInvocationException {
Objects.requireNonNull(this.actorId, "ActorId cannot be null");

Protocol.InvocationRequest.Builder invocationRequestBuilder = Protocol.InvocationRequest.newBuilder();

Map<String, String> metadata = new HashMap<>();
if (options.isPresent()) {
InvocationOpts opts = options.get();
options.ifPresent(opts -> {
invocationRequestBuilder.setAsync(opts.isAsync());

if (opts.getDelay().isPresent() && !opts.getScheduledTo().isPresent()) {
invocationRequestBuilder.setScheduledTo(opts.getDelay().get());
} else if (opts.getScheduledTo().isPresent()) {
invocationRequestBuilder.setScheduledTo(opts.getScheduleTimeInLong());
}

metadata.put("request-timeout", String.valueOf(opts.getTimeout()));
}
opts.getDelaySeconds().ifPresent(invocationRequestBuilder::setScheduledTo);
// 'scheduledTo' override 'delay' if both is set.
opts.getScheduledTo()
.ifPresent(scheduleTo -> invocationRequestBuilder.setScheduledTo(opts.getScheduleTimeInLong()));
});

final ActorOuterClass.Actor actorRef = ActorOuterClass.Actor.newBuilder()
.setId(this.actorId)
.build();

Any commandArg = Any.pack(argument);



invocationRequestBuilder
.setSystem(ActorOuterClass.ActorSystem.newBuilder().setName(this.actorId.getSystem()).build())
.setActor(actorRef)
Expand All @@ -328,13 +312,16 @@ private <T extends GeneratedMessageV3, S extends GeneratedMessageV3> Optional<T>
case UNRECOGNIZED:
String msg = String.format("Error when trying to invoke Actor %s. Details: %s",
this.getActorName(), status.getMessage());

throw new ActorInvokeException(msg);
throw new ActorInvocationException(msg);
case ACTOR_NOT_FOUND:
throw new ActorNotFoundException();
throw new ActorNotFoundException("Actor not found.");
case OK:
if (resp.hasValue() && Objects.nonNull(outputType)) {
return Optional.of(resp.getValue().unpack(outputType));
try {
return Optional.of(resp.getValue().unpack(outputType));
} catch (Exception e) {
throw new ActorInvocationException("Error handling response.", e);
}
}
return Optional.empty();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/eigr/spawn/api/InvocationOpts.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class InvocationOpts {
private Duration timeoutSeconds = Duration.ofSeconds(10);

@Builder.Default
private Optional<Long> delay = Optional.empty();
private Optional<Long> delaySeconds = Optional.empty();

@Builder.Default
private Optional<LocalDateTime> scheduledTo = Optional.empty();
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/io/eigr/spawn/api/actors/workflows/SideEffect.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,12 @@ public static <T extends GeneratedMessageV3> SideEffect to(ActorRef actor, Strin
public Protocol.SideEffect build() {
Protocol.InvocationRequest.Builder requestBuilder = Protocol.InvocationRequest.newBuilder();

if (this.opts.isPresent()) {
InvocationOpts options = this.opts.get();
if (options.getDelay().isPresent() && !options.getScheduledTo().isPresent()) {
requestBuilder.setScheduledTo(options.getDelay().get());
} else if (options.getScheduledTo().isPresent()) {
requestBuilder.setScheduledTo(options.getScheduleTimeInLong());
}
}
opts.ifPresent(invocationOpts -> {
invocationOpts.getDelaySeconds().ifPresent(requestBuilder::setScheduledTo);
// 'scheduledTo' override 'delay' if both is set.
invocationOpts.getScheduledTo()
.ifPresent(scheduleTo -> requestBuilder.setScheduledTo(invocationOpts.getScheduleTimeInLong()));
});

requestBuilder.setSystem(ActorOuterClass.ActorSystem.newBuilder()
.setName(this.actor.getActorSystem())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.eigr.spawn.api.exceptions;

/**
* Actor Creation Exception.
*
* @author Paulo H3nrique Alves
*/
public class ActorCreationException extends SpawnException {

public ActorCreationException() {
super();
}

public ActorCreationException(String s) {
super(s);
}

public ActorCreationException(String message, Throwable cause) {
super(message, cause);
}

public ActorCreationException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.eigr.spawn.api.exceptions;

public class ActorInvocationException extends SpawnException {

public ActorInvocationException() {
super();
}

public ActorInvocationException(String message) {
super(message);
}

public ActorInvocationException(String message, Throwable cause) {
super(message, cause);
}

public ActorInvocationException(Throwable cause) {
super(cause);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
package io.eigr.spawn.api.exceptions;

public final class ActorNotFoundException extends IllegalArgumentException{
/**
* Actor Registration Exception.
*
* @author Paulo H3nrique Alves
*/
public class ActorNotFoundException extends ActorInvocationException {

public ActorNotFoundException() {
super();
}
public ActorNotFoundException(String s) {
super(s);
}

public ActorNotFoundException(String message, Throwable cause) {
super(message, cause);
}

public ActorNotFoundException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.eigr.spawn.api.exceptions;

/**
* Actor Registration Exception.
*
* @author Paulo H3nrique Alves
*/
public class ActorRegistrationException extends SpawnException {

public ActorRegistrationException() {
super();
}
public ActorRegistrationException(String s) {
super(s);
}

public ActorRegistrationException(String message, Throwable cause) {
super(message, cause);
}

public ActorRegistrationException(Throwable cause) {
super(cause);
}
}
25 changes: 25 additions & 0 deletions src/main/java/io/eigr/spawn/api/exceptions/SpawnException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.eigr.spawn.api.exceptions;

/**
* Generic Spawn exception.
*
* @author Paulo H3nrique Alves
*/
public class SpawnException extends Exception {

protected SpawnException() {
super();
}

public SpawnException(String s) {
super(s);
}

public SpawnException(String message, Throwable cause) {
super(message, cause);
}

public SpawnException(Throwable cause) {
super(cause);
}
}
Loading