-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
290 lines (250 loc) · 7.33 KB
/
client.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package gospice
import (
"context"
"crypto/x509"
"fmt"
"math"
"net/http"
"strings"
"time"
"github.com/apache/arrow/go/v16/arrow/array"
"github.com/apache/arrow/go/v16/arrow/flight"
"github.com/cenkalti/backoff/v4"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"
)
const (
MAX_MESSAGE_SIZE_BYTES = 100 * 1024 * 1024
)
var defaultCloudConfig = LoadConfig()
var defaultLocalConfig = LoadLocalConfig()
// SpiceClient is a client for Spice.ai - Data and AI infrastructure for web3
// https://spice.ai
// For documentation visit https://docs.spice.ai/sdks/go-sdk
type SpiceClient struct {
appId string
apiKey string
flightAddress string
firecacheAddress string
baseHttpUrl string
flightClient flight.Client
firecacheClient flight.Client
httpClient http.Client
backoffPolicy backoff.BackOff
maxRetries uint
}
// NewSpiceClient creates a new SpiceClient
func NewSpiceClient() *SpiceClient {
return NewSpiceClientWithAddress(defaultLocalConfig.FlightUrl, defaultLocalConfig.FirecacheUrl)
}
func NewSpiceClientWithAddress(flightAddress string, firecacheAddress string) *SpiceClient {
spiceClient := &SpiceClient{
flightAddress: flightAddress,
firecacheAddress: firecacheAddress,
baseHttpUrl: defaultCloudConfig.HttpUrl,
httpClient: http.Client{
Transport: &http.Transport{
MaxIdleConnsPerHost: 10,
DisableCompression: false,
},
},
maxRetries: 3,
}
spiceClient.backoffPolicy = spiceClient.getBackoffPolicy()
return spiceClient
}
type SpiceClientModifier func(c *SpiceClient) error
func WithApiKey(apiKey string) SpiceClientModifier {
return func(c *SpiceClient) error {
if apiKey == "" {
return fmt.Errorf("apiKey is required")
}
apiKeyParts := strings.Split(apiKey, "|")
if len(apiKeyParts) != 2 {
return fmt.Errorf("apiKey is invalid")
}
c.appId = apiKeyParts[0]
c.apiKey = apiKey
return nil
}
}
func WithFlightAddress(address string) SpiceClientModifier {
return func(c *SpiceClient) error {
c.flightAddress = address
return nil
}
}
func WithFirecacheAddress(address string) SpiceClientModifier {
return func(c *SpiceClient) error {
c.firecacheAddress = address
return nil
}
}
func WithSpiceCloudAddress() SpiceClientModifier {
return func(c *SpiceClient) error {
c.flightAddress = defaultCloudConfig.FlightUrl
c.firecacheAddress = defaultCloudConfig.FirecacheUrl
return nil
}
}
// Init initializes the SpiceClient
func (c *SpiceClient) Init(opts ...SpiceClientModifier) error {
for _, opt := range opts {
err := opt(c)
if err != nil {
return err
}
}
systemCertPool, err := x509.SystemCertPool()
if err != nil {
return fmt.Errorf("error getting system cert pool: %w", err)
}
flightClient, err := c.createClient(c.flightAddress, systemCertPool)
if err != nil {
return fmt.Errorf("error creating Spice Flight client: %w", err)
}
firecacheClient, err := c.createClient(c.firecacheAddress, systemCertPool)
if err != nil {
return fmt.Errorf("error creating Spice Firecache client: %w", err)
}
c.flightClient = flightClient
c.firecacheClient = firecacheClient
return nil
}
// Sets the maximum number of times to retry Query and FireQuery calls.
// The default is 3. Setting to 0 will disable retries.
func (c *SpiceClient) SetMaxRetries(maxRetries uint) {
c.maxRetries = maxRetries
}
// Close closes the SpiceClient and cleans up resources
func (c *SpiceClient) Close() error {
var errors []error
if c.flightClient != nil {
err := c.flightClient.Close()
if err != nil {
errors = append(errors, err)
}
}
if c.firecacheClient != nil {
err := c.firecacheClient.Close()
if err != nil {
errors = append(errors, err)
}
}
c.httpClient.CloseIdleConnections()
if len(errors) > 0 {
return fmt.Errorf("error closing SpiceClient: %v", errors)
}
return nil
}
func (c *SpiceClient) query(ctx context.Context, client flight.Client, appId string, apiKey string, sql string) (array.RecordReader, error) {
var rdr array.RecordReader
err := backoff.Retry(func() error {
var err error
rdr, err = c.queryInternal(ctx, client, appId, apiKey, sql)
if err != nil {
st, ok := status.FromError(err)
if ok {
switch st.Code() {
case codes.Unavailable, codes.Unknown, codes.DeadlineExceeded, codes.Aborted, codes.Internal:
return err
}
if strings.Contains(err.Error(), "malformed header: missing HTTP content-type") {
return err
}
if err.Error() == "rpc error: code = Unknown desc = " {
return err
}
}
return backoff.Permanent(err)
}
return nil
}, backoff.WithMaxRetries(c.backoffPolicy, uint64(c.maxRetries)))
if err != nil {
return nil, err
}
return rdr, nil
}
func (c *SpiceClient) queryInternal(ctx context.Context, client flight.Client, appId string, apiKey string, sql string) (array.RecordReader, error) {
if client == nil {
return nil, fmt.Errorf("flight client is not initialized")
}
authContext, err := client.AuthenticateBasicToken(ctx, appId, apiKey)
if err != nil {
return nil, err
}
fd := &flight.FlightDescriptor{
Type: flight.DescriptorCMD,
Cmd: []byte(sql),
}
info, err := client.GetFlightInfo(authContext, fd)
if err != nil {
return nil, err
}
stream, err := client.DoGet(authContext, info.Endpoint[0].Ticket)
if err != nil {
return nil, err
}
rdr, err := flight.NewRecordReader(stream)
if err != nil {
return nil, err
}
return rdr, nil
}
func (c *SpiceClient) createClient(address string, systemCertPool *x509.CertPool) (flight.Client, error) {
retryPolicy := fmt.Sprintf(`{
"methodConfig": [{
"name": [{"service": "arrow.flight.protocol.FlightService"}],
"waitForReady": true,
"retryPolicy": {
"MaxAttempts": %d,
"InitialBackoff": "0.1s",
"MaxBackoff": "0.225s",
"BackoffMultiplier": 1.5,
"RetryableStatusCodes": [ "UNAVAILABLE", "UNKNOWN", "INTERNAL" ]
}
}]
}`, c.maxRetries+1)
grpcDialOpts := []grpc.DialOption{
grpc.WithDefaultServiceConfig(retryPolicy),
grpc.WithDefaultCallOptions(
grpc.MaxCallRecvMsgSize(MAX_MESSAGE_SIZE_BYTES),
grpc.MaxCallSendMsgSize(MAX_MESSAGE_SIZE_BYTES),
),
}
if strings.HasPrefix(address, "grpc://") {
address = strings.TrimPrefix(address, "grpc://")
grpcDialOpts = append(grpcDialOpts, grpc.WithTransportCredentials(insecure.NewCredentials()))
} else {
grpcDialOpts = append(grpcDialOpts, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(systemCertPool, "")))
}
client, err := flight.NewClientWithMiddleware(
address,
nil,
nil,
grpcDialOpts...,
)
if err != nil {
return nil, err
}
return client, nil
}
func (c *SpiceClient) getBackoffPolicy() backoff.BackOff {
initialInterval := 250 * time.Millisecond
maxInterval := initialInterval * time.Duration(math.Ceil(float64(c.maxRetries)*backoff.DefaultMultiplier))
maxElapsedTime := maxInterval * time.Duration(c.maxRetries)
b := &backoff.ExponentialBackOff{
InitialInterval: initialInterval,
RandomizationFactor: backoff.DefaultRandomizationFactor, // 0.5
Multiplier: backoff.DefaultMultiplier, // 1.5
MaxInterval: maxInterval,
MaxElapsedTime: maxElapsedTime,
Stop: backoff.Stop,
Clock: backoff.SystemClock,
}
b.Reset()
return b
}