| 1 |
// Copyright (c) 2024 Arnaud Rebillout |
| 2 |
// Licensed under the MIT license |
| 3 |
|
| 4 |
package v2 |
| 5 |
|
| 6 |
import ( |
| 7 |
"strings" |
| 8 |
|
| 9 |
"github.com/etix/mirrorbits/core" |
| 10 |
"github.com/etix/mirrorbits/database/interfaces" |
| 11 |
"github.com/gomodule/redigo/redis" |
| 12 |
) |
| 13 |
|
| 14 |
// NewUpgraderV2 upgrades the database from version 1 to 2 |
| 15 |
func NewUpgraderV2(redis interfaces.Redis) *Version2 { |
| 16 |
return &Version2{ |
| 17 |
Redis: redis, |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
type Version2 struct { |
| 22 |
Redis interfaces.Redis |
| 23 |
} |
| 24 |
|
| 25 |
type actions struct { |
| 26 |
rename map[string]string |
| 27 |
} |
| 28 |
|
| 29 |
func (v *Version2) Upgrade() error { |
| 30 |
a := &actions{ |
| 31 |
rename: make(map[string]string), |
| 32 |
} |
| 33 |
|
| 34 |
conn := v.Redis.UnblockedGet() |
| 35 |
defer conn.Close() |
| 36 |
|
| 37 |
// Erase previous work keys (previous failed upgrade?) |
| 38 |
_, err := conn.Do("EVAL", ` |
| 39 |
local keys = redis.call('keys', ARGV[1]) |
| 40 |
for i=1,#keys,5000 do |
| 41 |
redis.call('del', unpack(keys, i, math.min(i+4999, #keys))) |
| 42 |
end |
| 43 |
return keys`, 0, "V2_*") |
| 44 |
|
| 45 |
if err != nil { |
| 46 |
return err |
| 47 |
} |
| 48 |
|
| 49 |
err = v.UpdateMirrors(a) |
| 50 |
if err != nil { |
| 51 |
return err |
| 52 |
} |
| 53 |
|
| 54 |
// Start a transaction to atomically and irrevocably set the new version |
| 55 |
conn.Send("MULTI") |
| 56 |
|
| 57 |
for k, v := range a.rename { |
| 58 |
conn.Send("RENAME", k, v) |
| 59 |
} |
| 60 |
|
| 61 |
conn.Send("SET", core.DBVersionKey, 2) |
| 62 |
|
| 63 |
// Finalize the transaction |
| 64 |
_, err = conn.Do("EXEC") |
| 65 |
|
| 66 |
// <-- At this point, if any of the previous mutation failed, it is still |
| 67 |
// safe to run a previous version of mirrorbits. |
| 68 |
|
| 69 |
return err |
| 70 |
} |
| 71 |
|
| 72 |
func (v *Version2) UpdateMirrors(a *actions) error { |
| 73 |
conn := v.Redis.UnblockedGet() |
| 74 |
defer conn.Close() |
| 75 |
|
| 76 |
// Get the list of mirrors |
| 77 |
keys, err := redis.Strings(conn.Do("KEYS", "MIRROR_*")) |
| 78 |
if err != nil && err != redis.ErrNil { |
| 79 |
return err |
| 80 |
} |
| 81 |
|
| 82 |
// Iterate on mirrors |
| 83 |
for _, keyProd := range keys { |
| 84 |
// Copy the key |
| 85 |
key := "V2_" + keyProd |
| 86 |
err := CopyKey(conn, keyProd, key) |
| 87 |
if err != nil { |
| 88 |
return err |
| 89 |
} |
| 90 |
|
| 91 |
// Get the http url |
| 92 |
url, err := redis.String(conn.Do("HGET", key, "http")) |
| 93 |
if err != nil { |
| 94 |
return err |
| 95 |
} |
| 96 |
|
| 97 |
// Get the status. Note that the key might not exist if ever |
| 98 |
// the mirror was never enabled or scanned successfully. |
| 99 |
up, err := redis.Bool(conn.Do("HGET", key, "up")) |
| 100 |
if err != nil && err != redis.ErrNil { |
| 101 |
return err |
| 102 |
} |
| 103 |
upExists := true |
| 104 |
if err == redis.ErrNil { |
| 105 |
upExists = false |
| 106 |
} |
| 107 |
|
| 108 |
// Get the excluded reason. As above: the key might not exist. |
| 109 |
reason, err := redis.String(conn.Do("HGET", key, "excludeReason")) |
| 110 |
if err != nil && err != redis.ErrNil { |
| 111 |
return err |
| 112 |
} |
| 113 |
reasonExists := true |
| 114 |
if err == redis.ErrNil { |
| 115 |
reasonExists = false |
| 116 |
} |
| 117 |
|
| 118 |
// Start a transaction to do all the changes in one go |
| 119 |
conn.Send("MULTI") |
| 120 |
|
| 121 |
if strings.HasPrefix(url, "https://") { |
| 122 |
// Update up key if needed |
| 123 |
if upExists { |
| 124 |
conn.Send("HSET", key, "httpsUp", up) |
| 125 |
conn.Send("HDEL", key, "up") |
| 126 |
} |
| 127 |
// Update reason key if needed |
| 128 |
if reasonExists { |
| 129 |
conn.Send("HSET", key, "httpsDownReason", reason) |
| 130 |
conn.Send("HDEL", key, "excludeReason") |
| 131 |
} |
| 132 |
} else { |
| 133 |
// Update up key if needed |
| 134 |
if upExists { |
| 135 |
conn.Send("HSET", key, "httpUp", up) |
| 136 |
conn.Send("HDEL", key, "up") |
| 137 |
} |
| 138 |
// Update reason key if needed |
| 139 |
if reasonExists { |
| 140 |
conn.Send("HSET", key, "httpDownReason", reason) |
| 141 |
conn.Send("HDEL", key, "excludeReason") |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
// Finalize the transaction |
| 146 |
_, err = conn.Do("EXEC") |
| 147 |
if err != nil { |
| 148 |
return err |
| 149 |
} |
| 150 |
|
| 151 |
// Mark the key for renaming |
| 152 |
a.rename[key] = keyProd |
| 153 |
} |
| 154 |
|
| 155 |
return nil |
| 156 |
} |
| 157 |
|
| 158 |
func CopyKey(conn redis.Conn, src, dst string) error { |
| 159 |
// NB: Redis COPY https://redis.io/commands/copy/ is only available |
| 160 |
// since Redis 6.2, released in Feb 2021. That's a bit too recent, |
| 161 |
// so let's stick with the DUMP/RESTORE combination implemented in |
| 162 |
// this function (and copy/pasted from the v1 database upgrade). |
| 163 |
dmp, err := redis.String(conn.Do("DUMP", src)) |
| 164 |
if err != nil { |
| 165 |
return err |
| 166 |
} |
| 167 |
_, err = conn.Do("RESTORE", dst, 0, dmp, "REPLACE") |
| 168 |
return err |
| 169 |
} |
| 170 |
|