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

Make sure the oldest boot ID is included in the boot list #5591

Merged
merged 1 commit into from
Jan 31, 2025

Conversation

sairon
Copy link
Member

@sairon sairon commented Jan 30, 2025

Proposed change

If the system is running for a long time, or the logging is particularly chatty, the Systemd journal message we use to detect boot will be rotated out of the journal. Currently we only handled it if there was one boot, but we usually always missed the oldest boot if there were more boots.

Adjust the method for getting boot IDs to always get the very first log line in the journal instead of the last one, and make sure its boot ID is included in the list.

Current list of boots from Systemd:

# journalctl --list-boots
IDX BOOT ID                          FIRST ENTRY                 LAST ENTRY                 
 -6 91c41d0547614140bc0ebd5905359cef Tue 2025-01-21 00:47:38 UTC Wed 2025-01-22 08:17:47 UTC
 -5 9a70b449ceaa451cb040af664aa89069 Wed 2025-01-22 08:17:55 UTC Mon 2025-01-27 13:52:24 UTC
 -4 fa5692773d8e44fc985a2884a4afe457 Mon 2025-01-27 13:52:32 UTC Mon 2025-01-27 14:20:19 UTC
 -3 2bf6f14def81436ab7a760294677f2a8 Mon 2025-01-27 14:20:27 UTC Mon 2025-01-27 14:26:47 UTC
 -2 c0a1f6444e2b41fbbab58fae38bae726 Mon 2025-01-27 14:26:55 UTC Mon 2025-01-27 14:31:09 UTC
 -1 2327258eb45b41b0a2d8c04ba381e49b Mon 2025-01-27 14:31:17 UTC Mon 2025-01-27 14:48:32 UTC
  0 f738a762bc7143a69d9cacc3a139a957 Mon 2025-01-27 14:48:40 UTC Thu 2025-01-30 17:03:49 UTC

Before:

# ha host logs boots
boots:
  "-1": 2327258eb45b41b0a2d8c04ba381e49b
  "-2": c0a1f6444e2b41fbbab58fae38bae726
  "-3": 2bf6f14def81436ab7a760294677f2a8
  "-4": fa5692773d8e44fc985a2884a4afe457
  "-5": 9a70b449ceaa451cb040af664aa89069
  "0": f738a762bc7143a69d9cacc3a139a957

After:

# ha host logs boots
boots:
  "-1": 2327258eb45b41b0a2d8c04ba381e49b
  "-2": c0a1f6444e2b41fbbab58fae38bae726
  "-3": 2bf6f14def81436ab7a760294677f2a8
  "-4": fa5692773d8e44fc985a2884a4afe457
  "-5": 9a70b449ceaa451cb040af664aa89069
  "-6": 91c41d0547614140bc0ebd5905359cef
  "0": f738a762bc7143a69d9cacc3a139a957

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (which adds functionality to the supervisor)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to cli pull request:
  • Link to client library pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Ruff (ruff format supervisor tests)
  • Tests have been added to verify that the new code works.

If API endpoints or add-on configuration are added/changed:

Summary by CodeRabbit

  • Bug Fixes
    • Streamlined log retrieval process to ensure accurate boot ID collection by prioritizing the oldest log entry.
    • Enhanced handling of log entries to ensure inclusion of boot IDs, even when older entries have been rotated out of the journal.

@sairon sairon added the bugfix A bug fix label Jan 30, 2025
@sairon sairon requested review from agners and mdegat01 January 30, 2025 17:21
Copy link
Contributor

coderabbitai bot commented Jan 30, 2025

📝 Walkthrough

Walkthrough

The changes in the supervisor/host/logs.py file focus on modifying the get_boot_ids method within the LogsControl class. The primary modification involves improving the logic for retrieving boot IDs from system logs. The new implementation removes a previous fallback mechanism and ensures that the oldest log entry is retrieved first, even if it has been rotated out of the journal. The method now initializes an empty list for boot IDs and concatenates log entries to guarantee comprehensive boot ID extraction.

Changes

File Change Summary
supervisor/host/logs.py Modified get_boot_ids method in LogsControl class to improve boot ID retrieval logic. Changes include:
  • Removing the previous fallback query mechanism
  • Initializing self._boot_ids as an empty list
  • Ensuring retrieval of the oldest log entry
  • Concatenating log entry text to capture boot IDs
  • Maintaining consistent error handling with HostLogError

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1ddad57 and 1dd90e6.

📒 Files selected for processing (1)
  • supervisor/host/logs.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • supervisor/host/logs.py
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Build armv7 supervisor
  • GitHub Check: Build armhf supervisor
  • GitHub Check: Build aarch64 supervisor
  • GitHub Check: Run tests Python 3.13.1

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
supervisor/host/logs.py (1)

129-135: Consider adding explicit error handling for JSON parsing.

While the implementation correctly processes and deduplicates boot IDs, it could benefit from explicit error handling for JSON parsing to gracefully handle malformed entries.

Consider applying this diff:

 self._boot_ids = []
 for entry in text.split("\n"):
-    if (
-        entry
-        and (boot_id := json.loads(entry)[PARAM_BOOT_ID]) not in self._boot_ids
-    ):
-        self._boot_ids.append(boot_id)
+    if not entry:
+        continue
+    try:
+        data = json.loads(entry)
+        if (boot_id := data.get(PARAM_BOOT_ID)) and boot_id not in self._boot_ids:
+            self._boot_ids.append(boot_id)
+    except json.JSONDecodeError as err:
+        _LOGGER.warning("Failed to parse journal entry: %s", err)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d1d8446 and 1ddad57.

📒 Files selected for processing (1)
  • supervisor/host/logs.py (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: Build i386 supervisor
  • GitHub Check: Build armv7 supervisor
  • GitHub Check: Build armhf supervisor
  • GitHub Check: Build aarch64 supervisor
  • GitHub Check: Run tests Python 3.13.1
🔇 Additional comments (1)
supervisor/host/logs.py (1)

114-127: LGTM! Consider enhancing the comments and verifying the range header format.

The implementation effectively ensures the oldest boot ID is included. Two minor suggestions:

  1. Consider expanding the comment to explain the range header format.
  2. Consider adding error handling for malformed JSON responses.

Let's verify the range header format is correct for systemd-journal-gatewayd:

✅ Verification successful

Range header format is correct and consistent with test cases

The format entries=:0:1 follows the established pattern entries=:position:count used throughout the codebase, correctly requesting the oldest entry.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the range header format for systemd-journal-gatewayd
# Expected: Documentation or examples showing the correct format for the Range header

# Search for range header documentation in the systemd source code
rg -A 5 -B 5 "entries=" --glob "*.{c,h,md,html}"

# Search for examples of range header usage
rg -A 5 "Range.*entries" --glob "*.{py,c,h}"

Length of output: 879

@sairon
Copy link
Member Author

sairon commented Jan 30, 2025

The test failure is unrelated to this PR.

@sairon sairon force-pushed the host-logs-add-oldest-boot-id branch from 1ddad57 to 1dd90e6 Compare January 31, 2025 10:31
If the system is running for a long time, or the logging is particularly
chatty, the Systemd journal message we use to detect boot will be
rotated out of the journal. Currently we only handled it if there was
one boot, but we usually always missed the oldest boot if there were
more boots.

Adjust the method for getting boot IDs to always get the very first log
line in the journal instead of the last one, and make sure its boot ID
is included in the list.
@agners agners merged commit 1aabca9 into main Jan 31, 2025
20 checks passed
@agners agners deleted the host-logs-add-oldest-boot-id branch January 31, 2025 10:55
@github-actions github-actions bot locked and limited conversation to collaborators Feb 2, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants