Skip to content

Commit 707cf39

Browse files
committed
[grid] Purge sessions on Node restart for Jdbc backed session map
1 parent 9bb5163 commit 707cf39

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

java/src/org/openqa/selenium/grid/sessionmap/jdbc/JdbcBackedSessionMap.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.openqa.selenium.grid.config.Config;
2525
import org.openqa.selenium.grid.config.ConfigException;
2626
import org.openqa.selenium.grid.data.NodeRemovedEvent;
27+
import org.openqa.selenium.grid.data.NodeRestartedEvent;
2728
import org.openqa.selenium.grid.data.Session;
2829
import org.openqa.selenium.grid.data.SessionClosedEvent;
2930
import org.openqa.selenium.grid.log.LoggingOptions;
@@ -89,6 +90,8 @@ public JdbcBackedSessionMap(Tracer tracer, Connection jdbcConnection, EventBus b
8990
.filter(slot -> slot.getSession() != null)
9091
.map(slot -> slot.getSession().getId())
9192
.forEach(this::remove)));
93+
94+
bus.addListener(NodeRestartedEvent.listener(nodeStatus -> this.removeByUri(nodeStatus.getExternalUri())));
9295
}
9396

9497
public static SessionMap create(Config config) {
@@ -310,6 +313,42 @@ public void remove(SessionId id) {
310313
}
311314
}
312315

316+
public void removeByUri(URI sessionUri) {
317+
Require.nonNull("Session URI", sessionUri);
318+
try (Span span = tracer.getCurrentContext().createSpan(
319+
"DELETE from sessions_map where session_uri = ?")) {
320+
Map<String, EventAttributeValue> attributeMap = new HashMap<>();
321+
322+
try (PreparedStatement statement = connection.prepareStatement(
323+
String.format("delete from %1$s where %2$s = ?",
324+
TABLE_NAME,
325+
SESSION_URI_COL))) {
326+
327+
statement.setString(1, sessionUri.toString());
328+
String statementStr = statement.toString();
329+
span.setAttribute(DATABASE_STATEMENT, statementStr);
330+
span.setAttribute(DATABASE_OPERATION, "delete");
331+
attributeMap.put(DATABASE_STATEMENT, EventAttribute.setValue(statementStr));
332+
attributeMap.put(DATABASE_OPERATION, EventAttribute.setValue("delete"));
333+
334+
int rowCount = statement.executeUpdate();
335+
attributeMap.put("rows.deleted", EventAttribute.setValue(rowCount));
336+
span.addEvent("Deleted session from the database", attributeMap);
337+
338+
} catch (SQLException e) {
339+
span.setAttribute("error", true);
340+
span.setStatus(Status.CANCELLED);
341+
EXCEPTION.accept(attributeMap, e);
342+
attributeMap.put(AttributeKey.EXCEPTION_MESSAGE.getKey(),
343+
EventAttribute.setValue(
344+
"Unable to delete session information from the database: " + e
345+
.getMessage()));
346+
span.addEvent(AttributeKey.EXCEPTION_EVENT.getKey(), attributeMap);
347+
throw new JdbcException(e.getMessage());
348+
}
349+
}
350+
}
351+
313352
@Override
314353
public void close() {
315354
try {

0 commit comments

Comments
 (0)