-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_service.go
422 lines (372 loc) · 11.1 KB
/
data_service.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
package main
import (
"context"
"strings"
"github.com/childoftheuniverse/recordio"
"github.com/childoftheuniverse/red-cloud"
"github.com/childoftheuniverse/red-cloud/common"
"github.com/childoftheuniverse/red-cloud/storage"
"github.com/golang/protobuf/proto"
"github.com/prometheus/client_golang/prometheus"
"go.opencensus.io/trace"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
)
var numRequests = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "red_cloud",
Subsystem: "datanode",
Name: "num_requests",
Help: "Number of RPCs received by the data node",
}, []string{"service", "method"})
var numErrors = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "red_cloud",
Subsystem: "datanode",
Name: "num_errors",
Help: "Number of RPCs received to the data node",
}, []string{"service", "method", "error_class"})
func init() {
prometheus.MustRegister(numRequests)
prometheus.MustRegister(numErrors)
}
/*
DataNodeService implements the DataNodeService API object which offers actual
access to data contained inside the database.
*/
type DataNodeService struct {
rangeRegistry *ServingRangeRegistry
}
/*
NewDataNodeService instantiates a data node service around the specified
range registry.
*/
func NewDataNodeService(rangeRegistry *ServingRangeRegistry) *DataNodeService {
return &DataNodeService{
rangeRegistry: rangeRegistry,
}
}
/*
Get fetches an individual data cell from the involved sstables.
*/
func (dns *DataNodeService) Get(
parentCtx context.Context, req *redcloud.GetRequest) (
*redcloud.Column, error) {
var ctx context.Context
var span *trace.Span
var results = make(chan *redcloud.ColumnFamily)
var errors = make(chan error)
var doners = make(chan struct{})
var allErrors []string
var result *redcloud.Column
var sstPaths []*redcloud.SSTablePathDescription
var sstPath *redcloud.SSTablePathDescription
var path string
var numRequired int
var numDone int
var err error
ctx, span = trace.StartSpan(parentCtx, "red-cloud.DataNodeService/Get")
defer span.End()
span.AddAttributes(
trace.StringAttribute("table", req.Table),
trace.StringAttribute("column-family", req.ColumnFamily))
numRequests.With(prometheus.Labels{
"service": "DataNodeService",
"method": "Get",
}).Inc()
dns.rangeRegistry.Lock()
defer dns.rangeRegistry.Unlock()
/*
Try to figure out where the sstable files for the given path are
stored.
*/
sstPaths, err = dns.rangeRegistry.GetSSTablePathDescription(
ctx, req.Table, req.ColumnFamily, common.NewKeyRange(req.Key, req.Key))
if err != nil {
numErrors.With(prometheus.Labels{
"service": "DataNodeService",
"method": "Get",
"error_class": "get_sstable_path_description",
}).Inc()
span.AddAttributes(trace.StringAttribute("error", err.Error()))
span.Annotate(nil, "Error fetching sstable path")
return nil, err
}
for _, sstPath = range sstPaths {
if len(sstPath.MajorSstablePath) > 0 {
numRequired++
go storage.LookupInSstable(ctx, sstPath.MajorSstablePath,
[]string{req.Column}, common.NewKeyRange(req.Key, req.Key),
results, errors, doners)
}
if len(sstPath.MinorSstablePath) > 0 {
numRequired++
go storage.LookupInSstable(ctx, sstPath.MinorSstablePath,
[]string{req.Column}, common.NewKeyRange(req.Key, req.Key),
results, errors, doners)
}
numRequired += len(sstPath.RelevantJournalPaths)
for _, path = range sstPath.RelevantJournalPaths {
go storage.LookupInJournal(ctx, path, []string{req.Column},
common.NewKeyRange(req.Key, req.Key), results, errors, doners)
}
}
span.AddAttributes(
trace.Int64Attribute("files-touched", int64(numRequired)))
if numRequired == 0 {
numErrors.With(prometheus.Labels{
"service": "DataNodeService",
"method": "Get",
"error_class": "no_data_sources",
}).Inc()
span.Annotate(nil, "No data sources found")
// No sources means no data.
return nil, grpc.Errorf(codes.NotFound,
"No data sources in column family %s for key: empty",
req.ColumnFamily)
}
/*
Collect matching data from all sources (as above) and determine the
latest result. The result will be sent back to the client.
*/
for {
var cf *redcloud.ColumnFamily
var cs *redcloud.ColumnSet
var col *redcloud.Column
var found bool
var err error
select {
case err = <-errors:
allErrors = append(allErrors, err.Error())
case <-doners:
numDone++
if numDone == numRequired {
found = true
break
}
case cf = <-results:
for _, cs = range cf.ColumnSet {
for _, col = range cs.Column {
if result == nil {
result = col
} else if result.Timestamp < col.Timestamp {
result = col
}
}
}
}
if found {
break
}
}
if len(allErrors) > 0 {
numErrors.With(prometheus.Labels{
"service": "DataNodeService",
"method": "Get",
"error_class": "lookup_errors",
}).Inc()
span.AddAttributes(
trace.Int64Attribute("num-errors", int64(len(allErrors))))
span.Annotate(nil, "Read errors encountered")
return result, grpc.Errorf(codes.Internal,
strings.Join(allErrors, "; "))
}
if result == nil {
span.Annotate(nil, "Result empty")
return nil, grpc.Errorf(codes.NotFound,
"No column matched requirements")
}
return result, nil
}
/*
GetRange fetches all data cells from the involved sstables which are
matching the specified criteria.
*/
func (dns *DataNodeService) GetRange(
req *redcloud.GetRangeRequest, resp redcloud.DataNodeService_GetRangeServer) error {
var parentCtx context.Context
var ctx context.Context
var span *trace.Span
var cancel context.CancelFunc
var results = make(chan *redcloud.ColumnFamily)
var errors = make(chan error)
var doners = make(chan struct{})
var allErrors []string
var sstPaths []*redcloud.SSTablePathDescription
var sstPath *redcloud.SSTablePathDescription
var path string
var numRequired int
var numDone int
var numFound int64
var err error
parentCtx, cancel = context.WithCancel(resp.Context())
ctx, span = trace.StartSpan(parentCtx, "red-cloud.DataNodeService/GetRange")
defer span.End()
span.AddAttributes(
trace.StringAttribute("table", req.Table),
trace.StringAttribute("column-family", req.ColumnFamily))
numRequests.With(prometheus.Labels{
"service": "DataNodeService",
"method": "GetRange",
}).Inc()
dns.rangeRegistry.Lock()
defer dns.rangeRegistry.Unlock()
/*
Try to figure out where the sstable files for the given path are
stored.
*/
if sstPaths, err = dns.rangeRegistry.GetSSTablePathDescription(
ctx, req.Table, req.ColumnFamily,
common.NewKeyRange(req.StartKey, req.EndKey)); err != nil {
numErrors.With(prometheus.Labels{
"service": "DataNodeService",
"method": "GetRange",
"error_class": "get_sstable_path_description",
}).Inc()
span.AddAttributes(trace.StringAttribute("error", err.Error()))
span.Annotate(nil, "Error fetching sstable path")
return err
}
for _, sstPath = range sstPaths {
if len(sstPath.MajorSstablePath) > 0 {
numRequired++
go storage.LookupInSstable(ctx, sstPath.MajorSstablePath,
req.Column, common.NewKeyRange(req.StartKey, req.EndKey),
results, errors, doners)
}
if len(sstPath.MinorSstablePath) > 0 {
numRequired++
go storage.LookupInSstable(ctx, sstPath.MinorSstablePath,
req.Column, common.NewKeyRange(req.StartKey, req.EndKey),
results, errors, doners)
}
numRequired += len(sstPath.RelevantJournalPaths)
for _, path = range sstPath.RelevantJournalPaths {
go storage.LookupInJournal(ctx, path,
req.Column, common.NewKeyRange(req.StartKey, req.EndKey),
results, errors, doners)
}
}
span.AddAttributes(
trace.Int64Attribute("files-touched", int64(numRequired)))
if numRequired == 0 {
numErrors.With(prometheus.Labels{
"service": "DataNodeService",
"method": "GetRange",
"error_class": "no_data_sources",
}).Inc()
span.Annotate(nil, "No data sources found")
// No sources means no data.
return grpc.Errorf(codes.NotFound,
"No data sources in column family %s for key: empty",
req.ColumnFamily)
}
/*
Determine all matching columns from all selected sources (see above)
and stream them back to the client.
*/
for {
var cf *redcloud.ColumnFamily
var cs *redcloud.ColumnSet
var col *redcloud.Column
var complete bool
var err error
select {
case err = <-errors:
allErrors = append(allErrors, err.Error())
case <-doners:
numDone++
if numDone == numRequired {
complete = true
break
}
case cf = <-results:
for _, cs = range cf.ColumnSet {
var rcs = new(redcloud.ColumnSet)
rcs.Name = cs.Name
for _, col = range cs.Column {
if (req.MinTimestamp == 0 || col.Timestamp >= req.MinTimestamp) &&
(req.MaxTimestamp == 0 || col.Timestamp <= req.MaxTimestamp) {
rcs.Column = append(rcs.Column, col)
numFound++
}
}
if len(rcs.Column) > 0 {
resp.Send(rcs)
}
}
if req.MaxResults > 0 && numFound >= req.MaxResults {
complete = true
cancel()
}
}
if complete {
break
}
}
if len(allErrors) > 0 {
numErrors.With(prometheus.Labels{
"service": "DataNodeService",
"method": "GetRange",
"error_class": "lookup_errors",
}).Inc()
span.AddAttributes(
trace.Int64Attribute("num-errors", int64(len(allErrors))))
span.Annotate(nil, "Read errors encountered")
return grpc.Errorf(codes.Internal,
strings.Join(allErrors, "; "))
}
return nil
}
/*
Insert is used to set a very specific data cell to the specified value.
*/
func (dns *DataNodeService) Insert(
parentCtx context.Context, req *redcloud.InsertRequest) (
*redcloud.Empty, error) {
var ctx context.Context
var span *trace.Span
var cf redcloud.ColumnFamily
var cs *redcloud.ColumnSet
var writer *recordio.RecordWriter
var err error
ctx, span = trace.StartSpan(parentCtx, "red-cloud.DataNodeService/Insert")
defer span.End()
span.AddAttributes(
trace.StringAttribute("table", req.Table),
trace.StringAttribute("column-family", req.ColumnFamily))
numRequests.With(prometheus.Labels{
"service": "DataNodeService",
"method": "Insert",
}).Inc()
cs = new(redcloud.ColumnSet)
cs.Name = req.ColumnName
cs.Column = append(cs.Column, req.Column)
cf.Key = req.Key
cf.ColumnSet = append(cf.ColumnSet, cs)
// Get the journal writer for the corresponding data set.
dns.rangeRegistry.Lock()
defer dns.rangeRegistry.Unlock()
if writer, err = dns.rangeRegistry.GetJournalWriter(
ctx, req.Table, req.ColumnFamily, req.Key); err != nil {
numErrors.With(prometheus.Labels{
"service": "DataNodeService",
"method": "Insert",
"error_class": "get_journal_writer_failed",
}).Inc()
span.AddAttributes(trace.StringAttribute("error", err.Error()))
span.Annotate(nil, "Error setting up journal writer")
return &redcloud.Empty{}, err
}
if err = writer.WriteMessage(ctx, &cf); err != nil {
numErrors.With(prometheus.Labels{
"service": "DataNodeService",
"method": "Insert",
"error_class": "write_message_failed",
}).Inc()
span.AddAttributes(trace.StringAttribute("error", err.Error()))
span.Annotate(nil, "Error writing inserted data to journal")
} else {
dns.rangeRegistry.ReportJournalUsage(
ctx, req.Table, req.ColumnFamily, req.Key, int64(proto.Size(&cf)))
}
return &redcloud.Empty{}, err
}