-
Notifications
You must be signed in to change notification settings - Fork 492
/
wal.go
196 lines (162 loc) · 5.32 KB
/
wal.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
package wal
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/google/uuid"
"github.com/grafana/tempo/tempodb/backend"
"github.com/grafana/tempo/tempodb/backend/local"
"github.com/grafana/tempo/tempodb/encoding"
"github.com/grafana/tempo/tempodb/encoding/common"
"github.com/grafana/tempo/tempodb/encoding/vparquet"
)
const (
completedDir = "completed"
blocksDir = "blocks"
)
type WAL struct {
c *Config
l *local.Backend
}
type Config struct {
Filepath string `yaml:"path"`
CompletedFilepath string
BlocksFilepath string
Encoding backend.Encoding `yaml:"v2_encoding"`
SearchEncoding backend.Encoding `yaml:"search_encoding"`
IngestionSlack time.Duration `yaml:"ingestion_time_range_slack"`
Version string `yaml:"version,omitempty"`
}
func ValidateConfig(c *Config) error {
if c.Version == vparquet.VersionString {
return fmt.Errorf("this version of vParquet has been deprecated, please use vParquet2 or higher")
}
if _, err := encoding.FromVersion(c.Version); err != nil {
return fmt.Errorf("failed to validate block version %s: %w", c.Version, err)
}
return nil
}
func New(c *Config) (*WAL, error) {
if c.Filepath == "" {
return nil, fmt.Errorf("please provide a path for the WAL")
}
// make folder
err := os.MkdirAll(c.Filepath, os.ModePerm)
if err != nil {
return nil, err
}
// The /completed/ folder is now obsolete and no new data is written,
// but it needs to be cleared out one last time for any files left
// from a previous version.
if c.CompletedFilepath == "" {
completedFilepath := filepath.Join(c.Filepath, completedDir)
err = os.RemoveAll(completedFilepath)
if err != nil {
return nil, err
}
c.CompletedFilepath = completedFilepath
}
// Setup local backend in /blocks/
p := filepath.Join(c.Filepath, blocksDir)
err = os.MkdirAll(p, os.ModePerm)
if err != nil {
return nil, err
}
c.BlocksFilepath = p
l, err := local.NewBackend(&local.Config{
Path: p,
})
if err != nil {
return nil, err
}
return &WAL{
c: c,
l: l,
}, nil
}
// RescanBlocks returns a slice of append blocks from the wal folder
func (w *WAL) RescanBlocks(additionalStartSlack time.Duration, log log.Logger) ([]common.WALBlock, error) {
files, err := os.ReadDir(w.c.Filepath)
if err != nil {
return nil, err
}
encodings := encoding.AllEncodings()
blocks := make([]common.WALBlock, 0, len(files))
for _, f := range files {
// find owner
var owner encoding.VersionedEncoding
for _, e := range encodings {
if e.OwnsWALBlock(f) {
owner = e
break
}
}
if owner == nil {
level.Warn(log).Log("msg", "unowned file entry ignored during wal replay", "file", f.Name(), "err", err)
continue
}
start := time.Now()
fileInfo, err := f.Info()
if err != nil {
return nil, err
}
level.Info(log).Log("msg", "beginning replay", "file", f.Name(), "size", fileInfo.Size())
b, warning, err := owner.OpenWALBlock(f.Name(), w.c.Filepath, w.c.IngestionSlack, additionalStartSlack)
remove := false
if err != nil {
// wal replay failed, clear and warn
level.Warn(log).Log("msg", "failed to replay block. removing.", "file", f.Name(), "err", err)
remove = true
}
if b != nil && b.DataLength() == 0 {
level.Warn(log).Log("msg", "empty wal file. ignoring.", "file", f.Name(), "err", err)
remove = true
}
if warning != nil {
level.Warn(log).Log("msg", "received warning while replaying block. partial replay likely.", "file", f.Name(), "warning", warning, "length", b.DataLength())
}
if remove {
err = os.RemoveAll(filepath.Join(w.c.Filepath, f.Name()))
if err != nil {
return nil, err
}
continue
}
level.Info(log).Log("msg", "replay complete", "file", f.Name(), "duration", time.Since(start))
blocks = append(blocks, b)
}
return blocks, nil
}
func (w *WAL) NewBlock(id uuid.UUID, tenantID, dataEncoding string) (common.WALBlock, error) {
return w.NewBlockWithDedicatedColumns(id, tenantID, dataEncoding, nil)
}
// TODO(mapno): NewBlock and NewBlockWithDedicatedColumns should be consolidated into a single method
// They're currently separate because the dedicated columns feature is vParquet3-only,
// and we prefer to avoid leaking vParquet3-specific code where possible.
// There are a couple of ways to do this:
// 1. Add a dedicatedColumns parameter to NewBlock, and have it default to nil.
// 2. Have encoding-specific config be part of the encoding itself
// 3. Pass the meta file path to the WAL constructor
func (w *WAL) NewBlockWithDedicatedColumns(id uuid.UUID, tenantID, dataEncoding string, dedicatedColumns backend.DedicatedColumns) (common.WALBlock, error) {
return w.newBlock(id, tenantID, dataEncoding, w.c.Version, dedicatedColumns)
}
func (w *WAL) newBlock(id uuid.UUID, tenantID string, dataEncoding string, blockVersion string, dedicatedColumns backend.DedicatedColumns) (common.WALBlock, error) {
v, err := encoding.FromVersion(blockVersion)
if err != nil {
return nil, err
}
return v.CreateWALBlock(id, tenantID, w.c.Filepath, w.c.Encoding, dataEncoding, w.c.IngestionSlack, dedicatedColumns)
}
func (w *WAL) GetFilepath() string {
return w.c.Filepath
}
func (w *WAL) ClearFolder(dir string) error {
p := filepath.Join(w.c.Filepath, dir)
return os.RemoveAll(p)
}
func (w *WAL) LocalBackend() *local.Backend {
return w.l
}