forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zk.go
157 lines (145 loc) · 3.77 KB
/
zk.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
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package zk
import (
"fmt"
"net/url"
"path"
"strings"
"time"
"github.com/samuel/go-zookeeper/zk"
"golang.org/x/net/context"
"k8s.io/kubernetes/contrib/mesos/pkg/scheduler/components/framework/frameworkid"
)
const RPC_TIMEOUT = time.Second * 5
type storage struct {
frameworkid.LookupFunc
frameworkid.StoreFunc
frameworkid.RemoveFunc
}
func Store(zkurl, frameworkName string) frameworkid.Storage {
// TODO(jdef) validate Config
zkServers, zkChroot, parseErr := parseZk(zkurl)
withConnection := func(ctx context.Context, f func(c *zk.Conn) error) error {
if parseErr != nil {
return parseErr
}
timeout, err := timeout(ctx)
if err != nil {
return err
}
c, _, err := zk.Connect(zkServers, timeout)
if err != nil {
return err
}
defer c.Close()
return f(c)
}
return &storage{
LookupFunc: func(ctx context.Context) (rawData string, lookupErr error) {
lookupErr = withConnection(ctx, func(c *zk.Conn) error {
data, _, err := c.Get(path.Join(zkChroot, frameworkName))
if err == nil {
rawData = string(data)
} else if err != zk.ErrNoNode {
return err
}
return nil
})
return
},
RemoveFunc: func(ctx context.Context) error {
return withConnection(ctx, func(c *zk.Conn) error {
err := c.Delete(path.Join(zkChroot, frameworkName), -1)
if err != zk.ErrNoNode {
return err
}
return nil
})
},
StoreFunc: func(ctx context.Context, id string) error {
return withConnection(ctx, func(c *zk.Conn) error {
// attempt to create the path
_, err := c.Create(
zkChroot,
[]byte(""),
0,
zk.WorldACL(zk.PermAll),
)
if err != nil && err != zk.ErrNodeExists {
return err
}
select {
case <-ctx.Done():
return ctx.Err()
default:
}
// attempt to write framework ID to <path> / <frameworkName>
fpath := path.Join(zkChroot, frameworkName)
_, err = c.Create(fpath, []byte(id), 0, zk.WorldACL(zk.PermAll))
if err != nil && err == zk.ErrNodeExists {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
// cross-check value
data, _, err := c.Get(fpath)
if err != nil {
return err
}
if string(data) != id {
return frameworkid.ErrMismatch
}
return nil
}
return err
})
},
}
}
func parseZk(zkurl string) ([]string, string, error) {
u, err := url.Parse(zkurl)
if err != nil {
return nil, "", fmt.Errorf("bad zk url: %v", err)
}
if u.Scheme != "zk" {
return nil, "", fmt.Errorf("invalid url scheme for zk url: '%v'", u.Scheme)
}
return strings.Split(u.Host, ","), u.Path, nil
}
func timeout(ctx context.Context) (time.Duration, error) {
deadline, ok := ctx.Deadline()
if !ok {
// no deadline set
return RPC_TIMEOUT, nil
}
if now := time.Now(); now.Before(deadline) {
d := deadline.Sub(now)
if d > RPC_TIMEOUT {
// deadline is too far out, use our built-in
return RPC_TIMEOUT, nil
}
return d, nil
}
// deadline has expired..
select {
case <-ctx.Done():
return 0, ctx.Err()
default:
// this should never happen because Done() should be closed
// according to the contract of context. but we have this here
// just in case.
return 0, context.DeadlineExceeded
}
}