Skip to content

Commit b0d9e6a

Browse files
authored
Execute grpcCalls on Application executor service (#1506)
Signed-off-by: fjtirado <ftirados@ibm.com>
1 parent 3e95b5a commit b0d9e6a

8 files changed

Lines changed: 317 additions & 240 deletions

File tree

‎impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowError.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static Builder expression() {
3535
return error("https://serverlessworkflow.io/spec/1.0.0/errors/expression", 400);
3636
}
3737

38-
public static Builder communication(int status, TaskContext context, Exception ex) {
38+
public static Builder communication(int status, TaskContext context, Throwable ex) {
3939
return communication(status, context, ex.getMessage());
4040
}
4141

@@ -54,13 +54,13 @@ public static Builder communication(TaskContext context, String title) {
5454
return communication(Errors.COMMUNICATION.status(), context, title);
5555
}
5656

57-
public static Builder runtime(int status, TaskContext context, Exception ex) {
57+
public static Builder runtime(int status, TaskContext context, Throwable ex) {
5858
return new Builder(Errors.RUNTIME.toString(), status)
5959
.instance(context.position().jsonPointer())
6060
.title(ex.getMessage());
6161
}
6262

63-
public static Builder runtime(TaskContext context, Exception ex) {
63+
public static Builder runtime(TaskContext context, Throwable ex) {
6464
return runtime(Errors.RUNTIME.status(), context, ex);
6565
}
6666

‎impl/grpc/src/main/java/io/serverlessworkflow/impl/executors/grpc/CollectionStreamObserver.java‎

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,19 @@
1616
package io.serverlessworkflow.impl.executors.grpc;
1717

1818
import com.google.protobuf.Message;
19-
import io.grpc.stub.StreamObserver;
20-
import io.serverlessworkflow.impl.WorkflowModel;
19+
import io.serverlessworkflow.impl.TaskContext;
20+
import io.serverlessworkflow.impl.WorkflowContext;
2121
import io.serverlessworkflow.impl.WorkflowModelCollection;
22-
import io.serverlessworkflow.impl.WorkflowModelFactory;
23-
import java.util.concurrent.CompletableFuture;
2422

25-
class CollectionStreamObserver implements StreamObserver<Message> {
26-
private final WorkflowModelCollection modelCollection;
27-
private final WorkflowModelFactory modelFactory;
28-
private final CompletableFuture<WorkflowModel> future;
23+
class CollectionStreamObserver extends ModelStreamObserver<WorkflowModelCollection> {
2924

30-
public CollectionStreamObserver(WorkflowModelFactory modelFactory) {
31-
this.modelCollection = modelFactory.createCollection();
32-
this.modelFactory = modelFactory;
33-
this.future = new CompletableFuture<>();
25+
public CollectionStreamObserver(WorkflowContext workflowContext, TaskContext taskContext) {
26+
super(workflowContext, taskContext);
27+
this.model = modelFactory.createCollection();
3428
}
3529

3630
@Override
3731
public void onNext(Message value) {
38-
modelCollection.add(ProtobufMessageUtils.convert(value, modelFactory));
39-
}
40-
41-
@Override
42-
public void onError(Throwable t) {
43-
future.completeExceptionally(t);
44-
}
45-
46-
@Override
47-
public void onCompleted() {
48-
future.complete(modelCollection);
49-
}
50-
51-
public CompletableFuture<WorkflowModel> future() {
52-
return future;
32+
model.add(ProtobufMessageUtils.convert(value, modelFactory));
5333
}
5434
}

‎impl/grpc/src/main/java/io/serverlessworkflow/impl/executors/grpc/FileDescriptorReader.java‎

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
import com.github.os72.protocjar.Protoc;
1919
import com.google.protobuf.DescriptorProtos;
2020
import com.google.protobuf.DescriptorProtos.FileDescriptorProto;
21+
import com.google.protobuf.DescriptorProtos.FileDescriptorSet;
22+
import com.google.protobuf.Descriptors.DescriptorValidationException;
23+
import com.google.protobuf.Descriptors.FileDescriptor;
24+
import com.google.protobuf.ProtocolStringList;
2125
import io.serverlessworkflow.impl.resources.ExternalResourceHandler;
2226
import java.io.FileNotFoundException;
2327
import java.io.IOException;
@@ -28,15 +32,20 @@
2832
import java.nio.file.Path;
2933
import java.nio.file.SimpleFileVisitor;
3034
import java.nio.file.attribute.BasicFileAttributes;
35+
import java.util.ArrayList;
36+
import java.util.HashMap;
37+
import java.util.Iterator;
38+
import java.util.List;
39+
import java.util.Map;
40+
import java.util.Objects;
3141
import org.slf4j.Logger;
3242
import org.slf4j.LoggerFactory;
3343

3444
class FileDescriptorReader {
3545

3646
private static final Logger logger = LoggerFactory.getLogger(FileDescriptorReader.class);
3747

38-
static FileDescriptorProto readDescriptor(ExternalResourceHandler externalResourceHandler) {
39-
48+
static FileDescriptor readDescriptor(ExternalResourceHandler externalResourceHandler) {
4049
Path grpcDir = null;
4150
try (InputStream inputStream = externalResourceHandler.open()) {
4251
grpcDir = Files.createTempDirectory("serverless-workflow-");
@@ -46,21 +55,14 @@ static FileDescriptorProto readDescriptor(ExternalResourceHandler externalResour
4655
Files.copy(inputStream, protoFile);
4756
Path descriptorOutput = grpcDir.resolve("descriptor.protobin");
4857
generateFileDescriptor(grpcDir, protoFile, descriptorOutput);
49-
return DescriptorProtos.FileDescriptorSet.newBuilder()
50-
.mergeFrom(Files.readAllBytes(descriptorOutput))
51-
.build()
52-
.getFileList()
53-
.stream()
54-
.filter(n -> n.getName().equals(name))
55-
.findAny()
56-
.orElseThrow(
57-
() ->
58-
new IllegalStateException(
59-
"There was something really wrong generating the protoc descriptor, it does not included a file named "
60-
+ name));
58+
return toFileDescriptor(
59+
DescriptorProtos.FileDescriptorSet.newBuilder()
60+
.mergeFrom(Files.readAllBytes(descriptorOutput))
61+
.build(),
62+
name);
6163
} catch (IOException e) {
6264
throw new UncheckedIOException(
63-
"Unable to process gRPC proto file associated with resource "
65+
"Unable to process gRPC proto file associated with resource: "
6466
+ externalResourceHandler.name(),
6567
e);
6668
} finally {
@@ -70,6 +72,54 @@ static FileDescriptorProto readDescriptor(ExternalResourceHandler externalResour
7072
}
7173
}
7274

75+
private static FileDescriptor toFileDescriptor(FileDescriptorSet set, String name) {
76+
List<FileDescriptorProto> remainingProtos = new ArrayList<>(set.getFileList());
77+
Map<String, FileDescriptor> builtDescriptors = new HashMap<>();
78+
while (!remainingProtos.isEmpty()) {
79+
Iterator<FileDescriptorProto> iterator = remainingProtos.iterator();
80+
boolean modified = false;
81+
while (iterator.hasNext()) {
82+
FileDescriptorProto proto = iterator.next();
83+
ProtocolStringList requiredDependencies = proto.getDependencyList();
84+
List<FileDescriptor> currentDependencies =
85+
requiredDependencies.stream()
86+
.map(builtDescriptors::get)
87+
.filter(Objects::nonNull)
88+
.toList();
89+
if (requiredDependencies.size() == currentDependencies.size()) {
90+
FileDescriptor fileDescriptor = buildFileDescriptor(proto, currentDependencies);
91+
String protoName = proto.getName();
92+
if (protoName.equals(name)) {
93+
return fileDescriptor;
94+
}
95+
builtDescriptors.put(protoName, fileDescriptor);
96+
iterator.remove();
97+
modified = true;
98+
}
99+
}
100+
if (!modified) {
101+
throw new IllegalStateException(
102+
"Unable to build valid gRPC descriptor from proto file associated with resource: "
103+
+ name
104+
+ ". There are missing or circular dependencies");
105+
}
106+
}
107+
throw new IllegalStateException("Cannot build FileDescriptor for name: " + name);
108+
}
109+
110+
private static FileDescriptor buildFileDescriptor(
111+
FileDescriptorProto proto, List<FileDescriptor> currentDependencies) {
112+
try {
113+
return FileDescriptor.buildFrom(
114+
proto, currentDependencies.toArray(new FileDescriptor[currentDependencies.size()]));
115+
} catch (DescriptorValidationException e) {
116+
throw new IllegalStateException(
117+
"Unable to build valid gRPC descriptor from proto file associated with resource: "
118+
+ proto.getName(),
119+
e);
120+
}
121+
}
122+
73123
private static void deleteTempFiles(Path grpcDir) {
74124
try {
75125
Files.walkFileTree(
@@ -142,15 +192,10 @@ private static void generateFileDescriptorWithProcessBuilder(String[] protocArgs
142192
String[] command = new String[protocArgs.length + 1];
143193
command[0] = "protoc";
144194
System.arraycopy(protocArgs, 0, command, 1, protocArgs.length);
145-
146195
ProcessBuilder processBuilder = new ProcessBuilder(command);
147-
148196
processBuilder.redirectErrorStream(true);
149-
150197
Process process = processBuilder.start();
151-
152198
int exitCode = process.waitFor();
153-
154199
if (exitCode != 0) {
155200
throw new IOException(
156201
"Unable to generate file descriptor using system protoc. Exit code: " + exitCode);

0 commit comments

Comments
 (0)