Skip to content

Commit

Permalink
log switchover duration in db
Browse files Browse the repository at this point in the history
  • Loading branch information
var77 committed Nov 20, 2024
1 parent ca695a5 commit 22323bc
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
19 changes: 19 additions & 0 deletions model/lantern/lantern_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,25 @@ def delete_logical_subscription(name)
representative_server.run_query_all("DROP SUBSCRIPTION IF EXISTS #{name}")
end

def mark_switchover_start
commands = <<SQL
BEGIN;
DROP TABLE IF EXISTS _ldb_switchover_info;
CREATE TABLE _ldb_switchover_info(
id SERIAL PRIMARY KEY,
started_at TIMESTAMP NOT NULL DEFAULT NOW(),
finished_at TIMESTAMP
);
INSERT INTO _ldb_switchover_info(started_at) VALUES (NOW());
COMMIT;
SQL
representative_server.run_query(commands)
end

def mark_switchover_finish
representative_server.run_query("UPDATE _ldb_switchover_info SET finished_at=NOW()")
end

def create_logical_replica(lantern_version: nil, extras_version: nil, minor_version: nil, pg_upgrade: nil)
# TODO::
# 1. If new database will be created during logical replication it won't be added automatically
Expand Down
3 changes: 3 additions & 0 deletions prog/lantern/lantern_resource_nexus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ def before_run
# remove ddl_log
lantern_resource.drop_ddl_log

lantern_resource.mark_switchover_finish

hop_wait
end

Expand Down Expand Up @@ -336,6 +338,7 @@ def before_run
label def switchover_with_parent
decr_switchover_with_parent
lantern_resource.parent.set_to_readonly
lantern_resource.mark_switchover_start
hop_wait_for_synchronization
end

Expand Down
18 changes: 18 additions & 0 deletions spec/model/lantern/lantern_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,22 @@
expect { lantern_resource.rollback_switchover }.not_to raise_error
end
end

describe "#mark_switchover_start" do
it "marks switchover start time" do
representative_server = instance_double(LanternServer)
expect(lantern_resource).to receive(:representative_server).and_return(representative_server).at_least(:once)
expect(lantern_resource.representative_server).to receive(:run_query).with(a_string_matching(/_ldb_switchover_info/))
expect { lantern_resource.mark_switchover_start }.not_to raise_error
end
end

describe "#mark_switchover_finish" do
it "marks switchover finish time" do
representative_server = instance_double(LanternServer)
expect(lantern_resource).to receive(:representative_server).and_return(representative_server).at_least(:once)
expect(lantern_resource.representative_server).to receive(:run_query).with(a_string_matching(/_ldb_switchover_info/))
expect { lantern_resource.mark_switchover_finish }.not_to raise_error
end
end
end
2 changes: 2 additions & 0 deletions spec/prog/lantern/lantern_resource_nexus_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@
expect(lantern_resource).to receive(:update).with(parent_id: nil)
expect(lantern_resource).to receive(:timeline).and_return(timeline)
expect(lantern_resource).to receive(:drop_ddl_log)
expect(lantern_resource).to receive(:mark_switchover_finish)
expect(timeline).to receive(:update).with(parent_id: nil)

expect { nx.finish_take_over }.to hop("wait")
Expand All @@ -450,6 +451,7 @@
it "sets parent to readonly and hop" do
parent = instance_double(LanternResource)
expect(lantern_resource).to receive(:parent).and_return(parent)
expect(lantern_resource).to receive(:mark_switchover_start)
expect(parent).to receive(:set_to_readonly)
expect(nx).to receive(:decr_switchover_with_parent)

Expand Down

0 comments on commit 22323bc

Please sign in to comment.