Skip to content

Commit b934a75

Browse files
authored
[grid] Delete existing sessions if the Node is restarted
1 parent b7982ae commit b934a75

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.grid.data;
19+
20+
import org.openqa.selenium.events.Event;
21+
import org.openqa.selenium.events.EventListener;
22+
import org.openqa.selenium.events.EventName;
23+
import org.openqa.selenium.internal.Require;
24+
25+
import java.util.function.Consumer;
26+
27+
public class NodeRestartedEvent extends Event {
28+
29+
private static final EventName NODE_RESTARTED_EVENT = new EventName("node-restarted");
30+
31+
public NodeRestartedEvent(NodeStatus nodes) {
32+
super(NODE_RESTARTED_EVENT, nodes);
33+
}
34+
35+
public static EventListener<NodeStatus> listener(Consumer<NodeStatus> handler) {
36+
Require.nonNull("Handler", handler);
37+
38+
return new EventListener<>(NODE_RESTARTED_EVENT, NodeStatus.class , handler);
39+
}
40+
}

java/src/org/openqa/selenium/grid/distributor/GridModel.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.openqa.selenium.grid.data.NodeDrainStarted;
2525
import org.openqa.selenium.grid.data.NodeId;
2626
import org.openqa.selenium.grid.data.NodeRemovedEvent;
27+
import org.openqa.selenium.grid.data.NodeRestartedEvent;
2728
import org.openqa.selenium.grid.data.NodeStatus;
2829
import org.openqa.selenium.grid.data.Session;
2930
import org.openqa.selenium.grid.data.SessionClosedEvent;
@@ -104,6 +105,16 @@ public void add(NodeStatus node) {
104105
return;
105106
}
106107

108+
// If the URI is the same but NodeId is different then the Node has restarted
109+
if(!next.getNodeId().equals(node.getNodeId()) &&
110+
next.getExternalUri().equals(node.getExternalUri())) {
111+
LOG.info(String.format("Re-adding node with id %s and URI %s.", node.getNodeId(), node.getExternalUri()));
112+
113+
events.fire(new NodeRestartedEvent(node));
114+
iterator.remove();
115+
break;
116+
}
117+
107118
// If the URI has changed, then assume this is a new node and fall
108119
// out of the loop: we want to add it as `DOWN` until something
109120
// changes our mind.

java/src/org/openqa/selenium/grid/sessionmap/local/LocalSessionMap.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.openqa.selenium.events.EventBus;
2222
import org.openqa.selenium.grid.config.Config;
2323
import org.openqa.selenium.grid.data.NodeRemovedEvent;
24+
import org.openqa.selenium.grid.data.NodeRestartedEvent;
2425
import org.openqa.selenium.grid.data.Session;
2526
import org.openqa.selenium.grid.data.SessionClosedEvent;
2627
import org.openqa.selenium.grid.log.LoggingOptions;
@@ -77,6 +78,9 @@ public LocalSessionMap(Tracer tracer, EventBus bus) {
7778
.map(slot -> slot.getSession().getId())
7879
.forEach(knownSessions::remove)));
7980

81+
bus.addListener(NodeRestartedEvent.listener(nodeStatus -> knownSessions.values()
82+
.removeIf(value -> value.getUri().equals(nodeStatus.getExternalUri()))));
83+
8084
}
8185

8286
public static SessionMap create(Config config) {

0 commit comments

Comments
 (0)