-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
plan.go
245 lines (201 loc) · 5.81 KB
/
plan.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
package generate
import (
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"text/tabwriter"
"time"
"github.com/influxdata/influxdb/cmd/influx_tools/internal/errlist"
"github.com/influxdata/influxdb/cmd/influx_tools/server"
"github.com/influxdata/influxdb/services/meta"
"github.com/pkg/errors"
)
type StoragePlan struct {
Database string
Retention string
ReplicaN int
StartTime time.Time
ShardCount int
ShardDuration time.Duration
DatabasePath string
info *meta.DatabaseInfo
groups []meta.ShardGroupInfo
}
func (p *StoragePlan) String() string {
sb := new(strings.Builder)
p.PrintPlan(sb)
return sb.String()
}
func (p *StoragePlan) PrintPlan(w io.Writer) {
tw := tabwriter.NewWriter(w, 25, 4, 2, ' ', 0)
fmt.Fprintf(tw, "Data Path\t%s\n", p.ShardPath())
fmt.Fprintf(tw, "Shard Count\t%d\n", p.ShardCount)
fmt.Fprintf(tw, "Database\t%s/%s (Shard duration: %s)\n", p.Database, p.Retention, p.ShardDuration)
fmt.Fprintf(tw, "Start time\t%s\n", p.StartTime)
fmt.Fprintf(tw, "End time\t%s\n", p.EndTime())
tw.Flush()
}
func (p *StoragePlan) ShardPath() string {
return filepath.Join(p.DatabasePath, p.Retention)
}
// TimeSpan returns the total duration for which the data set.
func (p *StoragePlan) TimeSpan() time.Duration {
return p.ShardDuration * time.Duration(p.ShardCount)
}
func (p *StoragePlan) EndTime() time.Time {
return p.StartTime.Add(p.TimeSpan())
}
func (p *StoragePlan) InitMetadata(client server.MetaClient) (err error) {
if err = client.DropDatabase(p.Database); err != nil {
return err
}
rp := meta.RetentionPolicySpec{
Name: p.Retention,
ShardGroupDuration: p.ShardDuration,
ReplicaN: &p.ReplicaN,
}
info, err := client.CreateDatabaseWithRetentionPolicy(p.Database, &rp)
if err != nil {
return err
}
return p.createShardGroupMetadata(client, info.DefaultRetentionPolicy)
}
// InitFileSystem initializes the file system structure, cleaning up
// existing files and re-creating the appropriate shard directories.
func (p *StoragePlan) InitFileSystem(client server.MetaClient) error {
var err error
if err = os.RemoveAll(p.DatabasePath); err != nil {
return err
}
minT, maxT := p.TimeRange()
groups, err := client.NodeShardGroupsByTimeRange(p.Database, p.Retention, minT, maxT)
if err != nil {
return err
}
p.groups = groups
for i := 0; i < len(groups); i++ {
sgi := &groups[i]
if len(sgi.Shards) > 1 {
return fmt.Errorf("multiple shards for the same owner %v", sgi.Shards[0].Owners)
}
if err = os.MkdirAll(filepath.Join(p.ShardPath(), strconv.Itoa(int(sgi.Shards[0].ID))), 0777); err != nil {
return err
}
}
p.info = client.Database(p.Database)
return nil
}
// NodeShardGroups returns ShardGroupInfo with Shards limited to the current node
func (p *StoragePlan) NodeShardGroups() []meta.ShardGroupInfo {
return p.groups
}
func (p *StoragePlan) ShardGroups() []meta.ShardGroupInfo {
return p.info.RetentionPolicy(p.info.DefaultRetentionPolicy).ShardGroups
}
func (p *StoragePlan) createShardGroupMetadata(client server.MetaClient, rp string) error {
ts := p.StartTime.Truncate(p.ShardDuration).UTC()
var err error
groups := make([]*meta.ShardGroupInfo, p.ShardCount)
for i := 0; i < p.ShardCount; i++ {
groups[i], err = client.CreateShardGroup(p.Database, rp, ts)
if err != nil {
return err
}
ts = ts.Add(p.ShardDuration)
}
return nil
}
func (p *StoragePlan) TimeRange() (start, end time.Time) {
start = p.StartTime.Truncate(p.ShardDuration).UTC()
end = start.Add(time.Duration(p.ShardDuration.Nanoseconds() * int64(p.ShardCount)))
return start, end
}
func (p *StoragePlan) Validate() error {
// build default values
def := &planDefaults{}
WalkPlan(def, p)
// validate
val := &planValidator{}
WalkPlan(val, p)
return val.Err()
}
type Visitor interface {
Visit(node Node) Visitor
}
type Node interface{ node() }
func (*StoragePlan) node() {}
func WalkPlan(v Visitor, node Node) {
if v = v.Visit(node); v == nil {
return
}
switch n := node.(type) {
case *StoragePlan:
default:
panic(fmt.Sprintf("WalkConfig: unexpected node type %T", n))
}
}
type planValidator struct {
errs errlist.ErrorList
}
func (v *planValidator) Visit(node Node) Visitor {
switch n := node.(type) {
case *StoragePlan:
if n.DatabasePath == "" {
v.errs.Add(errors.New("missing DataPath"))
}
if n.StartTime.Add(n.TimeSpan()).After(time.Now()) {
v.errs.Add(fmt.Errorf("start time must be ≤ %s", time.Now().Truncate(n.ShardDuration).UTC().Add(-n.TimeSpan())))
}
}
return v
}
func (v *planValidator) Err() error {
return v.errs.Err()
}
type planDefaults struct{}
func (v *planDefaults) Visit(node Node) Visitor {
switch n := node.(type) {
case *StoragePlan:
if n.DatabasePath == "" {
n.DatabasePath = "${HOME}/.influxdb/data"
}
if n.Database == "" {
n.Database = "db"
}
if n.Retention == "" {
n.Retention = "autogen"
}
if n.ShardDuration == 0 {
n.ShardDuration = 24 * time.Hour
}
if n.ShardCount == 0 {
n.ShardCount = 1
}
if n.StartTime.IsZero() {
n.StartTime = time.Now().Truncate(n.ShardDuration).Add(-n.TimeSpan())
}
}
return v
}
type SchemaPlan struct {
StoragePlan *StoragePlan
Tags TagCardinalities
PointsPerSeriesPerShard int
}
func (p *SchemaPlan) String() string {
sb := new(strings.Builder)
p.PrintPlan(sb)
return sb.String()
}
func (p *SchemaPlan) PrintPlan(w io.Writer) {
tw := tabwriter.NewWriter(w, 25, 4, 2, ' ', 0)
fmt.Fprintf(tw, "Tag cardinalities\t%s\n", p.Tags)
fmt.Fprintf(tw, "Points per series per shard\t%d\n", p.PointsPerSeriesPerShard)
fmt.Fprintf(tw, "Total points per shard\t%d\n", p.Tags.Cardinality()*p.PointsPerSeriesPerShard)
fmt.Fprintf(tw, "Total series\t%d\n", p.Tags.Cardinality())
fmt.Fprintf(tw, "Total points\t%d\n", p.Tags.Cardinality()*p.StoragePlan.ShardCount*p.PointsPerSeriesPerShard)
_ = tw.Flush()
}