Skip to content

Commit 8362c63

Browse files
wcs1onlyartursouza
andauthored
SDK update for breaking changes in dapr core. (dapr#287)
* Update proto files for 0.8.0 release (dapr#283) * Bump dapr core to master * Update integration tests for 0.8.0 proto changes * Added unit tests to ensure StateOptions enums always map to their equivalent gRPC enums * Remove some uneed comments/imports * Update pub/sub subscription to match new route/metdata format (dapr#278) * Automatically initialize actor on first invocation (dapr#284) * Update integration tests to publish to more than one pubsub topic/route * Bump dapr CLI version * Remove uneeded interface and exception Co-authored-by: Artur Souza <[email protected]>
1 parent e0f3f3c commit 8362c63

File tree

19 files changed

+479
-239
lines changed

19 files changed

+479
-239
lines changed

.github/workflows/build.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ jobs:
2222
GOARCH: amd64
2323
GOPROXY: https://proxy.golang.org
2424
JDK_VER: 13.0.x
25-
DAPR_RUNTIME_VER: 0.6.0
26-
DAPR_INSTALL_URL: https://raw.githubusercontent.com/dapr/cli/f84566fb2bf5a599252ab9d6bd82fc78faf94dba/install/install.sh
27-
DAPR_CLI_REF: 2e607cb6de0bd69770b244020eecd668fcded19b
28-
DAPR_REF: 7d6e111a181d9d3fdd7537e5c7f6c02764064846
25+
DAPR_RUNTIME_VER: 0.7.1
26+
DAPR_INSTALL_URL: https://raw.githubusercontent.com/dapr/cli/98f8818260d941c2662908f686395f8480dabd64/install/install.sh
27+
DAPR_CLI_REF: 98f8818260d941c2662908f686395f8480dabd64
28+
DAPR_REF: 610b92568b1add897ba3e6938a711c0821833966
2929
OSSRH_USER_TOKEN: ${{ secrets.OSSRH_USER_TOKEN }}
3030
OSSRH_PWD_TOKEN: ${{ secrets.OSSRH_PWD_TOKEN }}
3131
GPG_KEY: ${{ secrets.GPG_KEY }}

examples/src/main/java/io/dapr/examples/invoke/grpc/HelloWorldService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
package io.dapr.examples.invoke.grpc;
77

88
import com.google.protobuf.Any;
9+
import io.dapr.v1.AppCallbackGrpc;
910
import io.dapr.v1.CommonProtos;
10-
import io.dapr.v1.DaprClientGrpc;
11-
import io.dapr.v1.DaprClientProtos;
11+
import io.dapr.v1.DaprAppCallbackProtos;
1212
import io.grpc.Server;
1313
import io.grpc.ServerBuilder;
1414
import io.grpc.stub.StreamObserver;
@@ -40,7 +40,7 @@ public class HelloWorldService {
4040
/**
4141
* Server mode: class that encapsulates all server-side logic for Grpc.
4242
*/
43-
private static class GrpcHelloWorldDaprService extends DaprClientGrpc.DaprClientImplBase {
43+
private static class GrpcHelloWorldDaprService extends AppCallbackGrpc.AppCallbackImplBase {
4444

4545
/**
4646
* Format to output date and time.

examples/src/main/java/io/dapr/examples/pubsub/http/SubscriberController.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import io.dapr.Topic;
99
import io.dapr.client.domain.CloudEvent;
10-
import io.dapr.serializer.DefaultObjectSerializer;
1110
import org.springframework.web.bind.annotation.GetMapping;
1211
import org.springframework.web.bind.annotation.PostMapping;
1312
import org.springframework.web.bind.annotation.RequestBody;

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<grpc.version>1.25.0</grpc.version>
1818
<protobuf.version>3.11.0</protobuf.version>
1919
<protoc.version>3.10.0</protoc.version>
20-
<dapr.proto.baseurl>https://raw.githubusercontent.com/dapr/dapr/89aab21ee86d2f65066bbe530809ee73ac75e921/dapr/proto</dapr.proto.baseurl>
20+
<dapr.proto.baseurl>https://raw.githubusercontent.com/dapr/dapr/610b92568b1add897ba3e6938a711c0821833966/dapr/proto</dapr.proto.baseurl>
2121
<os-maven-plugin.version>1.6.2</os-maven-plugin.version>
2222
<maven-dependency-plugin.version>3.1.1</maven-dependency-plugin.version>
2323
<maven-antrun-plugin.version>1.8</maven-antrun-plugin.version>

sdk-actors/src/main/java/io/dapr/actors/runtime/ActorManager.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,13 @@ class ActorManager<T extends AbstractActor> {
5858
* @return Asynchronous void response.
5959
*/
6060
Mono<Void> activateActor(ActorId actorId) {
61-
return Mono.fromSupplier(() -> this.runtimeContext.getActorFactory().createActor(runtimeContext, actorId))
62-
.flatMap(actor -> actor.onActivateInternal().then(this.onActivatedActor(actorId, actor)));
61+
return Mono.fromSupplier(() -> {
62+
if (this.activeActors.containsKey(actorId)) {
63+
return null;
64+
}
65+
66+
return this.runtimeContext.getActorFactory().createActor(runtimeContext, actorId);
67+
}).flatMap(actor -> actor.onActivateInternal().then(this.onActivatedActor(actorId, actor)));
6368
}
6469

6570
/**

sdk-actors/src/main/java/io/dapr/actors/runtime/ActorRuntime.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -198,18 +198,6 @@ public <T extends AbstractActor> void registerActor(
198198
this.config.addRegisteredActorType(actorTypeInfo.getName());
199199
}
200200

201-
/**
202-
* Activates an actor for an actor type with given actor id.
203-
*
204-
* @param actorTypeName Actor type name to activate the actor for.
205-
* @param actorId Actor id for the actor to be activated.
206-
* @return Async void task.
207-
*/
208-
public Mono<Void> activate(String actorTypeName, String actorId) {
209-
return Mono.fromSupplier(() -> this.getActorManager(actorTypeName))
210-
.flatMap(m -> m.activateActor(new ActorId(actorId)));
211-
}
212-
213201
/**
214202
* Deactivates an actor for an actor type with given actor id.
215203
*
@@ -233,8 +221,10 @@ public Mono<Void> deactivate(String actorTypeName, String actorId) {
233221
* @return Response for the actor method.
234222
*/
235223
public Mono<byte[]> invoke(String actorTypeName, String actorId, String actorMethodName, byte[] payload) {
224+
ActorId id = new ActorId(actorId);
236225
return Mono.fromSupplier(() -> this.getActorManager(actorTypeName))
237-
.flatMap(m -> m.invokeMethod(new ActorId(actorId), actorMethodName, payload));
226+
.flatMap(m -> m.activateActor(id).thenReturn(m))
227+
.flatMap(m -> ((ActorManager)m).invokeMethod(id, actorMethodName, payload));
238228
}
239229

240230
/**
@@ -247,8 +237,10 @@ public Mono<byte[]> invoke(String actorTypeName, String actorId, String actorMet
247237
* @return Async void task.
248238
*/
249239
public Mono<Void> invokeReminder(String actorTypeName, String actorId, String reminderName, byte[] params) {
240+
ActorId id = new ActorId(actorId);
250241
return Mono.fromSupplier(() -> this.getActorManager(actorTypeName))
251-
.flatMap(m -> m.invokeReminder(new ActorId(actorId), reminderName, params));
242+
.flatMap(m -> m.activateActor(id).thenReturn(m))
243+
.flatMap(m -> ((ActorManager)m).invokeReminder(new ActorId(actorId), reminderName, params));
252244
}
253245

254246
/**
@@ -260,8 +252,10 @@ public Mono<Void> invokeReminder(String actorTypeName, String actorId, String re
260252
* @return Async void task.
261253
*/
262254
public Mono<Void> invokeTimer(String actorTypeName, String actorId, String timerName) {
255+
ActorId id = new ActorId(actorId);
263256
return Mono.fromSupplier(() -> this.getActorManager(actorTypeName))
264-
.flatMap(m -> m.invokeTimer(new ActorId(actorId), timerName));
257+
.flatMap(m -> m.activateActor(id).thenReturn(m))
258+
.flatMap(m -> ((ActorManager)m).invokeTimer(new ActorId(actorId), timerName));
265259
}
266260

267261
/**

sdk-actors/src/test/java/io/dapr/actors/runtime/ActorRuntimeTest.java

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,60 @@ public class ActorRuntimeTest {
2525

2626
public interface MyActor {
2727
String say();
28+
int count();
2829
}
2930

3031
@ActorType(name = ACTOR_NAME)
3132
public static class MyActorImpl extends AbstractActor implements MyActor {
3233

34+
private int count = 0;
35+
36+
private Boolean activated;
37+
3338
public MyActorImpl(ActorRuntimeContext runtimeContext, ActorId id) {
3439
super(runtimeContext, id);
3540
}
3641

42+
public Mono<Void> onActivate() {
43+
return Mono.fromRunnable(() -> {
44+
if (this.activated != null) {
45+
throw new IllegalStateException("already activated once");
46+
}
47+
48+
this.activated = true;
49+
});
50+
}
51+
52+
public Mono<Void> onDeactivate() {
53+
return Mono.fromRunnable(() -> {
54+
if (this.activated == null) {
55+
throw new IllegalStateException("never activated");
56+
}
57+
58+
if (this.activated == false) {
59+
throw new IllegalStateException("already deactivated");
60+
}
61+
62+
if (this.count == 0) {
63+
throw new IllegalStateException("test expects a call before deactivate");
64+
}
65+
66+
this.activated = false;
67+
});
68+
}
69+
3770
public String say() {
71+
if (!this.activated) {
72+
throw new IllegalStateException("not activated");
73+
}
74+
75+
this.count++;
3876
return "Nothing to say.";
3977
}
78+
79+
public int count() {
80+
return this.count;
81+
}
4082
}
4183

4284
private static final ActorObjectSerializer ACTOR_STATE_SERIALIZER = new ActorObjectSerializer();
@@ -71,63 +113,34 @@ public void registerActor() throws Exception {
71113
Assert.assertTrue(new String(this.runtime.serializeConfig()).contains(ACTOR_NAME));
72114
}
73115

74-
@Test
75-
public void activateActor() throws Exception {
76-
String actorId = UUID.randomUUID().toString();
77-
this.runtime.registerActor(MyActorImpl.class);
78-
this.runtime.activate(ACTOR_NAME, actorId).block();
79-
}
80116

81117
@Test
82118
public void invokeActor() throws Exception {
83119
String actorId = UUID.randomUUID().toString();
84120
this.runtime.registerActor(MyActorImpl.class);
85-
this.runtime.activate(ACTOR_NAME, actorId).block();
86121

87122
byte[] response = this.runtime.invoke(ACTOR_NAME, actorId, "say", null).block();
88123
String message = ACTOR_STATE_SERIALIZER.deserialize(response, String.class);
89124
Assert.assertEquals("Nothing to say.", message);
90125
}
91126

92-
@Test
93-
public void activateThendeactivateActor() throws Exception {
94-
String actorId = UUID.randomUUID().toString();
95-
this.runtime.registerActor(MyActorImpl.class);
96-
this.runtime.activate(ACTOR_NAME, actorId).block();
97-
this.runtime.deactivate(ACTOR_NAME, actorId).block();
98-
}
99-
100127
@Test
101128
public void deactivateActor() throws Exception {
102129
String actorId = UUID.randomUUID().toString();
103130
this.runtime.registerActor(MyActorImpl.class);
104131
this.runtime.deactivate(ACTOR_NAME, actorId).block();
105132
}
106133

107-
@Test
108-
public void lazyActivate() throws Exception {
109-
String actorId = UUID.randomUUID().toString();
110-
this.runtime.registerActor(MyActorImpl.class);
111-
this.runtime.activate(ACTOR_NAME, actorId).block();
112-
113-
this.runtime.invoke(ACTOR_NAME, actorId, "say", null)
114-
.doOnError(e -> Assert.assertTrue(e.getMessage().contains("Could not find actor")))
115-
.doOnSuccess(s -> Assert.fail())
116-
.onErrorReturn("".getBytes())
117-
.block();
118-
}
119-
120134
@Test
121135
public void lazyDeactivate() throws Exception {
122136
String actorId = UUID.randomUUID().toString();
123137
this.runtime.registerActor(MyActorImpl.class);
124-
this.runtime.activate(ACTOR_NAME, actorId).block();
125138

126-
Mono<Void> deacticateCall = this.runtime.deactivate(ACTOR_NAME, actorId);
139+
Mono<Void> deactivateCall = this.runtime.deactivate(ACTOR_NAME, actorId);
127140

128141
this.runtime.invoke(ACTOR_NAME, actorId, "say", null).block();
129142

130-
deacticateCall.block();
143+
deactivateCall.block();
131144

132145
this.runtime.invoke(ACTOR_NAME, actorId, "say", null)
133146
.doOnError(e -> Assert.assertTrue(e.getMessage().contains("Could not find actor")))
@@ -143,9 +156,15 @@ public void lazyInvoke() throws Exception {
143156

144157
Mono<byte[]> invokeCall = this.runtime.invoke(ACTOR_NAME, actorId, "say", null);
145158

146-
this.runtime.activate(ACTOR_NAME, actorId).block();
159+
byte[] response = this.runtime.invoke(ACTOR_NAME, actorId, "count", null).block();
160+
int count = ACTOR_STATE_SERIALIZER.deserialize(response, Integer.class);
161+
Assert.assertEquals(0, count);
147162

148163
invokeCall.block();
164+
165+
response = this.runtime.invoke(ACTOR_NAME, actorId, "count", null).block();
166+
count = ACTOR_STATE_SERIALIZER.deserialize(response, Integer.class);
167+
Assert.assertEquals(1, count);
149168
}
150169

151170
}

sdk-autogen/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
<goal>wget</goal>
8989
</goals>
9090
<configuration>
91-
<url>${dapr.proto.baseurl}/dapr/v1/dapr.proto</url>
91+
<url>${dapr.proto.baseurl}/runtime/v1/dapr.proto</url>
9292
<outputFileName>dapr.proto</outputFileName>
9393
<!-- default target location, just to demonstrate the parameter -->
9494
<outputDirectory>${protobuf.input.directory}</outputDirectory>
@@ -102,8 +102,8 @@
102102
<goal>wget</goal>
103103
</goals>
104104
<configuration>
105-
<url>${dapr.proto.baseurl}/daprclient/v1/daprclient.proto</url>
106-
<outputFileName>daprclient.proto</outputFileName>
105+
<url>${dapr.proto.baseurl}/runtime/v1/appcallback.proto</url>
106+
<outputFileName>appcallback.proto</outputFileName>
107107
<!-- default target location, just to demonstrate the parameter -->
108108
<outputDirectory>${protobuf.input.directory}</outputDirectory>
109109
</configuration>

sdk-springboot/src/main/java/io/dapr/springboot/DaprBeanPostProcessor.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,30 @@
55

66
package io.dapr.springboot;
77

8+
import com.fasterxml.jackson.core.JsonProcessingException;
9+
import com.fasterxml.jackson.core.type.TypeReference;
10+
import com.fasterxml.jackson.databind.JsonMappingException;
11+
import com.fasterxml.jackson.databind.ObjectMapper;
812
import io.dapr.Topic;
13+
import io.dapr.client.ObjectSerializer;
14+
import io.dapr.serializer.DefaultObjectSerializer;
915
import org.springframework.beans.BeansException;
1016
import org.springframework.beans.factory.config.BeanPostProcessor;
1117
import org.springframework.stereotype.Component;
18+
import org.springframework.web.bind.annotation.PostMapping;
1219

1320
import java.lang.reflect.Method;
21+
import java.util.HashMap;
22+
import java.util.Map;
1423

1524
/**
1625
* Handles Dapr annotations in Springboot Controllers.
1726
*/
1827
@Component
1928
public class DaprBeanPostProcessor implements BeanPostProcessor {
2029

30+
private static final ObjectMapper MAPPER = new ObjectMapper();
31+
2132
/**
2233
* {@inheritDoc}
2334
*/
@@ -56,9 +67,23 @@ private static void subscribeToTopics(Class clazz) {
5667
continue;
5768
}
5869

70+
String route = topic.name();
71+
PostMapping mapping = method.getAnnotation(PostMapping.class);
72+
73+
if (mapping != null && mapping.path() != null && mapping.path().length >= 1) {
74+
route = mapping.path()[0];
75+
}
76+
5977
String topicName = topic.name();
6078
if ((topicName != null) && (topicName.length() > 0)) {
61-
DaprRuntime.getInstance().addSubscribedTopic(topicName);
79+
try {
80+
TypeReference<HashMap<String, String>> typeRef
81+
= new TypeReference<HashMap<String, String>>() {};
82+
Map<String, String> metadata = MAPPER.readValue(topic.metadata(), typeRef);
83+
DaprRuntime.getInstance().addSubscribedTopic(topicName, route, metadata);
84+
} catch (JsonProcessingException e) {
85+
throw new IllegalArgumentException("Error while parsing metadata: " + e.toString());
86+
}
6287
}
6388
}
6489
}

sdk-springboot/src/main/java/io/dapr/springboot/DaprController.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,6 @@ public byte[] daprSubscribe() throws IOException {
5656
return SERIALIZER.serialize(DaprRuntime.getInstance().listSubscribedTopics());
5757
}
5858

59-
/**
60-
* Handles API to activate an actor.
61-
* @param type Actor type.
62-
* @param id Actor Id.
63-
* @return Void.
64-
*/
65-
@PostMapping(path = "/actors/{type}/{id}")
66-
public Mono<Void> activateActor(@PathVariable("type") String type,
67-
@PathVariable("id") String id) {
68-
return ActorRuntime.getInstance().activate(type, id);
69-
}
70-
7159
/**
7260
* Handles API to deactivate an actor.
7361
* @param type Actor type.

0 commit comments

Comments
 (0)