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
3 changes: 1 addition & 2 deletions sdk/src/main/java/io/dapr/actors/runtime/Actor.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
package io.dapr.actors.runtime;

/**
* TODO - this is the interface user Actor methods should implement to receive
* calls.
* Base interface for inheriting reliable actor interfaces.
*/
public interface Actor {
}
24 changes: 24 additions & 0 deletions sdk/src/main/java/io/dapr/actors/runtime/ActorFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License.
*/

package io.dapr.actors.runtime;

import io.dapr.actors.ActorId;

/**
* Creates an actor of a given type.
* @param <T> Actor Type to be created.
*/
@FunctionalInterface
public interface ActorFactory<T extends AbstractActor> {

/**
* Creates an Actor.
* @param actorService Actor Service.
* @param actorId Actor Id.
* @return Actor or null it failed.
*/
T createActor(ActorService actorService, ActorId actorId);
}
15 changes: 6 additions & 9 deletions sdk/src/main/java/io/dapr/actors/runtime/ActorRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.function.Function;

/**
* Contains methods to register actor types. Registering the types allows the
Expand Down Expand Up @@ -88,18 +87,16 @@ public <T extends AbstractActor> void RegisterActor(Class<T> clazz) {
* Registers an actor with the runtime.
*
* @param clazz The type of actor.
* @param actorServiceFactory An optional delegate to create actor service.
* @param actorFactory An optional factory to create actors.
* This can be used for dependency injection into actors.
*/
public <T extends AbstractActor> void RegisterActor(Class<T> clazz, Function<ActorTypeInformation, ActorService> actorServiceFactory) {
public <T extends AbstractActor> void RegisterActor(Class<T> clazz, ActorFactory actorFactory) {
Comment thread
artursouza marked this conversation as resolved.
ActorTypeInformation actorTypeInfo = ActorTypeInformation.create(clazz);

ActorService actorService;
if (actorServiceFactory != null) {
actorService = actorServiceFactory.apply(actorTypeInfo);
} else {
actorService = new ActorService(actorTypeInfo);
}
ActorFactory actualActorFactory = actorFactory != null ? actorFactory : new DefaultActorFactory<T>(actorTypeInfo);
// TODO: Refactor into a Builder class.
DaprStateAsyncProvider stateProvider = new DaprStateAsyncProvider(this.appToDaprAsyncClient, new ActorStateProviderSerializer());
ActorService actorService = new ActorServiceImpl(actorTypeInfo, stateProvider, actualActorFactory);

// Create ActorManagers, override existing entry if registered again.
synchronized (this.actorManagers) {
Expand Down
15 changes: 11 additions & 4 deletions sdk/src/main/java/io/dapr/actors/runtime/ActorService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package io.dapr.actors.runtime;

// stub
public class ActorService {
import io.dapr.actors.ActorId;

public ActorService(ActorTypeInformation actorTypeInformation) {
/**
* Interface exposed to Actor's implementations (application layer).
*/
public interface ActorService {

}
/**
* Creates an actor.
* @param actorId Identifier for the Actor to be created.
* @return New Actor instance.
*/
AbstractActor createActor(ActorId actorId);
}
67 changes: 67 additions & 0 deletions sdk/src/main/java/io/dapr/actors/runtime/ActorServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License.
*/

package io.dapr.actors.runtime;

import io.dapr.actors.ActorId;

/**
* Implementation of the Actor Service that contains a state provider.
*/
class ActorServiceImpl implements ActorService {

/**
* Customizable factory for Actors.
*/
private final ActorFactory actorFactory;

/**
* State provider for Actors.
*/
private final DaprStateAsyncProvider stateProvider;

/**
* Information on the {@link Actor} type being serviced.
*/
private final ActorTypeInformation actorTypeInformation;

/**
* Instantiates a stateful service for a given {@link Actor} type.
* @param actorTypeInformation Information on the {@link Actor} type being serviced.
* @param stateProvider State provider for Actors.
* @param actorFactory Customizable factor for Actors.
*/
public ActorServiceImpl(ActorTypeInformation actorTypeInformation, DaprStateAsyncProvider stateProvider, ActorFactory actorFactory) {
this.actorTypeInformation = actorTypeInformation;
this.actorFactory = actorFactory;
this.stateProvider = stateProvider;
}

/**
* Gets the state provider for {@link Actor}.
* @return State provider.
*/
DaprStateAsyncProvider getStateProvider() {
return stateProvider;
}

/**
* Gets the information on the {@link Actor} Type.
* @return Information on the {@link Actor} Type.
*/
ActorTypeInformation getActorTypeInformation() {
return actorTypeInformation;
}

/**
* Creates an {@link Actor} for this service.
* @param actorId Identifier for the Actor to be created.
* @return New {@link Actor} instance.
*/
@Override
public AbstractActor createActor(ActorId actorId) {
return this.actorFactory.createActor(this, actorId);
}
}
67 changes: 67 additions & 0 deletions sdk/src/main/java/io/dapr/actors/runtime/ActorStateChange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License.
*/

package io.dapr.actors.runtime;

import java.io.IOException;

/**
* Represents a state change for an actor.
* @param <T> Type of the value being changed.
*/
public final class ActorStateChange<T> {

/**
* Name of the state being changed.
*/
private final String stateName;

/**
* New value for the state being changed.
*/
private final T value;

/**
* Type of change {@link ActorStateChangeKind}.
*/
private final ActorStateChangeKind changeKind;

/**
* Creates an actor state change.
* @param stateName Name of the state being changed.
* @param value New value for the state being changed.
* @param changeKind Kind of change.
*/
ActorStateChange(String stateName, T value, ActorStateChangeKind changeKind) {
this.stateName = stateName;
this.value = value;
this.changeKind = changeKind;
}

/**
* Gets the name of the state being changed.
* @return Name of the state.
*/
String getStateName() {
return stateName;
}

/**
* Gets the new value of the state being changed.
* @return New value.
*/
T getValue() {
return value;
}

/**
* Gets the kind of change.
* @return Kind of change.
*/
ActorStateChangeKind getChangeKind() {
return changeKind;
}

}
54 changes: 54 additions & 0 deletions sdk/src/main/java/io/dapr/actors/runtime/ActorStateChangeKind.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License.
*/

package io.dapr.actors.runtime;

/**
* Represents an actor's state change
*/
public enum ActorStateChangeKind {

/**
* No change in state.
*/
NONE(""),

/**
* State needs to be added.
*/
ADD("upsert"),

/**
* State needs to be updated.
*/
UPDATE("upsert"),

/**
* State needs to be removed.
*/
REMOVE("delete");

/**
* Operation name in Dapr's state management.
*/
private final String daprStateChangeOperation;

/**
* Creates a kind of actor state change.
* @param daprStateChangeOperation Equivalent operation name Dapr's state management
*/
ActorStateChangeKind(String daprStateChangeOperation) {
this.daprStateChangeOperation = daprStateChangeOperation;
}

/**
* Gets equivalent operation name Dapr's state management
* @return Equivalent operation name Dapr's state management
*/
String getDaprStateChangeOperation() {
return daprStateChangeOperation;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ class ActorStateProviderSerializer {
* @return Array of bytes[] with the serialized content.
* @throws IOException
*/
byte[] serialize(Object state) throws IOException {
return OBJECT_MAPPER.writeValueAsBytes(state);
String serialize(Object state) throws IOException {
return OBJECT_MAPPER.writeValueAsString(state);
}

/**
* Deserializes the byte array into the original object.
*
* @param buffer Array of bytes to be parsed.
* @param json String to be parsed.
* @param clazz Type of the object being deserialized.
* @param <T> Generic type of the object being deserialized.
* @return Object of type T.
* @throws IOException
*/
<T> T deserialize(byte[] buffer, Class<T> clazz) throws IOException {
return OBJECT_MAPPER.readValue(buffer, clazz);
<T> T deserialize(String json, Class<T> clazz) throws IOException {
return OBJECT_MAPPER.readValue(json, clazz);
}

}
Loading