18
18
package org .openqa .selenium .grid .router ;
19
19
20
20
import com .google .common .collect .ImmutableMap ;
21
+
21
22
import org .openqa .selenium .grid .data .DistributorStatus ;
22
23
import org .openqa .selenium .grid .distributor .Distributor ;
23
24
import org .openqa .selenium .internal .Require ;
24
- import org .openqa .selenium .json .Json ;
25
- import org .openqa .selenium .remote .http .HttpClient ;
26
25
import org .openqa .selenium .remote .http .HttpHandler ;
27
26
import org .openqa .selenium .remote .http .HttpRequest ;
28
27
import org .openqa .selenium .remote .http .HttpResponse ;
29
28
import org .openqa .selenium .remote .tracing .AttributeKey ;
30
29
import org .openqa .selenium .remote .tracing .EventAttribute ;
31
30
import org .openqa .selenium .remote .tracing .EventAttributeValue ;
32
- import org .openqa .selenium .remote .tracing .HttpTracing ;
33
31
import org .openqa .selenium .remote .tracing .Span ;
34
32
import org .openqa .selenium .remote .tracing .Status ;
35
33
import org .openqa .selenium .remote .tracing .Tracer ;
36
34
37
- import java .io .IOException ;
38
35
import java .util .HashMap ;
39
36
import java .util .List ;
40
37
import java .util .Map ;
41
- import java .util .concurrent .CompletableFuture ;
42
38
import java .util .concurrent .ExecutionException ;
43
39
import java .util .concurrent .ExecutorService ;
44
40
import java .util .concurrent .Executors ;
45
- import java .util .concurrent .Future ;
46
- import java .util .concurrent .ScheduledExecutorService ;
47
41
import java .util .concurrent .TimeoutException ;
48
42
49
- import static java .util .concurrent .TimeUnit .MILLISECONDS ;
50
43
import static java .util .concurrent .TimeUnit .SECONDS ;
51
44
import static java .util .stream .Collectors .toList ;
52
- import static org .openqa .selenium .json .Json .MAP_TYPE ;
53
45
import static org .openqa .selenium .remote .http .Contents .asJson ;
54
- import static org .openqa .selenium .remote .http .Contents .string ;
55
- import static org .openqa .selenium .remote .http .HttpMethod .GET ;
56
46
import static org .openqa .selenium .remote .tracing .HttpTracing .newSpanAsChildOf ;
57
47
import static org .openqa .selenium .remote .tracing .Tags .EXCEPTION ;
58
- import static org .openqa .selenium .remote .tracing .Tags .HTTP_RESPONSE ;
59
- import static org .openqa .selenium .remote .tracing .Tags .HTTP_RESPONSE_EVENT ;
60
48
import static org .openqa .selenium .remote .tracing .Tags .HTTP_REQUEST ;
61
49
import static org .openqa .selenium .remote .tracing .Tags .HTTP_REQUEST_EVENT ;
50
+ import static org .openqa .selenium .remote .tracing .Tags .HTTP_RESPONSE ;
51
+ import static org .openqa .selenium .remote .tracing .Tags .HTTP_RESPONSE_EVENT ;
62
52
63
53
class GridStatusHandler implements HttpHandler {
64
54
65
- private static final ScheduledExecutorService
66
- SCHEDULED_SERVICE =
67
- Executors .newScheduledThreadPool (
68
- 1 ,
69
- r -> {
70
- Thread thread = new Thread (r , "Scheduled grid status executor" );
71
- thread .setDaemon (true );
72
- return thread ;
73
- });
74
-
75
-
76
55
private static final ExecutorService EXECUTOR_SERVICE = Executors .newCachedThreadPool (
77
56
r -> {
78
57
Thread thread = new Thread (r , "Grid status executor" );
@@ -81,21 +60,16 @@ class GridStatusHandler implements HttpHandler {
81
60
});
82
61
83
62
84
- private final Json json ;
85
63
private final Tracer tracer ;
86
- private final HttpClient .Factory clientFactory ;
87
64
private final Distributor distributor ;
88
65
89
- GridStatusHandler (Json json , Tracer tracer , HttpClient .Factory clientFactory , Distributor distributor ) {
90
- this .json = Require .nonNull ("JSON encoder" , json );
66
+ GridStatusHandler (Tracer tracer , Distributor distributor ) {
91
67
this .tracer = Require .nonNull ("Tracer" , tracer );
92
- this .clientFactory = Require .nonNull ("HTTP client factory" , clientFactory );
93
68
this .distributor = Require .nonNull ("Distributor" , distributor );
94
69
}
95
70
96
71
@ Override
97
72
public HttpResponse execute (HttpRequest req ) {
98
- long start = System .currentTimeMillis ();
99
73
100
74
try (Span span = newSpanAsChildOf (tracer , req , "grid.status" )) {
101
75
Map <String , EventAttributeValue > attributeMap = new HashMap <>();
@@ -113,7 +87,8 @@ public HttpResponse execute(HttpRequest req) {
113
87
span .setStatus (Status .CANCELLED );
114
88
EXCEPTION .accept (attributeMap , e );
115
89
attributeMap .put (AttributeKey .EXCEPTION_MESSAGE .getKey (),
116
- EventAttribute .setValue ("Unable to get distributor status due to execution error or timeout: " + e .getMessage ()));
90
+ EventAttribute .setValue ("Error or timeout while getting Distributor "
91
+ + "status: " + e .getMessage ()));
117
92
HttpResponse response = new HttpResponse ().setContent (asJson (
118
93
ImmutableMap .of ("value" , ImmutableMap .of (
119
94
"ready" , false ,
@@ -129,7 +104,8 @@ public HttpResponse execute(HttpRequest req) {
129
104
span .setStatus (Status .ABORTED );
130
105
EXCEPTION .accept (attributeMap , e );
131
106
attributeMap .put (AttributeKey .EXCEPTION_MESSAGE .getKey (),
132
- EventAttribute .setValue ("Interruption while getting distributor status: " + e .getMessage ()));
107
+ EventAttribute .setValue ("Interruption while getting distributor status: "
108
+ + e .getMessage ()));
133
109
134
110
HttpResponse response = new HttpResponse ().setContent (asJson (
135
111
ImmutableMap .of ("value" , ImmutableMap .of (
@@ -141,82 +117,31 @@ public HttpResponse execute(HttpRequest req) {
141
117
span .addEvent (AttributeKey .EXCEPTION_EVENT .getKey (), attributeMap );
142
118
143
119
Thread .currentThread ().interrupt ();
144
- return response ;
120
+ return response ;
145
121
}
146
122
147
123
boolean ready = status .hasCapacity ();
148
124
149
- long remaining = System .currentTimeMillis () + 2000 - start ;
150
- List <Future <Map <String , Object >>> nodeResults = status .getNodes ().stream ()
151
- .map (node -> {
152
- ImmutableMap <String , Object > defaultResponse = ImmutableMap .of (
153
- "id" , node .getId (),
154
- "uri" , node .getUri (),
155
- "maxSessions" , node .getMaxSessionCount (),
156
- "slots" , node .getSlots (),
157
- "warning" , "Unable to read data from node." );
158
-
159
- CompletableFuture <Map <String , Object >> toReturn = new CompletableFuture <>();
160
-
161
- Future <?> future = EXECUTOR_SERVICE .submit (
162
- () -> {
163
- try (HttpClient client = clientFactory .createClient (node .getUri ().toURL ())) {
164
- HttpRequest nodeStatusReq = new HttpRequest (GET , "/se/grid/node/status" );
165
- HttpTracing .inject (tracer , span , nodeStatusReq );
166
- HttpResponse res = client .execute (nodeStatusReq );
167
-
168
- toReturn .complete (res .getStatus () == 200
169
- ? json .toType (string (res ), MAP_TYPE )
170
- : defaultResponse );
171
- } catch (IOException e ) {
172
- toReturn .complete (defaultResponse );
173
- }
174
- });
175
-
176
- SCHEDULED_SERVICE .schedule (
177
- () -> {
178
- if (!toReturn .isDone ()) {
179
- toReturn .complete (defaultResponse );
180
- future .cancel (true );
181
- }
182
- },
183
- remaining ,
184
- MILLISECONDS );
185
-
186
- return toReturn ;
187
- })
125
+ List <Map <String , Object >> nodeResults = status .getNodes ().stream ()
126
+ .map (node -> new ImmutableMap .Builder <String , Object >()
127
+ .put ("id" , node .getId ())
128
+ .put ("uri" , node .getUri ())
129
+ .put ("maxSessions" , node .getMaxSessionCount ())
130
+ .put ("osInfo" , node .getOsInfo ())
131
+ .put ("heartbeatPeriod" , node .heartbeatPeriod ().toMillis ())
132
+ .put ("availability" , node .getAvailability ())
133
+ .put ("version" , node .getVersion ())
134
+ .put ("slots" , node .getSlots ())
135
+ .build ())
188
136
.collect (toList ());
189
137
190
138
ImmutableMap .Builder <String , Object > value = ImmutableMap .builder ();
191
139
value .put ("ready" , ready );
192
140
value .put ("message" , ready ? "Selenium Grid ready." : "Selenium Grid not ready." );
141
+ value .put ("nodes" , nodeResults );
193
142
194
- value .put ("nodes" , nodeResults .stream ()
195
- .map (summary -> {
196
- try {
197
- return summary .get ();
198
- } catch (ExecutionException e ) {
199
- span .setAttribute ("error" , true );
200
- span .setStatus (Status .NOT_FOUND );
201
- EXCEPTION .accept (attributeMap , e );
202
- attributeMap .put (AttributeKey .EXCEPTION_MESSAGE .getKey (),
203
- EventAttribute .setValue ("Unable to get Node information: " + e .getMessage ()));
204
- span .addEvent (AttributeKey .EXCEPTION_EVENT .getKey (), attributeMap );
205
- throw wrap (e );
206
- } catch (InterruptedException e ) {
207
- span .setAttribute ("error" , true );
208
- span .setStatus (Status .NOT_FOUND );
209
- EXCEPTION .accept (attributeMap , e );
210
- attributeMap .put (AttributeKey .EXCEPTION_MESSAGE .getKey (),
211
- EventAttribute .setValue ("Unable to get Node information: " + e .getMessage ()));
212
- span .addEvent (AttributeKey .EXCEPTION_EVENT .getKey (), attributeMap );
213
- Thread .currentThread ().interrupt ();
214
- throw wrap (e );
215
- }
216
- })
217
- .collect (toList ()));
218
-
219
- HttpResponse res = new HttpResponse ().setContent (asJson (ImmutableMap .of ("value" , value .build ())));
143
+ HttpResponse res = new HttpResponse ()
144
+ .setContent (asJson (ImmutableMap .of ("value" , value .build ())));
220
145
HTTP_RESPONSE .accept (span , res );
221
146
HTTP_RESPONSE_EVENT .accept (attributeMap , res );
222
147
attributeMap .put ("grid.status" , EventAttribute .setValue (ready ));
@@ -225,18 +150,4 @@ public HttpResponse execute(HttpRequest req) {
225
150
return res ;
226
151
}
227
152
}
228
-
229
- private RuntimeException wrap (Exception e ) {
230
- if (e instanceof InterruptedException ) {
231
- Thread .currentThread ().interrupt ();
232
- return new RuntimeException (e );
233
- }
234
-
235
- Throwable cause = e .getCause ();
236
- if (cause == null ) {
237
- return e instanceof RuntimeException ? (RuntimeException ) e : new RuntimeException (e );
238
- }
239
- return cause instanceof RuntimeException ? (RuntimeException ) cause
240
- : new RuntimeException (cause );
241
- }
242
153
}
0 commit comments