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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.dapr.actors.runtime.AbstractActor;
import io.dapr.actors.runtime.ActorRuntimeContext;
import io.dapr.actors.runtime.Remindable;
import io.dapr.utils.TypeRef;
import reactor.core.publisher.Mono;

import java.text.DateFormat;
Expand Down Expand Up @@ -121,8 +122,8 @@ public void clock(String message) {
* @return Class for reminder's state.
*/
@Override
public Class<Integer> getStateType() {
return Integer.class;
public TypeRef<Integer> getStateType() {
return TypeRef.INT;
}

/**
Expand Down
22 changes: 22 additions & 0 deletions sdk-actors/src/main/java/io/dapr/actors/client/ActorProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.dapr.actors.client;

import io.dapr.actors.ActorId;
import io.dapr.utils.TypeRef;
import reactor.core.publisher.Mono;

/**
Expand All @@ -27,6 +28,16 @@ public interface ActorProxy {
*/
String getActorType();

/**
* Invokes an Actor method on Dapr.
*
* @param methodName Method name to invoke.
* @param type The type of the return class.
* @param <T> The type to be returned.
* @return Asynchronous result with the Actor's response.
*/
<T> Mono<T> invokeActorMethod(String methodName, TypeRef<T> type);

/**
* Invokes an Actor method on Dapr.
*
Expand All @@ -37,6 +48,17 @@ public interface ActorProxy {
*/
<T> Mono<T> invokeActorMethod(String methodName, Class<T> clazz);

/**
* Invokes an Actor method on Dapr.
*
* @param methodName Method name to invoke.
* @param data Object with the data.
* @param type The type of the return class.
* @param <T> The type to be returned.
* @return Asynchronous result with the Actor's response.
*/
<T> Mono<T> invokeActorMethod(String methodName, Object data, TypeRef<T> type);

/**
* Invokes an Actor method on Dapr.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.dapr.actors.ActorId;
import io.dapr.actors.ActorMethod;
import io.dapr.serializer.DaprObjectSerializer;
import io.dapr.utils.TypeRef;
import reactor.core.publisher.Mono;

import java.io.IOException;
Expand Down Expand Up @@ -72,20 +73,36 @@ public String getActorType() {
* {@inheritDoc}
*/
@Override
public <T> Mono<T> invokeActorMethod(String methodName, Object data, Class<T> clazz) {
public <T> Mono<T> invokeActorMethod(String methodName, Object data, TypeRef<T> type) {
return this.daprClient.invokeActorMethod(actorType, actorId.toString(), methodName, this.serialize(data))
.filter(s -> s.length > 0)
.map(s -> deserialize(s, clazz));
.map(s -> deserialize(s, type));
}

/**
* {@inheritDoc}
*/
@Override
public <T> Mono<T> invokeActorMethod(String methodName, Class<T> clazz) {
public <T> Mono<T> invokeActorMethod(String methodName, Object data, Class<T> clazz) {
return this.invokeActorMethod(methodName, data, TypeRef.get(clazz));
}

/**
* {@inheritDoc}
*/
@Override
public <T> Mono<T> invokeActorMethod(String methodName, TypeRef<T> type) {
return this.daprClient.invokeActorMethod(actorType, actorId.toString(), methodName, null)
.filter(s -> s.length > 0)
.map(s -> deserialize(s, clazz));
.map(s -> deserialize(s, type));
}

/**
* {@inheritDoc}
*/
@Override
public <T> Mono<T> invokeActorMethod(String methodName, Class<T> clazz) {
return this.invokeActorMethod(methodName, TypeRef.get(clazz));
}

/**
Expand Down Expand Up @@ -147,14 +164,14 @@ public Object invoke(Object proxy, Method method, Object[] args) {
* Extracts the response object from the Actor's method result.
*
* @param response response returned by API.
* @param clazz Expected response class.
* @param type Expected response type.
* @param <T> Expected response type.
* @return Response object or null.
* @throws RuntimeException In case it cannot generate Object.
*/
private <T> T deserialize(final byte[] response, Class<T> clazz) {
private <T> T deserialize(final byte[] response, TypeRef<T> type) {
try {
return this.serializer.deserialize(response, clazz);
return this.serializer.deserialize(response, type);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.dapr.actors.runtime;

import io.dapr.actors.ActorId;
import io.dapr.utils.TypeRef;
import reactor.core.publisher.Mono;

import java.io.IOException;
Expand Down Expand Up @@ -253,7 +254,7 @@ private Mono<byte[]> invokeMethod(ActorId actorId, ActorMethodContext context, S
if (method.getParameterCount() == 1) {
// Actor methods must have a one or no parameter, which is guaranteed at this point.
Class<?> inputClass = method.getParameterTypes()[0];
input = this.runtimeContext.getObjectSerializer().deserialize(request, inputClass);
input = this.runtimeContext.getObjectSerializer().deserialize(request, TypeRef.get(inputClass));
}

if (method.getReturnType().equals(Mono.class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.dapr.actors.runtime;

import io.dapr.actors.ActorId;
import io.dapr.utils.TypeRef;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
Expand Down Expand Up @@ -101,6 +102,18 @@ public <T> Mono<Void> add(String stateName, T value) {
* @return Asynchronous response with fetched object.
*/
public <T> Mono<T> get(String stateName, Class<T> clazz) {
return this.get(stateName, TypeRef.get(clazz));
}

/**
* Fetches the most recent value for the given state, including cached value.
*
* @param stateName Name of the state.
* @param type Class type for the value being fetched.
* @param <T> Type being fetched.
* @return Asynchronous response with fetched object.
*/
public <T> Mono<T> get(String stateName, TypeRef<T> type) {
return Mono.fromSupplier(() -> {
if (stateName == null) {
throw new IllegalArgumentException("State's name cannot be null.");
Expand All @@ -118,7 +131,7 @@ public <T> Mono<T> get(String stateName, Class<T> clazz) {

return (T) null;
}).switchIfEmpty(
this.stateProvider.load(this.actorTypeName, this.actorId, stateName, clazz)
this.stateProvider.load(this.actorTypeName, this.actorId, stateName, type)
.switchIfEmpty(Mono.error(new NoSuchElementException("State not found: " + stateName)))
.map(v -> {
this.stateChangeTracker.put(stateName, new StateChangeMetadata(ActorStateChangeKind.NONE, v));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.dapr.serializer.DaprObjectSerializer;
import io.dapr.serializer.DefaultObjectSerializer;
import io.dapr.utils.Properties;
import io.dapr.utils.TypeRef;
import reactor.core.publisher.Mono;

import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -59,12 +60,12 @@ class DaprStateAsyncProvider {
this.isStateSerializerDefault = stateSerializer.getClass() == DefaultObjectSerializer.class;
}

<T> Mono<T> load(String actorType, ActorId actorId, String stateName, Class<T> clazz) {
<T> Mono<T> load(String actorType, ActorId actorId, String stateName, TypeRef<T> type) {
Mono<byte[]> result = this.daprClient.getActorState(actorType, actorId.toString(), stateName);

return result.flatMap(s -> {
try {
T response = this.stateSerializer.deserialize(s, clazz);
T response = this.stateSerializer.deserialize(s, type);
if (response == null) {
return Mono.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.dapr.actors.runtime;

import io.dapr.utils.TypeRef;
import reactor.core.publisher.Mono;

import java.time.Duration;
Expand All @@ -15,11 +16,11 @@
public interface Remindable<T> {

/**
* Gets the class for state object.
* Gets the type for state object.
*
* @return Class for state object.
*/
Class<T> getStateType();
TypeRef<T> getStateType();

/**
* The reminder call back invoked when an actor reminder is triggered.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.io.IOException;
import java.time.Duration;
import java.util.concurrent.atomic.AtomicInteger;

import io.dapr.utils.TypeRef;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -96,8 +98,8 @@ public MyActorImpl(ActorRuntimeContext runtimeContext, ActorId id) {
}

@Override
public Class<String> getStateType() {
return String.class;
public TypeRef<String> getStateType() {
return TypeRef.STRING;
}

@Override
Expand All @@ -124,8 +126,8 @@ public void activateThenInvoke() throws Exception {
this.manager.activateActor(actorId).block();
byte[] response = this.manager.invokeMethod(actorId, "say", message).block();
Assert.assertEquals(executeSayMethod(
this.context.getObjectSerializer().deserialize(message, String.class)),
this.context.getObjectSerializer().deserialize(response, String.class));
this.context.getObjectSerializer().deserialize(message, TypeRef.STRING)),
this.context.getObjectSerializer().deserialize(response, TypeRef.STRING));
}

@Test
Expand Down Expand Up @@ -192,8 +194,8 @@ public void activateInvokeDeactivateThenInvoke() throws Exception {
this.manager.activateActor(actorId).block();
byte[] response = this.manager.invokeMethod(actorId, "say", message).block();
Assert.assertEquals(executeSayMethod(
this.context.getObjectSerializer().deserialize(message, String.class)),
this.context.getObjectSerializer().deserialize(response, String.class));
this.context.getObjectSerializer().deserialize(message, TypeRef.STRING)),
this.context.getObjectSerializer().deserialize(response, TypeRef.STRING));

this.manager.deactivateActor(actorId).block();
this.manager.invokeMethod(actorId, "say", message).block();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.dapr.actors.client.ActorProxyForTestsImpl;
import io.dapr.actors.client.DaprClientStub;
import io.dapr.serializer.DefaultObjectSerializer;
import io.dapr.utils.TypeRef;
import org.junit.Assert;
import org.junit.Test;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -247,9 +248,9 @@ public String getIdString() {
}

@Override
public Class<String> getStateType() {
public TypeRef<String> getStateType() {
// Remindable type.
return String.class;
return TypeRef.STRING;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.dapr.actors.runtime;

import io.dapr.actors.ActorType;
import io.dapr.utils.TypeRef;
import org.junit.Assert;
import org.junit.Test;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -64,7 +65,7 @@ class A extends AbstractActor implements MyActor, Remindable {
}

@Override
public Class getStateType() {
public TypeRef getStateType() {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import io.dapr.actors.ActorId;
import io.dapr.serializer.DaprObjectSerializer;
import io.dapr.utils.TypeRef;
import reactor.core.publisher.Mono;

import java.io.IOException;
Expand All @@ -28,15 +29,15 @@ public class DaprInMemoryStateProvider extends DaprStateAsyncProvider {
}

@Override
<T> Mono<T> load(String actorType, ActorId actorId, String stateName, Class<T> clazz) {
<T> Mono<T> load(String actorType, ActorId actorId, String stateName, TypeRef<T> type) {
return Mono.fromSupplier(() -> {
try {
String stateId = this.buildId(actorType, actorId, stateName);
if (!stateStore.containsKey(stateId)) {
throw new IllegalStateException("State not found.");
}

return this.serializer.deserialize(this.stateStore.get(stateId), clazz);
return this.serializer.deserialize(this.stateStore.get(stateId), type);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.dapr.actors.ActorId;
import io.dapr.serializer.DaprObjectSerializer;
import io.dapr.serializer.DefaultObjectSerializer;
import io.dapr.utils.TypeRef;
import org.junit.Assert;
import org.junit.Test;
import reactor.core.publisher.Mono;
Expand Down Expand Up @@ -170,23 +171,24 @@ public void happyCaseLoad() throws Exception {
DaprStateAsyncProvider provider = new DaprStateAsyncProvider(daprClient, SERIALIZER);

Assert.assertEquals("Jon Doe",
provider.load("MyActor", new ActorId("123"), "name", String.class).block());
provider.load("MyActor", new ActorId("123"), "name", TypeRef.STRING).block());
Assert.assertEquals(98021,
(int)provider.load("MyActor", new ActorId("123"), "zipcode", int.class).block());
(int)provider.load("MyActor", new ActorId("123"), "zipcode", TypeRef.INT).block());
Assert.assertEquals(98,
(int) provider.load("MyActor", new ActorId("123"), "goals", int.class).block());
(int) provider.load("MyActor", new ActorId("123"), "goals", TypeRef.INT).block());
Assert.assertEquals(98,
(int) provider.load("MyActor", new ActorId("123"), "goals", int.class).block());
(int) provider.load("MyActor", new ActorId("123"), "goals", TypeRef.INT).block());
Assert.assertEquals(46.55,
(double) provider.load("MyActor", new ActorId("123"), "balance", double.class).block(),
(double) provider.load("MyActor", new ActorId("123"), "balance", TypeRef.DOUBLE).block(),
EPSILON);
Assert.assertEquals(true,
(boolean) provider.load("MyActor", new ActorId("123"), "active", boolean.class).block());
(boolean) provider.load("MyActor", new ActorId("123"), "active", TypeRef.BOOLEAN).block());
Assert.assertEquals(new Customer().setId(1000).setName("Roxane"),
provider.load("MyActor", new ActorId("123"), "customer", Customer.class).block());
provider.load("MyActor", new ActorId("123"), "customer", TypeRef.get(Customer.class)).block());
Assert.assertNotEquals(new Customer().setId(1000).setName("Roxane"),
provider.load("MyActor", new ActorId("123"), "anotherCustomer", Customer.class).block());
Assert.assertNull(provider.load("MyActor", new ActorId("123"), "nullCustomer", Customer.class).block());
provider.load("MyActor", new ActorId("123"), "anotherCustomer", TypeRef.get(Customer.class)).block());
Assert.assertNull(
provider.load("MyActor", new ActorId("123"), "nullCustomer", TypeRef.get(Customer.class)).block());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.dapr.actors.runtime;

import io.dapr.serializer.DaprObjectSerializer;
import io.dapr.utils.TypeRef;

import java.io.*;

Expand All @@ -32,7 +33,7 @@ public byte[] serialize(Object o) throws IOException {
* {@inheritDoc}
*/
@Override
public <T> T deserialize(byte[] data, Class<T> clazz) throws IOException {
public <T> T deserialize(byte[] data, TypeRef<T> type) throws IOException {
try (ByteArrayInputStream bis = new ByteArrayInputStream(data)) {
try (ObjectInputStream ois = new ObjectInputStream(bis)) {
try {
Expand Down
Loading