-
Notifications
You must be signed in to change notification settings - Fork 177
/
tsmeta.go
139 lines (118 loc) · 4.14 KB
/
tsmeta.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
// Copyright (c) 2021-2024 SigScalr, Inc.
//
// This file is part of SigLens Observability Solution
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package metadata
import (
"sync"
"time"
dtu "github.com/siglens/siglens/pkg/common/dtypeutils"
"github.com/siglens/siglens/pkg/config"
"github.com/siglens/siglens/pkg/segment/query/summary"
"github.com/siglens/siglens/pkg/segment/structs"
)
/*
Returns all tagTrees that we need to search and what MetricsSegments & MetricsBlocks pass time filtering.
Returns map[string][]*structs.MetricSearchRequest, mapping a tagsTree to all MetricSearchRequest that pass time filtering
*/
func GetMetricsSegmentRequests(tRange *dtu.MetricsTimeRange, querySummary *summary.QuerySummary, orgid uint64) (map[string][]*structs.MetricsSearchRequest, error) {
sTime := time.Now()
retUpdate := &sync.Mutex{}
wg := &sync.WaitGroup{}
parallelism := int(config.GetParallelism())
retVal := make(map[string][]*structs.MetricsSearchRequest)
var gErr error
globalMetricsMetadata.updateLock.Lock()
defer globalMetricsMetadata.updateLock.Unlock()
for i, mSegMeta := range globalMetricsMetadata.sortedMetricsSegmentMeta {
if !tRange.CheckRangeOverLap(mSegMeta.EarliestEpochSec, mSegMeta.LatestEpochSec) || mSegMeta.OrgId != orgid {
continue
}
wg.Add(1)
go func(msm *MetricsSegmentMetadata) {
defer wg.Done()
var forceLoaded bool
if !msm.loadedSearchMetadata {
err := msm.LoadSearchMetadata()
if err != nil {
gErr = err
return
}
forceLoaded = true
}
retBlocks := make(map[uint16]bool)
for _, mbsu := range msm.mBlockSummary {
if tRange.CheckRangeOverLap(mbsu.LowTs, mbsu.HighTs) {
retBlocks[mbsu.Blknum] = true
}
}
// copy of tag keys map
allTagKeys := make(map[string]bool)
for tk := range msm.TagKeys {
allTagKeys[tk] = true
}
if len(retBlocks) == 0 {
return
}
finalReq := &structs.MetricsSearchRequest{
MetricsKeyBaseDir: msm.MSegmentDir,
BlocksToSearch: retBlocks,
BlkWorkerParallelism: uint(2),
QueryType: structs.METRICS_SEARCH,
AllTagKeys: allTagKeys,
}
retUpdate.Lock()
_, ok := retVal[msm.TTreeDir]
if !ok {
retVal[msm.TTreeDir] = make([]*structs.MetricsSearchRequest, 0)
}
retVal[msm.TTreeDir] = append(retVal[msm.TTreeDir], finalReq)
retUpdate.Unlock()
if forceLoaded {
msm.clearSearchMetadata()
}
}(mSegMeta)
if i%parallelism == 0 {
wg.Wait()
}
}
wg.Wait()
timeElapsed := time.Since(sTime)
querySummary.UpdateTimeGettingRotatedSearchRequests(timeElapsed)
return retVal, gErr
}
func GetMetricSegmentsOverTheTimeRange(tRange *dtu.MetricsTimeRange, orgid uint64) map[string]*structs.MetricsMeta {
globalMetricsMetadata.updateLock.Lock()
defer globalMetricsMetadata.updateLock.Unlock()
metricsSegMeta := make(map[string]*structs.MetricsMeta)
for _, mSegMeta := range globalMetricsMetadata.sortedMetricsSegmentMeta {
if !tRange.CheckRangeOverLap(mSegMeta.EarliestEpochSec, mSegMeta.LatestEpochSec) || mSegMeta.OrgId != orgid {
continue
}
metricsSegMeta[mSegMeta.MSegmentDir] = &mSegMeta.MetricsMeta
}
return metricsSegMeta
}
func GetUniqueTagKeysForRotated(tRange *dtu.MetricsTimeRange, myid uint64) (map[string]struct{}, error) {
mSegmentsMeta := GetMetricSegmentsOverTheTimeRange(tRange, myid)
uniqueTagKeys := make(map[string]struct{})
// Iterate over the metadata and extract unique tag keys
for _, meta := range mSegmentsMeta {
for key := range meta.TagKeys {
uniqueTagKeys[key] = struct{}{}
}
}
return uniqueTagKeys, nil
}