-
Notifications
You must be signed in to change notification settings - Fork 394
/
pieceexpiration.go
136 lines (111 loc) · 3.12 KB
/
pieceexpiration.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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package storagenodedb
import (
"context"
"strings"
"sync"
"time"
"github.com/zeebo/errs"
"storj.io/common/storj"
"storj.io/storj/storagenode/pieces"
)
// ErrPieceExpiration represents errors from the piece expiration database.
var ErrPieceExpiration = errs.Class("pieceexpirationdb")
// PieceExpirationDBName represents the database filename.
const PieceExpirationDBName = "piece_expiration"
type pieceExpirationDB struct {
dbContainerImpl
mu sync.Mutex
buf map[pieces.ExpiredInfo]time.Time
}
var monGetExpired = mon.Task()
// GetExpired gets piece IDs that expire or have expired before the given time.
func (db *pieceExpirationDB) GetExpired(ctx context.Context, now time.Time, cb func(context.Context, pieces.ExpiredInfo) bool) (err error) {
defer monGetExpired(&ctx)(&err)
db.mu.Lock()
buf := map[pieces.ExpiredInfo]struct{}{}
for ei, exp := range db.buf {
if exp.Before(now) {
buf[ei] = struct{}{}
}
}
rows, err := db.QueryContext(ctx, `
SELECT satellite_id, piece_id
FROM piece_expirations
WHERE piece_expiration < ?
`, now.UTC())
db.mu.Unlock()
if err != nil {
return ErrPieceExpiration.Wrap(err)
}
defer func() { err = errs.Combine(err, rows.Err(), rows.Close()) }()
for ei := range buf {
if !cb(ctx, ei) {
return nil
}
}
for rows.Next() {
var ei pieces.ExpiredInfo
err = rows.Scan(&ei.SatelliteID, &ei.PieceID)
if err != nil {
return ErrPieceExpiration.Wrap(err)
}
if _, ok := buf[ei]; ok {
continue
}
if !cb(ctx, ei) {
return nil
}
}
return nil
}
var monSetExpiration = mon.Task()
// SetExpiration sets an expiration time for the given piece ID on the given satellite.
func (db *pieceExpirationDB) SetExpiration(ctx context.Context, satellite storj.NodeID, pieceID storj.PieceID, expiresAt time.Time) (err error) {
defer monSetExpiration(&ctx)(&err)
ei := pieces.ExpiredInfo{
SatelliteID: satellite,
PieceID: pieceID,
}
db.mu.Lock()
if db.buf == nil {
db.buf = make(map[pieces.ExpiredInfo]time.Time, 1000)
}
db.buf[ei] = expiresAt.UTC()
if len(db.buf) < 1000 {
db.mu.Unlock()
return nil
}
var args []any
for ei, expiresAt := range db.buf {
args = append(args, ei.SatelliteID, ei.PieceID, expiresAt)
}
// done in separate loop so runtime optimizes to map clear call
for ei := range db.buf {
delete(db.buf, ei)
}
db.mu.Unlock()
values := strings.TrimRight(strings.Repeat("(?,?,?),", len(args)/3), ",")
_, err = db.ExecContext(ctx, `
INSERT INTO piece_expirations (satellite_id, piece_id, piece_expiration) VALUES `+values,
args...,
)
return ErrPieceExpiration.Wrap(err)
}
// DeleteExpiration removes an expiration record for the given piece ID on the given satellite.
func (db *pieceExpirationDB) DeleteExpirations(ctx context.Context, now time.Time) (err error) {
defer mon.Task()(&ctx)(&err)
db.mu.Lock()
defer db.mu.Unlock()
for ei, exp := range db.buf {
if exp.Before(now) {
delete(db.buf, ei)
}
}
_, err = db.ExecContext(ctx, `
DELETE FROM piece_expirations
WHERE piece_expiration < ?
`, now.UTC())
return ErrPieceExpiration.Wrap(err)
}