Skip to content

Commit 54ead6a

Browse files
committed
[grid] Migrate cache builder to use Caffeine
Signed-off-by: Viet Nguyen Duc <[email protected]>
1 parent 35dadd7 commit 54ead6a

File tree

7 files changed

+136
-69
lines changed

7 files changed

+136
-69
lines changed

MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ maven.install(
177177
"com.google.code.findbugs:jsr305:3.0.2",
178178
"com.google.code.gson:gson:2.12.1",
179179
"com.google.guava:guava:33.4.5-jre",
180+
"com.github.ben-manes.caffeine:caffeine:3.2.0",
180181
"com.google.auto:auto-common:1.2.2",
181182
"com.google.auto.service:auto-service:1.1.1",
182183
"com.google.auto.service:auto-service-annotations:1.1.1",

java/maven_install.json

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": "THERE_IS_NO_DATA_ONLY_ZUUL",
3-
"__INPUT_ARTIFACTS_HASH": 466809341,
4-
"__RESOLVED_ARTIFACTS_HASH": -983027519,
3+
"__INPUT_ARTIFACTS_HASH": -95711414,
4+
"__RESOLVED_ARTIFACTS_HASH": -1591732796,
55
"artifacts": {
66
"com.beust:jcommander": {
77
"shasums": {
@@ -59,6 +59,13 @@
5959
},
6060
"version": "2.18.2"
6161
},
62+
"com.github.ben-manes.caffeine:caffeine": {
63+
"shasums": {
64+
"jar": "ec411dfdf0c03f25218648ce89861630b71680e5858a9a7278ebac8e55cab3d7",
65+
"sources": "67e14ef5c04c193a7fcafa788b55b89a079fd584b202469721ce6d2d6c753090"
66+
},
67+
"version": "3.2.0"
68+
},
6269
"com.github.javaparser:javaparser-core": {
6370
"shasums": {
6471
"jar": "a24c4fa7799ffe0c7a9af11d4eecd757098ed4498f86067bf28b46b2bfea1833",
@@ -810,6 +817,10 @@
810817
"com.fasterxml.jackson.core:jackson-databind",
811818
"org.yaml:snakeyaml"
812819
],
820+
"com.github.ben-manes.caffeine:caffeine": [
821+
"com.google.errorprone:error_prone_annotations",
822+
"org.jspecify:jspecify"
823+
],
813824
"com.github.spotbugs:spotbugs": [
814825
"com.github.spotbugs:spotbugs-annotations",
815826
"com.github.stephenc.jcip:jcip-annotations",
@@ -1199,6 +1210,10 @@
11991210
"com.fasterxml.jackson.dataformat.yaml.snakeyaml.error",
12001211
"com.fasterxml.jackson.dataformat.yaml.util"
12011212
],
1213+
"com.github.ben-manes.caffeine:caffeine": [
1214+
"com.github.benmanes.caffeine.cache",
1215+
"com.github.benmanes.caffeine.cache.stats"
1216+
],
12021217
"com.github.javaparser:javaparser-core": [
12031218
"com.github.javaparser",
12041219
"com.github.javaparser.ast",
@@ -2910,6 +2925,8 @@
29102925
"com.fasterxml.jackson.core:jackson-databind:jar:sources",
29112926
"com.fasterxml.jackson.dataformat:jackson-dataformat-yaml",
29122927
"com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:jar:sources",
2928+
"com.github.ben-manes.caffeine:caffeine",
2929+
"com.github.ben-manes.caffeine:caffeine:jar:sources",
29132930
"com.github.javaparser:javaparser-core",
29142931
"com.github.javaparser:javaparser-core:jar:sources",
29152932
"com.github.spotbugs:spotbugs",

java/src/org/openqa/selenium/grid/graphql/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ java_library(
2121
"//java/src/org/openqa/selenium/remote/http",
2222
artifact("com.google.guava:guava"),
2323
artifact("com.graphql-java:graphql-java"),
24+
artifact("com.github.ben-manes.caffeine:caffeine"),
2425
],
2526
)

java/src/org/openqa/selenium/grid/graphql/GraphqlHandler.java

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import static org.openqa.selenium.remote.tracing.Tags.HTTP_RESPONSE;
2929
import static org.openqa.selenium.remote.tracing.Tags.HTTP_RESPONSE_EVENT;
3030

31-
import com.google.common.cache.Cache;
32-
import com.google.common.cache.CacheBuilder;
31+
import com.github.benmanes.caffeine.cache.Cache;
32+
import com.github.benmanes.caffeine.cache.Caffeine;
3333
import graphql.ExecutionInput;
3434
import graphql.ExecutionResult;
3535
import graphql.GraphQL;
@@ -46,7 +46,6 @@
4646
import java.util.HashMap;
4747
import java.util.Map;
4848
import java.util.concurrent.CompletableFuture;
49-
import java.util.concurrent.ExecutionException;
5049
import org.openqa.selenium.grid.distributor.Distributor;
5150
import org.openqa.selenium.grid.sessionqueue.NewSessionQueue;
5251
import org.openqa.selenium.internal.Require;
@@ -84,33 +83,25 @@ public GraphqlHandler(
8483
this.publicUri = Require.nonNull("Uri", publicUri);
8584
this.version = Require.nonNull("GridVersion", version);
8685
this.tracer = Require.nonNull("Tracer", tracer);
86+
long maxMemory = Runtime.getRuntime().maxMemory();
87+
long cacheSize = maxMemory / 1024 / 1024;
8788

8889
GraphQLSchema schema =
8990
new SchemaGenerator()
9091
.makeExecutableSchema(buildTypeDefinitionRegistry(), buildRuntimeWiring());
9192

9293
Cache<String, CompletableFuture<PreparsedDocumentEntry>> cache =
93-
CacheBuilder.newBuilder().maximumSize(1024).build();
94+
Caffeine.newBuilder().maximumSize(cacheSize).build();
9495

9596
graphQl =
9697
GraphQL.newGraphQL(schema)
9798
.preparsedDocumentProvider(
98-
(executionInput, computeFunction) -> {
99-
try {
100-
return cache.get(
99+
(executionInput, computeFunction) ->
100+
cache.get(
101101
executionInput.getQuery(),
102-
() ->
102+
key ->
103103
CompletableFuture.supplyAsync(
104-
() -> computeFunction.apply(executionInput)));
105-
} catch (ExecutionException e) {
106-
if (e.getCause() instanceof RuntimeException) {
107-
throw (RuntimeException) e.getCause();
108-
} else if (e.getCause() != null) {
109-
throw new RuntimeException(e.getCause());
110-
}
111-
throw new RuntimeException(e);
112-
}
113-
})
104+
() -> computeFunction.apply(executionInput))))
114105
.build();
115106
}
116107

java/src/org/openqa/selenium/grid/node/local/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ java_library(
2828
"//java/src/org/openqa/selenium/json",
2929
"//java/src/org/openqa/selenium/remote",
3030
artifact("com.google.guava:guava"),
31+
artifact("com.github.ben-manes.caffeine:caffeine"),
3132
],
3233
)

java/src/org/openqa/selenium/grid/node/local/LocalNode.java

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,11 @@
3030
import static org.openqa.selenium.remote.http.Contents.string;
3131
import static org.openqa.selenium.remote.http.HttpMethod.DELETE;
3232

33+
import com.github.benmanes.caffeine.cache.Cache;
34+
import com.github.benmanes.caffeine.cache.Caffeine;
35+
import com.github.benmanes.caffeine.cache.RemovalCause;
36+
import com.github.benmanes.caffeine.cache.Ticker;
3337
import com.google.common.annotations.VisibleForTesting;
34-
import com.google.common.base.Ticker;
35-
import com.google.common.cache.Cache;
36-
import com.google.common.cache.CacheBuilder;
37-
import com.google.common.cache.RemovalCause;
38-
import com.google.common.cache.RemovalListener;
39-
import com.google.common.cache.RemovalNotification;
4038
import com.google.common.collect.ImmutableList;
4139
import com.google.common.collect.ImmutableMap;
4240
import java.io.Closeable;
@@ -56,7 +54,6 @@
5654
import java.util.Optional;
5755
import java.util.Set;
5856
import java.util.UUID;
59-
import java.util.concurrent.ExecutionException;
6057
import java.util.concurrent.Executors;
6158
import java.util.concurrent.ScheduledExecutorService;
6259
import java.util.concurrent.TimeUnit;
@@ -200,35 +197,35 @@ protected LocalNode(
200197
// Do not clear this cache automatically using a timer.
201198
// It will be explicitly cleaned up, as and when "currentSessions" is auto cleaned.
202199
this.uploadsTempFileSystem =
203-
CacheBuilder.newBuilder()
200+
Caffeine.newBuilder()
204201
.removalListener(
205-
(RemovalListener<SessionId, TemporaryFilesystem>)
206-
notification ->
207-
Optional.ofNullable(notification.getValue())
208-
.ifPresent(
209-
tempFS -> {
210-
tempFS.deleteTemporaryFiles();
211-
tempFS.deleteBaseDir();
212-
}))
202+
(SessionId key, TemporaryFilesystem tempFS, RemovalCause cause) -> {
203+
Optional.ofNullable(tempFS)
204+
.ifPresent(
205+
fs -> {
206+
fs.deleteTemporaryFiles();
207+
fs.deleteBaseDir();
208+
});
209+
})
213210
.build();
214211

215212
// Do not clear this cache automatically using a timer.
216213
// It will be explicitly cleaned up, as and when "currentSessions" is auto cleaned.
217214
this.downloadsTempFileSystem =
218-
CacheBuilder.newBuilder()
215+
Caffeine.newBuilder()
219216
.removalListener(
220-
(RemovalListener<SessionId, TemporaryFilesystem>)
221-
notification ->
222-
Optional.ofNullable(notification.getValue())
223-
.ifPresent(
224-
fs -> {
225-
fs.deleteTemporaryFiles();
226-
fs.deleteBaseDir();
227-
}))
217+
(SessionId key, TemporaryFilesystem tempFS, RemovalCause cause) -> {
218+
Optional.ofNullable(tempFS)
219+
.ifPresent(
220+
fs -> {
221+
fs.deleteTemporaryFiles();
222+
fs.deleteBaseDir();
223+
});
224+
})
228225
.build();
229226

230227
this.currentSessions =
231-
CacheBuilder.newBuilder()
228+
Caffeine.newBuilder()
232229
.expireAfterAccess(sessionTimeout)
233230
.ticker(ticker)
234231
.removalListener(this::stopTimedOutSession)
@@ -313,19 +310,19 @@ public void close() {
313310
shutdown.run();
314311
}
315312

316-
private void stopTimedOutSession(RemovalNotification<SessionId, SessionSlot> notification) {
313+
private void stopTimedOutSession(SessionId key, SessionSlot value, RemovalCause cause) {
317314
try (Span span = tracer.getCurrentContext().createSpan("node.stop_session")) {
318315
AttributeMap attributeMap = tracer.createAttributeMap();
319316
attributeMap.put(AttributeKey.LOGGER_CLASS.getKey(), getClass().getName());
320-
if (notification.getKey() != null && notification.getValue() != null) {
321-
SessionSlot slot = notification.getValue();
322-
SessionId id = notification.getKey();
317+
if (key != null && value != null) {
318+
SessionSlot slot = value;
319+
SessionId id = key;
323320
attributeMap.put("node.id", getId().toString());
324321
attributeMap.put("session.slotId", slot.getId().toString());
325322
attributeMap.put("session.id", id.toString());
326323
attributeMap.put("session.timeout_in_seconds", getSessionTimeout().toSeconds());
327-
attributeMap.put("session.remove.cause", notification.getCause().name());
328-
if (notification.wasEvicted() && notification.getCause() == RemovalCause.EXPIRED) {
324+
attributeMap.put("session.remove.cause", cause.name());
325+
if (cause == RemovalCause.EXPIRED) {
329326
// Session is timing out, stopping it by sending a DELETE
330327
LOG.log(Level.INFO, () -> String.format("Session id %s timed out, stopping...", id));
331328
span.setStatus(Status.CANCELLED);
@@ -334,7 +331,7 @@ private void stopTimedOutSession(RemovalNotification<SessionId, SessionSlot> not
334331
LOG.log(Level.INFO, () -> String.format("Session id %s is stopping on demand...", id));
335332
span.addEvent(String.format("Stopping the session %s on demand", id), attributeMap);
336333
}
337-
if (notification.wasEvicted()) {
334+
if (cause == RemovalCause.EXPIRED) {
338335
try {
339336
slot.execute(new HttpRequest(DELETE, "/session/" + id));
340337
} catch (Exception e) {
@@ -686,15 +683,11 @@ public Session getSession(SessionId id) throws NoSuchSessionException {
686683

687684
@Override
688685
public TemporaryFilesystem getUploadsFilesystem(SessionId id) throws IOException {
689-
try {
690-
return uploadsTempFileSystem.get(
691-
id,
692-
() ->
693-
TemporaryFilesystem.getTmpFsBasedOn(
694-
TemporaryFilesystem.getDefaultTmpFS().createTempDir("session", id.toString())));
695-
} catch (ExecutionException e) {
696-
throw new IOException(e);
697-
}
686+
return uploadsTempFileSystem.get(
687+
id,
688+
key ->
689+
TemporaryFilesystem.getTmpFsBasedOn(
690+
TemporaryFilesystem.getDefaultTmpFS().createTempDir("session", id.toString())));
698691
}
699692

700693
@Override
@@ -862,7 +855,7 @@ public void stop(SessionId id) throws NoSuchSessionException {
862855
}
863856

864857
private void stopAllSessions() {
865-
if (currentSessions.size() > 0) {
858+
if (currentSessions.estimatedSize() > 0) {
866859
LOG.info("Trying to stop all running sessions before shutting down...");
867860
currentSessions.invalidateAll();
868861
}

rust/Cargo.Bazel.lock

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"checksum": "f9b7e2302bd7b43ff0899cac357fbbfe0226873f02b610c0eb4246784e14e3b3",
2+
"checksum": "a896d7a078f1c885fa00c3b9aa5b8727c4f5bdfc58cdc26836fb74ebbdd3c87c",
33
"crates": {
44
"addr2line 0.21.0": {
55
"name": "addr2line",
@@ -2031,16 +2031,79 @@
20312031
"id": "jobserver 0.1.31",
20322032
"target": "jobserver"
20332033
},
2034-
{
2035-
"id": "libc 0.2.168",
2036-
"target": "libc"
2037-
},
20382034
{
20392035
"id": "shlex 1.3.0",
20402036
"target": "shlex"
20412037
}
20422038
],
2043-
"selects": {}
2039+
"selects": {
2040+
"aarch64-apple-darwin": [
2041+
{
2042+
"id": "libc 0.2.168",
2043+
"target": "libc"
2044+
}
2045+
],
2046+
"aarch64-unknown-linux-gnu": [
2047+
{
2048+
"id": "libc 0.2.168",
2049+
"target": "libc"
2050+
}
2051+
],
2052+
"aarch64-unknown-nixos-gnu": [
2053+
{
2054+
"id": "libc 0.2.168",
2055+
"target": "libc"
2056+
}
2057+
],
2058+
"arm-unknown-linux-gnueabi": [
2059+
{
2060+
"id": "libc 0.2.168",
2061+
"target": "libc"
2062+
}
2063+
],
2064+
"i686-unknown-linux-gnu": [
2065+
{
2066+
"id": "libc 0.2.168",
2067+
"target": "libc"
2068+
}
2069+
],
2070+
"powerpc-unknown-linux-gnu": [
2071+
{
2072+
"id": "libc 0.2.168",
2073+
"target": "libc"
2074+
}
2075+
],
2076+
"s390x-unknown-linux-gnu": [
2077+
{
2078+
"id": "libc 0.2.168",
2079+
"target": "libc"
2080+
}
2081+
],
2082+
"x86_64-apple-darwin": [
2083+
{
2084+
"id": "libc 0.2.168",
2085+
"target": "libc"
2086+
}
2087+
],
2088+
"x86_64-unknown-freebsd": [
2089+
{
2090+
"id": "libc 0.2.168",
2091+
"target": "libc"
2092+
}
2093+
],
2094+
"x86_64-unknown-linux-gnu": [
2095+
{
2096+
"id": "libc 0.2.168",
2097+
"target": "libc"
2098+
}
2099+
],
2100+
"x86_64-unknown-nixos-gnu": [
2101+
{
2102+
"id": "libc 0.2.168",
2103+
"target": "libc"
2104+
}
2105+
]
2106+
}
20442107
},
20452108
"edition": "2018",
20462109
"version": "1.1.30"

0 commit comments

Comments
 (0)