/
pagefile.go
254 lines (215 loc) · 6.86 KB
/
pagefile.go
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//
// This file provides low level routines for handling incremental backup
// Incremental file format is:
// 4 bytes header with designation information, format version and magic number
// 8 bytes uint file size
// 4 bytes uint changed pages count N
// (N * 4) bytes for Block Numbers of changed pages
// (N * DatabasePageSize) bytes for changed page data
//
package postgres
import (
"encoding/binary"
"fmt"
"io"
"os"
"path"
"regexp"
"strings"
"github.com/RoaringBitmap/roaring"
"github.com/apecloud/dataprotection-wal-g/internal/ioextensions"
"github.com/apecloud/dataprotection-wal-g/internal/limiters"
"github.com/apecloud/dataprotection-wal-g/internal/walparser"
"github.com/apecloud/dataprotection-wal-g/internal/walparser/parsingutil"
"github.com/apecloud/dataprotection-wal-g/utility"
"github.com/pkg/errors"
"github.com/wal-g/tracelog"
)
const (
DatabasePageSize = int64(walparser.BlockSize)
sizeofInt32 = 4
sizeofInt64 = 8
SignatureMagicNumber byte = 0x55
invalidLsn LSN = 0
validFlags = 7
layoutVersion = 4
headerSize = 24
DefaultTablespace = "base"
GlobalTablespace = "global"
NonDefaultTablespace = "pg_tblspc"
)
// InvalidBlockError indicates that file contain invalid page and cannot be archived incrementally
type InvalidBlockError struct {
error
}
func newInvalidBlockError(blockNo uint32) InvalidBlockError {
return InvalidBlockError{errors.Errorf("block %d is invalid", blockNo)}
}
func (err InvalidBlockError) Error() string {
return fmt.Sprintf(tracelog.GetErrorFormatter(), err.error)
}
type InvalidIncrementFileHeaderError struct {
error
}
func newInvalidIncrementFileHeaderError() InvalidIncrementFileHeaderError {
return InvalidIncrementFileHeaderError{errors.New("Invalid increment file header")}
}
func (err InvalidIncrementFileHeaderError) Error() string {
return fmt.Sprintf(tracelog.GetErrorFormatter(), err.error)
}
type UnknownIncrementFileHeaderError struct {
error
}
func newUnknownIncrementFileHeaderError() UnknownIncrementFileHeaderError {
return UnknownIncrementFileHeaderError{errors.New("Unknown increment file header")}
}
func (err UnknownIncrementFileHeaderError) Error() string {
return fmt.Sprintf(tracelog.GetErrorFormatter(), err.error)
}
type UnexpectedTarDataError struct {
error
}
func newUnexpectedTarDataError() UnexpectedTarDataError {
return UnexpectedTarDataError{errors.New("Expected end of Tar")}
}
func (err UnexpectedTarDataError) Error() string {
return fmt.Sprintf(tracelog.GetErrorFormatter(), err.error)
}
var pagedFilenameRegexp *regexp.Regexp
func init() {
pagedFilenameRegexp = regexp.MustCompile(`^(\d+)([.]\d+)?$`)
}
// TODO : unit tests
// isPagedFile checks basic expectations for paged file
func isPagedFile(info os.FileInfo, filePath string) bool {
// For details on which file is paged see
//nolint:lll // https://www.postgresql.org/message-id/flat/F0627DEB-7D0D-429B-97A9-D321450365B4%40yandex-team.ru#F0627DEB-7D0D-429B-97A9-D321450365B4@yandex-team.ru
if info.IsDir() ||
((!strings.Contains(filePath, DefaultTablespace)) && (!strings.Contains(filePath, NonDefaultTablespace))) ||
info.Size() == 0 ||
info.Size()%DatabasePageSize != 0 ||
!pagedFilenameRegexp.MatchString(path.Base(filePath)) {
return false
}
return true
}
func ReadIncrementalFile(filePath string,
fileSize int64,
lsn LSN,
deltaBitmap *roaring.Bitmap) (fileReader io.ReadCloser, size int64, err error) {
file, err := os.Open(filePath)
if err != nil {
return nil, 0, err
}
fileReadSeekCloser := &ioextensions.ReadSeekCloserImpl{
Reader: limiters.NewDiskLimitReader(file),
Seeker: file,
Closer: file,
}
pageReader := &IncrementalPageReader{fileReadSeekCloser, fileSize, lsn, nil, nil}
incrementSize, err := pageReader.initialize(deltaBitmap)
if err != nil {
utility.LoggedClose(file, "")
return nil, 0, err
}
return pageReader, incrementSize, nil
}
func ReadIncrementLocations(filePath string, fileSize int64, lsn LSN) ([]walparser.BlockLocation, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
fileReadSeekCloser := &ioextensions.ReadSeekCloserImpl{
Reader: limiters.NewDiskLimitReader(file),
Seeker: file,
Closer: file,
}
pageReader := &IncrementalPageReader{fileReadSeekCloser, fileSize, lsn, nil, nil}
err = pageReader.FullScanInitialize()
if err != nil {
return nil, err
}
return convertBlocksToLocations(filePath, pageReader.Blocks)
}
func convertBlocksToLocations(filePath string, blocks []uint32) ([]walparser.BlockLocation, error) {
relFileNode, err := GetRelFileNodeFrom(filePath)
if err != nil {
return nil, err
}
locations := make([]walparser.BlockLocation, 0, len(blocks))
for _, blockNo := range blocks {
locations = append(locations, *walparser.NewBlockLocation(relFileNode.SpcNode,
relFileNode.DBNode, relFileNode.RelNode, blockNo))
}
return locations, nil
}
// ApplyFileIncrement changes pages according to supplied change map file
func ApplyFileIncrement(fileName string, increment io.Reader, createNewIncrementalFiles bool, fsync bool) error {
tracelog.DebugLogger.Printf("Incrementing %s\n", fileName)
err := ReadIncrementFileHeader(increment)
if err != nil {
return err
}
var fileSize uint64
var diffBlockCount uint32
err = parsingutil.ParseMultipleFieldsFromReader([]parsingutil.FieldToParse{
{Field: &fileSize, Name: "fileSize"},
{Field: &diffBlockCount, Name: "diffBlockCount"},
}, increment)
if err != nil {
return err
}
diffMap := make([]byte, diffBlockCount*sizeofInt32)
_, err = io.ReadFull(increment, diffMap)
if err != nil {
return err
}
openFlags := os.O_RDWR
if createNewIncrementalFiles {
openFlags = openFlags | os.O_CREATE
}
file, err := os.OpenFile(fileName, openFlags, 0666)
if err != nil {
if os.IsNotExist(err) {
return errors.Wrap(err, "incremented file should always exist")
}
return errors.Wrap(err, "can't open file to increment")
}
defer utility.LoggedClose(file, "")
defer utility.LoggedSync(file, "", fsync)
err = file.Truncate(int64(fileSize))
if err != nil {
return err
}
page := make([]byte, DatabasePageSize)
for i := uint32(0); i < diffBlockCount; i++ {
blockNo := binary.LittleEndian.Uint32(diffMap[i*sizeofInt32 : (i+1)*sizeofInt32])
_, err = io.ReadFull(increment, page)
if err != nil {
return err
}
_, err = file.WriteAt(page, int64(blockNo)*DatabasePageSize)
if err != nil {
return err
}
}
all, _ := increment.Read(make([]byte, 1))
if all > 0 {
return newUnexpectedTarDataError()
}
return nil
}
func ReadIncrementFileHeader(reader io.Reader) error {
header := make([]byte, sizeofInt32)
_, err := io.ReadFull(reader, header)
if err != nil {
return err
}
if header[0] != 'w' || header[1] != 'i' || header[3] != SignatureMagicNumber {
return newInvalidIncrementFileHeaderError()
}
if header[2] != '1' {
return newUnknownIncrementFileHeaderError()
}
return nil
}