Skip to content

Commit e0d9309

Browse files
committed
[grid] Improving /readyz return http code.
Also, the Router was not doing a proper check. Fixes #10391
1 parent 1daa6d6 commit e0d9309

5 files changed

Lines changed: 55 additions & 44 deletions

File tree

java/src/org/openqa/selenium/grid/commands/Hub.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,6 @@
1717

1818
package org.openqa.selenium.grid.commands;
1919

20-
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
21-
import static java.net.HttpURLConnection.HTTP_OK;
22-
import static org.openqa.selenium.grid.config.StandardGridRoles.DISTRIBUTOR_ROLE;
23-
import static org.openqa.selenium.grid.config.StandardGridRoles.EVENT_BUS_ROLE;
24-
import static org.openqa.selenium.grid.config.StandardGridRoles.HTTPD_ROLE;
25-
import static org.openqa.selenium.grid.config.StandardGridRoles.ROUTER_ROLE;
26-
import static org.openqa.selenium.grid.config.StandardGridRoles.SESSION_QUEUE_ROLE;
27-
import static org.openqa.selenium.remote.http.Route.combine;
28-
2920
import com.google.auto.service.AutoService;
3021
import com.google.common.collect.ImmutableSet;
3122

@@ -73,6 +64,15 @@
7364
import java.util.Set;
7465
import java.util.logging.Logger;
7566

67+
import static java.net.HttpURLConnection.HTTP_OK;
68+
import static java.net.HttpURLConnection.HTTP_UNAVAILABLE;
69+
import static org.openqa.selenium.grid.config.StandardGridRoles.DISTRIBUTOR_ROLE;
70+
import static org.openqa.selenium.grid.config.StandardGridRoles.EVENT_BUS_ROLE;
71+
import static org.openqa.selenium.grid.config.StandardGridRoles.HTTPD_ROLE;
72+
import static org.openqa.selenium.grid.config.StandardGridRoles.ROUTER_ROLE;
73+
import static org.openqa.selenium.grid.config.StandardGridRoles.SESSION_QUEUE_ROLE;
74+
import static org.openqa.selenium.remote.http.Route.combine;
75+
7676
@AutoService(CliCommand.class)
7777
public class Hub extends TemplateGridServerCommand {
7878

@@ -174,10 +174,11 @@ protected Handlers createHandlers(Config config) {
174174
queue,
175175
serverOptions.getExternalUri(),
176176
getServerVersion());
177+
177178
HttpHandler readinessCheck = req -> {
178179
boolean ready = router.isReady() && bus.isReady();
179180
return new HttpResponse()
180-
.setStatus(ready ? HTTP_OK : HTTP_INTERNAL_ERROR)
181+
.setStatus(ready ? HTTP_OK : HTTP_UNAVAILABLE)
181182
.setContent(Contents.utf8String("Router is " + ready));
182183
};
183184

java/src/org/openqa/selenium/grid/commands/Standalone.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,6 @@
1717

1818
package org.openqa.selenium.grid.commands;
1919

20-
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
21-
import static java.net.HttpURLConnection.HTTP_OK;
22-
import static org.openqa.selenium.grid.config.StandardGridRoles.DISTRIBUTOR_ROLE;
23-
import static org.openqa.selenium.grid.config.StandardGridRoles.HTTPD_ROLE;
24-
import static org.openqa.selenium.grid.config.StandardGridRoles.NODE_ROLE;
25-
import static org.openqa.selenium.grid.config.StandardGridRoles.ROUTER_ROLE;
26-
import static org.openqa.selenium.grid.config.StandardGridRoles.SESSION_QUEUE_ROLE;
27-
import static org.openqa.selenium.remote.http.Route.combine;
28-
2920
import com.google.auto.service.AutoService;
3021
import com.google.common.collect.ImmutableSet;
3122

@@ -76,6 +67,15 @@
7667
import java.util.Set;
7768
import java.util.logging.Logger;
7869

70+
import static java.net.HttpURLConnection.HTTP_OK;
71+
import static java.net.HttpURLConnection.HTTP_UNAVAILABLE;
72+
import static org.openqa.selenium.grid.config.StandardGridRoles.DISTRIBUTOR_ROLE;
73+
import static org.openqa.selenium.grid.config.StandardGridRoles.HTTPD_ROLE;
74+
import static org.openqa.selenium.grid.config.StandardGridRoles.NODE_ROLE;
75+
import static org.openqa.selenium.grid.config.StandardGridRoles.ROUTER_ROLE;
76+
import static org.openqa.selenium.grid.config.StandardGridRoles.SESSION_QUEUE_ROLE;
77+
import static org.openqa.selenium.remote.http.Route.combine;
78+
7979
@AutoService(CliCommand.class)
8080
public class Standalone extends TemplateGridServerCommand {
8181

@@ -171,7 +171,7 @@ protected Handlers createHandlers(Config config) {
171171
HttpHandler readinessCheck = req -> {
172172
boolean ready = sessions.isReady() && distributor.isReady() && bus.isReady();
173173
return new HttpResponse()
174-
.setStatus(ready ? HTTP_OK : HTTP_INTERNAL_ERROR)
174+
.setStatus(ready ? HTTP_OK : HTTP_UNAVAILABLE)
175175
.setContent(Contents.utf8String("Standalone is " + ready));
176176
};
177177

java/src/org/openqa/selenium/grid/distributor/httpd/DistributorServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
import java.util.Set;
4141
import java.util.logging.Logger;
4242

43-
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
4443
import static java.net.HttpURLConnection.HTTP_OK;
44+
import static java.net.HttpURLConnection.HTTP_UNAVAILABLE;
4545
import static org.openqa.selenium.grid.config.StandardGridRoles.DISTRIBUTOR_ROLE;
4646
import static org.openqa.selenium.grid.config.StandardGridRoles.EVENT_BUS_ROLE;
4747
import static org.openqa.selenium.grid.config.StandardGridRoles.HTTPD_ROLE;
@@ -94,7 +94,7 @@ protected Handlers createHandlers(Config config) {
9494
HttpHandler readinessCheck = req -> {
9595
boolean ready = distributor.isReady();
9696
return new HttpResponse()
97-
.setStatus(ready ? HTTP_OK : HTTP_INTERNAL_ERROR)
97+
.setStatus(ready ? HTTP_OK : HTTP_UNAVAILABLE)
9898
.setHeader("Content-Type", MediaType.PLAIN_TEXT_UTF_8.toString())
9999
.setContent(Contents.utf8String("Distributor is " + ready));
100100
};

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@
1717

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

20-
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
21-
import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
22-
import static org.openqa.selenium.grid.config.StandardGridRoles.EVENT_BUS_ROLE;
23-
import static org.openqa.selenium.grid.config.StandardGridRoles.HTTPD_ROLE;
24-
import static org.openqa.selenium.grid.config.StandardGridRoles.NODE_ROLE;
25-
import static org.openqa.selenium.grid.data.Availability.DOWN;
26-
import static org.openqa.selenium.remote.http.Route.get;
27-
2820
import com.google.auto.service.AutoService;
2921
import com.google.common.collect.ImmutableSet;
3022
import com.google.common.net.MediaType;
@@ -68,6 +60,14 @@
6860
import java.util.concurrent.atomic.AtomicBoolean;
6961
import java.util.logging.Logger;
7062

63+
import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
64+
import static java.net.HttpURLConnection.HTTP_UNAVAILABLE;
65+
import static org.openqa.selenium.grid.config.StandardGridRoles.EVENT_BUS_ROLE;
66+
import static org.openqa.selenium.grid.config.StandardGridRoles.HTTPD_ROLE;
67+
import static org.openqa.selenium.grid.config.StandardGridRoles.NODE_ROLE;
68+
import static org.openqa.selenium.grid.data.Availability.DOWN;
69+
import static org.openqa.selenium.remote.http.Route.get;
70+
7171
@AutoService(CliCommand.class)
7272
public class NodeServer extends TemplateGridServerCommand {
7373

@@ -133,7 +133,7 @@ protected Handlers createHandlers(Config config) {
133133
}
134134

135135
return new HttpResponse()
136-
.setStatus(HTTP_INTERNAL_ERROR)
136+
.setStatus(HTTP_UNAVAILABLE)
137137
.setHeader("Content-Type", MediaType.PLAIN_TEXT_UTF_8.toString())
138138
.setContent(Contents.utf8String("No capacity available"));
139139
};

java/src/org/openqa/selenium/grid/router/httpd/RouterServer.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,6 @@
1717

1818
package org.openqa.selenium.grid.router.httpd;
1919

20-
import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
21-
import static org.openqa.selenium.grid.config.StandardGridRoles.DISTRIBUTOR_ROLE;
22-
import static org.openqa.selenium.grid.config.StandardGridRoles.HTTPD_ROLE;
23-
import static org.openqa.selenium.grid.config.StandardGridRoles.ROUTER_ROLE;
24-
import static org.openqa.selenium.grid.config.StandardGridRoles.SESSION_MAP_ROLE;
25-
import static org.openqa.selenium.grid.config.StandardGridRoles.SESSION_QUEUE_ROLE;
26-
import static org.openqa.selenium.net.Urls.fromUri;
27-
import static org.openqa.selenium.remote.http.Route.combine;
28-
import static org.openqa.selenium.remote.http.Route.get;
29-
3020
import com.google.auto.service.AutoService;
3121
import com.google.common.collect.ImmutableMap;
3222
import com.google.common.collect.ImmutableSet;
@@ -59,7 +49,9 @@
5949
import org.openqa.selenium.grid.web.GridUiRoute;
6050
import org.openqa.selenium.internal.Require;
6151
import org.openqa.selenium.remote.http.ClientConfig;
52+
import org.openqa.selenium.remote.http.Contents;
6253
import org.openqa.selenium.remote.http.HttpClient;
54+
import org.openqa.selenium.remote.http.HttpHandler;
6355
import org.openqa.selenium.remote.http.HttpResponse;
6456
import org.openqa.selenium.remote.http.Routable;
6557
import org.openqa.selenium.remote.http.Route;
@@ -71,6 +63,17 @@
7163
import java.util.Set;
7264
import java.util.logging.Logger;
7365

66+
import static java.net.HttpURLConnection.HTTP_OK;
67+
import static java.net.HttpURLConnection.HTTP_UNAVAILABLE;
68+
import static org.openqa.selenium.grid.config.StandardGridRoles.DISTRIBUTOR_ROLE;
69+
import static org.openqa.selenium.grid.config.StandardGridRoles.HTTPD_ROLE;
70+
import static org.openqa.selenium.grid.config.StandardGridRoles.ROUTER_ROLE;
71+
import static org.openqa.selenium.grid.config.StandardGridRoles.SESSION_MAP_ROLE;
72+
import static org.openqa.selenium.grid.config.StandardGridRoles.SESSION_QUEUE_ROLE;
73+
import static org.openqa.selenium.net.Urls.fromUri;
74+
import static org.openqa.selenium.remote.http.Route.combine;
75+
import static org.openqa.selenium.remote.http.Route.get;
76+
7477
@AutoService(CliCommand.class)
7578
public class RouterServer extends TemplateGridServerCommand {
7679

@@ -154,8 +157,8 @@ protected Handlers createHandlers(Config config) {
154157
getServerVersion());
155158

156159
Routable ui = new GridUiRoute();
157-
Routable routerWithSpecChecks = new Router(tracer, clientFactory, sessions, queue, distributor)
158-
.with(networkOptions.getSpecComplianceChecks());
160+
Router router = new Router(tracer, clientFactory, sessions, queue, distributor);
161+
Routable routerWithSpecChecks = router.with(networkOptions.getSpecComplianceChecks());
159162

160163
Routable route = Route.combine(
161164
ui,
@@ -170,10 +173,17 @@ protected Handlers createHandlers(Config config) {
170173
route = route.with(new BasicAuthenticationFilter(uap.username(), uap.password()));
171174
}
172175

176+
HttpHandler readinessCheck = req -> {
177+
boolean ready = router.isReady();
178+
return new HttpResponse()
179+
.setStatus(ready ? HTTP_OK : HTTP_UNAVAILABLE)
180+
.setContent(Contents.utf8String("Router is " + ready));
181+
};
182+
173183
// Since k8s doesn't make it easy to do an authenticated liveness probe, allow unauthenticated access to it.
174184
Routable routeWithLiveness = Route.combine(
175185
route,
176-
get("/readyz").to(() -> req -> new HttpResponse().setStatus(HTTP_NO_CONTENT)));
186+
get("/readyz").to(() -> readinessCheck));
177187

178188
return new Handlers(routeWithLiveness, new ProxyWebsocketsIntoGrid(clientFactory, sessions));
179189
}

0 commit comments

Comments
 (0)