-
Notifications
You must be signed in to change notification settings - Fork 394
/
store.go
201 lines (168 loc) · 4.91 KB
/
store.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
// Copyright (C) 2024 Storj Labs, Inc.
// See LICENSE for copying information.
package retain
import (
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/zeebo/errs"
"storj.io/common/bloomfilter"
"storj.io/common/storj"
"storj.io/storj/storagenode/blobstore/filestore"
)
// RequestStore is a cache of requests to retain pieces.
type RequestStore struct {
path string
data map[storj.NodeID]Request
}
// NewRequestStore loads the request caches from disk.
func NewRequestStore(path string) (RequestStore, error) {
store := RequestStore{
path: path,
data: make(map[storj.NodeID]Request),
}
err := os.MkdirAll(path, 0777)
if err != nil {
return store, errs.New("unable to create retain store directory: %w", err)
}
files, err := os.ReadDir(path)
if err != nil {
return store, Error.Wrap(err)
}
var errsEncountered errs.Group
for _, file := range files {
if file.IsDir() {
continue
}
filename := file.Name()
idEncoded, unixTimeStr, found := strings.Cut(filename, "-")
if !found {
// skip files that don't match the expected format
errsEncountered.Add(errs.New("invalid filename: %s", filename))
continue
}
id, err := filestore.PathEncoding.DecodeString(idEncoded)
if err != nil {
errsEncountered.Add(errs.New("invalid filename: %s; %w", filename, err))
continue
}
satelliteID, err := storj.NodeIDFromBytes(id)
if err != nil {
errsEncountered.Add(errs.New("invalid filename: %s; %w", filename, err))
continue
}
unixTime, err := strconv.ParseInt(unixTimeStr, 10, 64)
if err != nil {
errsEncountered.Add(errs.New("invalid filename: %s; %w", filename, err))
continue
}
createdBefore := time.Unix(0, unixTime)
if createdBefore.IsZero() || createdBefore.Equal(time.Unix(0, 0)) {
errsEncountered.Add(errs.New("invalid filename: %s; failed time validation", filename))
continue
}
req := Request{
SatelliteID: satelliteID,
CreatedBefore: time.Unix(0, unixTime),
}
data, err := os.ReadFile(filepath.Join(path, req.Filename()))
if err != nil {
errsEncountered.Add(err)
continue
}
req.Filter, err = bloomfilter.NewFromBytes(data)
if err != nil {
errsEncountered.Add(errs.New("malformed bloom filter: %w", err))
err := DeleteFile(store.path, file.Name())
if err != nil {
errsEncountered.Add(errs.New("failed to delete malformed bloom filter from store: %w", err))
}
continue
}
// check if there is already a request for this satellite
prevReq, ok := store.data[satelliteID]
if ok {
if prevReq.CreatedBefore.After(req.CreatedBefore) {
// if the new request is older, keep the old one
continue
}
// if the new request is newer, remove the old one
err := DeleteFile(store.path, prevReq.Filename())
if err != nil {
errsEncountered.Add(errs.New("failed to delete old bloomfilter file: %w", err))
}
}
store.data[satelliteID] = req
}
return store, errsEncountered.Err()
}
// Data returns the data in the store.
func (store *RequestStore) Data() map[storj.NodeID]Request {
return store.data
}
// Add adds a request to the store. It returns true if the request was added, and an
// error if the file could not be saved.
func (store *RequestStore) Add(req Request) (bool, error) {
// if there is already a request for this satellite, remove it
if prevReq, ok := store.data[req.SatelliteID]; ok {
err := DeleteFile(store.path, prevReq.Filename())
if err != nil {
return false, err
}
}
store.data[req.SatelliteID] = req
// save the new request
err := SaveRequest(store.path, req)
if err != nil {
return true, err
}
return true, nil
}
// Remove removes a request from the queue. It returns true if the request was found
// in the queue.
// It does not remove the cache file from the filesystem.
func (store *RequestStore) Remove(req Request) bool {
foundReq, ok := store.data[req.SatelliteID]
if !ok {
return false
}
if foundReq.CreatedBefore != req.CreatedBefore {
return false
}
delete(store.data, req.SatelliteID)
return true
}
// DeleteCache removes the request from the store and deletes the cache file.
func (store *RequestStore) DeleteCache(req Request) error {
_ = store.Remove(req)
return DeleteFile(store.path, req.Filename())
}
// Next returns the next request from the store.
func (store *RequestStore) Next() (Request, bool) {
for _, req := range store.data {
return req, true
}
return Request{}, false
}
// Len returns the number of requests in the store.
func (store *RequestStore) Len() int {
return len(store.data)
}
// DeleteFile removes a file from the filesystem.
func DeleteFile(path, filename string) error {
err := os.Remove(filepath.Join(path, filename))
if err != nil && !os.IsNotExist(err) {
return err
}
return nil
}
// SaveRequest stores the request to the filesystem.
func SaveRequest(path string, req Request) error {
err := os.WriteFile(filepath.Join(path, req.Filename()), req.Filter.Bytes(), 0644)
if err != nil {
return err
}
return nil
}