-
Notifications
You must be signed in to change notification settings - Fork 3
/
partition.go
116 lines (103 loc) · 2.78 KB
/
partition.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
package schema
import (
"encoding/binary"
"hash/fnv"
"github.com/cespare/xxhash"
jump "github.com/dgryski/go-jump"
)
type PartitionByMethod uint8
const (
// partition by organization id only
PartitionByOrg PartitionByMethod = iota
// partition by the metric name only
PartitionBySeries
// partition by metric name and tags, with the best distribution
// recommended for new deployments.
PartitionBySeriesWithTags
// partition by metric name and tags, with a sub-optimal distribution when using tags.
// compatible with PartitionBySeries if a metric has no tags,
// making it possible to adopt tags for existing PartitionBySeries deployments without a migration.
PartitionBySeriesWithTagsFnv
)
func (m *MetricData) PartitionID(method PartitionByMethod, partitions int32) (int32, error) {
var partition int32
switch method {
case PartitionByOrg:
h := fnv.New32a()
err := binary.Write(h, binary.LittleEndian, uint32(m.OrgId))
if err != nil {
return 0, err
}
partition = int32(h.Sum32()) % partitions
if partition < 0 {
partition = -partition
}
case PartitionBySeries:
h := fnv.New32a()
h.Write([]byte(m.Name))
partition = int32(h.Sum32()) % partitions
if partition < 0 {
partition = -partition
}
case PartitionBySeriesWithTags:
h := xxhash.New()
if err := writeSortedTagString(h, m.Name, m.Tags); err != nil {
return 0, err
}
partition = jump.Hash(h.Sum64(), int(partitions))
case PartitionBySeriesWithTagsFnv:
h := fnv.New32a()
if err := writeSortedTagString(h, m.Name, m.Tags); err != nil {
return 0, err
}
partition = int32(h.Sum32()) % partitions
if partition < 0 {
partition = -partition
}
default:
return 0, ErrUnknownPartitionMethod
}
return partition, nil
}
func (m *MetricDefinition) PartitionID(method PartitionByMethod, partitions int32) (int32, error) {
var partition int32
switch method {
case PartitionByOrg:
h := fnv.New32a()
err := binary.Write(h, binary.LittleEndian, uint32(m.OrgId))
if err != nil {
return 0, err
}
partition = int32(h.Sum32()) % partitions
if partition < 0 {
partition = -partition
}
case PartitionBySeries:
h := fnv.New32a()
h.Write([]byte(m.Name))
partition = int32(h.Sum32()) % partitions
if partition < 0 {
partition = -partition
}
case PartitionBySeriesWithTags:
h := xxhash.New()
h.WriteString(m.NameWithTags())
partition = jump.Hash(h.Sum64(), int(partitions))
case PartitionBySeriesWithTagsFnv:
h := fnv.New32a()
if len(m.nameWithTags) > 0 {
h.Write([]byte(m.nameWithTags))
} else {
if err := writeSortedTagString(h, m.Name, m.Tags); err != nil {
return 0, err
}
}
partition = int32(h.Sum32()) % partitions
if partition < 0 {
partition = -partition
}
default:
return 0, ErrUnknownPartitionMethod
}
return partition, nil
}