This repository was archived by the owner on Apr 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.go
220 lines (188 loc) · 6.63 KB
/
database.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
// Copyright (c) 2016, Cedric Staub <[email protected]>
//
// 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 main
import (
"crypto/sha256"
"database/sql"
"fmt"
"os"
"time"
"github.com/go-errors/errors"
_ "github.com/go-sql-driver/mysql"
)
type lock interface {
unlock() error
refresh() error
}
type database interface {
// Lock operations
lockPath(node, path string, lifetime time.Duration) (lock, error)
isLocked(path string) (bool, error)
// Results storage
storeResults(path, etag, results string, missing bool) error
fetchResults(path string) (time.Time, string, string, bool, error)
updateTimestamp(path string) error
}
type sqlDatabase struct {
*sql.DB
}
type sqlLock struct {
db *sqlDatabase
lifetime time.Duration
node string
path string
hash []byte
}
func newSQLDatabase() (database, error) {
url := os.Getenv("DATABASE_URL")
if url == "" {
return nil, errors.New("missing DATABASE_URL environment variable")
}
db, err := sql.Open("mysql", url)
if err != nil {
return nil, errors.WrapPrefix(err, "unable to talk to DB", 0)
}
return &sqlDatabase{db}, nil
}
func (db *sqlDatabase) lockPath(node, path string, lifetime time.Duration) (lock, error) {
tx, err := db.Begin()
if err != nil {
return nil, errors.New(err)
}
logger.Printf("node %s requesting lock %s for %s", node, path, lifetime.String())
hash := sha256.Sum256([]byte(path))
row := tx.QueryRow("SELECT holder, timestamp, lifetime FROM locks WHERE hash = ?", hash[:])
var lockHolder string
var lockTimestamp int64
var lockLifetime int64
err = row.Scan(&lockHolder, &lockTimestamp, &lockLifetime)
if err != nil && err != sql.ErrNoRows {
return nil, errors.WrapPrefix(err, "error talking to database", 0)
}
if err != nil {
// Insert lock
_, err := tx.Exec(
"INSERT INTO locks (hash, description, holder, timestamp, lifetime) VALUES (?, ?, ?, ?, ?)",
hash[:], path, node, time.Now().Unix(), lifetime/time.Second)
if err != nil {
logError(fmt.Sprintf("node %s error on rollback", node), tx.Rollback())
logger.Printf("node %s unable to acquire lock %s (DB error)", node, path)
return nil, errors.WrapPrefix(err, "unable to acquire lock (insert failed)", 0)
}
} else {
expiry := time.Unix(lockTimestamp, 0).Add(time.Duration(lockLifetime) * time.Second)
if lockHolder == node || time.Now().After(expiry) {
_, err := tx.Exec(
"UPDATE locks SET holder = ?, timestamp = ?, lifetime = ? WHERE hash = ?",
node, time.Now().Unix(), lifetime/time.Second, hash[:])
if err != nil {
logError(fmt.Sprintf("node %s error on rollback", node), tx.Rollback())
logger.Printf("node %s unable to acquire lock %s (DB error)", node, path)
return nil, errors.WrapPrefix(err, "unable to acquire lock (update failed)", 0)
}
} else {
logger.Printf("node %s unable to acquire lock %s (already locked)", node, path)
return nil, nil
}
}
err = tx.Commit()
if err != nil {
logger.Printf("node %s unable to acquire lock %s (commit failed)", node, path)
return nil, nil
}
logger.Printf("node %s acquired lock %s for %s", node, path, lifetime.String())
return &sqlLock{db, lifetime, node, path, hash[:]}, nil
}
func (db *sqlDatabase) isLocked(path string) (bool, error) {
hash := sha256.Sum256([]byte(path))
row := db.QueryRow("SELECT timestamp, lifetime FROM locks WHERE hash = ?", hash[:])
var lockTimestamp int64
var lockLifetime int64
err := row.Scan(&lockTimestamp, &lockLifetime)
if err != nil && err != sql.ErrNoRows {
return false, errors.WrapPrefix(err, "unable to check lock", 0)
}
if err == sql.ErrNoRows {
return false, nil
}
expiry := time.Unix(lockTimestamp, 0).Add(time.Duration(lockLifetime) * time.Second)
if time.Now().After(expiry) {
return false, nil
}
return true, nil
}
func (sl *sqlLock) refresh() error {
logger.Printf("node %s refreshing lock %s for %s", sl.node, sl.path, sl.lifetime.String())
r, err := sl.db.Exec(
"UPDATE locks SET timestamp = ?, lifetime = ? WHERE hash = ?, holder = ?",
time.Now().Unix(), sl.lifetime/time.Second, sl.hash, sl.node)
if err != nil {
logger.Printf("node %s unable to refresh lock %s (DB error)", sl.node, sl.path)
return errors.WrapPrefix(err, "unable to refresh lock (update failed)", 0)
}
n, err := r.RowsAffected()
if err != nil {
logger.Printf("node %s unable to refresh lock %s (result error)", sl.node, sl.path)
return errors.WrapPrefix(err, "unable to refresh (update failed)", 0)
}
if n != 1 {
logger.Printf("node %s unable to refresh lock %s (lost lock)", sl.node, sl.path)
return errors.New("unable to refresh lock: lost lock?")
}
return nil
}
func (sl *sqlLock) unlock() error {
logger.Printf("node %s dropping lock %s", sl.node, sl.path)
_, err := sl.db.Exec("DELETE FROM locks WHERE hash = ? AND holder = ?", sl.hash, sl.node)
if err != nil {
return errors.WrapPrefix(err, "unable to drop lock", 0)
}
return nil
}
func (db *sqlDatabase) storeResults(path, etag, results string, missing bool) error {
hash := sha256.Sum256([]byte(path))
_, err := db.Exec(
`INSERT INTO results (hash, timestamp, etag, results, missing) VALUES (?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE timestamp = ?, etag = ?, results = ?, missing = ?`,
hash[:], time.Now().Unix(), etag, results, missing, time.Now().Unix(), etag, results, missing)
if err != nil {
return errors.New(err)
}
return nil
}
func (db *sqlDatabase) fetchResults(path string) (time.Time, string, string, bool, error) {
hash := sha256.Sum256([]byte(path))
r := db.QueryRow("SELECT timestamp, etag, results, missing FROM results WHERE hash = ?", hash[:])
var timestamp int64
var results string
var etag sql.NullString
var missing sql.NullBool
err := r.Scan(×tamp, &etag, &results, &missing)
if err == sql.ErrNoRows {
return time.Now(), "", "", false, err
} else if err != nil {
return time.Now(), "", "", false, errors.New(err)
}
return time.Unix(timestamp, 0), etag.String, results, missing.Valid && missing.Bool, nil
}
func (db *sqlDatabase) updateTimestamp(path string) error {
hash := sha256.Sum256([]byte(path))
_, err := db.Exec(
`UPDATE results SET timestamp = ? WHERE hash = ?`,
time.Now().Unix(), hash[:])
if err != nil {
return errors.New(err)
}
return nil
}