-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
218 lines (195 loc) · 5.94 KB
/
api.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
package main
import (
"context"
"fmt"
"math/rand"
"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 createCDCStream(connectedSingleNodeClient client.YBConnectedClient, tableID []byte) (*ybApi.CreateCDCStreamResponsePB, error) {
parsedTableID, err := ybdbid.TryParseFromBytes(tableID)
if err != nil {
return nil, err
}
request := &ybApi.CreateCDCStreamRequestPB{
TableId: pstring(parsedTableID.String()),
}
response := &ybApi.CreateCDCStreamResponsePB{}
requestErr := connectedSingleNodeClient.Execute(request, response)
if requestErr != nil {
return nil, requestErr
}
return response, errors.NewCDCError(response.Error)
}
func getReachableHostPorts(hostPorts []*ybApi.HostPortPB) []*ybApi.HostPortPB {
// Given a list of host ports,
// discover and keep only those host ports to which we can connect.
// This adds overhead at the start, but shortens
// subsequent connects if the cluster is configured
// with host ports which cannot be reached from the cdc client.
newHostPorts := []*ybApi.HostPortPB{}
for _, hp := range hostPorts {
singleNodeConfig := &configs.YBSingleNodeClientConfig{
MasterHostPort: fmt.Sprintf("%s:%d", *hp.Host, *hp.Port),
}
singleNodeClient, err := client.NewDefaultConnector().Connect(singleNodeConfig)
if err == nil {
singleNodeClient.Close()
newHostPorts = append(newHostPorts, hp)
}
}
return newHostPorts
}
func getSingleNodeClient(hostPorts []*ybApi.HostPortPB, logger hclog.Logger) (client.YBConnectedClient, error) {
chanClient := make(chan client.YBConnectedClient)
go func() {
outer:
for {
r := rand.Intn(len(hostPorts))
singleNodeConfig := &configs.YBSingleNodeClientConfig{
MasterHostPort: fmt.Sprintf("%s:%d", *hostPorts[r].Host, *hostPorts[r].Port),
}
singleNodeClient, err := client.NewDefaultConnector().
WithLogger(logger).
Connect(singleNodeConfig)
if err != nil {
<-time.After(time.Millisecond * 100)
continue
}
select {
case <-singleNodeClient.OnConnected():
chanClient <- singleNodeClient
break outer
case <-singleNodeClient.OnConnectError():
<-time.After(time.Millisecond * 100)
continue
}
}
}()
select {
case c := <-chanClient:
return c, nil
case <-time.After(time.Second * 10):
return nil, fmt.Errorf("timed out")
}
}
func getCDCStreamByID(ybdbClient client.YBClient, streamID []byte) (*ybApi.CDCStreamInfoPB, error) {
request := &ybApi.GetCDCStreamRequestPB{
StreamId: streamID,
}
response := &ybApi.GetCDCStreamResponsePB{}
requestErr := ybdbClient.Execute(request, response)
if requestErr != nil {
return nil, requestErr
}
if err := errors.NewMasterError(response.Error); err != nil {
return nil, err
}
return response.Stream, nil
}
func listHostPorts(ybdbClient client.YBClient) ([]*ybApi.HostPortPB, error) {
request := &ybApi.ListTabletServersRequestPB{}
response := &ybApi.ListTabletServersResponsePB{}
requestErr := ybdbClient.Execute(request, response)
hostPorts := []*ybApi.HostPortPB{}
if requestErr != nil {
return nil, requestErr
}
if err := errors.NewMasterError(response.Error); err != nil {
return nil, err
}
for _, ts := range response.Servers {
hostPorts = append(hostPorts, ts.Registration.Common.PrivateRpcAddresses...)
}
for _, ts := range response.Servers {
hostPorts = append(hostPorts, ts.Registration.Common.BroadcastAddresses...)
}
return getReachableHostPorts(hostPorts), nil
}
func listTables(ybdbClient client.YBClient, database string) (*ybApi.ListTablesResponsePB, error) {
request := &ybApi.ListTablesRequestPB{
Namespace: &ybApi.NamespaceIdentifierPB{
// ask for tables of the requested database:
Name: pstring(database),
// ask for PGSQL tables only:
DatabaseType: pYQLDatabase(ybApi.YQLDatabase_YQL_DATABASE_PGSQL),
},
RelationTypeFilter: []ybApi.RelationType{
// ask for user tables only:
ybApi.RelationType_USER_TABLE_RELATION,
},
}
response := &ybApi.ListTablesResponsePB{}
requestErr := ybdbClient.Execute(request, response)
if requestErr != nil {
return nil, requestErr
}
return response, errors.NewMasterError(response.Error)
}
func listTabletLocations(ybdbClient client.YBClient, tableID []byte) ([]*ybApi.TabletLocationsPB, error) {
request := &ybApi.GetTableLocationsRequestPB{
Table: &ybApi.TableIdentifierPB{
TableId: tableID,
},
}
response := &ybApi.GetTableLocationsResponsePB{}
requestErr := ybdbClient.Execute(request, response)
if requestErr != nil {
return nil, requestErr
}
return response.TabletLocations, errors.NewMasterError(response.Error)
}
func compareOpId(old, new *ybApi.OpIdPB) int {
if *old.Index == *new.Index && *old.Term == *new.Term {
return 0 // the same
}
if *old.Index < *new.Index || *old.Term < *new.Term {
return 1 // new is bigger than old
}
return -1 // old is bigger than new
}
func waitForTableCreateDone(ybdbClient client.YBClient, tableID []byte) error {
chanDone := make(chan struct{})
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
go func() {
for {
select {
case <-ctx.Done():
return
default:
}
request := &ybApi.IsCreateTableDoneRequestPB{
Table: &ybApi.TableIdentifierPB{
TableId: tableID,
},
}
response := &ybApi.IsCreateTableDoneResponsePB{}
requestErr := ybdbClient.Execute(request, response)
if requestErr != nil {
<-time.After(time.Millisecond * 100)
continue
}
if err := errors.NewMasterError(response.Error); err != nil {
<-time.After(time.Millisecond * 100)
continue
}
if *response.Done {
close(chanDone)
return // done
}
}
}()
select {
case <-chanDone:
cancelFunc() // cancel the context,
return nil
case <-time.After(time.Second * 30): // configurable timeout?
return fmt.Errorf("timed out")
}
}