Skip to content

Commit 53a3e1b

Browse files
committed
stub for s3 integration
1 parent 4ecb11f commit 53a3e1b

File tree

6 files changed

+141
-5
lines changed

6 files changed

+141
-5
lines changed

Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ include $(top_builddir)/src/Makefile.global
7676
include $(top_srcdir)/contrib/contrib-global.mk
7777
endif
7878

79+
ifndef S3_DIR
80+
ifneq ("$(wildcard $(abspath $(top_pbk_srcdir))/../s3)", "")
81+
S3_DIR = $(abspath $(CURDIR))/../s3
82+
endif
83+
endif
84+
85+
ifdef S3_DIR
86+
LDFLAGS += -lcurl
87+
CFLAGS += $(shell pkg-config --cflags libxml-2.0) -DPBCKP_S3=1
88+
LDFLAGS += $(shell pkg-config --libs libxml-2.0)
89+
OBJS += $(S3_DIR)/s3.o
90+
endif
91+
7992
#
8093
PG_CPPFLAGS = -I$(libpq_srcdir) ${PTHREAD_CFLAGS} -I$(top_pbk_srcdir)src -I$(BORROW_DIR)
8194
PG_CPPFLAGS += -I$(top_pbk_srcdir)src/fu_util -Wno-declaration-after-statement
@@ -87,6 +100,9 @@ PG_LIBS_INTERNAL = $(libpq_pgport) ${PTHREAD_CFLAGS}
87100

88101
# additional dependencies on borrowed files
89102
src/backup.o src/catchup.o src/pg_probackup.o: $(BORROW_DIR)/streamutil.h
103+
ifdef S3_DIR
104+
src/backup.o src/catchup.o src/pg_probackup.o: $(S3_DIR)/s3.o
105+
endif
90106
src/stream.o $(BORROW_DIR)/receivelog.o $(BORROW_DIR)/streamutil.o $(BORROW_DIR)/walmethods.o: $(BORROW_DIR)/receivelog.h
91107
$(BORROW_DIR)/receivelog.h: $(BORROW_DIR)/walmethods.h
92108

src/backup.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ static void do_backup_pg(InstanceState *instanceState, PGconn *backup_conn,
4646

4747
static void pg_switch_wal(PGconn *conn);
4848

49-
static void pg_stop_backup(InstanceState *instanceState, pgBackup *backup, PGconn *pg_startbackup_conn, PGNodeInfo *nodeInfo);
5049

5150
static void check_external_for_tablespaces(parray *external_list,
5251
PGconn *backup_conn);
@@ -1827,7 +1826,7 @@ pg_stop_backup_write_file_helper(const char *path, const char *filename, const c
18271826
/*
18281827
* Notify end of backup to PostgreSQL server.
18291828
*/
1830-
static void
1829+
void
18311830
pg_stop_backup(InstanceState *instanceState, pgBackup *backup, PGconn *pg_startbackup_conn,
18321831
PGNodeInfo *nodeInfo)
18331832
{

src/pg_probackup.c

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ int64 ttl = -1;
164164
static char *expire_time_string = NULL;
165165
static pgSetBackupParams *set_backup_params = NULL;
166166

167+
#ifdef PBCKP_S3
168+
/* S3 options */
169+
S3_protocol s3_protocol;
170+
char* s3_target_bucket = NULL;
171+
#endif
172+
167173
/* ================ backupState =========== */
168174
static char *backup_id_string = NULL;
169175
pgBackup current;
@@ -174,6 +180,9 @@ static bool help_opt = false;
174180
static void opt_incr_restore_mode(ConfigOption *opt, const char *arg);
175181
static void opt_backup_mode(ConfigOption *opt, const char *arg);
176182
static void opt_show_format(ConfigOption *opt, const char *arg);
183+
#ifdef PBCKP_S3
184+
static void opt_s3_protocol(ConfigOption *opt, const char *arg);
185+
#endif
177186

178187
static void compress_init(ProbackupSubcmd const subcmd);
179188

@@ -270,6 +279,12 @@ static ConfigOption cmd_options[] =
270279
{ 'I', 170, "ttl", &ttl, SOURCE_CMD_STRICT, SOURCE_DEFAULT, 0, OPTION_UNIT_S, option_get_value},
271280
{ 's', 171, "expire-time", &expire_time_string, SOURCE_CMD_STRICT },
272281

282+
#ifdef PBCKP_S3
283+
/* S3 options */
284+
{ 'f', 245, "s3", opt_s3_protocol, SOURCE_CMD_STRICT },
285+
{ 's', 246, "target-bucket", &s3_target_bucket, SOURCE_CMD_STRICT },
286+
#endif
287+
273288
/* options for backward compatibility
274289
* TODO: remove in 3.0.0
275290
*/
@@ -952,6 +967,19 @@ main(int argc, char *argv[])
952967

953968
compress_init(backup_subcmd);
954969

970+
#ifdef PBCKP_S3
971+
if (s3_protocol != S3_INVALID_PROTOCOL)
972+
{
973+
char* s3_config_file="";
974+
read_s3_config(s3_config_file);
975+
}
976+
else
977+
{
978+
if (s3_target_bucket != NULL)
979+
elog(WARNING, "You cannot specify s3-target without using --s3 option with name of protocol");
980+
}
981+
#endif
982+
955983
/* do actual operation */
956984
switch (backup_subcmd)
957985
{
@@ -964,11 +992,27 @@ main(int argc, char *argv[])
964992
wal_file_path, wal_file_name, batch_size, !no_validate_wal);
965993
break;
966994
case ADD_INSTANCE_CMD:
967-
return do_add_instance(instanceState, &instance_config);
995+
{
996+
int err = 0;
997+
err = do_add_instance(instanceState, &instance_config);
998+
#ifdef PBCKP_S3
999+
if (err == 0 && s3_protocol != S3_INVALID_PROTOCOL)
1000+
err = do_S3_write_config(&instance_config);
1001+
#endif
1002+
return err;
1003+
}
9681004
case DELETE_INSTANCE_CMD:
9691005
return do_delete_instance(instanceState);
9701006
case INIT_CMD:
971-
return do_init(catalogState);
1007+
{
1008+
int err = 0;
1009+
err = do_init(catalogState);
1010+
#ifdef PBCKP_S3
1011+
if (err == 0 && s3_protocol != S3_INVALID_PROTOCOL)
1012+
err = S3_pre_start_check(config);
1013+
#endif
1014+
return err;
1015+
}
9721016
case BACKUP_CMD:
9731017
{
9741018
current.stream = stream_wal;
@@ -983,13 +1027,21 @@ main(int argc, char *argv[])
9831027
elog(ERROR, "required parameter not specified: BACKUP_MODE "
9841028
"(-b, --backup-mode)");
9851029

1030+
#ifdef PBCKP_S3
1031+
if (s3_protocol != S3_INVALID_PROTOCOL)
1032+
return do_S3_backup(instanceState, set_backup_params, start_time);
1033+
#endif
9861034
return do_backup(instanceState, set_backup_params,
9871035
no_validate, no_sync, backup_logs, start_time);
9881036
}
9891037
case CATCHUP_CMD:
9901038
return do_catchup(catchup_source_pgdata, catchup_destination_pgdata, num_threads, !no_sync,
9911039
exclude_absolute_paths_list, exclude_relative_paths_list);
9921040
case RESTORE_CMD:
1041+
#ifdef PBCKP_S3
1042+
if (s3_protocol != S3_INVALID_PROTOCOL)
1043+
return do_S3_restore(instanceState, current.backup_id);
1044+
#endif
9931045
return do_restore_or_validate(instanceState, current.backup_id,
9941046
recovery_target_options,
9951047
restore_params, no_sync);
@@ -1009,6 +1061,10 @@ main(int argc, char *argv[])
10091061
restore_params,
10101062
no_sync);
10111063
case SHOW_CMD:
1064+
#ifdef PBCKP_S3
1065+
if (s3_protocol != S3_INVALID_PROTOCOL)
1066+
return do_S3_show(instanceState);
1067+
#endif
10121068
return do_show(catalogState, instanceState, current.backup_id, show_archive);
10131069
case DELETE_CMD:
10141070

@@ -1197,3 +1253,34 @@ opt_exclude_path(ConfigOption *opt, const char *arg)
11971253
else
11981254
opt_parser_add_to_parray_helper(&exclude_relative_paths_list, arg);
11991255
}
1256+
1257+
#ifdef PBCKP_S3
1258+
static S3_protocol
1259+
parse_s3_protocol(const char *value)
1260+
{
1261+
const char *v = value;
1262+
size_t len;
1263+
1264+
/* Skip all spaces detected */
1265+
while (IsSpace(*v))
1266+
v++;
1267+
len = strlen(v);
1268+
1269+
if (len > 0 && pg_strncasecmp("MINIO", v, len) == 0)
1270+
return S3_MINIO_PROTOCOL;
1271+
if (len > 0 && pg_strncasecmp("AWS", v, len) == 0)
1272+
return S3_AWS_PROTOCOL;
1273+
else if (len > 0 && pg_strncasecmp("GOOGLE", v, len) == 0)
1274+
return S3_GOOGLE_PROTOCOL;
1275+
else if (len > 0 && pg_strncasecmp("VK", v, len) == 0)
1276+
return S3_VK_PROTOCOL;
1277+
else
1278+
return S3_INVALID_PROTOCOL;
1279+
}
1280+
1281+
static void
1282+
opt_s3_protocol(ConfigOption *opt, const char *arg)
1283+
{
1284+
s3_protocol = parse_s3_protocol(arg);
1285+
}
1286+
#endif

src/pg_probackup.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343

4444
#include "pg_probackup_state.h"
4545

46+
#ifdef PGPRO_S3
47+
#include "../s3/s3.h"
48+
#endif
49+
4650
#if defined(WIN32) && !(defined(_UCRT) && defined(__MINGW64__))
4751
#error Windows port requires compilation in MinGW64 UCRT environment
4852
#endif
@@ -800,6 +804,11 @@ extern pgBackup current;
800804
/* argv of the process */
801805
extern char** commands_args;
802806

807+
#ifdef PBCKP_S3
808+
/* S3 options */
809+
extern S3_protocol s3_protocol;
810+
#endif
811+
803812
/* in backup.c */
804813
extern int do_backup(InstanceState *instanceState, pgSetBackupParams *set_backup_params,
805814
bool no_validate, bool no_sync, bool backup_logs, time_t start_time);
@@ -810,6 +819,15 @@ extern const char *deparse_backup_mode(BackupMode mode);
810819
extern void process_block_change(ForkNumber forknum, RelFileNode rnode,
811820
BlockNumber blkno);
812821

822+
#ifdef PBCKP_S3
823+
/* in s3.c */
824+
extern int do_S3_backup(InstanceState *instanceState,
825+
pgSetBackupParams *set_backup_params,time_t start_time);
826+
extern int do_S3_show(InstanceState *instanceState);
827+
extern int do_S3_restore(InstanceState *instanceState, time_t target_backup_id);
828+
extern int do_S3_write_config(InstanceConfig *instance);
829+
#endif
830+
813831
/* in catchup.c */
814832
extern int do_catchup(const char *source_pgdata, const char *dest_pgdata, int num_threads, bool sync_dest_files,
815833
parray *exclude_absolute_paths_list, parray *exclude_relative_paths_list);
@@ -1248,6 +1266,7 @@ extern parray *backup_files_list;
12481266

12491267
extern void pg_start_backup(const char *label, bool smooth, pgBackup *backup,
12501268
PGNodeInfo *nodeInfo, PGconn *conn);
1269+
extern void pg_stop_backup(InstanceState *instanceState, pgBackup *backup, PGconn *pg_startbackup_conn, PGNodeInfo *nodeInfo);
12511270
extern void pg_silent_client_messages(PGconn *conn);
12521271
extern void pg_create_restore_point(PGconn *conn, time_t backup_start_time);
12531272
extern void pg_stop_backup_send(PGconn *conn, int server_version, bool is_started_on_replica, char **query_text);

src/utils/file.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
#include "file.h"
99
#include "storage/checksum.h"
1010

11+
#ifdef PBCKP_S3
12+
#include "../s3/s3.h"
13+
#endif
14+
1115
#define PRINTF_BUF_SIZE 1024
1216

1317
static __thread unsigned long fio_fdset = 0;
@@ -3417,6 +3421,13 @@ static pioDrive_i remoteDrive;
34173421
pioDrive_i
34183422
pioDriveForLocation(fio_location loc)
34193423
{
3424+
if (loc == FIO_CLOUD_HOST)
3425+
#ifdef PBCKP_S3
3426+
return cloudDrive;
3427+
#else
3428+
elog(ERROR, "NO CLOUD DRIVE YET");
3429+
#endif
3430+
34203431
if (fio_is_remote(loc))
34213432
return remoteDrive;
34223433
else
@@ -4915,4 +4926,7 @@ init_pio_objects(void)
49154926

49164927
localDrive = bindref_pioDrive($alloc(pioLocalDrive));
49174928
remoteDrive = bindref_pioDrive($alloc(pioRemoteDrive));
4929+
#ifdef PBCKP_S3
4930+
create_pioCloudeDrive();
4931+
#endif
49184932
}

src/utils/file.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ typedef enum
8888
FIO_LOCAL_HOST, /* data is locate at local host */
8989
FIO_DB_HOST, /* data is located at Postgres server host */
9090
FIO_BACKUP_HOST, /* data is located at backup host */
91-
FIO_REMOTE_HOST /* date is located at remote host */
91+
FIO_REMOTE_HOST, /* date is located at remote host */
92+
FIO_CLOUD_HOST /* date is located at cloud (S3) */
9293
} fio_location;
9394

9495
extern fio_location MyLocation;

0 commit comments

Comments
 (0)