-
Notifications
You must be signed in to change notification settings - Fork 34
/
concurrent.go
407 lines (334 loc) · 10.4 KB
/
concurrent.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package gonymizer
import (
"bufio"
"errors"
"fmt"
log "github.com/sirupsen/logrus"
"io"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"unicode"
)
// maxLinesPerChunk bounds the number of lines per chunk.
const maxLinesPerChunk = 100000
// Chunk is a section of Postgres' data dump together with metadata.
// SubChunkNumber: 0 if chunk does not contain a COPY line or isn't a continuation of a COPY section from another chunk
// before it; >= 1 otherwise.
// DataBegins: the line number starting from which actual table data is defined.
type Chunk struct {
Data *strings.Builder
SchemaName string
TableName string
ColumnNames []string
ChunkNumber int
SubChunkNumber int
NumLines int
DataBegins int
Inclusive bool
}
// Filename returns a filename for a chunk.
func (c Chunk) Filename() string {
return fmt.Sprintf("%06d.%06d.part", c.ChunkNumber, c.SubChunkNumber)
}
// ProcessConfig contains the arguments required for processing a dump.
type ProcessConfig struct {
DBMapper *DBMapper
DestinationFilename string
GenerateSeed bool
Inclusive bool
NumWorkers int
PostprocessFilename string
PreprocessFilename string
SourceFilename string
}
// ColumnMapperContainer can return a reference to a ColumnMapper
type ColumnMapperContainer interface {
ColumnMapper(schemaName, tableName, columnName string) *ColumnMapper
}
// StringWriter can write strings to a buffer/file/etc.
type StringWriter interface {
WriteString(input string) (int, error)
}
// StringReader can write strings to a buffer/file/etc.
type StringReader interface {
ReadString(delim byte) (string, error)
}
// ProcessConcurrently will process the supplied dump file concurrently according to the supplied database map file.
// generateSeed can also be set to true which will inform the function to use Go's built-in random number generator.
func ProcessConcurrently(config ProcessConfig) error {
err := seedRNG(config.DBMapper, config.GenerateSeed)
if err != nil {
return err
}
srcFile, err := os.Open(config.SourceFilename)
if err != nil {
log.Error(err)
return err
}
defer srcFile.Close()
reader := bufio.NewReader(srcFile)
var wg sync.WaitGroup
chunks := make(chan Chunk, config.NumWorkers*2)
// Start 1 worker to pull out chunks from the file and put it on a channel of size 2*N
go createChunks(chunks, reader, &wg, config.Inclusive, maxLinesPerChunk)
// Start N workers to read from the channel and process the chunks concurrently, writing results to file
startChunkWorkers(config.DBMapper, &wg, config.NumWorkers, chunks)
// Merge all partial results from file to final dst file, deleting the partial results
wg.Wait()
err = mergeFiles(config)
if err != nil {
log.Fatal(err)
}
return nil
}
// startChunkWorkers starts a number of workers to process chunks when they arrive in a given channel
func startChunkWorkers(mapper ColumnMapperContainer, wg *sync.WaitGroup, numWorkers int, chunks <-chan Chunk) {
wg.Add(numWorkers)
for i := 0; i < numWorkers; i++ {
go startChunkWorker(chunks, wg, mapper)
log.Infof("Worker %d started!", i+1)
}
}
// createChunks takes a reader and splits it up into maxLinesPerChunk sized pieces. These pieces are sent
// through a channel.
func createChunks(chunks chan<- Chunk, reader *bufio.Reader, wg *sync.WaitGroup, inclusive bool, maxLinesPerChunk int) {
defer close(chunks)
defer wg.Done()
wg.Add(1)
var (
schemaName string
tableName string
columnNames []string
chunkCount int
subchunkCount int
eof bool
hasSubchunk bool
)
for !eof {
var (
builder strings.Builder
lineIndex int
)
chunk := Chunk{
Data: &builder,
ChunkNumber: chunkCount,
SubChunkNumber: subchunkCount,
SchemaName: schemaName,
TableName: tableName,
ColumnNames: columnNames,
Inclusive: inclusive,
}
for lineIndex = 0; lineIndex < maxLinesPerChunk; lineIndex++ {
input, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
eof = true
} else {
log.Fatal(err)
}
break
}
builder.WriteString(input)
trimmedInput := strings.TrimLeftFunc(input, unicode.IsSpace)
if strings.HasPrefix(trimmedInput, StateChangeTokenBeginCopy) {
pattern := `^COPY (?P<Schema>[a-zA-Z_]+)\."?(?P<TableName>\w+)"? \((?P<Columns>.*)\) .*`
r := regexp.MustCompile(pattern)
submatch := r.FindStringSubmatch(trimmedInput)
if len(submatch) == 0 {
log.Fatal("Regex doesn't match: ", trimmedInput)
}
schemaName = submatch[1]
tableName = submatch[2]
columnNames = strings.Split(submatch[3], ", ")
subchunkCount++
chunk.SchemaName = schemaName
chunk.TableName = tableName
chunk.ColumnNames = columnNames
chunk.DataBegins = lineIndex + 1
chunk.SubChunkNumber = subchunkCount
hasSubchunk = true
} else if strings.HasPrefix(trimmedInput, StateChangeTokenEndCopy) {
subchunkCount = 0
schemaName = ""
tableName = ""
columnNames = nil
hasSubchunk = false
break
}
if lineIndex == maxLinesPerChunk-1 && hasSubchunk {
subchunkCount++
}
}
chunk.NumLines = lineIndex
chunks <- chunk
if subchunkCount == 0 {
chunkCount++
}
}
log.Infof("Processed %d chunks", chunkCount-1)
}
// mergeFiles takes a destination filename and pre/post-process files and writes all part files to the destination file
// including any pre/post-processing file data.
func mergeFiles(config ProcessConfig) error {
log.Info("Merging partial files...")
dstFile, err := os.Create(config.DestinationFilename)
if err != nil {
log.Error(err)
return err
}
defer dstFile.Close()
if len(config.PreprocessFilename) > 0 {
if err = fileInjector(config.PreprocessFilename, dstFile); err != nil {
log.Error("Unable to run preProcessor")
return err
}
}
if _, err := dstFile.WriteString("SET session_replication_role = 'replica';\n"); err != nil {
return err
}
pwd, err := os.Getwd()
if err != nil {
return err
}
matches, err := filepath.Glob("*.part")
if err != nil {
return err
}
for _, filename := range matches {
path := filepath.Join(pwd, filename)
partFile, err := os.Open(path)
if err != nil {
log.Error(err)
return err
}
_, err = io.Copy(dstFile, partFile)
if err != nil {
log.Error(err)
return err
}
partFile.Close()
os.Remove(filename)
}
if len(config.PostprocessFilename) > 0 {
if err = fileInjector(config.PostprocessFilename, dstFile); err != nil {
return err
}
}
if _, err := dstFile.WriteString("SET session_replication_role = 'origin';\n"); err != nil {
return err
}
return nil
}
// startChunkWorkers takes a receive-only channel of chunks and processes each chunk, writing them to file.
func startChunkWorker(chunks <-chan Chunk, wg *sync.WaitGroup, mapper ColumnMapperContainer) {
defer wg.Done()
for chunk := range chunks {
dst := chunk.Filename()
dstFile, err := os.Create(dst)
if err != nil {
log.Fatal(err)
}
processChunk(chunk, dstFile, mapper)
err = dstFile.Close()
if err != nil {
log.Fatal(err)
}
log.Infof("%s written to file", dst)
}
}
// processChunk reads data from a Chunk and writes to a destination file handler.
func processChunk(chunk Chunk, dstFile StringWriter, mapper ColumnMapperContainer) {
reader := bufio.NewReader(strings.NewReader(chunk.Data.String()))
cmaps, err := getColumnMappers(mapper, chunk)
if err != nil {
log.Fatalf("%s: Please add to Map file", err.Error())
}
processFromReader(chunk, dstFile, reader, cmaps)
}
// processFromReader reads data from a StringReader line by line, processes it and sends it to a StringWriter
func processFromReader(chunk Chunk, writer StringWriter, reader StringReader, cmaps []*ColumnMapper) {
for i := 0; i > -1; i++ {
input, err := reader.ReadString('\n')
if err != nil {
if err != io.EOF {
log.Fatal(err)
}
break
}
trimmedInput := strings.TrimLeftFunc(input, unicode.IsSpace)
isEnd := strings.HasPrefix(trimmedInput, StateChangeTokenEndCopy)
isEmpty := len(trimmedInput) == 0
aboveData := i < chunk.DataBegins
hasNoData := chunk.ColumnNames == nil
if aboveData || isEnd || isEmpty || hasNoData {
_, err = writer.WriteString(input)
if err != nil {
log.Fatal(err)
}
continue
}
output := processRowFromChunk(cmaps, input, chunk)
_, err = writer.WriteString(output)
if err != nil {
log.Fatal(err)
}
}
}
// getColumnMappers returns a slice of references to ColumnMappers corresponding to the columns of the given Chunk.
// Returns nil if given Chunk does not contain any schemas.
func getColumnMappers(mapper ColumnMapperContainer, chunk Chunk) ([]*ColumnMapper, error) {
if len(chunk.ColumnNames) <= 0 {
return nil, nil
}
result := make([]*ColumnMapper, 0, len(chunk.ColumnNames))
for _, columnName := range chunk.ColumnNames {
cmap := mapper.ColumnMapper(chunk.SchemaName, chunk.TableName, columnName)
if cmap == nil && chunk.Inclusive {
errorString := fmt.Sprintf("column '%s.%s.%s' does not exist", chunk.SchemaName, chunk.TableName, columnName)
return nil, errors.New(errorString)
}
result = append(result, cmap)
}
return result, nil
}
// processRowFromChunk processes a data row from a given Chunk
func processRowFromChunk(cmaps []*ColumnMapper, inputLine string, chunk Chunk) string {
rowValues := strings.Split(inputLine, "\t")
outputValues := make([]string, 0, len(rowValues))
for i, cmap := range cmaps {
output := processRawValue(rowValues[i], chunk.ColumnNames[i], cmap)
// Append the column to our new line
outputValues = append(outputValues, output)
}
return strings.Join(outputValues, "\t")
}
// processRawValue takes a rawValue of a row.column from the dump and anonymizes it
func processRawValue(rawValue, columnName string, cmap *ColumnMapper) string {
var (
err error
escapeChar string
output string
)
// Check to see if the column has an escape char at the end of it.
// If so cut it and keep it for later
if strings.HasSuffix(rawValue, "\n") {
escapeChar = "\n"
rawValue = strings.Replace(rawValue, "\n", "", -1)
}
// If column value is nil or if this column is not mapped, keep the value and continue on
if rawValue == "\\N" || cmap == nil {
output = rawValue
} else {
output, err = processValue(cmap, rawValue)
if err != nil {
log.Debug("columnName: ", columnName)
log.Fatal(err)
}
}
// Add escape character back to column
output += escapeChar
return output
}