-
Notifications
You must be signed in to change notification settings - Fork 867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add telemetry about replication #4412
Merged
JLockerman
merged 1 commit into
timescale:main
from
JLockerman:jl/replication-telemetry
Jun 29, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice testing |
||
"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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest separating the functionality of gathering the stats and updating the JSON instead of putting this all in one function. For instance, defining a struct with the fields we'd like to gather and then putting that in the json or at least reading the data into data fields first and always adding the info at the end.
Now there are several places where this function can exit early, without adding any json fields or just adding some of them. Is this what we want?