Skip to content

Commit

Permalink
[grid] Remove sessions from Redis backed session map on restart
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani committed Sep 2, 2021
1 parent a98b38f commit 8e6f4fe
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.openqa.selenium.events.EventBus;
import org.openqa.selenium.grid.config.Config;
import org.openqa.selenium.grid.data.NodeRemovedEvent;
import org.openqa.selenium.grid.data.NodeRestartedEvent;
import org.openqa.selenium.grid.data.Session;
import org.openqa.selenium.grid.data.SessionClosedEvent;
import org.openqa.selenium.grid.log.LoggingOptions;
Expand Down Expand Up @@ -85,6 +86,8 @@ public RedisBackedSessionMap(Tracer tracer, URI serverUri, EventBus bus) {
.filter(slot -> slot.getSession() != null)
.map(slot -> slot.getSession().getId())
.forEach(this::remove)));

bus.addListener(NodeRestartedEvent.listener(nodeStatus -> this.removeByUri(nodeStatus.getExternalUri())));
}

public static SessionMap create(Config config) {
Expand Down Expand Up @@ -275,6 +278,26 @@ public void remove(SessionId id) {
}
}

public void removeByUri(URI uri) {
List<String> uriKeys = connection.getKeysByPattern("session:*:uri");

if (uriKeys.isEmpty()) {
return;
}

String[] keys = new String[uriKeys.size()];
keys = uriKeys.toArray(keys);

List<KeyValue<String, String>> keyValues = connection.mget(keys);
keyValues.stream()
.filter(entry -> entry.getValue().equals(uri.toString()))
.map(KeyValue::getKey)
.map(key -> {
String[] sessionKey = key.split(":");
return new SessionId(sessionKey[1]);
}).forEach(this::remove);
}

@Override
public boolean isReady() {
return connection.isOpen();
Expand Down
4 changes: 4 additions & 0 deletions java/src/org/openqa/selenium/redis/GridRedisClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public String get(String key) {
return connection.sync().get(key);
}

public List<String> getKeysByPattern(String pattern) {
return connection.sync().keys(pattern);
}

public boolean isOpen() {
return connection.isOpen();
}
Expand Down

0 comments on commit 8e6f4fe

Please sign in to comment.