Skip to content

Commit 4b637e0

Browse files
authored
Merge pull request #206 from pdxjohnny/etcd_lib_fixes
kontrol: etcd: Debug logging and TTL fix
2 parents 3b2bec5 + 80353c2 commit 4b637e0

File tree

2 files changed

+83
-7
lines changed

2 files changed

+83
-7
lines changed

kontrol/etcd.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,31 @@ func NewEtcd(machines []string, log kite.Logger) *Etcd {
4848
panic("cannot connect to etcd cluster: " + strings.Join(machines, ","))
4949
}
5050

51-
return &Etcd{
52-
client: etcd.NewKeysAPI(client),
51+
e := &Etcd{
52+
client: NewKeysAPILogger(etcd.NewKeysAPI(client), log),
5353
log: log,
5454
}
55+
56+
// Create our prefix as a directory if it does not exist
57+
_, err = e.client.Get(context.Background(), KitesPrefix, nil)
58+
if err == nil {
59+
return e
60+
}
61+
62+
_, err = e.client.Set(
63+
context.Background(),
64+
KitesPrefix,
65+
"",
66+
&etcd.SetOptions{
67+
Dir: true,
68+
PrevExist: etcd.PrevIgnore,
69+
},
70+
)
71+
if err != nil {
72+
log.Fatal("Could not create KitesPrefix %q: %v", KitesPrefix, err)
73+
}
74+
75+
return e
5576
}
5677

5778
func (e *Etcd) Delete(k *protocol.Kite) error {
@@ -95,7 +116,7 @@ func (e *Etcd) Add(k *protocol.Kite, value *kontrolprotocol.RegisterValue) error
95116
etcdKey,
96117
valueString,
97118
&etcd.SetOptions{
98-
TTL: KeyTTL / time.Second,
119+
TTL: KeyTTL,
99120
PrevExist: etcd.PrevExist,
100121
},
101122
)
@@ -108,7 +129,7 @@ func (e *Etcd) Add(k *protocol.Kite, value *kontrolprotocol.RegisterValue) error
108129
etcdIDKey,
109130
valueString,
110131
&etcd.SetOptions{
111-
TTL: KeyTTL / time.Second,
132+
TTL: KeyTTL,
112133
PrevExist: etcd.PrevExist,
113134
},
114135
)
@@ -136,7 +157,7 @@ func (e *Etcd) Update(k *protocol.Kite, value *kontrolprotocol.RegisterValue) er
136157
etcdKey,
137158
valueString,
138159
&etcd.SetOptions{
139-
TTL: KeyTTL / time.Second,
160+
TTL: KeyTTL,
140161
PrevExist: etcd.PrevExist,
141162
},
142163
)
@@ -153,7 +174,7 @@ func (e *Etcd) Update(k *protocol.Kite, value *kontrolprotocol.RegisterValue) er
153174
etcdIDKey,
154175
valueString,
155176
&etcd.SetOptions{
156-
TTL: KeyTTL / time.Second,
177+
TTL: KeyTTL,
157178
PrevExist: etcd.PrevExist,
158179
},
159180
)
@@ -166,7 +187,7 @@ func (e *Etcd) Update(k *protocol.Kite, value *kontrolprotocol.RegisterValue) er
166187
KitesPrefix+"/"+k.Username,
167188
"",
168189
&etcd.SetOptions{
169-
TTL: KeyTTL / time.Second,
190+
TTL: KeyTTL,
170191
PrevExist: etcd.PrevExist,
171192
},
172193
)

kontrol/etcd_keysapi_logger.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package kontrol
2+
3+
import (
4+
"context"
5+
6+
etcd "github.com/coreos/etcd/client"
7+
"github.com/koding/kite"
8+
)
9+
10+
type KeysAPILogger struct {
11+
kapi etcd.KeysAPI
12+
log kite.Logger
13+
}
14+
15+
func NewKeysAPILogger(kapi etcd.KeysAPI, log kite.Logger) KeysAPILogger {
16+
return KeysAPILogger{
17+
kapi: kapi,
18+
log: log,
19+
}
20+
}
21+
22+
func (k KeysAPILogger) Get(ctx context.Context, key string, opts *etcd.GetOptions) (*etcd.Response, error) {
23+
k.log.Debug("Get: key: %v opts: %v", key, opts)
24+
return k.kapi.Get(ctx, key, opts)
25+
}
26+
27+
func (k KeysAPILogger) Set(ctx context.Context, key, value string, opts *etcd.SetOptions) (*etcd.Response, error) {
28+
k.log.Debug("Set: key: %v value: %v opts: %v", key, value, opts)
29+
return k.kapi.Set(ctx, key, value, opts)
30+
}
31+
32+
func (k KeysAPILogger) Delete(ctx context.Context, key string, opts *etcd.DeleteOptions) (*etcd.Response, error) {
33+
k.log.Debug("Delete: key: %v opts: %v", key, opts)
34+
return k.kapi.Delete(ctx, key, opts)
35+
}
36+
37+
func (k KeysAPILogger) Create(ctx context.Context, key, value string) (*etcd.Response, error) {
38+
k.log.Debug("Create: key: %v value: %v", key, value)
39+
return k.kapi.Create(ctx, key, value)
40+
}
41+
42+
func (k KeysAPILogger) CreateInOrder(ctx context.Context, dir, value string, opts *etcd.CreateInOrderOptions) (*etcd.Response, error) {
43+
k.log.Debug("CreateInOrder: dir: %v value: %v opts: %v", dir, value, opts)
44+
return k.kapi.CreateInOrder(ctx, dir, value, opts)
45+
}
46+
47+
func (k KeysAPILogger) Update(ctx context.Context, key, value string) (*etcd.Response, error) {
48+
k.log.Debug("Update: key: %v value: %v", key, value)
49+
return k.kapi.Update(ctx, key, value)
50+
}
51+
52+
func (k KeysAPILogger) Watcher(key string, opts *etcd.WatcherOptions) etcd.Watcher {
53+
k.log.Debug("Watcher: key: %v opts: %v", key, opts)
54+
return k.kapi.Watcher(key, opts)
55+
}

0 commit comments

Comments
 (0)