Best Selenium code snippet using org.openqa.selenium.grid.data.TraceSessionRequest
Source:LocalDistributor.java
...39import org.openqa.selenium.grid.data.SessionRequest;40import org.openqa.selenium.grid.data.SessionRequestCapability;41import org.openqa.selenium.grid.data.Slot;42import org.openqa.selenium.grid.data.SlotId;43import org.openqa.selenium.grid.data.TraceSessionRequest;44import org.openqa.selenium.grid.distributor.Distributor;45import org.openqa.selenium.grid.distributor.config.DistributorOptions;46import org.openqa.selenium.grid.distributor.selector.SlotSelector;47import org.openqa.selenium.grid.log.LoggingOptions;48import org.openqa.selenium.grid.node.HealthCheck;49import org.openqa.selenium.grid.node.Node;50import org.openqa.selenium.grid.node.remote.RemoteNode;51import org.openqa.selenium.grid.security.Secret;52import org.openqa.selenium.grid.security.SecretOptions;53import org.openqa.selenium.grid.server.EventBusOptions;54import org.openqa.selenium.grid.server.NetworkOptions;55import org.openqa.selenium.grid.sessionmap.SessionMap;56import org.openqa.selenium.grid.sessionmap.config.SessionMapOptions;57import org.openqa.selenium.grid.sessionqueue.NewSessionQueue;58import org.openqa.selenium.grid.sessionqueue.config.NewSessionQueueOptions;59import org.openqa.selenium.internal.Either;60import org.openqa.selenium.internal.Require;61import org.openqa.selenium.remote.SessionId;62import org.openqa.selenium.remote.http.HttpClient;63import org.openqa.selenium.remote.tracing.AttributeKey;64import org.openqa.selenium.remote.tracing.EventAttribute;65import org.openqa.selenium.remote.tracing.EventAttributeValue;66import org.openqa.selenium.remote.tracing.Span;67import org.openqa.selenium.remote.tracing.Status;68import org.openqa.selenium.remote.tracing.Tracer;69import org.openqa.selenium.status.HasReadyState;70import java.io.UncheckedIOException;71import java.time.Duration;72import java.util.ArrayList;73import java.util.Collection;74import java.util.HashMap;75import java.util.List;76import java.util.Map;77import java.util.Optional;78import java.util.Set;79import java.util.concurrent.ConcurrentHashMap;80import java.util.concurrent.Executors;81import java.util.concurrent.locks.Lock;82import java.util.concurrent.locks.ReadWriteLock;83import java.util.concurrent.locks.ReentrantReadWriteLock;84import java.util.logging.Level;85import java.util.logging.Logger;86import java.util.stream.Collectors;87import static com.google.common.collect.ImmutableSet.toImmutableSet;88import static org.openqa.selenium.grid.data.Availability.DOWN;89import static org.openqa.selenium.grid.data.Availability.DRAINING;90import static org.openqa.selenium.internal.Debug.getDebugLogLevel;91import static org.openqa.selenium.remote.RemoteTags.CAPABILITIES;92import static org.openqa.selenium.remote.RemoteTags.CAPABILITIES_EVENT;93import static org.openqa.selenium.remote.RemoteTags.SESSION_ID;94import static org.openqa.selenium.remote.RemoteTags.SESSION_ID_EVENT;95import static org.openqa.selenium.remote.tracing.AttributeKey.SESSION_URI;96import static org.openqa.selenium.remote.tracing.Tags.EXCEPTION;97public class LocalDistributor extends Distributor {98 private static final Logger LOG = Logger.getLogger(LocalDistributor.class.getName());99 private final Tracer tracer;100 private final EventBus bus;101 private final HttpClient.Factory clientFactory;102 private final SessionMap sessions;103 private final SlotSelector slotSelector;104 private final Secret registrationSecret;105 private final Regularly hostChecker = new Regularly("distributor host checker");106 private final Map<NodeId, Runnable> allChecks = new HashMap<>();107 private final Duration healthcheckInterval;108 private final ReadWriteLock lock = new ReentrantReadWriteLock(/* fair */ true);109 private final GridModel model;110 private final Map<NodeId, Node> nodes;111 private final NewSessionQueue sessionQueue;112 private final Regularly regularly;113 private final boolean rejectUnsupportedCaps;114 public LocalDistributor(115 Tracer tracer,116 EventBus bus,117 HttpClient.Factory clientFactory,118 SessionMap sessions,119 NewSessionQueue sessionQueue,120 SlotSelector slotSelector,121 Secret registrationSecret,122 Duration healthcheckInterval,123 boolean rejectUnsupportedCaps) {124 super(tracer, clientFactory, registrationSecret);125 this.tracer = Require.nonNull("Tracer", tracer);126 this.bus = Require.nonNull("Event bus", bus);127 this.clientFactory = Require.nonNull("HTTP client factory", clientFactory);128 this.sessions = Require.nonNull("Session map", sessions);129 this.sessionQueue = Require.nonNull("New Session Request Queue", sessionQueue);130 this.slotSelector = Require.nonNull("Slot selector", slotSelector);131 this.registrationSecret = Require.nonNull("Registration secret", registrationSecret);132 this.healthcheckInterval = Require.nonNull("Health check interval", healthcheckInterval);133 this.model = new GridModel(bus);134 this.nodes = new ConcurrentHashMap<>();135 this.rejectUnsupportedCaps = rejectUnsupportedCaps;136 bus.addListener(NodeStatusEvent.listener(this::register));137 bus.addListener(NodeStatusEvent.listener(model::refresh));138 bus.addListener(NodeHeartBeatEvent.listener(nodeStatus -> {139 if (nodes.containsKey(nodeStatus.getId())) {140 model.touch(nodeStatus.getId());141 } else {142 register(nodeStatus);143 }144 }));145 regularly = new Regularly(146 Executors.newSingleThreadScheduledExecutor(147 r -> {148 Thread thread = new Thread(r);149 thread.setName("New Session Queue");150 thread.setDaemon(true);151 return thread;152 }));153 NewSessionRunnable newSessionRunnable = new NewSessionRunnable();154 bus.addListener(NodeDrainComplete.listener(this::remove));155 bus.addListener(NewSessionRequestEvent.listener(ignored -> newSessionRunnable.run()));156 regularly.submit(model::purgeDeadNodes, Duration.ofSeconds(30), Duration.ofSeconds(30));157 regularly.submit(newSessionRunnable, Duration.ofSeconds(5), Duration.ofSeconds(5));158 }159 public static Distributor create(Config config) {160 Tracer tracer = new LoggingOptions(config).getTracer();161 EventBus bus = new EventBusOptions(config).getEventBus();162 DistributorOptions distributorOptions = new DistributorOptions(config);163 HttpClient.Factory clientFactory = new NetworkOptions(config).getHttpClientFactory(tracer);164 SessionMap sessions = new SessionMapOptions(config).getSessionMap();165 SecretOptions secretOptions = new SecretOptions(config);166 NewSessionQueue sessionQueue = new NewSessionQueueOptions(config).getSessionQueue(167 "org.openqa.selenium.grid.sessionqueue.remote.RemoteNewSessionQueue");168 return new LocalDistributor(169 tracer,170 bus,171 clientFactory,172 sessions,173 sessionQueue,174 distributorOptions.getSlotSelector(),175 secretOptions.getRegistrationSecret(),176 distributorOptions.getHealthCheckInterval(),177 distributorOptions.shouldRejectUnsupportedCaps());178 }179 @Override180 public boolean isReady() {181 try {182 return ImmutableSet.of(bus, sessions).parallelStream()183 .map(HasReadyState::isReady)184 .reduce(true, Boolean::logicalAnd);185 } catch (RuntimeException e) {186 return false;187 }188 }189 private void register(NodeStatus status) {190 Require.nonNull("Node", status);191 Lock writeLock = lock.writeLock();192 writeLock.lock();193 try {194 if (nodes.containsKey(status.getId())) {195 return;196 }197 Set<Capabilities> capabilities = status.getSlots().stream()198 .map(Slot::getStereotype)199 .map(ImmutableCapabilities::copyOf)200 .collect(toImmutableSet());201 // A new node! Add this as a remote node, since we've not called add202 RemoteNode remoteNode = new RemoteNode(203 tracer,204 clientFactory,205 status.getId(),206 status.getUri(),207 registrationSecret,208 capabilities);209 add(remoteNode);210 } finally {211 writeLock.unlock();212 }213 }214 @Override215 public LocalDistributor add(Node node) {216 Require.nonNull("Node", node);217 LOG.info(String.format("Added node %s at %s.", node.getId(), node.getUri()));218 nodes.put(node.getId(), node);219 model.add(node.getStatus());220 // Extract the health check221 Runnable runnableHealthCheck = asRunnableHealthCheck(node);222 allChecks.put(node.getId(), runnableHealthCheck);223 hostChecker.submit(runnableHealthCheck, healthcheckInterval, Duration.ofSeconds(30));224 bus.fire(new NodeAddedEvent(node.getId()));225 return this;226 }227 private Runnable asRunnableHealthCheck(Node node) {228 HealthCheck healthCheck = node.getHealthCheck();229 NodeId id = node.getId();230 return () -> {231 HealthCheck.Result result;232 try {233 result = healthCheck.check();234 } catch (Exception e) {235 LOG.log(Level.WARNING, "Unable to process node " + id, e);236 result = new HealthCheck.Result(DOWN, "Unable to run healthcheck. Assuming down");237 }238 Lock writeLock = lock.writeLock();239 writeLock.lock();240 try {241 model.setAvailability(id, result.getAvailability());242 } finally {243 writeLock.unlock();244 }245 };246 }247 @Override248 public boolean drain(NodeId nodeId) {249 Node node = nodes.get(nodeId);250 if (node == null) {251 LOG.info("Asked to drain unregistered node " + nodeId);252 return false;253 }254 Lock writeLock = lock.writeLock();255 writeLock.lock();256 try {257 node.drain();258 model.setAvailability(nodeId, DRAINING);259 } finally {260 writeLock.unlock();261 }262 return node.isDraining();263 }264 public void remove(NodeId nodeId) {265 Lock writeLock = lock.writeLock();266 writeLock.lock();267 try {268 model.remove(nodeId);269 Runnable runnable = allChecks.remove(nodeId);270 if (runnable != null) {271 hostChecker.remove(runnable);272 }273 } finally {274 writeLock.unlock();275 }276 }277 @Override278 public DistributorStatus getStatus() {279 Lock readLock = this.lock.readLock();280 readLock.lock();281 try {282 return new DistributorStatus(model.getSnapshot());283 } finally {284 readLock.unlock();285 }286 }287 @Beta288 public void refresh() {289 List<Runnable> allHealthChecks = new ArrayList<>();290 Lock readLock = this.lock.readLock();291 readLock.lock();292 try {293 allHealthChecks.addAll(allChecks.values());294 } finally {295 readLock.unlock();296 }297 allHealthChecks.parallelStream().forEach(Runnable::run);298 }299 protected Set<NodeStatus> getAvailableNodes() {300 Lock readLock = this.lock.readLock();301 readLock.lock();302 try {303 return model.getSnapshot().stream()304 .filter(node -> !DOWN.equals(node.getAvailability()))305 .collect(toImmutableSet());306 } finally {307 readLock.unlock();308 }309 }310 @Override311 public Either<SessionNotCreatedException, CreateSessionResponse> newSession(SessionRequest request)312 throws SessionNotCreatedException {313 Require.nonNull("Requests to process", request);314 Span span = tracer.getCurrentContext().createSpan("distributor.new_session");315 Map<String, EventAttributeValue> attributeMap = new HashMap<>();316 try {317 attributeMap.put(AttributeKey.LOGGER_CLASS.getKey(),318 EventAttribute.setValue(getClass().getName()));319 attributeMap.put("request.payload", EventAttribute.setValue(request.getDesiredCapabilities().toString()));320 String sessionReceivedMessage = "Session request received by the distributor";321 span.addEvent(sessionReceivedMessage, attributeMap);322 LOG.info(String.format("%s: \n %s", sessionReceivedMessage, request.getDesiredCapabilities()));323 // If there are no capabilities at all, something is horribly wrong324 if (request.getDesiredCapabilities().isEmpty()) {325 SessionNotCreatedException exception =326 new SessionNotCreatedException("No capabilities found in session request payload");327 EXCEPTION.accept(attributeMap, exception);328 attributeMap.put(AttributeKey.EXCEPTION_MESSAGE.getKey(),329 EventAttribute.setValue("Unable to create session. No capabilities found: " +330 exception.getMessage()));331 span.addEvent(AttributeKey.EXCEPTION_EVENT.getKey(), attributeMap);332 return Either.left(exception);333 }334 boolean retry = false;335 SessionNotCreatedException lastFailure = new SessionNotCreatedException("Unable to create new session");336 for (Capabilities caps : request.getDesiredCapabilities()) {337 if (!isSupported(caps)) {338 continue;339 }340 // Try and find a slot that we can use for this session. While we341 // are finding the slot, no other session can possibly be started.342 // Therefore, spend as little time as possible holding the write343 // lock, and release it as quickly as possible. Under no344 // circumstances should we try to actually start the session itself345 // in this next block of code.346 SlotId selectedSlot = reserveSlot(request.getRequestId(), caps);347 if (selectedSlot == null) {348 LOG.info(String.format("Unable to find slot for request %s. May retry: %s ", request.getRequestId(), caps));349 retry = true;350 continue;351 }352 CreateSessionRequest singleRequest = new CreateSessionRequest(353 request.getDownstreamDialects(),354 caps,355 request.getMetadata());356 try {357 CreateSessionResponse response = startSession(selectedSlot, singleRequest);358 sessions.add(response.getSession());359 model.setSession(selectedSlot, response.getSession());360 SessionId sessionId = response.getSession().getId();361 Capabilities sessionCaps = response.getSession().getCapabilities();362 String sessionUri = response.getSession().getUri().toString();363 SESSION_ID.accept(span, sessionId);364 CAPABILITIES.accept(span, sessionCaps);365 SESSION_ID_EVENT.accept(attributeMap, sessionId);366 CAPABILITIES_EVENT.accept(attributeMap, sessionCaps);367 span.setAttribute(SESSION_URI.getKey(), sessionUri);368 attributeMap.put(SESSION_URI.getKey(), EventAttribute.setValue(sessionUri));369 String sessionCreatedMessage = "Session created by the distributor";370 span.addEvent(sessionCreatedMessage, attributeMap);371 LOG.info(String.format("%s. Id: %s, Caps: %s", sessionCreatedMessage, sessionId, sessionCaps));372 return Either.right(response);373 } catch (SessionNotCreatedException e) {374 model.setSession(selectedSlot, null);375 lastFailure = e;376 }377 }378 // If we've made it this far, we've not been able to start a session379 if (retry) {380 lastFailure = new RetrySessionRequestException(381 "Will re-attempt to find a node which can run this session",382 lastFailure);383 attributeMap.put(384 AttributeKey.EXCEPTION_MESSAGE.getKey(),385 EventAttribute.setValue("Will retry session " + request.getRequestId()));386 } else {387 EXCEPTION.accept(attributeMap, lastFailure);388 attributeMap.put(AttributeKey.EXCEPTION_MESSAGE.getKey(),389 EventAttribute.setValue("Unable to create session: " + lastFailure.getMessage()));390 span.addEvent(AttributeKey.EXCEPTION_EVENT.getKey(), attributeMap);391 }392 return Either.left(lastFailure);393 } catch (SessionNotCreatedException e) {394 span.setAttribute(AttributeKey.ERROR.getKey(), true);395 span.setStatus(Status.ABORTED);396 EXCEPTION.accept(attributeMap, e);397 attributeMap.put(AttributeKey.EXCEPTION_MESSAGE.getKey(),398 EventAttribute.setValue("Unable to create session: " + e.getMessage()));399 span.addEvent(AttributeKey.EXCEPTION_EVENT.getKey(), attributeMap);400 return Either.left(e);401 } catch (UncheckedIOException e) {402 span.setAttribute(AttributeKey.ERROR.getKey(), true);403 span.setStatus(Status.UNKNOWN);404 EXCEPTION.accept(attributeMap, e);405 attributeMap.put(AttributeKey.EXCEPTION_MESSAGE.getKey(),406 EventAttribute.setValue("Unknown error in LocalDistributor while creating session: " + e.getMessage()));407 span.addEvent(AttributeKey.EXCEPTION_EVENT.getKey(), attributeMap);408 return Either.left(new SessionNotCreatedException(e.getMessage(), e));409 } finally {410 span.close();411 }412 }413 private CreateSessionResponse startSession(SlotId selectedSlot, CreateSessionRequest singleRequest) {414 Node node = nodes.get(selectedSlot.getOwningNodeId());415 if (node == null) {416 throw new SessionNotCreatedException("Unable to find owning node for slot");417 }418 Either<WebDriverException, CreateSessionResponse> result;419 try {420 result = node.newSession(singleRequest);421 } catch (SessionNotCreatedException e) {422 result = Either.left(e);423 } catch (RuntimeException e) {424 result = Either.left(new SessionNotCreatedException(e.getMessage(), e));425 }426 if (result.isLeft()) {427 WebDriverException exception = result.left();428 if (exception instanceof SessionNotCreatedException) {429 throw exception;430 }431 throw new SessionNotCreatedException(exception.getMessage(), exception);432 }433 return result.right();434 }435 private SlotId reserveSlot(RequestId requestId, Capabilities caps) {436 Lock writeLock = lock.writeLock();437 writeLock.lock();438 try {439 Set<SlotId> slotIds = slotSelector.selectSlot(caps, getAvailableNodes());440 if (slotIds.isEmpty()) {441 LOG.log(442 getDebugLogLevel(),443 String.format("No slots found for request %s and capabilities %s", requestId, caps));444 return null;445 }446 for (SlotId slotId : slotIds) {447 if (reserve(slotId)) {448 return slotId;449 }450 }451 return null;452 } finally {453 writeLock.unlock();454 }455 }456 private boolean isSupported(Capabilities caps) {457 return getAvailableNodes().stream().anyMatch(node -> node.hasCapability(caps));458 }459 private boolean reserve(SlotId id) {460 Require.nonNull("Slot ID", id);461 Lock writeLock = this.lock.writeLock();462 writeLock.lock();463 try {464 Node node = nodes.get(id.getOwningNodeId());465 if (node == null) {466 LOG.log(getDebugLogLevel(), String.format("Unable to find node with id %s", id));467 return false;468 }469 return model.reserve(id);470 } finally {471 writeLock.unlock();472 }473 }474 public void callExecutorShutdown() {475 LOG.info("Shutting down Distributor executor service");476 regularly.shutdown();477 }478 public class NewSessionRunnable implements Runnable {479 @Override480 public void run() {481 List<SessionRequestCapability> queueContents = sessionQueue.getQueueContents();482 if (rejectUnsupportedCaps) {483 checkMatchingSlot(queueContents);484 }485 int initialSize = queueContents.size();486 boolean retry = initialSize != 0;487 while (retry) {488 // We deliberately run this outside of a lock: if we're unsuccessful489 // starting the session, we just put the request back on the queue.490 // This does mean, however, that under high contention, we might end491 // up starving a session request.492 Set<Capabilities> stereotypes =493 getAvailableNodes().stream()494 .filter(NodeStatus::hasCapacity)495 .map(496 node ->497 node.getSlots().stream()498 .map(Slot::getStereotype)499 .collect(Collectors.toSet()))500 .flatMap(Collection::stream)501 .collect(Collectors.toSet());502 Optional<SessionRequest> maybeRequest = sessionQueue.getNextAvailable(stereotypes);503 maybeRequest.ifPresent(this::handleNewSessionRequest);504 int currentSize = sessionQueue.getQueueContents().size();505 retry = currentSize != 0 && currentSize != initialSize;506 initialSize = currentSize;507 }508 }509 private void checkMatchingSlot(List<SessionRequestCapability> sessionRequests) {510 for(SessionRequestCapability request : sessionRequests) {511 long unmatchableCount = request.getDesiredCapabilities().stream()512 .filter(caps -> !isSupported(caps))513 .count();514 if (unmatchableCount == request.getDesiredCapabilities().size()) {515 SessionNotCreatedException exception = new SessionNotCreatedException(516 "No nodes support the capabilities in the request");517 sessionQueue.complete(request.getRequestId(), Either.left(exception));518 }519 }520 }521 private void handleNewSessionRequest(SessionRequest sessionRequest) {522 RequestId reqId = sessionRequest.getRequestId();523 try (Span span = TraceSessionRequest.extract(tracer, sessionRequest).createSpan("distributor.poll_queue")) {524 Map<String, EventAttributeValue> attributeMap = new HashMap<>();525 attributeMap.put(526 AttributeKey.LOGGER_CLASS.getKey(),527 EventAttribute.setValue(getClass().getName()));528 span.setAttribute(AttributeKey.REQUEST_ID.getKey(), reqId.toString());529 attributeMap.put(530 AttributeKey.REQUEST_ID.getKey(),531 EventAttribute.setValue(reqId.toString()));532 attributeMap.put("request", EventAttribute.setValue(sessionRequest.toString()));533 Either<SessionNotCreatedException, CreateSessionResponse> response = newSession(sessionRequest);534 if (response.isLeft() && response.left() instanceof RetrySessionRequestException) {535 try(Span childSpan = span.createSpan("distributor.retry")) {536 LOG.info("Retrying");537 boolean retried = sessionQueue.retryAddToQueue(sessionRequest);...
Source:LocalNewSessionQueue.java
...10import org.openqa.selenium.grid.data.NewSessionRequestEvent;11import org.openqa.selenium.grid.data.RequestId;12import org.openqa.selenium.grid.data.SessionRequest;13import org.openqa.selenium.grid.data.SessionRequestCapability;14import org.openqa.selenium.grid.data.TraceSessionRequest;15import org.openqa.selenium.grid.data.SlotMatcher;16import org.openqa.selenium.grid.distributor.config.DistributorOptions;17import org.openqa.selenium.grid.jmx.JMXHelper;18import org.openqa.selenium.grid.jmx.ManagedAttribute;19import org.openqa.selenium.grid.jmx.ManagedService;20import org.openqa.selenium.grid.log.LoggingOptions;21import org.openqa.selenium.grid.security.Secret;22import org.openqa.selenium.grid.security.SecretOptions;23import org.openqa.selenium.grid.server.EventBusOptions;24import org.openqa.selenium.grid.sessionqueue.NewSessionQueue;25import org.openqa.selenium.grid.sessionqueue.config.SessionRequestOptions;26import org.openqa.selenium.internal.Either;27import org.openqa.selenium.internal.Require;28import org.openqa.selenium.remote.http.Contents;29import org.openqa.selenium.remote.http.HttpResponse;30import org.openqa.selenium.remote.tracing.Span;31import org.openqa.selenium.remote.tracing.TraceContext;32import org.openqa.selenium.remote.tracing.Tracer;33import java.io.Closeable;34import java.io.IOException;35import java.time.Duration;36import java.time.Instant;37import java.util.Collections;38import java.util.Deque;39import java.util.Iterator;40import java.util.List;41import java.util.Map;42import java.util.Optional;43import java.util.Set;44import java.util.concurrent.ConcurrentHashMap;45import java.util.concurrent.ConcurrentLinkedDeque;46import java.util.concurrent.CountDownLatch;47import java.util.concurrent.Executors;48import java.util.concurrent.ScheduledExecutorService;49import java.util.concurrent.locks.Lock;50import java.util.concurrent.locks.ReadWriteLock;51import java.util.concurrent.locks.ReentrantReadWriteLock;52import java.util.function.Predicate;53import java.util.stream.Collectors;54import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;55import static java.util.concurrent.TimeUnit.MILLISECONDS;56/**57 * An in-memory implementation of the list of new session requests.58 * <p>59 * The lifecycle of a request can be described as:60 * <ol>61 * <li>User adds an item on to the queue using {@link #addToQueue(SessionRequest)}. This62 * will block until the request completes in some way.63 * <li>After being added, a {@link NewSessionRequestEvent} is fired. Listeners should use64 * this as an indication to call {@link #remove(RequestId)} to get the session request.65 * <li>If the session request is completed, then {@link #complete(RequestId, Either)} must66 * be called. This will not only ensure that {@link #addToQueue(SessionRequest)}67 * returns, but will also fire a {@link NewSessionRejectedEvent} if the session was68 * rejected. Positive completions of events are assumed to be notified on the event bus69 * by other listeners.70 * <li>If the request cannot be handled right now, call71 * {@link #retryAddToQueue(SessionRequest)} to return the session request to the front72 * of the queue.73 * </ol>74 * <p>75 * There is a background thread that will reap {@link SessionRequest}s that have timed out.76 * This means that a request can either complete by a listener calling77 * {@link #complete(RequestId, Either)} directly, or by being reaped by the thread.78 */79@ManagedService(objectName = "org.seleniumhq.grid:type=SessionQueue,name=LocalSessionQueue",80 description = "New session queue")81public class LocalNewSessionQueue extends NewSessionQueue implements Closeable {82 private final EventBus bus;83 private final SlotMatcher slotMatcher;84 private final Duration requestTimeout;85 private final Map<RequestId, Data> requests;86 private final Map<RequestId, TraceContext> contexts;87 private final Deque<SessionRequest> queue;88 private final ReadWriteLock lock = new ReentrantReadWriteLock();89 private final ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(r -> {90 Thread thread = new Thread(r);91 thread.setDaemon(true);92 thread.setName("Local New Session Queue");93 return thread;94 });95 public LocalNewSessionQueue(96 Tracer tracer,97 EventBus bus,98 SlotMatcher slotMatcher,99 Duration retryPeriod,100 Duration requestTimeout,101 Secret registrationSecret) {102 super(tracer, registrationSecret);103 this.slotMatcher = Require.nonNull("Slot matcher", slotMatcher);104 this.bus = Require.nonNull("Event bus", bus);105 Require.nonNull("Retry period", retryPeriod);106 if (retryPeriod.isNegative() || retryPeriod.isZero()) {107 throw new IllegalArgumentException("Retry period must be positive");108 }109 this.requestTimeout = Require.nonNull("Request timeout", requestTimeout);110 if (requestTimeout.isNegative() || requestTimeout.isZero()) {111 throw new IllegalArgumentException("Request timeout must be positive");112 }113 this.requests = new ConcurrentHashMap<>();114 this.queue = new ConcurrentLinkedDeque<>();115 this.contexts = new ConcurrentHashMap<>();116 service.scheduleAtFixedRate(this::timeoutSessions, retryPeriod.toMillis(), retryPeriod.toMillis(), MILLISECONDS);117 new JMXHelper().register(this);118 }119 public static NewSessionQueue create(Config config) {120 LoggingOptions loggingOptions = new LoggingOptions(config);121 Tracer tracer = loggingOptions.getTracer();122 EventBusOptions eventBusOptions = new EventBusOptions(config);123 SessionRequestOptions requestOptions = new SessionRequestOptions(config);124 SecretOptions secretOptions = new SecretOptions(config);125 SlotMatcher slotMatcher = new DistributorOptions(config).getSlotMatcher();126 return new LocalNewSessionQueue(127 tracer,128 eventBusOptions.getEventBus(),129 slotMatcher,130 requestOptions.getSessionRequestRetryInterval(),131 requestOptions.getSessionRequestTimeout(),132 secretOptions.getRegistrationSecret());133 }134 private void timeoutSessions() {135 Instant now = Instant.now();136 Lock readLock = lock.readLock();137 readLock.lock();138 Set<RequestId> ids;139 try {140 ids = requests.entrySet().stream()141 .filter(entry -> isTimedOut(now, entry.getValue()))142 .map(Map.Entry::getKey)143 .collect(Collectors.toSet());144 } finally {145 readLock.unlock();146 }147 Lock writeLock = lock.writeLock();148 try {149 for (RequestId id : ids) {150 failDueToTimeout(id);151 }152 } finally {153 writeLock.unlock();154 }155 }156 private boolean isTimedOut(Instant now, Data data) {157 return data.endTime.isBefore(now);158 }159 @Override160 public HttpResponse addToQueue(SessionRequest request) {161 Require.nonNull("New session request", request);162 Require.nonNull("Request id", request.getRequestId());163 TraceContext context = TraceSessionRequest.extract(tracer, request);164 try (Span span = context.createSpan("sessionqueue.add_to_queue")) {165 contexts.put(request.getRequestId(), context);166 Data data = injectIntoQueue(request);167 if (isTimedOut(Instant.now(), data)) {168 failDueToTimeout(request.getRequestId());169 }170 Either<SessionNotCreatedException, CreateSessionResponse> result;171 try {172 if (data.latch.await(requestTimeout.toMillis(), MILLISECONDS)) {173 result = data.result;174 } else {175 result = Either.left(new SessionNotCreatedException("New session request timed out"));176 }177 } catch (InterruptedException e) {...
Source:TraceSessionRequest.java
1package org.openqa.selenium.grid.data;2import org.openqa.selenium.internal.Require;3import org.openqa.selenium.remote.tracing.TraceContext;4import org.openqa.selenium.remote.tracing.Tracer;5public class TraceSessionRequest {6 private TraceSessionRequest() {7 // Utility methods8 }9 public static TraceContext extract(Tracer tracer, SessionRequest sessionRequest) {10 Require.nonNull("Tracer", tracer);11 Require.nonNull("Session request", sessionRequest);12 return tracer.getPropagator()13 .extractContext(14 tracer.getCurrentContext(),15 sessionRequest,16 SessionRequest::getTraceHeader);17 }18}...
TraceSessionRequest
Using AI Code Generation
1TraceSessionRequest traceSessionRequest = new TraceSessionRequest("traceId", "sessionId");2TraceSessionResponse traceSessionResponse = new TraceSessionResponse("traceId", "sessionId", "status");3TraceSessionRequest traceSessionRequest = new TraceSessionRequest("traceId", "sessionId");4TraceSessionResponse traceSessionResponse = new TraceSessionResponse("traceId", "sessionId", "status");5TraceSessionRequest traceSessionRequest = new TraceSessionRequest("traceId", "sessionId");6TraceSessionResponse traceSessionResponse = new TraceSessionResponse("traceId", "sessionId", "status");7TraceSessionRequest traceSessionRequest = new TraceSessionRequest("traceId", "sessionId");8TraceSessionResponse traceSessionResponse = new TraceSessionResponse("traceId", "sessionId", "status");9TraceSessionRequest traceSessionRequest = new TraceSessionRequest("traceId", "sessionId");10TraceSessionResponse traceSessionResponse = new TraceSessionResponse("traceId", "sessionId", "status");11TraceSessionRequest traceSessionRequest = new TraceSessionRequest("traceId", "sessionId");12TraceSessionResponse traceSessionResponse = new TraceSessionResponse("traceId", "sessionId", "status");13TraceSessionRequest traceSessionRequest = new TraceSessionRequest("traceId", "sessionId");14TraceSessionResponse traceSessionResponse = new TraceSessionResponse("traceId", "sessionId", "status");
TraceSessionRequest
Using AI Code Generation
1import org.openqa.selenium.grid.data.TraceSessionRequest;2import org.openqa.selenium.grid.data.TraceSessionRequest;3import org.openqa.selenium.grid.data.TraceSessionRequest;4import org.openqa.selenium.grid.data.TraceSessionRequest;5import org.openqa.selenium.grid.data.TraceSessionRequest;6import org.openqa.selenium.grid.data.TraceSessionRequest;7import org.openqa.selenium.grid.data.TraceSessionRequest;8import org.openqa.selenium.grid.data.TraceSessionRequest;9import org.openqa.selenium.grid.data.TraceSessionRequest;10import org.openqa.selenium.grid.data.TraceSessionRequest;
TraceSessionRequest
Using AI Code Generation
1TraceSessionRequest traceSessionRequest = new TraceSessionRequest();2traceSessionRequest.setTraceType(TraceType.CLIENT);3traceSessionRequest.setDownstreamDialects("W3C");4traceSessionRequest.setDownstreamStatusCodes("200");5traceSessionRequest.setDownstreamUri("/session");6traceSessionRequest.setUpstreamDialects("W3C");7traceSessionRequest.setUpstreamStatusCodes("200");8traceSessionRequest.setUpstreamUri("/session");9TraceSessionResponse traceSessionResponse = new TraceSessionResponse();10traceSessionResponse.setDownstreamDialects("W3C");11traceSessionResponse.setDownstreamStatusCodes("200");12traceSessionResponse.setDownstreamUri("/session");13traceSessionResponse.setUpstreamDialects("W3C");14traceSessionResponse.setUpstreamStatusCodes("200");15traceSessionResponse.setUpstreamUri("/session");16TraceSession traceSession = new TraceSession();17traceSession.setSessionId("session-id");18traceSession.setSessionRequest(traceSessionRequest);19traceSession.setSessionResponse(traceSessionResponse);20TraceSessions traceSessions = new TraceSessions();21traceSessions.setSessions(Arrays.asList(traceSession));22TraceSessionsResponse traceSessionsResponse = new TraceSessionsResponse();23traceSessionsResponse.setSessions(traceSessions);24TraceSessionsResponse traceSessionsResponse = new TraceSessionsResponse();25traceSessionsResponse.setSessions(traceSessions);26TraceSessionsResponse traceSessionsResponse = new TraceSessionsResponse();27traceSessionsResponse.setSessions(traceSessions);28TraceSessionsResponse traceSessionsResponse = new TraceSessionsResponse();29traceSessionsResponse.setSessions(traceSessions);30TraceSessionsResponse traceSessionsResponse = new TraceSessionsResponse();31traceSessionsResponse.setSessions(traceSessions);32TraceSessionsResponse traceSessionsResponse = new TraceSessionsResponse();33traceSessionsResponse.setSessions(traceSessions);
TraceSessionRequest
Using AI Code Generation
1TraceSessionRequest sessionRequest = new TraceSessionRequest();2sessionRequest.setSessionId(sessionId);3sessionRequest.setCapabilities(capabilities);4sessionRequest.setStartTime(startTime);5sessionRequest.setUri(uri);6TraceSessionResponse sessionResponse = new TraceSessionResponse();7sessionResponse.setSessionId(sessionId);8sessionResponse.setCapabilities(capabilities);9sessionResponse.setStartTime(startTime);10sessionResponse.setUri(uri);11TraceSessionEvent sessionEvent = new TraceSessionEvent();12sessionEvent.setSessionId(sessionId);13sessionEvent.setCapabilities(capabilities);14sessionEvent.setStartTime(startTime);15sessionEvent.setUri(uri);16sessionEvent.setEventName(eventName);17TraceSessionInfo sessionInfo = new TraceSessionInfo();18sessionInfo.setSessionId(sessionId);19sessionInfo.setCapabilities(capabilities);20sessionInfo.setStartTime(startTime);21sessionInfo.setUri(uri);22sessionInfo.setEventName(eventName);23sessionInfo.setSessionInfo(sessionInfo);24TraceSession session = new TraceSession();25session.setSessionId(sessionId);26session.setCapabilities(capabilities);27session.setStartTime(startTime);28session.setUri(uri);29session.setEventName(eventName);30session.setSessionInfo(sessionInfo);31TraceNode node = new TraceNode();32node.setUri(uri);33node.setNodeInfo(nodeInfo);34TraceDistributor distributor = new TraceDistributor();35distributor.setUri(uri);36distributor.setDistributorInfo(distributorInfo);37TraceEvent event = new TraceEvent();38event.setSessionId(sessionId);39event.setCapabilities(capabilities);40event.setStartTime(startTime);41event.setUri(uri);42event.setEventName(eventName);43event.setSessionInfo(sessionInfo);44event.setNode(node);45event.setDistributor(distributor);46Trace trace = new Trace();47trace.setSessionId(sessionId);48trace.setCapabilities(capabilities);49trace.setStartTime(startTime);50trace.setUri(uri);51trace.setEventName(eventName);52trace.setSessionInfo(sessionInfo);53trace.setNode(node);54trace.setDistributor(distributor);
TraceSessionRequest
Using AI Code Generation
1TraceSessionRequest traceSessionRequest = new TraceSessionRequest();2traceSessionRequest.setSessionId("session-id");3traceSessionRequest.setTraceId("trace-id");4traceSessionRequest.setCommand("command");5traceSessionRequest.setStartTime(new Date());6traceSessionRequest.setEndTime(new Date());7traceSessionRequest.setDuration(1000);8traceSessionRequest.setSuccess(true);9traceSessionRequest.setFailureReason("failure-reason");10traceSessionRequest.setFailureMessage("failure-message");11traceSessionRequest.setFailureStacktrace("failure-stacktrace");12traceSessionRequest.setParameters("parameters");13traceSessionRequest.setRequestBody("request-body");14traceSessionRequest.setResponseBody("response-body");15traceSessionRequest.setResponseCode(200);16traceSessionRequest.setResponseMessage("response-message");17TraceSessionRequest traceSessionRequest = TraceSessionRequest.builder()18 .sessionId("session-id")19 .traceId("trace-id")20 .command("command")21 .startTime(new Date())22 .endTime(new Date())23 .duration(1000)24 .success(true)25 .failureReason("failure-reason")26 .failureMessage("failure-message")27 .failureStacktrace("failure-stacktrace")28 .parameters("parameters")29 .requestBody("request-body")30 .responseBody("response-body")31 .responseCode(200)32 .responseMessage("response-message")33 .build();34TraceSessionRequest traceSessionRequest = TraceSessionRequest.builder()35 .sessionId("session-id")36 .traceId("trace-id")37 .command("command")38 .startTime(new Date())39 .endTime(new Date())40 .duration(1000)41 .success(true)42 .failureReason("failure-reason")43 .failureMessage("failure-message")44 .failureStacktrace("failure-stacktrace")45 .parameters("parameters")46 .requestBody("request-body")47 .responseBody("response-body")48 .responseCode(200)49 .responseMessage("response-message")50 .build();51TraceSessionRequest traceSessionRequest = TraceSessionRequest.builder()52 .sessionId("session-id")53 .traceId("trace-id")54 .command("command")55 .startTime(new Date())56 .endTime(new Date())57 .duration(100
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!!