forked from clio/ten_years_rails
-
Notifications
You must be signed in to change notification settings - Fork 42
Add deprecations merge command to combine parallel CI shards #177
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
Open
JuanVqz
wants to merge
6
commits into
main
Choose a base branch
from
feature/merge-shards-command
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
19796dc
Add `deprecations merge` command to combine parallel CI shards
JuanVqz df97370
Extract ShardMerger specs into dedicated spec file
JuanVqz b579926
Update README.md
JuanVqz 92f5c40
Address PR feedback on merge shards command
JuanVqz e31073d
Remove merge_shards delegation test
JuanVqz e748183
Address code review feedback on merge shards command
JuanVqz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| require "json" | ||
| require "fileutils" | ||
|
|
||
| class DeprecationTracker | ||
| class ShardMerger | ||
| attr_reader :base_path, :delete_shards | ||
|
|
||
| def initialize(base_path, delete_shards: false) | ||
| @base_path = base_path | ||
| @delete_shards = delete_shards | ||
| end | ||
|
|
||
| def merge | ||
| shard_files = Dir.glob(shard_glob).sort | ||
|
|
||
| merged = {} | ||
| shard_files.each do |file| | ||
| parse_shard(file).each do |bucket, messages| | ||
| merged[bucket] = (merged[bucket] || []).concat(Array(messages)) | ||
| end | ||
| end | ||
|
|
||
| result = {} | ||
| merged.sort.each do |k, v| | ||
| result[k] = v.sort | ||
| end | ||
|
|
||
| dirname = File.dirname(base_path) | ||
| FileUtils.mkdir_p(dirname) unless File.directory?(dirname) | ||
JuanVqz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| begin | ||
| File.write(base_path, JSON.pretty_generate(result)) | ||
| rescue Errno::EACCES => e | ||
| raise "Cannot write to #{base_path}: #{e.message}" | ||
| end | ||
|
|
||
| shard_files.each { |f| File.delete(f) } if delete_shards | ||
|
|
||
| { shards: shard_files.size, result: result } | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def shard_glob | ||
| ext = File.extname(base_path) | ||
| "#{base_path.chomp(ext)}.node-*#{ext}" | ||
JuanVqz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| def parse_shard(file) | ||
| JSON.parse(File.read(file)) | ||
| rescue Errno::ENOENT | ||
| raise "Shard file not found: #{file}" | ||
| rescue JSON::ParserError => e | ||
| raise "Invalid JSON in shard file #{file}: #{e.message}" | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "spec_helper" | ||
|
|
||
| require "json" | ||
| require "tempfile" | ||
| require_relative "../lib/deprecation_tracker/shard_merger" | ||
|
|
||
| RSpec.describe DeprecationTracker::ShardMerger do | ||
| let(:base_path) do | ||
| dir = Dir.tmpdir | ||
| File.join(dir, "shitlist-#{Process.pid}-#{rand(1000)}.json") | ||
| end | ||
|
|
||
| after do | ||
| FileUtils.rm_f(base_path) | ||
| Dir.glob("#{base_path.chomp('.json')}.node-*.json").each { |f| FileUtils.rm_f(f) } | ||
| end | ||
|
|
||
| def write_shard(index, data) | ||
| path = "#{base_path.chomp('.json')}.node-#{index}.json" | ||
| File.write(path, JSON.pretty_generate(data)) | ||
| path | ||
| end | ||
|
|
||
| subject { described_class.new(base_path) } | ||
|
|
||
| it "merges multiple shard files into the canonical file" do | ||
| write_shard(0, { "bucket 1" => ["a"], "bucket 2" => ["b"] }) | ||
| write_shard(1, { "bucket 3" => ["c"] }) | ||
|
|
||
| output = subject.merge | ||
| result = output[:result] | ||
|
|
||
| expect(result).to eq( | ||
| "bucket 1" => ["a"], | ||
| "bucket 2" => ["b"], | ||
| "bucket 3" => ["c"] | ||
| ) | ||
| expect(output[:shards]).to eq(2) | ||
| expect(JSON.parse(File.read(base_path))).to eq(result) | ||
| end | ||
|
|
||
| it "deep-merges overlapping buckets by concatenating and sorting messages" do | ||
| write_shard(0, { "bucket 1" => ["b", "a"] }) | ||
| write_shard(1, { "bucket 1" => ["c", "a"] }) | ||
|
|
||
| result = subject.merge[:result] | ||
|
|
||
| expect(result).to eq("bucket 1" => ["a", "a", "b", "c"]) | ||
| end | ||
|
|
||
| it "returns an empty hash and writes empty JSON when no shards exist" do | ||
| output = subject.merge | ||
|
|
||
| expect(output[:result]).to eq({}) | ||
| expect(output[:shards]).to eq(0) | ||
| expect(JSON.parse(File.read(base_path))).to eq({}) | ||
| end | ||
|
|
||
| it "handles a single shard file" do | ||
| write_shard(0, { "bucket 1" => ["a"] }) | ||
|
|
||
| output = subject.merge | ||
|
|
||
| expect(output[:result]).to eq("bucket 1" => ["a"]) | ||
| expect(output[:shards]).to eq(1) | ||
| end | ||
|
|
||
| it "deletes shard files when delete_shards is true" do | ||
| shard0 = write_shard(0, { "bucket 1" => ["a"] }) | ||
| shard1 = write_shard(1, { "bucket 2" => ["b"] }) | ||
|
|
||
| merger = described_class.new(base_path, delete_shards: true) | ||
| merger.merge | ||
|
|
||
| expect(File.exist?(shard0)).to be false | ||
| expect(File.exist?(shard1)).to be false | ||
| expect(File.exist?(base_path)).to be true | ||
| end | ||
|
|
||
| it "preserves shard files by default" do | ||
| shard0 = write_shard(0, { "bucket 1" => ["a"] }) | ||
|
|
||
| subject.merge | ||
|
|
||
| expect(File.exist?(shard0)).to be true | ||
| end | ||
|
|
||
| it "sorts buckets alphabetically" do | ||
| write_shard(0, { "z_bucket" => ["a"] }) | ||
| write_shard(1, { "a_bucket" => ["b"] }) | ||
|
|
||
| result = subject.merge[:result] | ||
|
|
||
| expect(result.keys).to eq(["a_bucket", "z_bucket"]) | ||
| end | ||
|
|
||
| it "raises an error for invalid JSON in a shard file" do | ||
| shard_path = "#{base_path.chomp('.json')}.node-0.json" | ||
| File.write(shard_path, "not valid json") | ||
|
|
||
| expect { subject.merge }.to raise_error(/Invalid JSON in shard file/) | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.