-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
264 lines (213 loc) · 6.34 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"os"
"strings"
"sync"
"time"
"github.com/ClickHouse/clickhouse-go/v2"
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
log "github.com/sirupsen/logrus"
)
var ctx = context.Background()
func main() {
only := flag.String("only", "", "Only replicate one table by name")
configPath := flag.String("config", "config.yml", "Path to the configuration file")
drop := flag.String("drop", "", "Drop a table by name")
flag.Parse()
var config Config
if err := config.Parse(*configPath); err != nil {
log.Fatal("Failed to parse config", err)
}
dsn, err := clickhouse.ParseDSN(os.Getenv("CLICKHOUSE_DSN"))
if err != nil {
log.WithError(err).Fatal("Failed to parse ClickHouse DSN")
}
conn, err := clickhouse.Open(dsn)
if err != nil {
log.WithError(err).Fatal("Failed to connect to ClickHouse")
}
defer conn.Close()
db, err := pgxpool.New(ctx, os.Getenv("DATABASE_URL"))
if err != nil {
log.WithError(err).Fatal("Failed to connect to Postgres")
}
for idx, table := range config.Tables {
log.WithFields(log.Fields{
"source": table.Source,
"destination": table.Destination,
}).Info("Replicating table")
if *only != "" && *only != table.Source {
log.Warn("Skipping this table")
continue
}
start := time.Now()
if table.Cursor.Column != "" {
if table.Cursor.LastSync.IsZero() || *drop == table.Source {
log.Warn("No last sync date found, resetting cursor")
table.Cursor.LastSync = time.Time{}
}
log.WithFields(log.Fields{
"column": table.Cursor.Column,
"lastSync": table.Cursor.LastSync,
}).Info("Resuming from cursor")
}
if *drop != "" && *drop == table.Source {
log.WithField("table", table.Source).Info("Dropping table")
if _, err := db.Exec(ctx, fmt.Sprintf("DROP TABLE IF EXISTS %s", table.Destination)); err != nil {
log.WithError(err).Errorln("Failed to drop table")
}
}
if err := SynchronizeTable(config, table, conn, db); err != nil {
log.WithError(err).Errorln("Failed to synchronize table")
continue
}
if table.Cursor.Column != "" {
now := time.Now()
config.Tables[idx].Cursor.LastSync = now
log.WithFields(log.Fields{
"column": table.Cursor.Column,
"lastSync": config.Tables[idx].Cursor.LastSync,
}).Info("Updated cursor")
}
log.WithFields(log.Fields{
"source": table.Source,
"duration": time.Since(start),
}).Info("Table synchronized")
}
if err := config.Save(*configPath); err != nil {
log.WithError(err).Fatal("Failed to save config")
}
log.Info("Replication completed")
}
// SynchronizeTable synchronizes a table from ClickHouse to Postgres
func SynchronizeTable(config Config, table Table, conn driver.Conn, db *pgxpool.Pool) error {
if err := CreatePostgresTable(table, db); err != nil {
return err
}
columns := table.GetDestinationColumns()
batches := make(chan [][]interface{})
go func() {
defer close(batches)
total, err := Batching(table, conn, config.BatchSize, func(batch [][]interface{}) error {
batches <- batch
return nil
})
if err != nil {
log.WithError(err).Errorln("Failed to batch")
}
log.WithField("total", total).Infoln("Selecting data completed")
}()
wg := sync.WaitGroup{}
for batch := range batches {
wg.Add(1)
go func(batch [][]interface{}) {
defer wg.Done()
log.WithField("batch", len(batch)).Info("Inserting batch")
conn, err := db.Acquire(ctx)
if err != nil {
log.WithError(err).Errorln("Failed to acquire connection")
return
}
defer conn.Release()
tableName, err := MakeTemporaryTable(table, conn)
if err != nil {
log.WithError(err).Errorln("Failed to make temporary table")
return
}
_, err = conn.CopyFrom(
ctx,
pgx.Identifier{tableName},
columns,
pgx.CopyFromRows(batch),
)
if err != nil {
log.WithError(err).Errorln("Failed to insert batch")
}
if err := MoveTemporaryTable(table, conn, tableName); err != nil {
log.WithError(err).Errorln("Failed to move temporary table")
}
}(batch)
}
wg.Wait()
log.Infoln("Data inserted")
return nil
}
// MoveTemporaryTable moves the temporary table to the main table
func MoveTemporaryTable(table Table, conn *pgxpool.Conn, tableName string) error {
updateQuery := []string{}
for _, column := range table.GetDestinationColumns() {
updateQuery = append(updateQuery, fmt.Sprintf("%s = EXCLUDED.%s", column, column))
}
log.WithField("source", tableName).Info("Moving temporary table")
_, err := conn.Exec(ctx, fmt.Sprintf(`
INSERT INTO %s
SELECT DISTINCT ON (%s) * FROM %s
ON CONFLICT (%s) DO UPDATE SET
%s;
`, table.Destination,
strings.Join(table.GetPrimaryKey(), ", "),
tableName,
strings.Join(table.GetPrimaryKey(), ", "),
strings.Join(updateQuery, ", "),
))
if err != nil {
log.WithError(err).Errorln("Failed to move temporary table")
}
log.WithField("table", tableName).Infoln("Moved temporary table")
return nil
}
// CreatePostgresTable creates a table in Postgres
func CreatePostgresTable(table Table, db *pgxpool.Pool) error {
columns := []string{}
for _, column := range table.Columns {
columns = append(columns, fmt.Sprintf("%s %s", column.Destination, column.Type))
}
_, err := db.Exec(ctx, fmt.Sprintf(
`CREATE TABLE IF NOT EXISTS %s (%s)`,
table.Destination,
strings.Join(columns, ", "),
))
if err != nil {
return err
}
if len(table.GetPrimaryKey()) > 0 {
_, err = db.Exec(ctx, fmt.Sprintf(
`ALTER TABLE %s ADD PRIMARY KEY (%s)`,
table.Destination,
strings.Join(table.GetPrimaryKey(), ", "),
))
if err != nil {
log.WithError(err).Warn("Failed to add primary key")
}
}
for _, index := range table.Indexes {
_, err = db.Exec(ctx, fmt.Sprintf(
`CREATE INDEX IF NOT EXISTS %s_%s ON %s (%s)`,
table.Destination,
index.Name,
table.Destination,
strings.Join(index.Columns, ", "),
))
if err != nil {
log.WithError(err).Warn("Failed to create index")
}
}
return nil
}
// MakeTemporaryTable creates a temporary table
func MakeTemporaryTable(table Table, conn *pgxpool.Conn) (string, error) {
rnd := uuid.New().String()[:8]
tableName := fmt.Sprintf("%s_%s_tmp", table.Destination, rnd)
_, err := conn.Exec(ctx, fmt.Sprintf(
`CREATE TEMPORARY TABLE %s (LIKE %s INCLUDING DEFAULTS)`,
tableName,
table.Destination,
))
return tableName, err
}