Skip to content

Commit

Permalink
[grid]: Integrate protocol conversion into the new grid
Browse files Browse the repository at this point in the history
We expose more of the internals this way, but it'll be
for the best in the end.
  • Loading branch information
shs96c committed Mar 19, 2019
1 parent 02df06e commit afc680f
Show file tree
Hide file tree
Showing 32 changed files with 749 additions and 530 deletions.
1 change: 1 addition & 0 deletions java/server/src/org/openqa/selenium/grid/docker/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ java_library(
"//java/server/src/org/openqa/selenium/docker:docker",
"//java/server/src/org/openqa/selenium/grid/config:config",
"//java/server/src/org/openqa/selenium/grid/data:data",
"//java/server/src/org/openqa/selenium/grid/node:node",
"//java/server/src/org/openqa/selenium/grid/node/local:local",
"//java/server/src/org/openqa/selenium/grid/web:web",
"//third_party/java/beust:jcommander",
Expand Down
37 changes: 12 additions & 25 deletions java/server/src/org/openqa/selenium/grid/docker/DockerSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,36 @@

package org.openqa.selenium.grid.docker;

import static org.openqa.selenium.remote.http.HttpMethod.DELETE;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.docker.Container;
import org.openqa.selenium.grid.data.Session;
import org.openqa.selenium.grid.web.CommandHandler;
import org.openqa.selenium.grid.web.ReverseProxyHandler;
import org.openqa.selenium.grid.node.ProtocolConvertingSession;
import org.openqa.selenium.remote.Dialect;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;

import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.time.Duration;
import java.util.Objects;

class DockerSession extends Session implements CommandHandler {
class DockerSession extends ProtocolConvertingSession {

private final Container container;
private final CommandHandler handler;
private final String killUrl;

DockerSession(
Container container,
HttpClient client,
SessionId id,
URI uri,
URL url,
Capabilities capabilities,
HttpClient client) {
super(id, uri, capabilities);
Dialect downstream,
Dialect upstream) {
super(client, id, url, downstream, upstream, capabilities);
this.container = Objects.requireNonNull(container);

this.handler = new ReverseProxyHandler(Objects.requireNonNull(client));
this.killUrl = "/session/" + id;
}

@Override
public void execute(HttpRequest req, HttpResponse resp) throws IOException {
handler.execute(req, resp);

if (req.getMethod() == DELETE && req.getUri().equals(killUrl)) {
container.stop(Duration.ofMinutes(1));
container.delete();
}
public void stop() {
container.stop(Duration.ofMinutes(1));
container.delete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,27 @@
package org.openqa.selenium.grid.docker;

import static org.openqa.selenium.docker.ContainerInfo.image;
import static org.openqa.selenium.remote.Dialect.W3C;
import static org.openqa.selenium.remote.http.HttpMethod.GET;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.docker.Container;
import org.openqa.selenium.docker.Docker;
import org.openqa.selenium.docker.Image;
import org.openqa.selenium.docker.Port;
import org.openqa.selenium.grid.data.Session;
import org.openqa.selenium.grid.data.CreateSessionRequest;
import org.openqa.selenium.grid.node.ActiveSession;
import org.openqa.selenium.grid.node.SessionFactory;
import org.openqa.selenium.net.PortProber;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.Command;
import org.openqa.selenium.remote.Dialect;
import org.openqa.selenium.remote.DriverCommand;
import org.openqa.selenium.remote.ProtocolHandshake;
import org.openqa.selenium.remote.Response;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
Expand All @@ -43,11 +52,13 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.time.Duration;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DockerSessionFactory implements Function<Capabilities, Session> {
public class DockerSessionFactory implements SessionFactory {

public static final Logger LOG = Logger.getLogger(DockerSessionFactory.class.getName());
private final HttpClient.Factory clientFactory;
Expand All @@ -61,8 +72,13 @@ public DockerSessionFactory(HttpClient.Factory clientFactory, Docker docker, Ima
}

@Override
public Session apply(Capabilities capabilities) {
LOG.info("Starting session for " + capabilities);
public boolean test(Capabilities capabilities) {
return false;
}

@Override
public Optional<ActiveSession> apply(CreateSessionRequest sessionRequest) {
LOG.info("Starting session for " + sessionRequest.getCapabilities());
int port = PortProber.findFreePort();
URL remoteAddress = getUrl(port);
URI remoteUri = null;
Expand All @@ -83,23 +99,47 @@ public Session apply(Capabilities capabilities) {
} catch (TimeoutException e) {
container.stop(Duration.ofMinutes(1));
container.delete();
throw new SessionNotCreatedException(String.format(
LOG.warning(String.format(
"Unable to connect to docker server (container id: %s)", container.getId()));
return Optional.empty();
}
LOG.info(String.format("Server is ready (container id: %s)", container.getId()));

RemoteWebDriver driver = new RemoteWebDriver(remoteAddress, capabilities);
Command command = new Command(
null,
DriverCommand.NEW_SESSION(sessionRequest.getCapabilities()));
ProtocolHandshake.Result result;
Response response;
try {
result = new ProtocolHandshake().createSession(client, command);
response = result.createResponse();
} catch (IOException | RuntimeException e) {
container.stop(Duration.ofMinutes(1));
container.delete();
LOG.log(Level.WARNING, "Unable to create session: " + e.getMessage(), e);
return Optional.empty();
}

SessionId id = new SessionId(response.getSessionId());
Capabilities capabilities = new ImmutableCapabilities((Map<?, ?>) response.getValue());

Dialect downstream = sessionRequest.getDownstreamDialects().contains(result.getDialect()) ?
result.getDialect() :
W3C;

LOG.info(String.format(
"Created session: %s - %s (container id: %s)",
driver.getSessionId(),
driver.getCapabilities(),
id,
capabilities,
container.getId()));
return new DockerSession(
return Optional.of(new DockerSession(
container,
driver.getSessionId(),
remoteUri,
driver.getCapabilities(),
client);
client,
id,
remoteAddress,
capabilities,
downstream,
result.getDialect()));
}

private void waitForServerToStart(HttpClient client, Duration duration) {
Expand Down
40 changes: 40 additions & 0 deletions java/server/src/org/openqa/selenium/grid/node/ActiveSession.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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.node;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.grid.web.CommandHandler;
import org.openqa.selenium.remote.Dialect;
import org.openqa.selenium.remote.SessionId;

import java.net.URI;

public interface ActiveSession extends CommandHandler {

SessionId getId();

Capabilities getCapabilities();

URI getUri();

Dialect getUpstreamDialect();

Dialect getDownstreamDialect();

void stop();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// 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.node;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.grid.data.Session;
import org.openqa.selenium.remote.Dialect;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.remote.http.HttpClient;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Objects;

public abstract class BaseActiveSession implements ActiveSession {

private final Session session;
private final Dialect downstream;
private final Dialect upstream;

protected BaseActiveSession(
SessionId id,
URL url,
Dialect downstream,
Dialect upstream,
Capabilities capabilities) {
URI uri = null;
try {
uri = Objects.requireNonNull(url).toURI();
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}

this.session = new Session(
Objects.requireNonNull(id),
uri,
ImmutableCapabilities.copyOf(Objects.requireNonNull(capabilities)));

this.downstream = Objects.requireNonNull(downstream);
this.upstream = Objects.requireNonNull(upstream);
}

@Override
public SessionId getId() {
return session.getId();
}

@Override
public Capabilities getCapabilities() {
return session.getCapabilities();
}

@Override
public URI getUri() {
return session.getUri();
}

@Override
public Dialect getUpstreamDialect() {
return upstream;
}

@Override
public Dialect getDownstreamDialect() {
return downstream;
}

public Session asSession() {
return session;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,54 @@
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.grid.node.local;
package org.openqa.selenium.grid.node;

import static org.openqa.selenium.remote.http.HttpMethod.DELETE;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.grid.data.Session;
import org.openqa.selenium.grid.web.CommandHandler;
import org.openqa.selenium.grid.web.Values;
import org.openqa.selenium.grid.web.ProtocolConverter;
import org.openqa.selenium.grid.web.ReverseProxyHandler;
import org.openqa.selenium.remote.Dialect;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URL;
import java.util.Objects;

class TrackedSession extends Session {
public abstract class ProtocolConvertingSession extends BaseActiveSession {

private final SessionFactory factory;
private final CommandHandler handler;
private final String killUrl;

TrackedSession(SessionFactory createdBy, Session session, CommandHandler handler) {
super(session.getId(), session.getUri(), session.getCapabilities());
this.factory = Objects.requireNonNull(createdBy);
this.handler = Objects.requireNonNull(handler);
}
protected ProtocolConvertingSession(
HttpClient client,
SessionId id,
URL url,
Dialect downstream,
Dialect upstream,
Capabilities capabilities) {
super(id, url, downstream, upstream, capabilities);

public CommandHandler getHandler() {
return handler;
}
Objects.requireNonNull(client);

public Capabilities getStereotype() {
return factory.getStereotype();
}
if (downstream.equals(upstream)) {
this.handler = new ReverseProxyHandler(client);
} else {
this.handler = new ProtocolConverter(client, downstream, upstream);
}

public void stop() {
HttpResponse resp = new HttpResponse();
try {
handler.execute(new HttpRequest(DELETE, "/session/" + getId()), resp);
this.killUrl = "/session/" + id;
}

Values.get(resp, Void.class);
} catch (IOException e) {
throw new UncheckedIOException(e);
@Override
public void execute(HttpRequest req, HttpResponse resp) throws IOException {
handler.execute(req, resp);
if (req.getMethod() == DELETE && killUrl.equals(req.getUri())) {
stop();
}
}
}

0 comments on commit afc680f

Please sign in to comment.