Skip to content

Commit f811064

Browse files
committed
[grid] Rename NodeStatus id and uri field for serialization
1 parent 9617c44 commit f811064

File tree

11 files changed

+133
-89
lines changed

11 files changed

+133
-89
lines changed

java/server/src/org/openqa/selenium/grid/data/NodeStatus.java

+85-31
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
import org.openqa.selenium.internal.Require;
2222
import org.openqa.selenium.json.JsonInput;
2323
import org.openqa.selenium.json.TypeToken;
24+
import org.redisson.api.annotation.REntity;
25+
import org.redisson.api.annotation.RId;
26+
import org.redisson.api.annotation.RIndex;
2427

28+
import java.io.Serializable;
2529
import java.net.URI;
2630
import java.time.Duration;
2731
import java.time.Instant;
@@ -34,18 +38,24 @@
3438
import static java.util.Collections.unmodifiableMap;
3539
import static java.util.Collections.unmodifiableSet;
3640

41+
@REntity
3742
public class NodeStatus {
3843

39-
private final NodeId nodeId;
40-
private final URI externalUri;
41-
private final int maxSessionCount;
42-
private final Set<Slot> slots;
43-
private final Availability availability;
44+
@RId
45+
private NodeId nodeId;
46+
47+
private URI externalUri;
48+
private int maxSessionCount;
49+
private Set<Slot> slots;
50+
private Availability availability;
4451
private Duration heartbeatPeriod;
45-
private final String version;
46-
private final Map<String, String> osInfo;
52+
private String version;
53+
private Map<String, String> osInfo;
4754
private long touched = System.currentTimeMillis();
4855

56+
public NodeStatus() {
57+
}
58+
4959
public NodeStatus(
5060
NodeId nodeId,
5161
URI externalUri,
@@ -69,7 +79,7 @@ public NodeStatus(
6979

7080
public static NodeStatus fromJson(JsonInput input) {
7181
NodeId nodeId = null;
72-
URI uri = null;
82+
URI externalUri = null;
7383
int maxSessions = 0;
7484
Set<Slot> slots = null;
7585
Availability availability = null;
@@ -88,7 +98,7 @@ public static NodeStatus fromJson(JsonInput input) {
8898
heartbeatPeriod = Duration.ofMillis(input.read(Long.class));
8999
break;
90100

91-
case "id":
101+
case "nodeId":
92102
nodeId = input.read(NodeId.class);
93103
break;
94104

@@ -101,8 +111,8 @@ public static NodeStatus fromJson(JsonInput input) {
101111
}.getType());
102112
break;
103113

104-
case "uri":
105-
uri = input.read(URI.class);
114+
case "externalUri":
115+
externalUri = input.read(URI.class);
106116
break;
107117

108118
case "version":
@@ -122,7 +132,7 @@ public static NodeStatus fromJson(JsonInput input) {
122132

123133
return new NodeStatus(
124134
nodeId,
125-
uri,
135+
externalUri,
126136
maxSessions,
127137
slots,
128138
availability,
@@ -136,56 +146,100 @@ public boolean hasCapability(Capabilities caps) {
136146
}
137147

138148
public boolean hasCapacity() {
139-
return slots.stream().anyMatch(slot -> !slot.getSession().isPresent());
149+
return slots.stream().anyMatch(slot -> slot.getSession() == null);
140150
}
141151

142152
public boolean hasCapacity(Capabilities caps) {
143153
return slots.stream()
144-
.anyMatch(slot -> !slot.getSession().isPresent() && slot.isSupporting(caps));
154+
.anyMatch(slot -> slot.getSession() == null && slot.isSupporting(caps));
155+
}
156+
157+
public int getMaxSessionCount() {
158+
return maxSessionCount;
159+
}
160+
161+
public void setMaxSessionCount(int maxSessionCount) {
162+
this.maxSessionCount = maxSessionCount;
145163
}
146164

147-
public NodeId getId() {
165+
public NodeId getNodeId() {
148166
return nodeId;
149167
}
150168

151-
public URI getUri() {
169+
public void setNodeId(NodeId nodeId) {
170+
this.nodeId = nodeId;
171+
}
172+
173+
public URI getExternalUri() {
152174
return externalUri;
153175
}
154176

155-
public int getMaxSessionCount() {
156-
return maxSessionCount;
177+
public void setExternalUri(URI externalUri) {
178+
this.externalUri = externalUri;
157179
}
158180

159181
public Set<Slot> getSlots() {
160182
return slots;
161183
}
162184

185+
public void setSlots(Set<Slot> slots) {
186+
this.slots = slots;
187+
}
188+
163189
public Availability getAvailability() {
164190
return availability;
165191
}
166192

193+
public void setAvailability(Availability availability) {
194+
this.availability = availability;
195+
}
196+
197+
public Duration getHeartbeatPeriod() {
198+
return heartbeatPeriod;
199+
}
200+
201+
public void setHeartbeatPeriod(Duration heartbeatPeriod) {
202+
this.heartbeatPeriod = heartbeatPeriod;
203+
}
204+
167205
public String getVersion() {
168206
return version;
169207
}
170208

209+
public void setVersion(String version) {
210+
this.version = version;
211+
}
212+
171213
public Map<String, String> getOsInfo() {
172214
return osInfo;
173215
}
174216

217+
public void setOsInfo(Map<String, String> osInfo) {
218+
this.osInfo = osInfo;
219+
}
220+
221+
public long getTouched() {
222+
return touched;
223+
}
224+
225+
public void setTouched(long touched) {
226+
this.touched = touched;
227+
}
228+
175229
public float getLoad() {
176230
float inUse = slots.parallelStream()
177-
.filter(slot -> slot.getSession().isPresent())
231+
.filter(slot -> slot.getSession() != null)
178232
.count();
179233

180234
return (inUse / (float) maxSessionCount) * 100f;
181235
}
182236

183237
public long getLastSessionCreated() {
184-
return slots.parallelStream()
185-
.map(Slot::getLastStarted)
186-
.mapToLong(Instant::toEpochMilli)
187-
.max()
188-
.orElse(0);
238+
return slots.parallelStream()
239+
.map(Slot::getLastStarted)
240+
.mapToLong(Instant::toEpochMilli)
241+
.max()
242+
.orElse(0);
189243
}
190244

191245
public Duration heartbeatPeriod() {
@@ -208,11 +262,11 @@ public boolean equals(Object o) {
208262

209263
NodeStatus that = (NodeStatus) o;
210264
return Objects.equals(this.nodeId, that.nodeId) &&
211-
Objects.equals(this.externalUri, that.externalUri) &&
212-
this.maxSessionCount == that.maxSessionCount &&
213-
Objects.equals(this.slots, that.slots) &&
214-
Objects.equals(this.availability, that.availability) &&
215-
Objects.equals(this.version, that.version);
265+
Objects.equals(this.externalUri, that.externalUri) &&
266+
this.maxSessionCount == that.maxSessionCount &&
267+
Objects.equals(this.slots, that.slots) &&
268+
Objects.equals(this.availability, that.availability) &&
269+
Objects.equals(this.version, that.version);
216270
}
217271

218272
@Override
@@ -222,8 +276,8 @@ public int hashCode() {
222276

223277
private Map<String, Object> toJson() {
224278
Map<String, Object> toReturn = new TreeMap<>();
225-
toReturn.put("id", nodeId);
226-
toReturn.put("uri", externalUri);
279+
toReturn.put("nodeId", nodeId);
280+
toReturn.put("externalUri", externalUri);
227281
toReturn.put("maxSessions", maxSessionCount);
228282
toReturn.put("slots", slots);
229283
toReturn.put("availability", availability);

java/server/src/org/openqa/selenium/grid/distributor/AddNode.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public HttpResponse execute(HttpRequest req) {
6262
Node node = new RemoteNode(
6363
tracer,
6464
httpFactory,
65-
status.getId(),
66-
status.getUri(),
65+
status.getNodeId(),
66+
status.getExternalUri(),
6767
registrationSecret,
6868
status.getSlots().stream().map(Slot::getStereotype).collect(Collectors.toSet()));
6969

java/server/src/org/openqa/selenium/grid/distributor/gridmodel/local/LocalGridModel.java

+22-29
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
public class LocalGridModel implements GridModel {
5555

5656
private static final Logger LOG = Logger.getLogger(LocalGridModel.class.getName());
57-
private static final SessionId RESERVED = new SessionId("reserved");
5857
private final ReadWriteLock lock = new ReentrantReadWriteLock(/* fair */ true);
5958
private final Map<Availability, Set<NodeStatus>> nodes = new ConcurrentHashMap<>();
6059
private final EventBus events;
@@ -86,8 +85,8 @@ public void add(NodeStatus node) {
8685
NodeStatus next = iterator.next();
8786

8887
// If the ID is the same, we're re-adding a node. If the URI is the same a node probably restarted
89-
if (next.getId().equals(node.getId()) || next.getUri().equals(node.getUri())) {
90-
LOG.info(String.format("Re-adding node with id %s and URI %s.", node.getId(), node.getUri()));
88+
if (next.getNodeId().equals(node.getNodeId()) || next.getExternalUri().equals(node.getExternalUri())) {
89+
LOG.info(String.format("Re-adding node with id %s and URI %s.", node.getNodeId(), node.getExternalUri()));
9190
iterator.remove();
9291
}
9392
}
@@ -107,7 +106,7 @@ public void refresh(NodeStatus status) {
107106
Lock writeLock = lock.writeLock();
108107
writeLock.lock();
109108
try {
110-
AvailabilityAndNode availabilityAndNode = findNode(status.getId());
109+
AvailabilityAndNode availabilityAndNode = findNode(status.getNodeId());
111110

112111
if (availabilityAndNode == null) {
113112
return;
@@ -181,7 +180,7 @@ public void purgeDeadNodes() {
181180
LOG.info(String.format(
182181
"Switching nodes %s from UP to DOWN",
183182
lost.stream()
184-
.map(node -> String.format("%s (uri: %s)", node.getId(), node.getUri()))
183+
.map(node -> String.format("%s (uri: %s)", node.getNodeId(), node.getExternalUri()))
185184
.collect(joining(", "))));
186185
nodes(UP).removeAll(lost);
187186
nodes(DOWN).addAll(lost);
@@ -190,7 +189,7 @@ public void purgeDeadNodes() {
190189
LOG.info(String.format(
191190
"Switching nodes %s from DOWN to UP",
192191
resurrected.stream()
193-
.map(node -> String.format("%s (uri: %s)", node.getId(), node.getUri()))
192+
.map(node -> String.format("%s (uri: %s)", node.getNodeId(), node.getExternalUri()))
194193
.collect(joining(", "))));
195194
nodes(DOWN).removeAll(resurrected);
196195
nodes(UP).addAll(resurrected);
@@ -199,7 +198,7 @@ public void purgeDeadNodes() {
199198
LOG.info(String.format(
200199
"Removing nodes %s that are DOWN for too long",
201200
dead.stream()
202-
.map(node -> String.format("%s (uri: %s)", node.getId(), node.getUri()))
201+
.map(node -> String.format("%s (uri: %s)", node.getNodeId(), node.getExternalUri()))
203202
.collect(joining(", "))));
204203
nodes(DOWN).removeAll(dead);
205204
}
@@ -232,7 +231,7 @@ public Availability setAvailability(NodeId id, Availability availability) {
232231
LOG.info(String.format(
233232
"Switching node %s (uri: %s) from %s to %s",
234233
id,
235-
availabilityAndNode.status.getUri(),
234+
availabilityAndNode.status.getExternalUri(),
236235
availabilityAndNode.availability,
237236
availability));
238237
return availabilityAndNode.availability;
@@ -267,7 +266,7 @@ public boolean reserve(SlotId slotId) {
267266
if (!maybeSlot.isPresent()) {
268267
LOG.warning(String.format(
269268
"Asked to reserve slot on node %s, but no slot with id %s found",
270-
node.status.getId(),
269+
node.status.getNodeId(),
271270
slotId));
272271
return false;
273272
}
@@ -300,21 +299,23 @@ public Set<NodeStatus> nodes(Availability availability) {
300299
return nodes.computeIfAbsent(availability, ignored -> new HashSet<>());
301300
}
302301

303-
private AvailabilityAndNode findNode(NodeId id) {
302+
@Override
303+
public AvailabilityAndNode findNode(NodeId id) {
304304
for (Map.Entry<Availability, Set<NodeStatus>> entry : nodes.entrySet()) {
305305
for (NodeStatus nodeStatus : entry.getValue()) {
306-
if (id.equals(nodeStatus.getId())) {
306+
if (id.equals(nodeStatus.getNodeId())) {
307307
return new AvailabilityAndNode(entry.getKey(), nodeStatus);
308308
}
309309
}
310310
}
311311
return null;
312312
}
313313

314-
private NodeStatus rewrite(NodeStatus status, Availability availability) {
314+
@Override
315+
public NodeStatus rewrite(NodeStatus status, Availability availability) {
315316
return new NodeStatus(
316-
status.getId(),
317-
status.getUri(),
317+
status.getNodeId(),
318+
status.getExternalUri(),
318319
status.getMaxSessionCount(),
319320
status.getSlots(),
320321
availability,
@@ -356,7 +357,8 @@ public void release(SessionId id) {
356357
}
357358
}
358359

359-
private void reserve(NodeStatus status, Slot slot) {
360+
@Override
361+
public void reserve(NodeStatus status, Slot slot) {
360362
Instant now = Instant.now();
361363

362364
Slot reserved = new Slot(
@@ -365,7 +367,7 @@ private void reserve(NodeStatus status, Slot slot) {
365367
now,
366368
new Session(
367369
RESERVED,
368-
status.getUri(),
370+
status.getExternalUri(),
369371
slot.getStereotype(),
370372
slot.getStereotype(),
371373
now));
@@ -420,30 +422,21 @@ public void setSession(SlotId slotId, Session session) {
420422
}
421423
}
422424

423-
private void amend(Availability availability, NodeStatus status, Slot slot) {
425+
@Override
426+
public void amend(Availability availability, NodeStatus status, Slot slot) {
424427
Set<Slot> newSlots = new HashSet<>(status.getSlots());
425428
newSlots.removeIf(s -> s.getId().equals(slot.getId()));
426429
newSlots.add(slot);
427430

428431
nodes(availability).remove(status);
429432
nodes(availability).add(new NodeStatus(
430-
status.getId(),
431-
status.getUri(),
433+
status.getNodeId(),
434+
status.getExternalUri(),
432435
status.getMaxSessionCount(),
433436
newSlots,
434437
status.getAvailability(),
435438
status.heartbeatPeriod(),
436439
status.getVersion(),
437440
status.getOsInfo()));
438441
}
439-
440-
private static class AvailabilityAndNode {
441-
public final Availability availability;
442-
public final NodeStatus status;
443-
444-
public AvailabilityAndNode(Availability availability, NodeStatus status) {
445-
this.availability = availability;
446-
this.status = status;
447-
}
448-
}
449442
}

0 commit comments

Comments
 (0)