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

Implement separate long build queues #13

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
# Information representing a CI job.
class Job
NAME_REGEX =
/\A(?<runner>\d+(?:\.\d+)?(?:-arm64)?(?:-cross)?)-(?<run_id>\d+)(?:-(?<run_attempt>\d+))?(?:-(?<tag>[a-z]+))?\z/
/\A(?<runner>\d+(?:\.\d+)?(?:-arm64)?(?:-cross)?)-(?<run_id>\d+)(?:-(?<run_attempt>\d+))?(?:-(?<tags>[-a-z]+))?\z/

attr_reader :runner_name, :repository, :github_id, :secret
attr_reader :runner_name, :repository, :github_id, :secret, :group
attr_writer :orka_setup_timeout
attr_accessor :github_state, :orka_vm_id, :orka_setup_time, :orka_start_attempts, :runner_completion_time

Expand All @@ -26,6 +26,7 @@ def initialize(runner_name, repository, github_id, secret: nil)
@orka_start_attempts = 0
@secret = secret || SecureRandom.hex(32)
@runner_completion_time = nil
@group = long_build? ? :long : :default
end

def os
Expand All @@ -44,8 +45,12 @@ def run_attempt
@runner_name[NAME_REGEX, :run_attempt]
end

def tag
@runner_name[NAME_REGEX, :tag]
def tags
@runner_name[NAME_REGEX, :tags]&.split("-").to_a
end

def long_build?
tags.include?("long")
end

def runner_labels
Expand Down
36 changes: 36 additions & 0 deletions src/job_queue.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require_relative "shared_state"

# A variation of `Thread::Queue` that allows us to prioritise certain types of jobs.
class JobQueue
def initialize(queue_type)
@mutex = Mutex.new
@queue = Hash.new { |h, k| h[k] = [] }
@queue_type = queue_type
@condvar = ConditionVariable.new
end

def <<(job)
@mutex.synchronize do
@queue[job.group] << job
@condvar.signal
end
end

def pop
@mutex.synchronize do
loop do
running_long_build_count = SharedState.instance.running_jobs(@queue_type).count(&:long_build?)

if running_long_build_count < 2 && !@queue[:long].empty?
Copy link
Member

Choose a reason for hiding this comment

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

Given the limit in homebrew-core is 2 PRs, with a PR containing 3 OS versions, don't we actually want this to be 6 instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe? Is that the count we want per queue_type? If so, then yes, I think that's correct.

Copy link
Member Author

Choose a reason for hiding this comment

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

So these are our queue types:

MACOS_X86_64_LEGACY = 0
MACOS_ARM64 = 1
MACOS_X86_64 = 2

So, yes, 6 seems correct. Will open a new PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

#16

Copy link
Member

Choose a reason for hiding this comment

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

Yeah. All arm64 is all one queue type so 6 sounds correct to me.

x86_64 is technically two types but that's not going to be the case come macOS 15.

break @queue[:long].shift
elsif !@queue[:default].empty?
break @queue[:default].shift
else
@condvar.wait(@mutex)
end
end
end
end
end
5 changes: 3 additions & 2 deletions src/orka_start_processor.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative "thread_runner"
require_relative "job_queue"

require "timeout"

Expand All @@ -21,9 +22,9 @@ class OrkaStartProcessor < ThreadRunner

attr_reader :queue

def initialize(name)
def initialize(queue_type, name)
super("#{self.class.name} (#{name})")
@queue = Queue.new
@queue = JobQueue.new(queue_type)
end

def pausable?
Expand Down
6 changes: 5 additions & 1 deletion src/shared_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def initialize
@file_mutex = Mutex.new

@orka_start_processors = QueueTypes.to_h do |type|
[type, OrkaStartProcessor.new(QueueTypes.name(type))]
[type, OrkaStartProcessor.new(type, QueueTypes.name(type))]
end
@orka_stop_processor = OrkaStopProcessor.new
@orka_timeout_processor = OrkaTimeoutProcessor.new
Expand Down Expand Up @@ -215,6 +215,10 @@ def free_slot?(waiting_job)
@jobs.count { |job| job.queue_type == waiting_job.queue_type && !job.orka_vm_id.nil? } < max_slots
end

def running_jobs(queue_type)
jobs.select { |job| job.queue_type == queue_type && !job.orka_vm_id.nil? }
end

private

def load_pause_data(pause_data)
Expand Down