forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridges.go
208 lines (175 loc) · 5.83 KB
/
bridges.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
package query
import (
"bufio"
"context"
"io"
platform2 "github.com/influxdata/influxdb/v2/kit/platform"
"github.com/influxdata/flux"
"github.com/influxdata/flux/csv"
platform "github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/kit/check"
"github.com/influxdata/influxdb/v2/kit/tracing"
)
// QueryServiceBridge implements the QueryService interface while consuming the AsyncQueryService interface.
type QueryServiceBridge struct {
AsyncQueryService AsyncQueryService
}
func (b QueryServiceBridge) Query(ctx context.Context, req *Request) (flux.ResultIterator, error) {
query, err := b.AsyncQueryService.Query(ctx, req)
if err != nil {
return nil, err
}
return flux.NewResultIteratorFromQuery(query), nil
}
// Check returns the status of this query service. Since this bridge consumes an AsyncQueryService,
// which is not available over the network, this check always passes.
func (QueryServiceBridge) Check(context.Context) check.Response {
return check.Response{Name: "Query Service", Status: check.StatusPass}
}
// QueryServiceProxyBridge implements QueryService while consuming a ProxyQueryService interface.
type QueryServiceProxyBridge struct {
ProxyQueryService ProxyQueryService
}
func (b QueryServiceProxyBridge) Query(ctx context.Context, req *Request) (flux.ResultIterator, error) {
d := csv.Dialect{ResultEncoderConfig: csv.DefaultEncoderConfig()}
preq := &ProxyRequest{
Request: *req,
Dialect: d,
}
r, w := io.Pipe()
asri := &asyncStatsResultIterator{
r: newBufferedReadCloser(r),
statsReady: make(chan struct{}),
}
go func() {
stats, err := b.ProxyQueryService.Query(ctx, w, preq)
_ = w.CloseWithError(err)
asri.stats = stats
close(asri.statsReady)
}()
return asri, nil
}
func (b QueryServiceProxyBridge) Check(ctx context.Context) check.Response {
return b.ProxyQueryService.Check(ctx)
}
type asyncStatsResultIterator struct {
flux.ResultIterator
// The buffered reader and any error that has been
// encountered when reading.
r *bufferedReadCloser
err error
// Channel that is closed when stats have been written.
statsReady chan struct{}
// Statistics gathered from calling the proxy query service.
// This field must not be read until statsReady is closed.
stats flux.Statistics
}
func (i *asyncStatsResultIterator) More() bool {
if i.ResultIterator == nil {
// Peek into the read. If there is an error
// before reading any bytes, do not use the
// result decoder and use the error that is
// returned as the error for this result iterator.
if _, err := i.r.Peek(1); err != nil {
// Only an error if this is not an EOF.
if err != io.EOF {
i.err = err
}
return false
}
// At least one byte could be read so create a result
// iterator using the reader.
dec := csv.NewMultiResultDecoder(csv.ResultDecoderConfig{})
ri, err := dec.Decode(i.r)
if err != nil {
i.err = err
return false
}
i.ResultIterator = ri
}
return i.ResultIterator.More()
}
func (i *asyncStatsResultIterator) Err() error {
if i.err != nil {
return i.err
}
return i.ResultIterator.Err()
}
func (i *asyncStatsResultIterator) Release() {
if i.ResultIterator != nil {
i.ResultIterator.Release()
}
}
func (i *asyncStatsResultIterator) Statistics() flux.Statistics {
<-i.statsReady
return i.stats
}
// ProxyQueryServiceAsyncBridge implements ProxyQueryService while consuming an AsyncQueryService
type ProxyQueryServiceAsyncBridge struct {
AsyncQueryService AsyncQueryService
}
func (b ProxyQueryServiceAsyncBridge) Query(ctx context.Context, w io.Writer, req *ProxyRequest) (flux.Statistics, error) {
span, ctx := tracing.StartSpanFromContext(ctx)
defer span.Finish()
q, err := b.AsyncQueryService.Query(ctx, &req.Request)
if err != nil {
return flux.Statistics{}, tracing.LogError(span, err)
}
results := flux.NewResultIteratorFromQuery(q)
defer results.Release()
encoder := req.Dialect.Encoder()
_, err = encoder.Encode(w, results)
// Release the results and collect the statistics regardless of the error.
results.Release()
stats := results.Statistics()
if err != nil {
return stats, tracing.LogError(span, err)
}
if results, err := q.ProfilerResults(); err != nil {
return stats, tracing.LogError(span, err)
} else if results != nil {
_, err = encoder.Encode(w, results)
if err != nil {
return stats, tracing.LogError(span, err)
}
}
return stats, nil
}
// Check returns the status of this query service. Since this bridge consumes an AsyncQueryService,
// which is not available over the network, this check always passes.
func (ProxyQueryServiceAsyncBridge) Check(context.Context) check.Response {
return check.Response{Name: "Query Service", Status: check.StatusPass}
}
// REPLQuerier implements the repl.Querier interface while consuming a QueryService
type REPLQuerier struct {
// Authorization is the authorization to provide for all requests
Authorization *platform.Authorization
// OrganizationID is the ID to provide for all requests
OrganizationID platform2.ID
QueryService QueryService
}
// Query will pack a query to be sent to a remote server for execution. deps may be safely ignored since
// they will be correctly initialized on the server side.
func (q *REPLQuerier) Query(ctx context.Context, deps flux.Dependencies, compiler flux.Compiler) (flux.ResultIterator, error) {
req := &Request{
Authorization: q.Authorization,
OrganizationID: q.OrganizationID,
Compiler: compiler,
}
return q.QueryService.Query(ctx, req)
}
// bufferedReadCloser is a bufio.Reader that implements io.ReadCloser.
type bufferedReadCloser struct {
*bufio.Reader
r io.ReadCloser
}
// newBufferedReadCloser constructs a new bufferedReadCloser.
func newBufferedReadCloser(r io.ReadCloser) *bufferedReadCloser {
return &bufferedReadCloser{
Reader: bufio.NewReader(r),
r: r,
}
}
func (br *bufferedReadCloser) Close() error {
return br.r.Close()
}