-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathv3iotsdb.go
855 lines (735 loc) · 27.8 KB
/
v3iotsdb.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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
/*
Copyright 2018 Iguazio Systems Ltd.
Licensed under the Apache License, Version 2.0 (the "License") with
an addition restriction as set forth herein. You may not use this
file except in compliance with the License. You may obtain a copy of
the License at http://www.apache.org/licenses/LICENSE-2.0.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.
In addition, you may not use the software for any purposes that are
illegal under applicable law, and the grant of the foregoing license
under the Apache 2.0 license is conditioned upon your compliance with
such restriction.
*/
package tsdb
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"math"
"net/http"
pathUtil "path"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/nuclio/logger"
"github.com/pkg/errors"
"github.com/v3io/v3io-go/pkg/dataplane"
"github.com/v3io/v3io-go/pkg/dataplane/http"
v3ioerrors "github.com/v3io/v3io-go/pkg/errors"
"github.com/v3io/v3io-tsdb/pkg/aggregate"
"github.com/v3io/v3io-tsdb/pkg/appender"
"github.com/v3io/v3io-tsdb/pkg/chunkenc"
"github.com/v3io/v3io-tsdb/pkg/config"
"github.com/v3io/v3io-tsdb/pkg/partmgr"
"github.com/v3io/v3io-tsdb/pkg/pquerier"
"github.com/v3io/v3io-tsdb/pkg/querier"
"github.com/v3io/v3io-tsdb/pkg/utils"
)
const (
defaultHTTPTimeout = 30 * time.Second
errorCodeString = "ErrorCode"
falseConditionOuterErrorCode = "184549378" // todo: change codes
falseConditionInnerErrorCode = "385876025"
maxExpressionsInUpdateItem = 1500 // max is 2000, we're taking a buffer since it doesn't work with 2000
)
type V3ioAdapter struct {
startTimeMargin int64
logger logger.Logger
container v3io.Container
HTTPTimeout time.Duration
MetricsCache *appender.MetricsCache
cfg *config.V3ioConfig
partitionMngr *partmgr.PartitionManager
}
type DeleteParams struct {
Metrics []string
Filter string
From, To int64
DeleteAll bool
IgnoreErrors bool
}
func CreateTSDB(cfg *config.V3ioConfig, schema *config.Schema, container v3io.Container) error {
lgr, _ := utils.NewLogger(cfg.LogLevel)
httpTimeout := parseHTTPTimeout(cfg, lgr)
var err error
if container == nil {
container, err = utils.CreateContainer(lgr, cfg, httpTimeout)
if err != nil {
return errors.Wrap(err, "Failed to create a data container.")
}
}
data, err := json.Marshal(schema)
if err != nil {
return errors.Wrap(err, "Failed to marshal the TSDB schema file.")
}
dataPlaneInput := v3io.DataPlaneInput{Timeout: httpTimeout}
path := pathUtil.Join(cfg.TablePath, config.SchemaConfigFileName)
// Check whether the config file already exists, and abort if it does
for i := 0; i < 8; i++ {
_, err = container.GetObjectSync(&v3io.GetObjectInput{Path: path, DataPlaneInput: dataPlaneInput})
if err == nil {
return fmt.Errorf("A TSDB table already exists at path '" + cfg.TablePath + "'.")
} else if e, hasStatusCode := err.(v3ioerrors.ErrorWithStatusCode); hasStatusCode && e.StatusCode() != http.StatusNotFound {
err = errors.Wrapf(err, "Failed to check TSDB schema at path '%s/%s/%s'.", cfg.WebAPIEndpoint, cfg.Container, path)
lgr.Error(err)
} else {
// if no tsdb schema found
err = container.PutObjectSync(&v3io.PutObjectInput{Path: path, Body: data, DataPlaneInput: dataPlaneInput})
if err != nil {
err = errors.Wrapf(err, "Failed to create a TSDB schema at path '%s/%s/%s'.", cfg.WebAPIEndpoint, cfg.Container, path)
lgr.Error(err)
} else {
lgr.Info("Successfully created TSDB schema at path '%s/%s/%s'.", cfg.WebAPIEndpoint, cfg.Container, path)
break
}
}
}
return err
}
func parseHTTPTimeout(cfg *config.V3ioConfig, logger logger.Logger) time.Duration {
if cfg.HTTPTimeout == "" {
return defaultHTTPTimeout
}
timeout, err := time.ParseDuration(cfg.HTTPTimeout)
if err != nil {
logger.Warn("Failed to parse httpTimeout '%s'. Defaulting to %d millis.", cfg.HTTPTimeout, defaultHTTPTimeout/time.Millisecond)
return defaultHTTPTimeout
}
return timeout
}
// Create a new TSDB adapter, similar to Prometheus TSDB adapter but with a few
// extensions. The Prometheus compliant adapter is found under /promtsdb.
func NewV3ioAdapter(cfg *config.V3ioConfig, container v3io.Container, logger logger.Logger) (*V3ioAdapter, error) {
var err error
newV3ioAdapter := V3ioAdapter{}
newV3ioAdapter.cfg = cfg
if logger != nil {
newV3ioAdapter.logger = logger
} else {
newV3ioAdapter.logger, err = utils.NewLogger(cfg.LogLevel)
if err != nil {
return nil, err
}
}
newV3ioAdapter.HTTPTimeout = parseHTTPTimeout(cfg, logger)
if container != nil {
newV3ioAdapter.container = container
} else {
newV3ioAdapter.container, err = utils.CreateContainer(newV3ioAdapter.logger, cfg, newV3ioAdapter.HTTPTimeout)
if err != nil {
return nil, errors.Wrap(err, "Failed to create V3IO data container")
}
}
err = newV3ioAdapter.connect()
return &newV3ioAdapter, err
}
func NewContainer(v3ioURL string, numWorkers int, accessKey string, username string, password string, containerName string, logger logger.Logger) (v3io.Container, error) {
newContextInput := &v3iohttp.NewContextInput{
NumWorkers: numWorkers,
}
ctx, err := v3iohttp.NewContext(logger, newContextInput)
if err != nil {
return nil, err
}
session, err := ctx.NewSession(&v3io.NewSessionInput{URL: v3ioURL, Username: username, Password: password, AccessKey: accessKey})
if err != nil {
return nil, errors.Wrap(err, "Failed to create session.")
}
container, err := session.NewContainer(&v3io.NewContainerInput{ContainerName: containerName})
if err != nil {
return nil, err
}
return container, nil
}
func (a *V3ioAdapter) GetSchema() *config.Schema {
return a.partitionMngr.GetConfig()
}
func (a *V3ioAdapter) GetLogger(child string) logger.Logger {
return a.logger.GetChild(child)
}
func (a *V3ioAdapter) GetContainer() (v3io.Container, string) {
return a.container, a.cfg.TablePath
}
func (a *V3ioAdapter) connect() error {
fullpath := fmt.Sprintf("%s/%s/%s", a.cfg.WebAPIEndpoint, a.cfg.Container, a.cfg.TablePath)
resp, err := a.container.GetObjectSync(&v3io.GetObjectInput{Path: pathUtil.Join(a.cfg.TablePath, config.SchemaConfigFileName)})
if err != nil {
if utils.IsNotExistsError(err) {
return errors.Errorf("No TSDB schema file found at '%s'.", fullpath)
}
return errors.Wrapf(err, "Failed to read a TSDB schema from '%s'.", fullpath)
}
tableSchema := config.Schema{}
err = json.Unmarshal(resp.Body(), &tableSchema)
if err != nil {
return errors.Wrapf(err, "Failed to unmarshal the TSDB schema at '%s', got: %v .", fullpath, string(resp.Body()))
}
a.partitionMngr, err = partmgr.NewPartitionMngr(&tableSchema, a.container, a.cfg)
if err != nil {
return errors.Wrapf(err, "Failed to create a TSDB partition manager at '%s'.", fullpath)
}
err = a.partitionMngr.Init()
if err != nil {
return errors.Wrapf(err, "Failed to initialize the TSDB partition manager at: %s", fullpath)
}
a.logger.Debug("Running with the following TSDB configuration: %+v\n", a.cfg)
return nil
}
func (a *V3ioAdapter) InitAppenderCache() error {
if a.MetricsCache == nil {
a.MetricsCache = appender.NewMetricsCache(a.container, a.logger, a.cfg, a.partitionMngr)
return a.MetricsCache.Start()
}
return nil
}
// Create an appender interface, for writing performance
func (a *V3ioAdapter) Appender() (Appender, error) {
err := a.InitAppenderCache()
if err != nil {
return nil, err
}
newAppender := v3ioAppender{metricsCache: a.MetricsCache}
return newAppender, nil
}
func (a *V3ioAdapter) StartTime() (int64, error) {
startTime := time.Now().Unix() * 1000
return startTime - 1000*3600*24*1000, nil // TODO: from config or DB w default
}
func (a *V3ioAdapter) Close() error {
return nil
}
// Create a Querier interface, used for time-series queries
func (a *V3ioAdapter) Querier(_ context.Context, mint, maxt int64) (*querier.V3ioQuerier, error) {
if maxt < mint {
return nil, errors.Errorf("End time '%d' is lower than start time '%d'.", maxt, mint)
}
return querier.NewV3ioQuerier(a.container, a.logger, mint, maxt, a.cfg, a.partitionMngr), nil
}
// Create a Querier interface, used for time-series queries
func (a *V3ioAdapter) QuerierV2() (*pquerier.V3ioQuerier, error) {
return pquerier.NewV3ioQuerier(a.container, a.logger, a.cfg, a.partitionMngr), nil
}
// Delete by time range can optionally specify metrics and filter by labels
func (a *V3ioAdapter) DeleteDB(deleteParams DeleteParams) error {
if deleteParams.DeleteAll {
// Ignore time boundaries
deleteParams.From = 0
deleteParams.To = math.MaxInt64
} else {
if deleteParams.To == 0 {
deleteParams.To = time.Now().Unix() * 1000
}
}
// Delete Data
err := a.DeletePartitionsData(&deleteParams)
if err != nil {
return err
}
// If no data is left, delete Names folder
if len(a.partitionMngr.GetPartitionsPaths()) == 0 {
path := filepath.Join(a.cfg.TablePath, config.NamesDirectory) + "/" // Need a trailing slash
a.logger.Info("Delete metric names at path '%s'.", path)
err := utils.DeleteTable(a.logger, a.container, path, "", a.cfg.QryWorkers)
if err != nil && !deleteParams.IgnoreErrors {
return errors.Wrap(err, "Failed to delete the metric-names table.")
}
// Delete the Directory object
err = a.container.DeleteObjectSync(&v3io.DeleteObjectInput{Path: path})
if err != nil && !deleteParams.IgnoreErrors {
if !utils.IsNotExistsError(err) {
return errors.Wrapf(err, "Failed to delete table object '%s'.", path)
}
}
}
// If need to 'deleteAll', delete schema + TSDB table folder
if deleteParams.DeleteAll {
// Delete Schema file
schemaPath := pathUtil.Join(a.cfg.TablePath, config.SchemaConfigFileName)
a.logger.Info("Delete the TSDB configuration at '%s'.", schemaPath)
err := a.container.DeleteObjectSync(&v3io.DeleteObjectInput{Path: schemaPath})
if err != nil && !deleteParams.IgnoreErrors {
return errors.New("The configuration at '" + schemaPath + "' cannot be deleted or doesn't exist.")
}
// Delete the Directory object
path := a.cfg.TablePath + "/"
err = a.container.DeleteObjectSync(&v3io.DeleteObjectInput{Path: path})
if err != nil && !deleteParams.IgnoreErrors {
if !utils.IsNotExistsError(err) {
return errors.Wrapf(err, "Failed to delete table object '%s'.", path)
}
}
}
return nil
}
func (a *V3ioAdapter) DeletePartitionsData(deleteParams *DeleteParams) error {
partitions := a.partitionMngr.PartsForRange(deleteParams.From, deleteParams.To, true)
var entirelyDeletedPartitions []*partmgr.DBPartition
deleteWholePartition := deleteParams.DeleteAll || (deleteParams.Filter == "" && len(deleteParams.Metrics) == 0)
fileToDeleteChan := make(chan v3io.Item, 1024)
getItemsTerminationChan := make(chan error, len(partitions))
deleteTerminationChan := make(chan error, a.cfg.Workers)
numOfGetItemsRoutines := len(partitions)
if len(deleteParams.Metrics) > 0 {
numOfGetItemsRoutines = numOfGetItemsRoutines * len(deleteParams.Metrics)
}
goRoutinesNum := numOfGetItemsRoutines + a.cfg.Workers
onErrorTerminationChannel := make(chan struct{}, goRoutinesNum)
systemAttributesToFetch := []string{config.ObjectNameAttrName, config.MtimeSecsAttributeName, config.MtimeNSecsAttributeName, config.EncodingAttrName, config.MaxTimeAttrName}
var getItemsWorkers, getItemsTerminated, deletesTerminated int
var getItemsWG sync.WaitGroup
getItemsErrorChan := make(chan error, numOfGetItemsRoutines)
aggregates := a.GetSchema().PartitionSchemaInfo.Aggregates
hasServerSideAggregations := len(aggregates) != 1 || aggregates[0] != ""
var aggrMask aggregate.AggrType
var err error
if hasServerSideAggregations {
aggrMask, _, err = aggregate.AggregatesFromStringListWithCount(aggregates)
if err != nil {
return err
}
}
for i := 0; i < a.cfg.Workers; i++ {
go deleteObjectWorker(a.container, deleteParams, a.logger,
fileToDeleteChan, deleteTerminationChan, onErrorTerminationChannel,
aggrMask)
}
for _, part := range partitions {
partitionEntirelyInRange := deleteParams.From <= part.GetStartTime() && deleteParams.To >= part.GetEndTime()
deleteEntirePartitionFolder := partitionEntirelyInRange && deleteWholePartition
// Delete all files in partition folder and then delete the folder itself
if deleteEntirePartitionFolder {
a.logger.Info("Deleting entire partition '%s'.", part.GetTablePath())
getItemsWG.Add(1)
go deleteEntirePartition(a.logger, a.container, part.GetTablePath(), a.cfg.QryWorkers,
&getItemsWG, getItemsErrorChan)
entirelyDeletedPartitions = append(entirelyDeletedPartitions, part)
// First get all items based on filter+metric+time range then delete what is necessary
} else {
a.logger.Info("Deleting partial partition '%s'.", part.GetTablePath())
start, end := deleteParams.From, deleteParams.To
// Round the start and end times to the nearest aggregation buckets - to later on recalculate server side aggregations
if hasServerSideAggregations {
start = part.GetAggregationBucketStartTime(part.Time2Bucket(deleteParams.From))
end = part.GetAggregationBucketEndTime(part.Time2Bucket(deleteParams.To))
}
var chunkAttributesToFetch []string
// If we don't want to delete the entire object, fetch also the desired chunks to delete.
if !partitionEntirelyInRange {
chunkAttributesToFetch, _ = part.Range2Attrs("v", start, end)
}
allAttributes := append(chunkAttributesToFetch, systemAttributesToFetch...)
if len(deleteParams.Metrics) == 0 {
getItemsWorkers++
input := &v3io.GetItemsInput{Path: part.GetTablePath(),
AttributeNames: allAttributes,
Filter: deleteParams.Filter}
go getItemsWorker(a.logger, a.container, input, part, fileToDeleteChan, getItemsTerminationChan, onErrorTerminationChannel)
} else {
for _, metric := range deleteParams.Metrics {
for _, shardingKey := range part.GetShardingKeys(metric) {
getItemsWorkers++
input := &v3io.GetItemsInput{Path: part.GetTablePath(),
AttributeNames: allAttributes,
Filter: deleteParams.Filter,
ShardingKey: shardingKey}
go getItemsWorker(a.logger, a.container, input, part, fileToDeleteChan, getItemsTerminationChan, onErrorTerminationChannel)
}
}
}
}
}
a.logger.Debug("issued %v getItems", getItemsWorkers)
// Waiting fot deleting of full partitions
getItemsWG.Wait()
select {
case err = <-getItemsErrorChan:
// Signal all other goroutines to quite
for i := 0; i < goRoutinesNum; i++ {
onErrorTerminationChannel <- struct{}{}
}
return err
default:
}
if getItemsWorkers != 0 {
for deletesTerminated < a.cfg.Workers {
select {
case err := <-getItemsTerminationChan:
a.logger.Debug("finished getItems worker, total finished: %v, error: %v", getItemsTerminated+1, err)
if err != nil {
// If requested to ignore non-existing tables do not return error.
if !(deleteParams.IgnoreErrors && utils.IsNotExistsOrConflictError(err)) {
for i := 0; i < goRoutinesNum; i++ {
onErrorTerminationChannel <- struct{}{}
}
return errors.Wrapf(err, "GetItems failed during recursive delete.")
}
}
getItemsTerminated++
if getItemsTerminated == getItemsWorkers {
close(fileToDeleteChan)
}
case err := <-deleteTerminationChan:
a.logger.Debug("finished delete worker, total finished: %v, err: %v", deletesTerminated+1, err)
if err != nil {
for i := 0; i < goRoutinesNum; i++ {
onErrorTerminationChannel <- struct{}{}
}
return errors.Wrapf(err, "Delete failed during recursive delete.")
}
deletesTerminated++
}
}
} else {
close(fileToDeleteChan)
}
a.logger.Debug("finished deleting data, removing partitions from schema")
err = a.partitionMngr.DeletePartitionsFromSchema(entirelyDeletedPartitions)
if err != nil {
return err
}
return nil
}
func deleteEntirePartition(logger logger.Logger, container v3io.Container, partitionPath string, workers int,
wg *sync.WaitGroup, errChannel chan<- error) {
defer wg.Done()
err := utils.DeleteTable(logger, container, partitionPath, "", workers)
if err != nil {
errChannel <- errors.Wrapf(err, "Failed to delete partition '%s'.", partitionPath)
return
}
// Delete the Directory object
err = container.DeleteObjectSync(&v3io.DeleteObjectInput{Path: partitionPath})
if err != nil && !utils.IsNotExistsError(err) {
errChannel <- errors.Wrapf(err, "Failed to delete partition folder '%s'.", partitionPath)
}
}
func getItemsWorker(logger logger.Logger, container v3io.Container, input *v3io.GetItemsInput, partition *partmgr.DBPartition,
filesToDeleteChan chan<- v3io.Item, terminationChan chan<- error, onErrorTerminationChannel <-chan struct{}) {
for {
select {
case <-onErrorTerminationChannel:
terminationChan <- nil
return
default:
}
logger.Debug("going to getItems for partition '%v', input: %v", partition.GetTablePath(), *input)
resp, err := container.GetItemsSync(input)
if err != nil {
terminationChan <- err
return
}
resp.Release()
output := resp.Output.(*v3io.GetItemsOutput)
for _, item := range output.Items {
item["partition"] = partition
// In case we got error on delete while iterating getItems response
select {
case <-onErrorTerminationChannel:
terminationChan <- nil
return
default:
}
filesToDeleteChan <- item
}
if output.Last {
terminationChan <- nil
return
}
input.Marker = output.NextMarker
}
}
func deleteObjectWorker(container v3io.Container, deleteParams *DeleteParams, logger logger.Logger,
filesToDeleteChannel <-chan v3io.Item, terminationChan chan<- error, onErrorTerminationChannel <-chan struct{},
aggrMask aggregate.AggrType) {
for {
select {
case <-onErrorTerminationChannel:
return
case itemToDelete, ok := <-filesToDeleteChannel:
if !ok {
terminationChan <- nil
return
}
currentPartition := itemToDelete.GetField("partition").(*partmgr.DBPartition)
fileName, err := itemToDelete.GetFieldString(config.ObjectNameAttrName)
if err != nil {
terminationChan <- err
return
}
fullFileName := pathUtil.Join(currentPartition.GetTablePath(), fileName)
// Delete whole object
if deleteParams.From <= currentPartition.GetStartTime() &&
deleteParams.To >= currentPartition.GetEndTime() {
logger.Debug("delete entire item '%v' ", fullFileName)
input := &v3io.DeleteObjectInput{Path: fullFileName}
err = container.DeleteObjectSync(input)
if err != nil && !utils.IsNotExistsOrConflictError(err) {
terminationChan <- err
return
}
// Delete partial object - specific chunks or sub-parts of chunks
} else {
mtimeSecs, err := itemToDelete.GetFieldInt(config.MtimeSecsAttributeName)
if err != nil {
terminationChan <- err
return
}
mtimeNSecs, err := itemToDelete.GetFieldInt(config.MtimeNSecsAttributeName)
if err != nil {
terminationChan <- err
return
}
deleteUpdateExpression := strings.Builder{}
dataEncoding, err := getEncoding(itemToDelete)
if err != nil {
terminationChan <- err
return
}
var aggregationsByBucket map[int]*aggregate.AggregatesList
if aggrMask != 0 {
aggregationsByBucket = make(map[int]*aggregate.AggregatesList)
aggrBuckets := currentPartition.Times2BucketRange(deleteParams.From, deleteParams.To)
for _, bucketID := range aggrBuckets {
aggregationsByBucket[bucketID] = aggregate.NewAggregatesList(aggrMask)
}
}
var newMaxTime int64 = math.MaxInt64
var numberOfExpressionsInUpdate int
for attributeName, value := range itemToDelete {
if strings.HasPrefix(attributeName, "_v") {
// Check whether the whole chunk attribute needed to be deleted or just part of it.
if currentPartition.IsChunkInRangeByAttr(attributeName, deleteParams.From, deleteParams.To) {
deleteUpdateExpression.WriteString("delete(")
deleteUpdateExpression.WriteString(attributeName)
deleteUpdateExpression.WriteString(");")
} else {
currentChunksMaxTime, err := generatePartialChunkDeleteExpression(logger, &deleteUpdateExpression, attributeName,
value.([]byte), dataEncoding, deleteParams, currentPartition, aggregationsByBucket)
if err != nil {
terminationChan <- err
return
}
// We want to save the earliest max time possible
if currentChunksMaxTime < newMaxTime {
newMaxTime = currentChunksMaxTime
}
}
numberOfExpressionsInUpdate++
}
}
dbMaxTime := int64(itemToDelete.GetField(config.MaxTimeAttrName).(int))
// Update the partition's max time if needed.
if deleteParams.From < dbMaxTime && deleteParams.To >= dbMaxTime {
if deleteParams.From < newMaxTime {
// Limit the max time to be in the current partition's range
if deleteParams.From < currentPartition.GetStartTime() {
newMaxTime = currentPartition.GetStartTime()
} else {
newMaxTime = deleteParams.From
}
}
deleteUpdateExpression.WriteString(fmt.Sprintf("%v=%v;", config.MaxTimeAttrName, newMaxTime))
}
if deleteUpdateExpression.Len() > 0 {
// If there are server aggregates, update the needed buckets
if aggrMask != 0 {
for bucket, aggregations := range aggregationsByBucket {
numberOfExpressionsInUpdate = numberOfExpressionsInUpdate + len(*aggregations)
// Due to engine limitation, If we reached maximum number of expressions in an UpdateItem
// we need to break the update into chunks
// TODO: refactor in 2.8:
// in 2.8 there is a better way of doing it by uniting multiple update expressions into
// one expression by range in a form similar to `_v_sum[15...100]=0`
if numberOfExpressionsInUpdate < maxExpressionsInUpdateItem {
deleteUpdateExpression.WriteString(aggregations.SetExpr("v", bucket))
} else {
exprStr := deleteUpdateExpression.String()
logger.Debug("delete item '%v' with expression '%v'", fullFileName, exprStr)
mtimeSecs, mtimeNSecs, err = sendUpdateItem(fullFileName, exprStr, mtimeSecs, mtimeNSecs, container)
if err != nil {
terminationChan <- err
return
}
// Reset stuff for next update iteration
numberOfExpressionsInUpdate = 0
deleteUpdateExpression.Reset()
}
}
}
// If any expressions are left, save them
if deleteUpdateExpression.Len() > 0 {
exprStr := deleteUpdateExpression.String()
logger.Debug("delete item '%v' with expression '%v'", fullFileName, exprStr)
_, _, err = sendUpdateItem(fullFileName, exprStr, mtimeSecs, mtimeNSecs, container)
if err != nil {
terminationChan <- err
return
}
}
}
}
}
}
}
func sendUpdateItem(path, expr string, mtimeSecs, mtimeNSecs int, container v3io.Container) (int, int, error) {
condition := fmt.Sprintf("%v == %v and %v == %v",
config.MtimeSecsAttributeName, mtimeSecs,
config.MtimeNSecsAttributeName, mtimeNSecs)
input := &v3io.UpdateItemInput{Path: path,
Expression: &expr,
Condition: condition}
response, err := container.UpdateItemSync(input)
if err != nil && !utils.IsNotExistsOrConflictError(err) {
returnError := err
if isFalseConditionError(err) {
returnError = errors.Wrapf(err, "Item '%v' was updated while deleting occurred. Please disable any ingestion and retry.", path)
}
return 0, 0, returnError
}
output := response.Output.(*v3io.UpdateItemOutput)
return output.MtimeSecs, output.MtimeNSecs, nil
}
func getEncoding(itemToDelete v3io.Item) (chunkenc.Encoding, error) {
var encoding chunkenc.Encoding
encodingStr, ok := itemToDelete.GetField(config.EncodingAttrName).(string)
// If we don't have the encoding attribute, use XOR as default. (for backwards compatibility)
if !ok {
encoding = chunkenc.EncXOR
} else {
intEncoding, err := strconv.Atoi(encodingStr)
if err != nil {
return 0, fmt.Errorf("error parsing encoding type of chunk, got: %v, error: %v", encodingStr, err)
}
encoding = chunkenc.Encoding(intEncoding)
}
return encoding, nil
}
func generatePartialChunkDeleteExpression(logger logger.Logger, expr *strings.Builder,
attributeName string, value []byte, encoding chunkenc.Encoding, deleteParams *DeleteParams,
partition *partmgr.DBPartition, aggregationsByBucket map[int]*aggregate.AggregatesList) (int64, error) {
chunk, err := chunkenc.FromData(logger, encoding, value, 0)
if err != nil {
return 0, err
}
newChunk := chunkenc.NewChunk(logger, encoding == chunkenc.EncVariant)
appender, err := newChunk.Appender()
if err != nil {
return 0, err
}
var currentMaxTime int64
var remainingItemsCount int
iter := chunk.Iterator()
for iter.Next() {
var t int64
var v interface{}
if encoding == chunkenc.EncXOR {
t, v = iter.At()
} else {
t, v = iter.AtString()
}
// Append back only events that are not in the delete range
if t < deleteParams.From || t > deleteParams.To {
remainingItemsCount++
appender.Append(t, v)
// Calculate server-side aggregations
if aggregationsByBucket != nil {
currentAgg, ok := aggregationsByBucket[partition.Time2Bucket(t)]
// A chunk may contain more data then needed for the aggregations, if this is the case do not aggregate
if ok {
currentAgg.Aggregate(t, v)
}
}
// Update current chunk's new max time
if t > currentMaxTime {
currentMaxTime = t
}
}
}
if remainingItemsCount == 0 {
expr.WriteString("delete(")
expr.WriteString(attributeName)
expr.WriteString(");")
currentMaxTime, _ = partition.GetChunkStartTimeByAttr(attributeName)
} else {
bytes := appender.Chunk().Bytes()
val := base64.StdEncoding.EncodeToString(bytes)
expr.WriteString(fmt.Sprintf("%s=blob('%s'); ", attributeName, val))
}
return currentMaxTime, nil
}
// Return the number of items in a TSDB table
func (a *V3ioAdapter) CountMetrics(part string) (int, error) {
count := 0
paths := a.partitionMngr.GetPartitionsPaths()
for _, path := range paths {
input := v3io.GetItemsInput{Path: path, Filter: "", AttributeNames: []string{"__size"}}
iter, err := utils.NewAsyncItemsCursor(a.container, &input, a.cfg.QryWorkers, []string{}, a.logger)
if err != nil {
return 0, err
}
for iter.Next() {
count++
}
if iter.Err() != nil {
return count, errors.Wrap(iter.Err(), "Failed on count iterator.")
}
}
return count, nil
}
type v3ioAppender struct {
metricsCache *appender.MetricsCache
}
// Add a t/v value to a metric item and return refID (for AddFast)
func (a v3ioAppender) Add(lset utils.Labels, t int64, v interface{}) (uint64, error) {
return a.metricsCache.Add(lset, t, v)
}
// Faster Add using refID obtained from Add (avoid some hash/lookup overhead)
func (a v3ioAppender) AddFast(ref uint64, t int64, v interface{}) error {
return a.metricsCache.AddFast(ref, t, v)
}
// Wait for completion of all updates
func (a v3ioAppender) WaitForCompletion(timeout time.Duration) (int, error) {
return a.metricsCache.WaitForCompletion(timeout)
}
func (a v3ioAppender) Close() {
a.metricsCache.Close()
}
// In V3IO, all operations are committed (no client cache)
func (a v3ioAppender) Commit() error { return nil }
func (a v3ioAppender) Rollback() error { return nil }
// The Appender interface provides batched appends against a storage.
type Appender interface {
Add(l utils.Labels, t int64, v interface{}) (uint64, error)
AddFast(ref uint64, t int64, v interface{}) error
WaitForCompletion(timeout time.Duration) (int, error)
Commit() error
Rollback() error
Close()
}
// Check if the current error was caused specifically because the condition was evaluated to false.
func isFalseConditionError(err error) bool {
errString := err.Error()
if strings.Count(errString, errorCodeString) == 2 &&
strings.Contains(errString, falseConditionOuterErrorCode) &&
strings.Contains(errString, falseConditionInnerErrorCode) {
return true
}
return false
}