Skip to content

Commit

Permalink
[grid] Code formatting and moving default value to options class
Browse files Browse the repository at this point in the history
  • Loading branch information
diemol committed Dec 25, 2020
1 parent 546e66b commit cf298bc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,16 @@ protected Distributor(
Json json = new Json();
routes = Route.combine(
post("/se/grid/distributor/node")
.to(() -> new AddNode(tracer, this, json, httpClientFactory, registrationSecret))
.to(() ->
new AddNode(tracer, this, json, httpClientFactory, registrationSecret))
.with(requiresSecret),
post("/se/grid/distributor/node/{nodeId}/drain")
.to((Map<String, String> params) -> new DrainNode(this, new NodeId(UUID.fromString(params.get("nodeId")))))
.to((Map<String, String> params) ->
new DrainNode(this, new NodeId(UUID.fromString(params.get("nodeId")))))
.with(requiresSecret),
delete("/se/grid/distributor/node/{nodeId}")
.to(params -> new RemoveNode(this, new NodeId(UUID.fromString(params.get("nodeId")))))
.to(params ->
new RemoveNode(this, new NodeId(UUID.fromString(params.get("nodeId")))))
.with(requiresSecret),
get("/se/grid/distributor/status")
.to(() -> new GetDistributorStatus(this))
Expand All @@ -173,7 +176,8 @@ public Either<SessionNotCreatedException, CreateSessionResponse> newSession(Http
span.addEvent("Session request received by the distributor", attributeMap);

if (!iterator.hasNext()) {
SessionNotCreatedException exception = new SessionNotCreatedException("No capabilities found");
SessionNotCreatedException exception =
new SessionNotCreatedException("No capabilities found in session request payload");
EXCEPTION.accept(attributeMap, exception);
attributeMap.put(AttributeKey.EXCEPTION_MESSAGE.getKey(),
EventAttribute.setValue("Unable to create session. No capabilities found: " +
Expand All @@ -200,7 +204,8 @@ public Either<SessionNotCreatedException, CreateSessionResponse> newSession(Http
if (!hostsWithCaps) {
String errorMessage = String.format(
"No Node supports the required capabilities: %s",
payload.stream().map(Capabilities::toString).collect(Collectors.joining(", ")));
payload.stream().map(Capabilities::toString)
.collect(Collectors.joining(", ")));
SessionNotCreatedException exception = new SessionNotCreatedException(errorMessage);
span.setAttribute(AttributeKey.ERROR.getKey(), true);
span.setStatus(Status.ABORTED);
Expand Down Expand Up @@ -244,7 +249,8 @@ public Either<SessionNotCreatedException, CreateSessionResponse> newSession(Http
String errorMessage =
String.format(
"Unable to find provider for session: %s",
payload.stream().map(Capabilities::toString).collect(Collectors.joining(", ")));
payload.stream().map(Capabilities::toString)
.collect(Collectors.joining(", ")));
SessionNotCreatedException exception = new RetrySessionRequestException(errorMessage);
return Either.left(exception);
}
Expand All @@ -264,7 +270,8 @@ public Either<SessionNotCreatedException, CreateSessionResponse> newSession(Http

EXCEPTION.accept(attributeMap, e);
attributeMap.put(AttributeKey.EXCEPTION_MESSAGE.getKey(),
EventAttribute.setValue("Unknown error in LocalDistributor while creating session: " + e.getMessage()));
EventAttribute.setValue("Unknown error in LocalDistributor while creating session: " +
e.getMessage()));
span.addEvent(AttributeKey.EXCEPTION_EVENT.getKey(), attributeMap);

return Either.left(new SessionNotCreatedException(e.getMessage(), e));
Expand All @@ -283,7 +290,8 @@ public Either<SessionNotCreatedException, CreateSessionResponse> newSession(Http

protected abstract Set<NodeStatus> getAvailableNodes();

protected abstract Supplier<CreateSessionResponse> reserve(SlotId slot, CreateSessionRequest request);
protected abstract Supplier<CreateSessionResponse> reserve(SlotId slot,
CreateSessionRequest request);

@Override
public boolean test(HttpRequest httpRequest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Optional;
import java.util.logging.Logger;

public class DistributorOptions {

private static final String DISTRIBUTOR_SECTION = "distributor";
private static final Logger LOG = Logger.getLogger(DistributorOptions.class.getName());
private static final String DEFAULT_DISTRIBUTOR_IMPLEMENTATION =
"org.openqa.selenium.grid.distributor.local.LocalDistributor";

private final Config config;

Expand Down Expand Up @@ -74,7 +74,11 @@ public URI getDistributorUri() {
}
}

public Distributor getDistributor(String defaultClass) {
return config.getClass(DISTRIBUTOR_SECTION, "implementation", Distributor.class, defaultClass);
public Distributor getDistributor() {
return config.getClass(
DISTRIBUTOR_SECTION,
"implementation",
Distributor.class,
DEFAULT_DISTRIBUTOR_IMPLEMENTATION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
public class DistributorServer extends TemplateGridServerCommand {

private static final Logger LOG = Logger.getLogger(DistributorServer.class.getName());
private static final String LOCAL_DISTRIBUTOR_SERVER = "org.openqa.selenium.grid.distributor.local.LocalDistributor";

@Override
public String getName() {
Expand Down Expand Up @@ -88,7 +87,7 @@ protected Config getDefaultConfig() {
protected Handlers createHandlers(Config config) {
DistributorOptions distributorOptions = new DistributorOptions(config);

Distributor distributor = distributorOptions.getDistributor(LOCAL_DISTRIBUTOR_SERVER);
Distributor distributor = distributorOptions.getDistributor();

HttpHandler readinessCheck = req -> {
boolean ready = distributor.isReady();
Expand Down

0 comments on commit cf298bc

Please sign in to comment.