Skip to content

Commit 1d2f7aa

Browse files
committed
[Fix #921] Caching resources
Signed-off-by: fjtirado <[email protected]>
1 parent 6cf25cb commit 1d2f7aa

File tree

13 files changed

+137
-101
lines changed

13 files changed

+137
-101
lines changed

impl/core/src/main/java/io/serverlessworkflow/impl/WorkflowDefinition.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,5 +149,6 @@ public void addTaskExecutor(WorkflowMutablePosition position, TaskExecutor<?> ta
149149
@Override
150150
public void close() {
151151
safeClose(scheculedConsumer);
152+
safeClose(resourceLoader);
152153
}
153154
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package io.serverlessworkflow.impl.resources;
1717

1818
import java.io.InputStream;
19+
import java.util.Objects;
1920

2021
public class ClasspathResource implements ExternalResourceHandler {
2122

@@ -34,4 +35,18 @@ public InputStream open() {
3435
public String name() {
3536
return path;
3637
}
38+
39+
@Override
40+
public int hashCode() {
41+
return Objects.hash(path);
42+
}
43+
44+
@Override
45+
public boolean equals(Object obj) {
46+
if (this == obj) return true;
47+
if (obj == null) return false;
48+
if (getClass() != obj.getClass()) return false;
49+
ClasspathResource other = (ClasspathResource) obj;
50+
return Objects.equals(path, other.path);
51+
}
3752
}

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@
3232
import java.util.Map;
3333
import java.util.Optional;
3434
import java.util.ServiceLoader;
35+
import java.util.concurrent.ConcurrentHashMap;
3536
import java.util.concurrent.atomic.AtomicReference;
36-
import java.util.concurrent.locks.Lock;
37-
import java.util.concurrent.locks.ReentrantLock;
3837
import java.util.function.Function;
3938

4039
public class DefaultResourceLoader implements ResourceLoader {
@@ -45,8 +44,7 @@ public class DefaultResourceLoader implements ResourceLoader {
4544
private final AtomicReference<URITemplateResolver> templateResolver =
4645
new AtomicReference<URITemplateResolver>();
4746

48-
private Map<ExternalResourceHandler, CachedResource> resourceCache = new LRUCache<>(100);
49-
private Lock cacheLock = new ReentrantLock();
47+
private Map<ExternalResourceHandler, CachedResource> resourceCache = new ConcurrentHashMap<>();
5048

5149
protected DefaultResourceLoader(WorkflowApplication application, Path workflowPath) {
5250
this.application = application;
@@ -108,20 +106,15 @@ public <T> T load(
108106
workflowContext,
109107
taskContext,
110108
model == null ? application.modelFactory().fromNull() : model));
111-
try {
112-
CachedResource<T> cachedResource;
113-
cacheLock.lock();
114-
cachedResource = resourceCache.get(resourceHandler);
115-
cacheLock.unlock();
116-
if (cachedResource == null || resourceHandler.shouldReload(cachedResource.lastReload())) {
117-
cachedResource = new CachedResource(Instant.now(), function.apply(resourceHandler));
118-
cacheLock.lock();
119-
resourceCache.put(resourceHandler, cachedResource);
120-
}
121-
return cachedResource.content();
122-
} finally {
123-
cacheLock.unlock();
124-
}
109+
return (T)
110+
resourceCache
111+
.compute(
112+
resourceHandler,
113+
(k, v) ->
114+
v == null || resourceHandler.shouldReload(v.lastReload())
115+
? new CachedResource(Instant.now(), function.apply(resourceHandler))
116+
: v)
117+
.content();
125118
}
126119

127120
@Override
@@ -169,4 +162,8 @@ public URI apply(WorkflowContext workflow, TaskContext task, WorkflowModel node)
169162
return URI.create(expr.apply(workflow, task, node));
170163
}
171164
}
165+
166+
public void close() {
167+
resourceCache.clear();
168+
}
172169
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public interface ExternalResourceHandler {
2424

2525
InputStream open();
2626

27-
default boolean shouldReload(Instant lasUpdate) {
27+
default boolean shouldReload(Instant lastUpdate) {
2828
return false;
2929
}
3030
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import java.io.UncheckedIOException;
2121
import java.nio.file.Files;
2222
import java.nio.file.Path;
23+
import java.nio.file.attribute.BasicFileAttributes;
24+
import java.time.Instant;
25+
import java.util.Objects;
2326

2427
class FileResource implements ExternalResourceHandler {
2528

@@ -42,4 +45,30 @@ public InputStream open() {
4245
public String name() {
4346
return path.getFileName().toString();
4447
}
48+
49+
@Override
50+
public boolean shouldReload(Instant lastUpdate) {
51+
try {
52+
return Files.readAttributes(path, BasicFileAttributes.class)
53+
.lastModifiedTime()
54+
.toInstant()
55+
.isAfter(lastUpdate);
56+
} catch (IOException e) {
57+
throw new UncheckedIOException(e);
58+
}
59+
}
60+
61+
@Override
62+
public int hashCode() {
63+
return Objects.hash(path);
64+
}
65+
66+
@Override
67+
public boolean equals(Object obj) {
68+
if (this == obj) return true;
69+
if (obj == null) return false;
70+
if (getClass() != obj.getClass()) return false;
71+
FileResource other = (FileResource) obj;
72+
return Objects.equals(path, other.path);
73+
}
4574
}

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
import java.io.IOException;
1919
import java.io.InputStream;
2020
import java.io.UncheckedIOException;
21+
import java.net.HttpURLConnection;
2122
import java.net.URL;
23+
import java.net.URLConnection;
24+
import java.time.Instant;
25+
import java.util.Objects;
2226

2327
public class HttpResource implements ExternalResourceHandler {
2428

@@ -40,4 +44,37 @@ public InputStream open() {
4044
public String name() {
4145
return url.getFile();
4246
}
47+
48+
@Override
49+
public int hashCode() {
50+
return Objects.hash(url);
51+
}
52+
53+
@Override
54+
public boolean equals(Object obj) {
55+
if (this == obj) return true;
56+
if (obj == null) return false;
57+
if (getClass() != obj.getClass()) return false;
58+
HttpResource other = (HttpResource) obj;
59+
return Objects.equals(url, other.url);
60+
}
61+
62+
@Override
63+
public boolean shouldReload(Instant lastUpdate) {
64+
URLConnection connection = null;
65+
try {
66+
long millis = lastUpdate.toEpochMilli();
67+
connection = url.openConnection();
68+
connection.setDefaultUseCaches(true);
69+
connection.setIfModifiedSince(millis);
70+
connection.connect();
71+
boolean result = connection.getLastModified() > millis;
72+
if (!result && connection instanceof HttpURLConnection httpConnection) {
73+
httpConnection.disconnect();
74+
}
75+
return result;
76+
} catch (IOException e) {
77+
throw new UncheckedIOException(e);
78+
}
79+
}
4380
}

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

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import java.net.URI;
2525
import java.util.function.Function;
2626

27-
public interface ResourceLoader {
27+
public interface ResourceLoader extends AutoCloseable {
2828

2929
WorkflowValueResolver<URI> uriSupplier(Endpoint endpoint);
3030

impl/test/db-samples/running.db

8 KB
Binary file not shown.

0 commit comments

Comments
 (0)