-
Notifications
You must be signed in to change notification settings - Fork 849
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds telemetry about replication status to our telemetry gatherer. It adds a new sub object `replication` containing two fields: - `is_wal_receiver` is a boolean which is true if-and-only-if the current cluster has a `wal_receiver`. - `num_wal_senders` is the number of `wal_senders` that the current cluster has.
- Loading branch information
1 parent
8f9975d
commit 97ab772
Showing
7 changed files
with
171 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
# Add all *.c to sources in upperlevel directory | ||
set(SOURCES | ||
${CMAKE_CURRENT_SOURCE_DIR}/functions.c ${CMAKE_CURRENT_SOURCE_DIR}/stats.c | ||
${CMAKE_CURRENT_SOURCE_DIR}/functions.c | ||
${CMAKE_CURRENT_SOURCE_DIR}/replication.c | ||
${CMAKE_CURRENT_SOURCE_DIR}/stats.c | ||
${CMAKE_CURRENT_SOURCE_DIR}/telemetry_metadata.c | ||
${CMAKE_CURRENT_SOURCE_DIR}/telemetry.c) | ||
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* This file and its contents are licensed under the Apache License 2.0. | ||
* Please see the included NOTICE for copyright information and | ||
* LICENSE-APACHE for a copy of the license. | ||
*/ | ||
#include <postgres.h> | ||
|
||
#include "replication.h" | ||
|
||
#include <executor/spi.h> | ||
|
||
ReplicationInfo | ||
ts_telemetry_replication_info_gather(void) | ||
{ | ||
int res; | ||
bool isnull; | ||
Datum data; | ||
ReplicationInfo info = { | ||
.got_num_wal_senders = false, | ||
.got_is_wal_receiver = false, | ||
}; | ||
|
||
if (SPI_connect() != SPI_OK_CONNECT) | ||
return info; | ||
|
||
res = SPI_execute("SELECT cast(count(pid) as int) from pg_catalog.pg_stat_get_wal_senders() " | ||
"WHERE pid is not null", | ||
true, /* read_only */ | ||
0 /*count*/ | ||
); | ||
|
||
if (res >= 0) | ||
{ | ||
data = SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull); | ||
info.num_wal_senders = DatumGetInt32(data); | ||
info.got_num_wal_senders = true; | ||
} | ||
|
||
/* use count() > 0 in case they start having pg_stat_get_wal_receiver() | ||
* return no rows when the DB isn't a replica */ | ||
res = SPI_execute("SELECT count(pid) > 0 from pg_catalog.pg_stat_get_wal_receiver() WHERE pid " | ||
"is not null", | ||
true, /* read_only */ | ||
0 /*count*/ | ||
); | ||
if (res >= 0) | ||
{ | ||
data = SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull); | ||
info.is_wal_receiver = DatumGetBool(data); | ||
info.got_is_wal_receiver = true; | ||
} | ||
|
||
SPI_finish(); | ||
|
||
return info; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* This file and its contents are licensed under the Apache License 2.0. | ||
* Please see the included NOTICE for copyright information and | ||
* LICENSE-APACHE for a copy of the license. | ||
*/ | ||
#ifndef TIMESCALEDB_TELEMETRY_REPLICATION_H | ||
#define TIMESCALEDB_TELEMETRY_REPLICATION_H | ||
|
||
#include <postgres.h> | ||
|
||
#include "utils.h" | ||
|
||
typedef struct ReplicationInfo | ||
{ | ||
bool got_num_wal_senders; | ||
int32 num_wal_senders; | ||
|
||
bool got_is_wal_receiver; | ||
bool is_wal_receiver; | ||
} ReplicationInfo; | ||
|
||
extern ReplicationInfo ts_telemetry_replication_info_gather(void); | ||
|
||
#endif /* TIMESCALEDB_TELEMETRY_REPLICATION_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# This file and its contents are licensed under the Timescale License. | ||
# Please see the included NOTICE for copyright information and | ||
# LICENSE-TIMESCALE for a copy of the license. | ||
|
||
use strict; | ||
use warnings; | ||
use TimescaleNode; | ||
use TestLib; | ||
use Data::Dumper; | ||
use Test::More tests => 4; | ||
|
||
# This test checks that the extension state is handled correctly | ||
# across multiple sessions. Specifically, if the extension state | ||
# changes in one session (e.g., the extension is created or dropped), | ||
# this should be reflected in other concurrent sessions. | ||
# | ||
# To test this, we start one background psql session that stays open | ||
# for the duration of the tests and then change the extension state | ||
# from other sessions. | ||
my $node_primary = TimescaleNode->create( | ||
'primary', | ||
allows_streaming => 1, | ||
auth_extra => [ '--create-role', 'repl_role' ]); | ||
my $backup_name = 'my_backup'; | ||
|
||
# Take backup | ||
$node_primary->backup($backup_name); | ||
|
||
# Create streaming standby linking to primary | ||
my $node_standby = PostgresNode->get_new_node('standby_1'); | ||
$node_standby->init_from_backup($node_primary, $backup_name, | ||
has_streaming => 1); | ||
$node_standby->start; | ||
|
||
# Wait for standby to catch up | ||
$node_primary->wait_for_catchup($node_standby, 'replay', | ||
$node_primary->lsn('insert')); | ||
|
||
|
||
my $result = $node_primary->safe_psql('postgres', | ||
"SELECT get_telemetry_report()->'replication'->>'num_wal_senders'"); | ||
is($result, qq(1), 'number of wal senders on primary'); | ||
|
||
$result = $node_primary->safe_psql('postgres', | ||
"SELECT get_telemetry_report()->'replication'->>'is_wal_receiver'"); | ||
is($result, qq(false), 'primary is wal receiver'); | ||
|
||
$result = $node_standby->safe_psql('postgres', | ||
"SELECT get_telemetry_report()->'replication'->>'num_wal_senders'"); | ||
is($result, qq(0), 'number of wal senders on standby'); | ||
|
||
$result = $node_standby->safe_psql('postgres', | ||
"SELECT get_telemetry_report()->'replication'->>'is_wal_receiver'"); | ||
is($result, qq(true), 'standby is wal receiver'); | ||
|
||
done_testing(); | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters