Skip to content

Commit a684d3a

Browse files
joerg1985sandeepsuryaprasad
authored andcommitted
[grid] remove indirection in cache structures
1 parent 8d3504f commit a684d3a

File tree

3 files changed

+37
-75
lines changed

3 files changed

+37
-75
lines changed

java/src/org/openqa/selenium/grid/node/Node.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.util.Map;
3333
import java.util.ServiceLoader;
3434
import java.util.Set;
35-
import java.util.UUID;
3635
import java.util.logging.Logger;
3736
import java.util.stream.Collectors;
3837
import java.util.stream.StreamSupport;
@@ -245,7 +244,7 @@ public TemporaryFilesystem getUploadsFilesystem(SessionId id) throws IOException
245244
throw new UnsupportedOperationException();
246245
}
247246

248-
public TemporaryFilesystem getDownloadsFilesystem(UUID uuid) throws IOException {
247+
public TemporaryFilesystem getDownloadsFilesystem(SessionId id) throws IOException {
249248
throw new UnsupportedOperationException();
250249
}
251250

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

Lines changed: 32 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ public class LocalNode extends Node implements Closeable {
139139
private final List<SessionSlot> factories;
140140
private final Cache<SessionId, SessionSlot> currentSessions;
141141
private final Cache<SessionId, TemporaryFilesystem> uploadsTempFileSystem;
142-
private final Cache<UUID, TemporaryFilesystem> downloadsTempFileSystem;
143-
private final Cache<SessionId, UUID> sessionToDownloadsDir;
142+
private final Cache<SessionId, TemporaryFilesystem> downloadsTempFileSystem;
144143
private final AtomicInteger pendingSessions = new AtomicInteger();
145144
private final AtomicInteger sessionCount = new AtomicInteger();
146145
private final Runnable shutdown;
@@ -198,7 +197,7 @@ protected LocalNode(
198197
: healthCheck;
199198

200199
// Do not clear this cache automatically using a timer.
201-
// It will be explicitly cleaned up, as and when "sessionToDownloadsDir" is auto cleaned.
200+
// It will be explicitly cleaned up, as and when "currentSessions" is auto cleaned.
202201
this.uploadsTempFileSystem =
203202
CacheBuilder.newBuilder()
204203
.removalListener(
@@ -213,11 +212,11 @@ protected LocalNode(
213212
.build();
214213

215214
// Do not clear this cache automatically using a timer.
216-
// It will be explicitly cleaned up, as and when "sessionToDownloadsDir" is auto cleaned.
215+
// It will be explicitly cleaned up, as and when "currentSessions" is auto cleaned.
217216
this.downloadsTempFileSystem =
218217
CacheBuilder.newBuilder()
219218
.removalListener(
220-
(RemovalListener<UUID, TemporaryFilesystem>)
219+
(RemovalListener<SessionId, TemporaryFilesystem>)
221220
notification ->
222221
Optional.ofNullable(notification.getValue())
223222
.ifPresent(
@@ -227,32 +226,6 @@ protected LocalNode(
227226
}))
228227
.build();
229228

230-
// Do not clear this cache automatically using a timer.
231-
// It will be explicitly cleaned up, as and when "currentSessions" is auto cleaned.
232-
this.sessionToDownloadsDir =
233-
CacheBuilder.newBuilder()
234-
.removalListener(
235-
(RemovalListener<SessionId, UUID>)
236-
notification -> {
237-
Optional.ofNullable(notification.getValue())
238-
.ifPresent(
239-
value -> {
240-
downloadsTempFileSystem.invalidate(value);
241-
LOG.fine(
242-
"Removing Downloads folder associated with "
243-
+ notification.getKey());
244-
});
245-
Optional.ofNullable(notification.getKey())
246-
.ifPresent(
247-
value -> {
248-
uploadsTempFileSystem.invalidate(value);
249-
LOG.fine(
250-
"Removing Uploads folder associated with "
251-
+ notification.getKey());
252-
});
253-
})
254-
.build();
255-
256229
this.currentSessions =
257230
CacheBuilder.newBuilder()
258231
.expireAfterAccess(sessionTimeout)
@@ -386,15 +359,6 @@ public int getCurrentSessionCount() {
386359
return Math.toIntExact(n);
387360
}
388361

389-
@VisibleForTesting
390-
public UUID getDownloadsIdForSession(SessionId id) {
391-
UUID uuid = sessionToDownloadsDir.getIfPresent(id);
392-
if (uuid == null) {
393-
throw new NoSuchSessionException("Cannot find session with id: " + id);
394-
}
395-
return uuid;
396-
}
397-
398362
@ManagedAttribute(name = "MaxSessions")
399363
public int getMaxSessionCount() {
400364
return maxSessionCount;
@@ -508,21 +472,32 @@ public Either<WebDriverException, CreateSessionResponse> newSession(
508472
return Either.left(new RetrySessionRequestException("Drain after session count reached."));
509473
}
510474

511-
UUID uuidForSessionDownloads = UUID.randomUUID();
512475
Capabilities desiredCapabilities = sessionRequest.getDesiredCapabilities();
476+
TemporaryFilesystem downloadsTfs;
513477
if (managedDownloadsRequested(desiredCapabilities)) {
514-
Capabilities enhanced = setDownloadsDirectory(uuidForSessionDownloads, desiredCapabilities);
478+
UUID uuidForSessionDownloads = UUID.randomUUID();
479+
480+
downloadsTfs =
481+
TemporaryFilesystem.getTmpFsBasedOn(
482+
TemporaryFilesystem.getDefaultTmpFS()
483+
.createTempDir("uuid", uuidForSessionDownloads.toString()));
484+
485+
Capabilities enhanced = setDownloadsDirectory(downloadsTfs, desiredCapabilities);
515486
enhanced = desiredCapabilities.merge(enhanced);
516487
sessionRequest =
517488
new CreateSessionRequest(
518489
sessionRequest.getDownstreamDialects(), enhanced, sessionRequest.getMetadata());
490+
} else {
491+
downloadsTfs = null;
519492
}
520493

521494
Either<WebDriverException, ActiveSession> possibleSession = slotToUse.apply(sessionRequest);
522495

523496
if (possibleSession.isRight()) {
524497
ActiveSession session = possibleSession.right();
525-
sessionToDownloadsDir.put(session.getId(), uuidForSessionDownloads);
498+
if (downloadsTfs != null) {
499+
downloadsTempFileSystem.put(session.getId(), downloadsTfs);
500+
}
526501
currentSessions.put(session.getId(), slotToUse);
527502

528503
SessionId sessionId = session.getId();
@@ -558,6 +533,10 @@ public Either<WebDriverException, CreateSessionResponse> newSession(
558533
getEncoder(session.getDownstreamDialect()).apply(externalSession)));
559534
} else {
560535
slotToUse.release();
536+
if (downloadsTfs != null) {
537+
downloadsTfs.deleteTemporaryFiles();
538+
downloadsTfs.deleteBaseDir();
539+
}
561540
span.setAttribute(AttributeKey.ERROR.getKey(), true);
562541
span.setStatus(Status.ABORTED);
563542
span.addEvent("Unable to create session with the driver", attributeMap);
@@ -576,15 +555,8 @@ private boolean managedDownloadsRequested(Capabilities capabilities) {
576555
&& Boolean.parseBoolean(downloadsEnabled.toString());
577556
}
578557

579-
private Capabilities setDownloadsDirectory(UUID uuid, Capabilities caps) {
580-
File tempDir;
581-
try {
582-
TemporaryFilesystem tempFS = getDownloadsFilesystem(uuid);
583-
// tempDir = tempFS.createTempDir("download", "file");
584-
tempDir = tempFS.createTempDir("download", "");
585-
} catch (IOException e) {
586-
throw new UncheckedIOException(e);
587-
}
558+
private Capabilities setDownloadsDirectory(TemporaryFilesystem downloadsTfs, Capabilities caps) {
559+
File tempDir = downloadsTfs.createTempDir("download", "");
588560
if (Browser.CHROME.is(caps) || Browser.EDGE.is(caps)) {
589561
ImmutableMap<String, Serializable> map =
590562
ImmutableMap.of(
@@ -700,16 +672,8 @@ public TemporaryFilesystem getUploadsFilesystem(SessionId id) throws IOException
700672
}
701673

702674
@Override
703-
public TemporaryFilesystem getDownloadsFilesystem(UUID uuid) throws IOException {
704-
try {
705-
return downloadsTempFileSystem.get(
706-
uuid,
707-
() ->
708-
TemporaryFilesystem.getTmpFsBasedOn(
709-
TemporaryFilesystem.getDefaultTmpFS().createTempDir("uuid", uuid.toString())));
710-
} catch (ExecutionException e) {
711-
throw new IOException(e);
712-
}
675+
public TemporaryFilesystem getDownloadsFilesystem(SessionId sessionId) throws IOException {
676+
return downloadsTempFileSystem.getIfPresent(sessionId);
713677
}
714678

715679
@Override
@@ -746,11 +710,7 @@ public HttpResponse downloadFile(HttpRequest req, SessionId id) {
746710
+ "[--enable-managed-downloads] and restart the node";
747711
throw new WebDriverException(msg);
748712
}
749-
UUID uuid = sessionToDownloadsDir.getIfPresent(id);
750-
if (uuid == null) {
751-
throw new NoSuchSessionException("Cannot find session with id: " + id);
752-
}
753-
TemporaryFilesystem tempFS = downloadsTempFileSystem.getIfPresent(uuid);
713+
TemporaryFilesystem tempFS = downloadsTempFileSystem.getIfPresent(id);
754714
if (tempFS == null) {
755715
String msg =
756716
"Cannot find downloads file system for session id: "
@@ -860,8 +820,11 @@ public HttpResponse uploadFile(HttpRequest req, SessionId id) {
860820
public void stop(SessionId id) throws NoSuchSessionException {
861821
Require.nonNull("Session ID", id);
862822

863-
if (sessionToDownloadsDir.getIfPresent(id) != null) {
864-
sessionToDownloadsDir.invalidate(id);
823+
if (downloadsTempFileSystem.getIfPresent(id) != null) {
824+
downloadsTempFileSystem.invalidate(id);
825+
}
826+
if (uploadsTempFileSystem.getIfPresent(id) != null) {
827+
uploadsTempFileSystem.invalidate(id);
865828
}
866829

867830
SessionSlot slot = currentSessions.getIfPresent(id);

java/test/org/openqa/selenium/grid/node/NodeTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -640,8 +640,8 @@ void canDownloadMultipleFile() throws IOException {
640640
String decodedContents = String.join("", Files.readAllLines(path));
641641
assertThat(decodedContents).isEqualTo(hello);
642642
} finally {
643-
UUID downloadsId = local.getDownloadsIdForSession(session.getId());
644-
File someDir = getTemporaryFilesystemBaseDir(local.getDownloadsFilesystem(downloadsId));
643+
TemporaryFilesystem downloadsTfs = local.getDownloadsFilesystem(session.getId());
644+
File someDir = getTemporaryFilesystemBaseDir(downloadsTfs);
645645
node.stop(session.getId());
646646
assertThat(someDir).doesNotExist();
647647
}
@@ -947,8 +947,8 @@ private CreateSessionRequest createSessionRequest(Capabilities caps) {
947947

948948
private String simulateFileDownload(SessionId id, String text) throws IOException {
949949
File zip = createTmpFile(text);
950-
UUID downloadsId = local.getDownloadsIdForSession(id);
951-
File someDir = getTemporaryFilesystemBaseDir(local.getDownloadsFilesystem(downloadsId));
950+
TemporaryFilesystem downloadsTfs = local.getDownloadsFilesystem(id);
951+
File someDir = getTemporaryFilesystemBaseDir(downloadsTfs);
952952
File downloadsDirectory = Optional.ofNullable(someDir.listFiles()).orElse(new File[] {})[0];
953953
File target = new File(downloadsDirectory, zip.getName());
954954
boolean renamed = zip.renameTo(target);

0 commit comments

Comments
 (0)