-
Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathmain.go
259 lines (224 loc) · 7.76 KB
/
main.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 (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// This tool can be used to verify that the index files for multiple hosts that
// share the same shards are the same. If they're not identical, it will output
// the name of any series that are missing, or have mismatched checksums. The tool
// expects a directory where each subdirectory is the name of the host, and within
// each of those subdirectories is the "data" directory for that host, exactly as
// generated by M3DB itself. See the README for more details.
package main
import (
"flag"
"io"
"io/ioutil"
"log"
_ "net/http/pprof"
"os"
"path"
"strconv"
"strings"
"time"
"github.com/m3db/m3/src/cmd/tools"
"github.com/m3db/m3/src/dbnode/persist/fs"
"github.com/m3db/m3x/ident"
"github.com/m3db/m3x/pool"
)
var flagParser = flag.NewFlagSet("Verify Index files", flag.ExitOnError)
type seriesChecksums struct {
host string
shard uint32
block int64
series seriesMap
}
type seriesMap map[string]series
type checksum uint32
type series struct {
name string
checksum checksum
}
var (
pathPrefixArg = flagParser.String("path-prefix", "/var/lib/m3db", "Path prefix - must contain a folder called 'commitlogs'")
namespaceArg = flagParser.String("namespace", "metrics", "Namespace")
shardsArg = flagParser.String("shards", "", "Shards - set comma separated list of shards")
blocksArgs = flagParser.String("blocks", "", "Start unix timestamp (Seconds) - set comma separated list of unix timestamps")
compareChecksumsArg = flagParser.Bool("compare-checksums", true, "Compare checksums")
)
var bytesPool pool.CheckedBytesPool
func main() {
flagParser.Parse(os.Args[1:])
log.SetOutput(os.Stdout)
log.Println("initializing bytes pool...")
bytesPool = tools.NewCheckedBytesPool()
var (
pathPrefix = *pathPrefixArg
namespaceStr = *namespaceArg
shardsVal = *shardsArg
blocksVal = *blocksArgs
compareChecksums = *compareChecksumsArg
)
blocks := parseBlockArgs(blocksVal)
hosts, err := ioutil.ReadDir(pathPrefix)
if err != nil {
log.Fatalf("err reading dir: %s, err: %s\n", pathPrefix, err)
}
shards := parseShards(shardsVal)
for _, block := range blocks {
for _, shard := range shards {
log.Printf("running test for shard: %d\n", shard)
allHostSeriesChecksumsForShard := []seriesChecksums{}
// Accumulate all the series checksums for each host for this shard
for _, host := range hosts {
hostShardReader, err := newReader(namespaceStr, pathPrefix, host.Name(), shard, time.Unix(block, 0))
if err != nil {
// Ignore folders for hosts that don't have this data
if err == fs.ErrCheckpointFileNotFound {
continue
}
log.Fatalf("err creating new reader: %s\n", err.Error())
}
hostShardSeriesChecksums := seriesChecksumsFromReader(hostShardReader, host.Name(), shard, block)
allHostSeriesChecksumsForShard = append(allHostSeriesChecksumsForShard, hostShardSeriesChecksums)
}
allHostSeriesMapsForShard := []seriesMap{}
for _, seriesChecksum := range allHostSeriesChecksumsForShard {
allHostSeriesMapsForShard = append(allHostSeriesMapsForShard, seriesChecksum.series)
}
allSeriesForShard := mergeMaps(allHostSeriesMapsForShard...)
for _, hostSeriesForShard := range allHostSeriesChecksumsForShard {
compareSeriesChecksums(allSeriesForShard, hostSeriesForShard, compareChecksums)
}
}
}
}
func seriesChecksumsFromReader(reader fs.DataFileSetReader, host string, shard uint32, block int64) seriesChecksums {
seriesMap := seriesMap{}
seriesChecksums := seriesChecksums{
host: host,
series: seriesMap,
shard: shard,
block: block,
}
for {
id, _, _, checksumVal, err := reader.ReadMetadata()
if err == io.EOF {
return seriesChecksums
}
if err != nil {
log.Fatal("err reading from reader: ", err.Error())
}
idString := id.String()
seriesMap[idString] = series{
name: idString,
checksum: checksum(checksumVal),
}
}
}
func compareSeriesChecksums(against seriesMap, evaluate seriesChecksums, compareChecksums bool) {
againstMap := against
evaluateMap := evaluate.series
missingSeries := []series{}
checksumMismatchSeries := []series{}
if len(againstMap) == len(evaluateMap) {
log.Printf(
"host %s has all %d series for shard: %d and block: %d",
evaluate.host, len(againstMap), evaluate.shard, evaluate.block,
)
} else {
log.Printf(
"host %s has %d series, but there are a total of %d series in shard: %d and block %d\n",
evaluate.host, len(evaluateMap), len(againstMap), evaluate.shard, evaluate.block,
)
}
for seriesName, againstSeries := range againstMap {
evaluateSeries, ok := evaluateMap[seriesName]
if !ok {
missingSeries = append(missingSeries, againstSeries)
continue
}
if compareChecksums && againstSeries.checksum != evaluateSeries.checksum {
checksumMismatchSeries = append(checksumMismatchSeries, againstSeries)
}
}
for _, missing := range missingSeries {
log.Printf("host %s is missing %s\n", evaluate.host, missing.name)
}
for _, mismatch := range checksumMismatchSeries {
log.Printf("host %s has mismatching checksum for %s\n", evaluate.host, mismatch.name)
}
}
func mergeMaps(seriesMaps ...seriesMap) seriesMap {
merged := seriesMap{}
for _, sMap := range seriesMaps {
for key, val := range sMap {
merged[key] = val
}
}
return merged
}
func newReader(namespace, pathPrefix, hostName string, shard uint32, start time.Time) (fs.DataFileSetReader, error) {
fsOpts := fs.NewOptions().SetFilePathPrefix(path.Join(pathPrefix, hostName))
reader, err := fs.NewReader(bytesPool, fsOpts)
if err != nil {
return nil, err
}
openOpts := fs.DataReaderOpenOptions{
Identifier: fs.FileSetFileIdentifier{
Namespace: ident.StringID(namespace),
Shard: shard,
BlockStart: start,
},
}
err = reader.Open(openOpts)
return reader, err
}
func parseShards(shards string) []uint32 {
var allShards []uint32
if strings.TrimSpace(shards) == "" {
return []uint32{}
}
// Handle commda-delimited shard list 1,3,5, etc
for _, shard := range strings.Split(shards, ",") {
shard = strings.TrimSpace(shard)
if shard == "" {
log.Fatalf("invalid shard list: '%s'", shards)
}
value, err := strconv.Atoi(shard)
if err != nil {
log.Fatalf("could not parse shard '%s': %v", shard, err)
}
allShards = append(allShards, uint32(value))
}
return allShards
}
func parseBlockArgs(blocks string) []int64 {
allBlocks := []int64{}
for _, block := range strings.Split(blocks, ",") {
block = strings.TrimSpace(block)
if block == "" {
log.Fatalf("invalid block list: '%s'", block)
}
value, err := strconv.Atoi(block)
if err != nil {
log.Fatalf("could not parse blocks '%s': %v", block, err)
}
allBlocks = append(allBlocks, int64(value))
}
return allBlocks
}