Files
ubicloud/spec/prog/setup_grafana_spec.rb
Jeremy Evans 7544717b96 Have last exiting child schedule parent strand
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.
2025-06-28 03:30:43 +09:00

97 lines
3.7 KiB
Ruby

# frozen_string_literal: true
require_relative "../model/spec_helper"
RSpec.describe Prog::SetupGrafana do
subject(:sn) { described_class.new(st) }
let(:st) { Strand.create(prog: "SetupGrafana", label: "start", stack: [{"subject_id" => sshable.id, "cert_email" => "email@gmail.com", "domain" => "grafana.domain.com"}]) }
let(:sshable) { Sshable.create(host: "test.localhost", raw_private_key_1: SshKey.generate.keypair) }
before do
allow(sn).to receive(:sshable).and_return(sshable)
end
describe "#domain" do
it "returns the domain based on the stack" do
expect(sn.domain).to eq("grafana.domain.com")
end
end
describe "#cert_email" do
it "returns the cert email based on the stack" do
expect(sn.cert_email).to eq("email@gmail.com")
end
end
describe "#assemble" do
it "fails if domain is not provided" do
expect { described_class.assemble(sshable.id, grafana_domain: "", certificate_owner_email: "cert@gmail.com") }.to raise_error(RuntimeError)
end
it "fails if cert_email is not provided" do
expect { described_class.assemble(sshable.id, grafana_domain: "domain.com", certificate_owner_email: "") }.to raise_error(RuntimeError)
end
it "fails if sshable is not provided or does not exist" do
expect { described_class.assemble("vm6htsmcfx5t1p60s609v49fbf", grafana_domain: "domain.com", certificate_owner_email: "cert@gmail.com") }.to raise_error(RuntimeError)
end
it "creates an strand with the right input" do
st = described_class.assemble(sshable.id, grafana_domain: "domain.com", certificate_owner_email: "cert@gmail.com")
st.reload
expect(st.label).to eq("start")
end
end
describe "#install_rhizome" do
it "buds a bootstrap rhizome prog and hops to wait_for_rhizome" do
expect(sn).to receive(:bud).with(Prog::BootstrapRhizome, {"target_folder" => "host", "subject_id" => sshable.id, "user" => sshable.unix_user})
expect { sn.start }.to hop("wait_for_rhizome")
end
end
describe "#wait_for_rhizome" do
it "donates if install_rhizome is not done" do
Strand.create(parent_id: st.id, prog: "BootstrapRhizome", label: "start", stack: [{}], lease: Time.now + 10)
expect { sn.wait_for_rhizome }.to nap(120)
end
it "hops to install_grafana when rhizome is done" do
expect { sn.wait_for_rhizome }.to hop("install_grafana")
end
end
describe "#install_grafana" do
it "starts to install the grafana if its the first time" do
expect(sn).to receive(:domain).and_return("grafana.domain.com")
expect(sn).to receive(:cert_email).and_return("email@gmail.com")
expect(sshable).to receive(:d_check).and_return("NotStarted")
expect(sshable).to receive(:d_run).with("install_grafana", "sudo", "host/bin/setup-grafana", "grafana.domain.com", "email@gmail.com")
expect { sn.install_grafana }.to nap(10)
end
it "cleans up and pops when the installation is done" do
expect(sshable).to receive(:d_check).and_return("Succeeded")
expect(sshable).to receive(:d_clean).with("install_grafana")
expect { sn.install_grafana }.to exit({"msg" => "grafana was setup"})
end
it "naps when strand is in the middle of execution" do
expect(sshable).to receive(:d_check).and_return("InProgress")
expect { sn.install_grafana }.to nap(10)
end
it "naps for a long time when the installation fails" do
expect(sshable).to receive(:d_check).and_return("Failed")
expect { sn.install_grafana }.to nap(65536)
end
it "naps forever if the daemonizer check returns something unknown" do
expect(sshable).to receive(:d_check).and_return("Unknown")
expect { sn.install_grafana }.to nap(65536)
end
end
end