Skip to content

Enhance backup logging by implementing fingerprint tracking to avoid duplicates - #18

Open
phuochoit wants to merge 6 commits into
mainfrom
phuochoit/mwp-1701
Open

phuochoit wants to merge 6 commits into
mainfrom
phuochoit/mwp-1701

Conversation

@phuochoit

@phuochoit phuochoit commented Aug 5, 2026 •

Copy link
Copy Markdown

Summary by CodeRabbit

  • New Features

    • Backup activity now supports fingerprint tracking to reliably identify individual backup events.
    • Backup integrations can provide optional event identifiers when logging activity.
    • Fingerprint-based lookup is available to verify whether a backup event was recorded.
  • Bug Fixes

    • Prevented valid fingerprinted backup events from being incorrectly suppressed as duplicates.
    • “Last backup” timestamps update only after the related entry is confirmed as logged.
    • Improved site and network identification for multisite activity logging.

…duplicates

Enhance backup logging by implementing fingerprint tracking to avoid duplicates
@coderabbitai

coderabbitai Bot commented Aug 5, 2026 •

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

Backup callbacks now pass fingerprints through deduplicating logging. Fingerprints are checked against locks and Stream metadata. Backup timestamps update only after confirmed logging. Site and network ID lookups are normalized.

Changes

Backup fingerprint flow

Layer / File(s) Summary
Fingerprint storage and deduplication
classes/class-connector.php, connectors/class-connector-mainwp-backups.php
The connector permits existing records with a backup_fingerprint to continue through logging. Backup fingerprints use sanitization, expiring locks, Stream metadata checks, and ownership-checked cleanup.
Backup callback fingerprint wiring
connectors/class-connector-mainwp-backups.php
BackupBuddy, BackupWordPress, BackWPup, UpdraftPlus, WP Time Capsule, and WPvivid callbacks pass optional fingerprints through deduplicating logging.
UpdraftPlus confirmation handling
classes/class-mainwp-child-report-helper.php
UpdraftPlus generates a fingerprint and updates the last-backup timestamp only when that fingerprint is confirmed as logged.

Site and network lookup handling

Layer / File(s) Summary
Site and network ID normalization
classes/class-log.php, classes/class-network.php
Multisite logging explicitly calls the global site lookup. Network query arguments normalize numeric site IDs and apply installation-specific defaults.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant BackupCallback
  participant MainWPBackups
  participant WordPressOptions
  participant Stream
  participant Connector
  BackupCallback->>MainWPBackups: pass backup fingerprint
  MainWPBackups->>WordPressOptions: acquire fingerprint lock
  MainWPBackups->>Stream: check backup_fingerprint metadata
  MainWPBackups->>Connector: insert deduplicated backup record
  MainWPBackups->>WordPressOptions: release fingerprint lock
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the primary change: fingerprint tracking in backup logging to prevent duplicate records.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch phuochoit/mwp-1701

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 5

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@classes/class-mainwp-child-report-helper.php`:
- Around line 367-368: Update the static call in the fingerprint check to use
the namespaced WP_MainWP_Stream\Connector_MainWP_Backups class validated by
class_exists(), so was_fingerprint_logged() succeeds and
mainwp_lasttime_backup_updraftplus is updated.

In `@connectors/class-connector-mainwp-backups.php`:
- Around line 270-274: Update the fingerprint condition after Connector::log()
so FINGERPRINT_OPTION is modified only when log() returns a truthy confirmed
Stream insert result, preventing null results from indexing skipped records. Use
the existing $result value and preserve the fingerprint non-empty check.
- Around line 260-274: Update log_backup to reserve the fingerprint atomically
instead of doing a separate fingerprint_exists check before insert, since
concurrent calls can race between the check, Stream write, and option update.
Use a single lock or unique reservation mechanism around the fingerprint path in
log_backup so only one callback can claim a given fingerprint at a time. Keep
the existing backup_fingerprint assignment and option write flow, but release or
roll back the reservation if $this->log() fails or returns a WP_Error so failed
inserts do not block future retries.
- Around line 272-274: Bound the recent-fingerprint cache in the backup flow
around the fingerprint option update in the MainWP backups connector, since the
current get_option/self::FINGERPRINT_OPTION array grows without limit and
rewrites the full payload on every backup. Update the code that adds
$fingerprint to the cache to prune older entries before update_option(), using
the existing fingerprint handling path in this connector. Preserve the metadata
fallback for fingerprints that are evicted from the cache so previously stored
backups can still be recognized.
- Around line 297-304: Update the fallback fingerprint lookup in the surrounding
deduplication method to join mainwp_stream_meta with mainwp_stream on record_id,
then constrain the query using get_current_site()->id and get_current_blog_id().
Preserve the existing backup_fingerprint and fingerprint matching while ensuring
identical fingerprints from other sites or blogs do not suppress logging.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5603a9f3-6709-4e6e-90d3-fd45c5ba5c6d

📥 Commits

Reviewing files that changed from the base of the PR and between 276603d and 030f1c1.

📒 Files selected for processing (3)
  • classes/class-connector.php
  • classes/class-mainwp-child-report-helper.php
  • connectors/class-connector-mainwp-backups.php

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@connectors/class-connector-mainwp-backups.php`:
- Around line 274-301: Update the fingerprint lock handling around the lock
acquisition and cleanup in the enclosing logging method to use an atomically
reclaimable lease with an expiration time, allowing later deliveries to replace
only expired locks without overwriting active ones. After successful
acquisition, register shutdown cleanup for the specific lock so abnormal
termination removes it when possible; retain normal-path deletion and ensure
cleanup does not remove a newer owner’s lock.
- Around line 325-333: Update the fallback metadata lookup in the fingerprint
query to use the effective blog ID passed through the same logging filter as
classes/class-log.php, rather than the raw get_current_blog_id() value. Apply
the existing wp_mainwp_stream_blog_id_logged filter before supplying the blog ID
to the SQL parameters, while preserving the current site ID and fingerprint
matching.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ae36c62b-5d81-4325-b8d0-8e44c95322ef

📥 Commits

Reviewing files that changed from the base of the PR and between 030f1c1 and 6449776.

📒 Files selected for processing (2)
  • classes/class-mainwp-child-report-helper.php
  • connectors/class-connector-mainwp-backups.php
🚧 Files skipped from review as they are similar to previous changes (1)
  • classes/class-mainwp-child-report-helper.php

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
connectors/class-connector-mainwp-backups.php (2)

273-279: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Retry after an active lock owner fails.

When add_option() finds an unexpired lock, this method returns immediately. If the lock owner then receives false or null from Connector::log() and releases the lock, this delivery is lost unless the producer sends another callback.

Add a bounded retry after the lock disappears. Re-acquire the lock and run fingerprint_exists() again before logging.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@connectors/class-connector-mainwp-backups.php` around lines 273 - 279, Update
the lock-handling flow around add_option() in the relevant connector method so
that, when an initially active lock disappears, it performs one bounded retry:
re-acquire the lock, re-run fingerprint_exists(), and continue to
Connector::log() only if appropriate. Preserve the existing immediate failure
behavior for malformed or still-unexpired locks, and avoid unbounded retries.

356-369: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Handle lookup errors separately from missing fingerprints.

$wpdb->get_var() returns null for both cases. Check $wpdb->last_error immediately after the query. Return WP_Error on failure and null !== $record_id for a matching record. In log_backup(), release the lock and return false when the lookup returns WP_Error. In was_fingerprint_logged(), return true === self::fingerprint_exists( $fingerprint ) so an error cannot advance the UpdraftPlus timestamp.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@connectors/class-connector-mainwp-backups.php` around lines 356 - 369, Update
the fingerprint lookup method around $wpdb->get_var() to inspect
$wpdb->last_error immediately, return a WP_Error for query failures, and use
null !== $record_id to distinguish a match from a missing record. In
log_backup(), release the lock and return false when fingerprint_exists()
returns WP_Error; in was_fingerprint_logged(), return true ===
self::fingerprint_exists( $fingerprint ) so lookup errors cannot advance the
timestamp.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@connectors/class-connector-mainwp-backups.php`:
- Around line 273-279: Update the lock-handling flow around add_option() in the
relevant connector method so that, when an initially active lock disappears, it
performs one bounded retry: re-acquire the lock, re-run fingerprint_exists(),
and continue to Connector::log() only if appropriate. Preserve the existing
immediate failure behavior for malformed or still-unexpired locks, and avoid
unbounded retries.
- Around line 356-369: Update the fingerprint lookup method around
$wpdb->get_var() to inspect $wpdb->last_error immediately, return a WP_Error for
query failures, and use null !== $record_id to distinguish a match from a
missing record. In log_backup(), release the lock and return false when
fingerprint_exists() returns WP_Error; in was_fingerprint_logged(), return true
=== self::fingerprint_exists( $fingerprint ) so lookup errors cannot advance the
timestamp.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 15b455f8-0546-48ff-ba0e-911a02475fc7

📥 Commits

Reviewing files that changed from the base of the PR and between 1b2a519 and 067d153.

📒 Files selected for processing (1)
  • connectors/class-connector-mainwp-backups.php

@sonarqubecloud

Copy link
Copy Markdown

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants