-
-
Notifications
You must be signed in to change notification settings - Fork 8k
/
Distributor.java
297 lines (262 loc) · 11.7 KB
/
Distributor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.openqa.selenium.grid.distributor;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.grid.data.CreateSessionRequest;
import org.openqa.selenium.grid.data.CreateSessionResponse;
import org.openqa.selenium.grid.data.DistributorStatus;
import org.openqa.selenium.grid.data.NodeId;
import org.openqa.selenium.grid.data.NodeStatus;
import org.openqa.selenium.grid.data.Session;
import org.openqa.selenium.grid.data.SlotId;
import org.openqa.selenium.grid.distributor.selector.SlotSelector;
import org.openqa.selenium.grid.node.Node;
import org.openqa.selenium.grid.security.RequiresSecretFilter;
import org.openqa.selenium.grid.security.Secret;
import org.openqa.selenium.grid.sessionmap.SessionMap;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.NewSessionPayload;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;
import org.openqa.selenium.remote.http.Routable;
import org.openqa.selenium.remote.http.Route;
import org.openqa.selenium.remote.tracing.AttributeKey;
import org.openqa.selenium.remote.tracing.EventAttribute;
import org.openqa.selenium.remote.tracing.EventAttributeValue;
import org.openqa.selenium.remote.tracing.Span;
import org.openqa.selenium.remote.tracing.SpanDecorator;
import org.openqa.selenium.remote.tracing.Status;
import org.openqa.selenium.remote.tracing.Tracer;
import org.openqa.selenium.status.HasReadyState;
import java.io.IOException;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import static org.openqa.selenium.remote.RemoteTags.CAPABILITIES;
import static org.openqa.selenium.remote.RemoteTags.CAPABILITIES_EVENT;
import static org.openqa.selenium.remote.RemoteTags.SESSION_ID;
import static org.openqa.selenium.remote.RemoteTags.SESSION_ID_EVENT;
import static org.openqa.selenium.remote.http.Contents.bytes;
import static org.openqa.selenium.remote.http.Contents.reader;
import static org.openqa.selenium.remote.http.Route.delete;
import static org.openqa.selenium.remote.http.Route.get;
import static org.openqa.selenium.remote.http.Route.post;
import static org.openqa.selenium.remote.tracing.HttpTracing.newSpanAsChildOf;
import static org.openqa.selenium.remote.tracing.Tags.EXCEPTION;
/**
* Responsible for being the central place where the {@link Node}s
* on which {@link Session}s run
* are determined.
* <p>
* This class responds to the following URLs:
* <table summary="HTTP commands the Distributor understands">
* <tr>
* <th>Verb</th>
* <th>URL Template</th>
* <th>Meaning</th>
* </tr>
* <tr>
* <td>POST</td>
* <td>/session</td>
* <td>This is exactly the same as the New Session command
* from the WebDriver spec.</td>
* </tr>
* <tr>
* <td>POST</td>
* <td>/se/grid/distributor/node</td>
* <td>Adds a new {@link Node} to this distributor.
* Please read the javadocs for {@link Node} for
* how the Node should be serialized.</td>
* </tr>
* <tr>
* <td>DELETE</td>
* <td>/se/grid/distributor/node/{nodeId}</td>
* <td>Remove the {@link Node} identified by {@code nodeId}
* from this distributor. It is expected
* that any sessions running on the Node are allowed to complete:
* this simply means that no new
* sessions will be scheduled on this Node.</td>
* </tr>
* </table>
*/
public abstract class Distributor implements HasReadyState, Predicate<HttpRequest>, Routable {
private final Route routes;
protected final Tracer tracer;
private final SlotSelector slotSelector;
private final SessionMap sessions;
private final ReadWriteLock lock = new ReentrantReadWriteLock(true);
protected Distributor(
Tracer tracer,
HttpClient.Factory httpClientFactory,
SlotSelector slotSelector,
SessionMap sessions,
Secret registrationSecret) {
this.tracer = Require.nonNull("Tracer", tracer);
Require.nonNull("HTTP client factory", httpClientFactory);
this.slotSelector = Require.nonNull("Slot selector", slotSelector);
this.sessions = Require.nonNull("Session map", sessions);
Require.nonNull("Registration secret", registrationSecret);
RequiresSecretFilter requiresSecret = new RequiresSecretFilter(registrationSecret);
Json json = new Json();
routes = Route.combine(
post("/session").to(() -> req -> {
CreateSessionResponse sessionResponse = newSession(req);
return new HttpResponse().setContent(bytes(sessionResponse.getDownstreamEncodedResponse()));
}),
post("/se/grid/distributor/session")
.to(() -> new CreateSession(this))
.with(requiresSecret),
post("/se/grid/distributor/node")
.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")))))
.with(requiresSecret),
delete("/se/grid/distributor/node/{nodeId}")
.to(params -> new RemoveNode(this, new NodeId(UUID.fromString(params.get("nodeId")))))
.with(requiresSecret),
get("/se/grid/distributor/status")
.to(() -> new GetDistributorStatus(this))
.with(new SpanDecorator(tracer, req -> "distributor.status")));
}
public CreateSessionResponse newSession(HttpRequest request)
throws SessionNotCreatedException {
Span span = newSpanAsChildOf(tracer, request, "distributor.new_session");
Map<String, EventAttributeValue> attributeMap = new HashMap<>();
try (
Reader reader = reader(request);
NewSessionPayload payload = NewSessionPayload.create(reader)) {
Objects.requireNonNull(payload, "Requests to process must be set.");
attributeMap.put(AttributeKey.LOGGER_CLASS.getKey(),
EventAttribute.setValue(getClass().getName()));
Iterator<Capabilities> iterator = payload.stream().iterator();
attributeMap.put("request.payload", EventAttribute.setValue(payload.toString()));
span.addEvent("Session request received by the distributor", attributeMap);
if (!iterator.hasNext()) {
SessionNotCreatedException exception = new SessionNotCreatedException("No capabilities found");
EXCEPTION.accept(attributeMap, exception);
attributeMap.put(AttributeKey.EXCEPTION_MESSAGE.getKey(),
EventAttribute.setValue("Unable to create session. No capabilities found: "
+ exception.getMessage()));
span.addEvent(AttributeKey.EXCEPTION_EVENT.getKey(), attributeMap);
throw exception;
}
Optional<Supplier<CreateSessionResponse>> selected;
CreateSessionRequest firstRequest = new CreateSessionRequest(
payload.getDownstreamDialects(),
iterator.next(),
ImmutableMap.of("span", span));
Lock writeLock = this.lock.writeLock();
writeLock.lock();
try {
Set<NodeStatus> model = ImmutableSet.copyOf(getAvailableNodes());
// Find a host that supports the capabilities present in the new session
Set<SlotId> slotIds = slotSelector.selectSlot(firstRequest.getCapabilities(), model);
if (!slotIds.isEmpty()) {
selected = Optional.of(reserve(slotIds.iterator().next(), firstRequest));
} else {
selected = Optional.empty();
}
} finally {
writeLock.unlock();
}
CreateSessionResponse sessionResponse = selected
.orElseThrow(
() -> {
span.setAttribute("error", true);
SessionNotCreatedException
exception =
new SessionNotCreatedException(
"Unable to find provider for session: " + payload.stream()
.map(Capabilities::toString).collect(Collectors.joining(", ")));
EXCEPTION.accept(attributeMap, exception);
attributeMap.put(AttributeKey.EXCEPTION_MESSAGE.getKey(),
EventAttribute.setValue(
"Unable to find provider for session: "
+ exception.getMessage()));
span.addEvent(AttributeKey.EXCEPTION_EVENT.getKey(), attributeMap);
return exception;
})
.get();
sessions.add(sessionResponse.getSession());
SessionId sessionId = sessionResponse.getSession().getId();
Capabilities caps = sessionResponse.getSession().getCapabilities();
String sessionUri = sessionResponse.getSession().getUri().toString();
SESSION_ID.accept(span, sessionId);
CAPABILITIES.accept(span, caps);
SESSION_ID_EVENT.accept(attributeMap, sessionId);
CAPABILITIES_EVENT.accept(attributeMap, caps);
span.setAttribute(AttributeKey.SESSION_URI.getKey(), sessionUri);
attributeMap.put(AttributeKey.SESSION_URI.getKey(), EventAttribute.setValue(sessionUri));
return sessionResponse;
} catch (SessionNotCreatedException e) {
span.setAttribute("error", true);
span.setStatus(Status.ABORTED);
EXCEPTION.accept(attributeMap, e);
attributeMap.put(AttributeKey.EXCEPTION_MESSAGE.getKey(),
EventAttribute.setValue("Unable to create session: " + e.getMessage()));
span.addEvent(AttributeKey.EXCEPTION_EVENT.getKey(), attributeMap);
throw e;
} catch (IOException e) {
span.setAttribute("error", true);
span.setStatus(Status.UNKNOWN);
EXCEPTION.accept(attributeMap, e);
attributeMap.put(AttributeKey.EXCEPTION_MESSAGE.getKey(),
EventAttribute.setValue("Unknown error in LocalDistributor while creating session: " + e.getMessage()));
span.addEvent(AttributeKey.EXCEPTION_EVENT.getKey(), attributeMap);
throw new SessionNotCreatedException(e.getMessage(), e);
} finally {
span.close();
}
}
public abstract Distributor add(Node node);
public abstract boolean drain(NodeId nodeId);
public abstract void remove(NodeId nodeId);
public abstract DistributorStatus getStatus();
protected abstract Set<NodeStatus> getAvailableNodes();
protected abstract Supplier<CreateSessionResponse> reserve(SlotId slot, CreateSessionRequest request);
@Override
public boolean test(HttpRequest httpRequest) {
return matches(httpRequest);
}
@Override
public boolean matches(HttpRequest req) {
return routes.matches(req);
}
@Override
public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
return routes.execute(req);
}
}