forked from flynn/flynn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_store.go
220 lines (197 loc) · 5.1 KB
/
data_store.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
"encoding/json"
"errors"
"log"
"path"
"time"
"github.com/flynn/flynn/Godeps/_workspace/src/github.com/coreos/go-etcd/etcd"
"github.com/flynn/flynn/router/types"
)
type EtcdClient interface {
Create(key string, value string, ttl uint64) (*etcd.Response, error)
Set(key string, value string, ttl uint64) (*etcd.Response, error)
Get(key string, sort, recursive bool) (*etcd.Response, error)
Delete(key string, recursive bool) (*etcd.Response, error)
Watch(prefix string, waitIndex uint64, recursive bool, receiver chan *etcd.Response, stop chan bool) (*etcd.Response, error)
}
type DataStore interface {
Add(route *router.Route) error
Set(route *router.Route) error
Get(id string) (*router.Route, error)
List() ([]*router.Route, error)
Remove(id string) error
Sync(h SyncHandler, started chan<- error)
StopSync()
}
type DataStoreReader interface {
Get(id string) (*router.Route, error)
List() ([]*router.Route, error)
}
type SyncHandler interface {
Set(route *router.Route) error
Remove(id string) error
}
func NewEtcdDataStore(etcd EtcdClient, prefix string) DataStore {
return &etcdDataStore{
prefix: prefix,
etcd: etcd,
stopSync: make(chan bool),
}
}
type etcdDataStore struct {
prefix string
etcd EtcdClient
stopSync chan bool
}
var ErrExists = errors.New("router: route already exists")
var ErrNotFound = errors.New("router: route not found")
func (s *etcdDataStore) Add(r *router.Route) error {
data, err := json.Marshal(r)
if err != nil {
return err
}
_, err = s.etcd.Create(s.path(r.ID), string(data), 0)
if e, ok := err.(*etcd.EtcdError); ok && e.ErrorCode == 105 {
err = ErrExists
}
return err
}
func (s *etcdDataStore) Set(r *router.Route) error {
data, err := json.Marshal(r)
if err != nil {
return err
}
_, err = s.etcd.Set(s.path(r.ID), string(data), 0)
return err
}
func (s *etcdDataStore) Remove(id string) error {
_, err := s.etcd.Delete(s.path(id), true)
if e, ok := err.(*etcd.EtcdError); ok && e.ErrorCode == 100 {
return ErrNotFound
}
return err
}
func (s *etcdDataStore) Get(id string) (*router.Route, error) {
res, err := s.etcd.Get(s.path(id), false, false)
if err != nil {
if e, ok := err.(*etcd.EtcdError); ok && e.ErrorCode == 100 {
err = ErrNotFound
}
return nil, err
}
r := &router.Route{}
err = json.Unmarshal([]byte(res.Node.Value), r)
return r, err
}
func (s *etcdDataStore) List() ([]*router.Route, error) {
res, err := s.etcd.Get(s.prefix, false, true)
if err != nil {
if e, ok := err.(*etcd.EtcdError); ok && e.ErrorCode == 100 {
err = nil
}
return nil, err
}
routes := make([]*router.Route, len(res.Node.Nodes))
for i, node := range res.Node.Nodes {
r := &router.Route{}
if err := json.Unmarshal([]byte(node.Value), r); err != nil {
return nil, err
}
routes[i] = r
}
return routes, nil
}
func (s *etcdDataStore) path(id string) string {
return s.prefix + path.Base(id)
}
func (s *etcdDataStore) Sync(h SyncHandler, started chan<- error) {
keys := make(map[string]uint64)
nextIndex := uint64(1)
fullSync:
data, err := s.etcd.Get(s.prefix, false, true)
if e, ok := err.(*etcd.EtcdError); ok && e.ErrorCode == 100 {
// key not found, delete existing keys and then watch
for id := range keys {
delete(keys, id)
if err := h.Remove(id); err != nil {
log.Printf("Error while processing delete from etcd fullsync: %s, %s", id, err)
}
}
goto watch
}
syncErr:
if err != nil {
if started != nil {
started <- err
return
}
log.Printf("Error while doing fullsync from etcd: %s", err)
time.Sleep(time.Second)
goto fullSync
}
nextIndex = data.EtcdIndex + 1
for _, node := range data.Node.Nodes {
key := path.Base(node.Key)
if modified, ok := keys[key]; ok && modified >= node.ModifiedIndex {
continue
}
keys[key] = node.ModifiedIndex
route := &router.Route{}
if err = json.Unmarshal([]byte(node.Value), route); err != nil {
goto syncErr
}
if err = h.Set(route); err != nil {
goto syncErr
}
}
watch:
if started != nil {
started <- nil
started = nil
}
for {
stream := make(chan *etcd.Response)
watchDone := make(chan struct{})
var watchErr error
go func() {
_, watchErr = s.etcd.Watch(s.prefix, nextIndex, true, stream, s.stopSync)
close(watchDone)
}()
for res := range stream {
nextIndex = res.EtcdIndex + 1
id := path.Base(res.Node.Key)
var err error
if res.Action == "delete" {
delete(keys, id)
err = h.Remove(id)
} else {
keys[id] = res.Node.ModifiedIndex
route := &router.Route{}
if err = json.Unmarshal([]byte(res.Node.Value), route); err != nil {
goto fail
}
err = h.Set(route)
}
fail:
if err != nil {
log.Printf("Error while processing update from etcd: %s, %#v", err, res.Node)
}
}
<-watchDone
select {
case <-s.stopSync:
return
default:
}
if e, ok := watchErr.(*etcd.EtcdError); ok && e.ErrorCode == 401 {
// event log has been pruned beyond our waitIndex, force fullSync
goto fullSync
}
log.Printf("Restarting etcd watch %s due to error: %s", s.prefix, watchErr)
// TODO: backoff here
}
}
func (s *etcdDataStore) StopSync() {
close(s.stopSync)
}