Skip to content

Commit

Permalink
[grid] Add Redis-backed SessionMap
Browse files Browse the repository at this point in the history
This allows someone to make use of Redis as a backing store for the
session map. While not useful in the common case, when Grid is
deployed to (eg) k8s, this provides a hook for scaling the system.
  • Loading branch information
shs96c committed Jan 16, 2020
1 parent d7389a2 commit bf926fc
Show file tree
Hide file tree
Showing 7 changed files with 483 additions and 1 deletion.
2 changes: 2 additions & 0 deletions java/maven_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def selenium_java_deps():
"com.google.auto.service:auto-service-annotations:1.0-rc6",
"com.squareup.okhttp3:okhttp:4.3.0",
"com.typesafe.netty:netty-reactive-streams:2.0.4",
"io.lettuce:lettuce-core:5.2.1.RELEASE",
"io.netty:netty-buffer:%s" % netty_version,
"io.netty:netty-codec-haproxy:%s" % netty_version,
"io.netty:netty-codec-http:%s" % netty_version,
Expand All @@ -25,6 +26,7 @@ def selenium_java_deps():
"io.opentracing:opentracing-api:0.33.0",
"io.opentracing:opentracing-noop:0.33.0",
"io.opentracing.contrib:opentracing-tracerresolver:0.1.8",
"it.ozimov:embedded-redis:0.7.2",
"javax.servlet:javax.servlet-api:3.1.0",
maven.artifact(
group = "junit",
Expand Down
170 changes: 169 additions & 1 deletion java/maven_install.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"dependency_tree": {
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": -702764252,
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": -1459113808,
"conflict_resolution": {},
"dependencies": [
{
Expand Down Expand Up @@ -916,6 +916,70 @@
"sha256": "5d78a0f6cb46b8196b8d57269a68ce29f1721c0b8f44c93a6a1c13ad544a13db",
"url": "https://repo1.maven.org/maven2/eu/neilalexander/jnacl/1.0.0/jnacl-1.0.0-sources.jar"
},
{
"coord": "io.lettuce:lettuce-core:5.2.1.RELEASE",
"dependencies": [
"org.reactivestreams:reactive-streams:1.0.3",
"io.netty:netty-resolver:4.1.44.Final",
"io.netty:netty-codec:4.1.44.Final",
"io.netty:netty-buffer:4.1.44.Final",
"io.projectreactor:reactor-core:3.3.0.RELEASE",
"io.netty:netty-transport:4.1.44.Final",
"io.netty:netty-common:4.1.44.Final",
"io.netty:netty-handler:4.1.44.Final"
],
"directDependencies": [
"io.netty:netty-common:4.1.44.Final",
"io.netty:netty-handler:4.1.44.Final",
"io.netty:netty-transport:4.1.44.Final",
"io.projectreactor:reactor-core:3.3.0.RELEASE"
],
"exclusions": [
"org.hamcrest:hamcrest-all",
"org.hamcrest:hamcrest-core",
"io.netty:netty-all"
],
"file": "v1/https/repo1.maven.org/maven2/io/lettuce/lettuce-core/5.2.1.RELEASE/lettuce-core-5.2.1.RELEASE.jar",
"mirror_urls": [
"https://repo1.maven.org/maven2/io/lettuce/lettuce-core/5.2.1.RELEASE/lettuce-core-5.2.1.RELEASE.jar",
"https://jcenter.bintray.com/io/lettuce/lettuce-core/5.2.1.RELEASE/lettuce-core-5.2.1.RELEASE.jar",
"https://maven.google.com/io/lettuce/lettuce-core/5.2.1.RELEASE/lettuce-core-5.2.1.RELEASE.jar"
],
"sha256": "e821b90ed7df17911c4cea9427293f1a27bdf26ce5e5fa018b99ddbfd7177daf",
"url": "https://repo1.maven.org/maven2/io/lettuce/lettuce-core/5.2.1.RELEASE/lettuce-core-5.2.1.RELEASE.jar"
},
{
"coord": "io.lettuce:lettuce-core:jar:sources:5.2.1.RELEASE",
"dependencies": [
"io.netty:netty-resolver:jar:sources:4.1.44.Final",
"org.reactivestreams:reactive-streams:jar:sources:1.0.3",
"io.netty:netty-buffer:jar:sources:4.1.44.Final",
"io.projectreactor:reactor-core:jar:sources:3.3.0.RELEASE",
"io.netty:netty-handler:jar:sources:4.1.44.Final",
"io.netty:netty-common:jar:sources:4.1.44.Final",
"io.netty:netty-codec:jar:sources:4.1.44.Final",
"io.netty:netty-transport:jar:sources:4.1.44.Final"
],
"directDependencies": [
"io.netty:netty-common:jar:sources:4.1.44.Final",
"io.netty:netty-handler:jar:sources:4.1.44.Final",
"io.netty:netty-transport:jar:sources:4.1.44.Final",
"io.projectreactor:reactor-core:jar:sources:3.3.0.RELEASE"
],
"exclusions": [
"org.hamcrest:hamcrest-all",
"org.hamcrest:hamcrest-core",
"io.netty:netty-all"
],
"file": "v1/https/repo1.maven.org/maven2/io/lettuce/lettuce-core/5.2.1.RELEASE/lettuce-core-5.2.1.RELEASE-sources.jar",
"mirror_urls": [
"https://repo1.maven.org/maven2/io/lettuce/lettuce-core/5.2.1.RELEASE/lettuce-core-5.2.1.RELEASE-sources.jar",
"https://jcenter.bintray.com/io/lettuce/lettuce-core/5.2.1.RELEASE/lettuce-core-5.2.1.RELEASE-sources.jar",
"https://maven.google.com/io/lettuce/lettuce-core/5.2.1.RELEASE/lettuce-core-5.2.1.RELEASE-sources.jar"
],
"sha256": "b7d8ff9273afed8c88744ffc55423a0479791fb61e6024585ae68331f7150703",
"url": "https://repo1.maven.org/maven2/io/lettuce/lettuce-core/5.2.1.RELEASE/lettuce-core-5.2.1.RELEASE-sources.jar"
},
{
"coord": "io.netty:netty-buffer:4.1.44.Final",
"dependencies": [
Expand Down Expand Up @@ -1694,6 +1758,110 @@
"sha256": "f92d87a877b4466a7e7913d3d8bb74902af0630924d6963609fca813e36dc505",
"url": "https://repo1.maven.org/maven2/io/opentracing/opentracing-noop/0.33.0/opentracing-noop-0.33.0-sources.jar"
},
{
"coord": "io.projectreactor:reactor-core:3.3.0.RELEASE",
"dependencies": [
"org.reactivestreams:reactive-streams:1.0.3"
],
"directDependencies": [
"org.reactivestreams:reactive-streams:1.0.3"
],
"exclusions": [
"org.hamcrest:hamcrest-all",
"org.hamcrest:hamcrest-core",
"io.netty:netty-all"
],
"file": "v1/https/repo1.maven.org/maven2/io/projectreactor/reactor-core/3.3.0.RELEASE/reactor-core-3.3.0.RELEASE.jar",
"mirror_urls": [
"https://repo1.maven.org/maven2/io/projectreactor/reactor-core/3.3.0.RELEASE/reactor-core-3.3.0.RELEASE.jar",
"https://jcenter.bintray.com/io/projectreactor/reactor-core/3.3.0.RELEASE/reactor-core-3.3.0.RELEASE.jar",
"https://maven.google.com/io/projectreactor/reactor-core/3.3.0.RELEASE/reactor-core-3.3.0.RELEASE.jar"
],
"sha256": "a25f0433d5f61ecd24c2f44a7171079101ffd4a19f2bfaa423af00c73feba136",
"url": "https://repo1.maven.org/maven2/io/projectreactor/reactor-core/3.3.0.RELEASE/reactor-core-3.3.0.RELEASE.jar"
},
{
"coord": "io.projectreactor:reactor-core:jar:sources:3.3.0.RELEASE",
"dependencies": [
"org.reactivestreams:reactive-streams:jar:sources:1.0.3"
],
"directDependencies": [
"org.reactivestreams:reactive-streams:jar:sources:1.0.3"
],
"exclusions": [
"org.hamcrest:hamcrest-all",
"org.hamcrest:hamcrest-core",
"io.netty:netty-all"
],
"file": "v1/https/repo1.maven.org/maven2/io/projectreactor/reactor-core/3.3.0.RELEASE/reactor-core-3.3.0.RELEASE-sources.jar",
"mirror_urls": [
"https://repo1.maven.org/maven2/io/projectreactor/reactor-core/3.3.0.RELEASE/reactor-core-3.3.0.RELEASE-sources.jar",
"https://jcenter.bintray.com/io/projectreactor/reactor-core/3.3.0.RELEASE/reactor-core-3.3.0.RELEASE-sources.jar",
"https://maven.google.com/io/projectreactor/reactor-core/3.3.0.RELEASE/reactor-core-3.3.0.RELEASE-sources.jar"
],
"sha256": "36b3a60cbe054fc26fd7c7fc83919c7c741b54e6083b1ee9608d53f97dbf894f",
"url": "https://repo1.maven.org/maven2/io/projectreactor/reactor-core/3.3.0.RELEASE/reactor-core-3.3.0.RELEASE-sources.jar"
},
{
"coord": "it.ozimov:embedded-redis:0.7.2",
"dependencies": [
"com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava",
"com.google.j2objc:j2objc-annotations:1.3",
"commons-io:commons-io:2.6",
"com.google.code.findbugs:jsr305:3.0.2",
"com.google.errorprone:error_prone_annotations:2.3.4",
"com.google.guava:guava:28.2-jre",
"org.checkerframework:checker-qual:2.10.0",
"com.google.guava:failureaccess:1.0.1"
],
"directDependencies": [
"com.google.guava:guava:28.2-jre",
"commons-io:commons-io:2.6"
],
"exclusions": [
"org.hamcrest:hamcrest-all",
"org.hamcrest:hamcrest-core",
"io.netty:netty-all"
],
"file": "v1/https/repo1.maven.org/maven2/it/ozimov/embedded-redis/0.7.2/embedded-redis-0.7.2.jar",
"mirror_urls": [
"https://repo1.maven.org/maven2/it/ozimov/embedded-redis/0.7.2/embedded-redis-0.7.2.jar",
"https://jcenter.bintray.com/it/ozimov/embedded-redis/0.7.2/embedded-redis-0.7.2.jar",
"https://maven.google.com/it/ozimov/embedded-redis/0.7.2/embedded-redis-0.7.2.jar"
],
"sha256": "afa1fd72beb381ee2faa742536ae9381b843fec8912e9cab08c89e016e1fe82a",
"url": "https://repo1.maven.org/maven2/it/ozimov/embedded-redis/0.7.2/embedded-redis-0.7.2.jar"
},
{
"coord": "it.ozimov:embedded-redis:jar:sources:0.7.2",
"dependencies": [
"com.google.code.findbugs:jsr305:jar:sources:3.0.2",
"com.google.j2objc:j2objc-annotations:jar:sources:1.3",
"com.google.errorprone:error_prone_annotations:jar:sources:2.3.4",
"commons-io:commons-io:jar:sources:2.6",
"org.checkerframework:checker-qual:jar:sources:2.10.0",
"com.google.guava:guava:jar:sources:28.2-jre",
"com.google.guava:listenablefuture:jar:sources:9999.0-empty-to-avoid-conflict-with-guava",
"com.google.guava:failureaccess:jar:sources:1.0.1"
],
"directDependencies": [
"com.google.guava:guava:jar:sources:28.2-jre",
"commons-io:commons-io:jar:sources:2.6"
],
"exclusions": [
"org.hamcrest:hamcrest-all",
"org.hamcrest:hamcrest-core",
"io.netty:netty-all"
],
"file": "v1/https/repo1.maven.org/maven2/it/ozimov/embedded-redis/0.7.2/embedded-redis-0.7.2-sources.jar",
"mirror_urls": [
"https://repo1.maven.org/maven2/it/ozimov/embedded-redis/0.7.2/embedded-redis-0.7.2-sources.jar",
"https://jcenter.bintray.com/it/ozimov/embedded-redis/0.7.2/embedded-redis-0.7.2-sources.jar",
"https://maven.google.com/it/ozimov/embedded-redis/0.7.2/embedded-redis-0.7.2-sources.jar"
],
"sha256": "afb92917fec024cf9af1b76149668b0feef11f594ef248e273021ac32eeb892f",
"url": "https://repo1.maven.org/maven2/it/ozimov/embedded-redis/0.7.2/embedded-redis-0.7.2-sources.jar"
},
{
"coord": "javax.servlet:javax.servlet-api:3.1.0",
"dependencies": [],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
load("@rules_jvm_external//:defs.bzl", "artifact")
load("//java:version.bzl", "SE_VERSION")
load("//java:defs.bzl", "java_export")

java_export(
name = "redis",
srcs = glob(["*.java"]),
maven_coordinates = "org.seleniumhq.selenium:selenium-session-map-redis:%s" % SE_VERSION,
pom_template = "//java/client/src/org/openqa/selenium:template-pom",
exports = [
# SessionMap is in the public api
"//java/server/src/org/openqa/selenium/grid:grid",
],
deps = [
"//java/client/src/org/openqa/selenium/json",
"//java/client/src/org/openqa/selenium/remote",
"//java/server/src/org/openqa/selenium/grid:grid",
artifact("com.google.guava:guava"),
artifact("io.lettuce:lettuce-core"),
],
visibility = [
"//visibility:public",
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.grid.sessionmap.redis;

import com.google.common.collect.ImmutableMap;
import io.lettuce.core.KeyValue;
import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import io.opentracing.Tracer;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.NoSuchSessionException;
import org.openqa.selenium.grid.config.Config;
import org.openqa.selenium.grid.data.Session;
import org.openqa.selenium.grid.log.LoggingOptions;
import org.openqa.selenium.grid.sessionmap.SessionMap;
import org.openqa.selenium.grid.sessionmap.config.SessionMapOptions;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.SessionId;

import java.io.Closeable;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Objects;

public class RedisBackedSessionMap extends SessionMap implements Closeable {

private static final Json JSON = new Json();
private final RedisClient client;
private final StatefulRedisConnection<String, String> connection;

public RedisBackedSessionMap(Tracer tracer, URI serverUri) {
super(tracer);

client = RedisClient.create(RedisURI.create(serverUri));
connection = client.connect();
}

public static SessionMap create(Config config) {
Tracer tracer = new LoggingOptions(config).getTracer();
URI sessionMapUri = new SessionMapOptions(config).getSessionMapUri();

return new RedisBackedSessionMap(tracer, sessionMapUri);
}

@Override
public boolean add(Session session) {
Objects.requireNonNull(session, "Session to add must be set.");

RedisCommands<String, String> commands = connection.sync();
commands.mset(
ImmutableMap.of(
uriKey(session.getId()), session.getUri().toString(),
capabilitiesKey(session.getId()), JSON.toJson(session.getCapabilities())));

return true;
}

@Override
public Session get(SessionId id) throws NoSuchSessionException {
Objects.requireNonNull(id, "Session ID to use must be set.");

URI uri = getUri(id);

RedisCommands<String, String> commands = connection.sync();
String rawCapabilities = commands.get(capabilitiesKey(id));
Capabilities caps = rawCapabilities == null ?
new ImmutableCapabilities() :
JSON.toType(rawCapabilities, Capabilities.class);

return new Session(id, uri, caps);
}

@Override
public URI getUri(SessionId id) throws NoSuchSessionException {
Objects.requireNonNull(id, "Session ID to use must be set.");

RedisCommands<String, String> commands = connection.sync();

List<KeyValue<String, String>> rawValues = commands.mget(uriKey(id), capabilitiesKey(id));

String rawUri = rawValues.get(0).getValueOrElse(null);
if (rawUri == null) {
throw new NoSuchSessionException("Unable to find URI for session " + id);
}

try {
return new URI(rawUri);
} catch (URISyntaxException e) {
throw new NoSuchSessionException(String.format("Unable to convert session id (%s) to uri: %s", id, rawUri), e);
}
}

@Override
public void remove(SessionId id) {
Objects.requireNonNull(id, "Session ID to use must be set.");

RedisCommands<String, String> commands = connection.sync();

commands.del(uriKey(id), capabilitiesKey(id));
}

@Override
public void close() {
client.shutdown();
}

private String uriKey(SessionId id) {
Objects.requireNonNull(id, "Session ID to use must be set.");

return "session:" + id.toString() + ":uri";
}

private String capabilitiesKey(SessionId id) {
Objects.requireNonNull(id, "Session ID to use must be set.");

return "session:" + id.toString() + ":capabilities";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ public Session get(SessionId id) {
return session;
}

@Override
public URI getUri(SessionId id) throws NoSuchSessionException {
Objects.requireNonNull(id, "Session ID must be set");

URI value = makeRequest(new HttpRequest(GET, "/se/grid/session/" + id + "/uri"), URI.class);
if (value == null) {
throw new NoSuchSessionException("Unable to find session with ID: " + id);
}
return value;
}

@Override
public void remove(SessionId id) {
Objects.requireNonNull(id, "Session ID must be set");
Expand Down
Loading

0 comments on commit bf926fc

Please sign in to comment.