-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
270 lines (219 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
265
266
267
268
269
270
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"math/rand"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/hashicorp/go-hclog"
"github.com/radekg/yugabyte-db-go-client/client"
"github.com/radekg/yugabyte-db-go-client/configs"
"github.com/radekg/yugabyte-db-go-client/errors"
"github.com/radekg/yugabyte-db-go-client/utils/ybdbid"
ybApi "github.com/radekg/yugabyte-db-go-proto/v2/yb/api"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func main() {
cfg := &cdcConfig{}
flag.StringVar(&cfg.database, "database", "", "database to use")
flag.BoolVar(&cfg.logAsJSON, "log-as-json", false, "log as JSON")
flag.StringVar(&cfg.logLevel, "log-level", defaultLogLevel, "log level")
flag.StringVar(&cfg.logLevelClient, "log-level-client", defaultLogLevel, "YugabyteDB client log level")
flag.StringVar(&cfg.masters, "masters", "127.0.0.1:7100,127.0.0.1:7101,127.0.0.1:7102", "comma-delimited list of master addresses")
flag.StringVar(&cfg.stream, "stream-id", "", "stream ID")
flag.StringVar(&cfg.table, "table", "", "table to use")
flag.Parse()
os.Exit(process(cfg))
}
func process(cfg *cdcConfig) int {
logger := hclog.New(&hclog.LoggerOptions{
Name: "cdctest",
Level: hclog.LevelFromString(cfg.logLevel),
JSONFormat: cfg.logAsJSON,
}).With("table", cfg.table)
if cfg.stream != "" {
logger = logger.With("stream-id", cfg.stream)
}
loggerClient := hclog.New(&hclog.LoggerOptions{
Name: "cdctest-client",
Level: hclog.LevelFromString(cfg.logLevelClient),
JSONFormat: cfg.logAsJSON,
}).With("table", cfg.table)
clientConfig := &configs.YBClientConfig{
MasterHostPort: strings.Split(cfg.masters, ","),
OpTimeout: time.Second * 10,
}
ybdbClient := client.NewYBClient(clientConfig)
if err := ybdbClient.Connect(); err != nil {
logger.Error("failed connecting to the cluster", "reason", err)
return 1
}
defer ybdbClient.Close()
logger.Info("connected to the cluster")
var table *ybApi.ListTablesResponsePB_TableInfo
response, err := listTables(ybdbClient, cfg.database)
if err != nil {
logger.Error("failed listing tables", "reason", err)
return 1
}
for _, tableInfo := range response.Tables {
if *tableInfo.Name == cfg.table {
table = tableInfo
break
}
}
if table == nil {
logger.Error("table not found")
return 1
}
if err := waitForTableCreateDone(ybdbClient, table.Id); err != nil {
logger.Error("failed while waiting for table create done status", "reason", err)
return 1
}
logger.Info("table found")
hostPorts, err := listHostPorts(ybdbClient)
if err != nil {
logger.Error("error while listing tablet servers", "reason", err)
return 1
}
if len(hostPorts) == 0 {
logger.Error("could not discover any hosts to run the CDC against")
return 1
}
logger.Info("found host ports", "host-ports", hostPorts)
tabletLocations, err := listTabletLocations(ybdbClient, table.Id)
if err != nil {
logger.Error("error while listing tablet locations", "reason", err)
return 1
}
if len(tabletLocations) == 0 {
logger.Error("no tablet locations to run the CDC against")
return 1
}
logger.Info("found tablet locations", "num-tablet-locations", len(tabletLocations))
cp := newClientProvider(hostPorts)
var streamIDBytes []byte
if cfg.stream == "" {
for {
c, err := cp.getClient(loggerClient)
if err != nil {
logger.Error("could not get connected client, going to retry", "reason", err)
<-time.After(time.Millisecond * 100)
continue
}
streamResponse, err := createCDCStream(c, table.Id)
if err != nil {
c.Close()
logger.Error("error creating new CDC stream, going to retry", "reason", err)
<-time.After(time.Millisecond * 100)
continue
}
c.Close()
streamIDBytes = streamResponse.StreamId
break
}
parsedStreamID, err := ybdbid.TryParseFromBytes(streamIDBytes)
if err != nil {
logger.Error("failed parsing new stream ID", "reason", err)
return 1
}
logger = logger.With("stream-id", parsedStreamID.String())
logger.Info("created a new CDC stream")
} else {
reverseParsedStreamID, err := ybdbid.TryParseFromString(cfg.stream)
if err != nil {
logger.Error("failed parsing given CDC stream ID to bytes", "reason", err)
return 1
}
streamIDBytes = reverseParsedStreamID.Bytes()
_, getStreamErr := getCDCStreamByID(ybdbClient, streamIDBytes)
if getStreamErr != nil {
logger.Error("failed fetching stream info for given stream ID",
"reason", getStreamErr)
return 1
}
}
// handle shutdown gracefully:
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
logger.Info("shutting down...")
cancelFunc()
}()
// start consuming:
wg := &sync.WaitGroup{}
for _, location := range tabletLocations {
wg.Add(1)
go func(tabletID []byte) {
consume(ctx, logger, loggerClient, cp, streamIDBytes, tabletID)
wg.Done()
}(location.TabletId)
}
wg.Wait()
logger.Info("all work done, bye...")
return 0
}
func consume(ctx context.Context,
logger hclog.Logger,
loggerClient hclog.Logger,
cp *clientProvider,
streamID, tabletID []byte) {
checkpoint := &ybApi.CDCCheckpointPB{
OpId: &ybApi.OpIdPB{
Term: pint64(0),
Index: pint64(0),
},
}
<-time.After(time.Millisecond * time.Duration(rand.Intn(100-10)+10))
for {
select {
case <-ctx.Done():
return
case <-time.After(time.Millisecond * 500):
// reiterate
}
c, err := cp.getClient(loggerClient)
if err != nil {
logger.Error("failed fetching a client", "reason", err)
continue
}
request := &ybApi.GetChangesRequestPB{
StreamId: streamID,
TabletId: tabletID,
FromCheckpoint: checkpoint,
}
response := &ybApi.GetChangesResponsePB{}
requestErr := c.Execute(request, response)
if requestErr != nil {
logger.Error("failed fetching changes", "reason", requestErr)
continue
}
if err := errors.NewCDCError(response.Error); err != nil {
logger.Error("failed fetching changes", "reason", err)
continue
}
if len(response.Records) == 0 {
continue
}
if compareOpId(checkpoint.OpId, response.Checkpoint.OpId) == 1 {
bs, err := json.MarshalIndent(response, "", " ")
if err != nil {
logger.Error("failed marshaling JSON", "reason", err)
continue
}
fmt.Println(string(bs))
checkpoint = response.Checkpoint
}
}
}