-
Notifications
You must be signed in to change notification settings - Fork 19
/
create.go
141 lines (104 loc) · 3.57 KB
/
create.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
package curator
import (
"github.com/samuel/go-zookeeper/zk"
)
type createBuilder struct {
client *curatorFramework
createMode CreateMode
backgrounding backgrounding
createParentsIfNeeded bool
compress bool
acling acling
}
func (b *createBuilder) ForPath(path string) (string, error) {
return b.ForPathWithData(path, b.client.defaultData)
}
func (b *createBuilder) ForPathWithData(givenPath string, payload []byte) (string, error) {
if b.compress {
if data, err := b.client.compressionProvider.Compress(givenPath, payload); err != nil {
return "", err
} else {
payload = data
}
}
adjustedPath := b.client.fixForNamespace(givenPath, b.createMode.IsSequential())
if b.backgrounding.inBackground {
go b.pathInBackground(adjustedPath, payload, givenPath)
return b.client.unfixForNamespace(adjustedPath), nil
} else {
path, err := b.pathInForeground(adjustedPath, payload)
return b.client.unfixForNamespace(path), err
}
}
func (b *createBuilder) pathInBackground(path string, payload []byte, givenPath string) {
tracer := b.client.ZookeeperClient().StartTracer("createBuilder.pathInBackground")
defer tracer.Commit()
createdPath, err := b.pathInForeground(path, payload)
if b.backgrounding.callback != nil {
event := &curatorEvent{
eventType: CREATE,
err: err,
path: createdPath,
data: payload,
acls: b.acling.getAclList(path),
context: b.backgrounding.context,
}
if err != nil {
event.path = givenPath
}
event.name = GetNodeFromPath(event.path)
b.backgrounding.callback(b.client, event)
}
}
func (b *createBuilder) pathInForeground(path string, payload []byte) (string, error) {
zkClient := b.client.ZookeeperClient()
result, err := zkClient.NewRetryLoop().CallWithRetry(func() (interface{}, error) {
if conn, err := zkClient.Conn(); err != nil {
return nil, err
} else {
createdPath, err := conn.Create(path, payload, int32(b.createMode), b.acling.getAclList(path))
if err == zk.ErrNoNode && b.createParentsIfNeeded {
if err := MakeDirs(conn, path, false, b.acling.aclProvider); err != nil {
return "", err
}
return conn.Create(path, payload, int32(b.createMode), b.acling.getAclList(path))
} else {
return createdPath, err
}
}
})
createdPath, _ := result.(string)
return createdPath, err
}
func (b *createBuilder) CreatingParentsIfNeeded() CreateBuilder {
b.createParentsIfNeeded = true
return b
}
func (b *createBuilder) WithMode(mode CreateMode) CreateBuilder {
b.createMode = mode
return b
}
func (b *createBuilder) WithACL(acls ...zk.ACL) CreateBuilder {
b.acling.aclList = acls
return b
}
func (b *createBuilder) Compressed() CreateBuilder {
b.compress = true
return b
}
func (b *createBuilder) InBackground() CreateBuilder {
b.backgrounding = backgrounding{inBackground: true}
return b
}
func (b *createBuilder) InBackgroundWithContext(context interface{}) CreateBuilder {
b.backgrounding = backgrounding{inBackground: true, context: context}
return b
}
func (b *createBuilder) InBackgroundWithCallback(callback BackgroundCallback) CreateBuilder {
b.backgrounding = backgrounding{inBackground: true, callback: callback}
return b
}
func (b *createBuilder) InBackgroundWithCallbackAndContext(callback BackgroundCallback, context interface{}) CreateBuilder {
b.backgrounding = backgrounding{inBackground: true, context: context, callback: callback}
return b
}