Skip to content

Commit 1f33417

Browse files
MatejNedicartursouza
authored andcommitted
Fix 787 (dapr#832)
* prepare before testing * Update tests * fix checkstyle --------- Co-authored-by: Artur Souza <[email protected]> Signed-off-by: Mahmut Canga <[email protected]>
1 parent d49c87b commit 1f33417

File tree

2 files changed

+390
-358
lines changed

2 files changed

+390
-358
lines changed

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

Lines changed: 77 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,24 @@
1414
package io.dapr.actors.runtime;
1515

1616
import com.fasterxml.jackson.databind.ObjectMapper;
17-
import com.google.common.util.concurrent.ListenableFuture;
1817
import com.google.protobuf.Any;
1918
import com.google.protobuf.ByteString;
2019
import com.google.protobuf.Empty;
2120
import io.dapr.config.Properties;
21+
import io.dapr.exceptions.DaprException;
2222
import io.dapr.utils.DurationUtils;
2323
import io.dapr.v1.DaprGrpc;
2424
import io.dapr.v1.DaprProtos;
2525
import io.grpc.ManagedChannel;
26+
import io.grpc.stub.StreamObserver;
2627
import reactor.core.publisher.Mono;
28+
import reactor.core.publisher.MonoSink;
2729

2830
import java.io.IOException;
2931
import java.nio.charset.Charset;
3032
import java.util.ArrayList;
3133
import java.util.List;
34+
import java.util.concurrent.ExecutionException;
3235

3336
/**
3437
* A DaprClient over HTTP for Actor's runtime.
@@ -48,44 +51,42 @@ class DaprGrpcClient implements DaprClient {
4851
/**
4952
* The GRPC client to be used.
5053
*
51-
* @see io.dapr.v1.DaprGrpc.DaprFutureStub
54+
* @see io.dapr.v1.DaprGrpc.DaprStub
5255
*/
53-
private DaprGrpc.DaprFutureStub client;
56+
private DaprGrpc.DaprStub client;
5457

5558
/**
5659
* Internal constructor.
5760
*
5861
* @param channel channel (client needs to close channel after use).
5962
*/
6063
DaprGrpcClient(ManagedChannel channel) {
61-
this(DaprGrpc.newFutureStub(channel));
64+
this(DaprGrpc.newStub(channel));
6265
}
6366

6467
/**
6568
* Internal constructor.
6669
*
67-
* @param grpcClient Dapr's GRPC client.
70+
* @param daprStubClient Dapr's GRPC client.
6871
*/
69-
DaprGrpcClient(DaprGrpc.DaprFutureStub grpcClient) {
70-
this.client = grpcClient;
72+
DaprGrpcClient(DaprGrpc.DaprStub daprStubClient) {
73+
this.client = daprStubClient;
7174
}
7275

7376
/**
7477
* {@inheritDoc}
7578
*/
7679
@Override
7780
public Mono<byte[]> getState(String actorType, String actorId, String keyName) {
78-
return Mono.fromCallable(() -> {
79-
DaprProtos.GetActorStateRequest req =
80-
DaprProtos.GetActorStateRequest.newBuilder()
81-
.setActorType(actorType)
82-
.setActorId(actorId)
83-
.setKey(keyName)
84-
.build();
85-
86-
ListenableFuture<DaprProtos.GetActorStateResponse> futureResponse = client.getActorState(req);
87-
return futureResponse.get();
88-
}).map(r -> r.getData().toByteArray());
81+
DaprProtos.GetActorStateRequest req =
82+
DaprProtos.GetActorStateRequest.newBuilder()
83+
.setActorType(actorType)
84+
.setActorId(actorId)
85+
.setKey(keyName)
86+
.build();
87+
88+
return Mono.<DaprProtos.GetActorStateResponse>create(it ->
89+
client.getActorState(req, createStreamObserver(it))).map(r -> r.getData().toByteArray());
8990
}
9091

9192
/**
@@ -132,10 +133,7 @@ public Mono<Void> saveStateTransactionally(
132133
.addAllOperations(grpcOps)
133134
.build();
134135

135-
return Mono.fromCallable(() -> {
136-
ListenableFuture<Empty> futureResponse = client.executeActorStateTransaction(req);
137-
return futureResponse.get();
138-
}).then();
136+
return Mono.<Empty>create(it -> client.executeActorStateTransaction(req, createStreamObserver(it))).then();
139137
}
140138

141139
/**
@@ -147,40 +145,31 @@ public Mono<Void> registerReminder(
147145
String actorId,
148146
String reminderName,
149147
ActorReminderParams reminderParams) {
150-
return Mono.fromCallable(() -> {
151-
DaprProtos.RegisterActorReminderRequest req =
152-
DaprProtos.RegisterActorReminderRequest.newBuilder()
153-
.setActorType(actorType)
154-
.setActorId(actorId)
155-
.setName(reminderName)
156-
.setData(ByteString.copyFrom(reminderParams.getData()))
157-
.setDueTime(DurationUtils.convertDurationToDaprFormat(reminderParams.getDueTime()))
158-
.setPeriod(DurationUtils.convertDurationToDaprFormat(reminderParams.getPeriod()))
159-
.build();
160-
161-
ListenableFuture<Empty> futureResponse = client.registerActorReminder(req);
162-
futureResponse.get();
163-
return null;
164-
});
148+
DaprProtos.RegisterActorReminderRequest req =
149+
DaprProtos.RegisterActorReminderRequest.newBuilder()
150+
.setActorType(actorType)
151+
.setActorId(actorId)
152+
.setName(reminderName)
153+
.setData(ByteString.copyFrom(reminderParams.getData()))
154+
.setDueTime(DurationUtils.convertDurationToDaprFormat(reminderParams.getDueTime()))
155+
.setPeriod(DurationUtils.convertDurationToDaprFormat(reminderParams.getPeriod()))
156+
.build();
157+
return Mono.<Empty>create(it -> client.registerActorReminder(req, createStreamObserver(it))).then().then();
165158
}
166159

167160
/**
168161
* {@inheritDoc}
169162
*/
170163
@Override
171164
public Mono<Void> unregisterReminder(String actorType, String actorId, String reminderName) {
172-
return Mono.fromCallable(() -> {
173-
DaprProtos.UnregisterActorReminderRequest req =
174-
DaprProtos.UnregisterActorReminderRequest.newBuilder()
175-
.setActorType(actorType)
176-
.setActorId(actorId)
177-
.setName(reminderName)
178-
.build();
179-
180-
ListenableFuture<Empty> futureResponse = client.unregisterActorReminder(req);
181-
futureResponse.get();
182-
return null;
183-
});
165+
DaprProtos.UnregisterActorReminderRequest req =
166+
DaprProtos.UnregisterActorReminderRequest.newBuilder()
167+
.setActorType(actorType)
168+
.setActorId(actorId)
169+
.setName(reminderName)
170+
.build();
171+
172+
return Mono.<Empty>create(it -> client.unregisterActorReminder(req, createStreamObserver(it))).then().then();
184173
}
185174

186175
/**
@@ -192,41 +181,52 @@ public Mono<Void> registerTimer(
192181
String actorId,
193182
String timerName,
194183
ActorTimerParams timerParams) {
195-
return Mono.fromCallable(() -> {
196-
DaprProtos.RegisterActorTimerRequest req =
197-
DaprProtos.RegisterActorTimerRequest.newBuilder()
198-
.setActorType(actorType)
199-
.setActorId(actorId)
200-
.setName(timerName)
201-
.setCallback(timerParams.getCallback())
202-
.setData(ByteString.copyFrom(timerParams.getData()))
203-
.setDueTime(DurationUtils.convertDurationToDaprFormat(timerParams.getDueTime()))
204-
.setPeriod(DurationUtils.convertDurationToDaprFormat(timerParams.getPeriod()))
205-
.build();
206-
207-
ListenableFuture<Empty> futureResponse = client.registerActorTimer(req);
208-
futureResponse.get();
209-
return null;
210-
});
184+
DaprProtos.RegisterActorTimerRequest req =
185+
DaprProtos.RegisterActorTimerRequest.newBuilder()
186+
.setActorType(actorType)
187+
.setActorId(actorId)
188+
.setName(timerName)
189+
.setCallback(timerParams.getCallback())
190+
.setData(ByteString.copyFrom(timerParams.getData()))
191+
.setDueTime(DurationUtils.convertDurationToDaprFormat(timerParams.getDueTime()))
192+
.setPeriod(DurationUtils.convertDurationToDaprFormat(timerParams.getPeriod()))
193+
.build();
194+
195+
return Mono.<Empty>create(it -> client.registerActorTimer(req, createStreamObserver(it))).then().then();
211196
}
212197

213198
/**
214199
* {@inheritDoc}
215200
*/
216201
@Override
217202
public Mono<Void> unregisterTimer(String actorType, String actorId, String timerName) {
218-
return Mono.fromCallable(() -> {
219-
DaprProtos.UnregisterActorTimerRequest req =
220-
DaprProtos.UnregisterActorTimerRequest.newBuilder()
221-
.setActorType(actorType)
222-
.setActorId(actorId)
223-
.setName(timerName)
224-
.build();
225-
226-
ListenableFuture<Empty> futureResponse = client.unregisterActorTimer(req);
227-
futureResponse.get();
228-
return null;
229-
});
203+
DaprProtos.UnregisterActorTimerRequest req =
204+
DaprProtos.UnregisterActorTimerRequest.newBuilder()
205+
.setActorType(actorType)
206+
.setActorId(actorId)
207+
.setName(timerName)
208+
.build();
209+
210+
return Mono.<Empty>create(it -> client.unregisterActorTimer(req, createStreamObserver(it))).then().then();
211+
}
212+
213+
private <T> StreamObserver<T> createStreamObserver(MonoSink<T> sink) {
214+
return new StreamObserver<T>() {
215+
@Override
216+
public void onNext(T value) {
217+
sink.success(value);
218+
}
219+
220+
@Override
221+
public void onError(Throwable t) {
222+
sink.error(DaprException.propagate(new ExecutionException(t)));
223+
}
224+
225+
@Override
226+
public void onCompleted() {
227+
sink.success();
228+
}
229+
};
230230
}
231231

232232
}

0 commit comments

Comments
 (0)