/
trackstore.go
212 lines (188 loc) · 5.59 KB
/
trackstore.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
package trackstore
import (
"encoding/json"
"fmt"
"strings"
"sync"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/query"
"github.com/textileio/powergate/ffs"
)
// Store persist information about storage configs which
// should be repaired or renewed. It always contain the
// latest Storage Config value of a Cid to execute those actions.
// This store is used by the Scheduler background jobs that
// repair or renew storage configurations.
type Store struct {
lock sync.Mutex
ds datastore.Datastore
}
// TrackedStorageConfig has information about a StorageConfig from
// an APIID which is repairable/renewable.
type TrackedStorageConfig struct {
IID ffs.APIID
StorageConfig ffs.StorageConfig
}
// TrackedCid contains tracked storage configs for a Cid.
type TrackedCid struct {
Cid cid.Cid
Tracked []TrackedStorageConfig
}
// New retruns a new Store.
func New(ds datastore.Datastore) (*Store, error) {
return &Store{ds: ds}, nil
}
// Put updates the StorageConfig tracking state for a Cid.
// If the StorageConfig is repairable or renewable, it will be
// added (or updated if exist) for a Cid. If it isn't repairable
// or renewable, it will ensure it's removed from the store
// if exists. This last point happens when a StorageConfig
// which was repairable/renewable get that feature disabled.
func (s *Store) Put(iid ffs.APIID, c cid.Cid, sc ffs.StorageConfig) error {
s.lock.Lock()
defer s.lock.Unlock()
scs, err := s.get(c)
if err != nil {
return fmt.Errorf("getting current storage configs for cid: %s", err)
}
idx := -1
for i := range scs {
if scs[i].IID == iid {
idx = i
break
}
}
isRepairable := sc.Repairable
isRenewable := sc.Cold.Enabled && sc.Cold.Filecoin.Renew.Enabled
// If has any of the interesting features, it should be in the slice.
if isRepairable || isRenewable {
if idx == -1 {
nsc := TrackedStorageConfig{IID: iid, StorageConfig: sc}
scs = append(scs, nsc)
} else {
scs[idx].StorageConfig = sc
}
} else { // Not interesting storage config to track, remove if present or simply return.
if idx != -1 {
scs[idx] = scs[len(scs)-1]
scs = scs[:len(scs)-1]
} else {
return nil
}
}
if err := s.persist(c, scs); err != nil {
return fmt.Errorf("persisting updated storage configs for cid: %s", err)
}
return nil
}
// Remove removes tracking the storage config from iid.
func (s *Store) Remove(iid ffs.APIID, c cid.Cid) error {
scs, err := s.get(c)
if err != nil {
return fmt.Errorf("getting current storage configs for cid: %s", err)
}
idx := -1
for i := range scs {
if scs[i].IID == iid {
idx = i
break
}
}
if idx != -1 {
scs[idx] = scs[len(scs)-1]
scs = scs[:len(scs)-1]
}
if err := s.persist(c, scs); err != nil {
return fmt.Errorf("persisting updated storage configs for cid: %s", err)
}
return nil
}
// GetRepairables returns all the tracked repairable storage configs.
func (s *Store) GetRepairables() ([]TrackedCid, error) {
s.lock.Lock()
defer s.lock.Unlock()
tcs, err := s.query(false, true)
if err != nil {
return nil, fmt.Errorf("getting repairable cids from datastore: %s", err)
}
return tcs, nil
}
// GetRenewables returns all the tracked renewable storage configs.
func (s *Store) GetRenewables() ([]TrackedCid, error) {
s.lock.Lock()
defer s.lock.Unlock()
tcs, err := s.query(true, false)
if err != nil {
return nil, fmt.Errorf("getting renewable cids from datastore: %s", err)
}
return tcs, nil
}
func (s *Store) query(renewable bool, repairable bool) ([]TrackedCid, error) {
q := query.Query{}
r, err := s.ds.Query(q)
if err != nil {
return nil, fmt.Errorf("querying persisted tracked storage configs: %s", err)
}
defer func() { _ = r.Close() }()
var res []TrackedCid
for v := range r.Next() {
if v.Error != nil {
return nil, fmt.Errorf("iterating query result: %s", v.Error)
}
var tscs []TrackedStorageConfig
if err := json.Unmarshal(v.Value, &tscs); err != nil {
return nil, fmt.Errorf("unmarshaling storage configs: %s", err)
}
cidStr := strings.TrimPrefix(v.Key, "/")
c, err := cid.Decode(cidStr)
if err != nil {
return nil, fmt.Errorf("decoding cid: %s", err)
}
tc := TrackedCid{Cid: c}
for _, tsc := range tscs {
isRepairable := tsc.StorageConfig.Repairable
isRenewable := tsc.StorageConfig.Cold.Enabled && tsc.StorageConfig.Cold.Filecoin.Renew.Enabled
if (repairable && isRepairable) || (renewable && isRenewable) {
tc.Tracked = append(tc.Tracked, tsc)
}
}
if len(tc.Tracked) > 0 {
res = append(res, tc)
}
}
return res, nil
}
func (s *Store) persist(c cid.Cid, scs []TrackedStorageConfig) error {
// If the list of storage configs resulted to be empty, then remove it from datastore.
key := datastore.NewKey(c.String())
if len(scs) == 0 {
if err := s.ds.Delete(key); err != nil {
return fmt.Errorf("deleting empty storage configs for cid: %s", err)
}
return nil
}
// Persist the updated storage config list for the cid.
buf, err := json.Marshal(scs)
if err != nil {
return fmt.Errorf("marshaling storage configs: %s", err)
}
if err := s.ds.Put(key, buf); err != nil {
return fmt.Errorf("saving storage config list in datastore: %s", err)
}
return nil
}
func (s *Store) get(c cid.Cid) ([]TrackedStorageConfig, error) {
v, err := s.ds.Get(datastore.NewKey(c.String()))
if err == datastore.ErrNotFound {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("getting tracked storage configs: %s", err)
}
var tsc []TrackedStorageConfig
if err := json.Unmarshal(v, &tsc); err != nil {
return nil, fmt.Errorf("unmarshaling tracked storage configs: %s", err)
}
return tsc, nil
}