Skip to content

Add SQLite copy and Elasticsearch reindex tasks for the move - #1617

Draft
benrfairless wants to merge 5 commits into
feature/1579-new-serverfrom
feature/1589-sqlite-and-reindex-tasks
Draft

benrfairless wants to merge 5 commits into
feature/1579-new-serverfrom
feature/1589-sqlite-and-reindex-tasks

Conversation

@benrfairless

Copy link
Copy Markdown
Member

What and why

  • Adds the code and docs for Move SQLite scraper databases and reindex Elasticsearch #1589: a resumable rsync bulk copy plus final delta of db/scrapers, a reindex of Elasticsearch from the database, and verification tasks for both. Runbook in docs/sqlite_and_search_migration.md. Part of Spec: move morph.io to a new Ubuntu 26.04 server #1579.
  • app:sqlite_migration:bulk and :delta take source and destination from SQLITE_SOURCE and SQLITE_DESTINATION, authenticate through ssh only (a location with a password in it is refused), and print and optionally log (LOG_FILE) elapsed time and transfer speed. The delta deletes only with DELETE=1 and a MAX_DELETE limit, and fails on vanished files, because nothing should change after the freeze.
  • app:sqlite_migration:manifest and :verify compare file counts and sizes, with data/ and repos/ subtotals. The listing is plain path<TAB>size, so the old server (Ruby 3.0.7, cannot run this branch) can make it with find db/scrapers -type f -printf '%P\t%s\n' | sort.
  • app:search_migration:reindex rebuilds the Scraper and Owner indexes with Searchkick's reindex (fresh index, then alias swap, nothing carried over) and prints the Elasticsearch version, progress, time and document counts, exiting 1 if they differ. :verify is counts only and can take SOURCE_COUNTS=Scraper=N,Owner=N from the old server for the record. Counts use the count API because Index#total_docs stops at 10,000, and production has about 11,127 scrapers (checked on a scratch index of 10,001 documents).
  • Redis is not carried over and there is no Redis code. The docs say the new server starts with an empty Redis.
  • Targets feature/1579-new-server, not main. Could it go to main instead? Possibly, all of it: it avoids Ruby 3.1+ syntax and uses only public Searchkick 5.0.5 calls. I did not run it on Ruby 3.0.7 or Rails 6.1, and it is only used from the new server, so I left it here. That call is Ben's.
  • Choices not in the tickets: task names app:sqlite_migration and app:search_migration (separate files, so Migrate MySQL with a filtered dump and S3 archive #1588 does not conflict); sizes compared, not checksums; rsync --inplace so a 16 GB file can resume and update in place; no default source or destination (in rehearsal the destination is the app's own shared/db/scrapers); the delta failing on rsync exit 24 while the bulk copy only warns.
  • Reindexing against Elasticsearch 8.19 depends on Run Elasticsearch 8.19 in the local stack, with the Searchkick fix #1613 (the Content-Type override) and ELASTIC_CLIENT_APIVERSIONING=true in the server's environment. Neither is in this branch yet, so this was only run against 7.17.7.
  • TESTING.md will probably conflict with the Migrate MySQL with a filtered dump and S3 archive #1588 branch: both append a section at the end of the file.

Type of change

  • Bug fix
  • New feature
  • Chore
  • Already deployed
  • Breaking change
  • Documentation

How this was checked

In a private docker-compose.ruby34.yml stack (Ruby 3.4.12, Elasticsearch 7.17.7, MySQL 5.7.33, rsync added to a local image), not against production or any real server. The quick profile gave 977 examples, 0 failures at 80.16% against 76.67. The CI profile (docker and GitHub specs excluded, slow specs on) gave 989 examples, 0 failures at 82.46% against 80.05, so no threshold changed. rubocop reported no offences and haml-lint 0 lints, and srb tc reports nothing in the new files. Specs use tmp directories only and never touch db/scrapers. By hand I ran the bulk and delta copies with real rsync between temporary directories, the manifest and verify tasks, and a real reindex, verify and progress poll on 7.17.7.

  • Checked the affected area on my own or a staging system
  • Ran the automated tests
  • Ran /code-review (or equivalent): fixed any concerns (and rechecked), linked follow-up issues above, or noted why they are not important above
  • GitHub Actions tests: likewise fixed so they pass or linked a follow-up issue / replied why not important directly on each comment (reviewer will mark resolved as part of review)
  • Documentation updated, or not needed

Acceptance criteria that need the rehearsal server, not proved here:

  • Bulk copy from the old server to /srv/www/rehearsal/shared/db/scrapers runs, with elapsed time and transfer speed recorded
  • The delta pass completes after the freeze, and the file counts and sizes then match
  • The reindex completes on Elasticsearch 8.19 with matching counts, and search works on the site afterwards (needs Run Elasticsearch 8.19 in the local stack, with the Searchkick fix #1613)
  • Redis starts empty on the new server

Assisted-by: Claude Code:claude-sonnet-5-5

Copies db/scrapers from the old server to the new one with rsync: a
resumable bulk copy days ahead and a final delta pass inside the cutover
window. Records elapsed time and transfer speed, takes source and
destination from the environment, and refuses a location with a
password in it. The delta deletes only with an explicit limit.

Adds a file listing (path and size) that the old server can also make
with find, and a comparison that prints a report for the dry run record.

Part of #1589.

Assisted-by: Claude Code:claude-sonnet-5-5
Signed-off-by: Ben Fairless <ben@oaf.org.au>
Rebuilds the Scraper and Owner indexes from the database with
Searchkick's reindex, prints progress and the server version, and
compares document counts with the database and, optionally, with counts
taken from the old server. Nothing from the old index is carried over.

Part of #1589.

Assisted-by: Claude Code:claude-sonnet-5-5
Signed-off-by: Ben Fairless <ben@oaf.org.au>
Runbook for the new tasks, including the shell commands the old server
uses for its file listing, the note that Redis is not carried over, and
the manual checks for the rehearsal server in TESTING.md.

Part of #1589.

Assisted-by: Claude Code:claude-sonnet-5-5
Signed-off-by: Ben Fairless <ben@oaf.org.au>
@benrfairless benrfairless self-assigned this Nov 27, 2026
@benrfairless
benrfairless marked this pull request as ready for review November 27, 2026 10:37
@benrfairless
benrfairless requested a review from a team as a code owner November 27, 2026 10:37
Comment thread lib/tasks/sqlite_migration.rake Resolved
Comment on lines +151 to +161
total_file_bytes: stat(text, /Total file size: ([\d,.]+)/),
sent: stat(text, /Total bytes sent: ([\d,.]+)/),
received: stat(text, /Total bytes received: ([\d,.]+)/)
}.compact
end

sig { params(text: String, pattern: Regexp).returns(T.nilable(Integer)) }
def self.stat(text, pattern)
match = text.match(pattern)
match && match[1]&.delete(",.")&.to_i
end

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: Rsync output with invalid UTF-8 byte sequences can raise an ArgumentError during regex parsing in parse_stats.
Severity: LOW

Suggested Fix

Call .scrub on the concatenated output string before performing regex matching, or configure process stream encodings to handle non-UTF-8 bytes cleanly.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: app/lib/morph/sqlite_migration/sync.rb#L146-L161

Potential issue: In `Sync#run`, output lines from `Open3.popen2e` are gathered and
passed directly to `parse_stats` via `output.join`. If `rsync` encounters and prints
filenames containing invalid UTF-8 byte sequences (e.g., in warning or vanished-file
messages), matching regexes against the resulting string in `parse_stats` raises an
`ArgumentError: invalid byte sequence in UTF-8`. Because only `Sync::Error` is rescued
in the task, this unhandled exception crashes the task before metrics can be recorded.

Did we get this right? 👍 / 👎 to inform future reviews.

- Follow a symlinked db/scrapers root, which is a Capistrano linked_dir
- Skip files that vanish during a scan, and keep tabs in file names
- Reject a non-numeric MAX_DELETE or BWLIMIT_KBPS instead of reading it as 0
- Mark dry runs in the log record, explain rsync exit 23 and 25, and
  report a missing rsync as a tidy error

Part of #1589.

Assisted-by: Claude Code:claude-sonnet-5-5
The SQLite scraper data lives in a single directory outside both stages
(for example /srv/www/data/scrapers), linked into each stage's
shared/db/scrapers. The rehearsal stage is retired at cutover but its
data is not copied twice. Replaces the stage directory rename from #1595
in the runbook, the manual tests and the spec examples. The code still
has no default source or destination.

Part of #1589.

Assisted-by: Claude Code:claude-sonnet-5-5
Comment on lines +35 to +41
def self.positive_integer(name, value)
return nil if value.nil? || value.strip.empty?

number = T.let(Integer(value, exception: false), T.nilable(Integer))
raise Error, "#{name} must be a whole number above 0" if number.nil? || number < 1

number

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: positive_integer calls Integer(value, exception: false) without a base, causing inputs with leading zeroes to be parsed as octal or rejected.
Severity: LOW

Suggested Fix

Specify base 10 by calling Integer(value, 10, exception: false) or validate against \A\d+\z before converting to an integer.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: app/lib/morph/sqlite_migration/sync.rb#L35-L41

Potential issue: In `positive_integer`, calling `Integer(value, exception: false)`
without specifying a base defaults to Ruby literal parsing rules. Inputs with a leading
zero (such as `010`) are interpreted as octal numbers instead of decimal, or rejected as
invalid when containing non-octal digits like `08` or `09`. As a result, values passed
into `--max-delete` or `--bwlimit` can misrepresent the operator's intended base-10
configuration.

@benrfairless
benrfairless marked this pull request as draft November 27, 2026 13:01
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.

1 participant