-
Notifications
You must be signed in to change notification settings - Fork 1
/
zookeeper.tcl
203 lines (174 loc) · 4.44 KB
/
zookeeper.tcl
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
#
# zookeeper support functions
#
#
package require base64
package require sha1
namespace eval ::zookeeper {
variable zkwd "/"
#
# mkpath - make all the znodes
# in the path that don't exist
#
proc mkpath {zk path} {
if {[$zk exists $path]} {
return
}
mkpath $zk [file dirname $path]
$zk create $path
}
proc rmrf {zk path} {
foreach child [$zk children $path] {
rmrf $zk $path/$child
}
$zk delete $path -1
}
proc read_file {file} {
set fp [open $file]
set data [read $fp]
close $fp
return $data
}
proc write_file {file data} {
set fp [open $file w]
puts -nonewline $fp $data
close $fp
}
#
# copy_file - copy a file
#
proc copy_file {zk file zpath} {
set data [read_file $file]
copy_data $zk $data $zpath
}
#
# copy_data - copy data
#
proc copy_data {zk data zpath} {
if {![$zk exists $zpath]} {
# the node doesn't exist, create it
$zk create $zpath -value $data
return
}
# node exists, but it may not have any data in it.
# if it has data, compare that and if it's the
# same, do nothing.
#
# otherwise, set the data
if {[$zk get $zpath -data zdata -version zversion]} {
# if it matches the file, leave it alone
if {[info exists zdata] && $data == $zdata} {
return
}
} else {
set zversion 0
}
# file is different set the znode to contain it
$zk set $zpath $data $zversion
}
#
# zsync - sync a filesystem tree to a znode tree
#
# zpath is prepended to the destination path
#
proc zsync {zk path zpath {pattern *}} {
mkpath $zk $zpath
set regexp "^${path}(.*)"
foreach file [lsort [glob -nocomplain -dir $path -types f $pattern]] {
regexp $regexp $file dummy tail
copy_file $zk $file $zpath$tail
}
foreach dir [lsort [glob -nocomplain -dir $path -types d *]] {
regexp $regexp $dir dummy tailDir
zsync $zk $dir $zpath$tailDir $pattern
}
}
#
# sync_znode_to_file - sync the data at zpath to
# path/zpath/Zdata and Zversion, if there is
# data at that znode.
#
proc sync_znode_to_file {zk zpath path} {
set outpath $path/$zpath
set zdataFile $outpath/Zdata
set zversionFile $outpath/Zversion
set exists 1
try {
file stat $zdataFile stat
} trap {POSIX ENOENT} {} {
set exists 0
}
if {![$zk get $zpath -data zdata -version zversion] || ![info exists zdata]} {
# there is no data at the znode. if there is a file,
# delete it.
if {$exists} {
file delete $zdataFile $zversionFile
}
return
}
# there was data at the znode.
# if it matches what we have, no need to write.
if {$exists && $stat(size) == [string length $zdata] && [read_file $zdataFile] eq $zdata && [read_file $zversionFile] eq $zversion} {
#puts stderr "skip writing $zdataFile, existing one matches"
return
}
file mkdir $outpath
if {!$exists} {
# files didn't exist, we can write them without renaming
write_file $zdataFile $zdata
write_file $zversionFile $zversion
} else {
# we need to write and the files exist, do it more cleanly
# so a reader won't see an empty file if they look at just
# the right time.
# NB still a race condition here where the zdata is updated
# but very briefly the zversion isn't.
write_file $zdataFile.new $zdata
write_file $zversionFile.new $zversion
file rename -force -- $zdataFile.new $zdataFile
file rename -force -- $zversionFile.new $zversionFile
}
}
#
# sync_ztree_to_filetree - copy a znode tree to a filesystem
#
# zpath is prepended to the destination path
#
proc sync_ztree_to_directory {zk zpath path} {
#puts stderr "sync_ztree_to_directory $zk $zpath $path"
sync_znode_to_file $zk $zpath $path
# recursively invoke ourself for all children of the current znode
set children [$zk children $zpath]
foreach znode $children {
sync_ztree_to_directory $zk [file join $zpath $znode] $path
}
}
proc zls {{where ""}} {
variable zkwd
set children [zk children [file join $zkwd $where]]
return [lsort $children]
}
proc zpwd {} {
variable zkwd
return $zkwd
}
proc zcd {{where ""}} {
variable zkwd
if {$where eq ""} {
set zkwd /
} else {
set children
}
return
}
#
# zdigest_id - given a username and password for use with the digest
# ACL scheme, return the id portion of the ACL, which is
# username:[zdigest password]
#
proc zdigest_id {un pw} {
set digest [base64::encode [sha1::sha1 -bin "${un}:${pw}"]]
return [format {%s:%s} $un $digest]
}
} ;# namespace ::zookeeper
# vim: set ts=4 sw=4 sts=4 noet :