forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shard_space.go
150 lines (135 loc) · 3.82 KB
/
shard_space.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
package cluster
import (
"fmt"
"regexp"
"strings"
"time"
"github.com/influxdb/influxdb/common"
)
type ShardSpace struct {
// required, must be unique within the database
Name string `json:"name"`
// required, a database has many shard spaces and a shard space belongs to a database
Database string `json:"database"`
// this is optional, if they don't set it, we'll set to /.*/
Regex string `json:"regex"`
compiledRegex *regexp.Regexp
// this is optional, if they don't set it, it will default to the storage.dir in the config
RetentionPolicy string `json:"retentionPolicy"`
ShardDuration string `json:"shardDuration"`
ReplicationFactor uint32 `json:"replicationFactor"`
Split uint32 `json:"split"`
shards []*ShardData
}
const (
SEVEN_DAYS = time.Hour * 24 * 7
DEFAULT_SPLIT = 1
DEFAULT_REPLICATION_FACTOR = 1
DEFAULT_SHARD_DURATION = SEVEN_DAYS
DEFAULT_RETENTION_POLICY_DURATION = 0
)
const InfiniteRetention = time.Duration(0)
func NewShardSpace(database, name string) *ShardSpace {
s := &ShardSpace{
Database: database,
Name: name,
shards: make([]*ShardData, 0),
}
s.SetDefaults()
return s
}
func (s *ShardSpace) Validate(clusterConfig *ClusterConfiguration, checkForDb bool) error {
if clusterConfig.ShardSpaceExists(s) {
return fmt.Errorf("Shard space %s exists for db %s", s.Name, s.Database)
}
if checkForDb {
if !clusterConfig.DatabaseExists(s.Database) {
return fmt.Errorf("Database '%s' doesn't exist.", s.Database)
}
}
if s.Name == "" {
return fmt.Errorf("Shard space must have a name")
}
if s.Regex == "" {
return fmt.Errorf("A regex is required for a shard space")
}
r, err := s.compileRegex(s.Regex)
if err != nil {
return fmt.Errorf("Error parsing regex: %s", err)
}
s.compiledRegex = r
if s.Split == 0 {
s.Split = DEFAULT_SPLIT
}
if s.ReplicationFactor == 0 {
s.ReplicationFactor = DEFAULT_REPLICATION_FACTOR
}
if s.ShardDuration != "" {
if _, err := common.ParseTimeDuration(s.ShardDuration); err != nil {
return err
}
}
if s.RetentionPolicy != "" && s.RetentionPolicy != "inf" {
if _, err := common.ParseTimeDuration(s.RetentionPolicy); err != nil {
return err
}
}
return nil
}
func (s *ShardSpace) compileRegex(reg string) (*regexp.Regexp, error) {
if strings.HasPrefix(reg, "/") {
if strings.HasSuffix(reg, "/i") {
reg = fmt.Sprintf("(?i)%s", reg[1:len(reg)-2])
} else {
reg = reg[1 : len(reg)-1]
}
}
return regexp.Compile(reg)
}
func (s *ShardSpace) SetDefaults() {
r, _ := regexp.Compile(".*")
s.compiledRegex = r
s.Split = DEFAULT_SPLIT
s.ReplicationFactor = DEFAULT_REPLICATION_FACTOR
s.Regex = "/.*/"
s.RetentionPolicy = "inf"
s.ShardDuration = "7d"
}
func (s *ShardSpace) MatchesSeries(name string) bool {
if s.compiledRegex == nil {
s.compiledRegex, _ = s.compileRegex(s.Regex)
}
return s.compiledRegex.MatchString(name)
}
func (s *ShardSpace) SecondsOfDuration() float64 {
return s.ParsedShardDuration().Seconds()
}
func (s *ShardSpace) ParsedRetentionPeriod() time.Duration {
if s.RetentionPolicy == "" {
return DEFAULT_RETENTION_POLICY_DURATION
} else if s.RetentionPolicy == "inf" {
return InfiniteRetention
}
d, _ := common.ParseTimeDuration(s.RetentionPolicy)
return time.Duration(d)
}
func (s *ShardSpace) ParsedShardDuration() time.Duration {
if s.ShardDuration != "" {
d, _ := common.ParseTimeDuration(s.ShardDuration)
return time.Duration(d)
}
return DEFAULT_SHARD_DURATION
}
func (s *ShardSpace) UpdateFromSpace(space *ShardSpace) error {
r, err := s.compileRegex(space.Regex)
if err != nil {
return err
}
s.Regex = space.Regex
s.compiledRegex = r
s.RetentionPolicy = space.RetentionPolicy
s.ShardDuration = space.ShardDuration
s.ReplicationFactor = space.ReplicationFactor
s.Split = space.Split
return nil
}