Skip to content

Commit 8ec2cfc

Browse files
committed
Properly cleaning up temporary dir and simplying one file handling
Signed-off-by: fjtirado <ftirados@ibm.com>
1 parent 3537489 commit 8ec2cfc

5 files changed

Lines changed: 58 additions & 66 deletions

File tree

impl/core/src/main/java/io/serverlessworkflow/impl/resources/ClasspathResource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public ClasspathResource(String path) {
3131
public InputStream open() throws IOException {
3232
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
3333
if (is == null) {
34-
throw new IOException("Cannot load resouce with path: " + path + " from ClassPath");
34+
throw new IOException("Cannot load resource with path: " + path + " from classpath");
3535
}
3636
return is;
3737
}

impl/grpc/src/main/java/io/serverlessworkflow/impl/executors/grpc/FileDescriptorContext.java

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

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

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,50 +17,74 @@
1717

1818
import com.github.os72.protocjar.Protoc;
1919
import com.google.protobuf.DescriptorProtos;
20+
import com.google.protobuf.DescriptorProtos.FileDescriptorProto;
2021
import io.serverlessworkflow.impl.resources.ExternalResourceHandler;
2122
import java.io.FileNotFoundException;
2223
import java.io.IOException;
2324
import java.io.InputStream;
2425
import java.io.UncheckedIOException;
26+
import java.nio.file.FileVisitResult;
2527
import java.nio.file.Files;
2628
import java.nio.file.Path;
27-
import java.nio.file.StandardCopyOption;
29+
import java.nio.file.SimpleFileVisitor;
30+
import java.nio.file.attribute.BasicFileAttributes;
2831
import org.slf4j.Logger;
2932
import org.slf4j.LoggerFactory;
3033

3134
class FileDescriptorReader {
3235

3336
private static final Logger logger = LoggerFactory.getLogger(FileDescriptorReader.class);
3437

35-
static FileDescriptorContext readDescriptor(ExternalResourceHandler externalResourceHandler) {
38+
static FileDescriptorProto readDescriptor(ExternalResourceHandler externalResourceHandler) {
3639

40+
Path grpcDir = null;
3741
try (InputStream inputStream = externalResourceHandler.open()) {
38-
39-
Path grpcDir = Files.createTempDirectory("serverless-workflow-");
40-
41-
Path protoFile = grpcDir.resolve(externalResourceHandler.name());
42-
if (!Files.exists(protoFile)) {
43-
Files.createDirectories(protoFile.getParent());
44-
}
45-
46-
Files.copy(inputStream, protoFile, StandardCopyOption.REPLACE_EXISTING);
47-
42+
grpcDir = Files.createTempDirectory("serverless-workflow-");
43+
Files.createDirectories(grpcDir);
44+
Path protoFile = grpcDir.resolve(Path.of(externalResourceHandler.name()).getFileName());
45+
Files.copy(inputStream, protoFile);
4846
Path descriptorOutput = grpcDir.resolve("descriptor.protobin");
49-
5047
generateFileDescriptor(grpcDir, protoFile, descriptorOutput);
51-
52-
DescriptorProtos.FileDescriptorSet fileDescriptorSet =
53-
DescriptorProtos.FileDescriptorSet.newBuilder()
54-
.mergeFrom(Files.readAllBytes(descriptorOutput))
55-
.build();
56-
57-
return new FileDescriptorContext(fileDescriptorSet, externalResourceHandler.name());
58-
48+
return DescriptorProtos.FileDescriptorSet.newBuilder()
49+
.mergeFrom(Files.readAllBytes(descriptorOutput))
50+
.build()
51+
.getFile(0);
5952
} catch (IOException e) {
6053
throw new UncheckedIOException(
61-
"Unable to process GRPC descriptor file associated with resource "
54+
"Unable to process GRPC proto file associated with resource "
6255
+ externalResourceHandler.name(),
6356
e);
57+
} finally {
58+
if (grpcDir != null) {
59+
deleteTempFiles(grpcDir);
60+
}
61+
}
62+
}
63+
64+
private static void deleteTempFiles(Path grpcDir) {
65+
try {
66+
Files.walkFileTree(
67+
grpcDir,
68+
new SimpleFileVisitor<Path>() {
69+
@Override
70+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
71+
throws IOException {
72+
Files.delete(file);
73+
return FileVisitResult.CONTINUE;
74+
}
75+
76+
@Override
77+
public FileVisitResult postVisitDirectory(Path dir, IOException exc)
78+
throws IOException {
79+
if (exc != null) {
80+
throw exc;
81+
}
82+
Files.delete(dir);
83+
return FileVisitResult.CONTINUE;
84+
}
85+
});
86+
} catch (IOException e) {
87+
logger.warn("Error deleting GRPC temp dir " + grpcDir, e);
6488
}
6589
}
6690

@@ -69,11 +93,6 @@ static FileDescriptorContext readDescriptor(ExternalResourceHandler externalReso
6993
* the embedded protoc from protoc-jar library. If that fails with FileNotFoundException
7094
* (unsupported architecture), falls back to using the system's installed protoc via
7195
* ProcessBuilder.
72-
*
73-
* @param grpcDir a temporary directory
74-
* @param protoFile the .proto file used by <code>protoc</code> to generate the file descriptor
75-
* @param descriptorOutput the output directory where the descriptor file will be generated
76-
* @throws IOException
7796
*/
7897
private static void generateFileDescriptor(Path grpcDir, Path protoFile, Path descriptorOutput)
7998
throws IOException {
@@ -107,8 +126,6 @@ private static void generateFileDescriptor(Path grpcDir, Path protoFile, Path de
107126

108127
/*
109128
* Fallback method to generate file descriptor using system's installed protoc via ProcessBuilder.
110-
*
111-
* @param protocArgs the arguments to pass to protoc command
112129
*/
113130
private static void generateFileDescriptorWithProcessBuilder(String[] protocArgs)
114131
throws IOException {

impl/grpc/src/main/java/io/serverlessworkflow/impl/executors/grpc/GrpcExecutor.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package io.serverlessworkflow.impl.executors.grpc;
1717

18-
import com.google.protobuf.DescriptorProtos;
18+
import com.google.protobuf.DescriptorProtos.FileDescriptorProto;
1919
import com.google.protobuf.Descriptors;
2020
import com.google.protobuf.DynamicMessage;
2121
import com.google.protobuf.InvalidProtocolBufferException;
@@ -42,15 +42,15 @@ public class GrpcExecutor implements CallableTask {
4242

4343
private final GrpcRequestContext requestContext;
4444
private final WorkflowValueResolver<Map<String, Object>> arguments;
45-
private final FileDescriptorContext fileDescriptorContext;
45+
private final FileDescriptorProto fileDescriptorProto;
4646

4747
public GrpcExecutor(
4848
GrpcRequestContext requestContext,
4949
WorkflowValueResolver<Map<String, Object>> arguments,
50-
FileDescriptorContext fileDescriptorContext) {
50+
FileDescriptorProto fileDescriptorProto) {
5151
this.requestContext = requestContext;
5252
this.arguments = arguments;
53-
this.fileDescriptorContext = fileDescriptorContext;
53+
this.fileDescriptorProto = fileDescriptorProto;
5454
}
5555

5656
@Override
@@ -65,12 +65,6 @@ private CompletableFuture<WorkflowModel> buildGrpcCallExecutor(
6565

6666
Channel channel = GrpcChannelResolver.channel(workflowContext, taskContext, requestContext);
6767

68-
DescriptorProtos.FileDescriptorProto fileDescriptorProto =
69-
fileDescriptorContext.fileDescriptorSet().getFileList().stream()
70-
.filter(file -> file.getName().equals(fileDescriptorContext.inputProto()))
71-
.findFirst()
72-
.orElseThrow(() -> new IllegalStateException("Proto file not found in descriptor set"));
73-
7468
try {
7569
Descriptors.FileDescriptor fileDescriptor =
7670
Descriptors.FileDescriptor.buildFrom(

impl/grpc/src/main/java/io/serverlessworkflow/impl/executors/grpc/GrpcExecutorBuilder.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ public CallableTaskFactory init(
5050
new GrpcRequestContext(
5151
service.getHost(), service.getPort(), with.getMethod(), service.getName());
5252

53-
FileDescriptorContext fileDescriptorContext =
54-
definition
55-
.resourceLoader()
56-
.loadStatic(with.getProto().getEndpoint(), FileDescriptorReader::readDescriptor);
57-
return () -> new GrpcExecutor(grpcRequestContext, arguments, fileDescriptorContext);
53+
return () ->
54+
new GrpcExecutor(
55+
grpcRequestContext,
56+
arguments,
57+
definition
58+
.resourceLoader()
59+
.loadStatic(with.getProto().getEndpoint(), FileDescriptorReader::readDescriptor));
5860
}
5961
}

0 commit comments

Comments
 (0)