-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_partition_filter.go
259 lines (211 loc) · 8.19 KB
/
config_partition_filter.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
// Copyright 2024 Aerospike, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// 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.
package backup
import (
"fmt"
"regexp"
"strconv"
"strings"
a "github.com./aerospike/aerospike-client-go/v8"
)
var (
//nolint:lll // The regexp is long.
expPartitionRange = regexp.MustCompile(`^([0-9]|[1-9][0-9]{1,3}|40[0-8][0-9]|409[0-5])\-([1-9]|[1-9][0-9]{1,3}|40[0-8][0-9]|409[0-6])$`)
expPartitionID = regexp.MustCompile(`^(409[0-6]|40[0-8]\d|[123]?\d{1,3}|0)$`)
expPartitionDigest = regexp.MustCompile(`^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$`)
)
// NewPartitionFilterByRange returns a partition range with boundaries specified by the provided values.
func NewPartitionFilterByRange(begin, count int) *a.PartitionFilter {
return a.NewPartitionFilterByRange(begin, count)
}
// NewPartitionFilterByID returns a partition filter by id with specified id.
func NewPartitionFilterByID(partitionID int) *a.PartitionFilter {
return a.NewPartitionFilterById(partitionID)
}
// NewPartitionFilterByDigest returns a partition filter by digest with specified value.
func NewPartitionFilterByDigest(namespace, digest string) (*a.PartitionFilter, error) {
key, err := newKeyByDigest(namespace, digest)
if err != nil {
return nil, err
}
pf := a.NewPartitionFilterByKey(key)
// We must nullify digest, to start partition from the beginning.
pf.Digest = nil
return pf, nil
}
// NewPartitionFilterAfterDigest returns a partition filter to scan records after the digest.
func NewPartitionFilterAfterDigest(namespace, digest string) (*a.PartitionFilter, error) {
key, err := newKeyByDigest(namespace, digest)
if err != nil {
return nil, err
}
defaultFilter := NewPartitionFilterAll()
begin := key.PartitionId()
count := defaultFilter.Count - begin
return &a.PartitionFilter{
Begin: begin,
Count: count,
Digest: key.Digest(),
}, nil
}
// NewPartitionFilterAll returns a partition range containing all partitions.
func NewPartitionFilterAll() *a.PartitionFilter {
return a.NewPartitionFilterByRange(0, MaxPartitions)
}
// splitPartitions splits the partitions to groups.
func splitPartitions(partitionFilters []*a.PartitionFilter, numWorkers int) ([]*a.PartitionFilter, error) {
if numWorkers < 1 || numWorkers < len(partitionFilters) {
return nil, fmt.Errorf("numWorkers is less than partitionFilters, cannot split partitionFilters")
}
// Validations.
for i := range partitionFilters {
if partitionFilters[i].Begin < 0 {
return nil, fmt.Errorf("startPartition is less than 0, cannot split partitionFilters")
}
if partitionFilters[i].Count < 1 {
return nil, fmt.Errorf("numPartitions is less than 1, cannot split partitionFilters")
}
if partitionFilters[i].Begin+partitionFilters[i].Count > MaxPartitions {
return nil, fmt.Errorf("startPartition + numPartitions is greater than the max partitionFilters: %d",
MaxPartitions)
}
}
// If we have one partition filter with range.
if len(partitionFilters) == 1 && partitionFilters[0].Count != 1 {
return splitPartitionRange(partitionFilters[0], numWorkers), nil
}
// If the same amount of partition filters, we distribute them to workers 1=1.
if len(partitionFilters) == numWorkers {
return partitionFilters, nil
}
// If we have more workers than filters.
allWorkers := numWorkers
filtersWithSingle := make([]*a.PartitionFilter, 0)
filtersWithRange := make([]*a.PartitionFilter, 0)
// Spread partitions by groups.
for _, filter := range partitionFilters {
switch filter.Count {
case 1:
filtersWithSingle = append(filtersWithSingle, filter)
default:
filtersWithRange = append(filtersWithRange, filter)
}
}
// If single filters == numWorkers, return result.
if len(filtersWithSingle) > 0 {
// If we don't have range filters, we return single filters.
if len(filtersWithRange) == 0 {
return filtersWithSingle, nil
}
allWorkers -= len(filtersWithSingle)
}
// Now, distribute remaining workers to filters with Count > 1
var totalRangeCount int
for _, filter := range filtersWithRange {
totalRangeCount += filter.Count
}
// Split remaining workers between range filters proportionally
result := make([]*a.PartitionFilter, 0, numWorkers)
result = append(result, filtersWithSingle...)
remainingWorkers := allWorkers
for i, filter := range filtersWithRange {
numRangeWorkers := (filter.Count * allWorkers) / totalRangeCount
if numRangeWorkers == 0 {
numRangeWorkers = 1
}
// for the last range we give all remaining workers
if i == len(filtersWithRange)-1 {
numRangeWorkers = remainingWorkers
}
result = append(result, splitPartitionRange(filter, numRangeWorkers)...)
remainingWorkers -= numRangeWorkers
}
return result, nil
}
// splitPartitionRange splits one range filter to numWorkers.
func splitPartitionRange(partitionFilters *a.PartitionFilter, numWorkers int) []*a.PartitionFilter {
result := make([]*a.PartitionFilter, numWorkers)
for j := 0; j < numWorkers; j++ {
result[j] = &a.PartitionFilter{}
result[j].Begin = (j * partitionFilters.Count) / numWorkers
result[j].Count = (((j + 1) * partitionFilters.Count) / numWorkers) - result[j].Begin
result[j].Begin += partitionFilters.Begin
// Set digest property for the first group.
if partitionFilters.Digest != nil && j == 0 {
result[j].Digest = partitionFilters.Digest
}
}
return result
}
// ParsePartitionFilterListString parses comma separated values to slice of partition filters.
// Example: "0-1000,1000-1000,2222,EjRWeJq83vEjRRI0VniavN7xI0U="
// Namespace can be empty, must be set only for partition by digest.
func ParsePartitionFilterListString(namespace, filters string) ([]*a.PartitionFilter, error) {
if filters == "" {
return nil, fmt.Errorf("empty filters")
}
filterSlice := strings.Split(filters, ",")
partitionFilters := make([]*a.PartitionFilter, 0, len(filterSlice))
for i := range filterSlice {
partitionFilter, err := ParsePartitionFilterString(namespace, filterSlice[i])
if err != nil {
return nil, fmt.Errorf("failed to parse partition filter, filter: %s, err: %v", filterSlice[i], err)
}
partitionFilters = append(partitionFilters, partitionFilter)
}
return partitionFilters, nil
}
// ParsePartitionFilterString check inputs from string with regexp.
// Parse values and returns *aerospike.PartitionFilter or error.
// Namespace can be empty, must be set only for partition by digest.
func ParsePartitionFilterString(namespace, filter string) (*a.PartitionFilter, error) {
// Range 0-4096
if expPartitionRange.MatchString(filter) {
return parsePartitionFilterByRange(filter)
}
// Id 1456
if expPartitionID.MatchString(filter) {
return parsePartitionFilterByID(filter)
}
// Digest (base64 string)
if expPartitionDigest.MatchString(filter) {
return parsePartitionFilterByDigest(namespace, filter)
}
return nil, fmt.Errorf("failed to parse partition filter: %s", filter)
}
func parsePartitionFilterByRange(filter string) (*a.PartitionFilter, error) {
bounds := strings.Split(filter, "-")
if len(bounds) != 2 {
return nil, fmt.Errorf("invalid partition filter: %s", filter)
}
begin, err := strconv.Atoi(bounds[0])
if err != nil {
return nil, fmt.Errorf("invalid partition filter %s begin value: %w", filter, err)
}
count, err := strconv.Atoi(bounds[1])
if err != nil {
return nil, fmt.Errorf("invalid partition filter %s count value: %w", filter, err)
}
return NewPartitionFilterByRange(begin, count), nil
}
func parsePartitionFilterByID(filter string) (*a.PartitionFilter, error) {
id, err := strconv.Atoi(filter)
if err != nil {
return nil, fmt.Errorf("invalid partition filter %s id value: %w", filter, err)
}
return NewPartitionFilterByID(id), nil
}
func parsePartitionFilterByDigest(namespace, filter string) (*a.PartitionFilter, error) {
return NewPartitionFilterByDigest(namespace, filter)
}