This repository has been archived by the owner on Sep 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathclient.go
713 lines (644 loc) · 18.8 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
// Copyright 2017 Pilosa Corp.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
package pilosa
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"sort"
"time"
"github.com/golang/protobuf/proto"
"github.com/pilosa/go-pilosa/internal"
)
const maxHosts = 10
// // both Content-Type and Accept headers must be set for protobuf content
var protobufHeaders = map[string]string{
"Content-Type": "application/x-protobuf",
"Accept": "application/x-protobuf",
}
// Client is the HTTP client for Pilosa server.
type Client struct {
cluster *Cluster
host *URI
client *http.Client
cacheClients map[string]*Client
}
// DefaultClient creates a client with the default address and options.
func DefaultClient() *Client {
return NewClientWithURI(DefaultURI())
}
// NewClientWithURI creates a client with the given server address.
func NewClientWithURI(uri *URI) *Client {
return NewClientWithCluster(NewClusterWithHost(uri), nil)
}
// NewClientFromAddresses creates a client for a cluster specified by `hosts`. Each
// string in `hosts` is the string representation of a URI. E.G
// node0.pilosa.com:10101
func NewClientFromAddresses(addresses []string, options *ClientOptions) (*Client, error) {
uris := make([]*URI, len(addresses))
for i, address := range addresses {
uri, err := NewURIFromAddress(address)
if err != nil {
return nil, err
}
uris[i] = uri
}
cluster := NewClusterWithHost(uris...)
client := NewClientWithCluster(cluster, options)
return client, nil
}
// NewClientWithCluster creates a client with the given cluster and options.
// Pass nil for default options.
func NewClientWithCluster(cluster *Cluster, options *ClientOptions) *Client {
if options == nil {
options = &ClientOptions{}
}
return &Client{
cluster: cluster,
client: newHTTPClient(options.withDefaults()),
cacheClients: make(map[string]*Client),
}
}
// Query runs the given query against the server with the given options.
// Pass nil for default options.
func (c *Client) Query(query PQLQuery, options *QueryOptions) (*QueryResponse, error) {
if err := query.Error(); err != nil {
return nil, err
}
if options == nil {
options = &QueryOptions{}
}
data := makeRequestData(query.serialize(), options)
path := fmt.Sprintf("/index/%s/query", query.Index().name)
_, buf, err := c.httpRequest("POST", path, data, protobufHeaders, rawResponse)
if err != nil {
return nil, err
}
iqr := &internal.QueryResponse{}
err = proto.Unmarshal(buf, iqr)
if err != nil {
return nil, err
}
queryResponse, err := newQueryResponseFromInternal(iqr)
if err != nil {
return nil, err
}
if !queryResponse.Success {
return nil, NewError(queryResponse.ErrorMessage)
}
return queryResponse, nil
}
// CreateIndex creates an index on the server using the given Index struct.
func (c *Client) CreateIndex(index *Index) error {
data := []byte(index.options.String())
path := fmt.Sprintf("/index/%s", index.name)
_, _, err := c.httpRequest("POST", path, data, nil, noResponse)
if err != nil {
return err
}
if index.options.TimeQuantum != TimeQuantumNone {
err = c.patchIndexTimeQuantum(index)
}
return err
}
// CreateFrame creates a frame on the server using the given Frame struct.
func (c *Client) CreateFrame(frame *Frame) error {
data := []byte(frame.options.String())
path := fmt.Sprintf("/index/%s/frame/%s", frame.index.name, frame.name)
_, _, err := c.httpRequest("POST", path, data, nil, noResponse)
if err != nil {
return err
}
if frame.options.TimeQuantum != TimeQuantumNone {
err = c.patchFrameTimeQuantum(frame)
}
return err
}
// EnsureIndex creates an index on the server if it does not exist.
func (c *Client) EnsureIndex(index *Index) error {
err := c.CreateIndex(index)
if err == ErrorIndexExists {
return nil
}
return err
}
// EnsureFrame creates a frame on the server if it doesn't exists.
func (c *Client) EnsureFrame(frame *Frame) error {
err := c.CreateFrame(frame)
if err == ErrorFrameExists {
return nil
}
return err
}
// DeleteIndex deletes an index on the server.
func (c *Client) DeleteIndex(index *Index) error {
path := fmt.Sprintf("/index/%s", index.name)
_, _, err := c.httpRequest("DELETE", path, nil, nil, noResponse)
return err
}
// DeleteFrame deletes a frame on the server.
func (c *Client) DeleteFrame(frame *Frame) error {
path := fmt.Sprintf("/index/%s/frame/%s", frame.index.name, frame.name)
_, _, err := c.httpRequest("DELETE", path, nil, nil, noResponse)
return err
}
// SyncSchema updates a schema with the indexes and frames on the server and
// creates the indexes and frames in the schema on the server side.
// This function does not delete indexes and the frames on the server side nor in the schema.
func (c *Client) SyncSchema(schema *Schema) error {
var err error
serverSchema, err := c.Schema()
if err != nil {
return err
}
// find out local - remote schema
diffSchema := schema.diff(serverSchema)
// create the indexes and frames which doesn't exist on the server side
for indexName, index := range diffSchema.indexes {
if _, ok := serverSchema.indexes[indexName]; !ok {
c.EnsureIndex(index)
}
for _, frame := range index.frames {
c.EnsureFrame(frame)
}
}
// find out remote - local schema
diffSchema = serverSchema.diff(schema)
for indexName, index := range diffSchema.indexes {
if localIndex, ok := schema.indexes[indexName]; !ok {
schema.indexes[indexName] = index
} else {
for frameName, frame := range index.frames {
localIndex.frames[frameName] = frame
}
}
}
return nil
}
// Schema returns the indexes and frames on the server.
func (c *Client) Schema() (*Schema, error) {
status, err := c.status()
if err != nil {
return nil, err
}
if len(status.Nodes) == 0 {
return nil, errors.New("Status should contain at least 1 node")
}
schema := NewSchema()
for _, indexInfo := range status.Nodes[0].Indexes {
options := &IndexOptions{
ColumnLabel: indexInfo.Meta.ColumnLabel,
TimeQuantum: TimeQuantum(indexInfo.Meta.TimeQuantum),
}
index, err := schema.Index(indexInfo.Name, options)
if err != nil {
return nil, err
}
for _, frameInfo := range indexInfo.Frames {
frameOptions := &FrameOptions{
RowLabel: frameInfo.Meta.RowLabel,
CacheSize: frameInfo.Meta.CacheSize,
CacheType: CacheType(frameInfo.Meta.CacheType),
InverseEnabled: frameInfo.Meta.InverseEnabled,
TimeQuantum: TimeQuantum(frameInfo.Meta.TimeQuantum),
}
_, err := index.Frame(frameInfo.Name, frameOptions)
if err != nil {
return nil, err
}
}
}
return schema, nil
}
// ImportFrame imports bits from the given CSV iterator
func (c *Client) ImportFrame(frame *Frame, bitIterator BitIterator, batchSize uint) error {
const sliceWidth = 1048576
linesLeft := true
bitGroup := map[uint64][]Bit{}
var currentBatchSize uint
indexName := frame.index.name
frameName := frame.name
for linesLeft {
bit, err := bitIterator.NextBit()
if err == io.EOF {
linesLeft = false
} else if err != nil {
return err
}
slice := bit.ColumnID / sliceWidth
if sliceArray, ok := bitGroup[slice]; ok {
bitGroup[slice] = append(sliceArray, bit)
} else {
bitGroup[slice] = []Bit{bit}
}
currentBatchSize++
// if the batch is full or there's no line left, start importing bits
if currentBatchSize >= batchSize || !linesLeft {
for slice, bits := range bitGroup {
if len(bits) > 0 {
err := c.importBits(indexName, frameName, slice, bits)
if err != nil {
return err
}
}
}
bitGroup = map[uint64][]Bit{}
currentBatchSize = 0
}
}
return nil
}
func (c *Client) getDirectClient(host string) (*Client, error) {
key := host
client, ok := c.cacheClients[key]
if ok {
return client, nil
}
uri, err := NewURIFromAddress(host)
if err != nil {
return nil, err
}
client = NewClientWithURI(uri)
c.cacheClients[key] = client
return client, nil
}
func (c *Client) importBits(indexName string, frameName string, slice uint64, bits []Bit) error {
sort.Sort(bitsForSort(bits))
nodes, err := c.fetchFragmentNodes(indexName, slice)
if err != nil {
return err
}
for _, node := range nodes {
client, err := c.getDirectClient(node.Host)
if err != nil {
return err
}
err = client.importNode(bitsToImportRequest(indexName, frameName, slice, bits))
if err != nil {
return err
}
}
return nil
}
func (c *Client) fetchFragmentNodes(indexName string, slice uint64) ([]fragmentNode, error) {
path := fmt.Sprintf("/fragment/nodes?slice=%d&index=%s", slice, indexName)
_, body, err := c.httpRequest("GET", path, []byte{}, nil, errorCheckedResponse)
if err != nil {
return nil, err
}
fragmentNodes := []fragmentNode{}
err = json.Unmarshal(body, &fragmentNodes)
if err != nil {
return nil, err
}
return fragmentNodes, nil
}
func (c *Client) importNode(request *internal.ImportRequest) error {
data, _ := proto.Marshal(request)
// request.Marshal never returns an error
_, _, err := c.httpRequest("POST", "/import", data, protobufHeaders, noResponse)
if err != nil {
return err
}
return nil
}
// ExportFrame exports bits for a frame.
func (c *Client) ExportFrame(frame *Frame, view string) (BitIterator, error) {
status, err := c.status()
if err != nil {
return nil, err
}
sliceURIs := statusToNodeSlicesForIndex(status, frame.index.Name())
return NewCSVBitIterator(newExportReader(sliceURIs, frame, view)), nil
}
// Views fetches and returns the views of a frame
func (c *Client) Views(frame *Frame) ([]string, error) {
path := fmt.Sprintf("/index/%s/frame/%s/views", frame.index.name, frame.name)
_, body, err := c.httpRequest("GET", path, nil, nil, errorCheckedResponse)
if err != nil {
return nil, err
}
viewsInfo := viewsInfo{}
err = json.Unmarshal(body, &viewsInfo)
if err != nil {
return nil, err
}
return viewsInfo.Views, nil
}
func (c *Client) patchIndexTimeQuantum(index *Index) error {
data := []byte(fmt.Sprintf(`{"timeQuantum": "%s"}`, index.options.TimeQuantum))
path := fmt.Sprintf("/index/%s/time-quantum", index.name)
_, _, err := c.httpRequest("PATCH", path, data, nil, noResponse)
return err
}
func (c *Client) patchFrameTimeQuantum(frame *Frame) error {
data := []byte(fmt.Sprintf(`{"index": "%s", "frame": "%s", "timeQuantum": "%s"}`,
frame.index.name, frame.name, frame.options.TimeQuantum))
path := fmt.Sprintf("/index/%s/frame/%s/time-quantum", frame.index.name, frame.name)
_, _, err := c.httpRequest("PATCH", path, data, nil, noResponse)
return err
}
func (c *Client) status() (*Status, error) {
_, data, err := c.httpRequest("GET", "/status", nil, nil, errorCheckedResponse)
if err != nil {
return nil, err
}
root := &statusRoot{}
err = json.Unmarshal(data, root)
if err != nil {
return nil, err
}
return root.Status, nil
}
func (c *Client) httpRequest(method string, path string, data []byte, headers map[string]string, returnResponse returnClientInfo) (*http.Response, []byte, error) {
if data == nil {
data = []byte{}
}
reader := bytes.NewReader(data)
// try at most maxHosts non-failed hosts; protect against broken cluster.removeHost
var response *http.Response
var err error
for i := 0; i < maxHosts; i++ {
if c.host == nil {
c.host = c.cluster.Host()
if c.host == nil {
return nil, nil, ErrorEmptyCluster
}
}
request, err := c.makeRequest(method, path, headers, reader)
if err != nil {
return nil, nil, err
}
response, err = c.client.Do(request)
if err == nil {
break
}
c.cluster.RemoveHost(c.host)
c.host = c.cluster.Host()
}
if response == nil {
return nil, nil, ErrorTriedMaxHosts
}
defer response.Body.Close()
// TODO: Optimize buffer creation
buf, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, nil, err
}
if returnResponse == rawResponse {
return response, buf, nil
}
if response.StatusCode < 200 || response.StatusCode >= 300 {
msg := string(buf)
err = matchError(msg)
if err != nil {
return nil, nil, err
}
return nil, nil, NewError(fmt.Sprintf("Server error (%d) %s: %s", response.StatusCode, response.Status, msg))
}
if returnResponse == noResponse {
return nil, nil, nil
}
return response, buf, nil
}
func (c *Client) makeRequest(method string, path string, headers map[string]string, reader io.Reader) (*http.Request, error) {
request, err := http.NewRequest(method, c.host.Normalize()+path, reader)
if err != nil {
return nil, err
}
for k, v := range headers {
request.Header.Set(k, v)
}
return request, err
}
func newHTTPClient(options *ClientOptions) *http.Client {
transport := &http.Transport{
Dial: (&net.Dialer{
Timeout: options.ConnectTimeout,
}).Dial,
MaxIdleConnsPerHost: options.PoolSizePerRoute,
MaxIdleConns: options.TotalPoolSize,
}
return &http.Client{
Transport: transport,
Timeout: options.SocketTimeout,
}
}
func makeRequestData(query string, options *QueryOptions) []byte {
request := &internal.QueryRequest{
Query: query,
ColumnAttrs: options.Columns,
ExcludeAttrs: options.ExcludeAttrs,
ExcludeBits: options.ExcludeBits,
}
r, _ := proto.Marshal(request)
// request.Marshal never returns an error
return r
}
func matchError(msg string) error {
switch msg {
case "index already exists\n":
return ErrorIndexExists
case "frame already exists\n":
return ErrorFrameExists
}
return nil
}
func bitsToImportRequest(indexName string, frameName string, slice uint64, bits []Bit) *internal.ImportRequest {
rowIDs := make([]uint64, 0, len(bits))
columnIDs := make([]uint64, 0, len(bits))
timestamps := make([]int64, 0, len(bits))
for _, bit := range bits {
rowIDs = append(rowIDs, bit.RowID)
columnIDs = append(columnIDs, bit.ColumnID)
timestamps = append(timestamps, bit.Timestamp)
}
return &internal.ImportRequest{
Index: indexName,
Frame: frameName,
Slice: slice,
RowIDs: rowIDs,
ColumnIDs: columnIDs,
Timestamps: timestamps,
}
}
// statusToNodeSlicesForIndex finds the hosts which contains slices for the given index
func statusToNodeSlicesForIndex(status *Status, indexName string) map[uint64]*URI {
result := make(map[uint64]*URI)
for _, node := range status.Nodes {
for _, index := range node.Indexes {
if index.Name != indexName {
continue
}
for _, slice := range index.Slices {
uri, err := NewURIFromAddress(node.Host)
// err will always be nil, but prevent a panic in the odd chance the server returns an invalid URI
if err == nil {
result[slice] = uri
}
}
break
}
}
return result
}
// ClientOptions control the properties of client connection to the server
type ClientOptions struct {
SocketTimeout time.Duration
ConnectTimeout time.Duration
PoolSizePerRoute int
TotalPoolSize int
}
func (options *ClientOptions) withDefaults() (updated *ClientOptions) {
// copy options so the original is not updated
updated = &ClientOptions{}
*updated = *options
// impose defaults
if updated.SocketTimeout <= 0 {
updated.SocketTimeout = time.Second * 300
}
if updated.ConnectTimeout <= 0 {
updated.ConnectTimeout = time.Second * 30
}
if updated.PoolSizePerRoute <= 0 {
updated.PoolSizePerRoute = 10
}
if updated.TotalPoolSize <= 100 {
updated.TotalPoolSize = 100
}
return
}
// QueryOptions contains options to customize the Query function.
type QueryOptions struct {
// Columns enables returning columns in the query response.
Columns bool
// ExcludeAttrs inhibits returning attributes
ExcludeAttrs bool
// ExcludeBits inhibits returning bits
ExcludeBits bool
}
type returnClientInfo uint
const (
noResponse returnClientInfo = iota
rawResponse
errorCheckedResponse
)
type fragmentNode struct {
Host string
InternalHost string
}
type statusRoot struct {
Status *Status `json:"status"`
}
// Status contains the status information from a Pilosa server.
type Status struct {
Nodes []StatusNode
}
// StatusNode contains node information.
type StatusNode struct {
Host string
Indexes []StatusIndex
}
// StatusIndex contains index information.
type StatusIndex struct {
Name string
Meta StatusMeta
Frames []StatusFrame
Slices []uint64
}
// StatusFrame contains frame information.
type StatusFrame struct {
Name string
Meta StatusMeta
}
// StatusMeta contains options for a frame or an index.
type StatusMeta struct {
ColumnLabel string
RowLabel string
CacheType string
CacheSize uint
InverseEnabled bool
TimeQuantum string
}
type viewsInfo struct {
Views []string `json:"views"`
}
type exportReader struct {
sliceURIs map[uint64]*URI
frame *Frame
body []byte
bodyIndex int
currentSlice uint64
sliceCount uint64
view string
}
func newExportReader(sliceURIs map[uint64]*URI, frame *Frame, view string) *exportReader {
return &exportReader{
sliceURIs: sliceURIs,
frame: frame,
sliceCount: uint64(len(sliceURIs)),
view: view,
}
}
// Read updates the passed array with the exported CSV data and returns the number of bytes read
func (r *exportReader) Read(p []byte) (n int, err error) {
if r.currentSlice >= r.sliceCount {
err = io.EOF
return
}
if r.body == nil {
uri, _ := r.sliceURIs[r.currentSlice]
headers := map[string]string{
"Accept": "text/csv",
}
client := NewClientWithURI(uri)
path := fmt.Sprintf("/export?index=%s&frame=%s&slice=%d&view=%s",
r.frame.index.Name(), r.frame.Name(), r.currentSlice, r.view)
_, r.body, err = client.httpRequest("GET", path, nil, headers, errorCheckedResponse)
if err != nil {
return
}
r.bodyIndex = 0
}
n = copy(p, r.body[r.bodyIndex:])
r.bodyIndex += n
if n >= len(r.body) {
r.body = nil
r.currentSlice++
}
return
}