Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[grid] Expose register status via Node status response #15448

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from

Conversation

VietND96
Copy link
Member

@VietND96 VietND96 commented Mar 18, 2025

User description

Thanks for contributing to Selenium!
A PR well described will help maintainers to quickly review and merge it

Before submitting your PR, please check our contributing guidelines.
Avoid large PRs, help reviewers by making them as simple and short as possible.

Motivation and Context

Fixes SeleniumHQ/docker-selenium#2705
Currently, Node status response looks like

{
  "value": {
    "ready": true,
    "message": "Ready",
    "node": {
      "availability": "UP",

"ready": true is returned by hasCapability() - this status reflects Node is ready (connected to Bus, has slots available).
However, this does not include registration status.
To have reliable data for adding Node health checks (Docker) or Node startup/readiness probes (K8s), via /status response, we expose one more attribute registered (true/false) for registration status with Hub.

Now, response when Node just up, not registered to Hub yet

{
  "value": {
    "ready": true,
    "message": "Ready",
    "registered": false,
    "node": {
      "availability": "UP",

Once it is able to register to Hub

{
  "value": {
    "ready": true,
    "message": "Ready",
    "registered": true,
    "node": {
      "availability": "UP",

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

PR Type

Enhancement


Description

  • Added registered attribute to Node status response for registration status.

  • Updated hasCapacity and hasCapability methods to check Node availability.

  • Introduced isRegistered and register methods in Node class.

  • Enhanced readiness check to include Node registration status.


Changes walkthrough 📝

Relevant files
Enhancement
NodeStatus.java
Update capacity checks with Node availability                       

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

  • Updated hasCapability and hasCapacity methods to check Node
    availability.
  • Ensured Node's availability is considered in capacity checks.
  • +6/-3     
    Node.java
    Add registration status tracking to Node                                 

    java/src/org/openqa/selenium/grid/node/Node.java

  • Added registered attribute to track Node registration status.
  • Introduced isRegistered and register methods for registration
    handling.
  • +9/-0     
    StatusHandler.java
    Include registration status in Node status response           

    java/src/org/openqa/selenium/grid/node/StatusHandler.java

  • Added registered attribute to Node status response.
  • Modified response to include registration status.
  • +2/-0     
    NodeServer.java
    Improve readiness check with registration status                 

    java/src/org/openqa/selenium/grid/node/httpd/NodeServer.java

  • Enhanced readiness check to include Node registration status.
  • Updated Node registration event to set registered attribute.
  • +2/-1     

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • Signed-off-by: Viet Nguyen Duc <nguyenducviet4496@gmail.com>
    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Incomplete Condition

    The readiness check now checks for both node.isReady() and node.getStatus().hasCapacity(), but the error response doesn't indicate which condition failed. This could make debugging harder.

    if (node.isReady() && node.getStatus().hasCapacity()) {
      return new HttpResponse()
          .setStatus(HTTP_OK)
          .setHeader("Content-Type", MediaType.PLAIN_TEXT_UTF_8.toString())
          .setContent(Contents.utf8String("Node has capacity available"));
    }
    
    return new HttpResponse()
        .setStatus(HTTP_UNAVAILABLE)
        .setHeader("Content-Type", MediaType.PLAIN_TEXT_UTF_8.toString())
        .setContent(Contents.utf8String("Node has no capacity available"));
    Initialization Issue

    The new 'registered' field is added but not initialized. It should be explicitly initialized to false in the constructor to ensure proper state tracking.

    protected boolean registered;

    @VietND96 VietND96 requested a review from pujagani March 18, 2025 11:12
    Copy link
    Contributor

    qodo-merge-pro bot commented Mar 18, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    General
    Include registration in readiness check

    The readiness check should also verify that the node is registered. The
    isReady() check might not fully capture the registration state, and the code
    should be consistent with the new registration status tracking.

    java/src/org/openqa/selenium/grid/node/httpd/NodeServer.java [131-137]

     HttpHandler readinessCheck =
         req -> {
    -      if (node.isReady() && node.getStatus().hasCapacity()) {
    +      if (node.isReady() && node.isRegistered() && node.getStatus().hasCapacity()) {
             return new HttpResponse()
                 .setStatus(HTTP_OK)
                 .setHeader("Content-Type", MediaType.PLAIN_TEXT_UTF_8.toString())
                 .setContent(Contents.utf8String("Node has capacity available"));
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    __

    Why: This suggestion correctly identifies that the readiness check should include the new registration status. Since the PR adds registration tracking functionality, ensuring the node is registered before reporting it as ready is important for system consistency and correctness.

    Medium
    Initialize registration state field

    Initialize the registered field to false in the constructor to ensure it has a
    proper default value. Currently, it's declared but not initialized, which could
    lead to unexpected behavior.

    java/src/org/openqa/selenium/grid/node/Node.java [134-138]

     protected boolean registered;
     
     protected Node(
         Tracer tracer, NodeId id, URI uri, Secret registrationSecret, Duration sessionTimeout) {
       this.tracer = Require.nonNull("Tracer", tracer);
    +  this.registered = false;
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    __

    Why: The suggestion addresses a potential issue where the new 'registered' field is declared but not initialized in the constructor. Explicitly initializing it to false ensures consistent behavior across all Node instances and prevents potential bugs from uninitialized state.

    Medium
    • Update

    Comment on lines 150 to +151
    nodeRegistered.set(true);
    node.register();
    Copy link
    Member

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Do we need this flag in two places?
    Can we just use the new one for both?

    Comment on lines 133 to +134
    protected boolean draining;
    protected boolean registered;
    Copy link
    Member

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    These fields should be volatile to ensure different threads will see the changes.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    [🐛 Bug]: Using Healthchecks to monitor nodes causes "Binding additional locator mechanisms: relative"
    2 participants