Previously, when reaping child processes, if there were no remaining reapable children, the parent strand would only nap 1, which puts unnecessary load on respirate unless at least one child strand exits in the next second. Change this approach by having the exiting child strands, after they release the lease, schedule their parent immediately if the parent has no non-exited child strands. When doing this, you need to be careful to make sure there are not race conditions that would delay the scheduling of the parent. There are two potential situations you need to handle: 1. Multiple children exiting at the same time 2. Parent currently running while child is exiting By waiting until after the child strand leases are released, you still have a race condition with 1, but the race condition is that multiple child strands exiting concurrently could both reschedule the parent strand. However, that isn't a problem. You want to avoid the case where neither child strand schedules the parent, which rescheduling after releasing the lease should do. To handle 2, inside reap use Model#lock! to lock the parent strand. This will make exiting child strands block if they UPDATE the parent strand with a new schedule, until the parent strand's transaction commits. However, it's possible that a child strand already UPDATED the parent. To handle this situation, before calling lock!, store the cached schedule value in a local variable. lock! implicitly does a reload, so compare the schedule value after reload. If the schedule has changed, likely a child scheduled the parent for immediate execution, so nap 0 in that case. Just in case there are unforeseen race conditions that are not handled, only nap for 120 seconds if there are active children. Worst case scenario, this results in a 2 minute delay for running the parent. However, this can potentially result in 120x less load from parent strands polling children.
66 lines
2.4 KiB
Ruby
66 lines
2.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require_relative "../model/spec_helper"
|
|
|
|
RSpec.describe Prog::InstallDnsmasq do
|
|
subject(:idm) {
|
|
described_class.new(st)
|
|
}
|
|
|
|
let(:st) { Strand.new(prog: "InstallDnsmasq") }
|
|
|
|
describe "#start" do
|
|
it "starts sub-programs to install dependencies and download dnsmasq concurrently" do
|
|
expect(idm).to receive(:bud).with(described_class, {}, :install_build_dependencies)
|
|
expect(idm).to receive(:bud).with(described_class, {}, :git_clone_dnsmasq)
|
|
|
|
expect { idm.start }.to hop("wait_downloads")
|
|
end
|
|
end
|
|
|
|
describe "#wait_downloads" do
|
|
it "donates if any sub-progs are still running" do
|
|
st.update(label: "wait_downloads", stack: [{}])
|
|
Strand.create(parent_id: st.id, prog: "InstallDnsmasq", label: "install_build_dependencies", stack: [{}], lease: Time.now + 10)
|
|
expect { idm.wait_downloads }.to nap(120)
|
|
end
|
|
|
|
it "hops to compile_and_install when the downloads are done" do
|
|
st.update(label: "wait_downloads", stack: [{}])
|
|
expect { idm.wait_downloads }.to hop("compile_and_install")
|
|
end
|
|
end
|
|
|
|
describe "#compile_and_install" do
|
|
it "runs a compile command and pops" do
|
|
sshable = instance_double(Sshable)
|
|
expect(sshable).to receive(:cmd).with "(cd dnsmasq && make -sj$(nproc) && sudo make install)"
|
|
expect(idm).to receive(:sshable).and_return(sshable)
|
|
|
|
expect { idm.compile_and_install }.to exit({"msg" => "compiled and installed dnsmasq"})
|
|
end
|
|
end
|
|
|
|
describe "#install_build_dependencies" do
|
|
it "installs dependencies and pops" do
|
|
sshable = instance_double(Sshable)
|
|
expect(sshable).to receive(:cmd).with "sudo apt-get -y install make gcc"
|
|
expect(idm).to receive(:sshable).and_return(sshable)
|
|
|
|
expect { idm.install_build_dependencies }.to exit({"msg" => "installed build dependencies"})
|
|
end
|
|
end
|
|
|
|
describe "#git_clone_dnsmasq" do
|
|
it "fetches a version and pops" do
|
|
sshable = instance_double(Sshable)
|
|
expect(sshable).to receive(:cmd).with <<CMD.rstrip
|
|
git init dnsmasq && (cd dnsmasq && git fetch https://github.com/ubicloud/dnsmasq.git b6769234bca9b0eabfe4768832b88d2cdb187092 --depth=1 && git checkout b6769234bca9b0eabfe4768832b88d2cdb187092 && git fsck --full)
|
|
CMD
|
|
expect(idm).to receive(:sshable).and_return(sshable)
|
|
|
|
expect { idm.git_clone_dnsmasq }.to exit({"msg" => "downloaded and verified dnsmasq successfully"})
|
|
end
|
|
end
|
|
end
|