Skip to content

Commit

Permalink
[grid] Lets avoid to create more than one "Regularly" when the Node r…
Browse files Browse the repository at this point in the history
…egisters again
  • Loading branch information
diemol committed Mar 8, 2021
1 parent 7004f1d commit 134cfec
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,21 @@

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

import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
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.NODE_ROLE;
import static org.openqa.selenium.grid.data.Availability.DOWN;
import static org.openqa.selenium.remote.http.Route.get;

import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableSet;
import com.google.common.net.MediaType;

import net.jodah.failsafe.Failsafe;
import net.jodah.failsafe.RetryPolicy;

import org.openqa.selenium.BuildInfo;
import org.openqa.selenium.cli.CliCommand;
import org.openqa.selenium.events.EventBus;
Expand Down Expand Up @@ -57,14 +67,6 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger;

import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
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.NODE_ROLE;
import static org.openqa.selenium.grid.data.Availability.DOWN;
import static org.openqa.selenium.remote.http.Route.get;

@AutoService(CliCommand.class)
public class NodeServer extends TemplateGridServerCommand {

Expand Down Expand Up @@ -197,7 +199,7 @@ public NettyServer start() {
Executors.newSingleThreadExecutor().submit(() -> {
Failsafe.with(registrationPolicy).run(
() -> {
LOG.info("Sending registration event");
LOG.info("Sending registration event...");
HealthCheck.Result check = node.getHealthCheck().check();
if (DOWN.equals(check.getAvailability())) {
LOG.severe("Node is not alive: " + check.getMessage());
Expand Down
34 changes: 20 additions & 14 deletions java/server/src/org/openqa/selenium/grid/node/local/LocalNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,25 @@

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

import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static org.openqa.selenium.grid.data.Availability.DRAINING;
import static org.openqa.selenium.grid.data.Availability.UP;
import static org.openqa.selenium.grid.node.CapabilityResponseEncoder.getEncoder;
import static org.openqa.selenium.remote.HttpSessionId.getSessionId;
import static org.openqa.selenium.remote.RemoteTags.CAPABILITIES;
import static org.openqa.selenium.remote.RemoteTags.SESSION_ID;
import static org.openqa.selenium.remote.http.Contents.asJson;
import static org.openqa.selenium.remote.http.Contents.string;
import static org.openqa.selenium.remote.http.HttpMethod.DELETE;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Ticker;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.RemovalListener;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.NoSuchSessionException;
Expand Down Expand Up @@ -84,21 +96,11 @@
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static org.openqa.selenium.grid.data.Availability.DRAINING;
import static org.openqa.selenium.grid.data.Availability.UP;
import static org.openqa.selenium.grid.node.CapabilityResponseEncoder.getEncoder;
import static org.openqa.selenium.remote.HttpSessionId.getSessionId;
import static org.openqa.selenium.remote.RemoteTags.CAPABILITIES;
import static org.openqa.selenium.remote.RemoteTags.SESSION_ID;
import static org.openqa.selenium.remote.http.Contents.asJson;
import static org.openqa.selenium.remote.http.Contents.string;
import static org.openqa.selenium.remote.http.HttpMethod.DELETE;

@ManagedService(objectName = "org.seleniumhq.grid:type=Node,name=LocalNode",
description = "Node running the webdriver sessions.")
public class LocalNode extends Node {
Expand All @@ -115,7 +117,8 @@ public class LocalNode extends Node {
private final Cache<SessionId, SessionSlot> currentSessions;
private final Cache<SessionId, TemporaryFilesystem> tempFileSystems;
private final Regularly regularly;
private AtomicInteger pendingSessions = new AtomicInteger();
private final AtomicInteger pendingSessions = new AtomicInteger();
private final AtomicBoolean heartBeatStarted = new AtomicBoolean(false);

private LocalNode(
Tracer tracer,
Expand Down Expand Up @@ -175,8 +178,11 @@ private LocalNode(

bus.addListener(NodeAddedEvent.listener(nodeId -> {
if (getId().equals(nodeId)) {
regularly.submit(() ->
bus.fire(new NodeHeartBeatEvent(getId())), heartbeatPeriod, heartbeatPeriod);
// Lets avoid to create more than one "Regularly" when the Node registers again.
if (!heartBeatStarted.getAndSet(true)) {
regularly.submit(
() -> bus.fire(new NodeHeartBeatEvent(getId())), heartbeatPeriod, heartbeatPeriod);
}
}
}));

Expand Down

0 comments on commit 134cfec

Please sign in to comment.