From 8a13c5b2b092896be80aeb3055226d93d34768bb Mon Sep 17 00:00:00 2001 From: Artem Gavrilov Date: Mon, 5 Aug 2024 15:36:30 +0200 Subject: [PATCH] PG-946 fix telemetry filenames (#13) * PG-946 Fix telemetry filenames * PG-946 Update tests --- expected/basic.out | 6 +++--- expected/basic_1.out | 6 +++--- percona_pg_telemetry.c | 14 +++++--------- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/expected/basic.out b/expected/basic.out index 79427bb..bf615f2 100644 --- a/expected/basic.out +++ b/expected/basic.out @@ -29,9 +29,9 @@ SELECT percona_pg_telemetry_version(); SELECT regexp_replace(regexp_replace(latest_output_filename, '\d{11,}', '', 'g'), '\d{6,}', '', 'g') AS latest_output_filename , pt_enabled FROM percona_pg_telemetry_status(); - latest_output_filename | pt_enabled --------------------------------------------------------+------------ - ./percona_pg_telemetry--.json | t + latest_output_filename | pt_enabled +----------------------------------+------------ + ./-.json | t (1 row) DROP EXTENSION percona_pg_telemetry; diff --git a/expected/basic_1.out b/expected/basic_1.out index f013f22..960aeff 100644 --- a/expected/basic_1.out +++ b/expected/basic_1.out @@ -27,9 +27,9 @@ SELECT percona_pg_telemetry_version(); SELECT regexp_replace(regexp_replace(latest_output_filename, '\d{11,}', '', 'g'), '\d{6,}', '', '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--.json | t + latest_output_filename | pt_enabled +----------------------------------------------------------------+------------ + /usr/local/percona/telemetry/pg/-.json | t (1 row) DROP EXTENSION percona_pg_telemetry; diff --git a/percona_pg_telemetry.c b/percona_pg_telemetry.c index 67b6b8f..cee3b78 100644 --- a/percona_pg_telemetry.c +++ b/percona_pg_telemetry.c @@ -49,7 +49,6 @@ 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 */ @@ -167,7 +166,7 @@ generate_filename(char *filename) time_t currentTime; time(¤tTime); - 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); @@ -240,11 +239,10 @@ load_telemery_files(void) DIR *d; struct dirent *de; uint64 system_id = GetSystemIdentifier(); - char json_file_id[MAXPGPATH]; + char filename_tail[MAXPGPATH]; char full_path[MAXPGPATH]; List *files_list = NIL; ListCell *lc = NULL; - int file_id_len; validate_dir(ptss->telemetry_path); @@ -258,12 +256,10 @@ load_telemery_files(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) { /* Construct the file full path */ snprintf(full_path, sizeof(full_path), "%s/%s", ptss->telemetry_path, de->d_name); @@ -413,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;