This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
genericsync.go
203 lines (163 loc) · 5.5 KB
/
genericsync.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
package tableengines
import (
"fmt"
"io"
"strings"
"time"
"github.com/jackc/pgx"
"github.com/mkabilov/pg2ch/pkg/utils/chutils/bulkupload"
"github.com/mkabilov/pg2ch/pkg/utils/dbtypes"
"github.com/mkabilov/pg2ch/pkg/utils/pgutils"
)
const (
syncProgressBatch = 1000000
)
func (t *genericTable) genSyncWrite(p []byte, extras ...[]byte) error {
if err := bulkupload.DecodeCopyToTuples(t.syncBuf, t.bulkUploader, t.syncPgColumns, t.syncColProps, p); err != nil {
return fmt.Errorf("could not parse copy string: %v", err)
}
if err := t.bulkUploader.WriteByte(columnDelimiter); err != nil {
return err
}
if _, err := t.bulkUploader.Write(t.syncSnapshotLSN.Decimal()); err != nil {
return err
}
for _, extra := range extras {
if err := t.bulkUploader.WriteByte(columnDelimiter); err != nil {
return err
}
if _, err := t.bulkUploader.Write(extra); err != nil {
return err
}
}
if err := t.bulkUploader.WriteByte('\n'); err != nil {
return err
}
t.syncedRows++
return nil
}
func (t *genericTable) InitSync() error {
t.Lock()
defer t.Unlock()
if !t.cfg.InitSyncSkipTruncate {
if err := t.truncateTable(t.cfg.ChMainTable); err != nil {
return fmt.Errorf("could not truncate main table: %v", err)
}
}
if !t.cfg.ChSyncAuxTable.IsEmpty() {
if err := t.truncateTable(t.cfg.ChSyncAuxTable); err != nil {
return fmt.Errorf("could not truncate aux table: %v", err)
}
}
t.inSync = true
return nil
}
// Sync the table. Saves PostgreSQL snapshot LSN and loads the buffered data
// after this snapshot to the table.
func (t *genericTable) genSync(chUploader bulkupload.BulkUploader, pgTx *pgx.Tx, snapshotLSN dbtypes.LSN, tblWriter io.Writer) error {
t.syncSnapshotLSN = snapshotLSN
t.bulkUploader = chUploader
t.syncedRows = 0
t.syncLastBatchTime = time.Now()
startTime := time.Now()
if tblLiveTuples, err := pgutils.PgStatLiveTuples(pgTx, t.cfg.PgTableName); err != nil {
t.logger.Warnf("genSync: could not get approximate number of rows: %v", err)
} else {
t.liveTuples = tblLiveTuples
}
t.logger.Infow("genSync: copy from postgresql to clickhouse table started",
"postgres", t.cfg.PgTableName.String(),
"clickhouse", t.cfg.ChMainTable,
"liveTuples", t.liveTuples,
"snapshotLSN", snapshotLSN.Dec())
loaderErrCh := make(chan error, 1)
go func(errCh chan error) {
errCh <- t.bulkUploader.BulkUpload(t.cfg.ChMainTable, t.chUsedColumns)
}(loaderErrCh)
ct, err := pgTx.CopyToWriter(
tblWriter,
fmt.Sprintf("copy (select %s from only %s) to stdout",
strings.Join(t.pgUsedColumns, ", "), t.cfg.PgTableName.String()))
if err != nil {
return fmt.Errorf("could not copy: %v", err)
}
if err := t.bulkUploader.Finish(); err != nil {
return fmt.Errorf("could not finalize bulkuploader: %v", err)
}
if pgRows := ct.RowsAffected(); pgRows != int64(t.syncedRows) {
return fmt.Errorf(
"number of rows inserted to clickhouse(%v) doesn't match the initial number of rows in pg(%v)",
t.syncedRows, pgRows)
}
t.logger.Infof("copied during the sync: %d rows in %v (%0.f rows/sec)",
t.syncedRows, time.Since(startTime), float64(t.syncedRows)/time.Since(startTime).Seconds())
if err := <-loaderErrCh; err != nil {
return fmt.Errorf("could not load to CH: %v", err)
}
close(loaderErrCh)
t.bulkUploader = nil
return t.loadSyncDeltas()
}
// load changes of the table which occurred during the sync and stored in the aux table
func (t *genericTable) loadSyncDeltas() error {
t.Lock()
defer t.Unlock()
defer func() {
t.inSync = false
t.syncBuf = nil
}()
if err := t.flushBuffer(); err != nil {
return fmt.Errorf("could not flush buffer: %v", err)
}
t.logger.Infof("delta size: %s", t.deltaSize(t.syncSnapshotLSN))
chSql := fmt.Sprintf("INSERT INTO %[1]s(%[2]s) SELECT %[2]s FROM %[3]s WHERE %[4]s > %[5]d and %[7]s = '%[8]s' ORDER BY %[6]s",
t.cfg.ChMainTable,
strings.Join(t.chUsedColumns, ","),
t.cfg.ChSyncAuxTable,
t.cfg.LsnColumnName, uint64(t.syncSnapshotLSN),
t.cfg.RowIDColumnName,
t.cfg.TableNameColumnName, t.auxTableNameColumnValue)
t.logger.Debugf("executing: %s", chSql)
if err := t.chLoader.Exec(chSql); err != nil {
return fmt.Errorf("could not merge with sync aux table: %v", err)
}
if !t.cfg.ChSyncAuxTable.IsEmpty() {
if err := t.dropTablePartition(t.cfg.ChSyncAuxTable, string(t.auxTableNameColumnValue)); err != nil {
return fmt.Errorf("could not drop partition of the %q table: %v", t.cfg.ChSyncAuxTable, err)
}
}
if err := t.saveLSN(t.syncSnapshotLSN); err != nil {
return fmt.Errorf("could not save lsn to file: %v", err)
}
return nil
}
func (t *genericTable) printSyncProgress() {
if t.syncedRows%syncProgressBatch != 0 {
return
}
var (
eta time.Duration
left uint64
)
speed := float64(syncProgressBatch) / time.Since(t.syncLastBatchTime).Seconds()
if t.liveTuples >= t.syncedRows {
left = t.liveTuples - t.syncedRows
}
if t.syncedRows < t.liveTuples {
eta = time.Second * time.Duration(left/uint64(speed))
}
t.logger.Infof("%d rows copied to %q (ETA: %v left: %v rows speed: %.0f rows/s)",
t.syncedRows, t.cfg.ChMainTable, eta.Truncate(time.Second), left, speed)
t.syncLastBatchTime = time.Now()
}
func (t *genericTable) syncAuxTableColumns() []string {
return append(t.chUsedColumns, t.cfg.RowIDColumnName, t.cfg.TableNameColumnName)
}
func (t *genericTable) deltaSize(lsn dbtypes.LSN) string {
res, err := t.chLoader.Query(fmt.Sprintf("SELECT count(*) FROM %s WHERE %s > %d",
t.cfg.ChSyncAuxTable, t.cfg.LsnColumnName, uint64(lsn)))
if err != nil {
t.logger.Fatalf("query error: %v", err)
}
return res[0][0]
}