Skip to content

Commit

Permalink
Add the reported timeline id to the monitor events table. (#753)
Browse files Browse the repository at this point in the history
  • Loading branch information
DimCitus committed Jul 5, 2021
1 parent ca79fba commit cf91b7a
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/bin/pg_autoctl/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -2232,7 +2232,7 @@ monitor_print_last_events(Monitor *monitor, char *formation, int group, int coun
paramCount, paramTypes, paramValues,
&context, &printLastEvents))
{
log_error("Failed to retrieve current state from the monitor");
log_error("Failed to retrieve last events from the monitor");
return false;
}

Expand Down
6 changes: 4 additions & 2 deletions src/monitor/notifications.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ InsertEvent(AutoFailoverNode *node, char *description)
replicationStateTypeOid, /* reportedstate */
replicationStateTypeOid, /* goalstate */
TEXTOID, /* pg_stat_replication.sync_state */
INT4OID, /* timeline_id */
LSNOID, /* reportedLSN */
INT4OID, /* candidate_priority */
BOOLOID, /* replication_quorum */
Expand All @@ -150,6 +151,7 @@ InsertEvent(AutoFailoverNode *node, char *description)
ObjectIdGetDatum(reportedStateOid), /* reportedstate */
ObjectIdGetDatum(goalStateOid), /* goalstate */
CStringGetTextDatum(SyncStateToString(node->pgsrSyncState)), /* sync_state */
Int32GetDatum(node->reportedTLI), /* reportedTLI */
LSNGetDatum(node->reportedLSN), /* reportedLSN */
Int32GetDatum(node->candidatePriority), /* candidate_priority */
BoolGetDatum(node->replicationQuorum), /* replication_quorum */
Expand All @@ -162,9 +164,9 @@ InsertEvent(AutoFailoverNode *node, char *description)
const char *insertQuery =
"INSERT INTO " AUTO_FAILOVER_EVENT_TABLE
"(formationid, nodeid, groupid, nodename, nodehost, nodeport,"
" reportedstate, goalstate, reportedrepstate, reportedlsn,"
" reportedstate, goalstate, reportedrepstate, reportedtli, reportedlsn,"
" candidatepriority, replicationquorum, description) "
"VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) "
"VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14) "
"RETURNING eventid";

SPI_connect();
Expand Down
134 changes: 134 additions & 0 deletions src/monitor/pgautofailover--1.5--1.6.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ DROP FUNCTION pgautofailover.get_other_nodes(int);
DROP FUNCTION pgautofailover.get_other_nodes
(integer,pgautofailover.replication_state);

DROP FUNCTION pgautofailover.last_events(int);
DROP FUNCTION pgautofailover.last_events(text,int);
DROP FUNCTION pgautofailover.last_events(text,int,int);

DROP FUNCTION pgautofailover.current_state(text);
DROP FUNCTION pgautofailover.current_state(text,int);

Expand Down Expand Up @@ -158,6 +162,51 @@ INSERT INTO pgautofailover.node
candidatepriority, replicationquorum, nodecluster
FROM pgautofailover.node_upgrade_old;


ALTER TABLE pgautofailover.event
RENAME TO event_upgrade_old;

CREATE TABLE pgautofailover.event
(
eventid bigint not null DEFAULT nextval('pgautofailover.event_eventid_seq'::regclass),
eventtime timestamptz not null default now(),
formationid text not null,
nodeid bigint not null,
groupid int not null,
nodename text not null,
nodehost text not null,
nodeport integer not null,
reportedstate pgautofailover.replication_state not null,
goalstate pgautofailover.replication_state not null,
reportedrepstate text,
reportedtli int not null default 1 check (reportedtli > 0),
reportedlsn pg_lsn not null default '0/0',
candidatepriority int,
replicationquorum bool,
description text,

PRIMARY KEY (eventid)
);

ALTER SEQUENCE pgautofailover.event_eventid_seq
OWNED BY pgautofailover.event.eventid;

INSERT INTO pgautofailover.event
(
eventid, eventtime, formationid, nodeid, groupid,
nodename, nodehost, nodeport,
reportedstate, goalstate, reportedrepstate,
reportedtli, reportedlsn, candidatepriority, replicationquorum,
description
)
SELECT eventid, eventtime, formationid, nodeid, groupid,
nodename, nodehost, nodeport,
reportedstate, goalstate, reportedrepstate,
1 as reportedtli, reportedlsn, candidatepriority, replicationquorum,
description
FROM pgautofailover.event_upgrade_old as event;

DROP TABLE pgautofailover.event_upgrade_old;
DROP TABLE pgautofailover.node_upgrade_old;
DROP TYPE pgautofailover.old_replication_state;

Expand Down Expand Up @@ -405,6 +454,91 @@ CREATE TRIGGER disable_secondary_check
EXECUTE PROCEDURE pgautofailover.update_secondary_check();


CREATE FUNCTION pgautofailover.last_events
(
count int default 10
)
RETURNS SETOF pgautofailover.event LANGUAGE SQL STRICT
AS $$
with last_events as
(
select eventid, eventtime, formationid,
nodeid, groupid, nodename, nodehost, nodeport,
reportedstate, goalstate,
reportedrepstate, reportedtli, reportedlsn,
candidatepriority, replicationquorum, description
from pgautofailover.event
order by eventid desc
limit count
)
select * from last_events order by eventtime, eventid;
$$;

comment on function pgautofailover.last_events(int)
is 'retrieve last COUNT events';

grant execute on function pgautofailover.last_events(int)
to autoctl_node;

CREATE FUNCTION pgautofailover.last_events
(
formation_id text default 'default',
count int default 10
)
RETURNS SETOF pgautofailover.event LANGUAGE SQL STRICT
AS $$
with last_events as
(
select eventid, eventtime, formationid,
nodeid, groupid, nodename, nodehost, nodeport,
reportedstate, goalstate,
reportedrepstate, reportedtli, reportedlsn,
candidatepriority, replicationquorum, description
from pgautofailover.event
where formationid = formation_id
order by eventid desc
limit count
)
select * from last_events order by eventtime, eventid;
$$;

comment on function pgautofailover.last_events(text,int)
is 'retrieve last COUNT events for given formation';

grant execute on function pgautofailover.last_events(text,int)
to autoctl_node;

CREATE FUNCTION pgautofailover.last_events
(
formation_id text,
group_id int,
count int default 10
)
RETURNS SETOF pgautofailover.event LANGUAGE SQL STRICT
AS $$
with last_events as
(
select eventid, eventtime, formationid,
nodeid, groupid, nodename, nodehost, nodeport,
reportedstate, goalstate,
reportedrepstate, reportedtli, reportedlsn,
candidatepriority, replicationquorum, description
from pgautofailover.event
where formationid = formation_id
and groupid = group_id
order by eventid desc
limit count
)
select * from last_events order by eventtime, eventid;
$$;

comment on function pgautofailover.last_events(text,int,int)
is 'retrieve last COUNT events for given formation and group';

grant execute on function pgautofailover.last_events(text,int,int)
to autoctl_node;


CREATE FUNCTION pgautofailover.current_state
(
IN formation_id text default 'default',
Expand Down
16 changes: 13 additions & 3 deletions src/monitor/pgautofailover.sql
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ CREATE TABLE pgautofailover.event
reportedstate pgautofailover.replication_state not null,
goalstate pgautofailover.replication_state not null,
reportedrepstate text,
reportedtli int not null default 1 check (reportedtli > 0),
reportedlsn pg_lsn not null default '0/0',
candidatepriority int,
replicationquorum bool,
Expand Down Expand Up @@ -510,7 +511,7 @@ with last_events as
select eventid, eventtime, formationid,
nodeid, groupid, nodename, nodehost, nodeport,
reportedstate, goalstate,
reportedrepstate, reportedlsn,
reportedrepstate, reportedtli, reportedlsn,
candidatepriority, replicationquorum, description
from pgautofailover.event
order by eventid desc
Expand All @@ -522,6 +523,9 @@ $$;
comment on function pgautofailover.last_events(int)
is 'retrieve last COUNT events';

grant execute on function pgautofailover.last_events(int)
to autoctl_node;

CREATE FUNCTION pgautofailover.last_events
(
formation_id text default 'default',
Expand All @@ -534,7 +538,7 @@ with last_events as
select eventid, eventtime, formationid,
nodeid, groupid, nodename, nodehost, nodeport,
reportedstate, goalstate,
reportedrepstate, reportedlsn,
reportedrepstate, reportedtli, reportedlsn,
candidatepriority, replicationquorum, description
from pgautofailover.event
where formationid = formation_id
Expand All @@ -547,6 +551,9 @@ $$;
comment on function pgautofailover.last_events(text,int)
is 'retrieve last COUNT events for given formation';

grant execute on function pgautofailover.last_events(text,int)
to autoctl_node;

CREATE FUNCTION pgautofailover.last_events
(
formation_id text,
Expand All @@ -560,7 +567,7 @@ with last_events as
select eventid, eventtime, formationid,
nodeid, groupid, nodename, nodehost, nodeport,
reportedstate, goalstate,
reportedrepstate, reportedlsn,
reportedrepstate, reportedtli, reportedlsn,
candidatepriority, replicationquorum, description
from pgautofailover.event
where formationid = formation_id
Expand All @@ -574,6 +581,9 @@ $$;
comment on function pgautofailover.last_events(text,int,int)
is 'retrieve last COUNT events for given formation and group';

grant execute on function pgautofailover.last_events(text,int,int)
to autoctl_node;

CREATE FUNCTION pgautofailover.current_state
(
IN formation_id text default 'default',
Expand Down
10 changes: 5 additions & 5 deletions tests/pgautofailover_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,19 +669,19 @@ def get_events_str(self):
if events:
return "\n".join(
[
"%32s %8s %17s/%-17s %7s %10s %s"
"%32s %8s %17s/%-17s %10s %10s %s"
% (
"eventtime",
"name",
"state",
"goal state",
"repl st",
"lsn",
"tli:lsn",
"event",
)
]
+ [
"%32s %8s %17s/%-17s %7s %10s %s" % result
"%32s %8s %17s/%-17s %3d:%7s %10s %s" % result
for result in events
]
)
Expand Down Expand Up @@ -1274,7 +1274,7 @@ def get_events(self):
last_events_query = (
"select eventtime, nodename, "
"reportedstate, goalstate, "
"reportedrepstate, reportedlsn, description "
"reportedrepstate, reportedtli, reportedlsn, description "
"from pgautofailover.last_events('default', count => 20)"
)
return self.monitor.get_events()
Expand Down Expand Up @@ -1842,7 +1842,7 @@ def get_events(self):
last_events_query = (
"select eventtime, nodename, "
"reportedstate, goalstate, "
"reportedrepstate, reportedlsn, description "
"reportedrepstate, reportedtli, reportedlsn, description "
"from pgautofailover.last_events('default', count => 20)"
)

Expand Down

0 comments on commit cf91b7a

Please sign in to comment.