This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
/
deduplicating_slice.go
227 lines (183 loc) 路 5.43 KB
/
deduplicating_slice.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
package phlaredb
import (
"context"
"os"
"path/filepath"
"sort"
"sync"
"github.com/pkg/errors"
"github.com/segmentio/parquet-go"
"go.uber.org/atomic"
"github.com/grafana/phlare/pkg/phlaredb/block"
schemav1 "github.com/grafana/phlare/pkg/phlaredb/schemas/v1"
"github.com/grafana/phlare/pkg/util/build"
)
var int64SlicePool = &sync.Pool{
New: func() interface{} {
return make([]int64, 0)
},
}
var defaultParquetConfig = &ParquetConfig{
MaxBufferRowCount: 100_000,
MaxRowGroupBytes: 10 * 128 * 1024 * 1024,
MaxBlockBytes: 10 * 10 * 128 * 1024 * 1024,
}
type deduplicatingSlice[M Models, K comparable, H Helper[M, K], P schemav1.Persister[M]] struct {
slice []M
size atomic.Uint64
lock sync.RWMutex
lookup map[K]int64
persister P
helper H
file *os.File
cfg *ParquetConfig
metrics *headMetrics
writer *parquet.GenericWriter[P]
rowsFlushed int
}
func (s *deduplicatingSlice[M, K, H, P]) Name() string {
return s.persister.Name()
}
func (s *deduplicatingSlice[M, K, H, P]) MemorySize() uint64 {
return s.size.Load()
}
func (s *deduplicatingSlice[M, K, H, P]) Size() uint64 {
return s.size.Load()
}
func (s *deduplicatingSlice[M, K, H, P]) Init(path string, cfg *ParquetConfig, metrics *headMetrics) error {
s.cfg = cfg
s.metrics = metrics
file, err := os.OpenFile(filepath.Join(path, s.persister.Name()+block.ParquetSuffix), os.O_RDWR|os.O_CREATE|os.O_EXCL, 0o644)
if err != nil {
return err
}
s.file = file
// TODO: Reuse parquet.Writer beyond life time of the head.
s.writer = parquet.NewGenericWriter[P](file, s.persister.Schema(),
parquet.ColumnPageBuffers(parquet.NewFileBufferPool(os.TempDir(), "phlaredb-parquet-buffers*")),
parquet.CreatedBy("github.com/grafana/phlare/", build.Version, build.Revision),
)
s.lookup = make(map[K]int64)
return nil
}
func (s *deduplicatingSlice[M, K, H, P]) Close() error {
if err := s.writer.Close(); err != nil {
return errors.Wrap(err, "closing parquet writer")
}
if err := s.file.Close(); err != nil {
return errors.Wrap(err, "closing parquet file")
}
return nil
}
func (s *deduplicatingSlice[M, K, H, P]) maxRowsPerRowGroup() int {
// with empty slice we need to return early
if len(s.slice) == 0 {
return 1
}
var (
// average size per row in memory
bytesPerRow = s.Size() / uint64(len(s.slice))
// how many rows per RG with average size are fitting in the maxRowGroupBytes, ensure that we at least flush 1 row
maxRows = s.cfg.MaxRowGroupBytes / bytesPerRow
)
if maxRows <= 0 {
return 1
}
return int(maxRows)
}
func (s *deduplicatingSlice[M, K, H, P]) Flush(ctx context.Context) (numRows uint64, numRowGroups uint64, err error) {
s.lock.RLock()
defer s.lock.RUnlock()
buffer := parquet.NewBuffer(
s.persister.Schema(),
parquet.SortingRowGroupConfig(s.persister.SortingColumns()),
parquet.ColumnBufferCapacity(s.cfg.MaxBufferRowCount),
)
var (
maxRows = s.maxRowsPerRowGroup()
rowGroupsFlushed int
rowsFlushed int
)
for {
// how many rows of the head still in need of flushing
rowsToFlush := len(s.slice) - s.rowsFlushed
if rowsToFlush == 0 {
break
}
// cap max row size by bytes
if rowsToFlush > maxRows {
rowsToFlush = maxRows
}
// cap max row size by buffer
if rowsToFlush > s.cfg.MaxBufferRowCount {
rowsToFlush = s.cfg.MaxBufferRowCount
}
rows := make([]parquet.Row, rowsToFlush)
var slicePos int
for pos := range rows {
slicePos = pos + s.rowsFlushed
rows[pos] = s.persister.Deconstruct(rows[pos], uint64(slicePos), s.slice[slicePos])
}
buffer.Reset()
if _, err = buffer.WriteRows(rows); err != nil {
return 0, 0, err
}
sort.Sort(buffer)
if _, err = s.writer.WriteRowGroup(buffer); err != nil {
return 0, 0, err
}
s.rowsFlushed += rowsToFlush
rowsFlushed += rowsToFlush
rowGroupsFlushed++
}
return uint64(rowsFlushed), uint64(rowGroupsFlushed), nil
}
func (s *deduplicatingSlice[M, K, H, P]) ingest(_ context.Context, elems []M, rewriter *rewriter) error {
var (
rewritingMap = make(map[int64]int64)
missing = int64SlicePool.Get().([]int64)
)
// rewrite elements
for pos := range elems {
if err := s.helper.rewrite(rewriter, elems[pos]); err != nil {
return err
}
}
// try to find if element already exists in slice, when supposed to deduplicate
s.lock.RLock()
for pos := range elems {
k := s.helper.key(elems[pos])
if posSlice, exists := s.lookup[k]; exists {
rewritingMap[int64(s.helper.setID(uint64(pos), uint64(posSlice), elems[pos]))] = posSlice
} else {
missing = append(missing, int64(pos))
}
}
s.lock.RUnlock()
// if there are missing elements, acquire write lock
if len(missing) > 0 {
s.lock.Lock()
posSlice := int64(len(s.slice))
for _, pos := range missing {
// check again if element exists
k := s.helper.key(elems[pos])
if posSlice, exists := s.lookup[k]; exists {
rewritingMap[int64(s.helper.setID(uint64(pos), uint64(posSlice), elems[pos]))] = posSlice
continue
}
// add element to slice/map
s.slice = append(s.slice, s.helper.clone(elems[pos]))
s.lookup[k] = posSlice
rewritingMap[int64(s.helper.setID(uint64(pos), uint64(posSlice), elems[pos]))] = posSlice
posSlice++
// increase size of stored data
s.metrics.sizeBytes.WithLabelValues(s.Name()).Set(float64(s.size.Add(s.helper.size(elems[pos]))))
}
s.lock.Unlock()
}
// nolint staticcheck
int64SlicePool.Put(missing[:0])
// add rewrite information to struct
s.helper.addToRewriter(rewriter, rewritingMap)
return nil
}