forked from cockroachdb/replicator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
218 lines (179 loc) · 6.24 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
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"net"
"net/http"
"os/signal"
"syscall"
"time"
"github.com/jackc/pgx/v4/pgxpool"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
)
var connectionString = flag.String(
"conn",
"postgresql://root@localhost:26257/defaultdb?sslmode=disable",
"cockroach connection string",
)
var port = flag.Int("port", 26258, "http server listening port")
var sinkDB = flag.String("sink_db", "_CDC_SINK", "db for storing temp sink tables")
var dropDB = flag.Bool("drop", false, "Drop the sink db before starting?")
var sinkDBZone = flag.Bool(
"sink_db_zone_override",
true,
"allow sink_db zone config to be overridden with the cdc-sink default values",
)
var configuration = flag.String(
"config",
"",
`This flag must be set. It requires a single line for each table passed in.
The format is the following:
[
{"endpoint":"", "source_table":"", "destination_database":"", "destination_table":""},
{"endpoint":"", "source_table":"", "destination_database":"", "destination_table":""},
]
Each table being updated requires a single line. Note that source database is
not required.
Each changefeed requires the same endpoint and you can have more than one table
in a single changefeed.
Here are two examples:
1) Single table changefeed. Source table and destination table are both called
users:
[{endpoint:"cdc.sql", source_table:"users", destination_database:"defaultdb", destination_table:"users"}]
The changefeed is initialized on the source database:
CREATE CHANGEFEED FOR TABLE users INTO 'experimental-[cdc-sink-url:port]/cdc.sql' WITH updated,resolved
2) Two table changefeed. Two tables this time, users and customers:
[
{"endpoint":"cdc.sql", "source_table":"users", "destination_database":"defaultdb", "destination_table":"users"},
{"endpoint":"cdc.sql", "source_table":"customers", "destination_database":"defaultdb", "destination_table":"customers"},
]
The changefeed is initialized on the source database:
CREATE CHANGEFEED FOR TABLE users,customers INTO 'experimental-[cdc-sink-url:port]/cdc.sql' WITH updated,resolved
As of right now, only a single endpoint is supported.
Don't forget to escape the json quotes:
./cdc-sink --config="[{\"endpoint\":\"test.sql\", \"source_table\":\"in_test1\", \"destination_database\":\"defaultdb\", \"destination_table\":\"out_test1\"},{\"endpoint\":\"test.sql\", \"source_table\":\"in_test2\", \"destination_database\":\"defaultdb\", \"destination_table\":\"out_test2\"}]"`,
)
func createHandler(db *pgxpool.Pool, sinks *Sinks) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
// Is it an ndjson url?
ndjson, ndjsonErr := parseNdjsonURL(r.RequestURI)
if ndjsonErr == nil {
sink := sinks.FindSink(ndjson.endpoint, ndjson.topic)
if sink != nil {
sink.HandleRequest(db, w, r)
return
}
// No sink found, throw an error.
http.Error(
w,
fmt.Sprintf("could not find a sync for %s", ndjson.topic),
http.StatusInternalServerError,
)
return
}
// Is it a resolved url?
resolved, resolvedErr := parseResolvedURL(r.RequestURI)
if resolvedErr == nil {
sinks.HandleResolvedRequest(r.Context(), db, resolved, w, r)
return
}
// Could not recognize url.
http.Error(
w,
fmt.Sprintf("URL pattern does not match either an ndjson (%s) or a resolved (%s)",
ndjsonErr, resolvedErr,
),
http.StatusInternalServerError,
)
return
}
}
// Config parses the passed in config.
type Config []ConfigEntry
// ConfigEntry is a single table configuration entry in a config.
type ConfigEntry struct {
Endpoint string `json:"endpoint"`
SourceTable string `json:"source_table"`
DestinationDatabase string `json:"destination_database"`
DestinationTable string `json:"destination_table"`
}
func parseConfig(rawConfig string) (Config, error) {
var config Config
if err := json.Unmarshal([]byte(rawConfig), &config); err != nil {
return Config{}, fmt.Errorf("Could not parse config: %s", err.Error())
}
if len(config) == 0 {
return Config{}, fmt.Errorf("No config lines provided")
}
for _, entry := range config {
if len(entry.Endpoint) == 0 {
return Config{}, fmt.Errorf("Each config entry requires and endpoint")
}
if len(entry.SourceTable) == 0 {
return Config{}, fmt.Errorf("Each config entry requires a source_table")
}
if len(entry.DestinationDatabase) == 0 {
return Config{}, fmt.Errorf("Each config entry requires a destination_database")
}
if len(entry.DestinationTable) == 0 {
return Config{}, fmt.Errorf("Each config entry requires a destination_table")
}
}
return config, nil
}
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
// First, parse the config.
flag.Parse()
config, err := parseConfig(*configuration)
if err != nil {
log.Print(*configuration)
log.Fatal(err)
}
db, err := pgxpool.Connect(ctx, *connectionString)
if err != nil {
log.Fatalf("could not parse config string: %v", err)
}
defer db.Close()
if *dropDB {
if err := DropSinkDB(ctx, db); err != nil {
log.Fatalf("Could not drop the sinkDB:%s - %v", *sinkDB, err)
}
}
if err := CreateSinkDB(ctx, db); err != nil {
log.Fatalf("Could not create the sinkDB:%s - %v", *sinkDB, err)
}
sinks, err := CreateSinks(ctx, db, config)
if err != nil {
log.Fatal(err)
}
l, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
if err != nil {
log.Fatalf("could not open listener: %v", err)
}
log.Printf("listening on %s", l.Addr())
handler := http.Handler(http.HandlerFunc(createHandler(db, sinks)))
handler = h2c.NewHandler(handler, &http2.Server{})
// TODO(bob): Consider configuring timeouts
svr := &http.Server{Handler: handler}
go svr.Serve(l)
<-ctx.Done()
log.Printf("waiting for connections to drain")
cancel()
ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
_ = svr.Shutdown(ctx)
cancel()
}