Best Selenium code snippet using org.openqa.selenium.grid.distributor.GridModel.updateHealthCheckCount
Source:LocalDistributor.java
...350 LOG.log(351 getDebugLogLevel(),352 String.format("Health check result for %s was %s", nodeUri, availability));353 model.setAvailability(id, availability);354 model.updateHealthCheckCount(id, availability);355 } finally {356 writeLock.unlock();357 }358 }359 @Override360 public boolean drain(NodeId nodeId) {361 Node node = nodes.get(nodeId);362 if (node == null) {363 LOG.info("Asked to drain unregistered node " + nodeId);364 return false;365 }366 Lock writeLock = lock.writeLock();367 writeLock.lock();368 try {...
Source:GridModel.java
...85 LOG.log(Debug.getDebugLogLevel(), "Refreshing node with id %s", node.getNodeId());86 NodeStatus refreshed = rewrite(node, next.getAvailability());87 nodes.add(refreshed);88 nodePurgeTimes.put(refreshed.getNodeId(), Instant.now());89 updateHealthCheckCount(refreshed.getNodeId(), refreshed.getAvailability());90 return;91 }92 // If the URI is the same but NodeId is different, then the Node has restarted93 if(!next.getNodeId().equals(node.getNodeId()) &&94 next.getExternalUri().equals(node.getExternalUri())) {95 LOG.info(String.format("Re-adding node with id %s and URI %s.", node.getNodeId(), node.getExternalUri()));96 events.fire(new NodeRestartedEvent(node));97 iterator.remove();98 break;99 }100 // If the URI has changed, then assume this is a new node and fall101 // out of the loop: we want to add it as `DOWN` until something102 // changes our mind.103 if (next.getNodeId().equals(node.getNodeId())) {104 LOG.info(String.format("Re-adding node with id %s and URI %s.", node.getNodeId(), node.getExternalUri()));105 iterator.remove();106 break;107 }108 }109 // Nodes are initially added in the "down" state until something changes their availability110 LOG.log(111 Debug.getDebugLogLevel(),112 String.format("Adding node with id %s and URI %s", node.getNodeId(), node.getExternalUri()));113 NodeStatus refreshed = rewrite(node, DOWN);114 nodes.add(refreshed);115 nodePurgeTimes.put(refreshed.getNodeId(), Instant.now());116 updateHealthCheckCount(refreshed.getNodeId(), refreshed.getAvailability());117 } finally {118 writeLock.unlock();119 }120 }121 public void refresh(NodeStatus status) {122 Require.nonNull("Node status", status);123 Lock writeLock = lock.writeLock();124 writeLock.lock();125 try {126 Iterator<NodeStatus> iterator = nodes.iterator();127 while (iterator.hasNext()) {128 NodeStatus node = iterator.next();129 if (node.getNodeId().equals(status.getNodeId())) {130 iterator.remove();131 // if the node was marked as "down", keep it down until a healthcheck passes:132 // just because the node can hit the event bus doesn't mean it's reachable133 if (node.getAvailability() == DOWN) {134 nodes.add(rewrite(status, DOWN));135 } else {136 // Otherwise, trust what it tells us.137 nodes.add(status);138 }139 nodePurgeTimes.put(status.getNodeId(), Instant.now());140 return;141 }142 }143 } finally {144 writeLock.unlock();145 }146 }147 public void touch(NodeId id) {148 Require.nonNull("Node ID", id);149 Lock writeLock = lock.writeLock();150 writeLock.lock();151 try {152 NodeStatus node = getNode(id);153 if (node != null) {154 nodePurgeTimes.put(node.getNodeId(), Instant.now());155 }156 } finally {157 writeLock.unlock();158 }159 }160 public void remove(NodeId id) {161 Require.nonNull("Node ID", id);162 Lock writeLock = lock.writeLock();163 writeLock.lock();164 try {165 nodes.removeIf(n -> n.getNodeId().equals(id));166 nodePurgeTimes.remove(id);167 nodeHealthCount.remove(id);168 } finally {169 writeLock.unlock();170 }171 }172 public void purgeDeadNodes() {173 Lock writeLock = lock.writeLock();174 writeLock.lock();175 try {176 Map<NodeStatus, NodeStatus> replacements = new HashMap<>();177 Set<NodeStatus> toRemove = new HashSet<>();178 for (NodeStatus node : nodes) {179 NodeId id = node.getNodeId();180 if (nodeHealthCount.getOrDefault(id, 0) > UNHEALTHY_THRESHOLD) {181 toRemove.add(node);182 break;183 }184 Instant now = Instant.now();185 Instant lastTouched = nodePurgeTimes.getOrDefault(id, Instant.now());186 Instant lostTime = lastTouched.plus(node.getHeartbeatPeriod().multipliedBy(PURGE_TIMEOUT_MULTIPLIER / 2));187 Instant deadTime = lastTouched.plus(node.getHeartbeatPeriod().multipliedBy(PURGE_TIMEOUT_MULTIPLIER));188 if (node.getAvailability() == UP && lostTime.isBefore(now)) {189 LOG.info(String.format("Switching Node %s from UP to DOWN", node.getExternalUri()));190 replacements.put(node, rewrite(node, DOWN));191 }192 if (node.getAvailability() == DOWN && deadTime.isBefore(now)) {193 LOG.info(String.format("Removing Node %s, DOWN for too long", node.getExternalUri()));194 toRemove.add(node);195 }196 }197 replacements.forEach((before, after) -> {198 nodes.remove(before);199 nodes.add(after);200 });201 toRemove.forEach(node -> {202 nodes.remove(node);203 nodePurgeTimes.remove(node.getNodeId());204 nodeHealthCount.remove(node.getNodeId());205 events.fire(new NodeRemovedEvent(node));206 });207 } finally {208 writeLock.unlock();209 }210 }211 public Availability setAvailability(NodeId id, Availability availability) {212 Require.nonNull("Node ID", id);213 Require.nonNull("Availability", availability);214 Lock writeLock = lock.writeLock();215 writeLock.lock();216 try {217 NodeStatus node = getNode(id);218 if (node == null) {219 return DOWN;220 }221 if (availability.equals(node.getAvailability())) {222 if (node.getAvailability() == UP) {223 nodePurgeTimes.put(node.getNodeId(), Instant.now());224 }225 return availability;226 }227 LOG.info(String.format(228 "Switching node %s (uri: %s) from %s to %s",229 id,230 node.getExternalUri(),231 node.getAvailability(),232 availability));233 Availability previous = node.getAvailability();234 NodeStatus refreshed = rewrite(node, availability);235 nodes.remove(node);236 nodes.add(refreshed);237 nodePurgeTimes.put(node.getNodeId(), Instant.now());238 return previous;239 } finally {240 writeLock.unlock();241 }242 }243 public boolean reserve(SlotId slotId) {244 Lock writeLock = lock.writeLock();245 writeLock.lock();246 try {247 NodeStatus node = getNode(slotId.getOwningNodeId());248 if (node == null) {249 LOG.warning(String.format("Asked to reserve slot on node %s, but unable to find node", slotId.getOwningNodeId()));250 return false;251 }252 if (!UP.equals(node.getAvailability())) {253 LOG.warning(String.format(254 "Asked to reserve a slot on node %s, but node is %s",255 slotId.getOwningNodeId(),256 node.getAvailability()));257 return false;258 }259 Optional<Slot> maybeSlot = node.getSlots().stream()260 .filter(slot -> slotId.equals(slot.getId()))261 .findFirst();262 if (!maybeSlot.isPresent()) {263 LOG.warning(String.format(264 "Asked to reserve slot on node %s, but no slot with id %s found",265 node.getNodeId(),266 slotId));267 return false;268 }269 reserve(node, maybeSlot.get());270 return true;271 } finally {272 writeLock.unlock();273 }274 }275 public Set<NodeStatus> getSnapshot() {276 Lock readLock = this.lock.readLock();277 readLock.lock();278 try {279 return ImmutableSet.copyOf(nodes);280 } finally {281 readLock.unlock();282 }283 }284 private NodeStatus getNode(NodeId id) {285 Require.nonNull("Node ID", id);286 Lock readLock = lock.readLock();287 readLock.lock();288 try {289 return nodes.stream()290 .filter(n -> n.getNodeId().equals(id))291 .findFirst()292 .orElse(null);293 } finally {294 readLock.unlock();295 }296 }297 private NodeStatus rewrite(NodeStatus status, Availability availability) {298 return new NodeStatus(299 status.getNodeId(),300 status.getExternalUri(),301 status.getMaxSessionCount(),302 status.getSlots(),303 availability,304 status.getHeartbeatPeriod(),305 status.getVersion(),306 status.getOsInfo());307 }308 public void release(SessionId id) {309 if (id == null) {310 return;311 }312 Lock writeLock = lock.writeLock();313 writeLock.lock();314 try {315 for (NodeStatus node : nodes) {316 for (Slot slot : node.getSlots()) {317 if (slot.getSession()==null) {318 continue;319 }320 if (id.equals(slot.getSession().getId())) {321 Slot released = new Slot(322 slot.getId(),323 slot.getStereotype(),324 slot.getLastStarted(),325 null);326 amend(node.getAvailability(), node, released);327 return;328 }329 }330 }331 } finally {332 writeLock.unlock();333 }334 }335 public void reserve(NodeStatus status, Slot slot) {336 Instant now = Instant.now();337 Slot reserved = new Slot(338 slot.getId(),339 slot.getStereotype(),340 now,341 new Session(342 RESERVED,343 status.getExternalUri(),344 slot.getStereotype(),345 slot.getStereotype(),346 now));347 amend(UP, status, reserved);348 }349 public void setSession(SlotId slotId, Session session) {350 Require.nonNull("Slot ID", slotId);351 Lock writeLock = lock.writeLock();352 writeLock.lock();353 try {354 NodeStatus node = getNode(slotId.getOwningNodeId());355 if (node == null) {356 LOG.warning("Grid model and reality have diverged. Unable to find node " + slotId.getOwningNodeId());357 return;358 }359 Optional<Slot> maybeSlot = node.getSlots().stream()360 .filter(slot -> slotId.equals(slot.getId()))361 .findFirst();362 if (!maybeSlot.isPresent()) {363 LOG.warning("Grid model and reality have diverged. Unable to find slot " + slotId);364 return;365 }366 Slot slot = maybeSlot.get();367 Session maybeSession = slot.getSession();368 if (maybeSession == null) {369 LOG.warning("Grid model and reality have diverged. Slot is not reserved. " + slotId);370 return;371 }372 if (!RESERVED.equals(maybeSession.getId())) {373 LOG.warning(374 "Grid model and reality have diverged. Slot has session and is not reserved. " + slotId);375 return;376 }377 Slot updated = new Slot(378 slot.getId(),379 slot.getStereotype(),380 session == null ? slot.getLastStarted() : session.getStartTime(),381 session);382 amend(node.getAvailability(), node, updated);383 } finally {384 writeLock.unlock();385 }386 }387 public void updateHealthCheckCount(NodeId id, Availability availability) {388 Require.nonNull("Node ID", id);389 Require.nonNull("Availability", availability);390 Lock writeLock = lock.writeLock();391 writeLock.lock();392 try {393 int unhealthyCount = nodeHealthCount.getOrDefault(id, 0);394 // Keep track of consecutive number of times the Node health check fails395 if (availability.equals(DOWN)) {396 nodeHealthCount.put(id, unhealthyCount + 1);397 }398 // If the Node is healthy again before crossing the threshold, then reset the count.399 if (unhealthyCount <= UNHEALTHY_THRESHOLD && availability.equals(UP)) {400 nodeHealthCount.put(id, 0);401 }...
updateHealthCheckCount
Using AI Code Generation
1package org.openqa.selenium.grid.distributor;2import static org.openqa.selenium.grid.data.Availability.DOWN;3import static org.openqa.selenium.grid.data.Availability.UP;4import static org.openqa.selenium.grid.data.AvailabilityStatus.AVAILABLE;5import static org.openqa.selenium.grid.data.AvailabilityStatus.DRAINING;6import static org.openqa.selenium.grid.data.AvailabilityStatus.UNDER_MAINTENANCE;7import static org.openqa.selenium.grid.data.AvailabilityStatus.UNAVAILABLE;8import static org.openqa.selenium.grid.data.HealthCheckResults.HEALTHY;9import static org.openqa.selenium.grid.data.HealthCheckResults.UNHEALTHY;10import static org.openqa.selenium.grid.data.MaintenanceStatus.NONE;11import static org.openqa.selenium.remote.http.Contents.asJson;12import static org.openqa.selenium.remote.http.Contents.string;13import static org.openqa.selenium.remote.http.Filters.addResponseTrailer;14import static org.openqa.selenium.remote.http.Filters.setServerTrailer;15import static org.openqa.selenium.remote.http.HttpMethod.DELETE;16import static org.openqa.selenium.remote.http.HttpMethod.GET;17import static org.openqa.selenium.remote.http.HttpMethod.POST;18import static org.openqa.selenium.remote.http.HttpMethod.PUT;19import com.google.common.collect.ImmutableMap;20import com.google.common.collect.ImmutableSet;21import org.openqa.selenium.Capabilities;22import org.openqa.selenium.NoSuchSessionException;23import org.openqa.selenium.SessionNotCreatedException;24import org.openqa.selenium.WebDriverException;25import org.openqa.selenium.concurrent.Regularly;26import org.openqa.selenium.events.EventBus;27import org.openqa.selenium.grid.config.Config;28import org.openqa.selenium.grid.data.Availability;29import org.openqa.selenium.grid.data.AvailabilityStatus;30import org.openqa.selenium.grid.data.CreateSessionResponse;31import org.openqa.selenium.grid.data.DrainCompleteEvent;32import org.openqa.selenium.grid.data.DrainStartedEvent;33import org.openqa.selenium.grid.data.HealthCheckResults;34import org.openqa.selenium.grid.data.MaintenanceStatus;35import org.openqa.selenium.grid.data.NodeId;36import org.openqa.selenium.grid.data.NodeStatus;37import org.openqa.selenium.grid.data.NodeStatusEvent;38import org.openqa.selenium.grid.data.NodeStatusEvent.NodeStatusEventAction;39import org.openqa.selenium.grid.data.NodeStatusEvent.NodeStatusEventAction.Action;40import org.openqa.selenium.grid.data.NodeStatusEvent.NodeStatusEventAction.ActionType;41import org.openqa.selenium.grid.data.NodeStatusEvent.NodeStatusEventAction.NodeStatusEventActionBuilder;42import org.openqa.selenium.grid.data.NodeStatusEvent.NodeStatusEventAction.NodeStatusEventActionBuilder.NodeStatusEventActionBuilderImpl;43import org.openqa.selenium.grid.data.NodeStatusEvent.NodeStatusEventAction.NodeStatusEventAction
updateHealthCheckCount
Using AI Code Generation
1package com.selenium4beginners.java.grid;2import java.util.Map;3import org.openqa.selenium.grid.config.MapConfig;4import org.openqa.selenium.grid.distributor.Distributor;5import org.openqa.selenium.grid.distributor.local.LocalDistributor;6import org.openqa.selenium.grid.distributor.model.DistributorStatus;7import org.openqa.selenium.grid.node.local.LocalNode;8import org.openqa.selenium.remote.http.HttpClient;9public class UpdateHealthCheckCount {10 public static void main(String[] args) {11 Distributor distributor = new LocalDistributor(new MapConfig(Map.of()), HttpClient.Factory.createDefault());12 LocalNode node = LocalNode.builder(distributor, new MapConfig(Map.of())).build();13 node.start();14 distributor.updateHealthCheckCount(1);15 DistributorStatus status = distributor.getStatus();16 System.out.println(status.getHealthCheckCount());17 node.stop();18 distributor.stop();19 }20}
updateHealthCheckCount
Using AI Code Generation
1public void updateHealthCheckCount(Host host, int healthCheckCount) {2 Objects.requireNonNull(host);3 Objects.requireNonNull(healthCheckCount);4 this.hosts.computeIfPresent(host.getId(), (id, h) -> new Host(h.getId(), h.getUri(), healthCheckCount));5}6public void updateHealthCheckCount(Host host, int healthCheckCount) {7 Objects.requireNonNull(host);8 Objects.requireNonNull(healthCheckCount);9 this.hosts.computeIfPresent(host.getId(), (id, h) -> new Host(h.getId(), h.getUri(), healthCheckCount));10}11public void updateHealthCheckCount(Host host, int healthCheckCount) {12 Objects.requireNonNull(host);13 Objects.requireNonNull(healthCheckCount);14 this.hosts.computeIfPresent(host.getId(), (id, h) -> new Host(h.getId(), h.getUri(), healthCheckCount));15}16public void updateHealthCheckCount(Host host, int healthCheckCount) {17 Objects.requireNonNull(host);18 Objects.requireNonNull(healthCheckCount);19 this.hosts.computeIfPresent(host.getId(), (id, h) -> new Host(h.getId(), h.getUri(), healthCheckCount));20}21public void updateHealthCheckCount(Host host, int healthCheckCount) {22 Objects.requireNonNull(host);23 Objects.requireNonNull(healthCheckCount);24 this.hosts.computeIfPresent(host.getId(), (id, h) -> new Host(h.getId(), h.getUri(), healthCheckCount));25}26public void updateHealthCheckCount(Host host, int healthCheckCount) {27 Objects.requireNonNull(host);28 Objects.requireNonNull(healthCheckCount);29 this.hosts.computeIfPresent(host.getId(), (id, h) -> new Host(h.getId(), h.getUri(), healthCheckCount));30}31public void updateHealthCheckCount(Host host, int healthCheckCount) {32 Objects.requireNonNull(host);33 Objects.requireNonNull(healthCheckCount);34 this.hosts.computeIfPresent(host.getId(), (id, h) -> new Host(h.getId(), h.getUri(), healthCheckCount));35}36public void updateHealthCheckCount(Host host, int healthCheckCount) {37 Objects.requireNonNull(host);38 Objects.requireNonNull(healthCheckCount);39 this.hosts.computeIfPresent(host.getId(), (id, h) -> new Host(h.getId(), h.getUri(), healthCheckCount));40}41public void updateHealthCheckCount(Host host, int healthCheckCount) {42 Objects.requireNonNull(host);43 Objects.requireNonNull(health
LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.
Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.
What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.
Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.
Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.
How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.
Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.
Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.
LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!