Skip to content
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

PG-934 PG-946 PG-947 Bugfixes #12

Merged
merged 3 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions expected/basic.out
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ SELECT percona_pg_telemetry_version();
SELECT regexp_replace(regexp_replace(latest_output_filename, '\d{11,}', '<INSTANCE ID>', 'g'), '\d{6,}', '<TIMESTAMP>', 'g') AS latest_output_filename
, pt_enabled
FROM percona_pg_telemetry_status();
latest_output_filename | pt_enabled
-------------------------------------------------------+------------
./percona_pg_telemetry-<INSTANCE ID>-<TIMESTAMP>.json | t
latest_output_filename | pt_enabled
----------------------------------+------------
./<TIMESTAMP>-<INSTANCE ID>.json | t
(1 row)

DROP EXTENSION percona_pg_telemetry;
6 changes: 3 additions & 3 deletions expected/basic_1.out
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ SELECT percona_pg_telemetry_version();
SELECT regexp_replace(regexp_replace(latest_output_filename, '\d{11,}', '<INSTANCE ID>', 'g'), '\d{6,}', '<TIMESTAMP>', 'g') AS latest_output_filename
, pt_enabled
FROM percona_pg_telemetry_status();
latest_output_filename | pt_enabled
-------------------------------------------------------------------------------------+------------
/usr/local/percona/telemetry/pg/percona_pg_telemetry-<INSTANCE ID>-<TIMESTAMP>.json | t
latest_output_filename | pt_enabled
----------------------------------------------------------------+------------
/usr/local/percona/telemetry/pg/<TIMESTAMP>-<INSTANCE ID>.json | t
(1 row)

DROP EXTENSION percona_pg_telemetry;
88 changes: 70 additions & 18 deletions percona_pg_telemetry.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ PG_MODULE_MAGIC;

/* General defines */
#define PT_BUILD_VERSION "1.0"
#define PT_FILENAME_BASE "percona_pg_telemetry"
#define PT_FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)

/* Init and exported functions */
void _PG_init(void);
Expand All @@ -74,10 +74,16 @@ static shmem_request_hook_type prev_shmem_request_hook = NULL;
static BgwHandleStatus setup_background_worker(const char *bgw_function_name, const char *bgw_name, const char *bgw_type, Oid datid, pid_t bgw_notify_pid);
static void start_leader(void);
static long server_uptime(void);
static void cleaup_telemetry_dir(void);
static void load_telemery_files(void);
static char *generate_filename(char *filename);
static bool validate_dir(char *folder_path);

#if PG_VERSION_NUM >= 130000
static int compare_file_names(const ListCell *a, const ListCell *b);
#else
static int compare_file_names(const void *a, const void *b);
#endif

/* Database information collection and writing to file */
static void write_pg_settings(void);
static List *get_database_list(void);
Expand Down Expand Up @@ -160,7 +166,7 @@ generate_filename(char *filename)
time_t currentTime;

time(&currentTime);
pg_snprintf(f_name, MAXPGPATH, "%s-%lu-%ld.json", PT_FILENAME_BASE, system_id, currentTime);
pg_snprintf(f_name, MAXPGPATH, "%ld-%lu.json", currentTime, system_id);

join_path_components(filename, ptss->telemetry_path, f_name);

Expand Down Expand Up @@ -198,12 +204,17 @@ telemetry_file_is_valid(void)
}

/*
*
* Adds a new filename to the next position in the circular buffer. If position already has a filename
* (i.e. we made full circle), then it will try to remove this file from filesystem.
* Returns the previous filename that was in the position.
*/
static char *
telemetry_file_next(char *filename)
{
char *curr_oldest = telemetry_curr_filename();
/* Get current file that will become previous */
char *previous = telemetry_curr_filename();

/* Increment the index. We are using a circular buffer. */
ptss->curr_file_index = (ptss->curr_file_index + 1) % files_to_keep;

/* Remove the existing file on this location if valid */
Expand All @@ -212,22 +223,26 @@ telemetry_file_next(char *filename)
PathNameDeleteTemporaryFile(ptss->telemetry_filenames[ptss->curr_file_index], false);
}

/* Add new file to the new current position */
telemetry_add_filename(filename);

return (*curr_oldest) ? curr_oldest : NULL;
/* Return previous file */
return (*previous) ? previous : NULL;
}

/*
*
* Load all telemetry files from the telemetry directory.
*/
static void
cleaup_telemetry_dir(void)
load_telemery_files(void)
{
DIR *d;
struct dirent *de;
uint64 system_id = GetSystemIdentifier();
char json_file_id[MAXPGPATH];
int file_id_len;
char filename_tail[MAXPGPATH];
char full_path[MAXPGPATH];
List *files_list = NIL;
ListCell *lc = NULL;

validate_dir(ptss->telemetry_path);

Expand All @@ -241,20 +256,54 @@ cleaup_telemetry_dir(void)
ptss->telemetry_path)));
}

pg_snprintf(json_file_id, sizeof(json_file_id), "%s-%lu", PT_FILENAME_BASE, system_id);
file_id_len = strlen(json_file_id);

pg_snprintf(filename_tail, sizeof(filename_tail), "%lu.json", system_id);
while ((de = ReadDir(d, ptss->telemetry_path)) != NULL)
{
if (strncmp(json_file_id, de->d_name, file_id_len) == 0)
if (strstr(de->d_name, filename_tail) != NULL)
{
telemetry_file_next(de->d_name);
/* Construct the file full path */
snprintf(full_path, sizeof(full_path), "%s/%s", ptss->telemetry_path, de->d_name);

files_list = lappend(files_list, pstrdup(full_path));
}
}

#if PG_VERSION_NUM >= 130000
list_sort(files_list, compare_file_names);
#else
files_list = list_qsort(files_list, compare_file_names);
#endif

foreach(lc, files_list)
{
char *file_path = lfirst(lc);
telemetry_file_next(file_path);
}

list_free_deep(files_list);
FreeDir(d);
}


#if PG_VERSION_NUM >= 130000
static int
compare_file_names(const ListCell *a, const ListCell *b)
{
char *fna = (char *) lfirst(a);
char *fnb = (char *) lfirst(b);
return strcmp(fna, fnb);
}

#else
static int
compare_file_names(const void *a, const void *b)
{
char *fna = (char *) lfirst(*(ListCell **) a);
char *fnb = (char *) lfirst(*(ListCell **) b);
return strcmp(fna, fnb);
}
#endif

/*
* telemetry_path
*/
Expand Down Expand Up @@ -360,7 +409,7 @@ pt_shmem_init(void)

/* Set paths */
strncpy(ptss->telemetry_path, t_folder, MAXPGPATH);
pg_snprintf(ptss->dbtemp_filepath, MAXPGPATH, "%s/%s-%lu.temp", ptss->telemetry_path, PT_FILENAME_BASE, system_id);
pg_snprintf(ptss->dbtemp_filepath, MAXPGPATH, "%s/%lu.temp", ptss->telemetry_path, system_id);

/* Let's be optimistic here. No error code and no file currently being written. */
ptss->error_code = PT_SUCCESS;
Expand Down Expand Up @@ -850,8 +899,8 @@ percona_pg_telemetry_main(Datum main_arg)
/* Initialize shmem */
pt_shmem_init();

/* Cleanup the directory */
cleaup_telemetry_dir();
/* Load existing telemetry files */
load_telemery_files();

/* Set up connection */
BackgroundWorkerInitializeConnectionByOid(InvalidOid, InvalidOid, 0);
Expand Down Expand Up @@ -974,6 +1023,9 @@ percona_pg_telemetry_main(Datum main_arg)
/* Generate and save the filename */
telemetry_file_next(generate_filename(filename));

/* Change the file permissions before making it available to the agent. */
chmod(ptss->dbtemp_filepath, PT_FILE_MODE);

/* Let's rename the temp file so that agent can pick it up. */
if (rename(ptss->dbtemp_filepath, telemetry_curr_filename()) < 0)
{
Expand Down