-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathspill.c
151 lines (135 loc) · 3.1 KB
/
spill.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*-----------------------------------------------------------------------------
* spill.c
*
* Copyright (c) 2017-2021, Postgres Professional
*
*-----------------------------------------------------------------------------
*/
#include "postgres.h"
#include <unistd.h>
#include <sys/stat.h>
#include "storage/fd.h"
#include "spill.h"
#include "pgstat.h"
#include "multimaster.h"
#include "logger.h"
void
MtmSpillToFile(int fd, char const *data, size_t size)
{
Assert(fd >= 0);
while (size != 0)
{
int written = write(fd, data, size);
if (written <= 0)
{
close(fd);
ereport(ERROR,
(errcode_for_file_access(),
MTM_ERRMSG("pglogical_recevier failed to spill transaction to file: %m")));
}
data += written;
size -= written;
}
}
void
MtmCreateSpillDirectory(int node_id)
{
char path[MAXPGPATH];
struct dirent *spill_de;
DIR *spill_dir;
mkdir("pg_mtm", S_IRWXU);
sprintf(path, "pg_mtm/%d", node_id);
mkdir(path, S_IRWXU);
spill_dir = AllocateDir(path);
if (spill_dir == NULL)
{
ereport(PANIC,
(errcode_for_file_access(),
MTM_ERRMSG("pglogical_receiver failed to create spill directory \"%s\": %m",
path)));
}
/* cleanup old files in case of previous crash */
while ((spill_de = ReadDir(spill_dir, path)) != NULL)
{
if (strncmp(spill_de->d_name, "txn", 3) == 0)
{
sprintf(path, "pg_mtm/%d/%s", node_id, spill_de->d_name);
if (unlink(path) != 0)
ereport(PANIC,
(errcode_for_file_access(),
MTM_ERRMSG("pglogical_receiver could not remove spill file \"%s\": %m",
path)));
}
}
FreeDir(spill_dir);
}
int
MtmCreateSpillFile(int node_id, int *file_id)
{
static int spill_file_id;
char path[MAXPGPATH];
int fd;
sprintf(path, "pg_mtm/%d/txn-%d.snap",
node_id, ++spill_file_id);
fd = BasicOpenFile(path,
O_CREAT | O_TRUNC | O_WRONLY | O_APPEND | PG_BINARY);
if (fd < 0)
{
ereport(PANIC,
(errcode_for_file_access(),
MTM_ERRMSG("pglogical_receiver could not create spill file \"%s\": %m",
path)));
}
*file_id = spill_file_id;
return fd;
}
int
MtmOpenSpillFile(int node_id, int file_id)
{
static char path[MAXPGPATH];
int fd;
sprintf(path, "pg_mtm/%d/txn-%d.snap",
node_id, file_id);
fd = OpenTransientFile(path,
O_RDONLY | PG_BINARY);
if (fd < 0)
{
ereport(PANIC,
(errcode_for_file_access(),
MTM_ERRMSG("pglogical_apply could not open spill file \"%s\": %m",
path)));
}
if (unlink(path) < 0)
{ /* Should remove file on close */
ereport(LOG,
(errcode_for_file_access(),
MTM_ERRMSG("pglogical_apply failed to unlink spill file: %m")));
}
return fd;
}
void
MtmReadSpillFile(int fd, char *data, size_t size)
{
Assert(fd >= 0);
while (size != 0)
{
int rc = read(fd, data, size);
if (rc <= 0)
{
CloseTransientFile(fd);
ereport(ERROR,
(errcode_for_file_access(),
MTM_ERRMSG("pglogical_apply failed to read spill file: %m")));
}
data += rc;
size -= rc;
}
}
void
MtmCloseSpillFile(int fd)
{
if (close(fd) < 0)
ereport(ERROR,
(errcode_for_file_access(),
MTM_ERRMSG("pglogical_recevier failed to close spill file: %m")));
}