Skip to content

Commit 7f63f90

Browse files
cicoylejavier-aliaga
authored andcommitted
[1.17] make bulk pubsub stable (#1641)
* make bulk pubsub stable, update all tests/examples/code to the stable API Signed-off-by: Cassandra Coyle <cassie@diagrid.io> * fix checkstyle Signed-off-by: Cassandra Coyle <cassie@diagrid.io> * DaprPreviewClient -> DaprClient Signed-off-by: Cassandra Coyle <cassie@diagrid.io> * fix preview client test Signed-off-by: Cassandra Coyle <cassie@diagrid.io> * update readme to make mechanical markdown pass and rm old preview client refs Signed-off-by: Cassandra Coyle <cassie@diagrid.io> * fix mechanical markdown Signed-off-by: Cassandra Coyle <cassie@diagrid.io> * use correct comment for bash Signed-off-by: Cassandra Coyle <cassie@diagrid.io> * both handlers are on the same svc since bulk pubsub is stable now Signed-off-by: Cassandra Coyle <cassie@diagrid.io> * push empty line Signed-off-by: Cassandra Coyle <cassie@diagrid.io> --------- Signed-off-by: Cassandra Coyle <cassie@diagrid.io> Signed-off-by: Javier Aliaga <javier@diagrid.io>
1 parent 1e024b7 commit 7f63f90

12 files changed

Lines changed: 231 additions & 132 deletions

File tree

‎examples/src/main/java/io/dapr/examples/pubsub/BulkPublisher.java‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import io.dapr.client.DaprClient;
1717
import io.dapr.client.DaprClientBuilder;
18-
import io.dapr.client.DaprPreviewClient;
1918
import io.dapr.client.domain.BulkPublishResponse;
2019
import io.dapr.client.domain.BulkPublishResponseFailedEntry;
2120
import io.dapr.examples.OpenTelemetryConfig;
@@ -59,13 +58,11 @@ public static void main(String[] args) throws Exception {
5958
Tracer tracer = openTelemetrySdk.getTracer(BulkPublisher.class.getCanonicalName());
6059
Span span = tracer.spanBuilder("Bulk Publisher's Main").setSpanKind(SpanKind.CLIENT).startSpan();
6160

62-
try (DaprPreviewClient client = (new DaprClientBuilder()).buildPreviewClient()) {
63-
DaprClient c = (DaprClient) client;
64-
65-
c.waitForSidecar(10000);
61+
try (DaprClient client = (new DaprClientBuilder()).build()) {
62+
client.waitForSidecar(10000);
6663

6764
try (Scope scope = span.makeCurrent()) {
68-
System.out.println("Using preview client...");
65+
System.out.println("Using Dapr client...");
6966

7067
List<String> messages = new ArrayList<>();
7168

‎examples/src/main/java/io/dapr/examples/pubsub/CloudEventBulkPublisher.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
package io.dapr.examples.pubsub;
1515

16+
import io.dapr.client.DaprClient;
1617
import io.dapr.client.DaprClientBuilder;
17-
import io.dapr.client.DaprPreviewClient;
1818
import io.dapr.client.domain.BulkPublishEntry;
1919
import io.dapr.client.domain.BulkPublishRequest;
2020
import io.dapr.client.domain.BulkPublishResponse;
@@ -53,8 +53,8 @@ public class CloudEventBulkPublisher {
5353
* @throws Exception any exception
5454
*/
5555
public static void main(String[] args) throws Exception {
56-
try (DaprPreviewClient client = (new DaprClientBuilder()).buildPreviewClient()) {
57-
System.out.println("Using preview client...");
56+
try (DaprClient client = (new DaprClientBuilder()).build()) {
57+
System.out.println("Using Dapr client...");
5858
List<BulkPublishEntry<CloudEvent<Map<String, String>>>> entries = new ArrayList<>();
5959
for (int i = 0; i < NUM_MESSAGES; i++) {
6060
CloudEvent<Map<String, String>> cloudEvent = new CloudEvent<>();

‎examples/src/main/java/io/dapr/examples/pubsub/README.md‎

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,12 @@ Once running, the Subscriber should print the output as follows:
275275

276276
Messages have been retrieved from the topic.
277277

278-
### Bulk Publish Messages
279-
> Note : This API is currently in Alpha stage in Dapr runtime, hence the API methods in SDK are part of the DaprPreviewClient class.
278+
### Bulk Publish Messages
280279

281280
Another feature provided by the SDK is to allow users to publish multiple messages in a single call to the Dapr sidecar.
282-
For this example, we have a simple Java application with a main method that uses the Dapr gPRC Preview Client to publish 10 messages to a specific topic in a single call.
281+
For this example, we have a simple Java application with a main method that uses the Dapr Client to publish 10 messages to a specific topic in a single call.
283282

284-
In the `BulkPublisher.java` file, you will find the `BulkPublisher` class, containing the main method. The main method declares a Dapr Preview Client using the `DaprClientBuilder` class. Notice that this builder gets two serializer implementations in the constructor: one is for Dapr's sent and recieved objects, and the second is for objects to be persisted.
283+
In the `BulkPublisher.java` file, you will find the `BulkPublisher` class, containing the main method. The main method declares a Dapr Client using the `DaprClientBuilder` class. Notice that this builder gets two serializer implementations in the constructor: one is for Dapr's sent and recieved objects, and the second is for objects to be persisted.
285284
The client publishes messages using `publishEvents` method. The Dapr client is also within a try-with-resource block to properly close the client at the end. See the code snippet below:
286285
Dapr's sidecar will automatically wrap the payload received into a CloudEvent object, which will later on be parsed by the subscriber.
287286

@@ -296,11 +295,10 @@ public class BulkPublisher {
296295
OpenTelemetry openTelemetry = OpenTelemetryConfig.createOpenTelemetry();
297296
Tracer tracer = openTelemetry.getTracer(BulkPublisher.class.getCanonicalName());
298297
Span span = tracer.spanBuilder("Bulk Publisher's Main").setSpanKind(Span.Kind.CLIENT).startSpan();
299-
try (DaprPreviewClient client = (new DaprClientBuilder()).buildPreviewClient()) {
300-
DaprClient c = (DaprClient)client;
301-
c.waitForSidecar(10000);
298+
try (DaprClient client = (new DaprClientBuilder()).build()) {
299+
client.waitForSidecar(10000);
302300
try (Scope scope = span.makeCurrent()) {
303-
System.out.println("Using preview client...");
301+
System.out.println("Using Dapr client...");
304302
List<String> messages = new ArrayList<>();
305303
System.out.println("Constructing the list of messages to publish");
306304
for (int i = 0; i < NUM_MESSAGES; i++) {
@@ -333,7 +331,7 @@ public class BulkPublisher {
333331
}
334332
}
335333
```
336-
The code uses the `DaprPreviewClient` created by the `DaprClientBuilder` is used for the `publishEvents` (BulkPublish) preview API.
334+
The code uses the `DaprClient` created by the `DaprClientBuilder` for the `publishEvents` (BulkPublish) API.
337335

338336
In this case, when the `publishEvents` call is made, one of the arguments to the method is the content type of data, this being `text/plain` in the example.
339337
In this case, when parsing and printing the response, there is a concept of EntryID, which is automatically generated or can be set manually when using the `BulkPublishRequest` object.
@@ -350,7 +348,7 @@ In this case, the application **MUST** override the content-type parameter via `
350348
public class CloudEventBulkPublisher {
351349
///...
352350
public static void main(String[] args) throws Exception {
353-
try (DaprPreviewClient client = (new DaprClientBuilder()).buildPreviewClient()) {
351+
try (DaprClient client = (new DaprClientBuilder()).build()) {
354352
// Construct request
355353
BulkPublishRequest<CloudEvent<Map<String, String>>> request = new BulkPublishRequest<>(PUBSUB_NAME, TOPIC_NAME);
356354
List<BulkPublishRequestEntry<CloudEvent<Map<String, String>>>> entries = new ArrayList<>();
@@ -414,7 +412,7 @@ Once running, the BulkPublisher should print the output as follows:
414412
```txt
415413
✅ You're up and running! Both Dapr and your app logs will appear here.
416414
417-
== APP == Using preview client...
415+
== APP == Using Dapr client...
418416
== APP == Constructing the list of messages to publish
419417
== APP == Going to publish message : This is message #0
420418
== APP == Going to publish message : This is message #1
@@ -655,20 +653,23 @@ public class SubscriberGrpcService extends AppCallbackGrpc.AppCallbackImplBase {
655653
///...
656654
}
657655
```
658-
The `BulkSubscriberGrpcService.java` file is responsible for implementing the processing of bulk message subscriptions. When Dapr's sidecar successfully subscribes to bulk messages, it will call `onBulkTopicEventAlpha1` and pass them as a request parameter. You can refer to the example on how to handle bulk messages and respond correctly over gPRC.
656+
657+
The `SubscriberGrpcService.java` now handles both regular and bulk message subscriptions since bulk pubsub is now stable. When Dapr's sidecar successfully subscribes to messages, it will call either `onTopicEvent` for regular messages or `onBulkTopicEvent` for bulk messages. Below is an example of the bulk handler:
659658
660659
```java
661-
public class BulkSubscriberGrpcService extends AppCallbackAlphaGrpc.AppCallbackAlphaImplBase {
660+
public class SubscriberGrpcService extends AppCallbackGrpc.AppCallbackImplBase {
661+
// ... onTopicEvent method shown above ...
662662
663663
@Override
664-
public void onBulkTopicEventAlpha1(io.dapr.v1.DaprAppCallbackProtos.TopicEventBulkRequest request,
664+
public void onBulkTopicEvent(io.dapr.v1.DaprAppCallbackProtos.TopicEventBulkRequest request,
665665
io.grpc.stub.StreamObserver<io.dapr.v1.DaprAppCallbackProtos.TopicEventBulkResponse> responseObserver) {
666666
try {
667667
TopicEventBulkResponse.Builder responseBuilder = TopicEventBulkResponse.newBuilder();
668668
669669
if (request.getEntriesCount() == 0) {
670670
responseObserver.onNext(responseBuilder.build());
671671
responseObserver.onCompleted();
672+
return;
672673
}
673674
674675
System.out.println("Bulk Subscriber received " + request.getEntriesCount() + " messages.");
@@ -715,11 +716,11 @@ background: true
715716
sleep: 15
716717
-->
717718
```bash
718-
// stop http subscriber if you have started one.
719+
# stop http subscriber if you have started one.
719720
dapr stop --app-id subscriber
720721
721722
722-
// start a grpc subscriber
723+
# start a grpc subscriber
723724
dapr run --resources-path ./components/pubsub --app-id subscriber --app-port 3000 --app-protocol grpc -- java -jar target/dapr-java-sdk-examples-exec.jar io.dapr.examples.pubsub.grpc.Subscriber -p 3000
724725
```
725726
<!-- END_STEP -->

‎examples/src/main/java/io/dapr/examples/pubsub/grpc/BulkSubscriberGrpcService.java‎

Lines changed: 0 additions & 65 deletions
This file was deleted.

‎examples/src/main/java/io/dapr/examples/pubsub/grpc/Subscriber.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public static void main(String[] args) throws Exception {
5151
Server server = ServerBuilder.forPort(port)
5252
.intercept(new SubscriberGrpcService.MetadataInterceptor())
5353
.addService(new SubscriberGrpcService())
54-
.addService(new BulkSubscriberGrpcService())
5554
.build();
5655
server.start();
5756
server.awaitTermination();

‎examples/src/main/java/io/dapr/examples/pubsub/grpc/SubscriberGrpcService.java‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
import com.google.protobuf.Empty;
1717
import io.dapr.v1.AppCallbackGrpc;
1818
import io.dapr.v1.DaprAppCallbackProtos;
19+
import io.dapr.v1.DaprAppCallbackProtos.TopicEventBulkRequestEntry;
20+
import io.dapr.v1.DaprAppCallbackProtos.TopicEventBulkResponse;
21+
import io.dapr.v1.DaprAppCallbackProtos.TopicEventBulkResponseEntry;
22+
import io.dapr.v1.DaprAppCallbackProtos.TopicEventResponse.TopicEventResponseStatus;
1923
import io.grpc.Context;
2024
import io.grpc.Contexts;
2125
import io.grpc.Metadata;
@@ -103,6 +107,45 @@ public void onTopicEvent(DaprAppCallbackProtos.TopicEventRequest request,
103107
}
104108
}
105109

110+
@Override
111+
public void onBulkTopicEvent(DaprAppCallbackProtos.TopicEventBulkRequest request,
112+
StreamObserver<TopicEventBulkResponse> responseObserver) {
113+
try {
114+
TopicEventBulkResponse.Builder responseBuilder = TopicEventBulkResponse.newBuilder();
115+
116+
if (request.getEntriesCount() == 0) {
117+
responseObserver.onNext(responseBuilder.build());
118+
responseObserver.onCompleted();
119+
return;
120+
}
121+
122+
System.out.println("Bulk Subscriber received " + request.getEntriesCount() + " messages.");
123+
124+
for (TopicEventBulkRequestEntry entry : request.getEntriesList()) {
125+
try {
126+
System.out.printf("Bulk Subscriber message has entry ID: %s\n", entry.getEntryId());
127+
System.out.printf("Bulk Subscriber got: %s\n", entry.getCloudEvent().getData().toStringUtf8());
128+
TopicEventBulkResponseEntry.Builder responseEntryBuilder = TopicEventBulkResponseEntry
129+
.newBuilder()
130+
.setEntryId(entry.getEntryId())
131+
.setStatusValue(TopicEventResponseStatus.SUCCESS_VALUE);
132+
responseBuilder.addStatuses(responseEntryBuilder);
133+
} catch (Throwable e) {
134+
TopicEventBulkResponseEntry.Builder responseEntryBuilder = TopicEventBulkResponseEntry
135+
.newBuilder()
136+
.setEntryId(entry.getEntryId())
137+
.setStatusValue(TopicEventResponseStatus.RETRY_VALUE);
138+
responseBuilder.addStatuses(responseEntryBuilder);
139+
}
140+
}
141+
TopicEventBulkResponse response = responseBuilder.build();
142+
responseObserver.onNext(response);
143+
responseObserver.onCompleted();
144+
} catch (Throwable e) {
145+
responseObserver.onError(e);
146+
}
147+
}
148+
106149
/**
107150
* Add pubsub name and topic to topicSubscriptionList.
108151
*
@@ -119,4 +162,3 @@ public void registerConsumer(String pubsubName, String topic, boolean isBulkMess
119162
.build());
120163
}
121164
}
122-

‎sdk-tests/src/test/java/io/dapr/it/pubsub/http/PubSubIT.java‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import com.fasterxml.jackson.databind.ObjectMapper;
1818
import io.dapr.client.DaprClient;
1919
import io.dapr.client.DaprClientBuilder;
20-
import io.dapr.client.DaprPreviewClient;
2120
import io.dapr.client.domain.BulkPublishEntry;
2221
import io.dapr.client.domain.BulkPublishRequest;
2322
import io.dapr.client.domain.BulkPublishResponse;
@@ -120,7 +119,7 @@ public void testBulkPublishPubSubNotFound() throws Exception {
120119
this.getClass().getSimpleName(),
121120
60000));
122121

123-
try (DaprPreviewClient client = daprRun.newDaprClientBuilder().buildPreviewClient()) {
122+
try (DaprClient client = daprRun.newDaprClientBuilder().build()) {
124123
assertThrowsDaprException(
125124
"INVALID_ARGUMENT",
126125
"INVALID_ARGUMENT: pubsub unknown pubsub is not found",
@@ -152,16 +151,15 @@ public String getContentType() {
152151
return "application/json";
153152
}
154153
};
155-
try (DaprClient client = daprRun.newDaprClientBuilder().withObjectSerializer(serializer).build();
156-
DaprPreviewClient previewClient = daprRun.newDaprClientBuilder().withObjectSerializer(serializer).buildPreviewClient()) {
154+
try (DaprClient client = daprRun.newDaprClientBuilder().withObjectSerializer(serializer).build()) {
157155
// Only for the gRPC test
158156
// Send a multiple messages on one topic in messagebus pubsub via publishEvents API.
159157
List<String> messages = new ArrayList<>();
160158
for (int i = 0; i < NUM_MESSAGES; i++) {
161159
messages.add(String.format("This is message #%d on topic %s", i, TOPIC_BULK));
162160
}
163161
//Publishing 10 messages
164-
BulkPublishResponse response = previewClient.publishEvents(PUBSUB_NAME, TOPIC_BULK, "", messages).block();
162+
BulkPublishResponse response = client.publishEvents(PUBSUB_NAME, TOPIC_BULK, "", messages).block();
165163
System.out.println(String.format("Published %d messages to topic '%s' pubsub_name '%s'",
166164
NUM_MESSAGES, TOPIC_BULK, PUBSUB_NAME));
167165
assertNotNull(response, "expected not null bulk publish response");
@@ -170,14 +168,14 @@ public String getContentType() {
170168
//Publishing an object.
171169
MyObject object = new MyObject();
172170
object.setId("123");
173-
response = previewClient.publishEvents(PUBSUB_NAME, TOPIC_BULK,
171+
response = client.publishEvents(PUBSUB_NAME, TOPIC_BULK,
174172
"application/json", Collections.singletonList(object)).block();
175173
System.out.println("Published one object.");
176174
assertNotNull(response, "expected not null bulk publish response");
177175
assertEquals(0, response.getFailedEntries().size(), "expected no failures in the response");
178176

179177
//Publishing a single byte: Example of non-string based content published
180-
previewClient.publishEvents(PUBSUB_NAME, TOPIC_BULK, "",
178+
client.publishEvents(PUBSUB_NAME, TOPIC_BULK, "",
181179
Collections.singletonList(new byte[]{1})).block();
182180
System.out.println("Published one byte.");
183181

@@ -197,7 +195,7 @@ public String getContentType() {
197195
));
198196

199197
//Publishing a cloud event.
200-
previewClient.publishEvents(req).block();
198+
client.publishEvents(req).block();
201199
assertNotNull(response, "expected not null bulk publish response");
202200
assertEquals(0, response.getFailedEntries().size(), "expected no failures in the response");
203201

0 commit comments

Comments
 (0)