This repository has been archived by the owner on Mar 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
db.go
335 lines (283 loc) · 8.74 KB
/
db.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"sort"
"sync"
"time"
"github.com/DataDog/datadog-go/statsd"
)
var errNoVersions = errors.New("no versions available")
type db struct {
sequins *sequins
name string
mux *versionMux
refreshLock sync.Mutex
buildLock sync.Mutex
upgradeLock sync.Mutex
cleanupLock sync.Mutex
}
func newDB(sequins *sequins, name string) *db {
db := &db{
sequins: sequins,
name: name,
mux: newVersionMux(sequins.config.Test.VersionRemoveTimeout.Duration),
}
return db
}
func (db *db) listVersions(after string) ([]string, error) {
versions, err := db.sequins.backend.ListVersions(db.name, after, db.sequins.config.RequireSuccessFile)
if err != nil {
return nil, err
}
return filterPaths(versions), nil
}
func (db *db) localVersions() ([]string, error) {
files, err := ioutil.ReadDir(db.localPathDir())
if err != nil {
return nil, err
}
var versions []string
for _, f := range files {
versions = append(versions, f.Name())
}
sort.Strings(versions)
return versions, nil
}
// backfillVersions is called at startup, and tries to grab any versions that
// are either downloaded locally or available entirely at peers. This allows a
// node to join a cluster with an existing version all set to go, and start up
// serving that version (but exclusively proxy it). It also allows it to start
// up with stale data, even if there's newer data available.
func (db *db) backfillVersions(initialLocal bool) error {
db.refreshLock.Lock()
defer db.refreshLock.Unlock()
var versions []string
var err error
if initialLocal {
log.Printf("Initial fetch of local versions only: db=%q", db.name)
versions, err = db.localVersions()
} else {
versions, err = db.listVersions("")
}
if err != nil {
return err
} else if len(versions) == 0 {
return nil
}
// Only look at the last 3 versions, to keep this next part quick.
if len(versions) > 3 {
versions = versions[len(versions)-3:]
}
// Iterate through all the versions we know about, and track the remote and
// local partitions for it. We don't download anything we don't have, but if
// one is ready - because we have all the partitions locally, or because our
// peers already do - we can switch to it immediately. Even if none are
// available immediately, we can still start watching out for peers on old
// versions for which we have data locally, in case they start to appear (as
// would happen if a bunch of nodes with stale data started up together). It's
// important to do this now, synchronously before startup, so that the node
// can come up with the version ready to go, and avoid an awkward period of
// 404ing while we load versions asynchronously.
for i := len(versions) - 1; i >= 0; i-- {
v := versions[i]
version, err := newVersion(db.sequins, db, db.localPath(v), v)
if err != nil {
log.Printf("Error initializing version %s of %s: %s", db.name, v, err)
continue
}
if db.switchVersion(version) {
break
}
}
go db.cleanupStore()
return nil
}
// refresh finds the latest version in S3 and then triggers an upgrade.
func (db *db) refresh() error {
db.refreshLock.Lock()
defer db.refreshLock.Unlock()
after := ""
currentVersion := db.mux.getCurrent()
db.mux.release(currentVersion)
if currentVersion != nil {
after = currentVersion.name
}
versions, err := db.listVersions(after)
if err != nil {
return err
} else if len(versions) == 0 {
if after == "" {
return errNoVersions
} else {
return nil
}
}
latest := versions[len(versions)-1]
// Check if we already have this version in the pipeline.
existingVersion := db.mux.getVersion(latest)
db.mux.release(existingVersion)
if existingVersion != nil {
// If the build succeeded or is in progress, this is a noop. If it errored
// before, this will retry.
go existingVersion.build()
return nil
}
vs, err := newVersion(db.sequins, db, db.localPath(latest), latest)
if err != nil {
return err
}
db.switchVersion(vs)
return nil
}
// switchVersion goes through the upgrade process, making sure that we switch
// versions in step with our peers. It returns true if the version is ready,
// and false otherwise.
func (db *db) switchVersion(version *version) bool {
// Prepare the version, so that during the switching period we can respond
// to requests for it.
db.mux.prepare(version)
// Build any partitions we're missing in the background.
go version.build()
// Start advertising our partitions to peers.
go version.partitions.Advertise()
// If the version is ready now, we can switch to it synchronously. This is
// important to do so that on startup, we fully initialize ready versions
// before we start taking requests. For example, if our peers have a complete
// set of partitions, then we want to start up being able to proxy to them.
select {
case <-version.ready:
db.upgrade(version)
return true
default:
}
// Wait for a complete set of partitions to be available (in the non-
// distributed case, this just means waiting for building to finish). All
// peers should all see that everything is ready at roughly the same time. If
// they switch before us, that's fine; the new version has been 'prepared' and
// we can serve it to peers (but not clients). If they switch after us, that's
// also fine, since we'll keep the old version around for a bit before
// deleting it.
go func() {
<-version.ready
db.upgrade(version)
}()
return false
}
// upgrade takes a new version and processes it, upgrading if necessary and then
// clearing old ones. If it gets a version that is older than the current one,
// it ignores it, ensuring that it always rolls forward.
func (db *db) upgrade(version *version) {
db.upgradeLock.Lock()
defer db.upgradeLock.Unlock()
// This is just to make functional tests easier to write.
delay := db.sequins.config.Test.UpgradeDelay.Duration
if delay != 0 {
time.Sleep(delay)
}
// Make sure we always roll forward.
current := db.mux.getCurrent()
db.mux.release(current)
if current != nil && version.name < current.name {
// The version is already out of date, so get rid of it.
go db.removeVersion(version, false)
return
} else if version == current {
return
}
log.Printf("Switching to version %s of %s!", version.name, db.name)
db.mux.upgrade(version)
version.setState(versionActive)
if version.stats != nil {
title := fmt.Sprintf("A new version is active in %s.", db.name)
text := fmt.Sprintf("Version %s has been set to active in db %s.", version.name, db.name)
event := statsd.NewEvent(title, text)
event.Tags = []string{fmt.Sprintf("sequins_db:%s", db.name)}
version.stats.Event(event)
}
// Close the current version, and any older versions that were
// also being prepared (effectively preempting them).
for _, old := range db.mux.getAll() {
if old == current {
go db.removeVersion(old, true)
} else if old.name < version.name {
go db.removeVersion(old, false)
}
}
}
// removeVersion removes a version, blocking until it is no longer being
// requested by peers.
func (db *db) removeVersion(old *version, shouldWait bool) {
db.cleanupLock.Lock()
defer db.cleanupLock.Unlock()
old.setState(versionRemoving)
// If we don't have any peers, we never need to wait until the versions
// aren't being used.
if db.sequins.peers == nil {
shouldWait = false
}
// This will block until the version is no longer being used.
if removed := db.mux.remove(old, shouldWait); removed != nil {
removed.close()
err := removed.delete()
if err != nil {
log.Printf("Error cleaning up version %s of %s: %s", removed.name, db.name, err)
}
}
}
func (db *db) cleanupStore() {
db.cleanupLock.Lock()
defer db.cleanupLock.Unlock()
dirs, err := ioutil.ReadDir(db.localPath(""))
if os.IsNotExist(err) {
return
} else if err != nil {
log.Println("Error listing local dir:", err)
return
}
for _, info := range dirs {
if !info.IsDir() {
continue
}
v := info.Name()
version := db.mux.getVersion(v)
db.mux.release(version)
if version != nil {
continue
}
log.Println("Clearing defunct version", v, "of", db.name)
os.RemoveAll(db.localPath(v))
}
}
// localPath returns the path where local data for the given version should be
// stored.
func (db *db) localPath(version string) string {
return filepath.Join(db.localPathDir(), version)
}
func (db *db) localPathDir() string {
return filepath.Join(db.sequins.config.LocalStore, "data", db.name)
}
func (db *db) serveKey(w http.ResponseWriter, r *http.Request, key string) {
if key == "" {
db.serveStatus(w, r)
return
}
db.mux.serveKey(w, r, key)
}
func (db *db) close() {
db.refreshLock.Lock()
defer db.refreshLock.Unlock()
for _, vs := range db.mux.getAll() {
vs.close()
}
}
func (db *db) delete() {
for _, vs := range db.mux.getAll() {
vs.delete()
}
}