Debian Codegraph @

database/v1/version1.go

raw
1 // Copyright (c) 2014-2019 Ludovic Fauvet
2 // Licensed under the MIT license
3
4 package v1
5
6 import (
7 "fmt"
8
9 "github.com/etix/mirrorbits/core"
10 "github.com/etix/mirrorbits/database/interfaces"
11 "github.com/gomodule/redigo/redis"
12 )
13
14 // NewUpgraderV1 upgrades the database from version 0 to 1
15 func NewUpgraderV1(redis interfaces.Redis) *Version1 {
16 return &Version1{
17 Redis: redis,
18 }
19 }
20
21 type Version1 struct {
22 Redis interfaces.Redis
23 }
24
25 type actions struct {
26 delete []string
27 rename map[string]string
28 }
29
30 func (v *Version1) Upgrade() error {
31 a := &actions{
32 rename: make(map[string]string),
33 }
34
35 conn := v.Redis.UnblockedGet()
36 defer conn.Close()
37
38 // Erase previous work keys (previous failed upgrade?)
39 _, err := conn.Do("EVAL", `
40 local keys = redis.call('keys', ARGV[1])
41 for i=1,#keys,5000 do
42 redis.call('del', unpack(keys, i, math.min(i+4999, #keys)))
43 end
44 return keys`, 0, "V1_*")
45
46 if err != nil {
47 return err
48 }
49
50 m, err := v.CreateMirrorIndex(a)
51 if err != nil {
52 return err
53 }
54
55 err = v.RenameKeys(a, m)
56 if err != nil {
57 return err
58 }
59
60 err = v.FixMirrorID(a, m)
61 if err != nil {
62 return err
63 }
64
65 err = v.RenameStats(a, m)
66 if err != nil {
67 return err
68 }
69
70 // Start a transaction to atomically and irrevocably set the new version
71 conn.Send("MULTI")
72
73 for k, v := range a.rename {
74 conn.Send("RENAME", k, v)
75 }
76
77 for _, d := range a.delete {
78 do := true
79 for _, v := range a.rename {
80 if d == v {
81 // Abort the operation since this would
82 // delete the result of a rename
83 do = false
84 break
85 }
86 }
87 if do {
88 conn.Send("DEL", d)
89 }
90 }
91
92 conn.Send("SET", core.DBVersionKey, 1)
93
94 // Finalize the transaction
95 _, err = conn.Do("EXEC")
96
97 // <-- At this point, if any of the previous mutation failed, it is still
98 // safe to run a previous version of mirrorbits.
99
100 return err
101 }
102
103 func (v *Version1) CreateMirrorIndex(a *actions) (map[int]string, error) {
104 m := make(map[int]string)
105
106 conn := v.Redis.UnblockedGet()
107 defer conn.Close()
108
109 // Get the v0 list of mirrors
110 mirrors, err := redis.Strings(conn.Do("LRANGE", "MIRRORS", "0", "-1"))
111 if err != nil {
112 return m, err
113 }
114
115 for _, name := range mirrors {
116 // Create a unique ID for the current mirror
117 id, err := redis.Int(conn.Do("INCR", "LAST_MID"))
118 if err != nil {
119 return m, err
120 }
121
122 // Assign the ID to the current mirror
123 if _, err = conn.Do("HSET", "V1_MIRRORS", id, name); err != nil {
124 return m, err
125 }
126
127 m[id] = name
128 }
129
130 // Prepare for renaming
131 a.rename["V1_MIRRORS"] = "MIRRORS"
132
133 return m, nil
134 }
135
136 func (v *Version1) RenameKeys(a *actions, m map[int]string) error {
137 conn := v.Redis.UnblockedGet()
138 defer conn.Close()
139
140 // Rename all keys to contain the ID instead of the name
141 for id, name := range m {
142 // Get the list of files known to this mirror
143 files, err := redis.Strings(conn.Do("SMEMBERS", fmt.Sprintf("MIRROR_%s_FILES", name)))
144 if err == redis.ErrNil || IsErrNoSuchKey(err) {
145 continue
146 } else if err != nil {
147 return err
148 }
149
150 // Rename the FILEINFO_<name>_<file> keys
151 for _, file := range files {
152 a.rename[fmt.Sprintf("FILEINFO_%s_%s", name, file)] = fmt.Sprintf("FILEINFO_%d_%s", id, file)
153 }
154
155 // Rename the remaing global keys
156 a.rename[fmt.Sprintf("MIRROR_%s_FILES", name)] = fmt.Sprintf("MIRRORFILES_%d", id)
157 a.rename[fmt.Sprintf("HANDLEDFILES_%s", name)] = fmt.Sprintf("HANDLEDFILES_%d", id)
158 // MIRROR_%s -> MIRROR_%d is handled by FixMirrorID
159 }
160
161 // Get the list of files in the local repo
162 files, err := redis.Strings(conn.Do("SMEMBERS", "FILES"))
163 if err != nil && err != redis.ErrNil {
164 return err
165 }
166
167 // Rename the keys within FILEMIRRORS_*
168 for _, file := range files {
169 // Get the list of mirrors having each file
170 names, err := redis.Strings(conn.Do("SMEMBERS", fmt.Sprintf("FILEMIRRORS_%s", file)))
171 if err != nil {
172 return err
173 }
174
175 for _, name := range names {
176 var id int
177 for mid, mname := range m {
178 if mname == name {
179 id = mid
180 break
181 }
182 }
183
184 if id == 0 {
185 continue
186 }
187 conn.Send("SADD", fmt.Sprintf("V1_FILEMIRRORS_%s", file), id)
188 }
189 if err := conn.Flush(); err != nil {
190 return err
191 }
192
193 // Mark the key for renaming
194 a.rename[fmt.Sprintf("V1_FILEMIRRORS_%s", file)] = fmt.Sprintf("FILEMIRRORS_%s", file)
195 }
196
197 return nil
198 }
199
200 func (v *Version1) FixMirrorID(a *actions, m map[int]string) error {
201 conn := v.Redis.UnblockedGet()
202 defer conn.Close()
203
204 // Replace ID by the new mirror id
205 // Add a field 'name' containing the mirror name
206 for id, name := range m {
207 err := CopyKey(conn, fmt.Sprintf("MIRROR_%s", name), fmt.Sprintf("V1_MIRROR_%d", id))
208 if err != nil {
209 return err
210 }
211 conn.Send("HSET", fmt.Sprintf("V1_MIRROR_%d", id), "ID", id, "name", name)
212 a.rename[fmt.Sprintf("V1_MIRROR_%d", id)] = fmt.Sprintf("MIRROR_%d", id)
213 a.delete = append(a.delete, fmt.Sprintf("MIRROR_%s", name))
214 }
215 if err := conn.Flush(); err != nil {
216 return err
217 }
218
219 return nil
220 }
221
222 func (v *Version1) RenameStats(a *actions, m map[int]string) error {
223 conn := v.Redis.UnblockedGet()
224 defer conn.Close()
225
226 keys, err := redis.Strings(conn.Do("KEYS", "STATS_MIRROR_*"))
227 if err != nil && err != redis.ErrNil {
228 return err
229 }
230
231 for _, key := range keys {
232 // Here we get two formats:
233 // - STATS_MIRROR_*
234 // - STATS_MIRROR_BYTES_*
235 // and each of them with three differents dates (year, year+month, year+month+day)
236
237 stats, err := redis.StringMap(conn.Do("HGETALL", key))
238 if err != nil {
239 return err
240 }
241
242 for identifier, value := range stats {
243 var id int
244 for mid, mname := range m {
245 if mname == identifier {
246 id = mid
247 break
248 }
249 }
250 if id == 0 {
251 // Mirror does not exist anymore
252 // This is expected if mirrors were removed over time
253 continue
254 }
255
256 conn.Send("HSET", "V1_"+key, id, value)
257 a.rename["V1_"+key] = key
258 }
259 if err := conn.Flush(); err != nil {
260 return err
261 }
262 }
263
264 return nil
265 }
266
267 func CopyKey(conn redis.Conn, src, dst string) error {
268 dmp, err := redis.String(conn.Do("DUMP", src))
269 if err != nil {
270 return err
271 }
272 _, err = conn.Do("RESTORE", dst, 0, dmp, "REPLACE")
273 return err
274 }
275
276 // IsErrNoSuchKey return true if error is of type "no such key"
277 func IsErrNoSuchKey(err error) bool {
278 // PARSING: "ERR no such key"
279 if err != nil && err.Error() == "ERR no such key" {
280 return true
281 }
282 return false
283 }