Skip to content

Commit

Permalink
JDBC session map (#8378)
Browse files Browse the repository at this point in the history
Provides an implementation of the `SessionMap` that's
backed by JDBC.
  • Loading branch information
raju249 authored Jun 5, 2020
1 parent 482963e commit f5fc6cd
Show file tree
Hide file tree
Showing 12 changed files with 545 additions and 2 deletions.
1 change: 1 addition & 0 deletions java/maven_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def selenium_java_deps():
"org.eclipse.jetty:jetty-xml:%s" % jetty_version,
"org.eclipse.mylyn.github:org.eclipse.egit.github.core:2.1.5",
"org.hamcrest:hamcrest:2.2",
"org.hsqldb:hsqldb:2.5.0",
"org.mockito:mockito-core:3.3.3",
"org.slf4j:slf4j-jdk14:1.7.30",
"org.testng:testng:7.1.0",
Expand Down
38 changes: 37 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": 189058187,
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": 1361415682,
"conflict_resolution": {},
"dependencies": [
{
Expand Down Expand Up @@ -4215,6 +4215,42 @@
"sha256": "f49e697dbc70591f91a90dd7f741f5780f53f63f34a416d6a9879499d4d666af",
"url": "https://repo1.maven.org/maven2/org/hamcrest/hamcrest/2.2/hamcrest-2.2-sources.jar"
},
{
"coord": "org.hsqldb:hsqldb:2.5.0",
"dependencies": [],
"directDependencies": [],
"exclusions": [
"org.hamcrest:hamcrest-all",
"org.hamcrest:hamcrest-core",
"io.netty:netty-all"
],
"file": "v1/https/repo1.maven.org/maven2/org/hsqldb/hsqldb/2.5.0/hsqldb-2.5.0.jar",
"mirror_urls": [
"https://repo1.maven.org/maven2/org/hsqldb/hsqldb/2.5.0/hsqldb-2.5.0.jar",
"https://jcenter.bintray.com/org/hsqldb/hsqldb/2.5.0/hsqldb-2.5.0.jar",
"https://maven.google.com/org/hsqldb/hsqldb/2.5.0/hsqldb-2.5.0.jar"
],
"sha256": "acda459cc9d6a07b39b284364e93b5f29e11877d687e9544b91778d3554d2b38",
"url": "https://repo1.maven.org/maven2/org/hsqldb/hsqldb/2.5.0/hsqldb-2.5.0.jar"
},
{
"coord": "org.hsqldb:hsqldb:jar:sources:2.5.0",
"dependencies": [],
"directDependencies": [],
"exclusions": [
"org.hamcrest:hamcrest-all",
"org.hamcrest:hamcrest-core",
"io.netty:netty-all"
],
"file": "v1/https/repo1.maven.org/maven2/org/hsqldb/hsqldb/2.5.0/hsqldb-2.5.0-sources.jar",
"mirror_urls": [
"https://repo1.maven.org/maven2/org/hsqldb/hsqldb/2.5.0/hsqldb-2.5.0-sources.jar",
"https://jcenter.bintray.com/org/hsqldb/hsqldb/2.5.0/hsqldb-2.5.0-sources.jar",
"https://maven.google.com/org/hsqldb/hsqldb/2.5.0/hsqldb-2.5.0-sources.jar"
],
"sha256": "a09e15c2ef034480b5b0998d013ca3c98c4ccfd0e1d739acdc73400fd1a21dd3",
"url": "https://repo1.maven.org/maven2/org/hsqldb/hsqldb/2.5.0/hsqldb-2.5.0-sources.jar"
},
{
"coord": "org.jetbrains.kotlin:kotlin-stdlib-common:1.3.70",
"dependencies": [],
Expand Down
1 change: 1 addition & 0 deletions java/server/src/org/openqa/selenium/grid/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ java_export(
":base-command",
"//java/server/src/org/openqa/selenium/cli",
"//java/server/src/org/openqa/selenium/grid/config",
"//java/server/src/org/openqa/selenium/grid/sessionmap/jdbc",
],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ public class ConfigException extends RuntimeException {
public ConfigException(String message, Object... args) {
super(String.format(message, args));
}

public ConfigException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
import static org.openqa.selenium.grid.config.StandardGridRoles.EVENT_BUS_ROLE;
import static org.openqa.selenium.grid.config.StandardGridRoles.HTTPD_ROLE;
import static org.openqa.selenium.grid.config.StandardGridRoles.SESSION_MAP_ROLE;
import static org.openqa.selenium.json.Json.JSON_UTF_8;
import static org.openqa.selenium.remote.http.Contents.asJson;
import static org.openqa.selenium.remote.http.Route.get;
Expand All @@ -61,7 +62,7 @@ public String getDescription() {

@Override
public Set<Role> getConfigurableRoles() {
return ImmutableSet.of(EVENT_BUS_ROLE, HTTPD_ROLE);
return ImmutableSet.of(EVENT_BUS_ROLE, HTTPD_ROLE, SESSION_MAP_ROLE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
load("@rules_jvm_external//:defs.bzl", "artifact")
load("//java:version.bzl", "SE_VERSION")
load("//java:defs.bzl", "java_export")

java_export(
name = "jdbc",
srcs = glob(["*.java"]),
maven_coordinates = "org.seleniumhq.selenium:selenium-session-map-jdbc:%s" % SE_VERSION,
pom_template = "//java/client/src/org/openqa/selenium:template-pom",
visibility = [
"//visibility:public",
],
deps = [
"//java:auto-service",
"//java/client/src/org/openqa/selenium/json",
"//java/server/src/org/openqa/selenium/events",
"//java/client/src/org/openqa/selenium/remote",
"//java/server/src/org/openqa/selenium/grid/server",
"//java/server/src/org/openqa/selenium/grid/sessionmap",
"//java/server/src/org/openqa/selenium/grid/config",
"//java/server/src/org/openqa/selenium/grid/log",
"//java/server/src/org/openqa/selenium/grid/data",
artifact("com.beust:jcommander"),
artifact("com.google.guava:guava"),
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
// 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.jdbc;

import static org.openqa.selenium.grid.data.SessionClosedEvent.SESSION_CLOSED;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.NoSuchSessionException;
import org.openqa.selenium.events.EventBus;
import org.openqa.selenium.grid.config.Config;
import org.openqa.selenium.grid.config.ConfigException;
import org.openqa.selenium.grid.data.Session;
import org.openqa.selenium.grid.log.LoggingOptions;
import org.openqa.selenium.grid.server.EventBusOptions;
import org.openqa.selenium.grid.sessionmap.SessionMap;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.remote.tracing.Tracer;

import java.io.Closeable;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Logger;


public class JdbcBackedSessionMap extends SessionMap implements Closeable {

private static final Json JSON = new Json();
private static final Logger LOG = Logger.getLogger(JdbcBackedSessionMap.class.getName());
private static final String TABLE_NAME = "sessions_map";
private static final String SESSION_ID_COL = "session_ids";
private static final String SESSION_CAPS_COL = "session_caps";
private static final String SESSION_URI_COL = "session_uri";
private final EventBus bus;
private final Connection connection;


public JdbcBackedSessionMap(Tracer tracer, Connection jdbcConnection, EventBus bus) {
super(tracer);

Require.nonNull("JDBC Connection Object", jdbcConnection);
this.bus = Require.nonNull("Event bus", bus);

this.connection = jdbcConnection;
this.bus.addListener(SESSION_CLOSED, event -> {
SessionId id = event.getData(SessionId.class);
remove(id);
});
}

public static SessionMap create(Config config) {
Tracer tracer = new LoggingOptions(config).getTracer();
EventBus bus = new EventBusOptions(config).getEventBus();

JdbcSessionMapOptions sessionMapOptions = new JdbcSessionMapOptions(config);

Connection connection;

try {
connection = sessionMapOptions.getJdbcConnection();
} catch (SQLException e) {
throw new ConfigException(e);
}

return new JdbcBackedSessionMap(tracer, connection, bus);
}
@Override
public boolean add(Session session) {
Require.nonNull("Session to add", session);

try {
return insertSessionStatement(session).executeUpdate() >= 1;

} catch (SQLException e) {
throw new JdbcException(e);
}
}

@Override
public Session get(SessionId id) throws NoSuchSessionException {
Require.nonNull("Session ID", id);

URI uri = null;
Capabilities caps = null;
String rawUri = null;

try (ResultSet sessions = readSessionStatement(id).executeQuery()){
if (!sessions.next()) {
throw new NoSuchSessionException("Unable to find...");
}

rawUri = sessions.getString(SESSION_URI_COL);
String rawCapabilities = sessions.getString(SESSION_CAPS_COL);

caps = rawCapabilities == null ?
new ImmutableCapabilities() :
JSON.toType(rawCapabilities, Capabilities.class);
try {
uri = new URI(rawUri);
} catch (URISyntaxException e) {
throw new NoSuchSessionException(String.format("Unable to convert session id (%s) to uri: %s", id, rawUri), e);
}

return new Session(id, uri, caps);
} catch (SQLException e) {
throw new JdbcException(e);
}
}

@Override
public void remove(SessionId id) {
Require.nonNull("Session ID", id);

try {
getDeleteSqlForSession(id).executeUpdate();
} catch (SQLException e) {
throw new JdbcException(e.getMessage());
}
}

@Override
public void close() {
try {
connection.close();
} catch (SQLException e) {
LOG.warning("SQL exception while closing JDBC Connection:" + e.getMessage());
}
}

private PreparedStatement insertSessionStatement(Session session) throws SQLException {
PreparedStatement insertStatement = connection.prepareStatement(String.format("insert into %1$s (%2$s, %3$s, %4$s) values (?, ?, ?)",
TABLE_NAME,
SESSION_ID_COL,
SESSION_URI_COL,
SESSION_CAPS_COL));

insertStatement.setString(1, session.getId().toString());
insertStatement.setString(2, session.getUri().toString());
insertStatement.setString(3, JSON.toJson(session.getCapabilities()));

return insertStatement;
}

private PreparedStatement readSessionStatement(SessionId sessionId) throws SQLException {
PreparedStatement getSessionsStatement = connection.prepareStatement(String.format("select * from %1$s where %2$s = ?",
TABLE_NAME,
SESSION_ID_COL));

getSessionsStatement.setMaxRows(1);
getSessionsStatement.setString(1, sessionId.toString());

return getSessionsStatement;
}

private PreparedStatement getDeleteSqlForSession(SessionId sessionId) throws SQLException{
PreparedStatement deleteSessionStatement = connection.prepareStatement(String.format("delete from %1$s where %2$s = ?",
TABLE_NAME,
SESSION_ID_COL));

deleteSessionStatement.setString(1, sessionId.toString());

return deleteSessionStatement;

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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.jdbc;


import org.openqa.selenium.WebDriverException;

public class JdbcException extends WebDriverException {
public JdbcException() {
super();
}

public JdbcException(String message) {
super(message);
}

public JdbcException(Throwable cause) {
super(cause);
}

public JdbcException(String message, Throwable cause) {
super(message, cause);
}
}
Loading

0 comments on commit f5fc6cd

Please sign in to comment.