Skip to content

Commit f0939cf

Browse files
committed
cfs backup
1 parent 2bd2b6e commit f0939cf

File tree

5 files changed

+108
-13
lines changed

5 files changed

+108
-13
lines changed

src/backup.c

Lines changed: 89 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ static XLogRecPtr get_last_ptrack_lsn(void);
118118
static void check_server_version(void);
119119
static void check_system_identifiers(void);
120120
static void confirm_block_size(const char *name, int blcksz);
121+
static void set_cfs_datafiles(parray *files, const char *root, char *relative, size_t i);
121122

122123

123124
#define disconnect_and_exit(code) \
@@ -1878,6 +1879,7 @@ backup_files(void *arg)
18781879
struct stat buf;
18791880

18801881
pgFile *file = (pgFile *) parray_get(arguments->backup_files_list, i);
1882+
elog(VERBOSE, "Copying file: \"%s\" ", file->path);
18811883
if (__sync_lock_test_and_set(&file->lock, 1) != 0)
18821884
continue;
18831885

@@ -1918,8 +1920,9 @@ backup_files(void *arg)
19181920
if (S_ISREG(buf.st_mode))
19191921
{
19201922
/* copy the file into backup */
1921-
if (file->is_datafile)
1923+
if (file->is_datafile && !file->is_cfs)
19221924
{
1925+
/* backup block by block if datafile AND not compressed by cfs*/
19231926
if (!backup_data_file(arguments->from_root,
19241927
arguments->to_root, file,
19251928
arguments->prev_backup_start_lsn))
@@ -1965,12 +1968,14 @@ parse_backup_filelist_filenames(parray *files, const char *root)
19651968
int sscanf_result;
19661969

19671970
relative = GetRelativePath(file->path, root);
1971+
file->is_cfs = false;
19681972

19691973
elog(VERBOSE, "-----------------------------------------------------: %s", relative);
19701974
if (path_is_prefix_of_path("global", relative))
19711975
{
19721976
file->tblspcOid = GLOBALTABLESPACE_OID;
19731977
sscanf_result = sscanf(relative, "global/%s", filename);
1978+
elog(VERBOSE, "global sscanf result: %i", sscanf_result);
19741979
if (strcmp(relative, "global") == 0)
19751980
{
19761981
Assert(S_ISDIR(file->mode));
@@ -1986,6 +1991,7 @@ parse_backup_filelist_filenames(parray *files, const char *root)
19861991
{
19871992
file->tblspcOid = DEFAULTTABLESPACE_OID;
19881993
sscanf_result = sscanf(relative, "base/%u/%s", &(file->dbOid), filename);
1994+
elog(VERBOSE, "base sscanf result: %i", sscanf_result);
19891995
if (strcmp(relative, "base") == 0)
19901996
{
19911997
Assert(S_ISDIR(file->mode));
@@ -2007,6 +2013,8 @@ parse_backup_filelist_filenames(parray *files, const char *root)
20072013
char *temp_relative_path = palloc(MAXPGPATH);
20082014

20092015
sscanf_result = sscanf(relative, "pg_tblspc/%u/%s", &(file->tblspcOid), temp_relative_path);
2016+
elog(VERBOSE, "pg_tblspc sscanf result: %i", sscanf_result);
2017+
20102018
if (strcmp(relative, "pg_tblspc") == 0)
20112019
{
20122020
Assert(S_ISDIR(file->mode));
@@ -2022,21 +2030,43 @@ parse_backup_filelist_filenames(parray *files, const char *root)
20222030
/*continue parsing */
20232031
sscanf_result = sscanf(temp_relative_path+strlen(TABLESPACE_VERSION_DIRECTORY)+1, "%u/%s",
20242032
&(file->dbOid), filename);
2033+
elog(VERBOSE, "TABLESPACE_VERSION_DIRECTORY sscanf result: %i", sscanf_result);
20252034

2026-
if (sscanf_result == 0)
2035+
if (sscanf_result == -1)
20272036
{
2028-
elog(VERBOSE, "the TABLESPACE_VERSION_DIRECTORY itself, filepath %s", relative);
2037+
elog(VERBOSE, "The TABLESPACE_VERSION_DIRECTORY itself, filepath %s", relative);
2038+
}
2039+
else if (sscanf_result == 0)
2040+
{
2041+
/* Found file in pg_tblspc/tblsOid/TABLESPACE_VERSION_DIRECTORY
2042+
Legal only in case of 'pg_compression'
2043+
*/
2044+
if (strcmp(relative + strlen(relative) - strlen("pg_compression"), "pg_compression") == 0)
2045+
{
2046+
elog(VERBOSE, "Found pg_compression file in TABLESPACE_VERSION_DIRECTORY, filepath %s", relative);
2047+
/*Set every datafile in tablespace as is_cfs */
2048+
set_cfs_datafiles(files, root, relative, i);
2049+
}
2050+
else
2051+
{
2052+
elog(VERBOSE, "Found illegal file in TABLESPACE_VERSION_DIRECTORY, filepath %s", relative);
2053+
}
2054+
20292055
}
20302056
else if (sscanf_result == 1)
20312057
{
20322058
Assert(S_ISDIR(file->mode));
20332059
elog(VERBOSE, "dboid %u, filepath %s", file->dbOid, relative);
20342060
file->is_database = true;
20352061
}
2036-
else
2062+
else if (sscanf_result == 2)
20372063
{
20382064
elog(VERBOSE, "dboid %u, filename %s, filepath %s", file->dbOid, filename, relative);
20392065
}
2066+
else
2067+
{
2068+
elog(VERBOSE, "Illegal file filepath %s", relative);
2069+
}
20402070
}
20412071
}
20422072
else
@@ -2081,7 +2111,7 @@ parse_backup_filelist_filenames(parray *files, const char *root)
20812111
* Check that and do not mark them with 'is_datafile' flag.
20822112
*/
20832113
char *forkNameptr;
2084-
char *suffix = palloc(MAXPGPATH);;
2114+
char *suffix = palloc(MAXPGPATH);
20852115

20862116
forkNameptr = strstr(filename, "_");
20872117
if (forkNameptr != NULL)
@@ -2108,7 +2138,16 @@ parse_backup_filelist_filenames(parray *files, const char *root)
21082138
{
21092139
/* first segment of the relfile */
21102140
elog(VERBOSE, "relOid %u, segno %d, filepath %s", file->relOid, 0, relative);
2111-
file->is_datafile = true;
2141+
if (strcmp(relative + strlen(relative) - strlen("cfm"), "cfm") == 0)
2142+
{
2143+
/* reloid.cfm */
2144+
elog(VERBOSE, "Found cfm file %s", relative);
2145+
}
2146+
else
2147+
{
2148+
elog(VERBOSE, "Found first segment of the relfile %s", relative);
2149+
file->is_datafile = true;
2150+
}
21122151
}
21132152
else if (sscanf_result == 2)
21142153
{
@@ -2126,9 +2165,52 @@ parse_backup_filelist_filenames(parray *files, const char *root)
21262165
}
21272166
}
21282167
}
2129-
}
2168+
}
2169+
}
2170+
2171+
/* If file is equal to pg_compression, then we consider this tablespace as
2172+
* cfs-compressed and should mark every file in this tablespace as cfs-file
2173+
* Setting is_cfs is done via going back through 'files' set every file
2174+
* that contain cfs_tablespace in his path as 'is_cfs'
2175+
* Goings back through array 'files' is valid option possible because of current
2176+
* sort rules:
2177+
* tblspcOid/TABLESPACE_VERSION_DIRECTORY
2178+
* tblspcOid/TABLESPACE_VERSION_DIRECTORY/dboid
2179+
* tblspcOid/TABLESPACE_VERSION_DIRECTORY/dboid/1
2180+
* tblspcOid/TABLESPACE_VERSION_DIRECTORY/dboid/1.cfm
2181+
* tblspcOid/TABLESPACE_VERSION_DIRECTORY/pg_compression
2182+
*/
2183+
static void
2184+
set_cfs_datafiles(parray *files, const char *root, char *relative, size_t i)
2185+
{
2186+
int len;
2187+
size_t p;
2188+
char *cfs_tblspc_path;
2189+
2190+
cfs_tblspc_path = strdup(relative);
2191+
len = strlen("/pg_compression");
2192+
cfs_tblspc_path[strlen(cfs_tblspc_path) - len] = 0;
2193+
elog(VERBOSE, "CFS DIRECTORY %s, pg_compression path: %s", cfs_tblspc_path, relative);
2194+
2195+
for (p = i; p != 0; p--)
2196+
{
2197+
char *relative_prev_file;
2198+
pgFile *prev_file = (pgFile *) parray_get(files, p);
2199+
relative_prev_file = GetRelativePath(prev_file->path, root);
2200+
//elog(VERBOSE, "P: %d, CHECKING file %s", p, relative_prev_file);
2201+
if (strstr(relative_prev_file, cfs_tblspc_path) != NULL)
2202+
{
2203+
if (S_ISREG(prev_file->mode) && prev_file->is_datafile)
2204+
{
2205+
elog(VERBOSE, "Setting as 'is_cfs' file %s, fork %s",
2206+
relative_prev_file, prev_file->forkName);
2207+
prev_file->is_cfs = true;
2208+
}
2209+
}
2210+
}
21302211
}
21312212

2213+
21322214
/*
21332215
* Output the list of files to backup catalog DATABASE_FILE_LIST
21342216
*/

src/data.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,13 @@ backup_data_file(const char *from_root, const char *to_root,
317317
file->path, strerror(errno));
318318
}
319319

320-
if (file->size % BLCKSZ != 0)
320+
if (!file->is_cfs)
321321
{
322-
fclose(in);
323-
elog(ERROR, "File: %s, invalid file size %lu", file->path, file->size);
322+
if (file->size % BLCKSZ != 0)
323+
{
324+
fclose(in);
325+
elog(ERROR, "File: %s, invalid file size %lu", file->path, file->size);
326+
}
324327
}
325328

326329
/*
@@ -470,6 +473,7 @@ restore_data_file(const char *from_root,
470473
if (header.block < blknum)
471474
elog(ERROR, "backup is broken at block %u", blknum);
472475

476+
elog(VERBOSE, "file %s, header compressed size %d", file->path, header.compressed_size);
473477
Assert(header.compressed_size <= BLCKSZ);
474478

475479
read_len = fread(compressed_page.data, 1,

src/dir.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -678,9 +678,11 @@ print_file_list(FILE *out, const parray *files, const char *root)
678678
path = GetRelativePath(path, root);
679679

680680
fprintf(out, "{\"path\":\"%s\", \"size\":\"%lu\",\"mode\":\"%u\","
681-
"\"is_datafile\":\"%u\", \"crc\":\"%u\", \"compress_alg\":\"%s\"",
681+
"\"is_datafile\":\"%u\", \"is_cfs\":\"%u\", \"crc\":\"%u\","
682+
"\"compress_alg\":\"%s\"",
682683
path, (unsigned long) file->write_size, file->mode,
683-
file->is_datafile?1:0, file->crc, deparse_compress_alg(file->compress_alg));
684+
file->is_datafile?1:0, file->is_cfs?1:0, file->crc,
685+
deparse_compress_alg(file->compress_alg));
684686

685687
if (file->is_datafile)
686688
fprintf(out, ",\"segno\":\"%d\"", file->segno);
@@ -853,6 +855,7 @@ dir_read_file_list(const char *root, const char *file_txt)
853855
uint64 write_size,
854856
mode, /* bit length of mode_t depends on platforms */
855857
is_datafile,
858+
is_cfs,
856859
crc,
857860
segno;
858861
pgFile *file;
@@ -861,6 +864,7 @@ dir_read_file_list(const char *root, const char *file_txt)
861864
get_control_value(buf, "size", NULL, &write_size, true);
862865
get_control_value(buf, "mode", NULL, &mode, true);
863866
get_control_value(buf, "is_datafile", NULL, &is_datafile, true);
867+
get_control_value(buf, "is_cfs", NULL, &is_cfs, false);
864868
get_control_value(buf, "crc", NULL, &crc, true);
865869

866870
/* optional fields */
@@ -878,6 +882,7 @@ dir_read_file_list(const char *root, const char *file_txt)
878882
file->write_size = (size_t) write_size;
879883
file->mode = (mode_t) mode;
880884
file->is_datafile = is_datafile ? true : false;
885+
file->is_cfs = is_cfs ? true : false;
881886
file->crc = (pg_crc32) crc;
882887
file->compress_alg = parse_compress_alg(compress_alg_string);
883888
if (linked[0])

src/restore.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,8 @@ restore_files(void *arg)
686686
* block and have BackupPageHeader meta information, so we cannot just
687687
* copy the file from backup.
688688
*/
689-
if (file->is_datafile)
689+
elog(VERBOSE, "Restoring file %s, is_datafile %i, is_cfs %i", file->path, file->is_datafile?1:0, file->is_cfs?1:0);
690+
if (file->is_datafile && !file->is_cfs)
690691
restore_data_file(from_root, pgdata, file, arguments->backup);
691692
else
692693
copy_file(from_root, pgdata, file);

src/utils/logger.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ write_elevel(FILE *stream, int elevel)
8686
{
8787
switch (elevel)
8888
{
89+
case VERBOSE:
90+
fputs("VERBOSE: ", stream);
91+
break;
8992
case LOG:
9093
fputs("LOG: ", stream);
9194
break;

0 commit comments

Comments
 (0)