|
| 1 | +/* |
| 2 | + * Copyright 2026 The Dapr Authors |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | + * limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +package io.dapr.durabletask; |
| 15 | + |
| 16 | +import com.google.protobuf.StringValue; |
| 17 | +import io.dapr.durabletask.implementation.protobuf.Orchestration; |
| 18 | +import io.dapr.durabletask.implementation.protobuf.OrchestratorService; |
| 19 | +import io.dapr.durabletask.implementation.protobuf.TaskHubSidecarServiceGrpc; |
| 20 | +import io.grpc.ManagedChannel; |
| 21 | +import io.grpc.ManagedChannelBuilder; |
| 22 | +import io.grpc.Server; |
| 23 | +import io.grpc.netty.NettyServerBuilder; |
| 24 | +import io.grpc.stub.StreamObserver; |
| 25 | +import org.junit.jupiter.api.AfterEach; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +import java.util.concurrent.CompletableFuture; |
| 29 | +import java.util.concurrent.TimeUnit; |
| 30 | +import java.util.concurrent.TimeoutException; |
| 31 | +import java.util.concurrent.atomic.AtomicInteger; |
| 32 | + |
| 33 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 34 | + |
| 35 | +/** |
| 36 | + * Verifies DurableTaskGrpcWorker accepts a work item larger than gRPC's 4MB default receive limit. |
| 37 | + * |
| 38 | + * <p>daprd sends work items up to its own max body size, so a fan-out of five 1MB activity results |
| 39 | + * is a work item of about 6MB. The channel is built like the SDK's own, whose inbound limit defaults |
| 40 | + * to 4MB (DAPR_GRPC_MAX_INBOUND_MESSAGE_SIZE_BYTES). Uses a real TCP server because the in-process |
| 41 | + * transport does not enforce message sizes.</p> |
| 42 | + */ |
| 43 | +class DurableTaskGrpcWorkerLargeWorkItemTest { |
| 44 | + |
| 45 | + private static final int DEFAULT_MAX_INBOUND_BYTES = 4 * 1024 * 1024; |
| 46 | + private static final int PAYLOAD_CHARS = 6 * 1024 * 1024; |
| 47 | + private static final String ACTIVITY_NAME = "PayloadLength"; |
| 48 | + |
| 49 | + private DurableTaskGrpcWorker worker; |
| 50 | + private ManagedChannel channel; |
| 51 | + private Server server; |
| 52 | + |
| 53 | + @AfterEach |
| 54 | + void tearDown() throws Exception { |
| 55 | + if (worker != null) { |
| 56 | + worker.close(); |
| 57 | + } |
| 58 | + if (channel != null) { |
| 59 | + channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS); |
| 60 | + } |
| 61 | + if (server != null) { |
| 62 | + server.shutdownNow().awaitTermination(5, TimeUnit.SECONDS); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void workerProcessesWorkItemLargerThanDefaultReceiveLimit() throws Exception { |
| 68 | + OrchestratorService.ActivityRequest activityRequest = OrchestratorService.ActivityRequest.newBuilder() |
| 69 | + .setName(ACTIVITY_NAME) |
| 70 | + .setTaskId(1) |
| 71 | + .setWorkflowInstance(Orchestration.WorkflowInstance.newBuilder().setInstanceId("instance").build()) |
| 72 | + .setInput(StringValue.of("\"" + "x".repeat(PAYLOAD_CHARS) + "\"")) |
| 73 | + .build(); |
| 74 | + OrchestratorService.WorkItem workItem = OrchestratorService.WorkItem.newBuilder() |
| 75 | + .setActivityRequest(activityRequest) |
| 76 | + .build(); |
| 77 | + |
| 78 | + AtomicInteger streams = new AtomicInteger(); |
| 79 | + CompletableFuture<OrchestratorService.ActivityResponse> activityResponse = new CompletableFuture<>(); |
| 80 | + server = NettyServerBuilder.forPort(0) |
| 81 | + .addService(new TaskHubSidecarServiceGrpc.TaskHubSidecarServiceImplBase() { |
| 82 | + @Override |
| 83 | + public void getWorkItems( |
| 84 | + OrchestratorService.GetWorkItemsRequest request, |
| 85 | + StreamObserver<OrchestratorService.WorkItem> responseObserver) { |
| 86 | + streams.incrementAndGet(); |
| 87 | + if (!activityResponse.isDone()) { |
| 88 | + responseObserver.onNext(workItem); |
| 89 | + } |
| 90 | + // Hold the stream open, as daprd does. |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void completeActivityTask( |
| 95 | + OrchestratorService.ActivityResponse request, |
| 96 | + StreamObserver<OrchestratorService.CompleteTaskResponse> responseObserver) { |
| 97 | + activityResponse.complete(request); |
| 98 | + responseObserver.onNext(OrchestratorService.CompleteTaskResponse.getDefaultInstance()); |
| 99 | + responseObserver.onCompleted(); |
| 100 | + } |
| 101 | + }) |
| 102 | + .build() |
| 103 | + .start(); |
| 104 | + |
| 105 | + channel = ManagedChannelBuilder.forAddress("localhost", server.getPort()) |
| 106 | + .usePlaintext() |
| 107 | + .maxInboundMessageSize(DEFAULT_MAX_INBOUND_BYTES) |
| 108 | + .build(); |
| 109 | + worker = new DurableTaskGrpcWorkerBuilder() |
| 110 | + .grpcChannel(channel) |
| 111 | + .addActivity(new TaskActivityFactory() { |
| 112 | + @Override |
| 113 | + public String getName() { |
| 114 | + return ACTIVITY_NAME; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public TaskActivity create() { |
| 119 | + return ctx -> ctx.getInput(String.class).length(); |
| 120 | + } |
| 121 | + }) |
| 122 | + .build(); |
| 123 | + worker.start(); |
| 124 | + |
| 125 | + OrchestratorService.ActivityResponse result; |
| 126 | + try { |
| 127 | + result = activityResponse.get(15, TimeUnit.SECONDS); |
| 128 | + } catch (TimeoutException e) { |
| 129 | + throw new AssertionError("no activity result after " + streams.get() + " work item streams: " |
| 130 | + + "the worker could not receive the work item", e); |
| 131 | + } |
| 132 | + assertEquals(String.valueOf(PAYLOAD_CHARS), result.getResult().getValue()); |
| 133 | + } |
| 134 | +} |
0 commit comments