-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
74 lines (56 loc) · 1.28 KB
/
helpers.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
package sync
import (
"github.com/go-zookeeper/zk"
"fmt"
"path"
"sort"
"strings"
)
// createRecursively creates zookeeper path recursively
func (s *NodeSync) createRecursively(path string, acl []zk.ACL) error {
parts := strings.Split(path, "/")
if len(parts) < 1 {
return fmt.Errorf("path does not contain valid path")
}
for i := range parts {
Ensuring:
for {
child := fmt.Sprintf("%s", strings.Join(parts[:i+1], "/"))
if len(child) == 0 {
break
}
exists, _, err := s.Zk.Exists(child)
switch true {
case err == zk.ErrSessionExpired:
return err
case err != nil:
continue
case exists:
break Ensuring
default:
}
if _, err = s.Zk.Create(child, nil, 0, acl); err != nil {
continue
}
break
}
}
return nil
}
// sortChildren sorts sequenced zookeeper nodes by sequence id
// sequence id is a last part of node key and always consists of 10 digits
func sortChildren(children []string) []string {
var sorted []string
for _, child := range children {
if len(child) <= sequenceLen {
continue
}
sorted = append(sorted, child)
}
sort.SliceStable(sorted, func(i, j int) bool {
_, a := path.Split(sorted[i])
_, b := path.Split(sorted[j])
return a[len(a)-sequenceLen:] < b[len(b)-sequenceLen:]
})
return sorted
}