Skip to content

Commit 8500500

Browse files
committed
[grid] After registration, do not send any more registration events
1 parent 7ba4c62 commit 8500500

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

java/server/src/org/openqa/selenium/grid/node/config/NodeOptions.java

+13
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.lang.reflect.Modifier;
3939
import java.net.URI;
4040
import java.net.URISyntaxException;
41+
import java.time.Duration;
4142
import java.util.ArrayList;
4243
import java.util.Collection;
4344
import java.util.Comparator;
@@ -79,6 +80,18 @@ public Node getNode() {
7980
return config.getClass(NODE_SECTION, "implementation", Node.class, DEFAULT_IMPL);
8081
}
8182

83+
public Duration getRegisterCycle() {
84+
// If the user sets 0 or less, we default to 1s.
85+
int seconds = Math.max(config.getInt(NODE_SECTION, "register-cycle").orElse(10), 1);
86+
return Duration.ofSeconds(seconds);
87+
}
88+
89+
public Duration getRegisterPeriod() {
90+
// If the user sets 0 or less, we default to 1s.
91+
int seconds = Math.max(config.getInt(NODE_SECTION, "register-period").orElse(120), 1);
92+
return Duration.ofSeconds(seconds);
93+
}
94+
8295
public Map<Capabilities, Collection<SessionFactory>> getSessionFactories(
8396
/* Danger! Java stereotype ahead! */
8497
Function<Capabilities, Collection<SessionFactory>> factoryFactory) {

java/server/src/org/openqa/selenium/grid/node/httpd/NodeFlags.java

+18-5
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,26 @@ public class NodeFlags implements HasRoles {
9494
name = "driver-configuration",
9595
prefixed = true,
9696
example = "\n" +
97-
"name = \"Firefox Nightly\"\n" +
98-
"max-sessions = 2\n" +
99-
"stereotype = \"{\"browserName\": \"firefox\", \"browserVersion\": \"86\", " +
100-
"\"moz:firefoxOptions\": " +
101-
"{\"binary\":\"/Applications/Firefox Nightly.app/Contents/MacOS/firefox-bin\"}}\"")
97+
"name = \"Firefox Nightly\"\n" +
98+
"max-sessions = 2\n" +
99+
"stereotype = \"{\"browserName\": \"firefox\", \"browserVersion\": \"86\", " +
100+
"\"moz:firefoxOptions\": " +
101+
"{\"binary\":\"/Applications/Firefox Nightly.app/Contents/MacOS/firefox-bin\"}}\"")
102102
public List<String> driverConfiguration;
103103

104+
@Parameter(
105+
names = "--register-cycle",
106+
description = "How often, in seconds, the Node will try to register itself for the first time to the Distributor.")
107+
@ConfigValue(section = "node", name = "register-cycle", example = "10")
108+
public int registerCycle;
109+
110+
@Parameter(
111+
names = "--register-period",
112+
description = "How long, in seconds, will the Node try to register to the Distributor for the first time. " +
113+
"After this period is completed, the Node will not attempt to register again.")
114+
@ConfigValue(section = "node", name = "register-cycle", example = "120")
115+
public int registerPeriod;
116+
104117
@Override
105118
public Set<Role> getRoles() {
106119
return Collections.singleton(NODE_ROLE);

java/server/src/org/openqa/selenium/grid/node/httpd/NodeServer.java

+13-6
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,10 @@
5151
import org.openqa.selenium.remote.http.Route;
5252
import org.openqa.selenium.remote.tracing.Tracer;
5353

54-
import java.time.Duration;
55-
import java.time.temporal.ChronoUnit;
5654
import java.util.Collections;
5755
import java.util.Set;
5856
import java.util.concurrent.Executors;
57+
import java.util.concurrent.atomic.AtomicBoolean;
5958
import java.util.logging.Logger;
6059

6160
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
@@ -70,6 +69,7 @@
7069
public class NodeServer extends TemplateGridServerCommand {
7170

7271
private static final Logger LOG = Logger.getLogger(NodeServer.class.getName());
72+
private final AtomicBoolean nodeRegistered = new AtomicBoolean(false);
7373
private Node node;
7474
private EventBus bus;
7575

@@ -135,6 +135,7 @@ protected Handlers createHandlers(Config config) {
135135

136136
bus.addListener(NodeAddedEvent.listener(nodeId -> {
137137
if (node.getId().equals(nodeId)) {
138+
nodeRegistered.set(true);
138139
LOG.info("Node has been added");
139140
}
140141
}));
@@ -172,6 +173,7 @@ public Server<?> asServer(Config initialConfig) {
172173
Require.nonNull("Config", initialConfig);
173174

174175
Config config = new MemoizedConfig(new CompoundConfig(initialConfig, getDefaultConfig()));
176+
NodeOptions nodeOptions = new NodeOptions(config);
175177

176178
Handlers handler = createHandlers(config);
177179

@@ -183,11 +185,13 @@ public Server<?> asServer(Config initialConfig) {
183185
public NettyServer start() {
184186
super.start();
185187

186-
// Unlimited attempts, initial 5 seconds interval, backoff rate of 1.0005, max interval of 5 minutes
187-
RetryPolicy<Object> registrationPolicy = new RetryPolicy<>()
188+
// Unlimited attempts, every X seconds, we assume a Node should not need more than Y minutes to register
189+
// X defaults to 10s and Y to 120 seconds, but the user can overwrite that.
190+
RetryPolicy<Object> registrationPolicy = new RetryPolicy<>()
188191
.withMaxAttempts(-1)
189-
.handleResultIf(result -> true)
190-
.withBackoff(Duration.ofSeconds(5).getSeconds(), Duration.ofMinutes(5).getSeconds(), ChronoUnit.SECONDS, 1.0005);
192+
.withMaxDuration(nodeOptions.getRegisterPeriod())
193+
.withDelay(nodeOptions.getRegisterCycle())
194+
.handleResultIf(result -> true);
191195

192196
LOG.info("Starting registration process for node id " + node.getId());
193197
Executors.newSingleThreadExecutor().submit(() -> {
@@ -201,6 +205,9 @@ public NettyServer start() {
201205
throw new UnsupportedOperationException("Node cannot be registered");
202206
}
203207
bus.fire(new NodeStatusEvent(node.getStatus()));
208+
if (nodeRegistered.get()) {
209+
throw new InterruptedException("Stopping registration thread.");
210+
}
204211
}
205212
);
206213
});

0 commit comments

Comments
 (0)