-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.go
361 lines (285 loc) · 8.95 KB
/
tree.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// -*- tab-width:2 -*-
package treewalk
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"time"
count "github.com/jayalane/go-counter"
lll "github.com/jayalane/go-lll"
)
const (
channelSize = 1_000_000
readdirBuffer = 50
defaultWorkers = 5
)
// MaxDepth is the greatest depth of layers you can have.
const MaxDepth = 5
// maxSplits is the largest number of directory names that can be skipped.
const maxSplits = 10
// suffix is so count doesn't have to use reflection to get caller stack.
const suffix = "treewalk"
// StringPath is a string that's the current layer ID and the previous
// layers IDs needed to get to this one. e.g. name would be the file
// name and path would be the containing directories (for a file
// search, or name would be the object Key and the path would be the
// account ID and the bucket name (for S3).
type StringPath struct {
Name string
Path []string
Value interface{} // really will just be de.DirEntry
}
// Callback is the thing called for each string read from a channel; it can do anything (print a file)
// or push more strings into a channel (if a directory element is another directory)
// if the callback wants to add more work, call t.SendOn(layer, name, old StringPath).
type Callback func(sp StringPath)
// Treewalk keeps the state involved in walking thru a tree like dataflow.
type Treewalk struct {
firstString string
numWorkers []int64
cbs []Callback
chs []chan StringPath
chsRunning []int64
skips []string
lock *sync.RWMutex
log *lll.Lll
wg *sync.WaitGroup
depth int
}
// first a utility function to do joins that treats "" as nil.
func myJoin(strs []string, delim string) string {
res := ""
seenPrev := false
for _, s := range strs {
if !seenPrev && s == "" {
seenPrev = false
continue
}
if s == "" {
continue
}
if seenPrev {
res = res + delim + s
} else {
res += s
}
seenPrev = true
}
return res
}
// New returns the context needed to start a treewalk.
func New(firstString string, depth int) Treewalk {
if depth > MaxDepth {
s := fmt.Sprintln("MaxDepth is", MaxDepth, "depth", depth, "is too high")
panic(s)
}
res := Treewalk{}
res.firstString = firstString
res.cbs = make([]Callback, depth)
res.cbs[0] = nil // unneeded
res.chs = make([]chan StringPath, depth)
res.chsRunning = make([]int64, depth)
res.numWorkers = make([]int64, depth)
res.skips = make([]string, maxSplits)
for i := range depth {
res.chs[i] = make(chan StringPath, channelSize)
res.numWorkers[i] = defaultWorkers
}
res.depth = depth
res.lock = &sync.RWMutex{}
res.wg = &sync.WaitGroup{}
log := lll.Init("Treewalk", "state")
res.log = log
return res
}
// SetLogLevel configures the underlying logging to either "network"
// (most logging) "state" "all" or "none".
func (t Treewalk) SetLogLevel(level string) {
t.log.SetLevel(level)
}
// skipDir returns if a prospective dir should be skipped
// e.g. .snapshot on a NAC. Called from defaultDirHandle, not
// essential.
func (t Treewalk) skipDir(dir string) bool {
for _, x := range t.skips {
if x == dir {
return true
}
}
return false
}
// defaultDirHandle is a default handler in the case this app is doing
// find type search on a filesystem.
func (t Treewalk) defaultDirHandle(sp StringPath) {
fullPath := append(sp.Path, sp.Name) //nolint:gocritic
fn := strings.Join(fullPath, "/")
fn = filepath.Clean(fn)
dir, err := os.Open(fn)
if err != nil {
t.log.La("Error on OpenDir", sp.Name, err)
count.IncrSuffix("dir-handler-readdir-open-err", suffix)
return
}
dirEntrylen := 0
for {
des, err := dir.Readdir(readdirBuffer) // tunable ?
if err != nil {
if errors.Is(err, io.EOF) {
break
}
t.log.La("Error on OpenDir", sp.Name, err)
count.IncrSuffix("dir-handler-readdir-open-err", suffix)
return
}
dirEntrylen += len(des)
for _, de := range des {
spNew := StringPath{Name: de.Name(), Path: fullPath, Value: de}
t.log.Ln("Got a dirEntry", de.Name())
count.IncrSuffix("dir-handler-dirent-got", suffix)
if de.IsDir() {
if t.skipDir(de.Name()) {
t.log.Ls("Skipping", de.Name())
count.IncrSuffix("dir-handler-dirent-skip", suffix)
continue
}
count.IncrSuffix("dir-handler-dirent-got-dir", suffix)
go t.SendOn(0, de.Name(), spNew) // the go is needed to avoid a deadlock
} else {
t.SendOn(1, de.Name(), spNew)
count.IncrSuffix("dir-handler-dirent-got-not-dir", suffix)
}
}
count.MarkDistributionSuffix("dir-handler-readdir-len", float64(len(des)), suffix)
count.IncrSuffix("dir-handler-readdir-ok", suffix)
}
}
// SendOn puts the new StringPath from name and old StringPath sp into
// the channel for the layer The channel isn't exposed so not every
// callback has to worry if the channel is full and starting more
// workers or whatever. It will block the caller but also will start
// more workers as needed.
func (t Treewalk) SendOn(layer int, name string, sp StringPath) {
pathNew := make([]string, len(sp.Path))
copy(pathNew, sp.Path)
spNew := StringPath{name, pathNew, sp.Value}
t.wg.Add(1)
// all the counter names out of the for loop so just run 1 time
ctrNameRestart := fmt.Sprintf("ch-layer-%d-send-restart", layer)
ctrNameSent := fmt.Sprintf("ch-layer-%d-send-sent", layer)
triesCtrName := fmt.Sprintf("ch-layer-%d-retries", layer)
numCtr := fmt.Sprintf("layer-%d-num-running", layer)
ctrNameTimedOut := fmt.Sprintf("ch-layer-%d-send-timedout", layer)
for {
numRunning := atomic.LoadInt64(&t.chsRunning[layer])
if numRunning < t.numWorkers[layer]/2 {
// restart routines if needed
count.IncrSuffix(ctrNameRestart, suffix)
t.log.Ln("Only", numRunning, "go routines for layer", layer)
t.startNGoRoutines(layer, t.numWorkers[layer]-numRunning)
}
count.MarkDistributionSuffix(numCtr, float64(numRunning), suffix)
tries := 0
select {
case t.chs[layer] <- spNew:
count.IncrSuffix(ctrNameSent, suffix)
return
case <-time.After(time.Second * 30): //nolint:mnd
count.IncrSuffix(ctrNameTimedOut, suffix)
tries++
count.MarkDistributionSuffix(triesCtrName, float64(tries), suffix)
continue // checking every 30 seconds is fine
}
}
}
// SetHandler takes a handler and a depth and saves the callback/handler.
func (t Treewalk) SetHandler(level int, cb Callback) { // int before func for formatting prettiness
t.lock.Lock()
defer t.lock.Unlock()
t.cbs[level] = cb
}
// SetNumWorkers overrides the number of go routines for each layer (default 5).
func (t Treewalk) SetNumWorkers(numWorkers []int64) {
if len(numWorkers) != t.depth {
s := fmt.Sprintln("numWorkers length", len(numWorkers), "differs from depth", t.depth)
panic(s)
}
t.lock.Lock()
defer t.lock.Unlock()
for i, n := range numWorkers {
t.log.La("Setting layer", i, "to", n, "workers")
t.numWorkers[i] = n
}
}
// SetSkipDirs takes a slice of strings of directories to skip over in
// the default layer 0 handler.
func (t *Treewalk) SetSkipDirs(skips []string) { // int before func for formatting prettiness
t.lock.Lock()
defer t.lock.Unlock()
t.log.La("Setting skip list to", skips)
t.skips = skips
t.log.La("Setting skip list to", t.skips)
}
func (t Treewalk) startNGoRoutines(layer int, num int64) {
for range num {
t.wg.Add(1) // for this go routine
atomic.AddInt64(&t.chsRunning[layer], 1)
go func(layer int) {
t.log.Ln("Starting go routine for tree walk depth", layer)
ctrNameRecv := fmt.Sprintf("ch-layer-%d-recv", layer)
ctrNameCB := fmt.Sprintf("ch-layer-%d-cb", layer)
for {
select {
case d := <-t.chs[layer]:
t.log.Ln("Got a thing {", d.Name, "} layer", layer)
count.IncrSuffix(ctrNameRecv, suffix)
switch {
case t.cbs[layer] != nil:
count.TimeFuncRunSuffix(ctrNameCB, func() {
t.cbs[layer](d)
}, suffix)
case layer == 0:
count.TimeFuncRunSuffix(ctrNameCB, func() {
t.defaultDirHandle(d)
}, suffix)
default:
s := fmt.Sprintln("empty callback misconfigured")
panic(s)
}
t.wg.Done()
case <-time.After(3 * time.Second): //nolint:mnd
t.log.La("Giving up on layer", layer, "after 3 seconds with no traffic")
ctrNameRecvTimeout := fmt.Sprintf("ch-layer-%d-recv-timeout", layer)
count.IncrSuffix(ctrNameRecvTimeout, suffix)
t.wg.Done()
atomic.AddInt64(&t.chsRunning[layer], -1)
return
}
}
}(layer)
}
}
// startGoRoutines starts the go routines for one level.
func (t Treewalk) startGoRoutines(layer int) {
t.startNGoRoutines(layer, t.numWorkers[layer])
}
// Start starts the go routines for the processing.
func (t Treewalk) Start() {
t.wg.Add(1) // for this work
for i := range t.depth {
t.startGoRoutines(i)
}
t.wg.Add(1) // for the initial dir
sp := StringPath{t.firstString, []string{}, nil}
t.chs[0] <- sp
time.Sleep(time.Second) // so there's some work done before exiting.
t.wg.Done() // this work is done
}
// Wait waits for the work to all finish.
func (t Treewalk) Wait() {
t.wg.Wait()
}